{"id":12449,"date":"2015-06-04T17:17:00","date_gmt":"2015-06-04T16:17:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=591e33eeb51372d4e95fcb1952b72dd9"},"modified":"2022-07-29T12:58:42","modified_gmt":"2022-07-29T10:58:42","slug":"git-aliases-for-better-gerrit-usage","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2015\/06\/04\/git-aliases-for-better-gerrit-usage\/","title":{"rendered":"Git aliases for better Gerrit usage"},"content":{"rendered":"<h2 id=\"what-is-gerrit\">What is Gerrit?<\/h2>\n<div><a href=\"https:\/\/code.google.com\/p\/gerrit\/\">Gerrit<\/a> is a web application for code review and git project management. You push commit to specific ref in Gerrit and your collaborators could comment your code, give you a score (-2, -1, 0, 1, 2) or merge it with specific branch. Gerrit generates also events, so yout CI server (for example <a href=\"https:\/\/jenkins-ci.org\/\">Jenkins<\/a>) could start build based on this commit and give the positive score if build is <i>green<\/i>\u00a0or negative if it fails.<\/div>\n<div><\/div>\n<h2 id=\"pushing-commits-to-gerrit\">Pushing commits to gerrit<\/h2>\n<div>If you want to push commit to gerrit, then commit has to have generated Change-Id, which is uniq review identifier. You do not need to generate Change-Id on your own, because you could install pre-commit hook from Gerrit:<\/div>\n<div><\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">gitdir=$(git rev-parse --git-dir); scp -p -P &lt;GERRIT_PORT&gt; &lt;GERRIT_SSH&gt;:hooks\/commit-msg ${gitdir}\/hooks\/<\/pre>\n<\/div>\n<div><\/div>\n<div>Of course, you have to set GERRIT_PORT and GERRIT_SSH to point to yout Gerrit.<\/div>\n<div><\/div>\n<div>To push a commit for review you should use command:<\/div>\n<div><\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git\u00a0push origin HEAD:refs\/for\/&lt;BRANCH_NAME&gt;<\/pre>\n<\/div>\n<div><\/div>\n<div>It means that your current HEAD should be pushed to remote reference on origin (if Gerrit remote repository is named as origin). BRANCH_NAME is the remote branch with which your code will be compared and to which your commit should be merged (if it pass review).<\/div>\n<div><\/div>\n<div>You often push to master so there is alias to push as review for master in alias section in ~\/.gitconfig (globally) or .git\/config (only in current repository):<\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[alias]\r\n\u00a0 ...\r\n\u00a0 push-for-review = push origin HEAD:refs\/for\/master\r\n\u00a0 ...<\/pre>\n<\/div>\n<div>\n<p>To execute it just type:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git push-for-review<\/pre>\n<p>If I want to push as review to another branch then I use another alias:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[alias]\r\n\u00a0 ...\r\n\u00a0 push-for-review-branch = !git push origin HEAD:refs\/for\/$1\r\n\u00a0 ...<\/pre>\n<p>and branch name could be pass as argument from command line:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git push-for-review-branch &lt;BRANCH_NAME&gt;<\/pre>\n<h2 id=\"pushing-drafts\">Pushing drafts<\/h2>\n<\/div>\n<div>If you think that your commit is not ready to merge with remote branch, but you want to share it or just have it in remote repository, you could push it to draft reference. Draft on gerrit is available only for you and other users which are invited by you. Draft could be pushed via command:<\/div>\n<div><\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git\u00a0push origin HEAD:refs\/drafts\/&lt;BRANCH_NAME&gt;<\/pre>\n<\/div>\n<div><\/div>\n<div>Branch name must be given, because draft could be published and then merged, so branch have to be known before.<\/div>\n<div><\/div>\n<div>There also are simple aliases, which could be used in the same way as during push for review:<\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[alias]\r\n\u00a0 ...\r\n\u00a0\u00a0push-as-draft = push origin HEAD:refs\/drafts\/master\r\n\u00a0\u00a0push-as-draft-branch = !git push origin HEAD:refs\/drafts\/$1\r\n\u00a0 ...<\/pre>\n<\/div>\n<h2 id=\"invite-for-review\">Invite for review<\/h2>\n<div>After pushing for review or draft you could invite user or group, then they will be notified by Gerrit about new change. To invite from command line there should be added four aliases:<\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">[alias]\r\n\u00a0 ...\r\n\u00a0 gerrit-remote = \"!sh -c \\\"git remote -v | grep push | grep ssh | grep gerrit | head -1 | awk '{print $2}' | cut -d'\/' -f3\\\"\"\r\n\u00a0 gerrit-host = \"!sh -c \\\"git gerrit-remote | cut -d':' -f1\\\"\"\r\n\u00a0 gerrit-port = \"!sh -c \\\"git gerrit-remote | cut -d':' -f2\\\"\"\r\n\u00a0 gerrit-invite = \"!sh -c \\\"ssh -p <code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">git gerrit-port<\/code> <code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">git gerrit-host<\/code> 'gerrit set-reviewers --add' $1 <code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">git log | grep Change-Id | head -1 | tr -d &#039; &#039; | cut -d&#039;:&#039; -f2<\/code>\\\"\"\r\n\u00a0 ...<\/pre>\n<p>First alias selects remote repository which contains <i>gerrit<\/i> in name or url, could be used to push via ssh and extracts url to this repository.<\/p>\n<p>Second and third alias uses the first to extract host and port from repository url. It is\u00a0necessary for executing remote command via ssh.<\/p>\n<p>The last alias extract Change-Id from HEAD and add user or group given form command line. Example usage:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git\u00a0gerrit-invite &lt;USER_OR_GROUP&gt;<\/pre>\n<\/div>\n<h2 id=\"summary\">Summary<\/h2>\n<div>Gerrit is a great tool for git management and code reviewing, but it is difficult to type all references by memory. Git aliases described here are great support and simplify Gerrit usage.<\/div>\n","protected":false},"excerpt":{"rendered":"What is Gerrit?Gerrit is a web application for code review and git project management. You push commit to specific ref in Gerrit and your collaborators could comment your code, give you a score (-2, -1, 0, 1, 2) or merge it with specific branch. Gerrit&#8230;\n","protected":false},"author":54,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[353,212],"class_list":{"0":"post-12449","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-code-review","8":"tag-git"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12449"}],"version-history":[{"count":7,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12449\/revisions"}],"predecessor-version":[{"id":14716,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12449\/revisions\/14716"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}