make HACKING slightly more generic
[platform/upstream/coreutils.git] / HACKING
1 Coreutils Contribution Guidelines
2
3
4 Prerequisites
5 =============
6 You will need the "git" version control tools.
7 On Fedora-based systems, do "yum install git".
8 On Debian-based ones install the "git-core" package.
9 Then run "git --version".  If that says it's older than
10 version 1.4.4, then you'd do well to get a newer version.
11 At worst, just download the latest stable release from
12 http://git.or.cz/ and build from source.
13
14 For details on building the programs in this package, see
15 the file, README-hacking.
16
17
18 Use the latest upstream sources
19 ===============================
20 Base any changes you make on the latest upstream sources.
21 You can get a copy of the latest with this command:
22
23     git clone git://git.sv.gnu.org/coreutils
24
25 That downloads the entire repository, including revision control history
26 dating back to 1991.  The repository (the part you download, and which
27 resides in coreutils/.git) currently weighs in at about 30MB.  So you
28 don't want to download it more often than necessary.  Once downloaded,
29 you can get incremental updates by running one of these commands from
30 inside your new coreutils/ directory:
31
32 If you have made *no* changes:
33     git pull
34
35 If you *have* made changes and committed them to "master", do this:
36     git fetch
37     git rebase origin
38
39
40 *Before* you commit changes
41 ===========================
42
43 In this project, we much prefer patches that automatically record
44 authorship.  That is important not just to give credit where due, but
45 also from a legal standpoint (see below).  To create author-annotated
46 patches with git, you must first tell git who you are.  That information
47 is best recorded in your ~/.gitconfig file.  Edit that file, creating
48 it if needed, and put your name and email address in place of these
49 example values:
50
51 [user]
52   name = Joe X. User
53   email = joe.user@example.com
54
55
56 Your first commit: the quick and dirty way
57 ==========================================
58 First of all, realize that to "commit" a change in git is a purely
59 local operation.  It affects only the local repository (the .git/ dir)
60 in your current coreutils/ hierarchy.
61
62 To try this out, modify a file or two.  If you create a new file, you'll
63 need to tell git about it with "git add new-file.c".  Commit all changes
64 with "git commit -a".  That prompts you for a log message, which should
65 include a one-line summary, a blank line, and ChangeLog-style entries
66 for all affected files.  More on that below.
67
68 Once your change is committed, you can create a proper patch that includes
69 a log message and authorship information as well as any permissions
70 changes.  Use this command to save that single, most-recent change set:
71
72   git format-patch --stdout --signoff HEAD~1 > DIFF
73
74 The trouble with this approach is that you've just checked in a change
75 (remember, it's only local) on the "master" branch, and that's where new
76 changes would normally appear when you pull the latest from "upstream".
77 When you "pull" from a remote repository to get the latest, your local
78 changes on "master" may well induce conflicts.   For this reason, you
79 may want to keep "master" free of any local changes, so that you can
80 use it to track unadulterated upstream sources.
81
82 However, if your cloned directory is for a one-shot patch submission and
83 you're going to remove it right afterwards, then this approach is fine.
84 Otherwise, for a more sustainable (and more generally useful, IMHO)
85 process, read on about "topic" branches.
86
87
88 Make your changes on a private "topic" branch
89 =============================================
90 So you checked out coreutils like this:
91
92   git clone git://git.sv.gnu.org/coreutils
93
94 Now, cd into the coreutils/ directory and run:
95
96   git checkout -b my-topic
97
98 That creates the my-topic branch and puts you on it.
99 To see which branch you're on, type "git branch".
100 Right after the clone, you were on "master" (aka the trunk).
101 To get back to the trunk, do this:
102
103   git checkout master
104
105 Note 1:
106     Be careful to run "git pull" only when on the "master" branch,
107     not when on a branch.  With newer versions of git, you can't cause
108     trouble if you forget, so this is a good reason to ensure you're
109     using 1.5.3.1 or newer.
110
111 Note 2:
112     It's best not to try to switch from one branch to another if
113     you have pending (uncommitted) changes.  Sometimes it works,
114     sometimes the checkout will fail, telling you that your local
115     modifications conflict with changes required to switch branches.
116     However, in any case, you will *not* lose your uncommitted changes.
117
118 Anyhow, get back onto your just-created branch:
119
120   git checkout my-topic
121
122 Now, modify some file and commit it:
123
124   git commit some-file.c
125
126 Personally, no matter what package I'm working on, I find it useful to
127 put the ChangeLog entries *only* in the commit log, initially, unless
128 I plan to commit/push right away.  Otherwise, I tend to get unnecessary
129 merge conflicts with each rebase (see below).  In coreutils, I've gone
130 a step further, and no longer maintain an explicit ChangeLog file in
131 version control.  Instead, in a git working directory, you can view
132 ChangeLog information via "git log".  However, each distribution tarball
133 does include a ChangeLog file that is automatically generated from the
134 git logs.
135
136 So, you've committed a change.  But it's only in your local repository,
137 and only on your "my-topic" branch.  Let's say you wait a day, and
138 then see that someone else changed something and pushed it to the
139 public repository.  Now, you want to update your trunk and "rebase"
140 your changes on the branch so that they are once again relative to the
141 tip of the trunk.  Currently, your branch is attached to the trunk at
142 the next-to-last change set.
143
144 First: update the trunk from the public repo:
145 [you've first made sure that "git diff" produces no output]
146
147   git checkout master
148   git pull
149
150 Now, return to your branch, and "rebase" relative to trunk (master):
151
152   git checkout my-topic
153   git rebase master
154
155 If there are no conflicts, this requires no more work from you.
156 However, let's say there was one in ChangeLog, since you didn't
157 follow my advice and modified it anyway.
158 git rebase will tell you there was a conflict and in which
159 file, and instruct you to resolve it and then resume with
160 "git rebase --continue" once that's done.
161
162 So you resolve as usual, by editing ChangeLog (which has the
163 usual conflict markers), then type "git rebase --continue".
164 That will fail, with a diagnostic telling you to mark
165 the file as "conflict resolved" by doing this:
166
167   git add ChangeLog
168
169 Then, finally, you can proceed (possibly onto more conflict resolution,
170 if there are conflicts in other files):
171
172   git rebase --continue
173
174 Once it finishes, your changes on the branch are now relative to
175 the tip of the trunk.
176
177 Now use git format-patch, as above.
178
179
180 Amending the most recent change on your private branch
181 ======================================================
182 Let's say you've just committed a change on your private
183 branch, and then realize that something about it is not right.
184 It's easy to adjust:
185
186   edit your files # this can include running "git add NEW" or "git rm BAD"
187   git commit --amend -e -a
188   git format-patch --stdout --signoff HEAD~1 > your-branch.diff
189
190 That replaces the most recent change-set with the revised one.
191
192
193
194 Coreutils-specific:
195
196 No more ChangeLog files
197 =======================
198 Do not modify any of the ChangeLog files in coreutils.  Starting in
199 2008, the policy changed.  Before, we would insert the exact same text
200 (or worse, sometimes slightly differing) into both the ChangeLog file
201 and the commit log.  Now we put that information only in the commit log,
202 and generate the top-level ChangeLog file from logs at "make dist" time.
203 As such, there are strict requirements on the form of the commit log
204 messages.
205
206
207 Commit log requirements
208 =======================
209 Your commit log should always start with a one-line summary, the second
210 line should be blank, and the remaining lines are usually ChangeLog-style
211 entries for all affected files.  Omit the leading TABs that you're used
212 to seeing in a "real" ChangeLog file.
213
214
215 Use SPACE-only indentation in new files.
216 ========================================
217 In any new file, eliminate all leading TABs (e.g., via running GNU indent
218 with --no-tabs) and put these lines at the end of the file:
219 [FIXME: suggest vim syntax to do same thing, if it can be done safely.
220  Most distros now "set nomodeline" by default for a good reason. ]
221
222 /*
223  * Local variables:
224  * indent-tabs-mode: nil
225  * End:
226  */
227
228 Do not change TABs to spaces or vice versa in any existing file.
229
230
231 Send patches to the address listed in --help output
232 ===================================================
233 Please follow the guidelines in the "Sending your patches." section of
234 git's own SubmittingPatches:
235
236   http://git.kernel.org/?p=git/git.git;a=blob;f=Documentation/SubmittingPatches
237
238
239 Add documentation
240 =================
241 If you add a feature or change some user-visible aspect of a program,
242 document it.  If you add an option, document it both in --help output
243 (i.e., in the usage function that generates the --help output) and in
244 doc/*.texi.  The man pages are generated from --help output, so
245 you shouldn't need to change anything under man/.  User-visible changes
246 are usually documented in NEWS, too.
247
248
249 Add tests
250 ==========
251 Nearly every significant change must be accompanied by a test suite
252 addition that exercises it.  If you fix a bug, add at least one test that
253 fails without the patch, but that succeeds once your patch is applied.
254 If you add a feature, add tests to exercise as much of the new code
255 as possible.
256
257 There are hundreds of tests in the tests/ directories.  You can use
258 tests/sample-test as a template, or one of the various Perl-based ones
259 in tests/misc.
260
261 If writing tests is not your thing, don't worry too much about it,
262 but do provide scenarios, input/output pairs, or whatever, along with
263 examples of running the tool to demonstrate the new or changed feature,
264 and someone else will massage that into a test (writing portable tests
265 can be a challenge).
266
267
268 Copyright assignment
269 ====================
270 If your change is significant (i.e., if it adds more than ~10 lines),
271 then you'll have to have a copyright assignment on file with the FSF.
272 Since that involves first an email exchange between you and the FSF,
273 and then the exchange (FSF to you, then back) of an actual sheet of paper
274 with your signature on it, and finally, some administrative processing
275 in Boston, the process can take a few weeks.
276
277 The forms to choose from are in gnulib's doc/Copyright/ directory.
278 If you want to assign a single change, you should use the file,
279 doc/Copyright/request-assign.changes:
280
281     http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=doc/Copyright/request-assign.changes;hb=HEAD
282
283 If you would like to assign past and future contributions to a project,
284 you'd use doc/Copyright/request-assign.future:
285
286     http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=doc/Copyright/request-assign.future;hb=HEAD
287
288 You may make assignments for up to four projects at a time.
289
290
291 Run "make syntax-check", or even "make distcheck"
292 ================================================
293 Making either of those targets runs many integrity and
294 project-specific policy-conformance tests.  For example, the former
295 ensures that you add no trailing blanks and no uses of certain deprecated
296 functions.  The latter performs all "syntax-check" tests, and also
297 ensures that the build completes with no warnings when using a certain
298 set of gcc -W... options.  Don't even bother running "make distcheck"
299 unless you have a reasonably up to date installation including recent
300 versions of gcc and the linux kernel, and modern GNU tools.
301
302
303 Ensure that your changes are indented properly.
304 ===============================================
305 Format the code the way GNU indent does.
306 In a file with the "indent-tabs-mode: nil" directive at the end,
307 running "indent --no-tabs" should induce no change.
308 With other files, there will be some existing differences.
309 Try not to add any more.
310
311
312 -------------------------------------------
313
314 Miscellaneous useful git commands
315 =================================
316
317   * gitk: give a graphical view of the revision graph
318   * git log: to get most of the same info in text form
319   * git log -p: same as above, but with diffs
320   * git log -p SOME_FILE: same as above, but limit to SOME_FILE
321   * git reset --soft HEAD^: Commit the delta required to restore
322       state to the revision just before HEAD (i.e., next-to-last).
323   * git rebase -i master: run this from on a branch, and it gives
324       you an interface with which you can reorder and modify arbitrary
325       change sets on that branch.
326
327 -------------------------------------------
328
329 Finding things to do
330 ====================
331 If you don't know where to start, check out the TODO file for projects
332 that look like they're at your skill-/interest-level.  Another good
333 option is always to improve tests.  You never know what you might
334 uncover when you improve test coverage, and even if you don't find
335 any bugs your contribution is sure to be appreciated.
336
337 A good way to quickly assess current test coverage is to use "lcov"
338 to generate HTML coverage reports.  Follow these steps:
339
340   # configure with coverage information
341   ./configure CFLAGS="-g -fprofile-arcs -ftest-coverage"
342   make
343   # run whatever tests you want, i.e.:
344   make check
345   # run lcov
346   lcov -t coreutils -q -d lib -b lib -o lib.lcov -c
347   lcov -t coreutils -q -d src -b src -o src.lcov -c
348   # generate HTML from the output
349   genhtml -p `pwd` -t coreutils -q --output-directory lcov-html *.lcov
350
351 Then just open the index.html file (in the generated lcov-html directory)
352 in your favorite web browser.