Skip to content

Commit 88cc8ed

Browse files
committed
Merge branch 'en/header-cleanup'
Code clean-up to clarify the rule that "git-compat-util.h" must be the first to be included. * en/header-cleanup: diff.h: remove unnecessary include of object.h Remove unnecessary includes of builtin.h treewide: replace cache.h with more direct headers, where possible replace-object.h: move read_replace_refs declaration from cache.h to here object-store.h: move struct object_info from cache.h dir.h: refactor to no longer need to include cache.h object.h: stop depending on cache.h; make cache.h depend on object.h ident.h: move ident-related declarations out of cache.h pretty.h: move has_non_ascii() declaration from commit.h cache.h: remove dependence on hex.h; make other files include it explicitly hex.h: move some hex-related declarations from cache.h hash.h: move some oid-related declarations from cache.h alloc.h: move ALLOC_GROW() functions from cache.h treewide: remove unnecessary cache.h includes in source files treewide: remove unnecessary cache.h includes treewide: remove unnecessary git-compat-util.h includes in headers treewide: ensure one of the appropriate headers is sourced first
2 parents f17d232 + f524970 commit 88cc8ed

File tree

322 files changed

+858
-529
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

322 files changed

+858
-529
lines changed

Documentation/CodingGuidelines

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,12 @@ For C programs:
442442
detail.
443443

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

448452
- A C file must directly include the header files that declare the
449453
functions and the types it uses, except for the functions and types

add-interactive.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "color.h"
44
#include "config.h"
55
#include "diffcore.h"
6+
#include "hex.h"
67
#include "revision.h"
78
#include "refs.h"
89
#include "string-list.h"

add-patch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "add-interactive.h"
3+
#include "alloc.h"
34
#include "strbuf.h"
45
#include "run-command.h"
56
#include "strvec.h"

advice.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
2+
#include "advice.h"
23
#include "config.h"
34
#include "color.h"
5+
#include "gettext.h"
46
#include "help.h"
57
#include "string-list.h"
68

advice.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef ADVICE_H
22
#define ADVICE_H
33

4-
#include "git-compat-util.h"
5-
64
struct string_list;
75

86
/*

alias.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#include "cache.h"
1+
#include "git-compat-util.h"
22
#include "alias.h"
3+
#include "alloc.h"
34
#include "config.h"
5+
#include "gettext.h"
46
#include "string-list.h"
57

68
struct config_alias_data {

alloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* up with maximal alignment because it doesn't know what the object alignment
99
* for the new allocation is.
1010
*/
11-
#include "cache.h"
11+
#include "git-compat-util.h"
1212
#include "object.h"
1313
#include "blob.h"
1414
#include "tree.h"

alloc.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,79 @@ void *alloc_object_node(struct repository *r);
1717
struct alloc_state *allocate_alloc_state(void);
1818
void clear_alloc_state(struct alloc_state *s);
1919

20+
#define alloc_nr(x) (((x)+16)*3/2)
21+
22+
/**
23+
* Dynamically growing an array using realloc() is error prone and boring.
24+
*
25+
* Define your array with:
26+
*
27+
* - a pointer (`item`) that points at the array, initialized to `NULL`
28+
* (although please name the variable based on its contents, not on its
29+
* type);
30+
*
31+
* - an integer variable (`alloc`) that keeps track of how big the current
32+
* allocation is, initialized to `0`;
33+
*
34+
* - another integer variable (`nr`) to keep track of how many elements the
35+
* array currently has, initialized to `0`.
36+
*
37+
* Then before adding `n`th element to the item, call `ALLOC_GROW(item, n,
38+
* alloc)`. This ensures that the array can hold at least `n` elements by
39+
* calling `realloc(3)` and adjusting `alloc` variable.
40+
*
41+
* ------------
42+
* sometype *item;
43+
* size_t nr;
44+
* size_t alloc
45+
*
46+
* for (i = 0; i < nr; i++)
47+
* if (we like item[i] already)
48+
* return;
49+
*
50+
* // we did not like any existing one, so add one
51+
* ALLOC_GROW(item, nr + 1, alloc);
52+
* item[nr++] = value you like;
53+
* ------------
54+
*
55+
* You are responsible for updating the `nr` variable.
56+
*
57+
* If you need to specify the number of elements to allocate explicitly
58+
* then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`.
59+
*
60+
* Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
61+
* added niceties.
62+
*
63+
* DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
64+
*/
65+
#define ALLOC_GROW(x, nr, alloc) \
66+
do { \
67+
if ((nr) > alloc) { \
68+
if (alloc_nr(alloc) < (nr)) \
69+
alloc = (nr); \
70+
else \
71+
alloc = alloc_nr(alloc); \
72+
REALLOC_ARRAY(x, alloc); \
73+
} \
74+
} while (0)
75+
76+
/*
77+
* Similar to ALLOC_GROW but handles updating of the nr value and
78+
* zeroing the bytes of the newly-grown array elements.
79+
*
80+
* DO NOT USE any expression with side-effect for any of the
81+
* arguments.
82+
*/
83+
#define ALLOC_GROW_BY(x, nr, increase, alloc) \
84+
do { \
85+
if (increase) { \
86+
size_t new_nr = nr + (increase); \
87+
if (new_nr < nr) \
88+
BUG("negative growth in ALLOC_GROW_BY"); \
89+
ALLOC_GROW(x, new_nr, alloc); \
90+
memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
91+
nr = new_nr; \
92+
} \
93+
} while (0)
94+
2095
#endif

apply.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
*/
99

1010
#include "cache.h"
11+
#include "alloc.h"
1112
#include "config.h"
1213
#include "object-store.h"
1314
#include "blob.h"
1415
#include "delta.h"
1516
#include "diff.h"
1617
#include "dir.h"
18+
#include "hex.h"
1719
#include "xdiff-interface.h"
1820
#include "ll-merge.h"
1921
#include "lockfile.h"

archive-tar.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/*
22
* Copyright (c) 2005, 2006 Rene Scharfe
33
*/
4-
#include "cache.h"
4+
#include "git-compat-util.h"
5+
#include "alloc.h"
56
#include "config.h"
7+
#include "hex.h"
68
#include "tar.h"
79
#include "archive.h"
810
#include "object-store.h"

0 commit comments

Comments
 (0)