Add method to retrieve branch name from repository

This commit is contained in:
René Pfeuffer
2018-09-28 10:14:04 +02:00
parent 487ca6bd6d
commit e35ec519cb
4 changed files with 66 additions and 12 deletions

View File

@@ -499,6 +499,33 @@ public final class GitUtil
return ref;
}
public static String getRepositoryHeadBranchName(org.eclipse.jgit.lib.Repository repo) {
Map<String, Ref> refs = repo.getAllRefs();
Ref lastHeadRef = null;
for (Map.Entry<String, Ref> e : refs.entrySet()) {
String key = e.getKey();
if (REF_HEAD.equals(key)) {
if (e.getValue().isSymbolic() && isBranch(e.getValue().getTarget().getName())) {
return getBranch(e.getValue().getTarget());
}
} else if (key.startsWith(REF_HEAD_PREFIX)) {
if (REF_MASTER.equals(key.substring(REF_HEAD_PREFIX.length()))) {
return REF_MASTER;
} else {
lastHeadRef = e.getValue();
}
}
}
if (lastHeadRef == null) {
return null;
} else {
return getBranch(lastHeadRef);
}
}
/**
* Method description
*
@@ -648,7 +675,7 @@ public final class GitUtil
return tagName;
}
/**
* Method description
*

View File

@@ -97,9 +97,9 @@ public class AbstractGitCommand
return commit;
}
protected String getBranchNameOrDefault(String requestedBranch) {
protected String getBranchNameOrDefault(Repository gitRepository, String requestedBranch) {
if ( Strings.isNullOrEmpty(requestedBranch) ) {
return getDefaultBranchName();
return getDefaultBranchName(gitRepository);
} else {
return requestedBranch;
}
@@ -115,12 +115,12 @@ public class AbstractGitCommand
return head;
}
protected String getDefaultBranchName() {
protected String getDefaultBranchName(Repository gitRepository) {
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
if (!Strings.isNullOrEmpty(defaultBranchName)) {
return defaultBranchName;
} else {
return null;
return GitUtil.getRepositoryHeadBranchName(gitRepository);
}
}

View File

@@ -208,7 +208,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
}
ObjectId head = getBranchOrDefault(repository, request.getBranch());
String branch = getBranchNameOrDefault(request.getBranch());
String branch = getBranchNameOrDefault(repository,request.getBranch());
if (head != null) {
if (startId != null) {

View File

@@ -1,3 +1,4 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
@@ -33,24 +34,24 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.io.Files;
import org.junit.Test;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.GitConstants;
import sonia.scm.repository.Modifications;
import java.io.File;
import java.io.IOException;
import static java.nio.charset.Charset.defaultCharset;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
//~--- JDK imports ------------------------------------------------------------
/**
* Unit tests for {@link GitLogCommand}.
*
@@ -73,7 +74,7 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", result.getChangesets().get(1).getId());
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(2).getId());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(3).getId());
assertNull(result.getBranchName());
assertEquals("master", result.getBranchName());
assertTrue(result.getChangesets().stream().allMatch(r -> r.getBranches().isEmpty()));
// set default branch and fetch again
@@ -215,6 +216,32 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", c2.getId());
}
@Test
public void shouldFindDefaultBranchFromHEAD() throws Exception {
setRepositoryHeadReference("ref: refs/heads/test-branch");
ChangesetPagingResult changesets = createCommand().getChangesets(new LogCommandRequest());
assertEquals("test-branch", changesets.getBranchName());
}
@Test
public void shouldFindMasterBranchWhenHEADisNoRef() throws Exception {
setRepositoryHeadReference("592d797cd36432e591416e8b2b98154f4f163411");
ChangesetPagingResult changesets = createCommand().getChangesets(new LogCommandRequest());
assertEquals("master", changesets.getBranchName());
}
private void setRepositoryHeadReference(String s) throws IOException {
Files.write(s, repositoryHeadReferenceFile(), defaultCharset());
}
private File repositoryHeadReferenceFile() {
return new File(repositoryDirectory, "HEAD");
}
private GitLogCommand createCommand()
{
return new GitLogCommand(createContext(), repository);