diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-04-27 04:40:06 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-04-27 04:40:06 +0900 |
| commit | 0d5efdd6ddee49776cbf9a0826f7543e72a51fc5 (patch) | |
| tree | 13f246097c13464ff9e9259d2265b8fd06ef85cd /cmd/git-extract-issue | |
| parent | a6e09ce751165c78d1cdc5b9a75d94944fcc5326 (diff) | |
| download | git-helpers-0d5efdd6ddee49776cbf9a0826f7543e72a51fc5.tar.gz git-helpers-0d5efdd6ddee49776cbf9a0826f7543e72a51fc5.tar.zst git-helpers-0d5efdd6ddee49776cbf9a0826f7543e72a51fc5.zip | |
initial commitv0.1.0
Diffstat (limited to 'cmd/git-extract-issue')
| -rw-r--r-- | cmd/git-extract-issue/main.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/cmd/git-extract-issue/main.go b/cmd/git-extract-issue/main.go new file mode 100644 index 0000000..4681d4d --- /dev/null +++ b/cmd/git-extract-issue/main.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]) +} |
