Skip to content

Commit 632800e

Browse files
authored
Check for 'main' as potential default branch name (#14193)
1 parent c074e46 commit 632800e

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

modules/repository/init.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo
243243
found := false
244244
hasDefault := false
245245
hasMaster := false
246+
hasMain := false
246247
for _, branch := range branches {
247248
if branch == repo.DefaultBranch {
248249
found = true
@@ -251,13 +252,17 @@ func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo
251252
hasDefault = true
252253
} else if branch == "master" {
253254
hasMaster = true
255+
} else if branch == "main" {
256+
hasMain = true
254257
}
255258
}
256259
if !found {
257260
if hasDefault {
258261
repo.DefaultBranch = setting.Repository.DefaultBranch
259262
} else if hasMaster {
260263
repo.DefaultBranch = "master"
264+
} else if hasMain {
265+
repo.DefaultBranch = "main"
261266
} else if len(branches) > 0 {
262267
repo.DefaultBranch = branches[0]
263268
} else {

routers/private/hook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
412412
RepoName: repoName,
413413
}
414414
updates = append(updates, &option)
415-
if repo.IsEmpty && option.IsBranch() && option.BranchName() == "master" {
416-
// put the master branch first
415+
if repo.IsEmpty && option.IsBranch() && (option.BranchName() == "master" || option.BranchName() == "main") {
416+
// put the master/main branch first
417417
copy(updates[1:], updates)
418418
updates[0] = &option
419419
}

services/mirror/mirror.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, re
521521

522522
hasDefault := false
523523
hasMaster := false
524+
hasMain := false
524525
defaultBranchName := m.Repo.DefaultBranch
525526
if len(defaultBranchName) == 0 {
526527
defaultBranchName = setting.Repository.DefaultBranch
@@ -540,13 +541,16 @@ func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, re
540541

541542
hasDefault = hasDefault || name == defaultBranchName
542543
hasMaster = hasMaster || name == "master"
544+
hasMain = hasMain || name == "main"
543545
}
544546

545547
if len(firstName) > 0 {
546548
if hasDefault {
547549
m.Repo.DefaultBranch = defaultBranchName
548550
} else if hasMaster {
549551
m.Repo.DefaultBranch = "master"
552+
} else if hasMain {
553+
m.Repo.DefaultBranch = "main"
550554
} else {
551555
m.Repo.DefaultBranch = firstName
552556
}

0 commit comments

Comments
 (0)