Przeglądaj źródła

Don't panic on remote repository is empty

Brendan Abolivier 6 lat temu
rodzic
commit
f2f08e71c9
Podpisane przez: Brendan Abolivier <contact@brendanabolivier.com> ID klucza GPG: 8EF1500759F70623
1 zmienionych plików z 24 dodań i 2 usunięć
  1. 24
    2
      src/git/git.go

+ 24
- 2
src/git/git.go Wyświetl plik

@@ -10,6 +10,7 @@ import (
10 10
 	"github.com/sirupsen/logrus"
11 11
 	"golang.org/x/crypto/ssh"
12 12
 	gogit "gopkg.in/src-d/go-git.v4"
13
+	"gopkg.in/src-d/go-git.v4/plumbing/transport"
13 14
 	gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
14 15
 )
15 16
 
@@ -84,7 +85,8 @@ func clone(repo string, clonePath string, auth *gitssh.PublicKeys) (*gogit.Repos
84 85
 // Returns with the go-git representation of the repository.
85 86
 // Returns an error if there was an issue opening the repo, getting its work
86 87
 // tree or pulling from the remote. In the latter case, if the error is "already
87
-// up to date" or "non-fast-forward update", doesn't return any error.
88
+// up to date", "non-fast-forward update" or "remote repository is empty",
89
+// doesn't return any error.
88 90
 func pull(clonePath string, auth *gitssh.PublicKeys) (*gogit.Repository, error) {
89 91
 	// Open the repository
90 92
 	r, err := gogit.PlainOpen(clonePath)
@@ -116,6 +118,15 @@ func pull(clonePath string, auth *gitssh.PublicKeys) (*gogit.Repository, error)
116 118
 			return r, nil
117 119
 		}
118 120
 
121
+		if err == transport.ErrEmptyRemoteRepository {
122
+			logrus.WithFields(logrus.Fields{
123
+				"clone_path": clonePath,
124
+				"error":      err,
125
+			}).Info("Caught specific non-error")
126
+
127
+			return r, nil
128
+		}
129
+
119 130
 		// go-git doesn't have an error variable for "non-fast-forward update",
120 131
 		// so this is the only way to detect it
121 132
 		if strings.HasPrefix(err.Error(), "non-fast-forward update") {
@@ -150,7 +161,8 @@ func dirExists(path string) (bool, error) {
150 161
 // created from the configuration to authenticate on the remote.
151 162
 // Returns with an error if there was an issue creating the authentication
152 163
 // structure instance or pushing to the remote. In the latter case, if the error
153
-// is "already up to date" or "non-fast-forward update", doesn't return any error.
164
+// is "already up to date", "non-fast-forward update" or "remote repository is
165
+// empty", doesn't return any error.
154 166
 func Push(r *gogit.Repository, cfg config.GitSettings) error {
155 167
 	// Get the authentication structure instance
156 168
 	auth, err := getAuth(cfg.User, cfg.PrivateKeyPath)
@@ -181,6 +193,16 @@ func Push(r *gogit.Repository, cfg config.GitSettings) error {
181 193
 			return nil
182 194
 		}
183 195
 
196
+		if err == transport.ErrEmptyRemoteRepository {
197
+			logrus.WithFields(logrus.Fields{
198
+				"repo":       cfg.User + "@" + cfg.URL,
199
+				"clone_path": cfg.ClonePath,
200
+				"error":      err,
201
+			}).Info("Caught specific non-error")
202
+
203
+			return nil
204
+		}
205
+
184 206
 		// go-git doesn't have an error variable for "non-fast-forward update", so
185 207
 		// this is the only way to detect it
186 208
 		if strings.HasPrefix(err.Error(), "non-fast-forward update") {