Avoid deleting pre-existing remote branch on trail create cleanup · Entire
Avoid deleting pre-existing remote branch on trail create cleanup
57a22a3→main·
dipree·1mo ago·1 file·+26 added/-1 removed
Sessions
c44bd5a22885View transcript
[?
Fix Trail Command Behavior and API AlignmentPi·GPT-5.5·2 steps](/content/gh/entireio/cli/session/019ed4cf-858e-70be-9fde-ff8ea73b50bc#timeline-c44bd5a22885/index.html)
Changes
1
cmd/entire/cli
Mtrail_cmd.go+26/-1
644 unmodified lines
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
652
663
664
665
666
554 unmodified lines
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
644 unmodified lines
}
if needsCreation {
// needsCreation is a local-only signal, so the branch may still exist on
// origin (e.g. a teammate pushed it and we never fetched). In that case the
// remote branch is not ours, and our push may merely fast-forward it; we
// must not delete it during cleanup. Only treat the remote branch as
// created-by-us when it did not already exist before our push.
existedOnOrigin, existErr := branchExistsOnOrigin(branch)
if existErr != nil {
// Be conservative: if we cannot tell, do not delete the remote branch.
fmt.Fprintf(errW, "Warning: could not check whether branch %s already exists on origin: %v\n", branch, existErr)
existedOnOrigin = true
}
if err := pushBranchToOrigin(branch); err != nil {
cleanupCreatedTrailBranch(repo, branch, localBranchCreated, false, errW)
return fmt.Errorf("failed to push branch %q: %w", branch, err)
}
remoteBranchPushed = true
remoteBranchPushed = !existedOnOrigin
fmt.Fprintf(w, "Pushed branch %s to origin\n", branch)
}
554 unmodified lines
return nil
}
// branchExistsOnOrigin reports whether origin already has a branch with the
// given name, so callers can avoid treating a pre-existing remote branch as one
// they created.
func branchExistsOnOrigin(branchName string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--heads", "origin", branchName)
output, err := cmd.CombinedOutput()
if err != nil {
return false, fmt.Errorf("%s: %w", strings.TrimSpace(string(output)), err)
}
return strings.TrimSpace(string(output)) != "", nil
}
func deleteBranchFromOrigin(branchName string) error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()