Browse Source

Don't panic on remote repository is empty

Brendan Abolivier 7 years ago
parent
commit
f2f08e71c9
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 changed files with 24 additions and 2 deletions
  1. 24
    2
      src/git/git.go

+ 24
- 2
src/git/git.go View File

10
 	"github.com/sirupsen/logrus"
10
 	"github.com/sirupsen/logrus"
11
 	"golang.org/x/crypto/ssh"
11
 	"golang.org/x/crypto/ssh"
12
 	gogit "gopkg.in/src-d/go-git.v4"
12
 	gogit "gopkg.in/src-d/go-git.v4"
13
+	"gopkg.in/src-d/go-git.v4/plumbing/transport"
13
 	gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
14
 	gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
14
 )
15
 )
15
 
16
 
84
 // Returns with the go-git representation of the repository.
85
 // Returns with the go-git representation of the repository.
85
 // Returns an error if there was an issue opening the repo, getting its work
86
 // Returns an error if there was an issue opening the repo, getting its work
86
 // tree or pulling from the remote. In the latter case, if the error is "already
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
 func pull(clonePath string, auth *gitssh.PublicKeys) (*gogit.Repository, error) {
90
 func pull(clonePath string, auth *gitssh.PublicKeys) (*gogit.Repository, error) {
89
 	// Open the repository
91
 	// Open the repository
90
 	r, err := gogit.PlainOpen(clonePath)
92
 	r, err := gogit.PlainOpen(clonePath)
116
 			return r, nil
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
 		// go-git doesn't have an error variable for "non-fast-forward update",
130
 		// go-git doesn't have an error variable for "non-fast-forward update",
120
 		// so this is the only way to detect it
131
 		// so this is the only way to detect it
121
 		if strings.HasPrefix(err.Error(), "non-fast-forward update") {
132
 		if strings.HasPrefix(err.Error(), "non-fast-forward update") {
150
 // created from the configuration to authenticate on the remote.
161
 // created from the configuration to authenticate on the remote.
151
 // Returns with an error if there was an issue creating the authentication
162
 // Returns with an error if there was an issue creating the authentication
152
 // structure instance or pushing to the remote. In the latter case, if the error
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
 func Push(r *gogit.Repository, cfg config.GitSettings) error {
166
 func Push(r *gogit.Repository, cfg config.GitSettings) error {
155
 	// Get the authentication structure instance
167
 	// Get the authentication structure instance
156
 	auth, err := getAuth(cfg.User, cfg.PrivateKeyPath)
168
 	auth, err := getAuth(cfg.User, cfg.PrivateKeyPath)
181
 			return nil
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
 		// go-git doesn't have an error variable for "non-fast-forward update", so
206
 		// go-git doesn't have an error variable for "non-fast-forward update", so
185
 		// this is the only way to detect it
207
 		// this is the only way to detect it
186
 		if strings.HasPrefix(err.Error(), "non-fast-forward update") {
208
 		if strings.HasPrefix(err.Error(), "non-fast-forward update") {