diff --git a/main.go b/main.go index d40ecd2..2d22432 100644 --- a/main.go +++ b/main.go @@ -271,6 +271,10 @@ func run(args []string, findMergedPRs mergedPRFinder) error { printError(cfg.stderrColors, "%s", err.Error()) return err } + if err := checkRemoteName(remote.Name); err != nil { + printError(cfg.stderrColors, "fatal: %s", err.Error()) + return err + } currentBranch := "" if branch, err := getCurrentBranch(); err == nil { @@ -367,8 +371,12 @@ func run(args []string, findMergedPRs mergedPRFinder) error { // fetchRemote runs `git fetch --prune` for name. --progress only shows // something when git's stderr reaches the user, so stderr is passed through // when it is a terminal. Otherwise it is captured, so a failure is reported -// once, inside the returned error. +// once, inside the returned error. A name checkRemoteName rejects is not +// passed to git. func fetchRemote(cfg runConfig, name string, stderrIsTerminal bool) error { + if err := checkRemoteName(name); err != nil { + return err + } args := []string{"fetch", "--prune", "--quiet", "--progress", name} if stderrIsTerminal { return runGitStderr(cfg, args...) @@ -376,6 +384,16 @@ func fetchRemote(cfg runConfig, name string, stderrIsTerminal bool) error { return runGitSilent(cfg, args...) } +// checkRemoteName rejects a remote name git would read as an option: git +// allows names such as "--upload-pack=", and `git fetch` given one +// runs that command. --end-of-options would need git 2.24. +func checkRemoteName(name string) error { + if strings.HasPrefix(name, "-") { + return fmt.Errorf("remote name '%s' starts with '-'", name) + } + return nil +} + // trackedRemotes returns, sorted, the remotes other than main that some // local branch tracks. Values of branch..remote that are not a // configured remote (such as "." for a local upstream) are left out. diff --git a/main_test.go b/main_test.go index f5fcfe9..284ec7e 100644 --- a/main_test.go +++ b/main_test.go @@ -1327,6 +1327,72 @@ func TestRunRejectsInvalidOptions(t *testing.T) { } } +// TestRunRejectsDashRemoteNames checks that a remote whose name starts with +// "-" is never passed to git fetch, which would read it as an option +// (--upload-pack= runs that command). As the main remote it stops +// gh-sync before anything is synced; as another remote a branch tracks it is +// handled like a remote that can't be fetched. +func TestRunRejectsDashRemoteNames(t *testing.T) { + const dashName = "--upload-pack=gh-sync-test" + const wantReason = "remote name '" + dashName + "' starts with '-'" + + mainTests := []struct { + name string + // setup returns the arguments for run(). + setup func(f *syncFixture) []string + }{ + {"given with --remote", func(f *syncFixture) []string { + f.localGit("remote", "add", "--", dashName, f.remote) + return []string{"--remote=" + dashName} + }}, + {"automatically chosen", func(f *syncFixture) []string { + f.localGit("remote", "add", "--", dashName, f.remote) + f.localGit("remote", "remove", "origin") + return nil + }}, + } + for _, tt := range mainTests { + t.Run("main remote "+tt.name, func(t *testing.T) { + f := newSyncFixture(t) + before := f.sha(f.local, "main") + f.advanceUpstream("main", "upstream-1") + args := tt.setup(f) + + res := f.sync(args...) + if res.err == nil { + t.Fatal("run() error = nil, want an error") + } + assertContains(t, "stderr", res.stderr, "fatal: "+wantReason) + assertNotContains(t, "stderr", res.stderr, "Failed to fetch") + f.assertBranchAt("main", before) + }) + } + + // The branches tracking it are skipped with a warning and the exit + // status is 1; the main remote's branches are still synced. + t.Run("remote tracked by a branch", func(t *testing.T) { + f := newSyncFixture(t) + fork := f.addForkRemote() + f.pushForkBranch("topic", "topic-1") + before := f.sha(f.local, "topic") + // Deleted on the fork and merged: a successful fetch would delete it. + f.mergeIntoUpstreamMain("topic") + f.upstreamGit("push", "--quiet", "fork", "--delete", "topic") + f.localGit("remote", "add", "--", dashName, fork) + f.localGit("config", "--", "branch.topic.remote", dashName) + mainBefore := f.sha(f.local, "main") + + res := f.sync() + if res.err == nil { + t.Fatal("run() error = nil, want an error") + } + assertContains(t, "stderr", res.stderr, "warning: couldn't fetch "+dashName+"; skipping the branches that track it: "+wantReason) + assertContains(t, "stdout", res.stdout, fmt.Sprintf("Updated branch main (was %s).", shortSHA(mainBefore))) + assertNotContains(t, "stdout", res.stdout, "topic") + f.assertBranchAt("topic", before) + }) +} + // TestRunExclude checks that branches matching an exclude pattern, from // --exclude or gh-sync.exclude, are neither updated nor deleted, while other // branches still are.