mirror of
https://github.com/grdl/git-get.git
synced 2026-02-05 06:13:48 +00:00
Add Fetch function and test repo being behind upstream
This commit is contained in:
21
repo.go
21
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
|
||||
}
|
||||
|
||||
42
repo_test.go
Normal file
42
repo_test.go
Normal file
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user