Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeger-Jan van de Weg <git@zjvandeweg.nl>2021-05-25 10:52:57 +0300
committerVasilii Iakliushin <viakliushin@gitlab.com>2021-06-30 18:58:36 +0300
commitf3615215ae61f1f6ef1ddf58d43fdaafa9bc90bb (patch)
tree637c4fd13e53c7e9f3e1d4b7c164291a3c77cb8d
parent82a7a8e90f5bf3f0cae18d158a28eb8a7a1693c6 (diff)
HEAD: default to main for new repositoriesvi-switch-default-ref-main-2
HEAD in bare repositories has the function to determine what the default reference is. This helps GitLab decide what the target default is for merge requests and what README to render on the project page. This change changes the default for new repositories to `main` over `master`. For almost no-one this would influence their current repository. There's an exception though; old repositories might have a corrupt HEAD reference, e.g. it does not exist. In that case Gitaly now will favour main over master. Changelog: changed
-rw-r--r--internal/git/repository.go9
-rw-r--r--internal/gitaly/service/ref/refs.go8
-rw-r--r--internal/gitaly/service/ref/refs_test.go25
3 files changed, 35 insertions, 7 deletions
diff --git a/internal/git/repository.go b/internal/git/repository.go
index 76808fc8b..e5f5b25d5 100644
--- a/internal/git/repository.go
+++ b/internal/git/repository.go
@@ -8,13 +8,18 @@ import (
"gitlab.com/gitlab-org/gitaly/v14/internal/git/repository"
)
-// DefaultBranch now defaults to master, as that's the Git default
-const DefaultBranch = "master"
+// DefaultBranch is the default reference written to HEAD when a repository is created
+const DefaultBranch = "main"
// DefaultRef is the reference that GitLab will use if HEAD of the bare repository
// is not found, or other edge cases to detect the default branch.
var DefaultRef = []byte("refs/heads/" + DefaultBranch)
+// LegacyDefaultRef is the reference that used to be the default HEAD of the bare
+// repository. If the default reference is not found, Gitaly will still test the
+// legacy default.
+var LegacyDefaultRef = []byte("refs/heads/master")
+
var (
// ErrReferenceNotFound represents an error when a reference was not
// found.
diff --git a/internal/gitaly/service/ref/refs.go b/internal/gitaly/service/ref/refs.go
index 107c469bb..53e5709cb 100644
--- a/internal/gitaly/service/ref/refs.go
+++ b/internal/gitaly/service/ref/refs.go
@@ -241,7 +241,7 @@ func DefaultBranchName(ctx context.Context, repo git.RepositoryExecutor) ([]byte
return branches[0], nil
}
- hasDefaultRef := false
+ var hasDefaultRef, hasLegacyDefaultRef = false, false
headRef, err := headReference(ctx, repo)
if err != nil {
return nil, err
@@ -256,6 +256,8 @@ func DefaultBranchName(ctx context.Context, repo git.RepositoryExecutor) ([]byte
if bytes.Equal(branch, git.DefaultRef) {
hasDefaultRef = true
}
+
+ hasLegacyDefaultRef = hasLegacyDefaultRef || bytes.Equal(branch, git.LegacyDefaultRef)
}
// Return the default ref if it exists
@@ -263,6 +265,10 @@ func DefaultBranchName(ctx context.Context, repo git.RepositoryExecutor) ([]byte
return git.DefaultRef, nil
}
+ if hasLegacyDefaultRef {
+ return git.LegacyDefaultRef, nil
+ }
+
// If all else fails, return the first branch name
return branches[0], nil
}
diff --git a/internal/gitaly/service/ref/refs_test.go b/internal/gitaly/service/ref/refs_test.go
index f07a61e41..f2e3b89da 100644
--- a/internal/gitaly/service/ref/refs_test.go
+++ b/internal/gitaly/service/ref/refs_test.go
@@ -230,7 +230,7 @@ func TestHeadReference(t *testing.T) {
headRef, err := headReference(ctx, localrepo.NewTestRepo(t, cfg, repo))
require.NoError(t, err)
- require.Equal(t, git.DefaultRef, headRef)
+ require.Equal(t, git.LegacyDefaultRef, headRef)
}
func TestHeadReferenceWithNonExistingHead(t *testing.T) {
@@ -269,7 +269,7 @@ func TestSetDefaultBranchRef(t *testing.T) {
{
desc: "unknown ref",
ref: "refs/heads/non_existent_ref",
- expectedRef: "refs/heads/master",
+ expectedRef: string(git.LegacyDefaultRef),
},
}
@@ -332,7 +332,7 @@ func TestDefaultBranchName(t *testing.T) {
},
{
desc: "Get `ref/heads/master` when several branches exist",
- expected: git.DefaultRef,
+ expected: git.LegacyDefaultRef,
findBranchNames: func(context.Context, git.RepositoryExecutor) ([][]byte, error) {
return [][]byte{[]byte("refs/heads/foo"), []byte("refs/heads/master"), []byte("refs/heads/bar")}, nil
},
@@ -363,9 +363,14 @@ func TestDefaultBranchName(t *testing.T) {
}
func TestSuccessfulFindDefaultBranchName(t *testing.T) {
- _, repo, _, client := setupRefService(t)
+ cfg, repo, repoPath, client := setupRefService(t)
rpcRequest := &gitalypb.FindDefaultBranchNameRequest{Repository: repo}
+ // The testing repository has no main branch, so we create it and update
+ // HEAD to it
+ gittest.Exec(t, cfg, "-C", repoPath, "update-ref", "refs/heads/main", "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863")
+ gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/main")
+
ctx, cancel := testhelper.Context()
defer cancel()
r, err := client.FindDefaultBranchName(ctx, rpcRequest)
@@ -374,6 +379,18 @@ func TestSuccessfulFindDefaultBranchName(t *testing.T) {
require.Equal(t, r.GetName(), git.DefaultRef)
}
+func TestSuccessfulFindDefaultBranchNameLegacy(t *testing.T) {
+ _, repo, _, client := setupRefService(t)
+ rpcRequest := &gitalypb.FindDefaultBranchNameRequest{Repository: repo}
+
+ ctx, cancel := testhelper.Context()
+ defer cancel()
+ r, err := client.FindDefaultBranchName(ctx, rpcRequest)
+ require.NoError(t, err)
+
+ require.Equal(t, r.GetName(), git.LegacyDefaultRef)
+}
+
func TestEmptyFindDefaultBranchNameRequest(t *testing.T) {
_, client := setupRefServiceWithoutRepo(t)
rpcRequest := &gitalypb.FindDefaultBranchNameRequest{}