Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jgit-dev] GitBlit Pre/Post Receive Hook

Hi,

I've been struggling trying to figure out the correct way to implement a pre/post-receive hook for GitBlit (which embeds JGit). In my hook, I need to reliably determine which commits are being pushed to the server, despite whichever refs are involved. I've tried a couple of things with various levels of success.

I've also been struggling to find support for this. I'm here now, and I apologize if this is off-topic for this list. But I'm floundering with this, and I'd really appreciate any help you can offer.

I'm using code like this in my hook:

for(ReceiveCommand command : c.commands) {
	log.info("refname = " + command.getRefName());
	
	String oldId = ObjectId.zeroId().getName();
	ObjectId old = r.resolve(command.getRefName());
	if(old != null) {
		oldId = old.getName();
	}
	String newId = command.getNewId().getName();
log.info("Inspecting commits in range [" + oldId + "] -> [" + newId + "].");
	
	List<RevCommit> commits = GitUtility.getRevLog(r, oldId, newId);
	Collections.reverse(commits);

	log.info("Found " + commits.size() + " commits.");
}

And GitUtility.getRevlog() looks like this:

public static List<RevCommit> getRevLog(Repository repository, String startRangeId, String endRangeId) {
	List<RevCommit> list = new ArrayList<RevCommit>();
	if(!hasCommits(repository)) {
		return list;
	}
		
	try {
		ObjectId endRange = repository.resolve(endRangeId);
		ObjectId startRange = repository.resolve(startRangeId);

		RevWalk rw = new RevWalk(repository);
		rw.markStart(rw.parseCommit(endRange));
		if(!startRange.equals(ObjectId.zeroId())) {
		       rw.markUninteresting(rw.parseCommit(startRange));
		}

		Iterable<RevCommit> revlog = rw;
		for(RevCommit rev : revlog) {
			list.add(rev);
		}
			
		rw.dispose();
			
	} catch (Throwable t) {
		log.error("Failure.", t);
	}
		
	return list;
}

This works fine most of the time. But for some pushes on branches and tags, this code ends up traversing the entire commit history, which is a huge problem for the integration I'm trying to develop.

Can anyone point me in the right direction towards how to build a rock-solid pre/post-receive hook, which can determine only those commits that were contained in a push, even in the face of various branches and tags?

I'm sure there are things about the Git object model that I'm not clear on.

Thanks so much for your time and assistance.

Michael


Back to the top