Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* fix(error): correctly parse in case of `RequestFailedError`
* feat(run): add a `bash` alias for us out there often forgetting the `run` in front
* feat(sshkeys): add support for ed25519 keys
* feat(apps/create): detect Git main branch name

## 1.43.3

Expand Down
8 changes: 5 additions & 3 deletions apps/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func Create(ctx context.Context, appName, remote, buildpack, projectID string, h
}

fmt.Printf("App '%s' has been created\n", app.Name)
if _, ok := utils.DetectGit(); ok && utils.AddGitRemote(ctx, app.GitURL, remote) == nil {
fmt.Printf("Git repository detected: remote %s added\n→ 'git push %s master' to deploy your app\n", remote, remote)
mainBranch := utils.MainBranchName(ctx)
_, ok := utils.DetectGit()
if ok && utils.AddGitRemote(ctx, app.GitURL, remote) == nil {
fmt.Printf("Git repository detected: remote %s added\n→ 'git push %s %s' to deploy your app\n", remote, remote, mainBranch)
} else {
fmt.Printf("To deploy your application, run these commands in your GIT repository:\n→ git remote add %s %s\n→ git push %s master\n", remote, app.GitURL, remote)
fmt.Printf("To deploy your application, run these commands in your Git repository:\n→ git remote add %s %s\n→ git push %s %s\n", remote, app.GitURL, remote, mainBranch)
}
return nil
}
Expand Down
42 changes: 42 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/go-git/go-git/v5"
gitconfig "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"

"github.com/Scalingo/go-scalingo/v10/debug"
"github.com/Scalingo/go-utils/errors/v3"
Expand Down Expand Up @@ -98,3 +99,44 @@ func AddGitRemote(ctx context.Context, url string, name string) error {

return nil
}

// MainBranchName returns the repository main branch name, preferring origin/HEAD,
// then common local branch names. In case of errors, return `main`.
func MainBranchName(ctx context.Context) string {
repo, err := git.PlainOpen(".")
if err != nil {
return "main"
}

// Try to resolve the locally cached `origin/HEAD`
localOriginHEAD, err := repo.Reference(plumbing.ReferenceName("refs/remotes/origin/HEAD"), false)
if err == nil && localOriginHEAD.Type() == plumbing.SymbolicReference {
target := strings.TrimPrefix(localOriginHEAD.Target().Short(), "origin/")
if target != "" {
return target
}
}

// Try the usual main branch names
for _, name := range []string{"main", "master"} {
_, err := repo.Reference(plumbing.NewBranchReferenceName(name), true)
if err == nil {
return name
}
}

// Try to resolve `origin/HEAD`
remoteOrigin, err := repo.Remote("origin")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question. Do we really need to hit the remote repository to determine the main/master branch?

I wonder if it might be better to try to resolve the main/master branch locally first (refs/remotes/origin), then fallback to remote.

WDYT?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes you are right! Done in fa1612a

if err == nil {
references, _ := remoteOrigin.ListContext(ctx, &git.ListOptions{})
// Search through the list of references in that remote for a symbolic reference named HEAD;
// Its target should be the default branch name.
for _, reference := range references {
if reference.Name() == "HEAD" && reference.Type() == plumbing.SymbolicReference {
return reference.Target().Short()
}
}
}

return "main"
}