Skip to content

Commit

Permalink
Lookup correct remote for current branch
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz committed Mar 4, 2018
1 parent 010cdd5 commit 4d45257
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
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

0 comments on commit 4d45257

Please sign in to comment.