Browse Source

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
tags/v2.0.0.201206130900-r
Kevin Sawicki 12 years ago
parent
commit
058c74d8ad

+ 3
- 1
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java View File

// change third line in topic branch // change third line in topic branch
writeTrashFile(FILE1, "1\n2\n3\ntopic\n"); writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
git.add().addFilepattern(FILE1).call(); 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(); RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
assertEquals(Status.OK, res.getStatus()); assertEquals(Status.OK, res.getStatus());
assertEquals("refs/heads/topic", db.getFullBranch()); assertEquals("refs/heads/topic", db.getFullBranch());
assertEquals(lastMasterChange, new RevWalk(db).parseCommit( assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
db.resolve(Constants.HEAD)).getParent(0)); db.resolve(Constants.HEAD)).getParent(0));
assertEquals(origHead, db.readOrigHead());
} }


@Test @Test

+ 3
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java View File

assertFalse(inHead(fileInIndexPath)); assertFalse(inHead(fileInIndexPath));
assertFalse(inIndex(indexFile.getName())); assertFalse(inIndex(indexFile.getName()));
assertReflog(prevHead, head); assertReflog(prevHead, head);
assertEquals(prevHead, db.readOrigHead());
} }


@Test @Test
assertFalse(inHead(fileInIndexPath)); assertFalse(inHead(fileInIndexPath));
assertTrue(inIndex(indexFile.getName())); assertTrue(inIndex(indexFile.getName()));
assertReflog(prevHead, head); assertReflog(prevHead, head);
assertEquals(prevHead, db.readOrigHead());
} }


@Test @Test
assertFalse(inIndex(indexFile.getName())); assertFalse(inIndex(indexFile.getName()));


assertReflog(prevHead, head); assertReflog(prevHead, head);
assertEquals(prevHead, db.readOrigHead());
} }


@Test @Test

+ 3
- 2
org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java View File

// create the folder for the meta information // create the folder for the meta information
FileUtils.mkdir(rebaseDir); FileUtils.mkdir(rebaseDir);


createFile(repo.getDirectory(), Constants.ORIG_HEAD, headId.name());
repo.writeOrigHead(headId);
createFile(rebaseDir, REBASE_HEAD, headId.name()); createFile(rebaseDir, REBASE_HEAD, headId.name());
createFile(rebaseDir, HEAD_NAME, headName); createFile(rebaseDir, HEAD_NAME, headName);
createFile(rebaseDir, ONTO, upstreamCommit.name()); createFile(rebaseDir, ONTO, upstreamCommit.name());


private RebaseResult abort(RebaseResult result) throws IOException { private RebaseResult abort(RebaseResult result) throws IOException {
try { try {
String commitId = readFile(repo.getDirectory(), Constants.ORIG_HEAD);
ObjectId origHead = repo.readOrigHead();
String commitId = origHead != null ? origHead.name() : null;
monitor.beginTask(MessageFormat.format( monitor.beginTask(MessageFormat.format(
JGitText.get().abortingRebase, commitId), JGitText.get().abortingRebase, commitId),
ProgressMonitor.UNKNOWN); ProgressMonitor.UNKNOWN);

+ 4
- 0
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java View File

throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().cannotLock, ru.getName())); JGitText.get().cannotLock, ru.getName()));


ObjectId origHead = ru.getOldObjectId();
if (origHead != null)
repo.writeOrigHead(origHead);

switch (mode) { switch (mode) {
case HARD: case HARD:
checkoutIndex(commit); checkoutIndex(commit);

+ 33
- 0
org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java View File

writeHeadsFile(heads, Constants.CHERRY_PICK_HEAD); 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 <code>null</code>
* to delete the file
* @throws IOException
*/
public void writeOrigHead(ObjectId head) throws IOException {
List<ObjectId> 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. * Read a file from the git directory.
* *

Loading…
Cancel
Save