]> git.cworth.org Git - notmuch-wiki/blob - patchformatting.mdwn
How to Begin: 4. Read `devel/STYLE`
[notmuch-wiki] / patchformatting.mdwn
1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2 # Patch Formatting
3
4 ## How to Begin
5
6 Before you intend to provide patches outside of your local circle
7 you should check the following:
8
9 1. Run `git log` and examine quite a few commit messages.
10
11 2. Read mailing list (archives) and follow the discussions on the patches sent.
12
13 3. Get familiar with coding conventions used.
14
15 4. Read `devel/STYLE` in notmuch source.
16
17 This way you get some insight of the look and feel of the patches sent,
18 both the way code should be written, how to write commit log messages
19 and how to participate patch discussions.
20
21 ## Committing changes (locally)
22
23 After you've been editing your changes under cloned notmuch git repository
24 first commit your changes... preferably (to you) to a separate branch;
25 if you forgot to branch before starting you can do it now -- your modified
26 working tree will follow.
27
28 Enter your commit message in following format:
29
30         first commit line; short one line description
31       
32         After one empty line, a detailed description of your changes
33         the description most usually spans over multiple lines.
34
35 Wrap the lines to about __72__ characters or so. On an 80 column terminal,
36 if we subtract 4 columns for the indent on the left and 4 more for
37 symmetry on the right, we’re left with __72__ columns.
38
39 Regarding the commit message body contents,
40 Carl [has stated](http://article.gmane.org/gmane.mail.notmuch.general/504):
41
42 > The single line summary is good about saying *what* the commit does,
43 > but I always want to see at least one sentence about the *why* as well.
44
45 ### Activating default pre-commit hook
46
47 Git provides a default pre-commit hook which, when activated, checks
48 (at least) for whitespace errors (trailing whitespace and space before
49 tab). It is better to notice this kind of "errors" early than have
50 patch reviewers to mention about those.
51
52 The hook, when activated, is named as .git/hooks/pre-commit and it
53 has execute permissions set on. By default, when git tree is cloned
54 your hooks dir may have default, inactive pre-commit hook available
55 as:
56
57 1. .git/hooks/pre-commit  without execute permission set
58
59 2. .git/hooks/pre-commit.sample  usually with execute permission set
60
61 In case of 2, enter `cp .git/hooks/pre-commit.sample .git/hooks/pre-commit`.
62 And, now enter `chmod a+x .git/hooks/pre-commit` in case it does not
63 have execute permission set.
64
65 ## Remember: one patch per email
66
67 Every patch should (must!) contain only one bugfix or new feature.
68
69 Eric S. Raymond has written good 
70 [Software Release Practice HOWTO](http://tldp.org/HOWTO/Software-Release-Practice-HOWTO/).
71 Check what he has to say about this issue. 
72
73 ### Test Suite Enhancements
74
75 New features as well as bug fixes should typically come with test suite
76 enhancements.  The test suite changes should be done first (tagged as *expected
77 to fail*), and the feature implementation or bug fix should come second
78 (removing the *expected to fail* tag).  This way, the test suite specifies the
79 behavior you're trying to implement, be it a new feature or a bug fix.  By
80 defining beforehand exactly what you expect to happen, everyone can confirm
81 that your patch achieves what it is meant it to.
82
83 ## Prepare patches for e-mail submission
84
85 If you've made just one commit (containing just one bugfix or new feature)
86 you can run
87
88         git format-patch HEAD^
89
90 This outputs something like
91
92         0001-one-line-description.patch
93
94 This is the file name of your patch with content:
95
96         From <-40-character-sha1-hexadecimal-string-> Day Mon DD HH:MM:SS YYYY
97         From: user.name <user.email>
98         Date: Day, DD Mon YYYY HH:MM:SS TZOFF
99         Subject: [PATCH] first commit line; one line description, up to 72 chars
100
101         after one empty line, a detailed description of your patch
102         the description most usually spans over multiple lines.
103         ---
104          <diffstat lines>
105          nn files changed, nn insertions(+) nn deletions(-)
106
107         diff --git a/<1st filename> b/<1st filename>
108         ...
109
110 If you have committed more patches, and want to prepare all of those
111 you can check with `git log` a 40-char commit-sha1 of the last commit
112 *since* you want to generate patch files. When you enter 
113
114         git format-patch <commit-sha1(-prefix)>
115
116 every commit *after* that commit-sha1 will be used to generate 
117 patch files...
118
119 ### Test-applying your patches
120
121 Sometimes you may face a situation with your patches that you are unsure
122 whether those patches apply to the origin. Such a cases might be:
123
124 * You've taken your patches from a branch that has some other commits on top of origin.
125
126 * You have edited the commit message, comments below commit message or the patch content itself in the patch files generated.
127
128 To verify that your patches will apply on top of pristine origin you can 
129 test-apply your patch files on origin/master:
130
131 * Simple case -- no other changes on top of origin/master
132
133             git reset --hard origin/master
134             git pull
135             git am 00*
136
137 * A case where working tree is dirty
138
139             git log -1 --format=%H > head_commit
140             git stash save
141             git reset --hard origin/master
142             git pull
143             git am 00*
144             :
145             git reset --hard `cat head_commit`
146             git stash apply
147             rm head_commit
148             git stash drop
149
150 ## Sending patches
151
152 ### Using git send-email
153
154 (This is the preferred way)
155
156 If you try to execute `git send-email` and you get
157
158         git: 'send-email' is not a git command. See 'git --help'.
159
160 Then you're using git installation where send-email command is distributed
161 in separate package. In Debian/Ububtu/RedHat/Fedora the package is named
162 `git-email`. Use the package manager in your distribution to install this
163 package (or ask administrator to do this if you don't have privileges to do so).
164
165 Playing with `git send-email` is pretty safe. By default it will ask questions,
166 finally whether the email is to be sent or not. In normal cases you may
167 just need to set smtp server (in case local sendmail is not configured to
168 work properly). Check through `git-send-email` manual page and play with it.
169
170 In case of one-file you might want to use
171
172         git send-email --annotate 0001-* 
173
174 (other options omitted) to add a 'discussion' part into your
175 email. The `git am` tool which is eventually used to submit the patch
176 will ignore anything after first `---` and before the `diff --git ...`
177 in the mail message (see example content above). In this case be careful
178 you don't break the commit log message or the patch content.
179
180 In case of multi-patch send, `git send-email --compose 00*.patch` can be
181 used to send an introductory message (as separate email). This also follows
182 the principle of sending only one patch per mail -- by sending each patch
183 in separate mails.
184
185 After you've played (perhaps with `--dry-run`) a bit, send first test emails
186 to your own email address to see how the messages appear in your mailbox. 
187 In this phase you can "streamline" your `git send-email` options for
188 actual patch sending to the mailing list.
189
190 ### Sending one patch using compatible (emacs) email client.
191
192 Sometimes using git-send-email is not possible; It is not installed by
193 default and you don't have privileges to install it or you are not
194 able to compile it as it has more build-time requirements as git itself.
195
196 One alternative way to send your patches is to use, for example, the
197 emacs mail client you've already used to send mails to mailing list.
198 In this case you have to be very careful to keep the patch contents
199 unchanged:
200
201 1. Start composing new mail
202
203 2. Enter notmuch mailing list address into To: field.
204
205 3. Go to the body part of the email
206
207 4. Enter `C-x i` (M-x insert-file) and insert the patch file to the buffer
208
209 5. Replace Subject: line from the Subject line of the patch.
210
211 6. Remove everything before the description content from the beginning of the body. 
212
213 7. Fill the discussion part after `---` unless you have done so (and there is anything to discuss).
214
215 8. Check your text once more and then enter `C-c C-c` (message-send-and-exit).
216
217 When your patches appear on the mailing list read the comments and take part 
218 to the discussion and prepare to do adjustments to your patches.