From 57d61bffed4a774504b2e2d603401e69a9acd146 Mon Sep 17 00:00:00 2001
From: Marcin Tojek <mtojek@users.noreply.github.com>
Date: Thu, 13 Aug 2020 12:28:39 +0200
Subject: [PATCH] Fix: push only two branches (#47)

* Fix: fetch and push only relevant branches

* Fix refs

* Fix: comment
---
 cmd/promote.go              |  2 +-
 internal/promote/storage.go | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/cmd/promote.go b/cmd/promote.go
index 605d56e63..3222674bc 100644
--- a/cmd/promote.go
+++ b/cmd/promote.go
@@ -92,7 +92,7 @@ func promoteCommandAction(cmd *cobra.Command, args []string) error {
 	}
 
 	// Push changes
-	err = promote.PushChanges(githubUser, repository)
+	err = promote.PushChanges(githubUser, repository, newSourceStage, newDestinationStage)
 	if err != nil {
 		return errors.Wrapf(err, "pushing changes failed")
 	}
diff --git a/internal/promote/storage.go b/internal/promote/storage.go
index 4b5df017c..b9b350f6d 100644
--- a/internal/promote/storage.go
+++ b/internal/promote/storage.go
@@ -105,7 +105,12 @@ func CloneRepository(user, stage string) (*git.Repository, error) {
 
 	err = r.Fetch(&git.FetchOptions{
 		RemoteName: remoteName,
-		RefSpecs:   []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
+		RefSpecs: []config.RefSpec{
+			"HEAD:refs/heads/HEAD",
+			"refs/heads/snapshot:refs/heads/snapshot",
+			"refs/heads/staging:refs/heads/staging",
+			"refs/heads/production:refs/heads/production",
+		},
 	})
 	if err != nil {
 		return nil, errors.Wrap(err, "fetch remote branches failed")
@@ -370,8 +375,8 @@ func RemovePackages(r *git.Repository, sourceStage string, packages PackageVersi
 	return newSourceStage, nil
 }
 
-// PushChanges method pushes all branches to the remote repository.
-func PushChanges(user string, r *git.Repository) error {
+// PushChanges method pushes branches to the remote repository.
+func PushChanges(user string, r *git.Repository, newSourceStage, newDestinationStage string) error {
 	authToken, err := github.AuthToken()
 	if err != nil {
 		return errors.Wrap(err, "reading auth token failed")
@@ -379,6 +384,10 @@ func PushChanges(user string, r *git.Repository) error {
 
 	err = r.Push(&git.PushOptions{
 		RemoteName: user,
+		RefSpecs: []config.RefSpec{
+			config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", newSourceStage, newSourceStage)),
+			config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", newDestinationStage, newDestinationStage)),
+		},
 		Auth: &http.BasicAuth{
 			Username: user,
 			Password: authToken,