Skip to content

Commit c477b7d

Browse files
ktraversCopilot
andauthored
send pull.stacked option on force push (#562)
* first pass on including pull.stacked option on push * add fallback for remotes that don't accept push options * send pull.stacked option on force push only * handle locale Refactor push command to use command struct and handle errors. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * use local config instead of push options --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 199d42e commit c477b7d

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

‎cmd/rebase_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,7 @@ func setupAmendedParentRepo(t *testing.T, corruptBase bool) amendedParentRepo {
22732273
cloneDir := filepath.Join(t.TempDir(), "clone")
22742274

22752275
issue250Git(t, ".", "-c", "safe.bareRepository=all", "init", "--bare", "-b", "main", remoteDir)
2276+
issue250Git(t, ".", "-c", "safe.bareRepository=all", "-C", remoteDir, "config", "receive.advertisePushOptions", "true")
22762277
issue250Git(t, ".", "clone", remoteDir, cloneDir)
22772278
issue250Git(t, cloneDir, "config", "user.name", "Test")
22782279
issue250Git(t, cloneDir, "config", "user.email", "test@example.com")
@@ -2410,6 +2411,7 @@ func TestIntegration_AdoptedBranchRebasesFromCommonAncestor(t *testing.T) {
24102411
cloneDir := filepath.Join(t.TempDir(), "clone")
24112412

24122413
issue250Git(t, ".", "-c", "safe.bareRepository=all", "init", "--bare", "-b", "main", remoteDir)
2414+
issue250Git(t, ".", "-c", "safe.bareRepository=all", "-C", remoteDir, "config", "receive.advertisePushOptions", "true")
24132415
issue250Git(t, ".", "clone", remoteDir, cloneDir)
24142416
issue250Git(t, cloneDir, "config", "user.name", "Test")
24152417
issue250Git(t, cloneDir, "config", "user.email", "test@example.com")

‎internal/git/gitops.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,22 @@ func (d *defaultOps) Push(remote string, branches []string, force, atomic bool)
286286
for _, b := range branches {
287287
args = append(args, fmt.Sprintf("refs/heads/%s:refs/heads/%s", b, b))
288288
}
289-
return d.runSilent(args...)
289+
if !force {
290+
return d.runSilent(args...)
291+
}
292+
// Send the "pull.stacked" push option on force pushes, falling back to a
293+
// plain push for remotes that don't support push options.
294+
withOpt := append([]string{"-c", "push.pushOption=pull.stacked", "push"}, args[1:]...)
295+
cmd, err := d.command(withOpt...)
296+
if err != nil {
297+
return err
298+
}
299+
cmd.Env = append(cmd.Environ(), "LC_ALL=C")
300+
err = cmd.Run()
301+
if err != nil && strings.Contains(err.Error(), "does not support push options") {
302+
return d.runSilent(args...)
303+
}
304+
return err
290305
}
291306

292307
// ResolveRemote determines the remote for pushing a branch. It checks git

‎internal/git/gitops_test.go‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func setupBareAndClone(t *testing.T) (string, string) {
6363
out, err := cmd.CombinedOutput()
6464
require.NoError(t, err, "git init --bare: %s", string(out))
6565

66+
cfgCmd := exec.Command("git", "-c", "safe.bareRepository=all", "-C", bareDir, "config", "receive.advertisePushOptions", "true")
67+
out, err = cfgCmd.CombinedOutput()
68+
require.NoError(t, err, "git config: %s", string(out))
69+
6670
cloneDir := filepath.Join(t.TempDir(), "clone")
6771
gitExec(t, ".", "clone", bareDir, cloneDir)
6872

@@ -1763,3 +1767,66 @@ func TestIntegration_WorktreeIgnoresCallerRepositoryEnvironment(t *testing.T) {
17631767
require.NoError(t, err)
17641768
assert.Equal(t, "feature", branch)
17651769
}
1770+
1771+
// If remote supports push options, send the "pull.stacked" push option only for force pushes.
1772+
func TestIntegration_Push_SendsStackPushOption(t *testing.T) {
1773+
if runtime.GOOS == "windows" {
1774+
t.Skip("uses a shell pre-receive hook")
1775+
}
1776+
1777+
for _, tc := range []struct {
1778+
name string
1779+
force, atomic bool
1780+
}{
1781+
{"force", true, false},
1782+
{"force atomic", true, true},
1783+
{"atomic", false, true},
1784+
{"plain", false, false},
1785+
} {
1786+
t.Run(tc.name, func(t *testing.T) {
1787+
bareDir, cloneDir := setupBareAndClone(t)
1788+
restore := withGitDir(t, cloneDir)
1789+
defer restore()
1790+
1791+
optsFile := filepath.Join(t.TempDir(), "opts")
1792+
hook := fmt.Sprintf("#!/bin/sh\nenv | grep '^GIT_PUSH_OPTION' | sort > %q\n", optsFile)
1793+
require.NoError(t, os.WriteFile(filepath.Join(bareDir, "hooks", "pre-receive"), []byte(hook), 0o755))
1794+
1795+
gitExec(t, cloneDir, "checkout", "-b", "b1")
1796+
writeFile(t, cloneDir, "b1.txt", "v1")
1797+
gitExec(t, cloneDir, "add", ".")
1798+
gitExec(t, cloneDir, "commit", "-m", "b1")
1799+
1800+
d := &defaultOps{}
1801+
require.NoError(t, d.Push("origin", []string{"b1"}, tc.force, tc.atomic))
1802+
1803+
got, err := os.ReadFile(optsFile)
1804+
require.NoError(t, err)
1805+
want := "GIT_PUSH_OPTION_COUNT=0\n"
1806+
if tc.force {
1807+
want = "GIT_PUSH_OPTION_0=pull.stacked\nGIT_PUSH_OPTION_COUNT=1\n"
1808+
}
1809+
assert.Equal(t, want, string(got))
1810+
})
1811+
}
1812+
}
1813+
1814+
// Remotes that don't advertise push options must still accept pushes.
1815+
func TestIntegration_Push_FallsBackWithoutPushOptionSupport(t *testing.T) {
1816+
bareDir, cloneDir := setupBareAndClone(t)
1817+
restore := withGitDir(t, cloneDir)
1818+
defer restore()
1819+
1820+
out, err := exec.Command("git", "-c", "safe.bareRepository=all", "-C", bareDir,
1821+
"config", "receive.advertisePushOptions", "false").CombinedOutput()
1822+
require.NoError(t, err, string(out))
1823+
1824+
gitExec(t, cloneDir, "checkout", "-b", "b1")
1825+
writeFile(t, cloneDir, "b1.txt", "v1")
1826+
gitExec(t, cloneDir, "add", ".")
1827+
gitExec(t, cloneDir, "commit", "-m", "b1")
1828+
1829+
d := &defaultOps{}
1830+
require.NoError(t, d.Push("origin", []string{"b1"}, true, true))
1831+
assert.Equal(t, gitExec(t, cloneDir, "rev-parse", "b1"), remoteBranchSHA(t, bareDir, "b1"))
1832+
}

0 commit comments

Comments
 (0)