github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/docs/Gerrit/best-practices.md (about) 1 # Gerrit Recommended Practices 2 3 This document presents some best practices to help you use Gerrit more 4 effectively. The intent is to show how content can be submitted easily. Use the 5 recommended practices to reduce your troubleshooting time and improve 6 participation in the community. 7 8 ## Browsing the Git Tree 9 10 Visit [Gerrit](https://gerrit.hyperledger.org/r/#/admin/projects/fabric) then 11 select `Projects --> List --> SELECT-PROJECT --> Branches`. Select the branch 12 that interests you, click on `gitweb` located on the right-hand side. Now, 13 `gitweb` loads your selection on the Git web interface and redirects 14 appropriately. 15 16 ## Watching a Project 17 18 Visit [Gerrit](https://gerrit.hyperledger.org/r/#/admin/projects/fabric), then 19 select `Settings`, located on the top right corner. Select `Watched Projects` 20 and then add any projects that interest you. 21 22 ## Commit Messages 23 24 Gerrit follows the Git commit message format. Ensure the headers are at the 25 bottom and don't contain blank lines between one another. The following example 26 shows the format and content expected in a commit message: 27 28 Brief (no more than 50 chars) one line description. 29 30 Elaborate summary of the changes made referencing why (motivation), what was 31 changed and how it was tested. Note also any changes to documentation made to 32 remain consistent with the code changes, wrapping text at 72 chars/line. 33 34 Jira: FAB-100 35 Change-Id: LONGHEXHASH 36 Signed-off-by: Your Name your.email@example.org 37 AnotherExampleHeader: An Example of another Value 38 39 The Gerrit server provides a precommit hook to autogenerate the Change-Id which 40 is one time use. 41 42 **Recommended reading:** [How to Write a Git Commit Message](http://chris.beams.io/posts/git-commit/) 43 44 ## Avoid Pushing Untested Work to a Gerrit Server 45 46 To avoid pushing untested work to Gerrit. 47 48 Check your work at least three times before pushing your change to Gerrit. 49 Be mindful of what information you are publishing. 50 51 ## Keeping Track of Changes 52 53 - Set Gerrit to send you emails: 54 55 - Gerrit will add you to the email distribution list for a change if a 56 developer adds you as a reviewer, or if you comment on a specific Patch 57 Set. 58 59 - Opening a change in Gerrit's review interface is a quick way to follow that 60 change. 61 62 - Watch projects in the Gerrit projects section at `Gerrit`, select at least 63 *New Changes, New Patch Sets, All Comments* and *Submitted Changes*. 64 65 Always track the projects you are working on; also see the feedback/comments 66 mailing list to learn and help others ramp up. 67 68 ## Topic branches 69 70 Topic branches are temporary branches that you push to commit a set of 71 logically-grouped dependent commits: 72 73 To push changes from `REMOTE/master` tree to Gerrit for being reviewed as 74 a topic in **TopicName** use the following command as an example: 75 76 $ git push REMOTE HEAD:refs/for/master/TopicName 77 78 The topic will show up in the review :abbr:`UI` and in the 79 `Open Changes List`. Topic branches will disappear from the master 80 tree when its content is merged. 81 82 ## Creating a Cover Letter for a Topic 83 84 You may decide whether or not you'd like the cover letter to appear in the 85 history. 86 87 1. To make a cover letter that appears in the history, use this command: 88 89 ``` 90 git commit --allow-empty 91 ``` 92 93 Edit the commit message, this message then becomes the cover letter. 94 The command used doesn't change any files in the source tree. 95 96 2. To make a cover letter that doesn't appear in the history follow these steps: 97 98 - Put the empty commit at the end of your commits list so it can be ignored 99 without having to rebase. 100 101 - Now add your commits 102 103 ``` 104 git commit ... 105 git commit ... 106 git commit ... 107 ``` 108 109 - Finally, push the commits to a topic branch. The following command is an 110 example: 111 112 ``` 113 git push REMOTE HEAD:refs/for/master/TopicName 114 ``` 115 116 If you already have commits but you want to set a cover letter, create an empty 117 commit for the cover letter and move the commit so it becomes the last commit 118 on the list. Use the following command as an example: 119 120 ``` 121 git rebase -i HEAD~#Commits 122 ``` 123 124 Be careful to uncomment the commit before moving it. 125 `#Commits` is the sum of the commits plus your new cover letter. 126 127 128 ## Finding Available Topics 129 130 ``` 131 $ ssh -p 29418 gerrit.hyperledger.org gerrit query \ status:open project:fabric branch:master \ 132 | grep topic: | sort -u 133 ``` 134 135 - [gerrit.hyperledger.org]() Is the current URL where the project is hosted. 136 - *status* Indicates the topic's current status: open , merged, abandoned, draft, 137 merge conflict. 138 - *project* Refers to the current name of the project, in this case fabric. 139 - *branch* The topic is searched at this branch. 140 - *topic* The name of an specific topic, leave it blank to include them all. 141 - *sort* Sorts the found topics, in this case by update (-u). 142 143 ## Downloading or Checking Out a Change 144 145 In the review UI, on the top right corner, the **Download** link provides a 146 list of commands and hyperlinks to checkout or download diffs or files. 147 148 We recommend the use of the *git review* plugin. 149 The steps to install git review are beyond the scope of this document. 150 Refer to the [git review documentation](https://wiki.openstack.org/wiki/Documentation/HowTo/FirstTimers) for the installation process. 151 152 To check out a specific change using Git, the following command usually works: 153 154 ``` 155 git review -d CHANGEID 156 ``` 157 158 If you don't have Git-review installed, the following commands will do the same 159 thing: 160 161 ``` 162 git fetch REMOTE refs/changes/NN/CHANGEIDNN/VERSION \ && git checkout FETCH_HEAD 163 ``` 164 165 For example, for the 4th version of change 2464, NN is the first two digits 166 (24): 167 168 ``` 169 git fetch REMOTE refs/changes/24/2464/4 \ && git checkout FETCH_HEAD 170 ``` 171 172 ## Using Draft Branches 173 174 You can use draft branches to add specific reviewers before you publishing your 175 change. The Draft Branches are pushed to `refs/drafts/master/TopicName` 176 177 The next command ensures a local branch is created: 178 179 ``` 180 git checkout -b BRANCHNAME 181 ``` 182 183 The next command pushes your change to the drafts branch under **TopicName**: 184 185 ``` 186 git push REMOTE HEAD:refs/drafts/master/TopicName 187 ``` 188 189 ## Using Sandbox Branches 190 191 You can create your own branches to develop features. The branches are pushed to 192 the `refs/sandbox/USERNAME/BRANCHNAME` location. 193 194 These commands ensure the branch is created in Gerrit's server. 195 196 ``` 197 git checkout -b sandbox/USERNAME/BRANCHNAME 198 git push --set-upstream REMOTE HEAD:refs/heads/sandbox/USERNAME/BRANCHNAME 199 ``` 200 201 Usually, the process to create content is: 202 203 - develop the code, 204 - break the information into small commits, 205 - submit changes, 206 - apply feedback, 207 - rebase. 208 209 The next command pushes forcibly without review: 210 211 ``` 212 git push REMOTE sandbox/USERNAME/BRANCHNAME 213 ``` 214 215 You can also push forcibly with review: 216 217 ``` 218 git push REMOTE HEAD:ref/for/sandbox/USERNAME/BRANCHNAME 219 ``` 220 221 ## Updating the Version of a Change 222 223 During the review process, you might be asked to update your change. It is 224 possible to submit multiple versions of the same change. Each version of the 225 change is called a patch set. 226 227 Always maintain the **Change-Id** that was assigned. 228 For example, there is a list of commits, **c0...c7**, which were submitted as a 229 topic branch: 230 231 ``` 232 git log REMOTE/master..master 233 234 c0 235 ... 236 c7 237 238 git push REMOTE HEAD:refs/for/master/SOMETOPIC 239 ``` 240 241 After you get reviewers' feedback, there are changes in **c3** and **c4** that 242 must be fixed. If the fix requires rebasing, rebasing changes the commit Ids, 243 see the [rebasing](http://git-scm.com/book/en/v2/Git-Branching-Rebasing) section 244 for more information. However, you must keep the same Change-Id and push the 245 changes again: 246 247 ``` 248 git push REMOTE HEAD:refs/for/master/SOMETOPIC 249 ``` 250 251 This new push creates a patches revision, your local history is then cleared. 252 However you can still access the history of your changes in Gerrit on the 253 `review UI` section, for each change. 254 255 It is also permitted to add more commits when pushing new versions. 256 257 ## Rebasing 258 259 Rebasing is usually the last step before pushing changes to Gerrit; this allows 260 you to make the necessary *Change-Ids*. The *Change-Ids* must be kept the same. 261 262 - **squash:** mixes two or more commits into a single one. 263 - **reword:** changes the commit message. 264 - **edit:** changes the commit content. 265 - **reorder:** allows you to interchange the order of the commits. 266 - **rebase:** stacks the commits on top of the master. 267 268 ## Rebasing During a Pull 269 270 Before pushing a rebase to your master, ensure that the history has a 271 consecutive order. 272 273 For example, your `REMOTE/master` has the list of commits from **a0** to 274 **a4**; Then, your changes **c0...c7** are on top of **a4**; thus: 275 276 ``` 277 git log --oneline REMOTE/master..master 278 279 a0 280 a1 281 a2 282 a3 283 a4 284 c0 285 c1 286 ... 287 c7 288 ``` 289 290 If `REMOTE/master` receives commits **a5**, **a6** and **a7**. Pull with a 291 rebase as follows: 292 293 ``` 294 git pull --rebase REMOTE master 295 ``` 296 297 This pulls **a5-a7** and re-apply **c0-c7** on top of them: 298 299 300 ``` 301 $ git log --oneline REMOTE/master..master 302 a0 303 ... 304 a7 305 c0 306 c1 307 ... 308 c7 309 ``` 310 311 ## Getting Better Logs from Git 312 313 Use these commands to change the configuration of Git in order to produce better 314 logs: 315 316 ``` 317 git config log.abbrevCommit true 318 ``` 319 320 The command above sets the log to abbreviate the commits' hash. 321 322 ``` 323 git config log.abbrev 5 324 ``` 325 326 The command above sets the abbreviation length to the last 5 characters of the 327 hash. 328 329 ``` 330 git config format.pretty oneline 331 ``` 332 333 The command above avoids the insertion of an unnecessary line before the Author 334 line. 335 336 To make these configuration changes specifically for the current Git user, 337 you must add the path option `--global` to `config` as follows: