Skip to content

Commit f4f4f79

Browse files
committed
Fix Copybara ls-remote branch parsing for special characters
Update regexes in GitRepository.java to support branch names with special characters (using \S+ instead of \w+). Previously, branch names containing hyphens (e.g., `feature-branch`), dots (e.g., `v1.0.0`), slashes (e.g., `feature/foo`), or emojis (e.g., `branch-🚀`) failed to be handled. BUG: 523347052 GWSQ_IGNORES: chriscampos@google.com PiperOrigin-RevId: 931333663 Change-Id: I6bcd4aa04fad48b57433604120d232b2af25ed08
1 parent ffac707 commit f4f4f79

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

java/com/google/copybara/git/GitRepository.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,12 @@ public class GitRepository {
108108
"([0-9]{6}) (commit|tag|tree|blob) ([a-f0-9]{40})\t(.*)");
109109

110110
private static final Pattern LS_REMOTE_OUTPUT_LINE =
111-
Pattern.compile("([a-f0-9]{40}|ref: refs/heads/\\w+)\t(.+)");
111+
Pattern.compile("([a-f0-9]{40}|ref: refs/heads/\\S+)\t(.+)");
112112

113113
private static final Pattern SHA1_PATTERN = Pattern.compile("[a-f0-9]{6,40}");
114114

115115
private static final Pattern DEFAULT_BRANCH_PATTERN =
116-
Pattern.compile("(?s)ref: (refs/heads/(\\w+)).*");
117-
118-
116+
Pattern.compile("(?s)ref: (refs/heads/(\\S+)).*");
119117

120118
// Pattern for matching URLs with a scheme, such as http:// or rpc://.
121119
private static final Pattern URL_WITH_SCHEME_PATTERN =

javatests/com/google/copybara/git/GitRepositoryTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,16 @@ public void testFindRemotePrimaryBranch() throws Exception {
19461946
.matches("ma.*");
19471947
}
19481948

1949+
@Test
1950+
public void testFindRemotePrimaryBranch_specialCharacters() throws Exception {
1951+
repository.simpleCommand("symbolic-ref", "HEAD", "refs/heads/test-branch.1.x");
1952+
Files.write(workdir.resolve("foo.txt"), new byte[] {});
1953+
repository.add().files("foo.txt").run();
1954+
repository.simpleCommand("commit", "foo.txt", "-m", "message");
1955+
assertThat(repository.getPrimaryBranch("file://" + repository.getGitDir()))
1956+
.isEqualTo("test-branch.1.x");
1957+
}
1958+
19491959
@Test
19501960
public void testFindRemotePrimaryBranch_noHead() throws Exception {
19511961
Files.write(workdir.resolve("foo.txt"), new byte[]{});

0 commit comments

Comments
 (0)