Skip to content

Commit

Permalink
Path the RepoConfig to all the methods
Browse files Browse the repository at this point in the history
Instead of the repoPath. I want to configure the path of the git
process in the future.
  • Loading branch information
vHanda committed May 19, 2022
1 parent df6ba9c commit 67e7ae2
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 64 deletions.
6 changes: 3 additions & 3 deletions common/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
var errNoGitAuthorEmail = errors.New("Missing git author email")
var errNoGitAuthorName = errors.New("Missing git author name")

func ensureGitAuthor(repoPath string) error {
_, err := GitCommand(repoPath, []string{"config", "user.email"})
func ensureGitAuthor(repoConfig RepoConfig) error {
_, err := GitCommand(repoConfig, []string{"config", "user.email"})
if err != nil {
var exerr *exec.ExitError
if errors.As(err, &exerr) && exerr.ExitCode() == 1 {
Expand All @@ -20,7 +20,7 @@ func ensureGitAuthor(repoPath string) error {
return tracerr.Wrap(err)
}

_, err = GitCommand(repoPath, []string{"config", "user.name"})
_, err = GitCommand(repoConfig, []string{"config", "user.name"})
if err != nil {
var exerr *exec.ExitError
if errors.As(err, &exerr) && exerr.ExitCode() == 1 {
Expand Down
13 changes: 7 additions & 6 deletions common/autosync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@ import (
)

// FIXME: Add logs for when we commit, pull, and push
func AutoSync(repoPath string) error {
func AutoSync(repoConfig RepoConfig) error {
var err error
err = ensureGitAuthor(repoPath)
err = ensureGitAuthor(repoConfig)
if err != nil {
return tracerr.Wrap(err)
}

err = commit(repoPath)
err = commit(repoConfig)
if err != nil {
return tracerr.Wrap(err)
}

err = fetch(repoPath)
err = fetch(repoConfig)
if err != nil {
return tracerr.Wrap(err)
}

err = rebase(repoPath)
err = rebase(repoConfig)
if err != nil {
if errors.Is(err, errRebaseFailed) {
repoPath := repoConfig.RepoPath
err := beeep.Alert("Git Auto Sync - Conflict", "Could not rebase for - "+repoPath, "assets/warning.png")
if err != nil {
return tracerr.Wrap(err)
Expand All @@ -39,7 +40,7 @@ func AutoSync(repoPath string) error {
return tracerr.Wrap(err)
}

err = push(repoPath)
err = push(repoConfig)
if err != nil {
return tracerr.Wrap(err)
}
Expand Down
9 changes: 6 additions & 3 deletions common/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
git "gopkg.in/src-d/go-git.v4"
)

func commit(repoPath string) error {
func commit(repoConfig RepoConfig) error {
repoPath := repoConfig.RepoPath
repo, err := git.PlainOpenWithOptions(repoPath, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return tracerr.Wrap(err)
Expand Down Expand Up @@ -65,15 +66,17 @@ func commit(repoPath string) error {
return nil
}

_, err = GitCommand(repoPath, []string{"commit", "-m", msg})
_, err = GitCommand(repoConfig, []string{"commit", "-m", msg})
if err != nil {
return tracerr.Wrap(err)
}

return nil
}

func GitCommand(repoPath string, args []string) (bytes.Buffer, error) {
func GitCommand(repoConfig RepoConfig, args []string) (bytes.Buffer, error) {
repoPath := repoConfig.RepoPath

var outb, errb bytes.Buffer

statusCmd := exec.Command("git", args...)
Expand Down
34 changes: 17 additions & 17 deletions common/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gotest.tools/v3/assert"
)

func PrepareFixture(t *testing.T, name string) string {
func PrepareFixture(t *testing.T, name string) RepoConfig {
newRepoPath, err := ioutil.TempDir(os.TempDir(), name)
assert.NilError(t, err)

Expand All @@ -24,16 +24,16 @@ func PrepareFixture(t *testing.T, name string) string {
err = os.Rename(filepath.Join(newRepoPath, ".gitted"), filepath.Join(newRepoPath, ".git"))
assert.NilError(t, err)

return newRepoPath
return NewRepoConfig(newRepoPath)
}

func Test_NoChanges(t *testing.T) {
repoPath := PrepareFixture(t, "no_changes")
repoConfig := PrepareFixture(t, "no_changes")

err := commit(repoPath)
err := commit(repoConfig)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

head, err := r.Head()
Expand Down Expand Up @@ -61,30 +61,30 @@ func HasHeadCommit(t *testing.T, repoPath string, hash string, msg string) {
}

func Test_NewFile(t *testing.T) {
repoPath := PrepareFixture(t, "new_file")
repoConfig := PrepareFixture(t, "new_file")

err := commit(repoPath)
err := commit(repoConfig)
assert.NilError(t, err)

HasHeadCommit(t, repoPath, "28cc969d97ddb7640f5e1428bbc8f2947d1ffd57", "?? 2.md\n")
HasHeadCommit(t, repoConfig.RepoPath, "28cc969d97ddb7640f5e1428bbc8f2947d1ffd57", "?? 2.md\n")
}

func Test_OneFileChange(t *testing.T) {
repoPath := PrepareFixture(t, "one_file_change")
repoConfig := PrepareFixture(t, "one_file_change")

err := commit(repoPath)
err := commit(repoConfig)
assert.NilError(t, err)

HasHeadCommit(t, repoPath, "28cc969d97ddb7640f5e1428bbc8f2947d1ffd57", " M 1.md\n")
HasHeadCommit(t, repoConfig.RepoPath, "28cc969d97ddb7640f5e1428bbc8f2947d1ffd57", " M 1.md\n")
}

func Test_VimSwapFile(t *testing.T) {
repoPath := PrepareFixture(t, "vim_swap_file")
repoConfig := PrepareFixture(t, "vim_swap_file")

err := commit(repoPath)
err := commit(repoConfig)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

head, err := r.Head()
Expand All @@ -94,12 +94,12 @@ func Test_VimSwapFile(t *testing.T) {
}

func Test_MultipleFileChange(t *testing.T) {
repoPath := PrepareFixture(t, "multiple_file_change")
repoConfig := PrepareFixture(t, "multiple_file_change")

err := commit(repoPath)
err := commit(repoConfig)
assert.NilError(t, err)

HasHeadCommit(t, repoPath, "7058b6b292ee3d1382670334b5f29570a1117ef1", ` D dirA/2.md
HasHeadCommit(t, repoConfig.RepoPath, "7058b6b292ee3d1382670334b5f29570a1117ef1", ` D dirA/2.md
M 1.md
?? dirB/3.md
`)
Expand Down
5 changes: 3 additions & 2 deletions common/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
git "gopkg.in/src-d/go-git.v4"
)

func fetch(repoPath string) error {
func fetch(repoConfig RepoConfig) error {
repoPath := repoConfig.RepoPath
r, err := git.PlainOpenWithOptions(repoPath, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return tracerr.Wrap(err)
Expand All @@ -19,7 +20,7 @@ func fetch(repoPath string) error {
for _, remote := range remotes {
remoteName := remote.Config().Name

_, err := GitCommand(repoPath, []string{"fetch", remoteName})
_, err := GitCommand(repoConfig, []string{"fetch", remoteName})
if err != nil {
return tracerr.Wrap(err)
}
Expand Down
14 changes: 7 additions & 7 deletions common/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"gotest.tools/v3/assert"
)

func PrepareMultiFixtures(t *testing.T, name string, deps []string) string {
func PrepareMultiFixtures(t *testing.T, name string, deps []string) RepoConfig {
newTestDataPath, err := ioutil.TempDir(os.TempDir(), "mutli_fixture")
assert.NilError(t, err)

Expand All @@ -27,10 +27,10 @@ func PrepareMultiFixtures(t *testing.T, name string, deps []string) string {
assert.NilError(t, err)
}

newRepoPath := PrepareFixture(t, name)
FixFixtureGitConfig(t, newRepoPath, newTestDataPath)
newRepoConfig := PrepareFixture(t, name)
FixFixtureGitConfig(t, newRepoConfig.RepoPath, newTestDataPath)

return newRepoPath
return newRepoConfig
}

func FixFixtureGitConfig(t *testing.T, newRepoPath string, testDataPath string) {
Expand All @@ -47,12 +47,12 @@ func FixFixtureGitConfig(t *testing.T, newRepoPath string, testDataPath string)
}

func Test_SimpleFetch(t *testing.T) {
repoPath := PrepareMultiFixtures(t, "simple_fetch", []string{"multiple_file_change"})
repoConfig := PrepareMultiFixtures(t, "simple_fetch", []string{"multiple_file_change"})

err := fetch(repoPath)
err := fetch(repoConfig)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

head, err := r.Head()
Expand Down
2 changes: 1 addition & 1 deletion common/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Test_SimpleIgnore(t *testing.T) {
repoPath := PrepareFixture(t, "ignore")
repoPath := PrepareFixture(t, "ignore").RepoPath

ignore, err := isFileIgnoredByGit(repoPath, "1.txt")
assert.NilError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions common/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/ztrue/tracerr"
)

func push(repoPath string) error {
bi, err := fetchBranchInfo(repoPath)
func push(repoConfig RepoConfig) error {
bi, err := fetchBranchInfo(repoConfig.RepoPath)
if err != nil {
return tracerr.Wrap(err)
}
Expand All @@ -14,7 +14,7 @@ func push(repoPath string) error {
return nil
}

_, err = GitCommand(repoPath, []string{"push", bi.UpstreamRemote, bi.UpstreamBranch})
_, err = GitCommand(repoConfig, []string{"push", bi.UpstreamRemote, bi.UpstreamBranch})
if err != nil {
return tracerr.Wrap(err)
}
Expand Down
7 changes: 4 additions & 3 deletions common/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (

var errRebaseFailed = errors.New("git rebase failed")

func rebase(repoPath string) error {
func rebase(repoConfig RepoConfig) error {
repoPath := repoConfig.RepoPath
bi, err := fetchBranchInfo(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

_, rebaseErr := GitCommand(repoPath, []string{"rebase", bi.UpstreamRemote + "/" + bi.UpstreamBranch})
_, rebaseErr := GitCommand(repoConfig, []string{"rebase", bi.UpstreamRemote + "/" + bi.UpstreamBranch})
if rebaseErr != nil {
rebaseInProgress, err := isRebasing(repoPath)
if err != nil {
Expand All @@ -28,7 +29,7 @@ func rebase(repoPath string) error {

var exerr *exec.ExitError
if errors.As(rebaseErr, &exerr) && exerr.ExitCode() == 1 && rebaseInProgress {
_, err := GitCommand(repoPath, []string{"rebase", "--abort"})
_, err := GitCommand(repoConfig, []string{"rebase", "--abort"})
if err != nil {
return tracerr.Wrap(err)
}
Expand Down
30 changes: 15 additions & 15 deletions common/rebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

// No new commits on remote or local
func Test_RebaseNothing(t *testing.T) {
repoPath := PrepareMultiFixtures(t, "rebase_nothing", []string{"rebase_parent"})
repoConfig := PrepareMultiFixtures(t, "rebase_nothing", []string{"rebase_parent"})

err := rebase(repoPath)
err := rebase(repoConfig)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

head, err := r.Head()
Expand All @@ -26,12 +26,12 @@ func Test_RebaseNothing(t *testing.T) {

// New commits on local
func Test_RebaseLocalCommits(t *testing.T) {
repoPath := PrepareMultiFixtures(t, "rebase_local_commits", []string{"rebase_parent"})
repoConfig := PrepareMultiFixtures(t, "rebase_local_commits", []string{"rebase_parent"})

err := rebase(repoPath)
err := rebase(repoConfig)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

head, err := r.Head()
Expand All @@ -42,12 +42,12 @@ func Test_RebaseLocalCommits(t *testing.T) {

// New commits on remote
func Test_RebaseRemoteCommits(t *testing.T) {
repoPath := PrepareMultiFixtures(t, "rebase_remote_commits", []string{"rebase_parent"})
repoConfig := PrepareMultiFixtures(t, "rebase_remote_commits", []string{"rebase_parent"})

err := rebase(repoPath)
err := rebase(repoConfig)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

head, err := r.Head()
Expand All @@ -58,12 +58,12 @@ func Test_RebaseRemoteCommits(t *testing.T) {

// New commits on both, no conflict
func Test_RebaseBothCommitsNoConflict(t *testing.T) {
repoPath := PrepareMultiFixtures(t, "rebase_both_commits", []string{"rebase_parent"})
repoConfig := PrepareMultiFixtures(t, "rebase_both_commits", []string{"rebase_parent"})

err := rebase(repoPath)
err := rebase(repoConfig)
assert.NilError(t, err)

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

head, err := r.Head()
Expand All @@ -76,15 +76,15 @@ func Test_RebaseBothCommitsNoConflict(t *testing.T) {

// New commits on both, some kind of conflict
func Test_RebaseBothCommitsConflict(t *testing.T) {
repoPath := PrepareMultiFixtures(t, "rebase_both_commits_conflict", []string{"rebase_parent"})
repoConfig := PrepareMultiFixtures(t, "rebase_both_commits_conflict", []string{"rebase_parent"})

r, err := git.PlainOpen(repoPath)
r, err := git.PlainOpen(repoConfig.RepoPath)
assert.NilError(t, err)

origHead, err := r.Head()
assert.NilError(t, err)

err = rebase(repoPath)
err = rebase(repoConfig)
assert.Equal(t, err, errRebaseFailed)

newHead, err := r.Head()
Expand Down
6 changes: 3 additions & 3 deletions common/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func WatchForChanges(cfg RepoConfig) error {
repoPath := cfg.RepoPath
var err error

err = AutoSync(cfg.RepoPath)
err = AutoSync(cfg)
if err != nil {
return tracerr.Wrap(err)
}
Expand Down Expand Up @@ -64,14 +64,14 @@ func WatchForChanges(cfg RepoConfig) error {
done <- true
}()

err := AutoSync(repoPath)
err := AutoSync(cfg)
if err != nil {
log.Fatalln(err)
}
continue

case <-pollTicker.C:
err := AutoSync(repoPath)
err := AutoSync(cfg)
if err != nil {
log.Fatalln(err)
}
Expand Down
Loading

0 comments on commit 67e7ae2

Please sign in to comment.