From 5ac52df2c1d53015c2a6053a54d021e4b1371f94 Mon Sep 17 00:00:00 2001 From: roi-codefresh Date: Tue, 12 Oct 2021 11:47:02 +0300 Subject: [PATCH] use default branch from git config instead of master (#196) * use default branch from git config instead of master * fix linting --- cmd/commands/project.go | 4 ++-- pkg/git/mocks/repository.go | 21 +++++++++++++++++++++ pkg/git/repository.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/cmd/commands/project.go b/cmd/commands/project.go index d0bc9faf..9b9e1830 100644 --- a/cmd/commands/project.go +++ b/cmd/commands/project.go @@ -313,10 +313,10 @@ func generateProjectManifests(o *GenerateProjectOptions) (projectYAML, appSetYAM Directory: &argocdv1alpha1.ApplicationSourceDirectory{ Recurse: true, Exclude: "{{ exclude }}", - Include: "{{ include }}", + Include: "{{ include }}", }, }, - } , + }, }, }, }, diff --git a/pkg/git/mocks/repository.go b/pkg/git/mocks/repository.go index 9a97fb0e..76eba45b 100644 --- a/pkg/git/mocks/repository.go +++ b/pkg/git/mocks/repository.go @@ -14,6 +14,27 @@ type Repository struct { mock.Mock } +// CurrentBranch provides a mock function with given fields: +func (_m *Repository) CurrentBranch() (string, error) { + ret := _m.Called() + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + var r1 error + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // Persist provides a mock function with given fields: ctx, opts func (_m *Repository) Persist(ctx context.Context, opts *git.PushOptions) (string, error) { ret := _m.Called(ctx, opts) diff --git a/pkg/git/repository.go b/pkg/git/repository.go index d66d99f8..de80a9d1 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -36,6 +36,8 @@ type ( Repository interface { // Persist runs add, commit and push to the repository default remote Persist(ctx context.Context, opts *PushOptions) (string, error) + // CurrentBranch returns the name of the current branch + CurrentBranch() (string, error) } AddFlagsOptions struct { @@ -101,6 +103,19 @@ var ( worktree = func(r gogit.Repository) (gogit.Worktree, error) { return r.Worktree() } + + defaultBranch = func() (string, error) { + cfg, err := config.LoadConfig(config.GlobalScope) + if err != nil { + return "", fmt.Errorf("failed to load global git config: %w", err) + } + + if cfg.Init.DefaultBranch == "" { + return "main", nil + } + + return cfg.Init.DefaultBranch, nil + } ) func AddFlags(cmd *cobra.Command, opts *AddFlagsOptions) *CloneOptions { @@ -244,6 +259,15 @@ func (r *repo) Persist(ctx context.Context, opts *PushOptions) (string, error) { return h.String(), err } +func (r *repo) CurrentBranch() (string, error) { + ref, err := r.Head() + if err != nil { + return "", fmt.Errorf("failed to resolve ref: %w", err) + } + + return ref.Name().Short(), nil +} + func (r *repo) commit(opts *PushOptions) (*plumbing.Hash, error) { var h plumbing.Hash @@ -447,7 +471,10 @@ func (r *repo) initBranch(ctx context.Context, branchName string) error { } if branchName == "" { - return nil + branchName, err = defaultBranch() + if err != nil { + return err + } } b := plumbing.NewBranchReferenceName(branchName)