From b7fdb3f81bc1b5dbe0717468d45485265edfb920 Mon Sep 17 00:00:00 2001 From: Omar Jarjur Date: Tue, 13 Nov 2018 13:35:20 -0800 Subject: [PATCH] Make the repo method for pushing more general --- commands/commands.go | 7 ++++--- commands/push.go | 8 +++----- repository/git.go | 27 ++++++++++----------------- repository/mock_repo.go | 10 +++++----- repository/repo.go | 6 +++--- 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 21aeed66..b4ea0390 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -24,9 +24,10 @@ import ( ) const ( - notesRefPattern = "refs/notes/devtools/*" - archiveRefPattern = "refs/devtools/archives/*" - commentFilename = "APPRAISE_COMMENT_EDITMSG" + notesRefPattern = "refs/notes/devtools/*" + devtoolsRefPattern = "refs/devtools/*" + archiveRefPattern = "refs/devtools/archives/*" + commentFilename = "APPRAISE_COMMENT_EDITMSG" ) // Command represents the definition of a single command. diff --git a/commands/push.go b/commands/push.go index 83fb61d2..4f6e96e6 100644 --- a/commands/push.go +++ b/commands/push.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" - "github.com/google/git-appraise/fork" "github.com/google/git-appraise/repository" ) @@ -35,10 +34,9 @@ func push(repo repository.Repo, args []string) error { remote = args[0] } - if err := repo.PushNotesForksAndArchive(remote, notesRefPattern, fork.Ref, archiveRefPattern); err != nil { - return err - } - return nil + notesRefspec := fmt.Sprintf("%s:%s", notesRefPattern, notesRefPattern) + devtoolsRefspec := fmt.Sprintf("+%s*:%s*", devtoolsRefPattern, devtoolsRefPattern) + return repo.Push(remote, notesRefspec, devtoolsRefspec) } var pushCmd = &Command{ diff --git a/repository/git.go b/repository/git.go index 971dd735..3a3743fc 100644 --- a/repository/git.go +++ b/repository/git.go @@ -953,23 +953,6 @@ func (repo *GitRepo) PushNotesAndArchive(remote, notesRefPattern, archiveRefPatt return nil } -// PushNotesForksAndArchive pushes the given notes, forks, and archive refs to a remote repo. -func (repo *GitRepo) PushNotesForksAndArchive(remote, notesRefPattern, forksRef, archiveRefPattern string) error { - if !strings.HasPrefix(forksRef, devtoolsRefPrefix) { - return fmt.Errorf("Unsupported forks ref: %q", forksRef) - } - if !strings.HasPrefix(archiveRefPattern, devtoolsRefPrefix) { - return fmt.Errorf("Unsupported archive ref pattern: %q", archiveRefPattern) - } - notesRefspec := fmt.Sprintf("%s:%s", notesRefPattern, notesRefPattern) - devtoolsRefspec := fmt.Sprintf("+%s*:%s*", devtoolsRefPrefix, devtoolsRefPrefix) - err := repo.runGitCommandInline("push", remote, notesRefspec, devtoolsRefspec) - if err != nil { - return fmt.Errorf("Failed to push the local notes, forks, and archive to the remote '%s': %v", remote, err) - } - return nil -} - func getRemoteNotesRef(remote, localNotesRef string) string { relativeNotesRef := strings.TrimPrefix(localNotesRef, "refs/notes/") return "refs/notes/remotes/" + remote + "/" + relativeNotesRef @@ -1276,3 +1259,13 @@ func (repo *GitRepo) PullNotesForksAndArchive(remote, notesRefPattern, forksRef, } return nil } + +// Push pushes the given refs to a remote repo. +func (repo *GitRepo) Push(remote string, refSpecs ...string) error { + pushArgs := append([]string{"push", remote}, refSpecs...) + err := repo.runGitCommandInline(pushArgs...) + if err != nil { + return fmt.Errorf("Failed to push the local refs to the remote '%s': %v", remote, err) + } + return nil +} diff --git a/repository/mock_repo.go b/repository/mock_repo.go index cbb82bdc..3b9c15d0 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -663,11 +663,6 @@ func (r *mockRepoForTest) FetchAndReturnNewReviewHashes(remote, notesRefPattern, return nil, nil } -// PushNotesForksAndArchive pushes the given notes, forks, and archive refs to a remote repo. -func (r *mockRepoForTest) PushNotesForksAndArchive(remote, notesRefPattern, forksRef, archiveRefPattern string) error { - return nil -} - // PullNotesForksAndArchive fetches the contents of the notes, forks, and archives // refs from a remote repo, and merges them with the corresponding local refs. // @@ -686,3 +681,8 @@ func (r *mockRepoForTest) PushNotesForksAndArchive(remote, notesRefPattern, fork func (r *mockRepoForTest) PullNotesForksAndArchive(remote, notesRefPattern, forksRef, archiveRefPattern string) error { return nil } + +// Push pushes the given refs to a remote repo. +func (r *mockRepoForTest) Push(remote string, refPattern ...string) error { + return nil +} diff --git a/repository/repo.go b/repository/repo.go index eed7400b..e06fa1d5 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -311,9 +311,6 @@ type Repo interface { // they point to. FetchAndReturnNewReviewHashes(remote, notesRefPattern, archiveRefPattern string) ([]string, error) - // PushNotesForksAndArchive pushes the given notes, forks, and archive refs to a remote repo. - PushNotesForksAndArchive(remote, notesRefPattern, forksRef, archiveRefPattern string) error - // PullNotesForksAndArchive fetches the contents of the notes, forks, and archives // refs from a remote repo, and merges them with the corresponding local refs. // @@ -330,4 +327,7 @@ type Repo interface { // we merely ensure that their history graph includes every commit that we // intend to keep. PullNotesForksAndArchive(remote, notesRefPattern, forksRef, archiveRefPattern string) error + + // Push pushes the given refs to a remote repo. + Push(remote string, refPattern ...string) error }