Skip to content

Commit

Permalink
use default branch from git config instead of master (#196)
Browse files Browse the repository at this point in the history
* use default branch from git config instead of master

* fix linting
  • Loading branch information
roi-codefresh authored Oct 12, 2021
1 parent 795d4d9 commit 5ac52df
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ func generateProjectManifests(o *GenerateProjectOptions) (projectYAML, appSetYAM
Directory: &argocdv1alpha1.ApplicationSourceDirectory{
Recurse: true,
Exclude: "{{ exclude }}",
Include: "{{ include }}",
Include: "{{ include }}",
},
},
} ,
},
},
},
},
Expand Down
21 changes: 21 additions & 0 deletions pkg/git/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5ac52df

Please sign in to comment.