From 632800eda7108e5e8d90dd9e3e985da3deecf365 Mon Sep 17 00:00:00 2001 From: Chester Liu Date: Wed, 30 Dec 2020 23:46:26 +0800 Subject: [PATCH] Check for 'main' as potential default branch name (#14193) --- modules/repository/init.go | 5 +++++ routers/private/hook.go | 4 ++-- services/mirror/mirror.go | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/repository/init.go b/modules/repository/init.go index 16012864b11dd..a100456e778e9 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -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 @@ -251,6 +252,8 @@ 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 { @@ -258,6 +261,8 @@ func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo 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 { diff --git a/routers/private/hook.go b/routers/private/hook.go index 1b9ab000e4be8..34e849f6f7717 100644 --- a/routers/private/hook.go +++ b/routers/private/hook.go @@ -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 } diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index a2acb6f3633c4..328e23ad2fc75 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -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 @@ -540,6 +541,7 @@ 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 { @@ -547,6 +549,8 @@ func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, re m.Repo.DefaultBranch = defaultBranchName } else if hasMaster { m.Repo.DefaultBranch = "master" + } else if hasMain { + m.Repo.DefaultBranch = "main" } else { m.Repo.DefaultBranch = firstName }