aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Zarna2011-03-07 10:33:53 (EST)
committerChris Aniszczyk2011-03-07 11:19:20 (EST)
commitcda64073fd45d3056486b96877e4ff6dd12dbaa5 (patch)
treebfbad54b17495460f1cca2dcfdd9e61e9e9e9f33
parent440d7f41270f18f430b40d4ce3873d84593a5533 (diff)
downloadjgit-cda64073fd45d3056486b96877e4ff6dd12dbaa5.zip
jgit-cda64073fd45d3056486b96877e4ff6dd12dbaa5.tar.gz
jgit-cda64073fd45d3056486b96877e4ff6dd12dbaa5.tar.bz2
Allow to amend a commit with CommitCommandrefs/changes/56/2656/3
Bug: 339088 Change-Id: I57dc727688c4bb6968ac076b176661c857c05afa Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTests.java17
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java28
2 files changed, 43 insertions, 2 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTests.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTests.java
index f8e1e03..5fa2c81 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTests.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTests.java
@@ -238,4 +238,21 @@ public class CommitAndLogCommandTests extends RepositoryTestCase {
}
assertEquals(l, -1);
}
+
+ @Test
+ public void testCommitAmend() throws NoHeadException, NoMessageException,
+ UnmergedPathException, ConcurrentRefUpdateException,
+ JGitInternalException, WrongRepositoryStateException {
+ Git git = new Git(db);
+ git.commit().setMessage("first comit").call(); // typo
+ git.commit().setAmend(true).setMessage("first commit").call();
+
+ Iterable<RevCommit> commits = git.log().call();
+ int c = 0;
+ for (RevCommit commit : commits) {
+ assertEquals("first commit", commit.getFullMessage());
+ c++;
+ }
+ assertEquals(1, c);
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
index 3ec09d4..8ebee49 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
@@ -87,6 +87,8 @@ public class CommitCommand extends GitCommand<RevCommit> {
private boolean all;
+ private boolean amend;
+
/**
* parents this commit should have. The current HEAD will be in this list
* and also all commits mentioned in .git/MERGE_HEAD
@@ -155,7 +157,15 @@ public class CommitCommand extends GitCommand<RevCommit> {
// determine the current HEAD and the commit it is referring to
ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}");
if (headId != null)
- parents.add(0, headId);
+ if (amend) {
+ RevCommit previousCommit = new RevWalk(repo)
+ .parseCommit(headId);
+ RevCommit[] p = previousCommit.getParents();
+ for (int i = 0; i < p.length; i++)
+ parents.add(0, p[i].getId());
+ } else {
+ parents.add(0, headId);
+ }
// lock the index
DirCache index = repo.lockDirCache();
@@ -187,9 +197,10 @@ public class CommitCommand extends GitCommand<RevCommit> {
+ revCommit.getShortMessage(), false);
ru.setExpectedOldObjectId(headId);
- Result rc = ru.update();
+ Result rc = ru.forceUpdate();
switch (rc) {
case NEW:
+ case FORCED:
case FAST_FORWARD: {
setCallable(false);
if (state == RepositoryState.MERGING_RESOLVED) {
@@ -387,4 +398,17 @@ public class CommitCommand extends GitCommand<RevCommit> {
return this;
}
+ /**
+ * Used to amend the tip of the current branch. If set to true, the previous
+ * commit will be amended. This is equivalent to --amend on the command
+ * line.
+ *
+ * @param amend
+ * @return {@code this}
+ */
+ public CommitCommand setAmend(boolean amend) {
+ this.amend = amend;
+ return this;
+ }
+
}