JGit RevWalk & GitBlit Pre-Receive Hook [message #1259583] |
Fri, 28 February 2014 15:06 |
Michael Quigley Messages: 2 Registered: February 2014 |
Junior Member |
|
|
I'm working on creating a pre-receive hook for GitBlit (which uses JGit). In my pre-receive hook I'm trying to reliably get access to just the new commits when a push is made against a repository. I've borrowed one of the getRevLog methods from Gitblit's JGitUtils class and modified it slightly. I'm using:
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;
}
In my code, startRangeId and endRangeId are coming from the old id and new id retrieved from a ReceiveCommand.
This is doing the right thing whenever a push is made along the master branch, without specifying a branch. So, if I create a repository, make some changes along the master branch, and push it to the server, my hook is seeing the correct changes. Then if I make a couple more changes along the master branch and then just do a 'git push', my hook is seeing just the new changes, not any old changes (and startRangeId is set to a non-zero id). If I branch from an older commit, commit some changes, and then try to push it to the server with 'git push myRepositoryUrl mybranch', my hook is seeing all of the commits from the beginning of time (and my startRangeId is set to the zero id).
Is it possible to implement my hook, such that I can only see the new changes, even when using branches? Or, do I need to implement some sort of tracking database of my own (to make sure my code doesn't re-process commits that it has already processed)?
I'm sure there's something I'm not getting about how RevWalk works, or the structure of commits pushed against a Git repository. Any help would be appreciated!
|
|
|
|
|
|
Re: JGit RevWalk & GitBlit Pre-Receive Hook [message #1265834 is a reply to message #1265831] |
Thu, 06 March 2014 16:18 |
Christian Halstrick Messages: 274 Registered: July 2009 |
Senior Member |
|
|
Sorry, the script had some lines which got cut. Here is the correct script:
#!/bin/bash
set -x
rm -fr central.git developer
# create central repo
git init central
cd central
git commit --allow-empty -m initial
git branch side
cd ..
mv central/.git central.git
rm -fr central
git --git-dir central.git config core.bare true
echo -e '#!/bin/bash \n echo -e "pre-receive:\nrefs:\n$(git show-ref)\nInput:" && while read line ;do echo $line ;done' > central.git/hooks/pre-receive
echo -e '#!/bin/bash \n echo -e "post-receive:\nrefs:\n$(git show-ref)\nInput:" && while read line ;do echo $line ;done' > central.git/hooks/post-receive
chmod +x central.git/hooks/*-receive
# start a daemon
port=$((( RANDOM % 1000 ) + 25000 ))
git daemon --port=$port --verbose --export-all --enable=receive-pack --base-path=. &
sleep 3
# create developers repo, modify and push
git clone git://localhost:$port/central.git developer
cd developer
git commit --allow-empty -m modifiedByDev
git checkout -b side origin/side
git merge master
git checkout master
git commit --allow-empty -m modifiedByDev2
git push origin master:refs/heads/master
git checkout side
git push origin side:refs/heads/side
# stop the daemon
kill %%
Ciao
Chris
|
|
|
Powered by
FUDForum. Page generated in 0.04544 seconds