diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-08-28 23:42:14 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-08-28 23:42:14 +0900 |
| commit | 616f5820ef3a8a20b872e8938e3274f0c329ffba (patch) | |
| tree | 2e5a04ea1537bef98f64a0e9c0dfeee058007041 /src/gitalias/git-extract-issue.go | |
| parent | 2518a661986646b3a9b3f549a3a5514571948030 (diff) | |
| download | dotfiles-616f5820ef3a8a20b872e8938e3274f0c329ffba.tar.gz dotfiles-616f5820ef3a8a20b872e8938e3274f0c329ffba.tar.zst dotfiles-616f5820ef3a8a20b872e8938e3274f0c329ffba.zip | |
git:hooks: improve githooks/commit-msg
Diffstat (limited to 'src/gitalias/git-extract-issue.go')
| -rw-r--r-- | src/gitalias/git-extract-issue.go | 38 |
1 files changed, 38 insertions, 0 deletions
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]) +} |
