Browse Source

Enhance ResetCommand to allow disabling reflog update

This will be used by EGit for implementing commit amend in the staging
view (see Idcd1efeeee8b3065bae36e285bfc0af24ab1e88f).

Change-Id: Ice9ebbb1c0c3314c679f4db40cdd3664f61c27c3
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.5
Matthias Sohn 9 years ago committed by Andrey Loskutov
parent
commit
13f0db25f2
  1. 40
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java
  2. 26
      org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

40
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java

@ -156,6 +156,28 @@ public class ResetCommandTest extends RepositoryTestCase {
assertEquals(prevHead, db.readOrigHead()); assertEquals(prevHead, db.readOrigHead());
} }
@Test
public void testHardResetReflogDisabled() throws Exception {
setupRepository();
ObjectId prevHead = db.resolve(Constants.HEAD);
ResetCommand reset = git.reset();
assertSameAsHead(reset.setMode(ResetType.HARD)
.setRef(initialCommit.getName()).disableRefLog(true).call());
assertTrue("reflog should be disabled", reset.isReflogDisabled());
// check if HEAD points to initial commit now
ObjectId head = db.resolve(Constants.HEAD);
assertEquals(initialCommit, head);
// check if files were removed
assertFalse(indexFile.exists());
assertTrue(untrackedFile.exists());
// fileInIndex must no longer be in HEAD and in the index
String fileInIndexPath = indexFile.getAbsolutePath();
assertFalse(inHead(fileInIndexPath));
assertFalse(inIndex(indexFile.getName()));
assertReflogDisabled(head);
assertEquals(prevHead, db.readOrigHead());
}
@Test @Test
public void testHardResetWithConflicts_DoOverWriteUntrackedFile() public void testHardResetWithConflicts_DoOverWriteUntrackedFile()
throws JGitInternalException, throws JGitInternalException,
@ -562,6 +584,24 @@ public class ResetCommandTest extends RepositoryTestCase {
.getName()); .getName());
} }
private void assertReflogDisabled(ObjectId head)
throws IOException {
// Check the reflog for HEAD
String actualHeadMessage = db.getReflogReader(Constants.HEAD)
.getLastEntry().getComment();
String expectedHeadMessage = "commit: adding a.txt and dir/b.txt";
assertEquals(expectedHeadMessage, actualHeadMessage);
assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
.getLastEntry().getOldId().getName());
// The reflog for master contains the same as the one for HEAD
String actualMasterMessage = db.getReflogReader("refs/heads/master")
.getLastEntry().getComment();
String expectedMasterMessage = "commit: adding a.txt and dir/b.txt";
assertEquals(expectedMasterMessage, actualMasterMessage);
assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
.getLastEntry().getOldId().getName());
}
/** /**
* Checks if a file with the given path exists in the HEAD tree * Checks if a file with the given path exists in the HEAD tree
* *

26
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

@ -123,6 +123,8 @@ public class ResetCommand extends GitCommand<Ref> {
private Collection<String> filepaths = new LinkedList<String>(); private Collection<String> filepaths = new LinkedList<String>();
private boolean isReflogDisabled;
/** /**
* *
* @param repo * @param repo
@ -181,8 +183,12 @@ public class ResetCommand extends GitCommand<Ref> {
ru.setNewObjectId(commitId); ru.setNewObjectId(commitId);
String refName = Repository.shortenRefName(getRefOrHEAD()); String refName = Repository.shortenRefName(getRefOrHEAD());
if (isReflogDisabled) {
ru.disableRefLog();
} else {
String message = refName + ": updating " + Constants.HEAD; //$NON-NLS-1$ String message = refName + ": updating " + Constants.HEAD; //$NON-NLS-1$
ru.setRefLogMessage(message, false); ru.setRefLogMessage(message, false);
}
if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE)
throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().cannotLock, ru.getName())); JGitText.get().cannotLock, ru.getName()));
@ -289,6 +295,26 @@ public class ResetCommand extends GitCommand<Ref> {
return this; return this;
} }
/**
* @param disable
* if {@code true} disables writing a reflog entry for this reset
* command
* @return this instance
* @since 4.5
*/
public ResetCommand disableRefLog(boolean disable) {
this.isReflogDisabled = disable;
return this;
}
/**
* @return {@code true} if writing reflog is disabled for this reset command
* @since 4.5
*/
public boolean isReflogDisabled() {
return this.isReflogDisabled;
}
private String getRefOrHEAD() { private String getRefOrHEAD() {
if (ref != null) if (ref != null)
return ref; return ref;

Loading…
Cancel
Save