Skip to content

Commit a6bf2cd

Browse files
committed
cmd/gg: delete any previous refs in refs/gg-old/ during pull
Prevents unbounded growth and reduces potential for directory collisions. Also use new DeleteBranches method.
1 parent d382fc8 commit a6bf2cd

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

cmd/gg/pull.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,35 @@ func (ops *deferredFetchOps) reconcile(ctx context.Context, g *git.Git, headBran
284284
}
285285

286286
if len(ops.deletedRefs) > 0 {
287+
const oldNamespace = "refs/gg-old/"
288+
deleteOldOlds := make(map[git.Ref]git.RefMutation)
289+
for ref := range ops.localRefs {
290+
if strings.HasPrefix(string(ref), oldNamespace) {
291+
deleteOldOlds[ref] = git.DeleteRef()
292+
}
293+
}
294+
if err := g.MutateRefs(ctx, deleteOldOlds); err != nil {
295+
return err
296+
}
297+
287298
mutations := make(map[git.Ref]git.RefMutation)
288-
branchDeleteArgs := []string{"branch", "-D", "--"}
299+
var branchesToDelete []string
289300
for ref, expectHash := range ops.deletedRefs {
290301
val := expectHash.String()
291302
if branchName := ref.Branch(); branchName != "" {
292-
mutations[git.Ref("refs/gg-old/"+branchName)] = git.SetRef(val)
293-
branchDeleteArgs = append(branchDeleteArgs, branchName)
303+
mutations[git.Ref(oldNamespace+branchName)] = git.SetRef(val)
304+
branchesToDelete = append(branchesToDelete, branchName)
294305
} else {
295306
mutations[ref] = git.DeleteRefIfMatches(val)
296307
}
297308
}
298309
if err := g.MutateRefs(ctx, mutations); err != nil {
299310
return err
300311
}
301-
if err := g.Run(ctx, branchDeleteArgs...); err != nil {
312+
err := g.DeleteBranches(ctx, branchesToDelete, git.DeleteBranchOptions{
313+
Force: true,
314+
})
315+
if err != nil {
302316
return err
303317
}
304318
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ go 1.16
1818

1919
require (
2020
gg-scm.io/pkg/ghdevice v0.1.0
21-
gg-scm.io/pkg/git v0.10.0-beta1
21+
gg-scm.io/pkg/git v0.10.0
2222
github.com/google/go-cmp v0.5.7
2323
golang.org/x/sys v0.0.0-20210902050250-f475640dd07b
2424
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
3333
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3434
gg-scm.io/pkg/ghdevice v0.1.0 h1:WF7J5QPL/D9jMCmycpvb87zJceQwsNTHjU9Mn/2wyAU=
3535
gg-scm.io/pkg/ghdevice v0.1.0/go.mod h1:Cm60XpVqtWmOy5W109MLJ9KBlw25oHEliw/9ivICTQA=
36-
gg-scm.io/pkg/git v0.10.0-beta1 h1:mqyDnZmnfC0QcZ+I8z6OaJ7i9JDFpjvnYmQyQO2YW4w=
37-
gg-scm.io/pkg/git v0.10.0-beta1/go.mod h1:+3pS+T6qs44cQCl2CKvrXRT8DLErn0JJv2MDZ5E75Hg=
36+
gg-scm.io/pkg/git v0.10.0 h1:65LKWZnhy336BjeZJPm5jFIkBo8imQoKoiZygQqeKd4=
37+
gg-scm.io/pkg/git v0.10.0/go.mod h1:+3pS+T6qs44cQCl2CKvrXRT8DLErn0JJv2MDZ5E75Hg=
3838
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3939
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
4040
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

0 commit comments

Comments
 (0)