diff --git a/common/author.go b/common/author.go index f5e1b1d..f32206c 100644 --- a/common/author.go +++ b/common/author.go @@ -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 { @@ -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 { diff --git a/common/autosync.go b/common/autosync.go index 05e27fb..e7c0e97 100644 --- a/common/autosync.go +++ b/common/autosync.go @@ -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) @@ -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) } diff --git a/common/commit.go b/common/commit.go index 5a4f5a2..bb79bb1 100644 --- a/common/commit.go +++ b/common/commit.go @@ -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) @@ -65,7 +66,7 @@ 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) } @@ -73,7 +74,9 @@ func commit(repoPath string) error { 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...) diff --git a/common/commit_test.go b/common/commit_test.go index ead1412..ce9aea7 100644 --- a/common/commit_test.go +++ b/common/commit_test.go @@ -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) @@ -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() @@ -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() @@ -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 `) diff --git a/common/fetch.go b/common/fetch.go index bca02b9..9fd8e54 100644 --- a/common/fetch.go +++ b/common/fetch.go @@ -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) @@ -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) } diff --git a/common/fetch_test.go b/common/fetch_test.go index 9f0c5e7..e638952 100644 --- a/common/fetch_test.go +++ b/common/fetch_test.go @@ -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) @@ -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) { @@ -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() diff --git a/common/ignore_test.go b/common/ignore_test.go index f7b1ecc..5c21696 100644 --- a/common/ignore_test.go +++ b/common/ignore_test.go @@ -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) diff --git a/common/push.go b/common/push.go index ea19bd7..b375479 100644 --- a/common/push.go +++ b/common/push.go @@ -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) } @@ -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) } diff --git a/common/rebase.go b/common/rebase.go index 78694cc..b41769c 100644 --- a/common/rebase.go +++ b/common/rebase.go @@ -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 { @@ -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) } diff --git a/common/rebase_test.go b/common/rebase_test.go index 2e28c48..66e06f3 100644 --- a/common/rebase_test.go +++ b/common/rebase_test.go @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/common/watch.go b/common/watch.go index 8fe6e99..ae99f7e 100644 --- a/common/watch.go +++ b/common/watch.go @@ -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) } @@ -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) } diff --git a/main.go b/main.go index cfe8f6a..922ba2b 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,8 @@ func main() { return tracerr.Wrap(err) } - err = common.AutoSync(repoPath) + cfg := common.NewRepoConfig(repoPath) + err = common.AutoSync(cfg) if err != nil { return tracerr.Wrap(err) } diff --git a/todo.md b/todo.md index 4d09b3b..fd595c2 100644 --- a/todo.md +++ b/todo.md @@ -11,3 +11,10 @@ - It seems it wasn't being installed by the homebrew package * RPM / DEB packages - https://netdevops.me/2021/building-and-publishing-deb/rpm-packages-with-goreleaser-and-gemfury/ + +* Check the state of the git repo before + - Make sure it isn't in the middle of a rebase / merge + - Make sure that head commit is pointing to a branch + - Make sure that branch has a proper remote tracking branch + +* Write about how to handle the case with an ssh-agent running