Skip to content

Commit beac08e

Browse files
author
Yang Guo
committed
fix retrive branch or tag name with refs/heads
1 parent 95f52d4 commit beac08e

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

platform-util-git/src/main/java/com/flow/platform/util/git/JGitBasedClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.flow.platform.util.git;
1818

1919
import com.flow.platform.util.ExceptionUtil;
20+
import com.flow.platform.util.StringUtil;
2021
import com.flow.platform.util.git.model.GitCommit;
2122
import com.flow.platform.util.git.model.GitProject;
2223
import com.google.common.base.Strings;
@@ -246,9 +247,11 @@ private List<String> toRefString(Collection<Ref> refs) {
246247
List<String> refStringList = new ArrayList<>(refs.size());
247248

248249
for (Ref ref : refs) {
249-
// convert ref name from ref/head/master to master
250+
// convert ref name from refs/heads/master to master
250251
String refName = ref.getName();
251-
String simpleName = refName.replaceFirst("refs/heads/", "");
252+
String simpleName = refName
253+
.replaceFirst("refs/heads/", StringUtil.EMPTY)
254+
.replace("refs/tags/", StringUtil.EMPTY);
252255

253256
// add to result list
254257
refStringList.add(simpleName);

platform-util-git/src/test/java/com/flow/platform/util/git/test/GitHttpClientTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.File;
2222
import java.nio.file.Paths;
2323
import java.util.Collection;
24+
import java.util.List;
2425
import java.util.Set;
2526
import org.eclipse.jgit.lib.Ref;
2627
import org.junit.After;
@@ -45,14 +46,16 @@ public void should_load_all_branch_and_tags() throws Throwable {
4546
GitHttpClient client = new GitHttpClient(TEST_GIT_HTTP_URL, Paths.get(tmpPath), "", "");
4647

4748
// load all branches
48-
Collection<String> branches = client.branches();
49+
List<String> branches = client.branches();
4950
Assert.assertNotNull(branches);
5051
Assert.assertTrue(branches.size() >= 1);
52+
Assert.assertFalse(branches.get(0).startsWith("refs/heads/"));
5153

5254
// load all tags
53-
Collection<String> tags = client.tags();
55+
List<String> tags = client.tags();
5456
Assert.assertNotNull(tags);
5557
Assert.assertTrue(tags.size() >= 1);
58+
Assert.assertFalse(tags.get(0).startsWith("refs/tags/"));
5659
}
5760

5861
@Test

platform-util-git/src/test/java/com/flow/platform/util/git/test/GitSshClientTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.File;
2222
import java.nio.file.Paths;
2323
import java.util.Collection;
24+
import java.util.List;
2425
import java.util.Set;
2526
import org.eclipse.jgit.lib.Ref;
2627
import org.junit.After;
@@ -47,14 +48,16 @@ public void should_load_all_branch_and_tags() throws Throwable {
4748
GitSshClient client = new GitSshClient(TEST_GIT_SSH_URL, Paths.get(tmpPath));
4849

4950
// load all branches
50-
Collection<String> branches = client.branches();
51+
List<String> branches = client.branches();
5152
Assert.assertNotNull(branches);
5253
Assert.assertTrue(branches.size() >= 1);
54+
Assert.assertFalse(branches.get(0).startsWith("refs/heads/"));
5355

5456
// load all tags
55-
Collection<String> tags = client.tags();
57+
List<String> tags = client.tags();
5658
Assert.assertNotNull(tags);
5759
Assert.assertTrue(tags.size() >= 1);
60+
Assert.assertFalse(tags.get(0).startsWith("refs/tags/"));
5861
}
5962

6063
@Test

0 commit comments

Comments
 (0)