Skip to content

Commit 73e23e1

Browse files
chris-camposcopybara-github
authored andcommitted
Change full ref path to avoid conflicts
This can cause some conflicts if the given ref is refs/heads/main, for example. In that case, we can't write refs/copybara_fetch/refs/heads/main/copybara_full_reference. This changes this code to save the ref to `refs/copybara_fetch/refs/heads/main_copybara_full_reference`. (note that we are not adding `copybara_full_reference` as a path component, but just as a suffix with an underscore separator. BUG=380307584 PiperOrigin-RevId: 730997697 Change-Id: Ic3244000efc5fdedc8c4b270a26fe6b64f42700e
1 parent df33857 commit 73e23e1

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

Diff for: java/com/google/copybara/git/GitRepository.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public class GitRepository {
169169
public static final String GIT_DESCRIBE_ABBREV = "GIT_DESCRIBE_ABBREV";
170170
public static final String GIT_TAG_POINTS_AT = "GIT_TAG_POINTS_TO";
171171
public static final String HTTP_PERMISSION_DENIED = "The requested URL returned error: 403";
172-
public static final String FULL_REF_NAMESPACE = "copybara_full_ref";
172+
public static final String FULL_REF_NAMESPACE = "_copybara_full_ref";
173173
public static final String COPYBARA_FETCH_NAMESPACE = "refs/copybara_fetch";
174174

175175
/**
@@ -418,7 +418,7 @@ public GitRevision fetchSingleRefWithTags(
418418
.addAll(refspec.build())
419419
.add(
420420
String.format(
421-
"refs/*/%s:%s/refs/*/%s/%s",
421+
"refs/*/%s:%s/refs/*/%s%s",
422422
ref, COPYBARA_FETCH_NAMESPACE, ref, FULL_REF_NAMESPACE));
423423

424424
try {

Diff for: java/com/google/copybara/git/GitRevision.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public Optional<String> fullReference() {
149149
} else {
150150
try {
151151
ImmutableList<String> matchingRefs =
152-
repository.showRef(ImmutableList.of(reference, FULL_REF_NAMESPACE)).entrySet().stream()
152+
repository
153+
.showRef(ImmutableList.of(reference, reference + FULL_REF_NAMESPACE))
154+
.entrySet()
155+
.stream()
153156
.filter(e -> e.getKey().startsWith(COPYBARA_FETCH_NAMESPACE + "/refs/"))
154157
.filter(e -> e.getValue().getSha1().equals(sha1))
155158
.map(Entry::getKey)
@@ -181,9 +184,8 @@ private static String getCleanedFullReference(String originalRef) {
181184
if (fullRef.startsWith(prefix)) {
182185
fullRef = fullRef.substring(prefix.length());
183186
}
184-
String suffix = "/" + FULL_REF_NAMESPACE;
185-
if (fullRef.endsWith(suffix)) {
186-
fullRef = fullRef.substring(0, fullRef.length() - suffix.length());
187+
if (fullRef.endsWith(FULL_REF_NAMESPACE)) {
188+
fullRef = fullRef.substring(0, fullRef.length() - FULL_REF_NAMESPACE.length());
187189
}
188190

189191
return fullRef;

Diff for: javatests/com/google/copybara/git/GitRepositoryTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ public FetchResult fetch(
911911
ImmutableList.of(
912912
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:refs/copybara_fetch/aaaaaaaaaa"
913913
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
914-
"refs/*/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:refs/copybara_fetch/refs/*/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/copybara_full_ref"),
914+
"refs/*/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:refs/copybara_fetch/refs/*/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_copybara_full_ref"),
915915
ImmutableList.of(
916916
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:refs/copybara_fetch/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")));
917917
}

0 commit comments

Comments
 (0)