Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"git push" to Gerrit with a tracking branch

Pushing changes to Gerrit requires the rather esoteric command

git push origin HEAD:refs/for/branchname

We've scripted this, but I'm looking for a way to do this natively. With git's powerful config, it looks like I can preconfigure most of it[1] so that just git push suffices... almost. I'm stuck on the remote.<name>.push refspec.

I can already create a tracking topic branch so that git pull (no other parameters) will pull changes from upstream remote into my topic branch. However with Gerrit, the push and fetch refspecs aren't the same: one fetches (merges) from refs/heads/trackbranch, but one pushes to refs/for/trackbranch[2]

I can configure a push refspec in remote.<name>.push, however the syntax is pretty basic: if I put push = refs/heads/*:refs/for/*

Then that will try pushing my t-foo topic branch to refs/for/t-foo. But git has the information that t-foo is tracking trackbranch. Can I define a refspec so that git automatically tries to push any t-foo to its refs/for/trackbranch ?

We're currently using a script to do this, and I suppose I could define a push refspec for each topic branch (possibly through more scripting). I'm hoping there's a native git way of doing this so that we don't need to rely on more custom in-house scripts for our team.

[1] by defining a tracking branch with git checkout origin/upstream_branch -b topic_branch or within an existing branch git branch --set-upstream-to origin/upstream_branch

[2] pushing to refs/for/* creates a changeset for review. With appropriate permissions, one could push to refs/heads/* to bypass review, but that negates most of the point of Gerrit.

like image 265
John de Largentaye Avatar asked Oct 20 '22 13:10

John de Largentaye


1 Answers

For some reason :-) I went ahead and tried to write a script to set git config entries, and then found that there seems to be no way to get the desired results directly.

As an alternative, I wrote a script that pushes to the "gerrit review name" (note: I have not actually used gerrit so this is essentially untested, though I've run it with the --dry-run option and it looks like it Does The Right Thing—but I expect you might want to modify it). Of course this is probably functionally the same as your own script.

Anyway, you could then have a global or system-wide alias, git config alias.review or some such, that runs the script, so that you could say git review or git review branch. The alias would just be review = !sh /path/to/script (arguments will get passed on automatically at this point).

I put the script up here: http://web.torek.net/torek/git/gerrit-review.sh.txt (the .txt extension is just so that browsers view it by default, rather than downloading it by default).

like image 137
torek Avatar answered Oct 23 '22 03:10

torek