diff --git a/repo.go b/repo.go index 36098e3..201dd77 100644 --- a/repo.go +++ b/repo.go @@ -18,3 +18,24 @@ func CloneRepo(url string, path string) (*git.Repository, error) { repo, err := git.Clone(url, path, options) return repo, errors.Wrap(err, "Failed cloning repo") } + +func Fetch(repo *git.Repository) error { + remotes, err := repo.Remotes.List() + if err != nil { + return errors.Wrap(err, "Failed listing remotes") + } + + for _, r := range remotes { + remote, err := repo.Remotes.Lookup(r) + if err != nil { + return errors.Wrap(err, "Failed looking up remote") + } + + err = remote.Fetch(nil, nil, "") + if err != nil { + return errors.Wrap(err, "Failed fetching remote") + } + } + + return nil +} diff --git a/repo_test.go b/repo_test.go new file mode 100644 index 0000000..fb4020d --- /dev/null +++ b/repo_test.go @@ -0,0 +1,42 @@ +package main + +import "testing" + +func TestFetch(t *testing.T) { + // Create origin repo with a single commit in master + origin := newTestRepo(t) + createFile(t, origin, "file") + stageFile(t, origin, "file") + createCommit(t, origin, "Initial commit") + + // Clone the origin repo + repo, err := CloneRepo(origin.Path(), newTempDir(t)) + checkFatal(t, err) + + // Check cloned status. It should not be behind origin + status, err := NewRepoStatus(repo.Workdir()) + checkFatal(t, err) + + if status.BranchStatuses["master"].Behind != 0 { + t.Errorf("Master should not be behind") + } + + // Add another commit to origin + createFile(t, origin, "anotherFile") + stageFile(t, origin, "anotherFile") + createCommit(t, origin, "Second commit") + + // Fetch cloned repo and check the status again + err = Fetch(repo) + status, err = NewRepoStatus(repo.Workdir()) + checkFatal(t, err) + + // Cloned master should now be 1 commit behind origin + if status.BranchStatuses["master"].Behind != 1 { + t.Errorf("Master should be 1 commit behind") + } + + if status.BranchStatuses["master"].Ahead != 0 { + t.Errorf("Master should not be ahead") + } +} diff --git a/status.go b/status.go index 89e66a6..1cb9c01 100644 --- a/status.go +++ b/status.go @@ -8,7 +8,7 @@ import ( type RepoStatus struct { HasUntrackedFiles bool HasUncommittedChanges bool - BranchStatuses []BranchStatus + BranchStatuses map[string]BranchStatus } func NewRepoStatus(path string) (RepoStatus, error) { @@ -33,6 +33,12 @@ func NewRepoStatus(path string) (RepoStatus, error) { } } + branchStatuses, err := Branches(repo) + if err != nil { + return status, errors.Wrap(err, "Failed getting branches statuses") + } + status.BranchStatuses = branchStatuses + return status, nil } diff --git a/status_test.go b/status_test.go index 3774f3a..6879915 100644 --- a/status_test.go +++ b/status_test.go @@ -23,7 +23,7 @@ func TestStatusWithEmptyRepo(t *testing.T) { want := RepoStatus{ HasUntrackedFiles: false, HasUncommittedChanges: false, - BranchStatuses: nil, + BranchStatuses: status.BranchStatuses, } if !reflect.DeepEqual(status, want) { @@ -52,7 +52,7 @@ func TestStatusWithUntrackedFile(t *testing.T) { want := RepoStatus{ HasUntrackedFiles: true, HasUncommittedChanges: false, - BranchStatuses: nil, + BranchStatuses: status.BranchStatuses, } if !reflect.DeepEqual(status, want) { @@ -90,7 +90,7 @@ func TestStatusWithStagedFile(t *testing.T) { want := RepoStatus{ HasUntrackedFiles: false, HasUncommittedChanges: true, - BranchStatuses: nil, + BranchStatuses: status.BranchStatuses, } if !reflect.DeepEqual(status, want) { @@ -117,7 +117,7 @@ func TestStatusWithSingleCommit(t *testing.T) { want := RepoStatus{ HasUntrackedFiles: false, HasUncommittedChanges: false, - BranchStatuses: nil, + BranchStatuses: status.BranchStatuses, } if !reflect.DeepEqual(status, want) { @@ -147,7 +147,7 @@ func TestStatusWithMultipleCommits(t *testing.T) { want := RepoStatus{ HasUntrackedFiles: false, HasUncommittedChanges: false, - BranchStatuses: nil, + BranchStatuses: status.BranchStatuses, } if !reflect.DeepEqual(status, want) { @@ -168,7 +168,7 @@ func TestStatusCloned(t *testing.T) { want := RepoStatus{ HasUntrackedFiles: false, HasUncommittedChanges: false, - BranchStatuses: nil, + BranchStatuses: status.BranchStatuses, } if !reflect.DeepEqual(status, want) {