summaryrefslogtreecommitdiff
path: root/Documentation/CodingGuidelines
blob: 1a7bc4591cd4b924e066eeecd9c89534d11f6eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
Like other projects, we also have some guidelines to keep to the
code.  For Git in general, a few rough rules are:

 - Most importantly, we never say "It's in POSIX; we'll happily
   ignore your needs should your system not conform to it."
   We live in the real world.

 - However, we often say "Let's stay away from that construct,
   it's not even in POSIX".

 - In spite of the above two rules, we sometimes say "Although
   this is not in POSIX, it (is so convenient | makes the code
   much more readable | has other good characteristics) and
   practically all the platforms we care about support it, so
   let's use it".

   Again, we live in the real world, and it is sometimes a
   judgement call, the decision based more on real world
   constraints people face than what the paper standard says.

 - Fixing style violations while working on a real change as a
   preparatory clean-up step is good, but otherwise avoid useless code
   churn for the sake of conforming to the style.

   "Once it _is_ in the tree, it's not really worth the patch noise to
   go and fix it up."
   Cf. http://lkml.iu.edu/hypermail/linux/kernel/1001.3/01069.html

 - Log messages to explain your changes are as important as the
   changes themselves.  Clearly written code and in-code comments
   explain how the code works and what is assumed from the surrounding
   context.  The log messages explain what the changes wanted to
   achieve and why the changes were necessary (more on this in the
   accompanying SubmittingPatches document).

Make your code readable and sensible, and don't try to be clever.

As for more concrete guidelines, just imitate the existing code
(this is a good guideline, no matter which project you are
contributing to). It is always preferable to match the _local_
convention. New code added to Git suite is expected to match
the overall style of existing code. Modifications to existing
code is expected to match the style the surrounding code already
uses (even if it doesn't match the overall style of existing code).

But if you must have a list of rules, here they are.

For shell scripts specifically (not exhaustive):

 - We use tabs for indentation.

 - Case arms are indented at the same depth as case and esac lines,
   like this:

	case "$variable" in
	pattern1)
		do this
		;;
	pattern2)
		do that
		;;
	esac

 - Redirection operators should be written with space before, but no
   space after them.  In other words, write 'echo test >"$file"'
   instead of 'echo test> $file' or 'echo test > $file'.  Note that
   even though it is not required by POSIX to double-quote the
   redirection target in a variable (as shown above), our code does so
   because some versions of bash issue a warning without the quotes.

	(incorrect)
	cat hello > world < universe
	echo hello >$world

	(correct)
	cat hello >world <universe
	echo hello >"$world"

 - We prefer $( ... ) for command substitution; unlike ``, it
   properly nests.  It should have been the way Bourne spelled
   it from day one, but unfortunately isn't.

 - If you want to find out if a command is available on the user's
   $PATH, you should use 'type <command>', instead of 'which <command>'.
   The output of 'which' is not machine parsable and its exit code
   is not reliable across platforms.

 - We use POSIX compliant parameter substitutions and avoid bashisms;
   namely:

   - We use ${parameter-word} and its [-=?+] siblings, and their
     colon'ed "unset or null" form.

   - We use ${parameter#word} and its [#%] siblings, and their
     doubled "longest matching" form.

   - No "Substring Expansion" ${parameter:offset:length}.

   - No shell arrays.

   - No pattern replacement ${parameter/pattern/string}.

 - We use Arithmetic Expansion $(( ... )).

 - We do not use Process Substitution <(list) or >(list).

 - Do not write control structures on a single line with semicolon.
   "then" should be on the next line for if statements, and "do"
   should be on the next line for "while" and "for".

	(incorrect)
	if test -f hello; then
		do this
	fi

	(correct)
	if test -f hello
	then
		do this
	fi

 - If a command sequence joined with && or || or | spans multiple
   lines, put each command on a separate line and put && and || and |
   operators at the end of each line, rather than the start. This
   means you don't need to use \ to join lines, since the above
   operators imply the sequence isn't finished.

	(incorrect)
	grep blob verify_pack_result \
	| awk -f print_1.awk \
	| sort >actual &&
	...

	(correct)
	grep blob verify_pack_result |
	awk -f print_1.awk |
	sort >actual &&
	...

 - We prefer "test" over "[ ... ]".

 - We do not write the noiseword "function" in front of shell
   functions.

 - We prefer a space between the function name and the parentheses,
   and no space inside the parentheses. The opening "{" should also
   be on the same line.

	(incorrect)
	my_function(){
		...

	(correct)
	my_function () {
		...

 - As to use of grep, stick to a subset of BRE (namely, no \{m,n\},
   [::], [==], or [..]) for portability.

   - We do not use \{m,n\};

   - We do not use -E;

   - We do not use ? or + (which are \{0,1\} and \{1,\}
     respectively in BRE) but that goes without saying as these
     are ERE elements not BRE (note that \? and \+ are not even part
     of BRE -- making them accessible from BRE is a GNU extension).

 - Use Git's gettext wrappers in git-sh-i18n to make the user
   interface translatable. See "Marking strings for translation" in
   po/README.

 - We do not write our "test" command with "-a" and "-o" and use "&&"
   or "||" to concatenate multiple "test" commands instead, because
   the use of "-a/-o" is often error-prone.  E.g.

     test -n "$x" -a "$a" = "$b"

   is buggy and breaks when $x is "=", but

     test -n "$x" && test "$a" = "$b"

   does not have such a problem.

 - Even though "local" is not part of POSIX, we make heavy use of it
   in our test suite.  We do not use it in scripted Porcelains, and
   hopefully nobody starts using "local" before they are reimplemented
   in C ;-)


For C programs:

 - We use tabs to indent, and interpret tabs as taking up to
   8 spaces.

 - We try to keep to at most 80 characters per line.

 - As a Git developer we assume you have a reasonably modern compiler
   and we recommend you to enable the DEVELOPER makefile knob to
   ensure your patch is clear of all compiler warnings we care about,
   by e.g. "echo DEVELOPER=1 >>config.mak".

 - We try to support a wide range of C compilers to compile Git with,
   including old ones.  You should not use features from newer C
   standard, even if your compiler groks them.

   There are a few exceptions to this guideline:

   . since early 2012 with e1327023ea, we have been using an enum
     definition whose last element is followed by a comma.  This, like
     an array initializer that ends with a trailing comma, can be used
     to reduce the patch noise when adding a new identifier at the end.

   . since mid 2017 with cbc0f81d, we have been using designated
     initializers for struct (e.g. "struct t v = { .val = 'a' };").

   . since mid 2017 with 512f41cf, we have been using designated
     initializers for array (e.g. "int array[10] = { [5] = 2 }").

   . since early 2021 with 765dc168882, we have been using variadic
     macros, mostly for printf-like trace and debug macros.

   These used to be forbidden, but we have not heard any breakage
   report, and they are assumed to be safe.

 - Variables have to be declared at the beginning of the block, before
   the first statement (i.e. -Wdeclaration-after-statement).

 - Declaring a variable in the for loop "for (int i = 0; i < 10; i++)"
   is still not allowed in this codebase.

 - NULL pointers shall be written as NULL, not as 0.

 - When declaring pointers, the star sides with the variable
   name, i.e. "char *string", not "char* string" or
   "char * string".  This makes it easier to understand code
   like "char *string, c;".

 - Use whitespace around operators and keywords, but not inside
   parentheses and not around functions. So:

        while (condition)
		func(bar + 1);

   and not:

        while( condition )
		func (bar+1);

 - Do not explicitly compare an integral value with constant 0 or '\0',
   or a pointer value with constant NULL.  For instance, to validate that
   counted array <ptr, cnt> is initialized but has no elements, write:

	if (!ptr || cnt)
		BUG("empty array expected");

   and not:

	if (ptr == NULL || cnt != 0);
		BUG("empty array expected");

 - We avoid using braces unnecessarily.  I.e.

	if (bla) {
		x = 1;
	}

   is frowned upon. But there are a few exceptions:

	- When the statement extends over a few lines (e.g., a while loop
	  with an embedded conditional, or a comment). E.g.:

		while (foo) {
			if (x)
				one();
			else
				two();
		}

		if (foo) {
			/*
			 * This one requires some explanation,
			 * so we're better off with braces to make
			 * it obvious that the indentation is correct.
			 */
			doit();
		}

	- When there are multiple arms to a conditional and some of them
	  require braces, enclose even a single line block in braces for
	  consistency. E.g.:

		if (foo) {
			doit();
		} else {
			one();
			two();
			three();
		}

 - We try to avoid assignments in the condition of an "if" statement.

 - Try to make your code understandable.  You may put comments
   in, but comments invariably tend to stale out when the code
   they were describing changes.  Often splitting a function
   into two makes the intention of the code much clearer.

 - Multi-line comments include their delimiters on separate lines from
   the text.  E.g.

	/*
	 * A very long
	 * multi-line comment.
	 */

   Note however that a comment that explains a translatable string to
   translators uses a convention of starting with a magic token
   "TRANSLATORS: ", e.g.

	/*
	 * TRANSLATORS: here is a comment that explains the string to
	 * be translated, that follows immediately after it.
	 */
	_("Here is a translatable string explained by the above.");

 - Double negation is often harder to understand than no negation
   at all.

 - There are two schools of thought when it comes to comparison,
   especially inside a loop. Some people prefer to have the less stable
   value on the left hand side and the more stable value on the right hand
   side, e.g. if you have a loop that counts variable