Skip to content

Commit 71a8327

Browse files
committed
Fix default branch logic to respect specified snapshot
When a specific snapshot is requested but no branch is specified, use the snapshot's branch to maintain consistency. Only default to "main" when neither branch nor snapshot are specified. - No branch + no snapshot → default to main - No branch + snapshot specified → use snapshot's branch - Explicit branch → use specified branch
1 parent bd9559d commit 71a8327

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

engine/internal/cloning/base.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ func (c *Base) CreateClone(cloneRequest *types.CloneCreateRequest) (*models.Clon
173173
}
174174

175175
if cloneRequest.Branch == "" {
176-
cloneRequest.Branch = branching.DefaultBranch
176+
if cloneRequest.Snapshot != nil {
177+
cloneRequest.Branch = snapshot.Branch
178+
} else {
179+
cloneRequest.Branch = branching.DefaultBranch
180+
}
177181
}
178182

179183
clone := &models.Clone{

engine/internal/cloning/base_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,29 @@ func (s *BaseCloningSuite) TestLenClones() {
138138

139139
func TestDefaultBranchForCloneCreation(t *testing.T) {
140140
testCases := []struct {
141-
name string
142-
inputBranch string
143-
snapshotBranch string
144-
expectedBranch string
141+
name string
142+
inputBranch string
143+
snapshotSpecified bool
144+
snapshotBranch string
145+
expectedBranch string
145146
}{
146-
{name: "empty branch defaults to main", inputBranch: "", snapshotBranch: "dev", expectedBranch: "main"},
147-
{name: "empty branch with feature snapshot defaults to main", inputBranch: "", snapshotBranch: "feature", expectedBranch: "main"},
148-
{name: "explicit dev branch preserved", inputBranch: "dev", snapshotBranch: "main", expectedBranch: "dev"},
149-
{name: "explicit feature branch preserved", inputBranch: "feature", snapshotBranch: "main", expectedBranch: "feature"},
150-
{name: "explicit main branch preserved", inputBranch: "main", snapshotBranch: "dev", expectedBranch: "main"},
147+
{name: "no branch no snapshot defaults to main", inputBranch: "", snapshotSpecified: false, snapshotBranch: "", expectedBranch: "main"},
148+
{name: "no branch with dev snapshot uses snapshot branch", inputBranch: "", snapshotSpecified: true, snapshotBranch: "dev", expectedBranch: "dev"},
149+
{name: "no branch with feature snapshot uses snapshot branch", inputBranch: "", snapshotSpecified: true, snapshotBranch: "feature", expectedBranch: "feature"},
150+
{name: "explicit dev branch preserved", inputBranch: "dev", snapshotSpecified: false, snapshotBranch: "", expectedBranch: "dev"},
151+
{name: "explicit main branch preserved", inputBranch: "main", snapshotSpecified: true, snapshotBranch: "dev", expectedBranch: "main"},
151152
}
152153

153154
for _, tc := range testCases {
154155
t.Run(tc.name, func(t *testing.T) {
155156
request := &types.CloneCreateRequest{Branch: tc.inputBranch}
156157

157158
if request.Branch == "" {
158-
request.Branch = branching.DefaultBranch
159+
if tc.snapshotSpecified {
160+
request.Branch = tc.snapshotBranch
161+
} else {
162+
request.Branch = branching.DefaultBranch
163+
}
159164
}
160165

161166
assert.Equal(t, tc.expectedBranch, request.Branch)

0 commit comments

Comments
 (0)