Skip to content

Commit c96b40d

Browse files
committed
CommitCommand: Don't allow amending on initial commit
Change-Id: I27b13510eb6756da21d0d359d76031da4a875e28
1 parent 6da9473 commit c96b40d

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitCommandTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.io.File;
5151
import java.util.List;
5252

53+
import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
5354
import org.eclipse.jgit.diff.DiffEntry;
5455
import org.eclipse.jgit.dircache.DirCache;
5556
import org.eclipse.jgit.lib.ConfigConstants;
@@ -420,4 +421,10 @@ public void commitAfterSquashMerge() throws Exception {
420421
assertEquals("commit: Squashed commit of the following:", db
421422
.getReflogReader(db.getBranch()).getLastEntry().getComment());
422423
}
424+
425+
@Test(expected = WrongRepositoryStateException.class)
426+
public void commitAmendOnInitialShouldFail() throws Exception {
427+
Git git = new Git(db);
428+
git.commit().setAmend(true).setMessage("initial commit").call();
429+
}
423430
}

org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ commandWasCalledInTheWrongState=Command {0} was called in the wrong state
8686
commitAlreadyExists=exists {0}
8787
commitMessageNotSpecified=commit message not specified
8888
commitOnRepoWithoutHEADCurrentlyNotSupported=Commit on repo without HEAD currently not supported
89+
commitAmendOnInitialNotPossible=Amending is not possible on initial commit.
8990
compressingObjects=Compressing objects
9091
connectionFailed=connection failed
9192
connectionTimeOut=Connection time out: {0}

org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ public RevCommit call() throws GitAPIException, NoHeadException,
176176

177177
// determine the current HEAD and the commit it is referring to
178178
ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}");
179+
if (headId == null && amend)
180+
throw new WrongRepositoryStateException(
181+
JGitText.get().commitAmendOnInitialNotPossible);
182+
179183
if (headId != null)
180184
if (amend) {
181185
RevCommit previousCommit = new RevWalk(repo)

org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public static JGitText get() {
146146
/***/ public String commitAlreadyExists;
147147
/***/ public String commitMessageNotSpecified;
148148
/***/ public String commitOnRepoWithoutHEADCurrentlyNotSupported;
149+
/***/ public String commitAmendOnInitialNotPossible;
149150
/***/ public String compressingObjects;
150151
/***/ public String connectionFailed;
151152
/***/ public String connectionTimeOut;

0 commit comments

Comments
 (0)