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>
This commit is contained in:
parent
6e2e7280d0
commit
fd963a9180
|
@ -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.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 void testOnlyOptionWithDirectory() throws Exception {
|
|||
+ "[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");
|
||||
|
|
|
@ -79,6 +79,7 @@
|
|||
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 RevCommit call() throws NoHeadException, NoMessageException,
|
|||
// (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 RevCommit call() throws NoHeadException, NoMessageException,
|
|||
}
|
||||
}
|
||||
|
||||
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 CommitCommand setOnly(String only) {
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue