Skip to content

Commit

Permalink
Check for 'main' as potential default branch name (#14193)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyline75489 authored Dec 30, 2020
1 parent c074e46 commit 632800e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
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

0 comments on commit 632800e

Please sign in to comment.