mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 08:25:44 +01:00
Add method to retrieve branch name from repository
This commit is contained in:
@@ -499,6 +499,33 @@ public final class GitUtil
|
|||||||
return ref;
|
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
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -97,9 +97,9 @@ public class AbstractGitCommand
|
|||||||
return commit;
|
return commit;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getBranchNameOrDefault(String requestedBranch) {
|
protected String getBranchNameOrDefault(Repository gitRepository, String requestedBranch) {
|
||||||
if ( Strings.isNullOrEmpty(requestedBranch) ) {
|
if ( Strings.isNullOrEmpty(requestedBranch) ) {
|
||||||
return getDefaultBranchName();
|
return getDefaultBranchName(gitRepository);
|
||||||
} else {
|
} else {
|
||||||
return requestedBranch;
|
return requestedBranch;
|
||||||
}
|
}
|
||||||
@@ -115,12 +115,12 @@ public class AbstractGitCommand
|
|||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getDefaultBranchName() {
|
protected String getDefaultBranchName(Repository gitRepository) {
|
||||||
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
|
String defaultBranchName = repository.getProperty(GitConstants.PROPERTY_DEFAULT_BRANCH);
|
||||||
if (!Strings.isNullOrEmpty(defaultBranchName)) {
|
if (!Strings.isNullOrEmpty(defaultBranchName)) {
|
||||||
return defaultBranchName;
|
return defaultBranchName;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return GitUtil.getRepositoryHeadBranchName(gitRepository);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
ObjectId head = getBranchOrDefault(repository, request.getBranch());
|
ObjectId head = getBranchOrDefault(repository, request.getBranch());
|
||||||
String branch = getBranchNameOrDefault(request.getBranch());
|
String branch = getBranchNameOrDefault(repository,request.getBranch());
|
||||||
|
|
||||||
if (head != null) {
|
if (head != null) {
|
||||||
if (startId != null) {
|
if (startId != null) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright (c) 2010, Sebastian Sdorra
|
* Copyright (c) 2010, Sebastian Sdorra
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
@@ -33,24 +34,24 @@
|
|||||||
|
|
||||||
package sonia.scm.repository.spi;
|
package sonia.scm.repository.spi;
|
||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
import com.google.common.io.Files;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import sonia.scm.repository.Changeset;
|
import sonia.scm.repository.Changeset;
|
||||||
import sonia.scm.repository.ChangesetPagingResult;
|
import sonia.scm.repository.ChangesetPagingResult;
|
||||||
import sonia.scm.repository.GitConstants;
|
import sonia.scm.repository.GitConstants;
|
||||||
import sonia.scm.repository.Modifications;
|
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.hamcrest.Matchers.contains;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link GitLogCommand}.
|
* Unit tests for {@link GitLogCommand}.
|
||||||
*
|
*
|
||||||
@@ -73,7 +74,7 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
|
|||||||
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", result.getChangesets().get(1).getId());
|
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", result.getChangesets().get(1).getId());
|
||||||
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(2).getId());
|
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(2).getId());
|
||||||
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(3).getId());
|
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(3).getId());
|
||||||
assertNull(result.getBranchName());
|
assertEquals("master", result.getBranchName());
|
||||||
assertTrue(result.getChangesets().stream().allMatch(r -> r.getBranches().isEmpty()));
|
assertTrue(result.getChangesets().stream().allMatch(r -> r.getBranches().isEmpty()));
|
||||||
|
|
||||||
// set default branch and fetch again
|
// set default branch and fetch again
|
||||||
@@ -215,6 +216,32 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
|
|||||||
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", c2.getId());
|
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()
|
private GitLogCommand createCommand()
|
||||||
{
|
{
|
||||||
return new GitLogCommand(createContext(), repository);
|
return new GitLogCommand(createContext(), repository);
|
||||||
|
|||||||
Reference in New Issue
Block a user