Merge pull request #460 from MichaelMure/fix-push

Michael Muré created

repo: use go-git in more places, fix push

Change summary

api/graphql/graphql_test.go        |  2 +-
api/http/git_file_handlers_test.go |  2 +-
bridge/github/export_test.go       |  4 ++--
bridge/github/import_test.go       |  2 +-
bridge/gitlab/export_test.go       |  4 ++--
bridge/gitlab/import_test.go       |  2 +-
bug/bug_actions.go                 |  6 +++++-
bug/bug_test.go                    |  6 +++---
bug/operation_test.go              |  2 +-
cache/repo_cache_test.go           | 10 +++++-----
commands/select/select_test.go     |  2 +-
identity/identity_actions.go       |  6 +++++-
repository/git_testing.go          |  6 +++---
repository/gogit.go                | 30 ++++++++++++++++++++++++------
tests/read_bugs_test.go            |  4 ++--
15 files changed, 57 insertions(+), 31 deletions(-)

Detailed changes

api/graphql/graphql_test.go 🔗

@@ -14,7 +14,7 @@ import (
 )
 
 func TestQueries(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	random_bugs.FillRepoWithSeed(repo, 10, 42)

api/http/git_file_handlers_test.go 🔗

@@ -19,7 +19,7 @@ import (
 )
 
 func TestGitFileHandlers(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	mrc := cache.NewMultiRepoCache()

bridge/github/export_test.go 🔗

@@ -137,7 +137,7 @@ func TestPushPull(t *testing.T) {
 	}
 
 	// create repo backend
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	backend, err := cache.NewRepoCache(repo)
@@ -209,7 +209,7 @@ func TestPushPull(t *testing.T) {
 
 	fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())
 
-	repoTwo := repository.CreateTestRepo(false)
+	repoTwo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repoTwo)
 
 	// create a second backend

bridge/github/import_test.go 🔗

@@ -127,7 +127,7 @@ func Test_Importer(t *testing.T) {
 		},
 	}
 
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	backend, err := cache.NewRepoCache(repo)

bridge/gitlab/export_test.go 🔗

@@ -142,7 +142,7 @@ func TestPushPull(t *testing.T) {
 	}
 
 	// create repo backend
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	backend, err := cache.NewRepoCache(repo)
@@ -215,7 +215,7 @@ func TestPushPull(t *testing.T) {
 
 	fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())
 
-	repoTwo := repository.CreateTestRepo(false)
+	repoTwo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repoTwo)
 
 	// create a second backend

bridge/gitlab/import_test.go 🔗

@@ -76,7 +76,7 @@ func TestImport(t *testing.T) {
 		},
 	}
 
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	backend, err := cache.NewRepoCache(repo)

bug/bug_actions.go 🔗

@@ -12,6 +12,7 @@ import (
 // Fetch retrieve updates from a remote
 // This does not change the local bugs state
 func Fetch(repo repository.Repo, remote string) (string, error) {
+	// "refs/bugs/*:refs/remotes/<remote>>/bugs/*"
 	remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote)
 	fetchRefSpec := fmt.Sprintf("%s*:%s*", bugsRefPattern, remoteRefSpec)
 
@@ -20,7 +21,10 @@ func Fetch(repo repository.Repo, remote string) (string, error) {
 
 // Push update a remote with the local changes
 func Push(repo repository.Repo, remote string) (string, error) {
-	return repo.PushRefs(remote, bugsRefPattern+"*")
+	// "refs/bugs/*:refs/bugs/*"
+	refspec := fmt.Sprintf("%s*:%s*", bugsRefPattern, bugsRefPattern)
+
+	return repo.PushRefs(remote, refspec)
 }
 
 // Pull will do a Fetch + MergeAll

bug/bug_test.go 🔗

@@ -117,9 +117,9 @@ func equivalentBug(t *testing.T, expected, actual *Bug) {
 }
 
 func TestBugRemove(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
-	remoteA := repository.CreateTestRepo(true)
-	remoteB := repository.CreateTestRepo(true)
+	repo := repository.CreateGoGitTestRepo(false)
+	remoteA := repository.CreateGoGitTestRepo(true)
+	remoteB := repository.CreateGoGitTestRepo(true)
 	defer repository.CleanupTestRepos(repo, remoteA, remoteB)
 
 	err := repo.AddRemote("remoteA", "file://"+remoteA.GetPath())

bug/operation_test.go 🔗

@@ -79,7 +79,7 @@ func TestMetadata(t *testing.T) {
 }
 
 func TestID(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	repos := []repository.ClockedRepo{

cache/repo_cache_test.go 🔗

@@ -12,7 +12,7 @@ import (
 )
 
 func TestCache(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	cache, err := NewRepoCache(repo)
@@ -162,9 +162,9 @@ func TestPushPull(t *testing.T) {
 }
 
 func TestRemove(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
-	remoteA := repository.CreateTestRepo(true)
-	remoteB := repository.CreateTestRepo(true)
+	repo := repository.CreateGoGitTestRepo(false)
+	remoteA := repository.CreateGoGitTestRepo(true)
+	remoteB := repository.CreateGoGitTestRepo(true)
 	defer repository.CleanupTestRepos(repo, remoteA, remoteB)
 
 	err := repo.AddRemote("remoteA", "file://"+remoteA.GetPath())
@@ -211,7 +211,7 @@ func TestRemove(t *testing.T) {
 }
 
 func TestCacheEviction(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	repoCache, err := NewRepoCache(repo)
 	require.NoError(t, err)
 	repoCache.setCacheSize(2)

commands/select/select_test.go 🔗

@@ -11,7 +11,7 @@ import (
 )
 
 func TestSelect(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	repoCache, err := cache.NewRepoCache(repo)

identity/identity_actions.go 🔗

@@ -13,6 +13,7 @@ import (
 // Fetch retrieve updates from a remote
 // This does not change the local identities state
 func Fetch(repo repository.Repo, remote string) (string, error) {
+	// "refs/identities/*:refs/remotes/<remote>/identities/*"
 	remoteRefSpec := fmt.Sprintf(identityRemoteRefPattern, remote)
 	fetchRefSpec := fmt.Sprintf("%s*:%s*", identityRefPattern, remoteRefSpec)
 
@@ -21,7 +22,10 @@ func Fetch(repo repository.Repo, remote string) (string, error) {
 
 // Push update a remote with the local changes
 func Push(repo repository.Repo, remote string) (string, error) {
-	return repo.PushRefs(remote, identityRefPattern+"*")
+	// "refs/identities/*:refs/identities/*"
+	refspec := fmt.Sprintf("%s*:%s*", identityRefPattern, identityRefPattern)
+
+	return repo.PushRefs(remote, refspec)
 }
 
 // Pull will do a Fetch + MergeAll

repository/git_testing.go 🔗

@@ -44,9 +44,9 @@ func CreateTestRepo(bare bool) TestedRepo {
 }
 
 func SetupReposAndRemote() (repoA, repoB, remote TestedRepo) {
-	repoA = CreateTestRepo(false)
-	repoB = CreateTestRepo(false)
-	remote = CreateTestRepo(true)
+	repoA = CreateGoGitTestRepo(false)
+	repoB = CreateGoGitTestRepo(false)
+	remote = CreateGoGitTestRepo(true)
 
 	remoteAddr := "file://" + remote.GetPath()
 

repository/gogit.go 🔗

@@ -138,10 +138,16 @@ func InitGoGitRepo(path string) (*GoGitRepo, error) {
 		return nil, err
 	}
 
+	k, err := defaultKeyring()
+	if err != nil {
+		return nil, err
+	}
+
 	return &GoGitRepo{
-		r:      r,
-		path:   path + "/.git",
-		clocks: make(map[string]lamport.Clock),
+		r:       r,
+		path:    path + "/.git",
+		clocks:  make(map[string]lamport.Clock),
+		keyring: k,
 	}, nil
 }
 
@@ -152,10 +158,16 @@ func InitBareGoGitRepo(path string) (*GoGitRepo, error) {
 		return nil, err
 	}
 
+	k, err := defaultKeyring()
+	if err != nil {
+		return nil, err
+	}
+
 	return &GoGitRepo{
-		r:      r,
-		path:   path,
-		clocks: make(map[string]lamport.Clock),
+		r:       r,
+		path:    path,
+		clocks:  make(map[string]lamport.Clock),
+		keyring: k,
 	}, nil
 }
 
@@ -276,6 +288,9 @@ func (repo *GoGitRepo) FetchRefs(remote string, refSpec string) (string, error)
 		RefSpecs:   []config.RefSpec{config.RefSpec(refSpec)},
 		Progress:   buf,
 	})
+	if err == gogit.NoErrAlreadyUpToDate {
+		return "already up-to-date", nil
+	}
 	if err != nil {
 		return "", err
 	}
@@ -292,6 +307,9 @@ func (repo *GoGitRepo) PushRefs(remote string, refSpec string) (string, error) {
 		RefSpecs:   []config.RefSpec{config.RefSpec(refSpec)},
 		Progress:   buf,
 	})
+	if err == gogit.NoErrAlreadyUpToDate {
+		return "already up-to-date", nil
+	}
 	if err != nil {
 		return "", err
 	}

tests/read_bugs_test.go 🔗

@@ -9,7 +9,7 @@ import (
 )
 
 func TestReadBugs(t *testing.T) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	random_bugs.FillRepoWithSeed(repo, 15, 42)
@@ -23,7 +23,7 @@ func TestReadBugs(t *testing.T) {
 }
 
 func benchmarkReadBugs(bugNumber int, t *testing.B) {
-	repo := repository.CreateTestRepo(false)
+	repo := repository.CreateGoGitTestRepo(false)
 	defer repository.CleanupTestRepos(repo)
 
 	random_bugs.FillRepoWithSeed(repo, bugNumber, 42)