Skip to content

Commit

Permalink
Merge pull request #108 from garethjevans/rebase-fixes
Browse files Browse the repository at this point in the history
chore: improvements to rebase command
  • Loading branch information
cagiti authored Feb 3, 2021
2 parents 003a432 + 6d494ca commit 1dc7e85
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 100 deletions.
13 changes: 6 additions & 7 deletions pkg/domain/gh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package domain_test

import (
"bytes"
"strings"
"testing"

"github.com/plumming/dx/pkg/api"
Expand Down Expand Up @@ -35,13 +34,13 @@ func TestGetDefaultBranch_Master(t *testing.T) {
}

func TestGetOrgAndRepo(t *testing.T) {
output := `origin https://github.com/clone/chilly (fetch)
origin https://github.com/clone/chilly (push)
upstream https://github.com/plumming/dx (fetch)
upstream https://github.com/plumming/dx (push)`

org, repo, err := domain.ExtractOrgAndRepoFromGitRemotes(strings.NewReader(output))
org, repo, err := domain.ExtractOrgAndRepoURL("https://github.com/clone/chilly")
assert.NoError(t, err)
assert.Equal(t, org, "clone")
assert.Equal(t, repo, "chilly")

org, repo, err = domain.ExtractOrgAndRepoURL("https://github.com/plumming/dx")
assert.NoError(t, err)
assert.Equal(t, org, "plumming")
assert.Equal(t, repo, "dx")
}
72 changes: 31 additions & 41 deletions pkg/domain/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@ import (
"github.com/plumming/dx/pkg/util"
)

func GetOrgAndRepoFromCurrentDir() (string, string, error) {
c := util.Command{
Name: "git",
Args: []string{"remote", "-v"},
}
output, err := c.RunWithoutRetry()
if err != nil {
return "", "", err
}
var (
Runner util.CommandRunner
)

return ExtractOrgAndRepoFromGitRemotes(strings.NewReader(output))
func init() {
Runner = util.DefaultCommandRunner{}
}

func GetRemote(name string) (string, error) {
c := util.Command{
Name: "git",
Args: []string{"remote", "-v"},
}
output, err := c.RunWithoutRetry()
output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return "", err
}
Expand All @@ -43,20 +38,7 @@ func CurrentBranchName(dir string) (string, error) {
Args: []string{"branch", "--show-current"},
Dir: dir,
}
output, err := c.RunWithoutRetry()
if err != nil {
return "", err
}
return output, nil
}

func SwitchBranch(dir string, name string) (string, error) {
c := util.Command{
Name: "git",
Args: []string{"checkout", name},
Dir: dir,
}
output, err := c.RunWithoutRetry()
output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return "", err
}
Expand All @@ -69,10 +51,12 @@ func Stash(dir string) (string, error) {
Args: []string{"stash"},
Dir: dir,
}
output, err := c.RunWithoutRetry()

output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return "", err
}

return output, nil
}

Expand All @@ -82,7 +66,7 @@ func StashPop(dir string) (string, error) {
Args: []string{"stash", "pop"},
Dir: dir,
}
output, err := c.RunWithoutRetry()
output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return "", err
}
Expand All @@ -95,7 +79,7 @@ func Add(dir string, name string) (string, error) {
Args: []string{"add", name},
Dir: dir,
}
output, err := c.RunWithoutRetry()
output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return "", err
}
Expand All @@ -108,7 +92,7 @@ func Commit(dir string, message string) (string, error) {
Args: []string{"commit", "-m", message},
Dir: dir,
}
output, err := c.RunWithoutRetry()
output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return "", err
}
Expand All @@ -121,7 +105,7 @@ func Status(dir string) (string, error) {
Args: []string{"status"},
Dir: dir,
}
output, err := c.RunWithoutRetry()
output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return "", err
}
Expand All @@ -134,11 +118,22 @@ func LocalChanges(dir string) (bool, error) {
Args: []string{"status", "--porcelain"},
Dir: dir,
}
output, err := c.RunWithoutRetry()
output, err := Runner.RunWithoutRetry(&c)
if err != nil {
return false, err
}
return output != "", nil

split := strings.Split(strings.TrimSpace(output), "\n")
changed := []string{}
for _, s := range split {
if s != "" && !strings.HasPrefix(s, "??") {
changed = append(changed, s)
}
}

log.Logger().Debugf("changed files %s, len=%d", changed, len(changed))

return len(changed) > 0, nil
}

func ConfigCommitterInformation(dir string, email string, name string) error {
Expand All @@ -147,7 +142,7 @@ func ConfigCommitterInformation(dir string, email string, name string) error {
Args: []string{"config", "user.email", email},
Dir: dir,
}
_, err := c.RunWithoutRetry()
_, err := Runner.RunWithoutRetry(&c)
if err != nil {
return err
}
Expand All @@ -157,7 +152,7 @@ func ConfigCommitterInformation(dir string, email string, name string) error {
Args: []string{"config", "user.name", name},
Dir: dir,
}
_, err = c.RunWithoutRetry()
_, err = Runner.RunWithoutRetry(&c)
if err != nil {
return err
}
Expand Down Expand Up @@ -187,15 +182,10 @@ func ExtractURLFromRemote(reader io.Reader, name string) (string, error) {
}
}

return "", errors.New("unable to find remote named '" + name + "'")
return "", nil
}

func ExtractOrgAndRepoFromGitRemotes(reader io.Reader) (string, string, error) {
urlString, err := ExtractURLFromRemote(reader, "origin")
if err != nil {
return "", "", errors.New("unable to find remote named 'origin'")
}

func ExtractOrgAndRepoURL(urlString string) (string, string, error) {
url, err := url2.Parse(urlString)
if err != nil {
return "", "", err
Expand Down
85 changes: 83 additions & 2 deletions pkg/domain/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"strings"
"testing"

"github.com/plumming/dx/pkg/util/mocks"

"github.com/plumming/dx/pkg/domain"
"github.com/plumming/dx/pkg/util"
"github.com/stretchr/testify/assert"
)

func TestCanDetermineBranchName(t *testing.T) {
cr := util.DefaultCommandRunner{}

dir, err := ioutil.TempDir("", "domain_test__TestCanDetermineBranchName")
assert.NoError(t, err)

Expand All @@ -24,7 +28,8 @@ func TestCanDetermineBranchName(t *testing.T) {
Args: []string{"init", "-b", "master"},
Dir: dir,
}
output, err := c.RunWithoutRetry()

output, err := cr.RunWithoutRetry(&c)
assert.NoError(t, err)
t.Log(output)

Expand All @@ -35,6 +40,8 @@ func TestCanDetermineBranchName(t *testing.T) {
}

func TestCanStash(t *testing.T) {
cr := util.DefaultCommandRunner{}

dir, err := ioutil.TempDir("", "domain_test__TestCanStash")
assert.NoError(t, err)

Expand All @@ -45,7 +52,7 @@ func TestCanStash(t *testing.T) {
Args: []string{"init", "-b", "master"},
Dir: dir,
}
output, err := c.RunWithoutRetry()
output, err := cr.RunWithoutRetry(&c)
assert.NoError(t, err)
t.Log(output)

Expand Down Expand Up @@ -95,6 +102,74 @@ func TestCanStash(t *testing.T) {
localChanges, err = domain.LocalChanges(dir)
assert.NoError(t, err)
assert.True(t, localChanges)

output, err = domain.Add(dir, "README.md")
assert.NoError(t, err)
t.Log(output)

output, err = domain.Commit(dir, "Updated Commit")
assert.NoError(t, err)
t.Log(output)

localChanges, err = domain.LocalChanges(dir)
assert.NoError(t, err)
assert.False(t, localChanges)

d1 = []byte("hello\ngo\n")
err = ioutil.WriteFile(path.Join(dir, "OTHER.md"), d1, 0600)
assert.NoError(t, err)

localChanges, err = domain.LocalChanges(dir)
assert.NoError(t, err)
assert.False(t, localChanges)
}

func TestLocalChanges(t *testing.T) {
type test struct {
name string
raw string
expected bool
}

tests := []test{
{
name: "no changes",
raw: ``,
expected: false,
},
{
name: "changes to existing files",
raw: ` M go.sum`,
expected: true,
},
{
name: "new file",
raw: `?? ll`,
expected: false,
},
{
name: "changes to both existing and new files",
raw: ` M go.sum
?? ll`,
expected: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := mocks.MockCommandRunner{}
domain.Runner = &r
mocks.GetRunWithoutRetryFunc = func(c *util.Command) (string, error) {
return tc.raw, nil
}

b, err := domain.LocalChanges("")
assert.NoError(t, err)
assert.Equal(t, tc.expected, b)

t.Logf("commands> %s", r.Commands)
})
}
}

func TestCanDetermineRemoteNames(t *testing.T) {
Expand All @@ -121,6 +196,12 @@ upstream https://github.com/plumming/dx (push)`,
remote: "upstream",
expectedURL: "https://github.com/plumming/dx",
},
{
raw: `origin https://github.com/garethjevans/chilly (fetch)
origin https://github.com/garethjevans/chilly (push)`,
remote: "upstream",
expectedURL: "",
},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("TestCanDetermineRemoteNames-%s", tc.remote), func(t *testing.T) {
Expand Down
Loading

0 comments on commit 1dc7e85

Please sign in to comment.