25

I am configuring Gerrit and I would like to avoid writing:

git push gerrit HEAD:refs/for/master

I would like to write:

git push review

I am sure it's possible modifying .git/config but I can't make it work.

Macarse
  • 91,829
  • 44
  • 175
  • 230

8 Answers8

26

I set up two different push types, review and noreview:

for reviews:

git config remote.review.pushurl ssh://<GERRIT_HOST>:29418/<PROJECT_PATH>.git
git config remote.review.push refs/heads/*:refs/for/*
git push review # this will push your current branch up for review

to bypass review:

git config remote.noreview.pushurl ssh://<GERRIT_HOST>:29418/<PROJECT_PATH>.git
git config remote.noreview.push refs/heads/*
git push noreview # this will push your current branch up, bypassing review

Note that there are some Gerrit Project changes that need to be made by the Project Owner/Gerrit Admin in order to bypass a review as well. I think the "Push" permission will need to be added to the project for refs/* (unless you're getting specific about what branch you'll allow bypassing the review in). However, for reviews, the permissions needed to post in will already be set up. In other words, if your

git push gerrit HEAD:refs/for/master

is working, than the "review" part above should work as well without changing anything else.

RzR
  • 3,068
  • 29
  • 26
Alex Brown
  • 276
  • 3
  • 2
  • 1
    Just to point out, I think this only works if you're only concerned with the master... I suppose you could name a bunch of remotes for each branch you want to deal with, but that'll be almost as annoying as your initial problem. I'm fairly new to Git/Gerrit, so if I figure out a nicer way to handle everything, I'll post it up. – Alex Brown Jan 02 '12 at 21:27
  • 2
    I'd better use `git config remote.review.push HEAD:refs/for/master` I think review for master is more frequent. – Vanuan Dec 06 '12 at 09:45
  • for me this not working - still the dst refspec will be refs/heads/dev when I use this: git push review dev – laplasz Dec 17 '13 at 15:10
5

What you're best off doing is a Git scriptlet in your alias. Something like this works based on the 'upstream' branch, although I'd like to clean this up a little bit still:

~/.gitconfig

[alias]
  pub = "!f() { git push -u ${1:-origin} HEAD:`git config branch.$(git name-rev --name-only HEAD).merge | sed -e 's@refs/heads/@refs/for/@'`; }; f"

This way, you can simply type:

git pub

and it's just like you typed:

git push -u origin HEAD:refs/for/master

or

git push -u origin HEAD:refs/for/myremote-branch

The only requirement for this is that it is only compatible with the Git Bash shell on Windows, or one of the many Linux shells.

Ben Madsen
  • 101
  • 1
  • 4
4

You might also be interested in git-review, which not only eliminates the push by simply providing for:

git review

... but also lets you do things like:

git review branchname

Checkout the Usage section on the link above for more.

Dolph
  • 49,714
  • 13
  • 63
  • 88
4

Why don't create bash alias?

alias review="git push gerrit HEAD:refs/for/master"

Now you can just type:

review

If you want to work on more than one gerrit branch, check my bash helpers for that: https://github.com/tomwys/gerrit-bash-commands

Tomasz Wysocki
  • 11,170
  • 6
  • 47
  • 62
1

And if you want to allow direct push to branch in refs/heads/ you simply add a right in gerrit web gui:

  1. open project properties,
  2. pick access,
  3. Add Reference,
  4. refs/heads/*
  5. pick "Push"
  6. type group name,
  7. refs/heads/*
  8. save.

youre done.

Rafał
  • 582
  • 1
  • 9
  • 14
1

Maybe not exactly what you're looking for, but if you:

  1. Rename gerrit to review (with git remote rename gerrit review),
  2. git config push.default tracking, and
  3. git config branch.master.merge refs/for/master,

then you can git push review, and your master will be pushed to refs/for/master on gerrit.

And, incidentally, if you git config branch.master.remote review, then you can just git push and get the same thing.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • If you change branch.master.merge like in your answer, can you still pull? – Andreas Wederbrand Jan 09 '13 at 20:47
  • 1
    branch.master.merge effectively tells `git pull` what remote ref to fetch and merge. – Ryan Stewart Jan 11 '13 at 15:33
  • 1
    Yes, and there is no remote ref called refs/for/master so I'm pretty sure it won't work – Andreas Wederbrand Jan 11 '13 at 20:42
  • First the question didn't address pulling at all, and second, it's over a year old. If you have a question, I'd suggest posting a new one instead of resurrecting an old one through comments. – Ryan Stewart Jan 12 '13 at 15:47
  • 1
    You're right, I shouldn't have put it as a question. I'm merely pointing out, to anyone finding this (old) answer, that by doing it this way they will fix one problem but introduce another one just as severe (namely that pull won't work). – Andreas Wederbrand Jan 12 '13 at 16:20
0

Those of you who want to push only to master branch through gerrit, add the following to .git\config

[remote "review"]                                                        
    push = HEAD:refs/for/master                 
    pushurl = ssh://GERRIT_URL:29418/PROJECT_NAME

or execute in project dir

git config remote.review.push HEAD:refs/for/master
git config remote.review.pushurl ssh://GERRIT_URL:29418/PROJECT_NAME

Then you can use

git push review

to push to Gerrit. Using this method you can push from any of your local branches and the data will always go to master branch

0

Hi just create a simple script to automate the process:

   #!/bin/bash

   GROUP1_REVIEWER="group1 list of mail separated by space"
   GROUP2_REVIEWER="group2 list of mail separated by space"
   GROUP3_REVIEWER="group3 list of mail separated by space"

   ORIGIN=$(git config --get remote.origin.url)
   [ $? -ne 0 ] && echo "Git repo not found; EXIT" && exit 1

   case $1 in
       group1) REVIEWER_LIST=$GROUP1_REVIEWER ;;
       group2) REVIEWER_LIST=$GROUP2_REVIEWER ;;
       group3) REVIEWER_LIST=$GROUP3_REVIEWER ;;
       *) echo "Please specify one existent group"; exit 1 ;;
   esac

   [ "$1" == "" ] && echo "Please specify one existent group" && exit 1

   pushurl=${ORIGIN}
   push="HEAD:refs/for/master"
   receivepack="$(printf "git receive-pack "; for reviewer in $REVIEWER_LIST ; do printf -- "--reviewer $reviewer "; done)"

   # check if review remote is already present
   git config -l | grep remote.review 1>/dev/null 2>&1
   [ $? -eq 0 ] && echo "Remote review already present; configure it manually; EXIT" && exit 1

   # start config
   echo "Adding new remote review config"
   git config remote.review.pushurl $pushurl
   git config remote.review.push $push
   git config remote.review.receivepack "$receivepack"

You will the use as:

git_review_add_config.sh group1

And select the different group you need to configure.

Zioalex
  • 3,441
  • 2
  • 33
  • 30