diff options
| author | Jens Baumgart | 2011-03-17 17:15:41 +0000 |
|---|---|---|
| committer | Mathias Kinzler | 2011-03-17 17:15:41 +0000 |
| commit | fd963a9180af73b6a05a5c1a2af6dd7168899170 (patch) | |
| tree | 565fc7fd73c17090961db2a666eb8e604f06b1c4 | |
| parent | 6e2e7280d02f4da25207ec7f54cca2cb32d8f075 (diff) | |
| download | jgit-fd963a9180af73b6a05a5c1a2af6dd7168899170.tar.gz jgit-fd963a9180af73b6a05a5c1a2af6dd7168899170.tar.xz jgit-fd963a9180af73b6a05a5c1a2af6dd7168899170.zip | |
CommitCommand: add option to insert a change id
An option to insert a change id into the commit message was added
to CommitCommand.
This change is a prerequisite for removing GitIndex from EGit.
Change-Id: Iff9e26a8aaf21d8224bfd6ce3c98821c077bcd82
Signed-off-by: Jens Baumgart <jens.baumgart@sap.com>
Signed-off-by: Philipp Thun <philipp.thun@sap.com>
Signed-off-by: Mathias Kinzler <mathias.kinzler@sap.com>
| -rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CommitAndLogCommandTests.java | 61 | ||||
| -rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java | 36 |
2 files changed, 97 insertions, 0 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 d863f45d53..356ba0bfa7 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 @@ -43,6 +43,7 @@ package org.eclipse.jgit.api; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -71,6 +72,7 @@ import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.storage.file.ReflogReader; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.util.FileUtils; +import org.eclipse.jgit.util.RawParseUtils; import org.junit.Test; /** @@ -1159,6 +1161,65 @@ public class CommitAndLogCommandTests extends RepositoryTestCase { + "[d6/f12.txt, mode:100644, content:c12]", indexState(CONTENT)); } + @Test + public void testInsertChangeId() throws NoHeadException, + NoMessageException, + UnmergedPathException, ConcurrentRefUpdateException, + JGitInternalException, WrongRepositoryStateException { + Git git = new Git(db); + String messageHeader = "Some header line\n\nSome detail explanation\n"; + String changeIdTemplate = "\nChange-Id: I" + + ObjectId.zeroId().getName() + "\n"; + String messageFooter = "Some foooter lines\nAnother footer line\n"; + RevCommit commit = git.commit().setMessage( + messageHeader + messageFooter) + .setInsertChangeId(true).call(); + // we should find a real change id (at the end of the file) + byte[] chars = commit.getFullMessage().getBytes(); + int lastLineBegin = RawParseUtils.prevLF(chars, chars.length - 2); + String lastLine = RawParseUtils.decode(chars, lastLineBegin + 1, + chars.length); + assertTrue(lastLine.contains("Change-Id:")); + assertFalse(lastLine.contains( + "Change-Id: I" + ObjectId.zeroId().getName())); + + commit = git.commit().setMessage( + messageHeader + changeIdTemplate + messageFooter) + .setInsertChangeId(true).call(); + // we should find a real change id (in the line as dictated by the + // template) + chars = commit.getFullMessage().getBytes(); + int lineStart = 0; + int lineEnd = 0; + for (int i = 0; i < 4; i++) { + lineStart = RawParseUtils.nextLF(chars, lineStart); + } + lineEnd = RawParseUtils.nextLF(chars, lineStart); + + String line = RawParseUtils.decode(chars, lineStart, lineEnd); + + assertTrue(line.contains("Change-Id:")); + assertFalse(line.contains( + "Change-Id: I" + ObjectId.zeroId().getName())); + + commit = git.commit().setMessage( + messageHeader + changeIdTemplate + messageFooter) + .setInsertChangeId(false).call(); + // we should find the untouched template + chars = commit.getFullMessage().getBytes(); + lineStart = 0; + lineEnd = 0; + for (int i = 0; i < 4; i++) { + lineStart = RawParseUtils.nextLF(chars, lineStart); + } + lineEnd = RawParseUtils.nextLF(chars, lineStart); + + line = RawParseUtils.decode(chars, lineStart, lineEnd); + + assertTrue(commit.getFullMessage().contains( + "Change-Id: I" + ObjectId.zeroId().getName())); + } + @SuppressWarnings("unused") private File prepare_f1_1(final Git git) throws IOException { return writeTrashFile(F1, "c1"); 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 472b778c87..963e3ed842 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java @@ -79,6 +79,7 @@ import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.CanonicalTreeParser; import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.util.ChangeIdUtil; /** * A class used to execute a {@code Commit} command. It has setters for all @@ -104,6 +105,8 @@ public class CommitCommand extends GitCommand<RevCommit> { private boolean amend; + private boolean insertChangeId; + /** * parents this commit should have. The current HEAD will be in this list * and also all commits mentioned in .git/MERGE_HEAD @@ -195,6 +198,9 @@ public class CommitCommand extends GitCommand<RevCommit> { // (unresolved conflicts) ObjectId indexTreeId = index.writeTree(odi); + if (insertChangeId) + insertChangeId(indexTreeId); + // Create a Commit object, populate it and write it CommitBuilder commit = new CommitBuilder(); commit.setCommitter(committer); @@ -260,6 +266,19 @@ public class CommitCommand extends GitCommand<RevCommit> { } } + private void insertChangeId(ObjectId treeId) throws IOException { + ObjectId firstParentId = null; + if (!parents.isEmpty()) + firstParentId = parents.get(0); + ObjectId changeId = ChangeIdUtil.computeChangeId(treeId, firstParentId, + author, committer, message); + message = ChangeIdUtil.insertId(message, changeId); + if (changeId != null) + message = message.replaceAll("\nChange-Id: I" + + ObjectId.zeroId().getName() + "\n", "\nChange-Id: I" + + changeId.getName() + "\n"); + } + private DirCache createTemporaryIndex(ObjectId headId, DirCache index) throws IOException { ObjectInserter inserter = null; @@ -620,4 +639,21 @@ public class CommitCommand extends GitCommand<RevCommit> { this.only.add(o); return this; } + + /** + * If set to true a change id will be inserted into the commit message + * + * An existing change id is not replaced. An initial change id (I000...) + * will be replaced by the change id. + * + * @param insertChangeId + * + * @return {@code this} + */ + public CommitCommand setInsertChangeId(boolean insertChangeId) { + checkCallable(); + this.insertChangeId = insertChangeId; + return this; + } + } |
