mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 20:15:52 +01:00
improve trace logging for git repository hooks
This commit is contained in:
@@ -36,7 +36,6 @@ package sonia.scm.repository;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevSort;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
@@ -75,18 +74,34 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent
|
||||
*
|
||||
* @param directory
|
||||
* @param ref
|
||||
* @param refName
|
||||
* @param newId
|
||||
* @param oldId
|
||||
* @param type
|
||||
*/
|
||||
public GitRepositoryHookEvent(File directory, Ref ref, ObjectId newId,
|
||||
public GitRepositoryHookEvent(File directory, String refName, ObjectId newId,
|
||||
ObjectId oldId, RepositoryHookType type)
|
||||
{
|
||||
this.directory = directory;
|
||||
this.defaultBranch = GitUtil.getBranch(ref);
|
||||
this.branch = GitUtil.getBranch(refName);
|
||||
this.newId = newId;
|
||||
this.oldId = oldId;
|
||||
this.type = type;
|
||||
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
//J-
|
||||
logger.trace(
|
||||
"create hook event for branch={}, new-id={}, old-id={} and type={}",
|
||||
new Object[] {
|
||||
refName,
|
||||
GitUtil.getId(newId),
|
||||
GitUtil.getId(oldId),
|
||||
type
|
||||
}
|
||||
);
|
||||
//J+
|
||||
}
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
@@ -158,16 +173,14 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent
|
||||
{
|
||||
Changeset changeset = converter.createChangeset(commit);
|
||||
|
||||
if (changeset.getBranches().isEmpty()
|
||||
&& Util.isNotEmpty(defaultBranch))
|
||||
if (changeset.getBranches().isEmpty() && Util.isNotEmpty(branch))
|
||||
{
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("set branch to current default branch {}",
|
||||
defaultBranch);
|
||||
logger.trace("set branch to current default branch {}", branch);
|
||||
}
|
||||
|
||||
changeset.getBranches().add(defaultBranch);
|
||||
changeset.getBranches().add(branch);
|
||||
}
|
||||
|
||||
result.add(changeset);
|
||||
@@ -192,10 +205,10 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private List<Changeset> changesets;
|
||||
private String branch;
|
||||
|
||||
/** Field description */
|
||||
private String defaultBranch;
|
||||
private List<Changeset> changesets;
|
||||
|
||||
/** Field description */
|
||||
private File directory;
|
||||
|
||||
@@ -226,14 +226,27 @@ public class GitUtil
|
||||
|
||||
if (ref != null)
|
||||
{
|
||||
branch = getBranch(ref.getName());
|
||||
}
|
||||
|
||||
String name = ref.getName();
|
||||
return branch;
|
||||
}
|
||||
|
||||
if (name.startsWith(PREFIX_HEADS))
|
||||
{
|
||||
branch = name.substring(PREFIX_HEADS.length());
|
||||
}
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param name
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getBranch(String name)
|
||||
{
|
||||
String branch = null;
|
||||
|
||||
if (Util.isNotEmpty(name) && name.startsWith(PREFIX_HEADS))
|
||||
{
|
||||
branch = name.substring(PREFIX_HEADS.length());
|
||||
}
|
||||
|
||||
return branch;
|
||||
@@ -329,6 +342,26 @@ public class GitUtil
|
||||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param objectId
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getId(ObjectId objectId)
|
||||
{
|
||||
String id = Util.EMPTY_STRING;
|
||||
|
||||
if (objectId != null)
|
||||
{
|
||||
id = objectId.name();
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -49,6 +49,7 @@ import sonia.scm.io.CommandResult;
|
||||
import sonia.scm.io.SimpleCommand;
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.GitRepositoryHookEvent;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.RepositoryHookType;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.RepositoryNotFoundException;
|
||||
@@ -153,8 +154,9 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
logger.debug("execute file hook '{}' in directoy '{}'");
|
||||
}
|
||||
|
||||
final Command cmd = new SimpleCommand(hook.getAbsolutePath(), getId(oldId),
|
||||
getId(newId), Util.nonNull(refName));
|
||||
final Command cmd = new SimpleCommand(hook.getAbsolutePath(),
|
||||
GitUtil.getId(oldId), GitUtil.getId(newId),
|
||||
Util.nonNull(refName));
|
||||
|
||||
// issue-99
|
||||
cmd.setWorkDirectory(repositoryDirectory);
|
||||
@@ -221,7 +223,7 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
String repositoryName = RepositoryUtil.getRepositoryName(handler,
|
||||
directory);
|
||||
GitRepositoryHookEvent e = new GitRepositoryHookEvent(directory,
|
||||
rc.getRef(), newId, oldId, type);
|
||||
rc.getRefName(), newId, oldId, type);
|
||||
|
||||
repositoryManager.fireHookEvent(GitRepositoryHandler.TYPE_NAME,
|
||||
repositoryName, e);
|
||||
@@ -252,10 +254,11 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
private void onReceive(ReceivePack rpack,
|
||||
Collection<ReceiveCommand> receiveCommands, RepositoryHookType type)
|
||||
{
|
||||
if ( logger.isTraceEnabled() )
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("received git hook, type={}", type);
|
||||
}
|
||||
|
||||
for (ReceiveCommand rc : receiveCommands)
|
||||
{
|
||||
if (isReceiveable(rc, type))
|
||||
@@ -282,13 +285,15 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("handle update receive command from commit '{}' to '{}'",
|
||||
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());
|
||||
logger.trace("handle receive command for commit '{}'",
|
||||
newId.getName());
|
||||
}
|
||||
|
||||
File directory = rpack.getRepository().getDirectory();
|
||||
@@ -365,26 +370,6 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook
|
||||
return IOUtil.getScript(baseFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param objectId
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private String getId(ObjectId objectId)
|
||||
{
|
||||
String id = Util.EMPTY_STRING;
|
||||
|
||||
if (objectId != null)
|
||||
{
|
||||
id = objectId.name();
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user