]> git.proxmox.com Git - libgit2.git/blame - docs/conventions.md
Bump Standards-Version to 4.6.0 (no changes needed)
[libgit2.git] / docs / conventions.md
CommitLineData
24aec6db 1# Libgit2 Conventions
7335ffc3 2
1631147c
RB
3We like to keep the source consistent and readable. Herein are some
4guidelines that should help with that.
7335ffc3 5
1f8cb02f
CMN
6## External API
7
8We have a few rules to avoid surprising ways of calling functions and
9some rules for consumers of the library to avoid stepping on each
10other's toes.
11
12 - Property accessors return the value directly (e.g. an `int` or
13 `const char *`) but if a function can fail, we return a `int` value
14 and the output parameters go first in the parameter list, followed
15 by the object that a function is operating on, and then any other
16 arguments the function may need.
17
18 - If a function returns an object as a return value, that function is
19 a getter and the object's lifetime is tied to the parent
20 object. Objects which are returned as the first argument as a
c1239e33 21 pointer-to-pointer are owned by the caller and it is responsible
1f8cb02f
CMN
22 for freeing it. Strings are returned via `git_buf` in order to
23 allow for re-use and safe freeing.
24
c1239e33 25 - Most of what libgit2 does relates to I/O so as a general rule
1f8cb02f
CMN
26 you should assume that any function can fail due to errors as even
27 getting data from the filesystem can result in all sorts of errors
28 and complex failure cases.
29
30 - Paths inside the Git system are separated by a slash (0x2F). If a
31 function accepts a path on disk, then backslashes (0x5C) are also
32 accepted on Windows.
33
34 - Do not mix allocators. If something has been allocated by libgit2,
35 you do not know which is the right free function in the general
36 case. Use the free functions provided for each object type.
37
1631147c
RB
38## Compatibility
39
40`libgit2` runs on many different platforms with many different compilers.
1631147c 41
68a19ca9
RB
42The public API of `libgit2` is [ANSI C](http://en.wikipedia.org/wiki/ANSI_C)
43(a.k.a. C89) compatible.
44
45Internally, `libgit2` is written using a portable subset of C99 - in order
46to maximize compatibility (e.g. with MSVC) we avoid certain C99
47extensions. Specifically, we keep local variable declarations at the tops
48of blocks only and we avoid `//` style comments.
49
50Also, to the greatest extent possible, we try to avoid lots of `#ifdef`s
51inside the core code base. This is somewhat unavoidable, but since it can
52really hamper maintainability, we keep it to a minimum.
1631147c
RB
53
54## Match Surrounding Code
55
56If there is one rule to take away from this document, it is *new code should
57match the surrounding code in a way that makes it impossible to distinguish
58the new from the old.* Consistency is more important to us than anyone's
59personal opinion about where braces should be placed or spaces vs. tabs.
60
61If a section of code is being completely rewritten, it is okay to bring it
62in line with the standards that are laid out here, but we will not accept
63submissions that contain a large number of changes that are merely
64reformatting.
7335ffc3 65
24aec6db 66## Naming Things
7335ffc3 67
1631147c
RB
68All external types and functions start with `git_` and all `#define` macros
69start with `GIT_`. The `libgit2` API is mostly broken into related
70functional modules each with a corresponding header. All functions in a
71module should be named like `git_modulename_functioname()`
72(e.g. `git_repository_open()`).
7335ffc3 73
24aec6db
BS
74Functions with a single output parameter should name that parameter `out`.
75Multiple outputs should be named `foo_out`, `bar_out`, etc.
7335ffc3 76
24aec6db
BS
77Parameters of type `git_oid` should be named `id`, or `foo_id`. Calls that
78return an OID should be named `git_foo_id`.
6533aadc 79
1631147c
RB
80Where a callback function is used, the function should also include a
81user-supplied extra input that is a `void *` named "payload" that will be
82passed through to the callback at each invocation.
6533aadc 83
1631147c 84## Typedefs
7335ffc3 85
1631147c
RB
86Wherever possible, use `typedef`. In some cases, if a structure is just a
87collection of function pointers, the pointer types don't need to be
88separately typedef'd, but loose function pointer types should be.
7335ffc3 89
24aec6db 90## Exports
7335ffc3
SP
91
92All exported functions must be declared as:
93
1631147c 94```c
24aec6db 95GIT_EXTERN(result_type) git_modulename_functionname(arg_list);
ee72ffd0 96```
7335ffc3 97
24aec6db 98## Internals
7335ffc3 99
1631147c 100Functions whose *modulename* is followed by two underscores,
24aec6db 101for example `git_odb__read_packed`, are semi-private functions.
7335ffc3
SP
102They are primarily intended for use within the library itself,
103and may disappear or change their signature in a future release.
104
24aec6db
BS
105## Parameters
106
107Out parameters come first.
108
109Whenever possible, pass argument pointers as `const`. Some structures (such
1631147c
RB
110as `git_repository` and `git_index`) have mutable internal structure that
111prevents this.
24aec6db
BS
112
113Callbacks should always take a `void *` payload as their last parameter.
1631147c
RB
114Callback pointers are grouped with their payloads, and typically come last
115when passed as arguments:
24aec6db 116
1631147c
RB
117```c
118int git_foo(git_repository *repo, git_foo_cb callback, void *payload);
24aec6db
BS
119```
120
24aec6db
BS
121## Memory Ownership
122
123Some APIs allocate memory which the caller is responsible for freeing; others
124return a pointer into a buffer that's owned by some other object. Make this
125explicit in the documentation.
126
24aec6db
BS
127## Return codes
128
1631147c
RB
129Most public APIs should return an `int` error code. As is typical with most
130C library functions, a zero value indicates success and a negative value
131indicates failure.
132
133Some bindings will transform these returned error codes into exception
134types, so returning a semantically appropriate error code is important.
135Check
136[`include/git2/errors.h`](https://github.com/libgit2/libgit2/blob/development/include/git2/errors.h)
24aec6db
BS
137for the return codes already defined.
138
ac3d33df 139In your implementation, use `git_error_set()` to provide extended error
1631147c 140information to callers.
7335ffc3 141
1631147c 142If a `libgit2` function internally invokes another function that reports an
ac3d33df 143error, but the error is not propagated up, use `git_error_clear()` to prevent
1631147c 144callers from getting the wrong error message later on.
7335ffc3 145
7335ffc3 146
1631147c 147## Structs
24aec6db 148
1631147c 149Most public types should be opaque, e.g.:
7335ffc3 150
ee72ffd0 151```C
24aec6db 152typedef struct git_odb git_odb;
ee72ffd0 153```
7335ffc3 154
24aec6db
BS
155...with allocation functions returning an "instance" created within
156the library, and not within the application. This allows the type
157to grow (or shrink) in size without rebuilding client code.
158
ac3d33df
JK
159To preserve ABI compatibility, include an `int version` field in all transparent
160structures, and initialize to the latest version in the constructor call.
24aec6db
BS
161Increment the "latest" version whenever the structure changes, and try to only
162append to the end of the structure.
6dafd056 163
24aec6db 164## Option Structures
6dafd056 165
1631147c
RB
166If a function's parameter count is too high, it may be desirable to package
167up the options in a structure. Make them transparent, include a version
168field, and provide an initializer constant or constructor. Using these
169structures should be this easy:
7335ffc3 170
24aec6db
BS
171```C
172git_foo_options opts = GIT_FOO_OPTIONS_INIT;
173opts.baz = BAZ_OPTION_ONE;
174git_foo(&opts);
175```
0e7fa1fe 176
24aec6db 177## Enumerations
0e7fa1fe 178
1631147c
RB
179Typedef all enumerated types. If each option stands alone, use the enum
180type for passing them as parameters; if they are flags to be OR'ed together,
181pass them as `unsigned int` or `uint32_t` or some appropriate type.
7335ffc3 182
24aec6db 183## Code Layout
7335ffc3 184
1631147c
RB
185Try to keep lines less than 80 characters long. This is a loose
186requirement, but going significantly over 80 columns is not nice.
7335ffc3 187
1631147c
RB
188Use common sense to wrap most code lines; public function declarations
189can use a couple of different styles:
190
191```c
192/** All on one line is okay if it fits */
193GIT_EXTERN(int) git_foo_simple(git_oid *id);
194
195/** Otherwise one argument per line is a good next step */
24aec6db 196GIT_EXTERN(int) git_foo_id(
1631147c
RB
197 git_oid **out,
198 int a,
199 int b);
24aec6db
BS
200```
201
ac3d33df 202Indent with tabs; set your editor's tab width to eight for best effect.
24aec6db 203
1631147c
RB
204Avoid trailing whitespace and only commit Unix-style newlines (i.e. no CRLF
205in the repository - just set `core.autocrlf` to true if you are writing code
206on a Windows machine).
7335ffc3 207
24aec6db 208## Documentation
7335ffc3 209
24aec6db 210All comments should conform to Doxygen "javadoc" style conventions for
1631147c
RB
211formatting the public API documentation. Try to document every parameter,
212and keep the comments up to date if you change the parameter list.
7335ffc3 213
24aec6db
BS
214## Public Header Template
215
216Use this template when creating a new public header.
217
218```C
219#ifndef INCLUDE_git_${filename}_h__
220#define INCLUDE_git_${filename}_h__
221
222#include "git/common.h"
223
224/**
225 * @file git/${filename}.h
226 * @brief Git some description
227 * @defgroup git_${filename} some description routines
228 * @ingroup Git
229 * @{
230 */
231GIT_BEGIN_DECL
232
233/* ... definitions ... */
234
235/** @} */
236GIT_END_DECL
237#endif
ee72ffd0 238```
24aec6db 239
394711ff
TN
240## Inlined functions
241
242All inlined functions must be declared as:
243
244```C
245GIT_INLINE(result_type) git_modulename_functionname(arg_list);
246```
247
68a19ca9
RB
248`GIT_INLINE` (or `inline`) should not be used in public headers in order
249to preserve ANSI C compatibility.
250
1631147c
RB
251## Tests
252
a313de0d 253`libgit2` uses the [clar](https://github.com/vmg/clar) testing framework.
1631147c
RB
254
255All PRs should have corresponding tests.
256
257* If the PR fixes an existing issue, the test should fail prior to applying
258 the PR and succeed after applying it.
259* If the PR is for new functionality, then the tests should exercise that
260 new functionality to a certain extent. We don't require 100% coverage
261 right now (although we are getting stricter over time).
262
263When adding new tests, we prefer if you attempt to reuse existing test data
264(in `tests-clar/resources/`) if possible. If you are going to add new test
265repositories, please try to strip them of unnecessary files (e.g. sample
266hooks, etc).