Merge "Do not create reflog for remote tracking branches during clone" into stable-5.13

This commit is contained in:
Matthias Sohn 2022-06-26 15:36:03 -04:00 committed by Gerrit Code Review @ Eclipse.org
commit ca6b518432
3 changed files with 71 additions and 2 deletions

View File

@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.errors.GitAPIException;
@ -114,6 +115,49 @@ public void testCloneRepository() throws IOException,
assertTagOption(git2.getRepository(), TagOpt.AUTO_FOLLOW);
}
@Test
public void testCloneRepository_refLogForLocalRefs()
throws IOException, JGitInternalException, GitAPIException {
File directory = createTempDirectory("testCloneRepository");
CloneCommand command = Git.cloneRepository();
command.setDirectory(directory);
command.setURI(fileUri());
Git git2 = command.call();
Repository clonedRepo = git2.getRepository();
addRepoToClose(clonedRepo);
List<Ref> clonedRefs = clonedRepo.getRefDatabase().getRefs();
Stream<Ref> remoteRefs = clonedRefs.stream()
.filter(CloneCommandTest::isRemote);
Stream<Ref> localHeadsRefs = clonedRefs.stream()
.filter(CloneCommandTest::isLocalHead);
remoteRefs.forEach(ref -> assertFalse(
"Ref " + ref.getName()
+ " is remote and should not have a reflog",
hasRefLog(clonedRepo, ref)));
localHeadsRefs.forEach(ref -> assertTrue(
"Ref " + ref.getName()
+ " is local head and should have a reflog",
hasRefLog(clonedRepo, ref)));
}
private static boolean isRemote(Ref ref) {
return ref.getName().startsWith(Constants.R_REMOTES);
}
private static boolean isLocalHead(Ref ref) {
return !isRemote(ref) && ref.getName().startsWith(Constants.R_HEADS);
}
private static boolean hasRefLog(Repository repo, Ref ref) {
try {
return repo.getReflogReader(ref.getName()).getLastEntry() != null;
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
@Test
public void testCloneRepositoryExplicitGitDir() throws IOException,
JGitInternalException, GitAPIException {

View File

@ -76,6 +76,26 @@ public void testFetch() throws Exception {
db.resolve(tagRef.getObjectId().getName()));
}
@Test
public void testFetchHasRefLogForRemoteRef() throws Exception {
// create an initial commit SHA1 for the default branch
ObjectId defaultBranchSha1 = remoteGit.commit()
.setMessage("initial commit").call().getId();
git.fetch().setRemote("test")
.setRefSpecs("refs/heads/*:refs/remotes/origin/*").call();
List<Ref> allFetchedRefs = git.getRepository().getRefDatabase()
.getRefs();
assertEquals(allFetchedRefs.size(), 1);
Ref remoteRef = allFetchedRefs.get(0);
assertTrue(remoteRef.getName().startsWith(Constants.R_REMOTES));
assertEquals(defaultBranchSha1, remoteRef.getObjectId());
assertNotNull(git.getRepository().getReflogReader(remoteRef.getName())
.getLastEntry());
}
@Test
public void testForcedFetch() throws Exception {
remoteGit.commit().setMessage("commit").call();

View File

@ -205,8 +205,13 @@ else if (tagopt == TagOpt.FETCH_TAGS)
BatchRefUpdate batch = transport.local.getRefDatabase()
.newBatchUpdate()
.setAllowNonFastForwards(true)
.setRefLogMessage("fetch", true); //$NON-NLS-1$
.setAllowNonFastForwards(true);
// Generate reflog only when fetching updates and not at the first clone
if (initialBranch == null) {
batch.setRefLogMessage("fetch", true); //$NON-NLS-1$
}
try (RevWalk walk = new RevWalk(transport.local)) {
walk.setRetainBody(false);
if (monitor instanceof BatchingProgressMonitor) {