From d8795f4aac747cda25b19b1b029bbaa0ddcb06ce Mon Sep 17 00:00:00 2001 From: Jean-Francis Bastien Date: Tue, 27 Feb 2024 14:53:28 -0500 Subject: [PATCH] Add status --log option (#5) --- cmd/status.go | 4 +++- internal/git/git.go | 10 ++++++++++ internal/stack/stack_manager.go | 10 ++++++---- internal/stack/stack_manager_test.go | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/status.go b/cmd/status.go index 5eaed34..d8c6ac7 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -14,7 +14,8 @@ var statusCmd = &cobra.Command{ Use: "status", Short: "Get current stack.", Run: func(cmd *cobra.Command, args []string) { - fmt.Println(stack.Manager().CurrentStackStatus()) + showLogValue, _ := cmd.Flags().GetBool("log") + fmt.Println(stack.Manager().CurrentStackStatus(showLogValue)) }, } @@ -30,4 +31,5 @@ func init() { // Cobra supports local flags which will only run when this command // is called directly, e.g.: // statusCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + statusCmd.Flags().BoolP("log", "l", false, "Show last commit log for each branch in the stack.") } diff --git a/internal/git/git.go b/internal/git/git.go index 2ded761..e8d18ee 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -33,6 +33,7 @@ type InterfaceCommands interface { Checkout(branchName string) SyncBranches(branches []string, checkoutBranchEnd string, push bool) BranchDiff(baseBranch string, branch string) bool + LastLog(branch string) string } type Commands struct { @@ -128,6 +129,15 @@ func (c Commands) BranchDiff(baseBranch string, branch string) bool { return len(output) > 0 } +func (c Commands) LastLog(branch string) string { + output, err := c.exec([]string{"log", "--pretty=format:%s - %Cred%h%Creset - %C(bold blue)%an%Creset - %Cgreen%cr%Creset", "-n", "1", branch}) + if err != nil { + fmt.Println(err) + return "" + } + return output +} + func (c Commands) pushBranch(branchName string) { fmt.Println("Pushing", color.Yellow(branchName), "...") _, err := c.exec([]string{"push"}) diff --git a/internal/stack/stack_manager.go b/internal/stack/stack_manager.go index e8b4e75..f1f080f 100644 --- a/internal/stack/stack_manager.go +++ b/internal/stack/stack_manager.go @@ -47,15 +47,13 @@ func (sm StacksManager) CreateStack(stackName string) string { return "CreateStack stack created " + color.Green(stackName) } -func (sm StacksManager) CurrentStackStatus() string { +func (sm StacksManager) CurrentStackStatus(showLog bool) string { data := sm.load() var displayBranches string branches, _ := data.GetBranchesByName(data.CurrentStack) var previousBranch string for i, branch := range branches { - // Maybe someday it will be nice to add - // git log --pretty=format:'%s - %Cred%h%Creset %C(bold blue)%an%Creset %Cgreen%cr%Creset' -n 1 master displayBranches += fmt.Sprintf("%d. "+color.Yellow(branch), i+1) if i > 0 && previousBranch != branch { hasDiff := sm.gitCommands.BranchDiff(previousBranch, branch) @@ -63,6 +61,10 @@ func (sm StacksManager) CurrentStackStatus() string { displayBranches += " " + color.Red("*") } } + if showLog { + displayBranches += "\n\t" + sm.gitCommands.LastLog(branch) + } + displayBranches += "\n" } return "Current stack: " + color.Green(data.CurrentStack) + "\nBranches:\n" + displayBranches @@ -164,7 +166,7 @@ func (sm StacksManager) Delete(stackName string) { sm.stacksPersister.SaveStacks(data) fmt.Println("Stack", stackName, "deleted from stack") - fmt.Println(sm.CurrentStackStatus()) + fmt.Println(sm.CurrentStackStatus(false)) } func (sm StacksManager) Sync(push bool) { diff --git a/internal/stack/stack_manager_test.go b/internal/stack/stack_manager_test.go index 204d5fb..b282859 100644 --- a/internal/stack/stack_manager_test.go +++ b/internal/stack/stack_manager_test.go @@ -62,7 +62,7 @@ func TestCurrentStackStatus(t *testing.T) { gitCommands: gitCommandsStub{}, } - result := stacksManager.CurrentStackStatus() + result := stacksManager.CurrentStackStatus(false) want := "Current stack: " + color.Green("stack1") +