Skip pushing merges when the repo has no remote
This commit is contained in:
+39
-5
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user