Skip to content

Commit 38c5608

Browse files
respect branchNamePrefix config in all branch creation paths (#653)
The branchNamePrefix config setting was only being used when auto-generating branch names via `av commit -b`, but not when using `av branch` directly or `av branch --split` to split commits. Now the prefix is applied consistently across all branch creation commands by centralizing the logic in a single `applyBranchNamePrefix` helper function. When branches are created (whether explicitly named or auto-generated), the configured prefix gets prepended. ## Changes - Added `applyBranchNamePrefix()` helper function to centralize prefix logic - Applied prefix in `createBranch()` for `av branch <name>` - Applied prefix in `branchSplit()` for `av branch --split` - Removed duplicate prefix logic from `branchNameFromMessage()` in commit.go - Updated test repos to explicitly set empty branchNamePrefix to avoid interference from global config ## Testing - All existing tests pass - Added comprehensive test coverage for branch name prefix behavior across all creation paths
1 parent 6ca7438 commit 38c5608

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

cmd/av/branch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"emperror.dev/errors"
1010
"github.com/aviator-co/av/internal/actions"
11+
"github.com/aviator-co/av/internal/config"
1112
"github.com/aviator-co/av/internal/git"
1213
"github.com/aviator-co/av/internal/meta"
1314
"github.com/aviator-co/av/internal/utils/cleanup"
@@ -181,6 +182,9 @@ func branchSplit(
181182
newBranchName = sanitizeBranchName(lastCommit.Message)
182183
}
183184

185+
// Apply branch name prefix
186+
newBranchName = applyBranchNamePrefix(newBranchName)
187+
184188
// Get the parent of the last commit (HEAD~1)
185189
parentCommitIter := lastCommit.Parents()
186190
parentCommit, err := parentCommitIter.Next()
@@ -223,6 +227,14 @@ func branchSplit(
223227
return nil
224228
}
225229

230+
// applyBranchNamePrefix applies the configured branch name prefix to a branch name.
231+
func applyBranchNamePrefix(branchName string) string {
232+
if config.Av.PullRequest.BranchNamePrefix != "" {
233+
return config.Av.PullRequest.BranchNamePrefix + branchName
234+
}
235+
return branchName
236+
}
237+
226238
// sanitizeBranchName creates a valid branch name from a string.
227239
func sanitizeBranchName(input string) string {
228240
sanitized := strings.ToLower(strings.TrimSpace(input))
@@ -266,6 +278,9 @@ func createBranch(
266278
branchName string,
267279
parentBranchName string,
268280
) (reterr error) {
281+
// Apply branch name prefix
282+
branchName = applyBranchNamePrefix(branchName)
283+
269284
// Determine important contextual information from Git
270285
// or if a parent branch is provided, check it allows as a default branch
271286
defaultBranch := repo.DefaultBranch()

cmd/av/commit.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"emperror.dev/errors"
1111
"github.com/aviator-co/av/internal/actions"
12-
"github.com/aviator-co/av/internal/config"
1312
"github.com/aviator-co/av/internal/git"
1413
"github.com/aviator-co/av/internal/meta"
1514
"github.com/aviator-co/av/internal/utils/cleanup"
@@ -335,9 +334,6 @@ func branchNameFromMessage(message string) string {
335334
name = name[:branchNameLength]
336335
}
337336
name = strings.ToLower(name)
338-
if config.Av.PullRequest.BranchNamePrefix != "" {
339-
name = fmt.Sprintf("%s%s", config.Av.PullRequest.BranchNamePrefix, name)
340-
}
341337
return name
342338
}
343339

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package e2e_tests
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/aviator-co/av/internal/git/gittest"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
// setupRepoWithPrefix creates a test repository with the specified branch name prefix.
14+
func setupRepoWithPrefix(t *testing.T, prefix string) *gittest.GitTestRepo {
15+
t.Helper()
16+
repo := gittest.NewTempRepo(t)
17+
Chdir(t, repo.RepoDir)
18+
19+
configDir := filepath.Join(repo.RepoDir, ".git", "av")
20+
require.NoError(t, os.MkdirAll(configDir, 0o755))
21+
configFile := filepath.Join(configDir, "config.yml")
22+
configContent := fmt.Sprintf("pullRequest:\n branchNamePrefix: %q\n", prefix)
23+
require.NoError(t, os.WriteFile(configFile, []byte(configContent), 0o644))
24+
25+
return repo
26+
}
27+
28+
func TestBranchNamePrefixWithCommit(t *testing.T) {
29+
repo := setupRepoWithPrefix(t, "user/myname/")
30+
31+
// Create a branch using av commit -b
32+
repo.CreateFile(t, "test.txt", "test content")
33+
repo.AddFile(t, "test.txt")
34+
RequireAv(t, "commit", "-b", "-m", "test branch creation")
35+
36+
// Verify the branch name has the prefix
37+
currentBranch := repo.Git(t, "branch", "--show-current")
38+
require.Equal(t, "user/myname/test-branch-creation\n", currentBranch)
39+
}
40+
41+
func TestBranchNamePrefixWithSplit(t *testing.T) {
42+
repo := setupRepoWithPrefix(t, "feature/")
43+
44+
// Create a branch and make two commits
45+
RequireAv(t, "branch", "base-branch")
46+
repo.CommitFile(t, "file1.txt", "content1")
47+
repo.CommitFile(t, "file2.txt", "content2")
48+
49+
// Split the last commit
50+
RequireAv(t, "branch", "--split")
51+
52+
// Verify the new branch name has the exact expected prefix and sanitized name
53+
currentBranch := repo.Git(t, "branch", "--show-current")
54+
require.Equal(t, "feature/write-file2-txt\n", currentBranch)
55+
}
56+
57+
func TestBranchNamePrefixWithExplicitName(t *testing.T) {
58+
repo := setupRepoWithPrefix(t, "will/")
59+
60+
// Create a branch with explicit name using av branch
61+
RequireAv(t, "branch", "test-branch")
62+
63+
// Verify the branch name has the prefix applied
64+
currentBranch := repo.Git(t, "branch", "--show-current")
65+
require.Equal(t, "will/test-branch\n", currentBranch)
66+
}
67+
68+
func TestBranchNamePrefixWithSplitExplicitName(t *testing.T) {
69+
repo := setupRepoWithPrefix(t, "user/dev/")
70+
71+
// Create a branch and make two commits
72+
RequireAv(t, "branch", "base-branch")
73+
repo.CommitFile(t, "file1.txt", "content1")
74+
repo.CommitFile(t, "file2.txt", "content2")
75+
76+
// Split the last commit with explicit name
77+
// Now the prefix SHOULD be applied to explicit names too for consistency
78+
RequireAv(t, "branch", "--split", "my-explicit-branch")
79+
80+
// Verify the new branch name has the prefix applied
81+
currentBranch := repo.Git(t, "branch", "--show-current")
82+
require.Equal(t, "user/dev/my-explicit-branch\n", currentBranch)
83+
}
84+
85+
func TestBranchNamePrefixEmpty(t *testing.T) {
86+
repo := setupRepoWithPrefix(t, "")
87+
88+
repo.CreateFile(t, "test.txt", "test content")
89+
repo.AddFile(t, "test.txt")
90+
RequireAv(t, "commit", "-b", "-m", "test without prefix")
91+
92+
// Verify the branch name has no prefix
93+
currentBranch := repo.Git(t, "branch", "--show-current")
94+
require.Equal(t, "test-without-prefix\n", currentBranch)
95+
}

internal/git/gittest/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func NewTempRepoWithGitHubServer(t *testing.T, serverURL string) *GitTestRepo {
105105
github:
106106
token: "dummy_valid_token"
107107
baseUrl: %q
108+
pullRequest:
109+
branchNamePrefix: ""
108110
`, serverURL),
109111
0o644,
110112
)

0 commit comments

Comments
 (0)