Commit 20a5d07

Anton Medvedev <anton@medv.io>
2025-12-07 15:16:27
fix/make-branch-safe
1 parent 484203d
Changed files (2)
utils.go
@@ -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()
+}
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)
+			}
+		})
+	}
+}