Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header cleanups #1485

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 6 additions & 2 deletions Documentation/CodingGuidelines
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,12 @@ For C programs:
detail.

- The first #include in C files, except in platform specific compat/
implementations, must be either "git-compat-util.h", "cache.h" or
"builtin.h". You do not have to include more than one of these.
implementations and sha1dc/, must be either "git-compat-util.h" or
one of the approved headers that includes it first for you. (The
approved headers currently include "cache.h", "builtin.h",
"t/helper/test-tool.h", "xdiff/xinclude.h", or
"reftable/system.h"). You do not have to include more than one of
these.

- A C file must directly include the header files that declare the
functions and the types it uses, except for the functions and types
Expand Down
1 change: 1 addition & 0 deletions add-interactive.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "color.h"
#include "config.h"
#include "diffcore.h"
#include "hex.h"
#include "revision.h"
#include "refs.h"
#include "string-list.h"
Expand Down
1 change: 1 addition & 0 deletions add-patch.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cache.h"
newren marked this conversation as resolved.
Show resolved Hide resolved
#include "add-interactive.h"
#include "alloc.h"
#include "strbuf.h"
#include "run-command.h"
#include "strvec.h"
Expand Down
4 changes: 3 additions & 1 deletion advice.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "cache.h"
#include "git-compat-util.h"
#include "advice.h"
#include "config.h"
#include "color.h"
#include "gettext.h"
#include "help.h"
#include "string-list.h"

Expand Down
2 changes: 0 additions & 2 deletions advice.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef ADVICE_H
newren marked this conversation as resolved.
Show resolved Hide resolved
#define ADVICE_H

#include "git-compat-util.h"

struct string_list;

/*
Expand Down
4 changes: 3 additions & 1 deletion alias.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alias.h"
#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "string-list.h"

struct config_alias_data {
Expand Down
2 changes: 1 addition & 1 deletion alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* up with maximal alignment because it doesn't know what the object alignment
newren marked this conversation as resolved.
Show resolved Hide resolved
* for the new allocation is.
*/
#include "cache.h"
#include "git-compat-util.h"
#include "object.h"
#include "blob.h"
#include "tree.h"
Expand Down
75 changes: 75 additions & 0 deletions alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,79 @@ void *alloc_object_node(struct repository *r);
struct alloc_state *allocate_alloc_state(void);
void clear_alloc_state(struct alloc_state *s);

#define alloc_nr(x) (((x)+16)*3/2)

/**
* Dynamically growing an array using realloc() is error prone and boring.
*
* Define your array with:
*
* - a pointer (`item`) that points at the array, initialized to `NULL`
* (although please name the variable based on its contents, not on its
* type);
*
* - an integer variable (`alloc`) that keeps track of how big the current
* allocation is, initialized to `0`;
*
* - another integer variable (`nr`) to keep track of how many elements the
* array currently has, initialized to `0`.
*
* Then before adding `n`th element to the item, call `ALLOC_GROW(item, n,
* alloc)`. This ensures that the array can hold at least `n` elements by
* calling `realloc(3)` and adjusting `alloc` variable.
*
* ------------
* sometype *item;
* size_t nr;
* size_t alloc
*
* for (i = 0; i < nr; i++)
* if (we like item[i] already)
* return;
*
* // we did not like any existing one, so add one
* ALLOC_GROW(item, nr + 1, alloc);
* item[nr++] = value you like;
* ------------
*
* You are responsible for updating the `nr` variable.
*
* If you need to specify the number of elements to allocate explicitly
* then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`.
*
* Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
* added niceties.
*
* DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
*/
#define ALLOC_GROW(x, nr, alloc) \
do { \
if ((nr) > alloc) { \
if (alloc_nr(alloc) < (nr)) \
alloc = (nr); \
else \
alloc = alloc_nr(alloc); \
REALLOC_ARRAY(x, alloc); \
} \
} while (0)

/*
* Similar to ALLOC_GROW but handles updating of the nr value and
* zeroing the bytes of the newly-grown array elements.
*
* DO NOT USE any expression with side-effect for any of the
* arguments.
*/
#define ALLOC_GROW_BY(x, nr, increase, alloc) \
do { \
if (increase) { \
size_t new_nr = nr + (increase); \
if (new_nr < nr) \
BUG("negative growth in ALLOC_GROW_BY"); \
ALLOC_GROW(x, new_nr, alloc); \
memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
nr = new_nr; \
} \
} while (0)

#endif
2 changes: 2 additions & 0 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
*/

#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "object-store.h"
#include "blob.h"
#include "delta.h"
#include "diff.h"
#include "dir.h"
#include "hex.h"
#include "xdiff-interface.h"
#include "ll-merge.h"
#include "lockfile.h"
Expand Down
4 changes: 3 additions & 1 deletion archive-tar.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Copyright (c) 2005, 2006 Rene Scharfe
*/
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "hex.h"
#include "tar.h"
#include "archive.h"
#include "object-store.h"
Expand Down
1 change: 1 addition & 0 deletions archive-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "cache.h"
#include "config.h"
#include "archive.h"
#include "hex.h"
#include "streaming.h"
#include "utf8.h"
#include "object-store.h"
Expand Down
4 changes: 3 additions & 1 deletion archive.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "hex.h"
#include "refs.h"
#include "object-store.h"
#include "commit.h"
Expand Down
1 change: 1 addition & 0 deletions attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "exec-cmd.h"
#include "attr.h"
Expand Down
1 change: 1 addition & 0 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "config.h"
#include "commit.h"
#include "diff.h"
#include "hex.h"
#include "revision.h"
#include "refs.h"
#include "list-objects.h"
Expand Down
1 change: 1 addition & 0 deletions blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "mergesort.h"
#include "diff.h"
#include "diffcore.h"
#include "hex.h"
#include "tag.h"
#include "blame.h"
#include "alloc.h"
Expand Down
1 change: 0 additions & 1 deletion blame.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef BLAME_H
#define BLAME_H

#include "cache.h"
#include "commit.h"
#include "xdiff-interface.h"
#include "revision.h"
Expand Down
2 changes: 1 addition & 1 deletion blob.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cache.h"
#include "git-compat-util.h"
#include "blob.h"
#include "repository.h"
#include "alloc.h"
Expand Down
1 change: 1 addition & 0 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "cache.h"
#include "config.h"
#include "branch.h"
#include "hex.h"
#include "refs.h"
#include "refspec.h"
#include "remote.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "config.h"
#include "builtin.h"
#include "exec-cmd.h"
#include "hex.h"
#include "parse-options.h"
#include "dir.h"
#include "run-command.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/bisect.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "builtin.h"
#include "cache.h"
#include "hex.h"
#include "parse-options.h"
#include "bisect.h"
#include "refs.h"
Expand Down
4 changes: 3 additions & 1 deletion builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* See COPYING for licensing conditions
*/

#include "cache.h"
#include "git-compat-util.h"
#include "alloc.h"
#include "config.h"
#include "color.h"
#include "builtin.h"
#include "hex.h"
#include "repository.h"
#include "commit.h"
#include "diff.h"
Expand Down
4 changes: 4 additions & 0 deletions builtin/cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "diff.h"
#include "hex.h"
#include "ident.h"
#include "parse-options.h"
#include "userdiff.h"
#include "streaming.h"
#include "tree-walk.h"
#include "oid-array.h"
#include "packfile.h"
#include "object-store.h"
#include "replace-object.h"
#include "promisor-remote.h"
#include "mailmap.h"

Expand Down
1 change: 1 addition & 0 deletions builtin/check-mailmap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "builtin.h"
#include "config.h"
#include "ident.h"
#include "mailmap.h"
#include "parse-options.h"
#include "string-list.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/checkout--worker.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "builtin.h"
#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "parallel-checkout.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "config.h"
#include "diff.h"
#include "dir.h"
#include "hex.h"
#include "hook.h"
#include "ll-merge.h"
#include "lockfile.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "config.h"
#include "hex.h"
#include "lockfile.h"
#include "parse-options.h"
#include "fetch-pack.h"
Expand Down
2 changes: 2 additions & 0 deletions builtin/commit-graph.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "builtin.h"
#include "config.h"
#include "dir.h"
#include "hex.h"
#include "lockfile.h"
#include "parse-options.h"
#include "repository.h"
#include "commit-graph.h"
#include "object-store.h"
#include "progress.h"
#include "replace-object.h"
#include "tag.h"

#define BUILTIN_COMMIT_GRAPH_VERIFY_USAGE \
Expand Down
1 change: 1 addition & 0 deletions builtin/commit-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "cache.h"
#include "config.h"
#include "hex.h"
#include "object-store.h"
#include "repository.h"
#include "commit.h"
Expand Down
3 changes: 2 additions & 1 deletion builtin/config.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "builtin.h"
#include "cache.h"
#include "alloc.h"
#include "config.h"
#include "color.h"
#include "ident.h"
#include "parse-options.h"
#include "urlmatch.h"
#include "quote.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/credential-cache--daemon.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "builtin.h"
#include "alloc.h"
#include "parse-options.h"

#ifndef NO_UNIX_SOCKETS
Expand Down
1 change: 1 addition & 0 deletions builtin/describe.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "config.h"
#include "hex.h"
#include "lockfile.h"
#include "commit.h"
#include "tag.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/diff-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "config.h"
#include "diff.h"
#include "commit.h"
#include "hex.h"
#include "log-tree.h"
#include "builtin.h"
#include "submodule.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/difftool.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "builtin.h"
#include "run-command.h"
#include "exec-cmd.h"
#include "hex.h"
#include "parse-options.h"
#include "strvec.h"
#include "strbuf.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/fast-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "builtin.h"
#include "cache.h"
#include "config.h"
#include "hex.h"
#include "refs.h"
#include "refspec.h"
#include "object-store.h"
Expand Down
1 change: 1 addition & 0 deletions builtin/fast-import.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "builtin.h"
#include "cache.h"
#include "hex.h"
#include "repository.h"
#include "config.h"
#include "lockfile.h"
Expand Down