diff options
| author | Anton Medvedev <anton@medv.io> | 2025-12-07 16:16:27 +0100 |
|---|---|---|
| committer | Anton Medvedev <anton@medv.io> | 2025-12-07 16:16:27 +0100 |
| commit | 20a5d07b2a4620ace14c6a5f622ca45e27f9c20d (patch) | |
| tree | 600c05eb8ac613576d6ed9e8203522c8d02eb958 | |
| parent | 484203dc66ad90f38cd990a17898d51ca39c8740 (diff) | |
fix/make-branch-safe
| -rw-r--r-- | utils.go | 14 | ||||
| -rw-r--r-- | utils_test.go | 31 |
2 files changed, 45 insertions, 0 deletions
@@ -113,3 +113,17 @@ func containsBranch(branches []git.Ref, branch string) bool { } return false } + +func refToFileName(ref git.Ref) string { + var result strings.Builder + for _, c := range string(ref) { + if (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.' { + result.WriteByte(byte(c)) + } else if c >= 'A' && c <= 'Z' { + result.WriteByte(byte(c - 'A' + 'a')) + } else { + result.WriteByte('-') + } + } + return result.String() +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..f883e1b --- /dev/null +++ b/utils_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "testing" + + "github.com/antonmedv/gitmal/pkg/git" +) + +func TestRefToFileName(t *testing.T) { + tests := []struct { + in string + want string + }{ + {"main", "main"}, + {"master", "master"}, + {"release/v1.0", "release-v1.0"}, + {"feature/add-login", "feature-add-login"}, + {"bugfix\\windows\\path", "bugfix-windows-path"}, + {"1.0.0", "1.0.0"}, + {"1.x", "1.x"}, + } + + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + got := refToFileName(git.Ref(tt.in)) + if got != tt.want { + t.Fatalf("refToFileName(%q) = %q, want %q", tt.in, got, tt.want) + } + }) + } +} |
