From 0034cfef5cb29937c8ac7daf65a5b5bfd1925331 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 24 Feb 2020 23:09:55 +1100 Subject: show tags in commits panel --- pkg/commands/commit.go | 3 +- pkg/commands/commit_list_builder.go | 49 ++++++++++++++++++++++++++++---- pkg/commands/commit_list_builder_test.go | 10 +++---- 3 files changed, 50 insertions(+), 12 deletions(-) (limited to 'pkg') diff --git a/pkg/commands/commit.go b/pkg/commands/commit.go index 740bb5209..f60e98abd 100644 --- a/pkg/commands/commit.go +++ b/pkg/commands/commit.go @@ -61,7 +61,8 @@ func (c *Commit) GetDisplayStrings(isFocused bool) []string { if c.Action != "" { actionString = cyan.Sprint(utils.WithPadding(c.Action, 7)) + " " } else if len(c.Tags) > 0 { - tagString = utils.ColoredString(strings.Join(c.Tags, " "), color.FgMagenta) + " " + tagColor := color.New(color.FgMagenta, color.Bold) + tagString = utils.ColoredStringDirect(strings.Join(c.Tags, " "), tagColor) + " " } return []string{shaColor.Sprint(c.Sha[:8]), actionString + tagString + defaultColor.Sprint(c.Name)} diff --git a/pkg/commands/commit_list_builder.go b/pkg/commands/commit_list_builder.go index 8eee0d5ec..cf33fcac1 100644 --- a/pkg/commands/commit_list_builder.go +++ b/pkg/commands/commit_list_builder.go @@ -45,6 +45,44 @@ func NewCommitListBuilder(log *logrus.Entry, gitCommand *GitCommand, osCommand * }, nil } +// nameAndTag takes a line from a git log and extracts the sha, message and tag (if present) +// example inputs: +// 66e6369c284e96ed5af5 (tag: v0.14.4) allow fastforwarding the current branch +// 32e650e0bb3f4327749f (HEAD -> show-tags) this is my commit +// 32e650e0bb3f4327749e this is my other commit +func (c *CommitListBuilder) commitLineParts(line string) (string, string, []string) { + re := regexp.MustCompile(`(\w+) (.*)`) + shaMatch := re.FindStringSubmatch(line) + + if len(shaMatch) <= 1 { + return line, "", nil + } + sha := shaMatch[1] + rest := shaMatch[2] + + if !strings.HasPrefix(rest, "(") { + return sha, rest, nil + } + + re = regexp.MustCompile(`\((.*)\) (.*)`) + + parensMatch := re.FindStringSubmatch(rest) + if len(parensMatch) <= 1 { + return sha, rest, nil + } + + notes := parensMatch[1] + message := parensMatch[2] + re = regexp.MustCompile(`tag: ([^,]+)`) + tagMatch := re.FindStringSubmatch(notes) + if len(tagMatch) <= 1 { + return sha, message, nil + } + + tag := tagMatch[1] + return sha, message, []string{tag} +} + // GetCommits obtains the commits of the current branch func (c *CommitListBuilder) GetCommits(limit bool) ([]*Commit, error) { commits := []*Commit{} @@ -69,16 +107,15 @@ func (c *CommitListBuilder) GetCommits(limit bool) ([]*Commit, error) { // now we can split it up and turn it into commits for _, line := range utils.SplitLines(log) { - splitLine := strings.Split(line, " ") - sha := splitLine[0] + sha, name, tags := c.commitLineParts(line) _, unpushed := unpushedCommits[sha] status := map[bool]string{true: "unpushed", false: "pushed"}[unpushed] commits = append(commits, &Commit{ Sha: sha, - Name: strings.Join(splitLine[1:], " "), + Name: name, Status: status, - DisplayString: strings.Join(splitLine, " "), - // TODO: add tags here + DisplayString: line, + Tags: tags, }) } if rebaseMode != "" { @@ -288,7 +325,7 @@ func (c *CommitListBuilder) getLog(limit bool) string { limitFlag = "-30" } - result, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git log --oneline %s --abbrev=%d", limitFlag, 20)) + result, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git log --decorate --oneline %s --abbrev=%d", limitFlag, 20)) if err != nil { // assume if there is an error there are no commits yet for this branch return "" diff --git a/pkg/commands/commit_list_builder_test.go b/pkg/commands/commit_list_builder_test.go index e23bc5308..c4ded4bb9 100644 --- a/pkg/commands/commit_list_builder_test.go +++ b/pkg/commands/commit_list_builder_test.go @@ -163,7 +163,7 @@ func TestCommitListBuilderGetLog(t *testing.T) { "Retrieves logs", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"log", "--oneline", "-30", "--abbrev=20"}, args) + assert.EqualValues(t, []string{"log", "--decorate", "--oneline", "-30", "--abbrev=20"}, args) return exec.Command("echo", "6f0b32f commands/git : add GetCommits tests refactor\n9d9d775 circle : remove new line") }, @@ -175,7 +175,7 @@ func TestCommitListBuilderGetLog(t *testing.T) { "An error occurred when retrieving logs", func(cmd string, args ...string) *exec.Cmd { assert.EqualValues(t, "git", cmd) - assert.EqualValues(t, []string{"log", "--oneline", "-30", "--abbrev=20"}, args) + assert.EqualValues(t, []string{"log", "--decorate", "--oneline", "-30", "--abbrev=20"}, args) return exec.Command("test") }, func(output string) { @@ -212,7 +212,7 @@ func TestCommitListBuilderGetCommits(t *testing.T) { assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args) return exec.Command("echo") case "log": - assert.EqualValues(t, []string{"log", "--oneline", "-30", "--abbrev=20"}, args) + assert.EqualValues(t, []string{"log", "--decorate", "--oneline", "-30", "--abbrev=20"}, args) return exec.Command("echo") case "merge-base": assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args) @@ -239,7 +239,7 @@ func TestCommitListBuilderGetCommits(t *testing.T) { assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args) return exec.Command("echo", "8a2bb0e") case "log": - assert.EqualValues(t, []string{"log", "--oneline", "-30", "--abbrev=20"}, args) + assert.EqualValues(t, []string{"log", "--decorate", "--oneline", "-30", "--abbrev=20"}, args) return exec.Command("echo", "8a2bb0e commit 1\n78976bc commit 2") case "merge-base": assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args) @@ -280,7 +280,7 @@ func TestCommitListBuilderGetCommits(t *testing.T) { assert.EqualValues(t, []string{"rev-list", "@{u}..HEAD", "--abbrev-commit"}, args) return exec.Command("echo", "8a2bb0e") case "log": - assert.EqualValues(t, []string{"log", "--oneline", "-30", "--abbrev=20"}, args) + assert.EqualValues(t, []string{"log", "--decorate", "--oneline", "-30", "--abbrev=20"}, args) return exec.Command("echo", "8a2bb0e commit 1\n78976bc commit 2") case "merge-base": assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args) -- cgit v1.2.3