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:
authorJames Fargher <jfargher@gitlab.com>2023-06-29 06:50:28 +0300
committerJames Fargher <jfargher@gitlab.com>2023-06-30 00:23:34 +0300
commit5beb977ded04e0ac544ff0066df61caeca69f8e4 (patch)
tree9333f2884ac8cd8ea2ca64bff2cdd61e0adf45ec
parent5ed23b191c0cf22a382c2c23a78e492292616f5c (diff)
remoterepo: Add HeadReference
HeadReference is a replacement for GetDefaultBranch. The intention is to replace and remove the default branch heuristic that GetDefaultBranch uses and instead rely solely on HEAD. This could have been implemented as a parameter on GetDefaultBranch but this requires modifying all call sites and localrepo already has a method called HeadReference for this purpose.
-rw-r--r--internal/git/gittest/repository_suite.go61
-rw-r--r--internal/git/remoterepo/repository.go15
-rw-r--r--internal/git/remoterepo/repository_test.go2
-rw-r--r--internal/git/repository.go3
4 files changed, 80 insertions, 1 deletions
diff --git a/internal/git/gittest/repository_suite.go b/internal/git/gittest/repository_suite.go
index f9c64c293..d7310da5c 100644
--- a/internal/git/gittest/repository_suite.go
+++ b/internal/git/gittest/repository_suite.go
@@ -33,6 +33,10 @@ func TestRepository(t *testing.T, cfg config.Cfg, getRepository GetRepositoryFun
desc: "GetDefaultBranch",
test: testRepositoryGetDefaultBranch,
},
+ {
+ desc: "HeadReference",
+ test: testRepositoryHeadReference,
+ },
} {
t.Run(tc.desc, func(t *testing.T) {
tc.test(t, cfg, getRepository)
@@ -190,3 +194,60 @@ func testRepositoryGetDefaultBranch(t *testing.T, cfg config.Cfg, getRepository
})
}
}
+
+func testRepositoryHeadReference(t *testing.T, cfg config.Cfg, getRepository GetRepositoryFunc) {
+ t.Parallel()
+ ctx := testhelper.Context(t)
+
+ for _, tc := range []struct {
+ desc string
+ repo func(t *testing.T) git.Repository
+ expectedName git.ReferenceName
+ }{
+ {
+ desc: "default unborn",
+ repo: func(t *testing.T) git.Repository {
+ repo, _ := getRepository(t, ctx)
+ return repo
+ },
+ expectedName: git.DefaultRef,
+ },
+ {
+ desc: "default exists",
+ repo: func(t *testing.T) git.Repository {
+ repo, repoPath := getRepository(t, ctx)
+ WriteCommit(t, cfg, repoPath, WithBranch(git.DefaultBranch))
+ return repo
+ },
+ expectedName: git.DefaultRef,
+ },
+ {
+ desc: "HEAD set nonexistent",
+ repo: func(t *testing.T) git.Repository {
+ repo, repoPath := getRepository(t, ctx)
+ Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/feature")
+ return repo
+ },
+ expectedName: git.NewReferenceNameFromBranchName("feature"),
+ },
+ {
+ desc: "HEAD set exists",
+ repo: func(t *testing.T) git.Repository {
+ repo, repoPath := getRepository(t, ctx)
+ WriteCommit(t, cfg, repoPath, WithBranch("feature"))
+ Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/feature")
+ return repo
+ },
+ expectedName: git.NewReferenceNameFromBranchName("feature"),
+ },
+ } {
+ tc := tc
+
+ t.Run(tc.desc, func(t *testing.T) {
+ t.Parallel()
+ name, err := tc.repo(t).HeadReference(ctx)
+ require.NoError(t, err)
+ require.Equal(t, tc.expectedName, name)
+ })
+ }
+}
diff --git a/internal/git/remoterepo/repository.go b/internal/git/remoterepo/repository.go
index a7d946cd8..4b1f2eb8b 100644
--- a/internal/git/remoterepo/repository.go
+++ b/internal/git/remoterepo/repository.go
@@ -113,3 +113,18 @@ func (rr *Repo) GetDefaultBranch(ctx context.Context) (git.ReferenceName, error)
return git.ReferenceName(resp.Name), nil
}
+
+// HeadReference returns the reference that HEAD points to for the remote
+// repository.
+func (rr *Repo) HeadReference(ctx context.Context) (git.ReferenceName, error) {
+ resp, err := gitalypb.NewRefServiceClient(rr.conn).FindDefaultBranchName(
+ ctx, &gitalypb.FindDefaultBranchNameRequest{
+ Repository: rr.Repository,
+ HeadOnly: true,
+ })
+ if err != nil {
+ return "", err
+ }
+
+ return git.ReferenceName(resp.Name), nil
+}
diff --git a/internal/git/remoterepo/repository_test.go b/internal/git/remoterepo/repository_test.go
index 61edd5f83..21fcab898 100644
--- a/internal/git/remoterepo/repository_test.go
+++ b/internal/git/remoterepo/repository_test.go
@@ -26,7 +26,7 @@ func TestRepository(t *testing.T) {
cfg := setupGitalyServer(t)
pool := client.NewPool()
- defer pool.Close()
+ t.Cleanup(func() { testhelper.MustClose(t, pool) })
gittest.TestRepository(t, cfg, func(tb testing.TB, ctx context.Context) (git.Repository, string) {
tb.Helper()
diff --git a/internal/git/repository.go b/internal/git/repository.go
index b3cf9725f..fe999d9ac 100644
--- a/internal/git/repository.go
+++ b/internal/git/repository.go
@@ -49,6 +49,9 @@ type Repository interface {
HasBranches(ctx context.Context) (bool, error)
// GetDefaultBranch returns the default branch of the repository.
GetDefaultBranch(ctx context.Context) (ReferenceName, error)
+ // HeadReference returns the reference that HEAD points to for the
+ // repository.
+ HeadReference(ctx context.Context) (ReferenceName, error)
}
// RepositoryExecutor is an interface which allows execution of Git commands in a specific