Skip to content

Commit

Permalink
added git cherry-pick
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 23, 2020
1 parent 055b635 commit 3ee4bbf
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 34 deletions.
54 changes: 54 additions & 0 deletions completers/git_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,57 @@ func ActionFieldNames() carapace.Action {
"version:refname", "sort by versions",
)
}

func ActionCleanupMode() carapace.Action {
return carapace.ActionValuesDescribed(
"strip", "strip empty lines and trailing whitespace",
"whitespace", "same as strip except #commentary is not removed",
"verbatim", "do not change the message at all",
"scissors", "same as whitespace except that everything from (and including) the line found below is truncated",
"default", " Same as strip if the message is to be edited. Otherwise whitespace",
)
}

func ActionGpgKeyIds() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("sh", "-c", "gpg --list-keys --with-colons | awk -F: '/^pub:|^uid:/ { print $5 $10}'").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
lines := strings.Split(string(output), "\n")
return carapace.ActionValuesDescribed(lines[:len(lines)-1]...)
}
})
}

func ActionMergeStrategy() carapace.Action {
return carapace.ActionValuesDescribed(
"octopus", "resolve cases with more than two heads",
"ours", "auto-resolve cleanly by favoring our version",
"recursive", "recursively resolve two heads using a 3-way merge algorithm",
"resolve", "resolve two heads using a 3-way merge algorithm",
"subtree", "modified recursive straty with tree adjustment",
)
}

func ActionMergeStrategyOptions(strategy string) carapace.Action {
switch strategy {
case "recursive":
return carapace.ActionValuesDescribed(
"ours", "auto-resolve favoring ours",
"theirs", "auto-resolve favoring theirs",
"patience", "spend extra time to avoid mismerges",
"diff-algorithm=", "set dif allgorithm",
"ignore-space-change", "ignore space change",
"ignore-all-space", "ignore all space",
"ignore-space-at-eol", "ignore <space> at end of line",
"ignore-cr-at-eol", "ignore <cr> at end of line",
"renormalize", "enable renormalize",
"no-renormalize", "disable renormalize",
"no-renames", "turn off rename detection",
"find-renames", "turn on rename detection",
"subtree", "advance subtree stratebgy",
)
default:
return carapace.ActionValues()
}
}
61 changes: 61 additions & 0 deletions completers/git_completer/cmd/cherryPick.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/git_completer/cmd/action"
"github.com/spf13/cobra"
)

var cherryPickCmd = &cobra.Command{
Use: "cherry-pick",
Short: "Apply the changes introduced by some existing commits",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(cherryPickCmd).Standalone()

cherryPickCmd.Flags().BoolS("x", "x", false, "append cherry-pick source to original commit message")
cherryPickCmd.Flags().Bool("abort", false, "cancel the operation and return to the pre-sequence state")
cherryPickCmd.Flags().Bool("allow-empty", false, "preserve empty commits")
cherryPickCmd.Flags().Bool("allow-empty-message", false, "allow empty commit message")
cherryPickCmd.Flags().String("cleanup", "", "set commit message cleanup mode")
cherryPickCmd.Flags().Bool("continue", false, "ontinue the operation in progress")
cherryPickCmd.Flags().BoolP("edit", "e", false, "edit the commit message prior to committing")
cherryPickCmd.Flags().Bool("ff", false, "fast forward")
cherryPickCmd.Flags().StringP("gpg-sign", "S", "", "GPG-sign commits")
cherryPickCmd.Flags().Bool("keep-redundant-commits", false, "preserve redundant commits")
cherryPickCmd.Flags().StringP("mainline", "m", "", "specify parent number to pick relative change")
cherryPickCmd.Flags().BoolP("no-commit", "n", false, "apply changes without committing them")
cherryPickCmd.Flags().Bool("no-gpg-sign", false, "do not GPG-sign commits")
cherryPickCmd.Flags().Bool("no-rerere-autoupdate", false, "disallow rerere mechanism to update the index")
cherryPickCmd.Flags().Bool("quit", false, "forget about the current operation in progress")
cherryPickCmd.Flags().Bool("rerere-autoupdate", false, "allow rerere mechanism to update the index")
cherryPickCmd.Flags().BoolP("signoff", "s", false, "add Signed-off-by line at the end of the commit message")
cherryPickCmd.Flags().Bool("skip", false, "skip the current commit")
cherryPickCmd.Flags().String("strategy", "", "use the given merge strategy")
cherryPickCmd.Flags().StringP("strategy-option", "X", "", "pass the merge strategy-specific option through to the merge strategy")
rootCmd.AddCommand(cherryPickCmd)

cherryPickCmd.Flag("gpg-sign").NoOptDefVal = " "

carapace.Gen(cherryPickCmd).FlagCompletion(carapace.ActionMap{
"cleanup": action.ActionCleanupMode(),
"gpg-sign": action.ActionGpgKeyIds(),
"mainline": carapace.ActionValues("1", "2", "3", "4", "5"),
"strategy": action.ActionMergeStrategy(),
"strategy-option": carapace.ActionCallback(func(args []string) carapace.Action {
return action.ActionMergeStrategyOptions(cherryPickCmd.Flag("strategy").Value.String())
}),
})

carapace.Gen(cherryPickCmd).PositionalAnyCompletion(
// TODO `...` divider not yet working
carapace.ActionMultiParts("...", func(args, parts []string) carapace.Action {
if len(parts) < 2 {
return action.ActionRefs(action.RefOptionDefault)
}
return carapace.ActionValues()
}),
)
}
34 changes: 0 additions & 34 deletions completers/git_completer/cmd/cherry_pick_generated.go

This file was deleted.

0 comments on commit 3ee4bbf

Please sign in to comment.