Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use default branch from git config instead of master #196

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
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.Reference(plumbing.ReferenceName(plumbing.HEAD), true)
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