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