Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,13 @@ func getDefaultBranch(remote *Remote) (string, error) {
ref := strings.TrimSpace(string(output))
prefix := fmt.Sprintf("refs/remotes/%s/", remote.Name)
if strings.HasPrefix(ref, prefix) && refExists(ref) {
return strings.TrimPrefix(ref, prefix), nil
name := strings.TrimPrefix(ref, prefix)
// The remote chooses its HEAD, and ref names may start with
// "-"; git checkout would read such a name as an option.
if strings.HasPrefix(name, "-") {
return "", fmt.Errorf("default branch name '%s' starts with '-'", name)
}
return name, nil
}
}

Expand Down
19 changes: 19 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,25 @@ func TestRunIgnoresDanglingRemoteHead(t *testing.T) {
}
}

// TestRunRejectsDashDefaultBranch checks that a remote HEAD naming a branch
// that starts with "-" stops gh-sync before anything is synced: the name
// would reach `git checkout` as an option (-f discards local changes).
func TestRunRejectsDashDefaultBranch(t *testing.T) {
f := newSyncFixture(t)
f.gitIn(f.remote, "update-ref", "refs/heads/-f", "refs/heads/main")
f.localGit("fetch", "--quiet", "origin")
f.localGit("symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/-f")
before := f.sha(f.local, "main")
f.advanceUpstream("main", "upstream-1")

res := f.sync()
if res.err == nil {
t.Fatal("run() error = nil, want an error")
}
assertContains(t, "stderr", res.stderr, "default branch name '-f' starts with '-'")
f.assertBranchAt("main", before)
}

// TestRunPartialClone checks that a sync completes in a partial clone (#56).
func TestRunPartialClone(t *testing.T) {
f := newSyncFixture(t)
Expand Down
Loading