diff options
| -rw-r--r-- | .config/git/config | 1 | ||||
| -rwxr-xr-x | githooks/commit-msg | 6 | ||||
| -rwxr-xr-x | setup.sh | 2 | ||||
| -rw-r--r-- | src/gitalias/git-extract-issue.go | 38 |
4 files changed, 44 insertions, 3 deletions
diff --git a/.config/git/config b/.config/git/config index 16932ce..36c133e 100644 --- a/.config/git/config +++ b/.config/git/config @@ -144,3 +144,4 @@ echo \"unknown user: $1\" >&2; \ fi; \ }; __fn" + extract-issue = "!~/bin/gitalias/git-extract-issue" diff --git a/githooks/commit-msg b/githooks/commit-msg index 67b883f..c25b0a0 100755 --- a/githooks/commit-msg +++ b/githooks/commit-msg @@ -125,12 +125,12 @@ if [[ -z ${gitbranch} ]]; then fi # Extract issue number from branch name. -local issue_number="$(echo "${gitbranch}" | grep -o '[0-9][0-9]*' | head -1)" +local issue_number="$(git extract-issue "${gitbranch}")" if [[ -z ${issue_number} ]]; then # Failed to extract. Leave the commit message as it is. return 0 fi -# Substitute '###' with '#NNNNN', where 'NNNNN' is the issue number. +# Substitute '###'. # '$1' is passed by Git, the file name that holds commit message. -sed -i '' -e "s|###|#${issue_number}|g" "${1}" +sed -i '' -e "s|###|${issue_number}|g" "${1}" @@ -73,6 +73,7 @@ done # Golang: {{{2 for name in \ + gitalias/git-extract-issue \ gitalias/git-sw \ ; \ do @@ -99,6 +100,7 @@ fi # Make symlinks to utility scripts. {{{2 for name in \ tmux-pane-idx \ + gitalias/git-extract-issue \ gitalias/git-sw \ ; \ do diff --git a/src/gitalias/git-extract-issue.go b/src/gitalias/git-extract-issue.go new file mode 100644 index 0000000..165aa58 --- /dev/null +++ b/src/gitalias/git-extract-issue.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "os" + "regexp" +) + +func main() { + argv := os.Args + argc := len(argv) + if argc != 2 { + return + } + branchName := argv[1] + fmt.Println(extractIssueNumberFromBranchName(branchName)) +} + +// * 123 => #123 +// * 123-suffix => #123 +// * feature/123 => #123 +// * feature/123-suffix => #123 +// * feature/prefix-123 => prefix-123 +// * feature/prefix-123-suffix => prefix-123 +func extractIssueNumberFromBranchName(branchName string) string { + pattern := regexp.MustCompile(`\A(?:\w+/)?(\w+-)?(\d+)(?:-\w+)?\z`) + matches := pattern.FindSubmatch([]byte(branchName)) + if len(matches) != 3 { + return "" + } + var prefix string + if len(matches[1]) == 0 { + prefix = "#" + } else { + prefix = string(matches[1]) + } + return prefix + string(matches[2]) +} |
