Skip to content

Commit

Permalink
docs: Update package and function level documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
HRemonen committed Sep 7, 2023
1 parent be35b66 commit 7c326fc
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
4 changes: 4 additions & 0 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ var commitCmd = &cobra.Command{
coAuthors, err = commit.PromptForCoAuthors(prompt.Prompt{
Label: "Select authors that are involded",
})
if err != nil {
fmt.Println("Prompt failed:", err)
os.Exit(1)
}
}

isBreakingChange, err := commit.PromptForBool(prompt.Prompt{
Expand Down
18 changes: 6 additions & 12 deletions pkg/author/author.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
package author

Check warning on line 1 in pkg/author/author.go

View workflow job for this annotation

GitHub Actions / lint

package-comments: should have a package comment (revive)

import (
"fmt"
"os/exec"
"regexp"
"strings"
)

func extractNameAndEmail(line string) []string {
pattern := `(\w[\w\s]+)\s+<([^>]+)>`
re := regexp.MustCompile(pattern)
return re.FindStringSubmatch(line)
}

// GetSuggestedCoAuthors retrieves a list of suggested co-authors who have made commits in the Git repository.
//
// 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() ([]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)
output, err := cmd.CombinedOutput()

if err != nil {
return nil, err
}

authorString := strings.TrimSpace(string(output))

strings.Replace(authorString, `\n`, "\n", -1)
authorString = strings.ReplaceAll(authorString, `\n`, "\n")

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

fmt.Println("suggested authors", suggestedCoAuthors)

return suggestedCoAuthors, nil
}
16 changes: 13 additions & 3 deletions pkg/commit/commit_helpers.go → pkg/commit/commit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/*
Package commit provides functionality for creating Git commits.
This file includes utility functions for interacting with Git and creating Git commits.
Copyright © 2023 HENRI REMONEN <henri@remonen.fi>
*/
package commit

import (
"fmt"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -28,7 +36,7 @@ func GetStagedFiles() ([]string, error) {
}

// CreateCommitMessage creates a commit message in the Conventional Commits format.
func CreateCommitMessage(commitInfo CommitInfo) string {
func createCommitMessage(commitInfo CommitInfo) string {
commitMessage := commitInfo.CommitType
if commitInfo.CommitScope != "" {
commitMessage += "(" + commitInfo.CommitScope + ")"
Expand All @@ -44,7 +52,6 @@ func CreateCommitMessage(commitInfo CommitInfo) string {
commitMessage += "\n\n" + commitInfo.CommitBody
}

//Add space between body and footer as per the spec
if commitInfo.IsBreakingChange {
commitMessage += "\n\n"
}
Expand All @@ -58,7 +65,10 @@ func CreateCommitMessage(commitInfo CommitInfo) string {

// CreateGitCommit creates a Git commit with the given message and files.
func CreateGitCommit(commitInfo CommitInfo, files []string) error {
commitMessage := CreateCommitMessage(commitInfo)
commitMessage := createCommitMessage(commitInfo)

fmt.Println(commitMessage, files)

commitArgs := append([]string{"commit", "-m", commitMessage}, files...)

commitGitCmd := exec.Command("git", commitArgs...) //nolint:gosec // because I do not think the users can do anything bad here
Expand Down
15 changes: 15 additions & 0 deletions pkg/commit/commit_info.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
/*
Package commit provides functionality for creating Git commits.
The CommitInfo struct in this package represents information needed for creating a Git commit. It includes fields for specifying the commit type, scope, description, body, co-authors, and breaking change details.
Usage:
- Create instances of the CommitInfo struct to define commit information.
- Set the CommitType, CommitDescription, and other fields to configure the commit.
- Optionally, add co-authors to the CoAuthors field if the commit is co-authored.
- Use the IsBreakingChange field to indicate whether the commit introduces a breaking change.
- For more information on how to use the CommitInfo struct and create Git commits, refer to the package-specific functions and commands.
Copyright © 2023 HENRI REMONEN <henri@remonen.fi>
*/
package commit

// CommitInfo represents information needed for creating a Git commit.
type CommitInfo struct {

Check warning on line 18 in pkg/commit/commit_info.go

View workflow job for this annotation

GitHub Actions / lint

exported: type name will be used as commit.CommitInfo by other packages, and that stutters; consider calling this Info (revive)
CommitType string
CommitScope string
Expand Down
12 changes: 12 additions & 0 deletions pkg/commit/prompts.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
Package commit provides functionality for creating Git commits.
This file includes utility functions for interacting with the user.
Copyright © 2023 HENRI REMONEN <henri@remonen.fi>
*/
package commit

import (
Expand Down Expand Up @@ -60,6 +67,11 @@ func PromptForMultilineString(prompt prompt.Prompt) (string, error) {
return strings.Join(lines, "\n"), nil
}

// PromptForCoAuthors displays a prompt to select or enter co-authors 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.
func PromptForCoAuthors(prompt prompt.Prompt) ([]string, error) {
suggestedCoAuthors, err := author.GetSuggestedCoAuthors()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/validators/string_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package validators

import "fmt"

// ValidateStringNotEmpty checks if a string is not empty.
func ValidateStringNotEmpty(s string) error {
if len(s) > 0 {
return nil
}
return fmt.Errorf("please enter a valid string")
}

// ValidateStringYesNo checks if a string is "yes" or "no" (case-insensitive).
func ValidateStringYesNo(s string) error {
if s == "Y" || s == "N" || s == "y" || s == "n" {
return nil
Expand Down

0 comments on commit 7c326fc

Please sign in to comment.