]> git.proxmox.com Git - libgit2.git/blame - CONVENTIONS.md
Merge pull request #1203 from phkelley/reverse_dak
[libgit2.git] / CONVENTIONS.md
CommitLineData
24aec6db 1# Libgit2 Conventions
7335ffc3 2
24aec6db
BS
3We like to keep the source consistent and readable. Herein are some guidelines
4that should help with that.
7335ffc3 5
7335ffc3 6
24aec6db 7## Naming Things
7335ffc3 8
24aec6db 9All types and functions start with `git_`, and all #define macros start with `GIT_`.
7335ffc3 10
24aec6db
BS
11Functions with a single output parameter should name that parameter `out`.
12Multiple outputs should be named `foo_out`, `bar_out`, etc.
7335ffc3 13
24aec6db
BS
14Parameters of type `git_oid` should be named `id`, or `foo_id`. Calls that
15return an OID should be named `git_foo_id`.
6533aadc 16
24aec6db
BS
17Where there is a callback passed in, the `void *` that is passed into it should
18be named "payload".
6533aadc 19
24aec6db 20## Typedef
7335ffc3 21
24aec6db
BS
22Wherever possible, use `typedef`. If a structure is just a collection of
23function pointers, the pointer types don't need to be separately typedef'd, but
24loose function pointer types should be.
7335ffc3 25
24aec6db 26## Exports
7335ffc3
SP
27
28All exported functions must be declared as:
29
ee72ffd0 30```C
24aec6db 31GIT_EXTERN(result_type) git_modulename_functionname(arg_list);
ee72ffd0 32```
7335ffc3 33
24aec6db 34## Internals
7335ffc3
SP
35
36Functions whose modulename is followed by two underscores,
24aec6db 37for example `git_odb__read_packed`, are semi-private functions.
7335ffc3
SP
38They are primarily intended for use within the library itself,
39and may disappear or change their signature in a future release.
40
24aec6db
BS
41## Parameters
42
43Out parameters come first.
44
45Whenever possible, pass argument pointers as `const`. Some structures (such
46as `git_repository` and `git_index`) have internal structure that prevents
47this.
48
49Callbacks should always take a `void *` payload as their last parameter.
50Callback pointers are grouped with their payloads, and come last when passed as
51arguments:
52
53```C
54int foo(git_repository *repo, git_foo_cb callback, void *payload);
55```
56
57
58## Memory Ownership
59
60Some APIs allocate memory which the caller is responsible for freeing; others
61return a pointer into a buffer that's owned by some other object. Make this
62explicit in the documentation.
63
64
65## Return codes
66
67Return an `int` when a public API can fail in multiple ways. These may be
68transformed into exception types in some bindings, so returning a semantically
69appropriate error code is important. Check
70[`errors.h`](https://github.com/libgit2/libgit2/blob/development/include/git2/errors.h)
71for the return codes already defined.
72
73Use `giterr_set` to provide extended error information to callers.
7335ffc3 74
24aec6db
BS
75If an error is not to be propagated, use `giterr_clear` to prevent callers from
76getting the wrong error message later on.
7335ffc3 77
7335ffc3 78
24aec6db
BS
79## Opaque Structs
80
81Most types should be opaque, e.g.:
7335ffc3 82
ee72ffd0 83```C
24aec6db 84typedef struct git_odb git_odb;
ee72ffd0 85```
7335ffc3 86
24aec6db
BS
87...with allocation functions returning an "instance" created within
88the library, and not within the application. This allows the type
89to grow (or shrink) in size without rebuilding client code.
90
91To preserve ABI compatibility, include an `int version` field in all opaque
92structures, and initialize to the latest version in the construction call.
93Increment the "latest" version whenever the structure changes, and try to only
94append to the end of the structure.
6dafd056 95
24aec6db 96## Option Structures
6dafd056 97
24aec6db
BS
98If a function's parameter count is too high, it may be desirable to package up
99the options in a structure. Make them transparent, include a version field,
100and provide an initializer constant or constructor. Using these structures
101should be this easy:
7335ffc3 102
24aec6db
BS
103```C
104git_foo_options opts = GIT_FOO_OPTIONS_INIT;
105opts.baz = BAZ_OPTION_ONE;
106git_foo(&opts);
107```
0e7fa1fe 108
24aec6db 109## Enumerations
0e7fa1fe 110
24aec6db
BS
111Typedef all enumerated types. If each option stands alone, use the enum type
112for passing them as parameters; if they are flags, pass them as `unsigned int`.
7335ffc3 113
24aec6db 114## Code Layout
7335ffc3 115
24aec6db
BS
116Try to keep lines less than 80 characters long. Use common sense to wrap most
117code lines; public function declarations should use this convention:
7335ffc3 118
ee72ffd0 119```C
24aec6db
BS
120GIT_EXTERN(int) git_foo_id(
121 git_oid **out,
122 int a,
123 int b);
124```
125
a541eafa 126Indentation is done with tabs; set your editor's tab width to 3 for best effect.
24aec6db 127
7335ffc3 128
24aec6db 129## Documentation
7335ffc3 130
24aec6db
BS
131All comments should conform to Doxygen "javadoc" style conventions for
132formatting the public API documentation. Try to document every parameter, and
133keep the comments up to date if you change the parameter list.
7335ffc3 134
7335ffc3 135
24aec6db
BS
136## Public Header Template
137
138Use this template when creating a new public header.
139
140```C
141#ifndef INCLUDE_git_${filename}_h__
142#define INCLUDE_git_${filename}_h__
143
144#include "git/common.h"
145
146/**
147 * @file git/${filename}.h
148 * @brief Git some description
149 * @defgroup git_${filename} some description routines
150 * @ingroup Git
151 * @{
152 */
153GIT_BEGIN_DECL
154
155/* ... definitions ... */
156
157/** @} */
158GIT_END_DECL
159#endif
ee72ffd0 160```
24aec6db 161
394711ff
TN
162## Inlined functions
163
164All inlined functions must be declared as:
165
166```C
167GIT_INLINE(result_type) git_modulename_functionname(arg_list);
168```
169