Bladeren bron

Factorise non-error detection when talking to the Git remote

Brendan Abolivier 7 jaren geleden
bovenliggende
commit
46d20cf39e
Getekend door: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 gewijzigde bestanden met toevoegingen van 49 en 70 verwijderingen
  1. 49
    70
      src/git/git.go

+ 49
- 70
src/git/git.go Bestand weergeven

@@ -101,42 +101,15 @@ func pull(clonePath string, auth *gitssh.PublicKeys) (*gogit.Repository, error)
101 101
 	}
102 102
 
103 103
 	// Pull from remote
104
-	err = w.Pull(&gogit.PullOptions{
104
+	if err = w.Pull(&gogit.PullOptions{
105 105
 		RemoteName: "origin",
106 106
 		Auth:       auth,
107
-	})
108
-
109
-	// Don't return with an error for "already up to date" or "non-fast-forward
110
-	// update"
111
-	if err != nil {
112
-		if err == gogit.NoErrAlreadyUpToDate {
113
-			logrus.WithFields(logrus.Fields{
114
-				"clone_path": clonePath,
115
-				"error":      err,
116
-			}).Info("Caught specific non-error")
117
-
118
-			return r, nil
119
-		}
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
-
130
-		// go-git doesn't have an error variable for "non-fast-forward update",
131
-		// so this is the only way to detect it
132
-		if strings.HasPrefix(err.Error(), "non-fast-forward update") {
133
-			logrus.WithFields(logrus.Fields{
134
-				"clone_path": clonePath,
135
-				"error":      err,
136
-			}).Info("Caught specific non-error")
137
-
138
-			return r, nil
139
-		}
107
+	}); err != nil {
108
+		// Check error against known non-errors
109
+		err = checkRemoteErrors(err, logrus.Fields{
110
+			"clone_path": clonePath,
111
+			"error":      err,
112
+		})
140 113
 	}
141 114
 
142 115
 	return r, err
@@ -176,44 +149,50 @@ func Push(r *gogit.Repository, cfg config.GitSettings) error {
176 149
 	}).Info("Pushing to the remote")
177 150
 
178 151
 	// Push to remote
179
-	err = r.Push(&gogit.PushOptions{
152
+	if err = r.Push(&gogit.PushOptions{
180 153
 		Auth: auth,
181
-	})
154
+	}); err != nil {
155
+		// Check error against known non-errors
156
+		err = checkRemoteErrors(err, logrus.Fields{
157
+			"repo":       cfg.User + "@" + cfg.URL,
158
+			"clone_path": cfg.ClonePath,
159
+			"error":      err,
160
+		})
161
+	}
182 162
 
183
-	// Don't return with an error for "already up to date" or "non-fast-forward
184
-	// update"
185
-	if err != nil {
186
-		if err == gogit.NoErrAlreadyUpToDate {
187
-			logrus.WithFields(logrus.Fields{
188
-				"repo":       cfg.User + "@" + cfg.URL,
189
-				"clone_path": cfg.ClonePath,
190
-				"error":      err,
191
-			}).Info("Caught specific non-error")
192
-
193
-			return nil
194
-		}
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
-
206
-		// go-git doesn't have an error variable for "non-fast-forward update", so
207
-		// this is the only way to detect it
208
-		if strings.HasPrefix(err.Error(), "non-fast-forward update") {
209
-			logrus.WithFields(logrus.Fields{
210
-				"repo":       cfg.User + "@" + cfg.URL,
211
-				"clone_path": cfg.ClonePath,
212
-				"error":      err,
213
-			}).Info("Caught specific non-error")
214
-
215
-			return nil
216
-		}
163
+	return err
164
+}
165
+
166
+// processRemoteErrors checks an error against known non-errors returned when
167
+// communicating with the remote. If the error is a non-error, returns nil and
168
+// logs it with the provided fields. If not, returns the error.
169
+func checkRemoteErrors(err error, logFields logrus.Fields) error {
170
+	var nonError bool
171
+
172
+	// Check against known non-errors
173
+	switch err {
174
+	case gogit.NoErrAlreadyUpToDate:
175
+		nonError = true
176
+		break
177
+	case transport.ErrEmptyRemoteRepository:
178
+		nonError = true
179
+		break
180
+	default:
181
+		nonError = false
182
+		break
183
+	}
184
+
185
+	// go-git doesn't have an error variable for "non-fast-forward update", so
186
+	// this is the only way to detect it
187
+	if strings.HasPrefix(err.Error(), "non-fast-forward update") {
188
+		nonError = true
189
+	}
190
+
191
+	// Log non-error
192
+	if nonError {
193
+		logrus.WithFields(logFields).Warn("Caught specific non-error")
194
+
195
+		return nil
217 196
 	}
218 197
 
219 198
 	return err