Skip to content

Commit

Permalink
feat(co-author): Update the Co-Author adding functionality to streaml…
Browse files Browse the repository at this point in the history
…ine the process and allow for arbitrary unknown authors

Co-authored-by: Henri Remonen <[email protected]>
Co-authored-by: Henri Remonen <[email protected]>
  • Loading branch information
HRemonen and HRemonen committed Sep 18, 2023
1 parent 1caea0d commit 46b177e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 49 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ linters:
- typecheck
- unconvert
- unparam
- unused
- whitespace

issues:
Expand Down
18 changes: 7 additions & 11 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Copyright © 2023 HENRI REMONEN <[email protected]>
package cmd

import (
"commitsense/pkg/author"
"commitsense/pkg/commit"
"commitsense/pkg/prompt"
"commitsense/pkg/validators"
Expand All @@ -40,7 +39,9 @@ var commitCmd = &cobra.Command{
os.Exit(1)
}

commitType, err := commit.PromptCommitType()
commitType, err := commit.PromptCommitType(prompt.Prompt{
Label: "Select a commit type",
})
if err != nil {
fmt.Println("Error prompting for the commit type: ", err)
os.Exit(1)
Expand Down Expand Up @@ -73,16 +74,8 @@ var commitCmd = &cobra.Command{

var coAuthors []string
if isCoAuthored {
suggestedCoAuthors, err := author.GetSuggestedCoAuthors()
if err != nil {
fmt.Println("Error getting the suggested co-authors:", err)
os.Exit(1)
}

coAuthors, err = commit.PromptForCoAuthors(prompt.Prompt{
Label: "Select authors that are involded",
Items: suggestedCoAuthors,
CursorPos: 0,
Label: "Enter Co-Author information ",
})
if err != nil {
fmt.Println("Error prompting for the co-authors:", err)
Expand Down Expand Up @@ -116,6 +109,9 @@ var commitCmd = &cobra.Command{
fmt.Println("Error creating commit:", err)
os.Exit(1)
}

/* fmt.Println(stagedFiles)
fmt.Println(commitInfo) */
},
}

Expand Down
Binary file modified commitsense
Binary file not shown.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ require (
)

require (
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-tty v0.0.3 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect
)

require (
github.com/c-bata/go-prompt v0.2.6
github.com/chzyer/readline v1.5.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/peterh/liner v1.2.2
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.12.0 // indirect
)
29 changes: 29 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/c-bata/go-prompt v0.2.6 h1:POP+nrHE+DfLYx370bedwNhsqmpCUynWPxuHi0C5vZI=
github.com/c-bata/go-prompt v0.2.6/go.mod h1:/LMAke8wD2FsNu9EXNdHxNLbd9MedkPnCdfpU9wwHfY=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
Expand All @@ -12,12 +14,39 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-tty v0.0.3 h1:5OfyWorkyO7xP52Mq7tB36ajHDG5OHrmBGIS/DtakQI=
github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
github.com/peterh/liner v1.2.2 h1:aJ4AOodmL+JxOZZEL2u9iJf8omNRpqHc/EbrK+3mAXw=
github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiNRNwI=
github.com/pkg/term v1.2.0-beta.2 h1:L3y/h2jkuBVFdWiJvNfYfKmzcCnILw7mJWm2JQuMppw=
github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
17 changes: 3 additions & 14 deletions pkg/author/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,16 @@ Copyright © 2023 HENRI REMONEN <[email protected]>
package author

import (
"commitsense/pkg/item"
"os/exec"
"strings"
)

func getSuggestedAuthorsFromTerminalOutput(output []byte) []*item.Item {
func getSuggestedAuthorsFromTerminalOutput(output []byte) []string {
authorString := strings.TrimSpace(string(output))
authorString = strings.ReplaceAll(authorString, `\n`, "\n")

// Parse the output to extract author names and email addresses.
coAuthors := strings.Split(authorString, "\n")

var suggestedCoAuthors []*item.Item
for _, coAuthor := range coAuthors {
items := []*item.Item{
{
ID: coAuthor,
},
}
suggestedCoAuthors = append(suggestedCoAuthors, items...)
}
suggestedCoAuthors := strings.Split(authorString, "\n")

return suggestedCoAuthors
}
Expand All @@ -43,7 +32,7 @@ func getSuggestedAuthorsFromTerminalOutput(output []byte) []*item.Item {
// This function uses the `git log` command to obtain a list of authors who have made commits in the Git repository.
// It executes the command and processes the output to extract author names and email addresses. The resulting
// list represents suggested co-authors for Git commits.
func GetSuggestedCoAuthors() ([]*item.Item, error) {
func GetSuggestedCoAuthors() ([]string, error) {
// Use the `git rev-list` command to obtain a list of authors who have made commits in the Git repository.
revlist := "git log --pretty='%an <%ae>' | sort -u"
cmd := exec.Command("bash", "-c", revlist)
Expand Down
80 changes: 57 additions & 23 deletions pkg/commit/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ Copyright © 2023 HENRI REMONEN <[email protected]>
package commit

import (
"commitsense/pkg/author"
"commitsense/pkg/item"
"commitsense/pkg/prompt"
"fmt"
"os"
"strings"

p "github.com/c-bata/go-prompt"
"github.com/manifoldco/promptui"
)

// PromptCommitType prompts the user to select a commit type.
func PromptCommitType() (string, error) {
func PromptCommitType(prompt prompt.Prompt) (string, error) {
promptItems := []string{"feat", "fix", "chore", "docs", "style", "refactor", "perf", "test", "build", "ci"}

promptType := promptui.Select{
Label: "Select a commit type",
Items: []string{"feat", "fix", "chore", "docs", "style", "refactor", "perf", "test", "build", "ci"},
Label: prompt.Label,
Items: promptItems,
}

_, typeResult, err := promptType.Run()

return typeResult, err
}

Expand Down Expand Up @@ -68,12 +75,12 @@ func PromptForMultilineString(prompt prompt.Prompt) (string, error) {
return strings.Join(lines, "\n"), nil
}

// Append the prompt.Items with the continue item
func appendMultiplePromptWithContinue(items []*item.Item) []*item.Item {
const continueItem = "Continue"
if len(items) > 0 && items[0].ID != continueItem {
items = append([]*item.Item{{ID: continueItem}}, items...)
}
// Prepend the prompt.Items with the continue item
func prependItemsWithSpecialOptions(items []*item.Item) []*item.Item {
continueItem := &item.Item{ID: "Continue"}

items = append([]*item.Item{continueItem}, items...)

return items
}

Expand All @@ -85,8 +92,8 @@ func createSelectTemplates() *promptui.SelectTemplates {
}
}

func promptForMultiple(prompt prompt.Prompt) ([]*item.Item, error) {
promptItems := appendMultiplePromptWithContinue(prompt.Items)
func promptForMultipleItems(prompt prompt.Prompt) ([]*item.Item, error) {
promptItems := prependItemsWithSpecialOptions(prompt.Items)

promptMultiple := promptui.Select{
Label: prompt.Label,
Expand All @@ -105,7 +112,7 @@ func promptForMultiple(prompt prompt.Prompt) ([]*item.Item, error) {
return nil, fmt.Errorf("prompt failed: %w", err)
}

chosenItem := promptItems[selectionIdx]
chosenItem := prompt.Items[selectionIdx]

if chosenItem.ID == "Continue" {
break
Expand All @@ -118,7 +125,7 @@ func promptForMultiple(prompt prompt.Prompt) ([]*item.Item, error) {
}

// Collect selected items.
for _, item := range promptItems {
for _, item := range prompt.Items {
if item.IsSelected {
selectedItems = append(selectedItems, item)
}
Expand All @@ -127,21 +134,48 @@ func promptForMultiple(prompt prompt.Prompt) ([]*item.Item, error) {
return selectedItems, nil
}

// PromptForCoAuthors displays a prompt to select or enter co-authors for a Git commit.
func coAuthorCompleter(suggestedCoAuthors []string) p.Completer { // Use "p" as the alias
return func(d p.Document) []p.Suggest {
s := []p.Suggest{}
t := d.TextBeforeCursor()
for _, coAuthor := range suggestedCoAuthors {
if strings.HasPrefix(coAuthor, t) {
s = append(s, p.Suggest{Text: coAuthor})
}
}
return p.FilterHasPrefix(s, t, true)
}
}

// PromptForCoAuthors displays a prompt to enter co-author names for a Git commit.
//
// This function retrieves a list of suggested co-authors using the GetSuggestedCoAuthors function
// from the author package. It then presents the user with a selectable list of suggested co-authors
// and allows them to choose from the suggestions or enter custom co-authors.
// This function provides real-time auto-completion suggestions based on the suggestedCoAuthors
// list. Users can choose from the suggestions or enter custom co-authors. It returns a slice
// of selected co-author names.
func PromptForCoAuthors(prompt prompt.Prompt) ([]string, error) {
selectedAuthorPtrs, err := promptForMultiple(prompt)
suggestedCoAuthors, err := author.GetSuggestedCoAuthors()
if err != nil {
return nil, err
fmt.Println("Error getting the suggested co-authors:", err)
os.Exit(1)
}

var authorResult []string
for _, file := range selectedAuthorPtrs {
authorResult = append(authorResult, file.ID)
fmt.Println("Enter Co-authors:")
fmt.Println("Press 'Tab' to auto-complete.")

pr := p.New(
func(_ string) { /* No-op executor */ },
coAuthorCompleter(suggestedCoAuthors),
p.OptionPrefix(prompt.Label),
)

coAuthors := []string{}
for {
coAuthor := pr.Input()
if coAuthor == "" {
break
}
coAuthors = append(coAuthors, coAuthor)
}

return authorResult, nil
return coAuthors, nil
}

0 comments on commit 46b177e

Please sign in to comment.