Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Warn user about non-existing configured branch #1937

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/fluxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func main() {
SkipMessage: *gitSkipMessage,
}

repo := git.NewRepo(gitRemote, git.PollInterval(*gitPollInterval), git.Timeout(*gitTimeout))
repo := git.NewRepo(gitRemote, git.PollInterval(*gitPollInterval), git.Timeout(*gitTimeout), git.Branch(*gitBranch))
{
shutdownWg.Add(1)
go func() {
Expand Down
5 changes: 4 additions & 1 deletion git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ func checkout(ctx context.Context, workingDir, ref string) error {
// checkPush sanity-checks that we can write to the upstream repo
// (being able to `clone` is an adequate check that we can read the
// upstream).
func checkPush(ctx context.Context, workingDir, upstream string) error {
func checkPush(ctx context.Context, workingDir, upstream, branch string) error {
// --force just in case we fetched the tag from upstream when cloning
args := []string{"tag", "--force", CheckPushTag}
if branch != "" {
args = append(args, branch)
}
if err := execGitCmd(ctx, args, gitCmdConfig{dir: workingDir}); err != nil {
return errors.Wrap(err, "tag for write check")
}
Expand Down
2 changes: 1 addition & 1 deletion git/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestCheckPush(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = checkPush(context.Background(), working, upstreamDir)
err = checkPush(context.Background(), working, upstreamDir, "")
if err != nil {
t.Fatal(err)
}
Expand Down
27 changes: 24 additions & 3 deletions git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"errors"
"fmt"
"io/ioutil"
"os"
"sync"
Expand Down Expand Up @@ -47,6 +48,7 @@ const (
type Repo struct {
// As supplied to constructor
origin Remote
branch string
interval time.Duration
timeout time.Duration
readonly bool
Expand Down Expand Up @@ -83,6 +85,12 @@ func (t Timeout) apply(r *Repo) {
r.timeout = time.Duration(t)
}

type Branch string

func (b Branch) apply(r *Repo) {
r.branch = string(b)
}

var ReadOnly optionFunc = func(r *Repo) {
r.readonly = true
}
Expand Down Expand Up @@ -261,10 +269,23 @@ func (r *Repo) step(bg context.Context) bool {
return false

case RepoCloned:
ctx, cancel := context.WithTimeout(bg, r.timeout)
defer cancel()

if r.branch != "" {
ok, err := refExists(ctx, dir, "refs/heads/"+r.branch)
if err != nil {
r.setUnready(RepoCloned, err)
return false
}
if !ok {
r.setUnready(RepoCloned, fmt.Errorf("configured branch '%s' does not exist", r.branch))
return false
}
}

if !r.readonly {
ctx, cancel := context.WithTimeout(bg, r.timeout)
err := checkPush(ctx, dir, url)
cancel()
err := checkPush(ctx, dir, url, r.branch)
if err != nil {
r.setUnready(RepoCloned, err)
return false
Expand Down