From dac66672df0535f61a13273524d46e1e0012ca69 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 21 May 2012 15:00:23 -0700 Subject: Update smudged entries when writing index Overload DirCache.lock to take a repository that is used for updating smudged index entries with information from the repository's working tree. New unit tests are also added for updating smudged index entries on reset, checkout, and commit. Change-Id: I88689f26000e4e57e77931e5ace7c804d92af1b6 --- org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/lib') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index c70c9b0f85..5d9488ae95 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -871,7 +871,7 @@ public abstract class Repository { */ public DirCache readDirCache() throws NoWorkTreeException, CorruptObjectException, IOException { - return DirCache.read(getIndexFile(), getFS()); + return DirCache.read(this); } /** @@ -903,7 +903,7 @@ public abstract class Repository { notifyIndexChanged(); } }; - return DirCache.lock(getIndexFile(), getFS(), l); + return DirCache.lock(this, l); } static byte[] gitInternalSlash(byte[] bytes) { -- cgit v1.2.3 From 058c74d8adcfb5ef0eed203a64b7f9ff65e87c8c Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 10 May 2012 15:56:21 -0700 Subject: Update ORIG_HEAD when resetting Write the old object id from the RefUpdate to the ORIG_HEAD file after the update completes. Add two new convenience methods to Repository to read and write the ORIG_HEAD reference similar to the methods for reading/writing CHERRY_PICK_HEAD and MERGE_HEAD. Bug: 375525 Change-Id: I120b3b2cd3b1ddae88fce435285bae15cbf96f5e --- .../org/eclipse/jgit/api/RebaseCommandTest.java | 4 ++- .../tst/org/eclipse/jgit/api/ResetCommandTest.java | 3 ++ .../src/org/eclipse/jgit/api/RebaseCommand.java | 5 ++-- .../src/org/eclipse/jgit/api/ResetCommand.java | 4 +++ .../src/org/eclipse/jgit/lib/Repository.java | 33 ++++++++++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/lib') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java index 648ef6f01b..cf2dead5a4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java @@ -293,7 +293,8 @@ public class RebaseCommandTest extends RepositoryTestCase { // change third line in topic branch writeTrashFile(FILE1, "1\n2\n3\ntopic\n"); git.add().addFilepattern(FILE1).call(); - git.commit().setMessage("change file1 in topic").call(); + RevCommit origHead = git.commit().setMessage("change file1 in topic") + .call(); RebaseResult res = git.rebase().setUpstream("refs/heads/master").call(); assertEquals(Status.OK, res.getStatus()); @@ -302,6 +303,7 @@ public class RebaseCommandTest extends RepositoryTestCase { assertEquals("refs/heads/topic", db.getFullBranch()); assertEquals(lastMasterChange, new RevWalk(db).parseCommit( db.resolve(Constants.HEAD)).getParent(0)); + assertEquals(origHead, db.readOrigHead()); } @Test diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java index f16d436d94..0806cf0dd0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java @@ -149,6 +149,7 @@ public class ResetCommandTest extends RepositoryTestCase { assertFalse(inHead(fileInIndexPath)); assertFalse(inIndex(indexFile.getName())); assertReflog(prevHead, head); + assertEquals(prevHead, db.readOrigHead()); } @Test @@ -185,6 +186,7 @@ public class ResetCommandTest extends RepositoryTestCase { assertFalse(inHead(fileInIndexPath)); assertTrue(inIndex(indexFile.getName())); assertReflog(prevHead, head); + assertEquals(prevHead, db.readOrigHead()); } @Test @@ -206,6 +208,7 @@ public class ResetCommandTest extends RepositoryTestCase { assertFalse(inIndex(indexFile.getName())); assertReflog(prevHead, head); + assertEquals(prevHead, db.readOrigHead()); } @Test diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java index 5b73657ea7..ecf85932e9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java @@ -575,7 +575,7 @@ public class RebaseCommand extends GitCommand { // create the folder for the meta information FileUtils.mkdir(rebaseDir); - createFile(repo.getDirectory(), Constants.ORIG_HEAD, headId.name()); + repo.writeOrigHead(headId); createFile(rebaseDir, REBASE_HEAD, headId.name()); createFile(rebaseDir, HEAD_NAME, headName); createFile(rebaseDir, ONTO, upstreamCommit.name()); @@ -732,7 +732,8 @@ public class RebaseCommand extends GitCommand { private RebaseResult abort(RebaseResult result) throws IOException { try { - String commitId = readFile(repo.getDirectory(), Constants.ORIG_HEAD); + ObjectId origHead = repo.readOrigHead(); + String commitId = origHead != null ? origHead.name() : null; monitor.beginTask(MessageFormat.format( JGitText.get().abortingRebase, commitId), ProgressMonitor.UNKNOWN); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java index 2f46b7faf3..a6d425ea31 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java @@ -195,6 +195,10 @@ public class ResetCommand extends GitCommand { throw new JGitInternalException(MessageFormat.format( JGitText.get().cannotLock, ru.getName())); + ObjectId origHead = ru.getOldObjectId(); + if (origHead != null) + repo.writeOrigHead(origHead); + switch (mode) { case HARD: checkoutIndex(commit); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index 5d9488ae95..7b9d453aa2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -1247,6 +1247,39 @@ public abstract class Repository { writeHeadsFile(heads, Constants.CHERRY_PICK_HEAD); } + /** + * Write original HEAD commit into $GIT_DIR/ORIG_HEAD. + * + * @param head + * an object id of the original HEAD commit or null + * to delete the file + * @throws IOException + */ + public void writeOrigHead(ObjectId head) throws IOException { + List heads = head != null ? Collections.singletonList(head) + : null; + writeHeadsFile(heads, Constants.ORIG_HEAD); + } + + /** + * Return the information stored in the file $GIT_DIR/ORIG_HEAD. + * + * @return object id from ORIG_HEAD file or {@code null} if this file + * doesn't exist. Also if the file exists but is empty {@code null} + * will be returned + * @throws IOException + * @throws NoWorkTreeException + * if this is bare, which implies it has no working directory. + * See {@link #isBare()}. + */ + public ObjectId readOrigHead() throws IOException, NoWorkTreeException { + if (isBare() || getDirectory() == null) + throw new NoWorkTreeException(); + + byte[] raw = readGitDirectoryFile(Constants.ORIG_HEAD); + return raw != null ? ObjectId.fromString(raw, 0) : null; + } + /** * Read a file from the git directory. * -- cgit v1.2.3