// Structure named 'struct S'
struct strbuf;
// Functions are 'S_<verb>()' and take 'struct S *' first
void strbuf_add(struct strbuf *buf, ...);
void strbuf_reset(struct strbuf *buf);
// NOT:
void add_string(struct strbuf *buf, ...); // avoid
void reset_strbuf(struct strbuf *buf); // avoid
General Principles
POSIX Compatibility
We never say “It’s in POSIX; we’ll ignore your needs.” We live in the real world and support practical platforms.
Avoid Non-POSIX
We often say “Let’s stay away from that construct, it’s not even in POSIX.”
Pragmatic Decisions
Sometimes we use non-POSIX features if they’re convenient and practically all platforms we care about support them.
Minimal Churn
Avoid style fixes for the sake of conforming. “Once it is in the tree, it’s not worth the patch noise to fix it up.”
C Language Standards
C99 Baseline
As of Git v2.35.0, Git requires C99 (checking__STDC_VERSION__).
Allowed C99 Features
Allowed C99 Features
These features are safe to use:
-
Initializer elements not computable at load time (since 2007)
-
Trailing comma in enum definitions (since 2012)
-
Designated initializers for structs (since mid-2017)
-
Designated initializers for arrays (since mid-2017)
-
Variadic macros (since early 2021)
-
Loop variable declarations (since late 2021)
-
bool type from
<stdbool.h>(since late 2023)
Test Balloon Features
Test Balloon Features
Under evaluation (do NOT use yet):
- Compound literals (test balloon since v2.48.0-rc0~20)
Not Yet Allowed
Not Yet Allowed
These C99 features cannot be used:
-
%z printf format for size_t - MinGW C library doesn’t support it
-
Nested designated initializers - IBM XLC v12.01 issue
Code Style
Indentation and Whitespace
Variable Declarations
Pointers and NULL
Whitespace in Expressions
Braces
- Minimal Braces
- Multi-line Statements
- Consistent Arms
Comments
Naming Conventions
Functions and Structures
Primary Data Structure Pattern
Primary Data Structure Pattern
Common Function Names
Common Function Names
S_init()- Initialize without allocatingS_release()- Release contents without freeing structureS_clear()- Release + reinitialize for reuseS_free()- Release contents and free structure
The '_1' Suffix
The '_1' Suffix
Use only when:
- Function handles one element among a group
- Separating recursive function from setup
Configuration Variables
Arrays
Bit Fields
Include Files
C files should directly include headers for functions/types they use, except those provided by required headers.
Error Messages
- Porcelain Commands
- Plumbing Commands
- BUG Messages
Shell Scripts
Indentation and Style
Indentation and Style
Control Structures
Control Structures
Redirection
Redirection
Command Substitution
Command Substitution
Prefer 'test' over '['
Prefer 'test' over '['
