mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-02 19:45:51 +01:00
do not fire hook event, if no new id is specified
This commit is contained in:
@@ -35,6 +35,8 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevSort;
|
||||
@@ -145,58 +147,55 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent
|
||||
*/
|
||||
private List<Changeset> fetchChangesets()
|
||||
{
|
||||
List<Changeset> result = new ArrayList<Changeset>();
|
||||
List<Changeset> result = Lists.newArrayList();
|
||||
GitChangesetConverter converter = null;
|
||||
RevWalk walk = null;
|
||||
org.eclipse.jgit.lib.Repository repository = null;
|
||||
|
||||
if (newId != null)
|
||||
try
|
||||
{
|
||||
GitChangesetConverter converter = null;
|
||||
RevWalk walk = null;
|
||||
org.eclipse.jgit.lib.Repository repository = null;
|
||||
repository = GitUtil.open(directory);
|
||||
converter = new GitChangesetConverter(repository, GitUtil.ID_LENGTH);
|
||||
walk = new RevWalk(repository);
|
||||
walk.reset();
|
||||
walk.sort(RevSort.NONE);
|
||||
walk.markStart(walk.parseCommit(newId));
|
||||
|
||||
try
|
||||
if (oldId != null)
|
||||
{
|
||||
repository = GitUtil.open(directory);
|
||||
converter = new GitChangesetConverter(repository, GitUtil.ID_LENGTH);
|
||||
walk = new RevWalk(repository);
|
||||
walk.reset();
|
||||
walk.sort(RevSort.NONE);
|
||||
walk.markStart(walk.parseCommit(newId));
|
||||
walk.markUninteresting(walk.parseCommit(oldId));
|
||||
}
|
||||
|
||||
if (oldId != null)
|
||||
RevCommit commit = walk.next();
|
||||
|
||||
while (commit != null)
|
||||
{
|
||||
Changeset changeset = converter.createChangeset(commit);
|
||||
|
||||
if (changeset.getBranches().isEmpty() && Util.isNotEmpty(branch))
|
||||
{
|
||||
walk.markUninteresting(walk.parseCommit(oldId));
|
||||
}
|
||||
|
||||
RevCommit commit = walk.next();
|
||||
|
||||
while (commit != null)
|
||||
{
|
||||
Changeset changeset = converter.createChangeset(commit);
|
||||
|
||||
if (changeset.getBranches().isEmpty() && Util.isNotEmpty(branch))
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("set branch to current default branch {}", branch);
|
||||
}
|
||||
|
||||
changeset.getBranches().add(branch);
|
||||
logger.trace("set branch to current default branch {}", branch);
|
||||
}
|
||||
|
||||
result.add(changeset);
|
||||
commit = walk.next();
|
||||
changeset.getBranches().add(branch);
|
||||
}
|
||||
|
||||
result.add(changeset);
|
||||
commit = walk.next();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not fetch changesets", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(converter);
|
||||
GitUtil.release(walk);
|
||||
GitUtil.close(repository);
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error("could not fetch changesets", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(converter);
|
||||
GitUtil.release(walk);
|
||||
GitUtil.close(repository);
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -277,49 +277,16 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
}
|
||||
|
||||
ObjectId newId = rc.getNewId();
|
||||
ObjectId oldId = null;
|
||||
|
||||
if (isUpdateCommand(rc))
|
||||
if (newId != null)
|
||||
{
|
||||
oldId = rc.getOldId();
|
||||
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace(
|
||||
"handle update receive command from commit '{}' to '{}'",
|
||||
oldId.getName(), newId.getName());
|
||||
}
|
||||
onReceive(rpack, rc, newId, type);
|
||||
}
|
||||
else if (logger.isTraceEnabled())
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.trace("handle receive command for commit '{}'",
|
||||
newId.getName());
|
||||
logger.warn("received hook event without new id");
|
||||
}
|
||||
|
||||
File directory = rpack.getRepository().getDirectory();
|
||||
String scriptName = null;
|
||||
|
||||
if (type == RepositoryHookType.POST_RECEIVE)
|
||||
{
|
||||
scriptName = FILE_HOOK_POST_RECEIVE;
|
||||
}
|
||||
else if (type == RepositoryHookType.PRE_RECEIVE)
|
||||
{
|
||||
scriptName = FILE_HOOK_PRE_RECEIVE;
|
||||
}
|
||||
|
||||
if (scriptName != null)
|
||||
{
|
||||
File hookScript = getHookScript(directory, scriptName);
|
||||
|
||||
if (hookScript != null)
|
||||
{
|
||||
executeFileHook(rpack, rc, directory, hookScript, oldId, newId,
|
||||
rc.getRefName());
|
||||
}
|
||||
}
|
||||
|
||||
fireHookEvent(rpack, rc, directory, oldId, newId, type);
|
||||
}
|
||||
else if (logger.isTraceEnabled())
|
||||
{
|
||||
@@ -336,6 +303,61 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param rpack
|
||||
* @param rc
|
||||
* @param newId
|
||||
* @param type
|
||||
*/
|
||||
private void onReceive(ReceivePack rpack, ReceiveCommand rc, ObjectId newId,
|
||||
RepositoryHookType type)
|
||||
{
|
||||
ObjectId oldId = null;
|
||||
|
||||
if (isUpdateCommand(rc))
|
||||
{
|
||||
oldId = rc.getOldId();
|
||||
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("handle update receive command from commit '{}' to '{}'",
|
||||
oldId.getName(), newId.getName());
|
||||
}
|
||||
}
|
||||
else if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("handle receive command for commit '{}'", newId.getName());
|
||||
}
|
||||
|
||||
File directory = rpack.getRepository().getDirectory();
|
||||
String scriptName = null;
|
||||
|
||||
if (type == RepositoryHookType.POST_RECEIVE)
|
||||
{
|
||||
scriptName = FILE_HOOK_POST_RECEIVE;
|
||||
}
|
||||
else if (type == RepositoryHookType.PRE_RECEIVE)
|
||||
{
|
||||
scriptName = FILE_HOOK_PRE_RECEIVE;
|
||||
}
|
||||
|
||||
if (scriptName != null)
|
||||
{
|
||||
File hookScript = getHookScript(directory, scriptName);
|
||||
|
||||
if (hookScript != null)
|
||||
{
|
||||
executeFileHook(rpack, rc, directory, hookScript, oldId, newId,
|
||||
rc.getRefName());
|
||||
}
|
||||
}
|
||||
|
||||
fireHookEvent(rpack, rc, directory, oldId, newId, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user