Browse Source

Warn user about non-existing configured branch

When a user tried to configure Flux against an empty repository or a
branch that did not exist we would report a bogus status and left the
user confused.

This commit makes it possible to configure a branch that should be
checked for existence on creation of a new repository. In case there
is no HEAD found for this branch, it does not exist in git terms, and
we report back a meaningful error message to the user.

As a bonus we also use this configured branch when we create a tag for
our write check, so we do not touch anything the user has not told us
to.
Hidde Beydals 1 month ago
parent
commit
9d6355f4ad
4 changed files with 30 additions and 6 deletions
  1. 1
    1
      cmd/fluxd/main.go
  2. 4
    1
      git/operations.go
  3. 1
    1
      git/operations_test.go
  4. 24
    3
      git/repo.go

+ 1
- 1
cmd/fluxd/main.go View File

@@ -485,7 +485,7 @@ func main() {
485 485
 		SkipMessage: *gitSkipMessage,
486 486
 	}
487 487
 
488
-	repo := git.NewRepo(gitRemote, git.PollInterval(*gitPollInterval), git.Timeout(*gitTimeout))
488
+	repo := git.NewRepo(gitRemote, git.PollInterval(*gitPollInterval), git.Timeout(*gitTimeout), git.Branch(*gitBranch))
489 489
 	{
490 490
 		shutdownWg.Add(1)
491 491
 		go func() {

+ 4
- 1
git/operations.go View File

@@ -78,9 +78,12 @@ func checkout(ctx context.Context, workingDir, ref string) error {
78 78
 // checkPush sanity-checks that we can write to the upstream repo
79 79
 // (being able to `clone` is an adequate check that we can read the
80 80
 // upstream).
81
-func checkPush(ctx context.Context, workingDir, upstream string) error {
81
+func checkPush(ctx context.Context, workingDir, upstream, branch string) error {
82 82
 	// --force just in case we fetched the tag from upstream when cloning
83 83
 	args := []string{"tag", "--force", CheckPushTag}
84
+	if branch != "" {
85
+		args = append(args, branch)
86
+	}
84 87
 	if err := execGitCmd(ctx, args, gitCmdConfig{dir: workingDir}); err != nil {
85 88
 		return errors.Wrap(err, "tag for write check")
86 89
 	}

+ 1
- 1
git/operations_test.go View File

@@ -213,7 +213,7 @@ func TestCheckPush(t *testing.T) {
213 213
 	if err != nil {
214 214
 		t.Fatal(err)
215 215
 	}
216
-	err = checkPush(context.Background(), working, upstreamDir)
216
+	err = checkPush(context.Background(), working, upstreamDir, "")
217 217
 	if err != nil {
218 218
 		t.Fatal(err)
219 219
 	}

+ 24
- 3
git/repo.go View File

@@ -2,6 +2,7 @@ package git
2 2
 
3 3
 import (
4 4
 	"errors"
5
+	"fmt"
5 6
 	"io/ioutil"
6 7
 	"os"
7 8
 	"sync"
@@ -47,6 +48,7 @@ const (
47 48
 type Repo struct {
48 49
 	// As supplied to constructor
49 50
 	origin   Remote
51
+	branch   string
50 52
 	interval time.Duration
51 53
 	timeout  time.Duration
52 54
 	readonly bool
@@ -83,6 +85,12 @@ func (t Timeout) apply(r *Repo) {
83 85
 	r.timeout = time.Duration(t)
84 86
 }
85 87
 
88
+type Branch string
89
+
90
+func (b Branch) apply(r *Repo) {
91
+	r.branch = string(b)
92
+}
93
+
86 94
 var ReadOnly optionFunc = func(r *Repo) {
87 95
 	r.readonly = true
88 96
 }
@@ -261,10 +269,23 @@ func (r *Repo) step(bg context.Context) bool {
261 269
 		return false
262 270
 
263 271
 	case RepoCloned:
272
+		ctx, cancel := context.WithTimeout(bg, r.timeout)
273
+		defer cancel()
274
+
275
+		if r.branch != "" {
276
+			ok, err := refExists(ctx, dir, "refs/heads/"+r.branch)
277
+			if err != nil {
278
+				r.setUnready(RepoCloned, err)
279
+				return false
280
+			}
281
+			if !ok {
282
+				r.setUnready(RepoCloned, fmt.Errorf("configured branch '%s' does not exist", r.branch))
283
+				return false
284
+			}
285
+		}
286
+
264 287
 		if !r.readonly {
265
-			ctx, cancel := context.WithTimeout(bg, r.timeout)
266
-			err := checkPush(ctx, dir, url)
267
-			cancel()
288
+			err := checkPush(ctx, dir, url, r.branch)
268 289
 			if err != nil {
269 290
 				r.setUnready(RepoCloned, err)
270 291
 				return false

Loading…
Cancel
Save