Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookup correct remote for current branch #692

Merged
merged 1 commit into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions backend/sync/git/cli/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"bytes"
"context"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -80,17 +79,48 @@ func (g *Git) ConfigGet(ctx context.Context, key string) (string, error) {
return "", store.ErrGitNotInit
}

buf := &bytes.Buffer{}
buf := &strings.Builder{}

cmd := exec.CommandContext(ctx, "git", "config", "--get", key)
cmd.Dir = g.path
cmd.Stdout = buf
cmd.Stderr = os.Stderr

out.Debug(ctx, "store.gitConfigValue: %s %+v", cmd.Path, cmd.Args)
out.Debug(ctx, "store.gitConfigGet: %s %+v", cmd.Path, cmd.Args)
if err := cmd.Run(); err != nil {
return "", err
}

return strings.TrimSpace(buf.String()), nil
}

// ConfigList returns all git config settings
func (g *Git) ConfigList(ctx context.Context) (map[string]string, error) {
if !g.IsInitialized() {
return nil, store.ErrGitNotInit
}

buf := &strings.Builder{}

cmd := exec.CommandContext(ctx, "git", "config", "--list")
cmd.Dir = g.path
cmd.Stdout = buf
cmd.Stderr = os.Stderr

out.Debug(ctx, "store.gitConfigList: %s %+v", cmd.Path, cmd.Args)
if err := cmd.Run(); err != nil {
return nil, err
}

lines := strings.Split(buf.String(), "\n")
kv := make(map[string]string, len(lines))
for _, line := range lines {
line = strings.TrimSpace(line)
p := strings.SplitN(line, "=", 2)
if len(p) < 2 {
continue
}
kv[p[0]] = p[1]
}
return kv, nil
}
36 changes: 32 additions & 4 deletions backend/sync/git/cli/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,46 @@ func (g *Git) Commit(ctx context.Context, msg string) error {
return g.Cmd(ctx, "gitCommit", "commit", "-m", msg)
}

func (g *Git) defaultRemote(ctx context.Context, branch string) string {
opts, err := g.ConfigList(ctx)
if err != nil {
return "origin"
}

remote := opts["branch."+branch+".remote"]
if remote == "" {
return "origin"
}

needle := "remote." + remote + ".url"
for k := range opts {
if k == needle {
return remote
}
}
return "origin"
}

func (g *Git) defaultBranch(ctx context.Context) string {
out, _, err := g.captureCmd(ctx, "defaultBranch", "rev-parse", "--abbrev-ref", "HEAD")
if err != nil || string(out) == "" {
return "master"
}
return strings.TrimSpace(string(out))
}

// PushPull pushes the repo to it's origin.
// optional arguments: remote and branch
func (g *Git) PushPull(ctx context.Context, op, remote, branch string) error {
if !g.IsInitialized() {
return store.ErrGitNotInit
}

if remote == "" {
remote = "origin"
}
if branch == "" {
branch = "master"
branch = g.defaultBranch(ctx)
}
if remote == "" {
remote = g.defaultRemote(ctx, branch)
}

if v, err := g.ConfigGet(ctx, "remote."+remote+".url"); err != nil || v == "" {
Expand Down