Skip pushing merges when the repo has no remote

This commit is contained in:
Ian Gulliver
2026-10-22 18:05:52 -07:00
parent f3b15696b3
commit da7f324bab
6 changed files with 69 additions and 15 deletions
+39 -5
View File
@@ -53,14 +53,30 @@ func Inspect(ctx context.Context, path string) (RepoInfo, error) {
return RepoInfo{}, err
}
remote, err := run(ctx, root, "remote", "get-url", "origin")
remote, _, code, err := runCode(ctx, root, nil, "remote", "get-url", Remote)
if err != nil {
return RepoInfo{}, err
}
if code != 0 {
remote = ""
}
return RepoInfo{Root: root, DefaultBranch: branch, Remote: remote}, nil
}
func HasRemote(ctx context.Context, repo string) (bool, error) {
_, _, code, err := runCode(ctx, repo, nil, "remote", "get-url", Remote)
return err == nil && code == 0, err
}
func Upstream(ctx context.Context, repo, branch string) (string, error) {
has, err := HasRemote(ctx, repo)
if err != nil || !has {
return branch, err
}
return Remote + "/" + branch, nil
}
func AddWorktree(ctx context.Context, repo, path, branch, base string) (string, error) {
sha, err := run(ctx, repo, "rev-parse", "--verify", base+"^{commit}")
if err != nil {
@@ -92,12 +108,18 @@ func EnsureBranchCommit(ctx context.Context, repo, branch string, env []string)
if err != nil {
return err
}
_, stderr, code, err := runNetwork(ctx, repo, "push", Remote, commit+":refs/heads/"+branch)
has, err := HasRemote(ctx, repo)
if err != nil {
return err
}
if code != 0 {
return fmt.Errorf("pushing %s to %s: exit status %d: %s", branch, Remote, code, stderr)
if has {
_, stderr, code, err := runNetwork(ctx, repo, "push", Remote, commit+":refs/heads/"+branch)
if err != nil {
return err
}
if code != 0 {
return fmt.Errorf("pushing %s to %s: exit status %d: %s", branch, Remote, code, stderr)
}
}
_, err = run(ctx, repo, "update-ref", "refs/heads/"+branch, commit, "")
return err
@@ -258,6 +280,10 @@ var networkEnv = []string{
var ErrRemoteMoved = errors.New("the remote's branch has moved")
func Push(ctx context.Context, repo, commit, branch, notes string) error {
has, err := HasRemote(ctx, repo)
if err != nil || !has {
return err
}
out, stderr, code, err := runNetwork(ctx, repo, "push", "--atomic", "--porcelain", Remote, commit+":refs/heads/"+branch, notes+":"+NotesRef)
if err != nil {
return err
@@ -279,6 +305,10 @@ func FastForward(ctx context.Context, repo, rev string) error {
}
func Fetch(ctx context.Context, repo string) error {
has, err := HasRemote(ctx, repo)
if err != nil || !has {
return err
}
_, stderr, code, err := runNetwork(ctx, repo, "fetch", Remote)
if err != nil {
return err
@@ -290,7 +320,11 @@ func Fetch(ctx context.Context, repo string) error {
}
func FetchFastForward(ctx context.Context, repo, branch string) error {
err := Fetch(ctx, repo)
has, err := HasRemote(ctx, repo)
if err != nil || !has {
return err
}
err = Fetch(ctx, repo)
if err != nil {
return err
}
+4
View File
@@ -14,6 +14,10 @@ const NotesRef = "refs/notes/byre"
const notesFetch = NotesRef + "*:" + NotesRef + "*"
func ConfigureNotesFetch(ctx context.Context, repo string) error {
has, err := HasRemote(ctx, repo)
if err != nil || !has {
return err
}
key := "remote." + Remote + ".fetch"
out, stderr, code, err := runCode(ctx, repo, nil, "config", "--get-all", key)
if err != nil {