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

Check for 'main' as potential default branch name #14193

Merged
merged 2 commits into from
Dec 30, 2020
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
5 changes: 5 additions & 0 deletions modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo
found := false
hasDefault := false
hasMaster := false
hasMain := false
for _, branch := range branches {
if branch == repo.DefaultBranch {
found = true
Expand All @@ -251,13 +252,17 @@ func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo
hasDefault = true
} else if branch == "master" {
hasMaster = true
} else if branch == "main" {
hasMain = true
}
}
if !found {
if hasDefault {
repo.DefaultBranch = setting.Repository.DefaultBranch
} else if hasMaster {
repo.DefaultBranch = "master"
} else if hasMain {
repo.DefaultBranch = "main"
} else if len(branches) > 0 {
repo.DefaultBranch = branches[0]
} else {
Expand Down
4 changes: 2 additions & 2 deletions routers/private/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
RepoName: repoName,
}
updates = append(updates, &option)
if repo.IsEmpty && option.IsBranch() && option.BranchName() == "master" {
// put the master branch first
if repo.IsEmpty && option.IsBranch() && (option.BranchName() == "master" || option.BranchName() == "main") {
// put the master/main branch first
copy(updates[1:], updates)
updates[0] = &option
}
Expand Down
4 changes: 4 additions & 0 deletions services/mirror/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, re

hasDefault := false
hasMaster := false
hasMain := false
defaultBranchName := m.Repo.DefaultBranch
if len(defaultBranchName) == 0 {
defaultBranchName = setting.Repository.DefaultBranch
Expand All @@ -540,13 +541,16 @@ func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, re

hasDefault = hasDefault || name == defaultBranchName
hasMaster = hasMaster || name == "master"
hasMain = hasMain || name == "main"
}

if len(firstName) > 0 {
if hasDefault {
m.Repo.DefaultBranch = defaultBranchName
} else if hasMaster {
m.Repo.DefaultBranch = "master"
} else if hasMain {
m.Repo.DefaultBranch = "main"
} else {
m.Repo.DefaultBranch = firstName
}
Expand Down