Skip to content

Commit

Permalink
bit --version suggestion and tab completion
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswalz committed Oct 31, 2020
1 parent 6b126d3 commit d6f98a2
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
5 changes: 4 additions & 1 deletion a.MD
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,7 @@ bit save -e

combine bit & bitcomplete into one binary?

basic usage tracking
basic usage tracking

add bit --version suggestion (add global flag suggestions)
bit update not behaving with homebrew
3 changes: 3 additions & 0 deletions cmd/bitcomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func Bitcomplete() {

gogo := &complete.Command{
Sub: completionSubCmdMap,
Flags: map[string]complete.Predictor{
"version": predict.Nothing,
},
}

gogo.Complete("bit")
Expand Down
6 changes: 1 addition & 5 deletions cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ func AllGitAliases() (cc []*cobra.Command) {
}

func PrintGitVersion() {
msg, err := execCommand("git", "--version").CombinedOutput()
if err != nil {
log.Debug().Err(err)
}
log.Debug().Msg(string(msg))
RunInTerminalWithColor("git", []string{"--version"})
}

func checkoutBranch(branch string) bool {
Expand Down
1 change: 1 addition & 0 deletions cmd/git_sub_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ func AllGitSubCommands() []*cobra.Command {
{Use: "imap-send", Short: "Send a collection of patches from stdin to an IMAP folder"},
{Use: "p4", Short: "Import from and submit to Perforce repositories"},
{Use: "fast-export", Short: "Git data exporter"},
{Use: "version", Short: "Print bit and git version"},
}
}
7 changes: 4 additions & 3 deletions cmd/rootShell.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var ShellCmd = &cobra.Command{
return
}
if bitCmdMap[subCommand] == nil {
yes := GitCommandsPromptUsed(parsedArgs, completerSuggestionMap, cmd.Version)
yes := HijackGitCommandOccurred(parsedArgs, completerSuggestionMap, cmd.Version)
if yes {
return
}
Expand Down Expand Up @@ -158,15 +158,16 @@ func RunGitCommandWithArgs(args []string) {
return
}

func GitCommandsPromptUsed(args []string, suggestionMap map[string]func() []prompt.Suggest, version string) bool {
func HijackGitCommandOccurred(args []string, suggestionMap map[string]func() []prompt.Suggest, version string) bool {
sub := args[0]
// handle checkout,switch,co commands as checkout
// if "-b" flag is not provided and branch does not exist
// user would be prompted asking whether to create a branch or not
// expected usage format
// bit (checkout|switch|co) [-b] branch-name
if args[len(args)-1] == "--version" {
if args[len(args)-1] == "--version" || args[len(args)-1] == "version" {
fmt.Println("bit version " + version)
return false
}
if isBranchChangeCommand(sub) {
branchName := ""
Expand Down
4 changes: 4 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ func CobraCommandToSuggestions(cmds []*cobra.Command) []prompt.Suggest {
Description: branch.Short,
})
}
suggestions = append(suggestions, prompt.Suggest{
Text: "--version",
Description: "Print current version of bit",
})
return suggestions
}

Expand Down
12 changes: 3 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,15 @@ func main() {

// verify is git repo
if len(os.Args) >= 2 {
if os.Args[1] == "--version" {
if os.Args[1] == "--version" || os.Args[1] == "version" {
fmt.Println("bit version " + version)
bitcmd.PrintGitVersion()
return
}
} else {

}
if !bitcmd.IsGitRepo() {
if len(os.Args) >= 2 && (os.Args[1] == "update" || os.Args[1] == "clone") {
if len(os.Args) >= 2 && (os.Args[1] == "update" || os.Args[1] == "clone" || os.Args[1] == "complete") {
// do nothing here, proceed to update path
} else if len(os.Args) == 2 && os.Args[1] == "--version" {
fmt.Println("bit version " + version)
bitcmd.PrintGitVersion()
return
} else {
fmt.Println("fatal: not a git repository (or any of the parent directories): .git")
return
Expand All @@ -73,7 +67,7 @@ func main() {
bitcli()
} else {
completerSuggestionMap, _ := bitcmd.CreateSuggestionMap(bitcmd.ShellCmd)
yes := bitcmd.GitCommandsPromptUsed(argsWithoutProg, completerSuggestionMap, version)
yes := bitcmd.HijackGitCommandOccurred(argsWithoutProg, completerSuggestionMap, version)
if yes {
return
}
Expand Down

0 comments on commit d6f98a2

Please sign in to comment.