Ensure post-commit hook is called after index lock was released

Otherwise a post-commit hook cannot modify the index.

Bug: 566934
Change-Id: I0093dccd93b2064f243544b516bdce198afdb18b
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2021-03-20 11:15:20 +01:00
parent fd3edc7bfc
commit f43cb3605c
3 changed files with 19 additions and 6 deletions

View File

@ -529,6 +529,7 @@ peeledRefIsRequired=Peeled ref is required.
peerDidNotSupplyACompleteObjectGraph=peer did not supply a complete object graph
personIdentEmailNonNull=E-mail address of PersonIdent must not be null.
personIdentNameNonNull=Name of PersonIdent must not be null.
postCommitHookFailed=Execution of post-commit hook failed: {0}.
prefixRemote=remote:
problemWithResolvingPushRefSpecsLocally=Problem with resolving push ref specs locally: {0}
progressMonUploading=Uploading {0}

View File

@ -67,6 +67,8 @@
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
import org.eclipse.jgit.util.ChangeIdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A class used to execute a {@code Commit} command. It has setters for all
@ -78,6 +80,9 @@
* >Git documentation about Commit</a>
*/
public class CommitCommand extends GitCommand<RevCommit> {
private static final Logger log = LoggerFactory
.getLogger(CommitCommand.class);
private PersonIdent author;
private PersonIdent committer;
@ -212,6 +217,7 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
.setCommitMessage(message).call();
}
RevCommit revCommit;
// lock the index
DirCache index = repo.lockDirCache();
try (ObjectInserter odi = repo.newObjectInserter()) {
@ -267,7 +273,7 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
ObjectId commitId = odi.insert(commit);
odi.flush();
RevCommit revCommit = rw.parseCommit(commitId);
revCommit = rw.parseCommit(commitId);
RefUpdate ru = repo.updateRef(Constants.HEAD);
ru.setNewObjectId(commitId);
if (!useDefaultReflogMessage) {
@ -302,11 +308,7 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
repo.writeMergeCommitMsg(null);
repo.writeRevertHead(null);
}
Hooks.postCommit(repo,
hookOutRedirect.get(PostCommitHook.NAME),
hookErrRedirect.get(PostCommitHook.NAME)).call();
return revCommit;
break;
}
case REJECTED:
case LOCK_FAILURE:
@ -320,6 +322,15 @@ public RevCommit call() throws GitAPIException, AbortedByHookException,
} finally {
index.unlock();
}
try {
Hooks.postCommit(repo, hookOutRedirect.get(PostCommitHook.NAME),
hookErrRedirect.get(PostCommitHook.NAME)).call();
} catch (Exception e) {
log.error(MessageFormat.format(
JGitText.get().postCommitHookFailed, e.getMessage()),
e);
}
return revCommit;
} catch (UnmergedPathException e) {
throw new UnmergedPathsException(e);
} catch (IOException e) {

View File

@ -557,6 +557,7 @@ public static JGitText get() {
/***/ public String peerDidNotSupplyACompleteObjectGraph;
/***/ public String personIdentEmailNonNull;
/***/ public String personIdentNameNonNull;
/***/ public String postCommitHookFailed;
/***/ public String prefixRemote;
/***/ public String problemWithResolvingPushRefSpecsLocally;
/***/ public String progressMonUploading;