Fetch before Merge and push the squash commit to origin

This commit is contained in:
Ian Gulliver
2026-10-14 22:45:02 -07:00
parent 4ffdc1e517
commit 8825d38695
17 changed files with 352 additions and 120 deletions
+32 -8
View File
@@ -147,7 +147,7 @@ func UnmergedChanges(worktree, target string) (Unmerged, error) {
return u, nil
}
func SquashMerge(repo, worktree, target, message string, env []string) (string, error) {
func SquashCommit(repo, worktree, target, message string, env []string) (string, error) {
checkedOut, err := run(repo, "symbolic-ref", "--short", "HEAD")
if err != nil {
return "", err
@@ -196,16 +196,40 @@ func SquashMerge(repo, worktree, target, message string, env []string) (string,
return "", nil
}
commit, err := runEnv(repo, env, "commit-tree", lines[0], "-p", head, "-m", message)
if err != nil {
return "", err
}
return runEnv(repo, env, "commit-tree", lines[0], "-p", head, "-m", message)
}
_, err = run(repo, "merge", "--ff-only", commit)
const Remote = "origin"
var ErrRemoteMoved = errors.New("the remote's branch has moved")
func Push(repo, commit, branch string) error {
out, stderr, code, err := runCode(repo, nil, "push", "--porcelain", Remote, commit+":refs/heads/"+branch)
if err != nil {
return "", err
return err
}
return commit, nil
if code == 0 {
return nil
}
for _, line := range strings.Split(out, "\n") {
if strings.HasPrefix(line, "!") && strings.Contains(line, "[rejected]") && (strings.Contains(line, "(fetch first)") || strings.Contains(line, "(non-fast-forward)")) {
return fmt.Errorf("pushing %s to %s: %w", branch, Remote, ErrRemoteMoved)
}
}
return fmt.Errorf("pushing %s to %s: exit status %d: %s", branch, Remote, code, strings.TrimSpace(out+"\n"+stderr))
}
func FastForward(repo, rev string) error {
_, err := run(repo, "merge", "--ff-only", rev)
return err
}
func FetchFastForward(repo, branch string) error {
_, err := run(repo, "fetch", Remote, branch)
if err != nil {
return err
}
return FastForward(repo, Remote+"/"+branch)
}
func Head(dir string) (string, error) {