Skip to content

Commit

Permalink
Add status --log option (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz authored Feb 27, 2024
1 parent 31c3a88 commit d8795f4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
}

Expand All @@ -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.")
}
10 changes: 10 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"})
Expand Down
10 changes: 6 additions & 4 deletions internal/stack/stack_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,24 @@ 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)
if hasDiff {
displayBranches += " " + color.Red("*")
}
}
if showLog {
displayBranches += "\n\t" + sm.gitCommands.LastLog(branch)
}

displayBranches += "\n"
}
return "Current stack: " + color.Green(data.CurrentStack) + "\nBranches:\n" + displayBranches
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/stack/stack_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestCurrentStackStatus(t *testing.T) {
gitCommands: gitCommandsStub{},
}

result := stacksManager.CurrentStackStatus()
result := stacksManager.CurrentStackStatus(false)

want := "Current stack: " +
color.Green("stack1") +
Expand Down

0 comments on commit d8795f4

Please sign in to comment.