Skip to content

Git designed for AI agents.

operations: the start branch beats only a reference with its own name

Federico Elia21afb83de173
diff --git a/internal/gitaly/service/operations/branch_exact_test.go b/internal/gitaly/service/operations/branch_exact_test.go
index 65b38a3..2dfe156 100644
--- a/internal/gitaly/service/operations/branch_exact_test.go
+++ b/internal/gitaly/service/operations/branch_exact_test.go
@@ -845,7 +845,16 @@ func TestResolveStartBranch(t *testing.T) {
{name: "main", refs: []string{"refs/heads/main", "refs/remotes/main"}, expected: "refs/heads/main^{commit}"},
{name: "heads/main", refs: []string{"refs/heads/main", "refs/heads/heads/main"}, expected: "refs/heads/main^{commit}"},
{name: "tags/v1", refs: []string{"refs/tags/v1"}, expected: "refs/tags/v1^{commit}"},
- {name: "tags/v1", refs: []string{"refs/tags/v1", "refs/heads/tags/v1"}, expected: "refs/heads/tags/v1^{commit}"},
+ // A name that names its namespace follows git, whatever branch has that name.
+ {name: "tags/v1", refs: []string{"refs/tags/v1", "refs/heads/tags/v1"}, expected: "refs/tags/v1^{commit}"},
+ {
+ name: "remotes/origin/main",
+ refs: []string{"refs/remotes/origin/main", "refs/heads/remotes/origin/main"},
+ expected: "refs/remotes/origin/main^{commit}",
+ },
+ {name: "a/b", refs: []string{"refs/a/b", "refs/heads/a/b"}, expected: "refs/a/b^{commit}"},
+ // A tag of the same name is beaten by the branch, with or without a namespace in the name.
+ {name: "a/b", refs: []string{"refs/tags/a/b", "refs/heads/a/b"}, expected: "refs/heads/a/b^{commit}"},
{name: "origin/x", refs: []string{"refs/remotes/origin/x"}, expected: "refs/remotes/origin/x^{commit}"},
{name: "origin", refs: []string{"refs/remotes/origin/HEAD"}, expected: "refs/remotes/origin/HEAD^{commit}"},
{name: "main~1", refs: []string{"refs/tags/main", "refs/heads/main"}, expected: "refs/heads/main~1^{commit}"},
diff --git a/internal/gitaly/service/operations/revert.go b/internal/gitaly/service/operations/revert.go
index 3d0b466..ebfbe50 100644
--- a/internal/gitaly/service/operations/revert.go
+++ b/internal/gitaly/service/operations/revert.go
@@ -215,9 +215,11 @@ func (s *Server) UserRevert(ctx context.Context, req *gitalypb.UserRevertRequest
// takes a full object ID (in either case) for the object, a root reference such as HEAD or
// FETCH_HEAD from the repository's directory, and any other name x by its DWIM rules, the first
// that exists of refs/x, refs/tags/x, refs/heads/x, refs/remotes/x and refs/remotes/x/HEAD. The
-// one deviation: when that is refs/x or refs/tags/x, not a branch, and the branch refs/heads/x
-// exists, the branch is taken. An existing branch of the exact name beats a non-branch reference
-// of the same name, such as a tag an agent pushed to shadow it; everything else follows git. When
+// one deviation: when that is a reference with the same name as the branch, the tag refs/tags/x or,
+// when x names no namespace (no /), refs/x, and the branch refs/heads/x exists, the branch is taken.
+// An existing branch of the exact name beats a non-branch reference of the same name, such as a tag
+// an agent pushed to shadow it; a name such as tags/v1 or remotes/origin/main names its namespace,
+// and everything else follows git. When
// no reference is named x, git's resolution answers, by design: a SHA (abbreviated) or another
// revision keeps working as a start point, as before.
//
@@ -275,7 +277,10 @@ func startReference(name string, exists func(git.ReferenceName) (bool, error)) (
if !found {
continue
}
- if (candidate == other || candidate == tag) && !strings.HasPrefix(candidate.String(), "refs/heads/") {
+ // The deviation applies to a reference with the same name as the branch: the tag
+ // refs/tags/x, or refs/x when x names no namespace. A name such as tags/v1 or
+ // remotes/origin/main names its namespace, and follows git.
+ if candidate == tag || (candidate == other && !strings.Contains(name, "/")) {
isBranch, err := exists(branch)
if err != nil {
return "", err