* [PATCH 0/6] git-compat-util cleanups
@ 2023-05-16 17:09 Calvin Wan
2023-05-16 17:09 ` [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
` (7 more replies)
0 siblings, 8 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 17:09 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
This series focuses on cleaning up and reducing the scope of
git-compat-util.h by moving headers to their respective files and
separating out functionality from git-compat-util.h to a new file,
common.h. I go into more detail in patch 3 as to why I believe this
separation is useful.
By the end of this series, git-compat-util.h includes common.h which
includes wrapper.h and usage.h. Since virtually every file includes
git-compat-util.h and the large majority of files use functions defined
in common.h, wrapper.h, and usage.h, I believe it makes sense that those
are also automatically included with git-compat-util.h.
While this series does not intend to draw clearer boundaries for
common.h, I am open to ideas for how it can be cleaned up more and if
there is a better name for the file.
Calvin Wan (6):
strbuf.h: move declarations for strbuf.c functions from
git-compat-util.h
wrapper.h: move declarations for wrapper.c functions from
git-compat-util.h
common.h: move non-compat specific macros and functions from
git-compat-util.h
usage.h: move declarations for usage.c functions from
git-compat-util.h
treewide: remove unnecessary includes for wrapper.h
common: move alloc macros to common.h
add-patch.c | 1 -
alias.c | 1 -
alloc.h | 75 ----
apply.c | 2 -
archive-tar.c | 1 -
archive.c | 1 -
attr.c | 1 -
builtin/am.c | 1 -
builtin/bisect.c | 1 -
builtin/blame.c | 1 -
builtin/branch.c | 1 -
builtin/bugreport.c | 1 -
builtin/cat-file.c | 1 -
builtin/checkout--worker.c | 1 -
builtin/clone.c | 1 -
builtin/config.c | 2 -
builtin/credential-cache--daemon.c | 1 -
builtin/credential-cache.c | 1 -
builtin/difftool.c | 1 -
builtin/fast-import.c | 1 -
builtin/fetch-pack.c | 1 -
builtin/fmt-merge-msg.c | 1 -
builtin/fsmonitor--daemon.c | 1 -
builtin/gc.c | 1 -
builtin/get-tar-commit-id.c | 1 -
builtin/grep.c | 1 -
builtin/index-pack.c | 2 -
builtin/init-db.c | 1 -
builtin/log.c | 1 -
builtin/merge.c | 2 -
builtin/mktree.c | 1 -
builtin/mv.c | 1 -
builtin/name-rev.c | 1 -
builtin/pack-objects.c | 2 -
builtin/rebase.c | 1 -
builtin/receive-pack.c | 1 -
builtin/repack.c | 1 -
builtin/rerere.c | 1 -
builtin/rev-parse.c | 1 -
builtin/revert.c | 1 -
builtin/rm.c | 1 -
builtin/submodule--helper.c | 1 -
builtin/unpack-file.c | 1 -
builtin/worktree.c | 1 -
bulk-checkin.c | 2 -
cache-tree.c | 1 -
chunk-format.c | 1 -
combine-diff.c | 1 -
commit-graph.c | 1 -
commit-reach.c | 1 -
common.h | 551 ++++++++++++++++++++++++
compat/terminal.c | 1 -
config.c | 2 -
convert.c | 1 -
copy.c | 1 -
csum-file.c | 1 -
daemon.c | 2 -
delta-islands.c | 1 -
diff.c | 2 -
diffcore-rename.c | 1 -
dir-iterator.c | 1 -
dir.c | 2 -
editor.c | 1 -
entry.c | 1 -
environment.c | 1 -
ewah/bitmap.c | 1 -
ewah/ewah_bitmap.c | 1 -
fetch-pack.c | 2 -
fmt-merge-msg.c | 1 -
fsck.c | 1 -
git-compat-util.h | 664 +----------------------------
gpg-interface.c | 1 -
grep.c | 1 -
help.c | 1 -
http-backend.c | 2 -
imap-send.c | 1 -
line-log.c | 1 -
list-objects-filter-options.c | 1 -
list-objects-filter.c | 1 -
ll-merge.c | 1 -
merge-recursive.c | 1 -
midx.c | 1 -
notes-merge.c | 1 -
object-file.c | 2 -
oid-array.c | 1 -
oidtree.c | 1 -
pack-bitmap-write.c | 1 -
pack-bitmap.c | 1 -
pack-objects.c | 1 -
pack-write.c | 1 -
packfile.c | 2 -
parallel-checkout.c | 2 -
path.c | 1 -
pkt-line.c | 1 -
pretty.c | 1 -
prio-queue.c | 1 -
quote.c | 1 -
read-cache.c | 2 -
rebase-interactive.c | 1 -
ref-filter.c | 1 -
reflog-walk.c | 1 -
refs.c | 2 -
refspec.c | 1 -
remote-curl.c | 1 -
remote.c | 1 -
rerere.c | 2 -
revision.c | 1 -
send-pack.c | 1 -
sequencer.c | 2 -
server-info.c | 2 -
setup.c | 1 -
shallow.c | 2 -
sigchain.c | 1 -
sparse-index.c | 1 -
split-index.c | 1 -
strbuf.c | 2 -
strbuf.h | 32 ++
streaming.c | 1 -
string-list.c | 1 -
strvec.c | 1 -
submodule-config.c | 1 -
submodule.c | 1 -
t/helper/test-delta.c | 1 -
t/helper/test-fsmonitor-client.c | 1 -
t/helper/test-reach.c | 1 -
t/helper/test-read-cache.c | 1 -
tag.c | 1 -
tempfile.c | 1 -
trace.c | 1 -
trace2/tr2_tls.c | 1 -
trailer.c | 1 -
transport-helper.c | 1 -
transport.c | 2 -
tree-walk.c | 1 -
upload-pack.c | 1 -
usage.c | 1 -
usage.h | 60 +++
userdiff.c | 1 -
worktree.c | 2 -
wrapper.c | 1 -
wrapper.h | 111 +++++
write-or-die.c | 1 -
142 files changed, 756 insertions(+), 897 deletions(-)
create mode 100644 common.h
create mode 100644 usage.h
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
@ 2023-05-16 17:09 ` Calvin Wan
2023-05-16 21:18 ` Junio C Hamano
2023-05-16 17:09 ` [PATCH 2/6] wrapper.h: move declarations for wrapper.c " Calvin Wan
` (6 subsequent siblings)
7 siblings, 1 reply; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 17:09 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
While functions like starts_with() probably should not belong in the
boundaries of the strbuf library, this commit focuses on first splitting
out headers from git-compat-util.h.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
git-compat-util.h | 32 --------------------------------
strbuf.h | 32 ++++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 32 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 5b2b99c17c..51af0a53aa 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -679,9 +679,6 @@ void set_warn_routine(report_fn routine);
report_fn get_warn_routine(void);
void set_die_is_recursing_routine(int (*routine)(void));
-int starts_with(const char *str, const char *prefix);
-int istarts_with(const char *str, const char *prefix);
-
/*
* If the string "str" begins with the string found in "prefix", return 1.
* The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
@@ -710,29 +707,6 @@ static inline int skip_prefix(const char *str, const char *prefix,
return 0;
}
-/*
- * If the string "str" is the same as the string in "prefix", then the "arg"
- * parameter is set to the "def" parameter and 1 is returned.
- * If the string "str" begins with the string found in "prefix" and then a
- * "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1"
- * (i.e., to the point in the string right after the prefix and the "=" sign),
- * and 1 is returned.
- *
- * Otherwise, return 0 and leave "arg" untouched.
- *
- * When we accept both a "--key" and a "--key=<val>" option, this function
- * can be used instead of !strcmp(arg, "--key") and then
- * skip_prefix(arg, "--key=", &arg) to parse such an option.
- */
-int skip_to_optional_arg_default(const char *str, const char *prefix,
- const char **arg, const char *def);
-
-static inline int skip_to_optional_arg(const char *str, const char *prefix,
- const char **arg)
-{
- return skip_to_optional_arg_default(str, prefix, arg, "");
-}
-
/*
* Like skip_prefix, but promises never to read past "len" bytes of the input
* buffer, and returns the remaining number of bytes in "out" via "outlen".
@@ -777,12 +751,6 @@ static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
return strip_suffix_mem(str, len, suffix);
}
-static inline int ends_with(const char *str, const char *suffix)
-{
- size_t len;
- return strip_suffix(str, suffix, &len);
-}
-
#define SWAP(a, b) do { \
void *_swap_a_ptr = &(a); \
void *_swap_b_ptr = &(b); \
diff --git a/strbuf.h b/strbuf.h
index 3dfeadb44c..5e6f7f3d8e 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -727,4 +727,36 @@ char *xstrvfmt(const char *fmt, va_list ap);
__attribute__((format (printf, 1, 2)))
char *xstrfmt(const char *fmt, ...);
+int starts_with(const char *str, const char *prefix);
+int istarts_with(const char *str, const char *prefix);
+
+/*
+ * If the string "str" is the same as the string in "prefix", then the "arg"
+ * parameter is set to the "def" parameter and 1 is returned.
+ * If the string "str" begins with the string found in "prefix" and then a
+ * "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1"
+ * (i.e., to the point in the string right after the prefix and the "=" sign),
+ * and 1 is returned.
+ *
+ * Otherwise, return 0 and leave "arg" untouched.
+ *
+ * When we accept both a "--key" and a "--key=<val>" option, this function
+ * can be used instead of !strcmp(arg, "--key") and then
+ * skip_prefix(arg, "--key=", &arg) to parse such an option.
+ */
+int skip_to_optional_arg_default(const char *str, const char *prefix,
+ const char **arg, const char *def);
+
+static inline int skip_to_optional_arg(const char *str, const char *prefix,
+ const char **arg)
+{
+ return skip_to_optional_arg_default(str, prefix, arg, "");
+}
+
+static inline int ends_with(const char *str, const char *suffix)
+{
+ size_t len;
+ return strip_suffix(str, suffix, &len);
+}
+
#endif /* STRBUF_H */
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH 2/6] wrapper.h: move declarations for wrapper.c functions from git-compat-util.h
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
2023-05-16 17:09 ` [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
@ 2023-05-16 17:09 ` Calvin Wan
2023-05-16 22:08 ` Junio C Hamano
2023-05-16 17:09 ` [PATCH 3/6] common.h: move non-compat specific macros and " Calvin Wan
` (5 subsequent siblings)
7 siblings, 1 reply; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 17:09 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Since the functions in wrapper.c are widely used across the codebase,
include it by default in git-compat-util.h. A future patch will remove
now unnecessary inclusions of wrapper.h from other files.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
git-compat-util.h | 113 +---------------------------------------------
wrapper.h | 111 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 112 insertions(+), 112 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index 51af0a53aa..9898fe9374 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -626,8 +626,7 @@ static inline int git_has_dir_sep(const char *path)
#include "compat/bswap.h"
#include "wildmatch.h"
-
-struct strbuf;
+#include "wrapper.h"
/* General helper functions */
NORETURN void usage(const char *err);
@@ -1047,36 +1046,6 @@ static inline int cast_size_t_to_int(size_t a)
# define xalloca(size) (xmalloc(size))
# define xalloca_free(p) (free(p))
#endif
-char *xstrdup(const char *str);
-void *xmalloc(size_t size);
-void *xmallocz(size_t size);
-void *xmallocz_gently(size_t size);
-void *xmemdupz(const void *data, size_t len);
-char *xstrndup(const char *str, size_t len);
-void *xrealloc(void *ptr, size_t size);
-void *xcalloc(size_t nmemb, size_t size);
-void xsetenv(const char *name, const char *value, int overwrite);
-void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
-const char *mmap_os_err(void);
-void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
-int xopen(const char *path, int flags, ...);
-ssize_t xread(int fd, void *buf, size_t len);
-ssize_t xwrite(int fd, const void *buf, size_t len);
-ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
-int xdup(int fd);
-FILE *xfopen(const char *path, const char *mode);
-FILE *xfdopen(int fd, const char *mode);
-int xmkstemp(char *temp_filename);
-int xmkstemp_mode(char *temp_filename, int mode);
-char *xgetcwd(void);
-FILE *fopen_for_writing(const char *path);
-FILE *fopen_or_warn(const char *path, const char *mode);
-
-/*
- * Like strncmp, but only return zero if s is NUL-terminated and exactly len
- * characters long. If it is not, consider it greater than t.
- */
-int xstrncmpz(const char *s, const char *t, size_t len);
/*
* FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
@@ -1178,15 +1147,10 @@ static inline size_t xsize_t(off_t len)
return (size_t) len;
}
-__attribute__((format (printf, 3, 4)))
-int xsnprintf(char *dst, size_t max, const char *fmt, ...);
-
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
-int xgethostname(char *buf, size_t len);
-
/* in ctype.c, for kwset users */
extern const unsigned char tolower_trans_tbl[256];
@@ -1427,72 +1391,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);
#endif
#endif
-enum fsync_action {
- FSYNC_WRITEOUT_ONLY,
- FSYNC_HARDWARE_FLUSH
-};
-
-/*
- * Issues an fsync against the specified file according to the specified mode.
- *
- * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
- * systems to flush the OS cache without issuing a flush command to the storage
- * controller. If those interfaces are unavailable, the function fails with
- * ENOSYS.
- *
- * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
- * changes are durable. It is not expected to fail.
- */
-int git_fsync(int fd, enum fsync_action action);
-
-/*
- * Writes out trace statistics for fsync using the trace2 API.
- */
-void trace_git_fsync_stats(void);
-
-/*
- * Preserves errno, prints a message, but gives no warning for ENOENT.
- * Returns 0 on success, which includes trying to unlink an object that does
- * not exist.
- */
-int unlink_or_warn(const char *path);
- /*
- * Tries to unlink file. Returns 0 if unlink succeeded
- * or the file already didn't exist. Returns -1 and
- * appends a message to err suitable for
- * 'error("%s", err->buf)' on error.
- */
-int unlink_or_msg(const char *file, struct strbuf *err);
-/*
- * Preserves errno, prints a message, but gives no warning for ENOENT.
- * Returns 0 on success, which includes trying to remove a directory that does
- * not exist.
- */
-int rmdir_or_warn(const char *path);
-/*
- * Calls the correct function out of {unlink,rmdir}_or_warn based on
- * the supplied file mode.
- */
-int remove_or_warn(unsigned int mode, const char *path);
-
-/*
- * Call access(2), but warn for any error except "missing file"
- * (ENOENT or ENOTDIR).
- */
-#define ACCESS_EACCES_OK (1U << 0)
-int access_or_warn(const char *path, int mode, unsigned flag);
-int access_or_die(const char *path, int mode, unsigned flag);
-
-/* Warn on an inaccessible file if errno indicates this is an error */
-int warn_on_fopen_errors(const char *path);
-
-/*
- * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
- * may be racy. Do not use this as protection against an attacker who can
- * simultaneously create paths.
- */
-int open_nofollow(const char *path, int flags);
-
#ifndef SHELL_PATH
# define SHELL_PATH "/bin/sh"
#endif
@@ -1632,13 +1530,4 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
#endif /* !__GNUC__ */
-void sleep_millisec(int millisec);
-
-/*
- * Generate len bytes from the system cryptographically secure PRNG.
- * Returns 0 on success and -1 on error, setting errno. The inability to
- * satisfy the full request is an error.
- */
-int csprng_bytes(void *buf, size_t len);
-
#endif
diff --git a/wrapper.h b/wrapper.h
index f0c7d0616d..c85b1328d1 100644
--- a/wrapper.h
+++ b/wrapper.h
@@ -1,6 +1,42 @@
#ifndef WRAPPER_H
#define WRAPPER_H
+char *xstrdup(const char *str);
+void *xmalloc(size_t size);
+void *xmallocz(size_t size);
+void *xmallocz_gently(size_t size);
+void *xmemdupz(const void *data, size_t len);
+char *xstrndup(const char *str, size_t len);
+void *xrealloc(void *ptr, size_t size);
+void *xcalloc(size_t nmemb, size_t size);
+void xsetenv(const char *name, const char *value, int overwrite);
+void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
+const char *mmap_os_err(void);
+void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
+int xopen(const char *path, int flags, ...);
+ssize_t xread(int fd, void *buf, size_t len);
+ssize_t xwrite(int fd, const void *buf, size_t len);
+ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
+int xdup(int fd);
+FILE *xfopen(const char *path, const char *mode);
+FILE *xfdopen(int fd, const char *mode);
+int xmkstemp(char *temp_filename);
+int xmkstemp_mode(char *temp_filename, int mode);
+char *xgetcwd(void);
+FILE *fopen_for_writing(const char *path);
+FILE *fopen_or_warn(const char *path, const char *mode);
+
+/*
+ * Like strncmp, but only return zero if s is NUL-terminated and exactly len
+ * characters long. If it is not, consider it greater than t.
+ */
+int xstrncmpz(const char *s, const char *t, size_t len);
+
+__attribute__((format (printf, 3, 4)))
+int xsnprintf(char *dst, size_t max, const char *fmt, ...);
+
+int xgethostname(char *buf, size_t len);
+
/* set default permissions by passing mode arguments to open(2) */
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
int git_mkstemp_mode(char *pattern, int mode);
@@ -33,4 +69,79 @@ void write_file(const char *path, const char *fmt, ...);
/* Return 1 if the file is empty or does not exists, 0 otherwise. */
int is_empty_or_missing_file(const char *filename);
+enum fsync_action {
+ FSYNC_WRITEOUT_ONLY,
+ FSYNC_HARDWARE_FLUSH
+};
+
+/*
+ * Issues an fsync against the specified file according to the specified mode.
+ *
+ * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
+ * systems to flush the OS cache without issuing a flush command to the storage
+ * controller. If those interfaces are unavailable, the function fails with
+ * ENOSYS.
+ *
+ * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
+ * changes are durable. It is not expected to fail.
+ */
+int git_fsync(int fd, enum fsync_action action);
+
+/*
+ * Writes out trace statistics for fsync using the trace2 API.
+ */
+void trace_git_fsync_stats(void);
+
+/*
+ * Preserves errno, prints a message, but gives no warning for ENOENT.
+ * Returns 0 on success, which includes trying to unlink an object that does
+ * not exist.
+ */
+int unlink_or_warn(const char *path);
+ /*
+ * Tries to unlink file. Returns 0 if unlink succeeded
+ * or the file already didn't exist. Returns -1 and
+ * appends a message to err suitable for
+ * 'error("%s", err->buf)' on error.
+ */
+int unlink_or_msg(const char *file, struct strbuf *err);
+/*
+ * Preserves errno, prints a message, but gives no warning for ENOENT.
+ * Returns 0 on success, which includes trying to remove a directory that does
+ * not exist.
+ */
+int rmdir_or_warn(const char *path);
+/*
+ * Calls the correct function out of {unlink,rmdir}_or_warn based on
+ * the supplied file mode.
+ */
+int remove_or_warn(unsigned int mode, const char *path);
+
+/*
+ * Call access(2), but warn for any error except "missing file"
+ * (ENOENT or ENOTDIR).
+ */
+#define ACCESS_EACCES_OK (1U << 0)
+int access_or_warn(const char *path, int mode, unsigned flag);
+int access_or_die(const char *path, int mode, unsigned flag);
+
+/* Warn on an inaccessible file if errno indicates this is an error */
+int warn_on_fopen_errors(const char *path);
+
+/*
+ * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
+ * may be racy. Do not use this as protection against an attacker who can
+ * simultaneously create paths.
+ */
+int open_nofollow(const char *path, int flags);
+
+void sleep_millisec(int millisec);
+
+/*
+ * Generate len bytes from the system cryptographically secure PRNG.
+ * Returns 0 on success and -1 on error, setting errno. The inability to
+ * satisfy the full request is an error.
+ */
+int csprng_bytes(void *buf, size_t len);
+
#endif /* WRAPPER_H */
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH 3/6] common.h: move non-compat specific macros and functions from git-compat-util.h
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
2023-05-16 17:09 ` [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
2023-05-16 17:09 ` [PATCH 2/6] wrapper.h: move declarations for wrapper.c " Calvin Wan
@ 2023-05-16 17:09 ` Calvin Wan
2023-05-16 22:30 ` Junio C Hamano
2023-05-16 17:09 ` [PATCH 4/6] usage.h: move declarations for usage.c " Calvin Wan
` (4 subsequent siblings)
7 siblings, 1 reply; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 17:09 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
git-compat-util.h has grown to a large and difficult to read file partly
because its set of functionality is overloaded. Besides being a
compatibility file that ensures Git's operation across different
operating systems, it has also become a dumping ground for commonly used
macros and static inline functions.
This commit separates out macros and static inline functions that are
compatible across operating systems into common.h. common.h is
reincluded back into git-compat-util.h so that other files do not also
have to include it. The eventual goal is for common.h to be separated
out into more reasonable boundaries, but for now this should be a good
first step towards that goal.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
common.h | 475 ++++++++++++++++++++++++++++++++++++++++++++++
git-compat-util.h | 467 +--------------------------------------------
2 files changed, 477 insertions(+), 465 deletions(-)
create mode 100644 common.h
diff --git a/common.h b/common.h
new file mode 100644
index 0000000000..c0fdc5eb98
--- /dev/null
+++ b/common.h
@@ -0,0 +1,475 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include "git-compat-util.h"
+#include "wrapper.h"
+
+/*
+ * ARRAY_SIZE - get the number of elements in a visible array
+ * @x: the array whose size you want.
+ *
+ * This does not work on pointers, or arrays declared as [], or
+ * function parameters. With correct compiler support, such usage
+ * will cause a build error (see the build_assert_or_zero macro).
+ */
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))
+
+#define bitsizeof(x) (CHAR_BIT * sizeof(x))
+
+#define maximum_signed_value_of_type(a) \
+ (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
+
+#define maximum_unsigned_value_of_type(a) \
+ (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
+
+/*
+ * Signed integer overflow is undefined in C, so here's a helper macro
+ * to detect if the sum of two integers will overflow.
+ *
+ * Requires: a >= 0, typeof(a) equals typeof(b)
+ */
+#define signed_add_overflows(a, b) \
+ ((b) > maximum_signed_value_of_type(a) - (a))
+
+#define unsigned_add_overflows(a, b) \
+ ((b) > maximum_unsigned_value_of_type(a) - (a))
+
+/*
+ * Returns true if the multiplication of "a" and "b" will
+ * overflow. The types of "a" and "b" must match and must be unsigned.
+ * Note that this macro evaluates "a" twice!
+ */
+#define unsigned_mult_overflows(a, b) \
+ ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
+
+/*
+ * Returns true if the left shift of "a" by "shift" bits will
+ * overflow. The type of "a" must be unsigned.
+ */
+#define unsigned_left_shift_overflows(a, shift) \
+ ((shift) < bitsizeof(a) && \
+ (a) > maximum_unsigned_value_of_type(a) >> (shift))
+
+#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
+#define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
+
+#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
+
+/* Approximation of the length of the decimal representation of this type. */
+#define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
+
+/*
+ * If the string "str" begins with the string found in "prefix", return 1.
+ * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
+ * the string right after the prefix).
+ *
+ * Otherwise, return 0 and leave "out" untouched.
+ *
+ * Examples:
+ *
+ * [extract branch name, fail if not a branch]
+ * if (!skip_prefix(ref, "refs/heads/", &branch)
+ * return -1;
+ *
+ * [skip prefix if present, otherwise use whole string]
+ * skip_prefix(name, "refs/heads/", &name);
+ */
+static inline int skip_prefix(const char *str, const char *prefix,
+ const char **out)
+{
+ do {
+ if (!*prefix) {
+ *out = str;
+ return 1;
+ }
+ } while (*str++ == *prefix++);
+ return 0;
+}
+
+/*
+ * Like skip_prefix, but promises never to read past "len" bytes of the input
+ * buffer, and returns the remaining number of bytes in "out" via "outlen".
+ */
+static inline int skip_prefix_mem(const char *buf, size_t len,
+ const char *prefix,
+ const char **out, size_t *outlen)
+{
+ size_t prefix_len = strlen(prefix);
+ if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
+ *out = buf + prefix_len;
+ *outlen = len - prefix_len;
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * If buf ends with suffix, return 1 and subtract the length of the suffix
+ * from *len. Otherwise, return 0 and leave *len untouched.
+ */
+static inline int strip_suffix_mem(const char *buf, size_t *len,
+ const char *suffix)
+{
+ size_t suflen = strlen(suffix);
+ if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
+ return 0;
+ *len -= suflen;
+ return 1;
+}
+
+/*
+ * If str ends with suffix, return 1 and set *len to the size of the string
+ * without the suffix. Otherwise, return 0 and set *len to the size of the
+ * string.
+ *
+ * Note that we do _not_ NUL-terminate str to the new length.
+ */
+static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
+{
+ *len = strlen(str);
+ return strip_suffix_mem(str, len, suffix);
+}
+
+#define SWAP(a, b) do { \
+ void *_swap_a_ptr = &(a); \
+ void *_swap_b_ptr = &(b); \
+ unsigned char _swap_buffer[sizeof(a)]; \
+ memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
+ memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
+ BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
+ memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
+} while (0)
+
+static inline size_t st_add(size_t a, size_t b)
+{
+ if (unsigned_add_overflows(a, b))
+ die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
+ (uintmax_t)a, (uintmax_t)b);
+ return a + b;
+}
+#define st_add3(a,b,c) st_add(st_add((a),(b)),(c))
+#define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d))
+
+static inline size_t st_mult(size_t a, size_t b)
+{
+ if (unsigned_mult_overflows(a, b))
+ die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
+ (uintmax_t)a, (uintmax_t)b);
+ return a * b;
+}
+
+static inline size_t st_sub(size_t a, size_t b)
+{
+ if (a < b)
+ die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
+ (uintmax_t)a, (uintmax_t)b);
+ return a - b;
+}
+
+static inline size_t st_left_shift(size_t a, unsigned shift)
+{
+ if (unsigned_left_shift_overflows(a, shift))
+ die("size_t overflow: %"PRIuMAX" << %u",
+ (uintmax_t)a, shift);
+ return a << shift;
+}
+
+static inline unsigned long cast_size_t_to_ulong(size_t a)
+{
+ if (a != (unsigned long)a)
+ die("object too large to read on this platform: %"
+ PRIuMAX" is cut off to %lu",
+ (uintmax_t)a, (unsigned long)a);
+ return (unsigned long)a;
+}
+
+static inline int cast_size_t_to_int(size_t a)
+{
+ if (a > INT_MAX)
+ die("number too large to represent as int on this platform: %"PRIuMAX,
+ (uintmax_t)a);
+ return (int)a;
+}
+
+
+/*
+ * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
+ * that ptr is used twice, so don't pass e.g. ptr++.
+ */
+#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
+
+#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
+#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
+#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
+
+#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
+ BARF_UNLESS_COPYABLE((dst), (src)))
+static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
+{
+ if (n)
+ memcpy(dst, src, st_mult(size, n));
+}
+
+#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
+ BARF_UNLESS_COPYABLE((dst), (src)))
+static inline void move_array(void *dst, const void *src, size_t n, size_t size)
+{
+ if (n)
+ memmove(dst, src, st_mult(size, n));
+}
+
+#define DUP_ARRAY(dst, src, n) do { \
+ size_t dup_array_n_ = (n); \
+ COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \
+} while (0)
+
+/*
+ * These functions help you allocate structs with flex arrays, and copy
+ * the data directly into the array. For example, if you had:
+ *
+ * struct foo {
+ * int bar;
+ * char name[FLEX_ARRAY];
+ * };
+ *
+ * you can do:
+ *
+ * struct foo *f;
+ * FLEX_ALLOC_MEM(f, name, src, len);
+ *
+ * to allocate a "foo" with the contents of "src" in the "name" field.
+ * The resulting struct is automatically zero'd, and the flex-array field
+ * is NUL-terminated (whether the incoming src buffer was or not).
+ *
+ * The FLEXPTR_* variants operate on structs that don't use flex-arrays,
+ * but do want to store a pointer to some extra data in the same allocated
+ * block. For example, if you have:
+ *
+ * struct foo {
+ * char *name;
+ * int bar;
+ * };
+ *
+ * you can do:
+ *
+ * struct foo *f;
+ * FLEXPTR_ALLOC_STR(f, name, src);
+ *
+ * and "name" will point to a block of memory after the struct, which will be
+ * freed along with the struct (but the pointer can be repointed anywhere).
+ *
+ * The *_STR variants accept a string parameter rather than a ptr/len
+ * combination.
+ *
+ * Note that these macros will evaluate the first parameter multiple
+ * times, and it must be assignable as an lvalue.
+ */
+#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
+} while (0)
+#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((x) + 1, (buf), flex_array_len_); \
+ (x)->ptrname = (void *)((x)+1); \
+} while(0)
+#define FLEX_ALLOC_STR(x, flexname, str) \
+ FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
+#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
+ FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
+
+static inline char *xstrdup_or_null(const char *str)
+{
+ return str ? xstrdup(str) : NULL;
+}
+
+static inline size_t xsize_t(off_t len)
+{
+ if (len < 0 || (uintmax_t) len > SIZE_MAX)
+ die("Cannot handle files this big");
+ return (size_t) len;
+}
+
+/* in ctype.c, for kwset users */
+extern const unsigned char tolower_trans_tbl[256];
+
+/* Sane ctype - no locale, and works with signed chars */
+#undef isascii
+#undef isspace
+#undef isdigit
+#undef isalpha
+#undef isalnum
+#undef isprint
+#undef islower
+#undef isupper
+#undef tolower
+#undef toupper
+#undef iscntrl
+#undef ispunct
+#undef isxdigit
+
+extern const unsigned char sane_ctype[256];
+extern const signed char hexval_table[256];
+#define GIT_SPACE 0x01
+#define GIT_DIGIT 0x02
+#define GIT_ALPHA 0x04
+#define GIT_GLOB_SPECIAL 0x08
+#define GIT_REGEX_SPECIAL 0x10
+#define GIT_PATHSPEC_MAGIC 0x20
+#define GIT_CNTRL 0x40
+#define GIT_PUNCT 0x80
+#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
+#define isascii(x) (((x) & ~0x7f) == 0)
+#define isspace(x) sane_istest(x,GIT_SPACE)
+#define isdigit(x) sane_istest(x,GIT_DIGIT)
+#define isalpha(x) sane_istest(x,GIT_ALPHA)
+#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
+#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
+#define islower(x) sane_iscase(x, 1)
+#define isupper(x) sane_iscase(x, 0)
+#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
+#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
+#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
+#define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
+ GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
+#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
+#define tolower(x) sane_case((unsigned char)(x), 0x20)
+#define toupper(x) sane_case((unsigned char)(x), 0)
+#define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
+
+static inline int sane_case(int x, int high)
+{
+ if (sane_istest(x, GIT_ALPHA))
+ x = (x & ~0x20) | high;
+ return x;
+}
+
+static inline int sane_iscase(int x, int is_lower)
+{
+ if (!sane_istest(x, GIT_ALPHA))
+ return 0;
+
+ if (is_lower)
+ return (x & 0x20) != 0;
+ else
+ return (x & 0x20) == 0;
+}
+
+/*
+ * Like skip_prefix, but compare case-insensitively. Note that the comparison
+ * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
+ * locale-specific conversions).
+ */
+static inline int skip_iprefix(const char *str, const char *prefix,
+ const char **out)
+{
+ do {
+ if (!*prefix) {
+ *out = str;
+ return 1;
+ }
+ } while (tolower(*str++) == tolower(*prefix++));
+ return 0;
+}
+
+/*
+ * Like skip_prefix_mem, but compare case-insensitively. Note that the
+ * comparison is done via tolower(), so it is strictly ASCII (no multi-byte
+ * characters or locale-specific conversions).
+ */
+static inline int skip_iprefix_mem(const char *buf, size_t len,
+ const char *prefix,
+ const char **out, size_t *outlen)
+{
+ do {
+ if (!*prefix) {
+ *out = buf;
+ *outlen = len;
+ return 1;
+ }
+ } while (len-- > 0 && tolower(*buf++) == tolower(*prefix++));
+ return 0;
+}
+
+static inline int strtoul_ui(char const *s, int base, unsigned int *result)
+{
+ unsigned long ul;
+ char *p;
+
+ errno = 0;
+ /* negative values would be accepted by strtoul */
+ if (strchr(s, '-'))
+ return -1;
+ ul = strtoul(s, &p, base);
+ if (errno || *p || p == s || (unsigned int) ul != ul)
+ return -1;
+ *result = ul;
+ return 0;
+}
+
+static inline int strtol_i(char const *s, int base, int *result)
+{
+ long ul;
+ char *p;
+
+ errno = 0;
+ ul = strtol(s, &p, base);
+ if (errno || *p || p == s || (int) ul != ul)
+ return -1;
+ *result = ul;
+ return 0;
+}
+
+static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
+ size_t nmatch, regmatch_t pmatch[], int eflags)
+{
+ assert(nmatch > 0 && pmatch);
+ pmatch[0].rm_so = 0;
+ pmatch[0].rm_eo = size;
+ return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
+}
+
+/*
+ * Our code often opens a path to an optional file, to work on its
+ * contents when we can successfully open it. We can ignore a failure
+ * to open if such an optional file does not exist, but we do want to
+ * report a failure in opening for other reasons (e.g. we got an I/O
+ * error, or the file is there, but we lack the permission to open).
+ *
+ * Call this function after seeing an error from open() or fopen() to
+ * see if the errno indicates a missing file that we can safely ignore.
+ */
+static inline int is_missing_file_error(int errno_)
+{
+ return (errno_ == ENOENT || errno_ == ENOTDIR);
+}
+
+/*
+ * container_of - Get the address of an object containing a field.
+ *
+ * @ptr: pointer to the field.
+ * @type: type of the object.
+ * @member: name of the field within the object.
+ */
+#define container_of(ptr, type, member) \
+ ((type *) ((char *)(ptr) - offsetof(type, member)))
+
+/*
+ * helper function for `container_of_or_null' to avoid multiple
+ * evaluation of @ptr
+ */
+static inline void *container_of_or_null_offset(void *ptr, size_t offset)
+{
+ return ptr ? (char *)ptr - offset : NULL;
+}
+
+/*
+ * like `container_of', but allows returned value to be NULL
+ */
+#define container_of_or_null(ptr, type, member) \
+ (type *)container_of_or_null_offset(ptr, offsetof(type, member))
+
+
+#endif
diff --git a/git-compat-util.h b/git-compat-util.h
index 9898fe9374..c2b92a3033 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -106,51 +106,6 @@ struct strbuf;
BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \
sizeof(*(dst)) == sizeof(*(src)))
#endif
-/*
- * ARRAY_SIZE - get the number of elements in a visible array
- * @x: the array whose size you want.
- *
- * This does not work on pointers, or arrays declared as [], or
- * function parameters. With correct compiler support, such usage
- * will cause a build error (see the build_assert_or_zero macro).
- */
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))
-
-#define bitsizeof(x) (CHAR_BIT * sizeof(x))
-
-#define maximum_signed_value_of_type(a) \
- (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
-
-#define maximum_unsigned_value_of_type(a) \
- (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
-
-/*
- * Signed integer overflow is undefined in C, so here's a helper macro
- * to detect if the sum of two integers will overflow.
- *
- * Requires: a >= 0, typeof(a) equals typeof(b)
- */
-#define signed_add_overflows(a, b) \
- ((b) > maximum_signed_value_of_type(a) - (a))
-
-#define unsigned_add_overflows(a, b) \
- ((b) > maximum_unsigned_value_of_type(a) - (a))
-
-/*
- * Returns true if the multiplication of "a" and "b" will
- * overflow. The types of "a" and "b" must match and must be unsigned.
- * Note that this macro evaluates "a" twice!
- */
-#define unsigned_mult_overflows(a, b) \
- ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
-
-/*
- * Returns true if the left shift of "a" by "shift" bits will
- * overflow. The type of "a" must be unsigned.
- */
-#define unsigned_left_shift_overflows(a, shift) \
- ((shift) < bitsizeof(a) && \
- (a) > maximum_unsigned_value_of_type(a) >> (shift))
#ifdef __GNUC__
#define TYPEOF(x) (__typeof__(x))
@@ -158,14 +113,6 @@ struct strbuf;
#define TYPEOF(x)
#endif
-#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
-#define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
-
-#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
-
-/* Approximation of the length of the decimal representation of this type. */
-#define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
-
#ifdef __MINGW64__
#define _POSIX_C_SOURCE 1
#elif defined(__sun__)
@@ -678,88 +625,6 @@ void set_warn_routine(report_fn routine);
report_fn get_warn_routine(void);
void set_die_is_recursing_routine(int (*routine)(void));
-/*
- * If the string "str" begins with the string found in "prefix", return 1.
- * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
- * the string right after the prefix).
- *
- * Otherwise, return 0 and leave "out" untouched.
- *
- * Examples:
- *
- * [extract branch name, fail if not a branch]
- * if (!skip_prefix(ref, "refs/heads/", &branch)
- * return -1;
- *
- * [skip prefix if present, otherwise use whole string]
- * skip_prefix(name, "refs/heads/", &name);
- */
-static inline int skip_prefix(const char *str, const char *prefix,
- const char **out)
-{
- do {
- if (!*prefix) {
- *out = str;
- return 1;
- }
- } while (*str++ == *prefix++);
- return 0;
-}
-
-/*
- * Like skip_prefix, but promises never to read past "len" bytes of the input
- * buffer, and returns the remaining number of bytes in "out" via "outlen".
- */
-static inline int skip_prefix_mem(const char *buf, size_t len,
- const char *prefix,
- const char **out, size_t *outlen)
-{
- size_t prefix_len = strlen(prefix);
- if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
- *out = buf + prefix_len;
- *outlen = len - prefix_len;
- return 1;
- }
- return 0;
-}
-
-/*
- * If buf ends with suffix, return 1 and subtract the length of the suffix
- * from *len. Otherwise, return 0 and leave *len untouched.
- */
-static inline int strip_suffix_mem(const char *buf, size_t *len,
- const char *suffix)
-{
- size_t suflen = strlen(suffix);
- if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
- return 0;
- *len -= suflen;
- return 1;
-}
-
-/*
- * If str ends with suffix, return 1 and set *len to the size of the string
- * without the suffix. Otherwise, return 0 and set *len to the size of the
- * string.
- *
- * Note that we do _not_ NUL-terminate str to the new length.
- */
-static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
-{
- *len = strlen(str);
- return strip_suffix_mem(str, len, suffix);
-}
-
-#define SWAP(a, b) do { \
- void *_swap_a_ptr = &(a); \
- void *_swap_b_ptr = &(b); \
- unsigned char _swap_buffer[sizeof(a)]; \
- memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
- memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
- BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
- memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
-} while (0)
-
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
#ifndef PROT_READ
@@ -965,57 +830,6 @@ const char *inet_ntop(int af, const void *src, char *dst, size_t size);
int git_atexit(void (*handler)(void));
#endif
-static inline size_t st_add(size_t a, size_t b)
-{
- if (unsigned_add_overflows(a, b))
- die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
- (uintmax_t)a, (uintmax_t)b);
- return a + b;
-}
-#define st_add3(a,b,c) st_add(st_add((a),(b)),(c))
-#define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d))
-
-static inline size_t st_mult(size_t a, size_t b)
-{
- if (unsigned_mult_overflows(a, b))
- die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
- (uintmax_t)a, (uintmax_t)b);
- return a * b;
-}
-
-static inline size_t st_sub(size_t a, size_t b)
-{
- if (a < b)
- die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
- (uintmax_t)a, (uintmax_t)b);
- return a - b;
-}
-
-static inline size_t st_left_shift(size_t a, unsigned shift)
-{
- if (unsigned_left_shift_overflows(a, shift))
- die("size_t overflow: %"PRIuMAX" << %u",
- (uintmax_t)a, shift);
- return a << shift;
-}
-
-static inline unsigned long cast_size_t_to_ulong(size_t a)
-{
- if (a != (unsigned long)a)
- die("object too large to read on this platform: %"
- PRIuMAX" is cut off to %lu",
- (uintmax_t)a, (unsigned long)a);
- return (unsigned long)a;
-}
-
-static inline int cast_size_t_to_int(size_t a)
-{
- if (a > INT_MAX)
- die("number too large to represent as int on this platform: %"PRIuMAX,
- (uintmax_t)a);
- return (int)a;
-}
-
/*
* Limit size of IO chunks, because huge chunks only cause pain. OS X
* 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
@@ -1047,240 +861,10 @@ static inline int cast_size_t_to_int(size_t a)
# define xalloca_free(p) (free(p))
#endif
-/*
- * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
- * that ptr is used twice, so don't pass e.g. ptr++.
- */
-#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
-
-#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
-#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
-#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
-
-#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
- BARF_UNLESS_COPYABLE((dst), (src)))
-static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
-{
- if (n)
- memcpy(dst, src, st_mult(size, n));
-}
-
-#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
- BARF_UNLESS_COPYABLE((dst), (src)))
-static inline void move_array(void *dst, const void *src, size_t n, size_t size)
-{
- if (n)
- memmove(dst, src, st_mult(size, n));
-}
-
-#define DUP_ARRAY(dst, src, n) do { \
- size_t dup_array_n_ = (n); \
- COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \
-} while (0)
-
-/*
- * These functions help you allocate structs with flex arrays, and copy
- * the data directly into the array. For example, if you had:
- *
- * struct foo {
- * int bar;
- * char name[FLEX_ARRAY];
- * };
- *
- * you can do:
- *
- * struct foo *f;
- * FLEX_ALLOC_MEM(f, name, src, len);
- *
- * to allocate a "foo" with the contents of "src" in the "name" field.
- * The resulting struct is automatically zero'd, and the flex-array field
- * is NUL-terminated (whether the incoming src buffer was or not).
- *
- * The FLEXPTR_* variants operate on structs that don't use flex-arrays,
- * but do want to store a pointer to some extra data in the same allocated
- * block. For example, if you have:
- *
- * struct foo {
- * char *name;
- * int bar;
- * };
- *
- * you can do:
- *
- * struct foo *f;
- * FLEXPTR_ALLOC_STR(f, name, src);
- *
- * and "name" will point to a block of memory after the struct, which will be
- * freed along with the struct (but the pointer can be repointed anywhere).
- *
- * The *_STR variants accept a string parameter rather than a ptr/len
- * combination.
- *
- * Note that these macros will evaluate the first parameter multiple
- * times, and it must be assignable as an lvalue.
- */
-#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
- size_t flex_array_len_ = (len); \
- (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
- memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
-} while (0)
-#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
- size_t flex_array_len_ = (len); \
- (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
- memcpy((x) + 1, (buf), flex_array_len_); \
- (x)->ptrname = (void *)((x)+1); \
-} while(0)
-#define FLEX_ALLOC_STR(x, flexname, str) \
- FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
-#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
- FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
-
-static inline char *xstrdup_or_null(const char *str)
-{
- return str ? xstrdup(str) : NULL;
-}
-
-static inline size_t xsize_t(off_t len)
-{
- if (len < 0 || (uintmax_t) len > SIZE_MAX)
- die("Cannot handle files this big");
- return (size_t) len;
-}
-
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
-/* in ctype.c, for kwset users */
-extern const unsigned char tolower_trans_tbl[256];
-
-/* Sane ctype - no locale, and works with signed chars */
-#undef isascii
-#undef isspace
-#undef isdigit
-#undef isalpha
-#undef isalnum
-#undef isprint
-#undef islower
-#undef isupper
-#undef tolower
-#undef toupper
-#undef iscntrl
-#undef ispunct
-#undef isxdigit
-
-extern const unsigned char sane_ctype[256];
-extern const signed char hexval_table[256];
-#define GIT_SPACE 0x01
-#define GIT_DIGIT 0x02
-#define GIT_ALPHA 0x04
-#define GIT_GLOB_SPECIAL 0x08
-#define GIT_REGEX_SPECIAL 0x10
-#define GIT_PATHSPEC_MAGIC 0x20
-#define GIT_CNTRL 0x40
-#define GIT_PUNCT 0x80
-#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
-#define isascii(x) (((x) & ~0x7f) == 0)
-#define isspace(x) sane_istest(x,GIT_SPACE)
-#define isdigit(x) sane_istest(x,GIT_DIGIT)
-#define isalpha(x) sane_istest(x,GIT_ALPHA)
-#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
-#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
-#define islower(x) sane_iscase(x, 1)
-#define isupper(x) sane_iscase(x, 0)
-#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
-#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
-#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
-#define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
- GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
-#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
-#define tolower(x) sane_case((unsigned char)(x), 0x20)
-#define toupper(x) sane_case((unsigned char)(x), 0)
-#define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
-
-static inline int sane_case(int x, int high)
-{
- if (sane_istest(x, GIT_ALPHA))
- x = (x & ~0x20) | high;
- return x;
-}
-
-static inline int sane_iscase(int x, int is_lower)
-{
- if (!sane_istest(x, GIT_ALPHA))
- return 0;
-
- if (is_lower)
- return (x & 0x20) != 0;
- else
- return (x & 0x20) == 0;
-}
-
-/*
- * Like skip_prefix, but compare case-insensitively. Note that the comparison
- * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
- * locale-specific conversions).
- */
-static inline int skip_iprefix(const char *str, const char *prefix,
- const char **out)
-{
- do {
- if (!*prefix) {
- *out = str;
- return 1;
- }
- } while (tolower(*str++) == tolower(*prefix++));
- return 0;
-}
-
-/*
- * Like skip_prefix_mem, but compare case-insensitively. Note that the
- * comparison is done via tolower(), so it is strictly ASCII (no multi-byte
- * characters or locale-specific conversions).
- */
-static inline int skip_iprefix_mem(const char *buf, size_t len,
- const char *prefix,
- const char **out, size_t *outlen)
-{
- do {
- if (!*prefix) {
- *out = buf;
- *outlen = len;
- return 1;
- }
- } while (len-- > 0 && tolower(*buf++) == tolower(*prefix++));
- return 0;
-}
-
-static inline int strtoul_ui(char const *s, int base, unsigned int *result)
-{
- unsigned long ul;
- char *p;
-
- errno = 0;
- /* negative values would be accepted by strtoul */
- if (strchr(s, '-'))
- return -1;
- ul = strtoul(s, &p, base);
- if (errno || *p || p == s || (unsigned int) ul != ul)
- return -1;
- *result = ul;
- return 0;
-}
-
-static inline int strtol_i(char const *s, int base, int *result)
-{
- long ul;
- char *p;
-
- errno = 0;
- ul = strtol(s, &p, base);
- if (errno || *p || p == s || (int) ul != ul)
- return -1;
- *result = ul;
- return 0;
-}
-
void git_stable_qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
#ifdef INTERNAL_QSORT
@@ -1313,15 +897,6 @@ int git_qsort_s(void *base, size_t nmemb, size_t size,
#error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
#endif
-static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
- size_t nmatch, regmatch_t pmatch[], int eflags)
-{
- assert(nmatch > 0 && pmatch);
- pmatch[0].rm_so = 0;
- pmatch[0].rm_eo = size;
- return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
-}
-
#ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
int git_regcomp(regex_t *preg, const char *pattern, int cflags);
#define regcomp git_regcomp
@@ -1430,21 +1005,6 @@ int git_access(const char *path, int mode);
# endif
#endif
-/*
- * Our code often opens a path to an optional file, to work on its
- * contents when we can successfully open it. We can ignore a failure
- * to open if such an optional file does not exist, but we do want to
- * report a failure in opening for other reasons (e.g. we got an I/O
- * error, or the file is there, but we lack the permission to open).
- *
- * Call this function after seeing an error from open() or fopen() to
- * see if the errno indicates a missing file that we can safely ignore.
- */
-static inline int is_missing_file_error(int errno_)
-{
- return (errno_ == ENOENT || errno_ == ENOTDIR);
-}
-
int cmd_main(int, const char **);
/*
@@ -1486,37 +1046,14 @@ int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
uLong *sourceLen);
#endif
+#include "common.h"
+
/*
* This include must come after system headers, since it introduces macros that
* replace system names.
*/
#include "banned.h"
-/*
- * container_of - Get the address of an object containing a field.
- *
- * @ptr: pointer to the field.
- * @type: type of the object.
- * @member: name of the field within the object.
- */
-#define container_of(ptr, type, member) \
- ((type *) ((char *)(ptr) - offsetof(type, member)))
-
-/*
- * helper function for `container_of_or_null' to avoid multiple
- * evaluation of @ptr
- */
-static inline void *container_of_or_null_offset(void *ptr, size_t offset)
-{
- return ptr ? (char *)ptr - offset : NULL;
-}
-
-/*
- * like `container_of', but allows returned value to be NULL
- */
-#define container_of_or_null(ptr, type, member) \
- (type *)container_of_or_null_offset(ptr, offsetof(type, member))
-
/*
* like offsetof(), but takes a pointer to a variable of type which
* contains @member, instead of a specified type.
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH 4/6] usage.h: move declarations for usage.c functions from git-compat-util.h
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
` (2 preceding siblings ...)
2023-05-16 17:09 ` [PATCH 3/6] common.h: move non-compat specific macros and " Calvin Wan
@ 2023-05-16 17:09 ` Calvin Wan
2023-05-16 17:09 ` [PATCH 5/6] treewide: remove unnecessary includes for wrapper.h Calvin Wan
` (3 subsequent siblings)
7 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 17:09 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
common.h | 1 +
git-compat-util.h | 54 ------------------------------------------
usage.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 54 deletions(-)
create mode 100644 usage.h
diff --git a/common.h b/common.h
index c0fdc5eb98..4aa99bc98b 100644
--- a/common.h
+++ b/common.h
@@ -3,6 +3,7 @@
#include "git-compat-util.h"
#include "wrapper.h"
+#include "usage.h"
/*
* ARRAY_SIZE - get the number of elements in a visible array
diff --git a/git-compat-util.h b/git-compat-util.h
index c2b92a3033..bc543baa66 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -573,19 +573,6 @@ static inline int git_has_dir_sep(const char *path)
#include "compat/bswap.h"
#include "wildmatch.h"
-#include "wrapper.h"
-
-/* General helper functions */
-NORETURN void usage(const char *err);
-NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
-NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
-NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int die_message(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
-void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
#ifndef NO_OPENSSL
#ifdef APPLE_COMMON_CRYPTO
@@ -601,30 +588,6 @@ void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
#include <openssl/rand.h>
#endif
-/*
- * Let callers be aware of the constant return value; this can help
- * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
- * because other compilers may be confused by this.
- */
-#if defined(__GNUC__)
-static inline int const_error(void)
-{
- return -1;
-}
-#define error(...) (error(__VA_ARGS__), const_error())
-#define error_errno(...) (error_errno(__VA_ARGS__), const_error())
-#endif
-
-typedef void (*report_fn)(const char *, va_list params);
-
-void set_die_routine(NORETURN_PTR report_fn routine);
-report_fn get_die_message_routine(void);
-void set_error_routine(report_fn routine);
-report_fn get_error_routine(void);
-void set_warn_routine(report_fn routine);
-report_fn get_warn_routine(void);
-void set_die_is_recursing_routine(int (*routine)(void));
-
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
#ifndef PROT_READ
@@ -941,23 +904,6 @@ int git_regcomp(regex_t *preg, const char *pattern, int cflags);
#endif
#endif
-/* usage.c: only to be used for testing BUG() implementation (see test-tool) */
-extern int BUG_exit_code;
-
-/* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */
-extern int bug_called_must_BUG;
-
-__attribute__((format (printf, 3, 4))) NORETURN
-void BUG_fl(const char *file, int line, const char *fmt, ...);
-#define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
-__attribute__((format (printf, 3, 4)))
-void bug_fl(const char *file, int line, const char *fmt, ...);
-#define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__)
-#define BUG_if_bug(...) do { \
- if (bug_called_must_BUG) \
- BUG_fl(__FILE__, __LINE__, __VA_ARGS__); \
-} while (0)
-
#ifndef FSYNC_METHOD_DEFAULT
#ifdef __APPLE__
#define FSYNC_METHOD_DEFAULT FSYNC_METHOD_WRITEOUT_ONLY
diff --git a/usage.h b/usage.h
new file mode 100644
index 0000000000..770076e684
--- /dev/null
+++ b/usage.h
@@ -0,0 +1,60 @@
+#ifndef USAGE_H
+#define USAGE_H
+
+#include "git-compat-util.h"
+
+/* General helper functions */
+NORETURN void usage(const char *err);
+NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
+NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
+NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int die_message(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+
+void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
+void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+
+/*
+ * Let callers be aware of the constant return value; this can help
+ * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
+ * because other compilers may be confused by this.
+ */
+#if defined(__GNUC__)
+static inline int const_error(void)
+{
+ return -1;
+}
+#define error(...) (error(__VA_ARGS__), const_error())
+#define error_errno(...) (error_errno(__VA_ARGS__), const_error())
+#endif
+
+typedef void (*report_fn)(const char *, va_list params);
+
+void set_die_routine(NORETURN_PTR report_fn routine);
+report_fn get_die_message_routine(void);
+void set_error_routine(report_fn routine);
+report_fn get_error_routine(void);
+void set_warn_routine(report_fn routine);
+report_fn get_warn_routine(void);
+void set_die_is_recursing_routine(int (*routine)(void));
+
+/* usage.c: only to be used for testing BUG() implementation (see test-tool) */
+extern int BUG_exit_code;
+
+/* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */
+extern int bug_called_must_BUG;
+
+__attribute__((format (printf, 3, 4))) NORETURN
+void BUG_fl(const char *file, int line, const char *fmt, ...);
+#define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
+__attribute__((format (printf, 3, 4)))
+void bug_fl(const char *file, int line, const char *fmt, ...);
+#define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__)
+#define BUG_if_bug(...) do { \
+ if (bug_called_must_BUG) \
+ BUG_fl(__FILE__, __LINE__, __VA_ARGS__); \
+} while (0)
+
+#endif /* USAGE_H */
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH 5/6] treewide: remove unnecessary includes for wrapper.h
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
` (3 preceding siblings ...)
2023-05-16 17:09 ` [PATCH 4/6] usage.h: move declarations for usage.c " Calvin Wan
@ 2023-05-16 17:09 ` Calvin Wan
2023-05-16 17:09 ` [PATCH 6/6] common: move alloc macros to common.h Calvin Wan
` (2 subsequent siblings)
7 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 17:09 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
apply.c | 1 -
builtin/am.c | 1 -
builtin/bisect.c | 1 -
builtin/branch.c | 1 -
builtin/bugreport.c | 1 -
builtin/clone.c | 1 -
builtin/config.c | 1 -
builtin/credential-cache.c | 1 -
builtin/difftool.c | 1 -
builtin/fast-import.c | 1 -
builtin/fmt-merge-msg.c | 1 -
builtin/gc.c | 1 -
builtin/get-tar-commit-id.c | 1 -
builtin/index-pack.c | 1 -
builtin/init-db.c | 1 -
builtin/merge.c | 1 -
builtin/pack-objects.c | 1 -
builtin/rebase.c | 1 -
builtin/receive-pack.c | 1 -
builtin/rerere.c | 1 -
builtin/unpack-file.c | 1 -
builtin/worktree.c | 1 -
bulk-checkin.c | 1 -
combine-diff.c | 1 -
commit-graph.c | 1 -
compat/terminal.c | 1 -
config.c | 1 -
convert.c | 1 -
copy.c | 1 -
csum-file.c | 1 -
daemon.c | 1 -
diff.c | 1 -
dir.c | 1 -
editor.c | 1 -
entry.c | 1 -
environment.c | 1 -
fetch-pack.c | 1 -
gpg-interface.c | 1 -
grep.c | 1 -
http-backend.c | 1 -
imap-send.c | 1 -
ll-merge.c | 1 -
merge-recursive.c | 1 -
notes-merge.c | 1 -
object-file.c | 1 -
pack-write.c | 1 -
packfile.c | 1 -
parallel-checkout.c | 1 -
path.c | 1 -
pkt-line.c | 1 -
read-cache.c | 1 -
rebase-interactive.c | 1 -
refs.c | 1 -
rerere.c | 1 -
send-pack.c | 1 -
sequencer.c | 1 -
server-info.c | 1 -
setup.c | 1 -
shallow.c | 1 -
strbuf.c | 1 -
streaming.c | 1 -
t/helper/test-delta.c | 1 -
t/helper/test-fsmonitor-client.c | 1 -
t/helper/test-read-cache.c | 1 -
tag.c | 1 -
tempfile.c | 1 -
trace.c | 1 -
transport-helper.c | 1 -
transport.c | 1 -
upload-pack.c | 1 -
usage.c | 1 -
worktree.c | 1 -
wrapper.c | 1 -
write-or-die.c | 1 -
74 files changed, 74 deletions(-)
diff --git a/apply.c b/apply.c
index 3636bc14c2..ef6020b18f 100644
--- a/apply.c
+++ b/apply.c
@@ -30,7 +30,6 @@
#include "apply.h"
#include "entry.h"
#include "setup.h"
-#include "wrapper.h"
struct gitdiff_data {
struct strbuf *root;
diff --git a/builtin/am.c b/builtin/am.c
index 5c83f2e003..5689ed44c4 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -43,7 +43,6 @@
#include "pager.h"
#include "repository.h"
#include "pretty.h"
-#include "wrapper.h"
/**
* Returns the length of the first line of msg.
diff --git a/builtin/bisect.c b/builtin/bisect.c
index 4b2143d455..dc3bd0c3a0 100644
--- a/builtin/bisect.c
+++ b/builtin/bisect.c
@@ -14,7 +14,6 @@
#include "prompt.h"
#include "quote.h"
#include "revision.h"
-#include "wrapper.h"
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
diff --git a/builtin/branch.c b/builtin/branch.c
index 501c47657c..728931f011 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -28,7 +28,6 @@
#include "worktree.h"
#include "help.h"
#include "commit-reach.h"
-#include "wrapper.h"
static const char * const builtin_branch_usage[] = {
N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"),
diff --git a/builtin/bugreport.c b/builtin/bugreport.c
index daf6c23657..d2ae5c305d 100644
--- a/builtin/bugreport.c
+++ b/builtin/bugreport.c
@@ -11,7 +11,6 @@
#include "diagnose.h"
#include "object-file.h"
#include "setup.h"
-#include "wrapper.h"
static void get_system_info(struct strbuf *sys_info)
{
diff --git a/builtin/clone.c b/builtin/clone.c
index 186845ef0b..c632ad4a9a 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -42,7 +42,6 @@
#include "hook.h"
#include "bundle.h"
#include "bundle-uri.h"
-#include "wrapper.h"
/*
* Overall FIXMEs:
diff --git a/builtin/config.c b/builtin/config.c
index 9401f1e5e3..71bb376fb3 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -12,7 +12,6 @@
#include "quote.h"
#include "setup.h"
#include "worktree.h"
-#include "wrapper.h"
static const char *const builtin_config_usage[] = {
N_("git config [<options>]"),
diff --git a/builtin/credential-cache.c b/builtin/credential-cache.c
index 508da4c6e4..c84920a3c1 100644
--- a/builtin/credential-cache.c
+++ b/builtin/credential-cache.c
@@ -1,7 +1,6 @@
#include "builtin.h"
#include "gettext.h"
#include "parse-options.h"
-#include "wrapper.h"
#include "write-or-die.h"
#ifndef NO_UNIX_SOCKETS
diff --git a/builtin/difftool.c b/builtin/difftool.c
index f09d24d37f..785105c61c 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -30,7 +30,6 @@
#include "dir.h"
#include "entry.h"
#include "setup.h"
-#include "wrapper.h"
static int trust_exit_code;
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index bbd9b2b3e7..7473f3a42e 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -26,7 +26,6 @@
#include "commit-reach.h"
#include "khash.h"
#include "date.h"
-#include "wrapper.h"
#define PACK_ID_BITS 16
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index cc81241642..0f9855b680 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -3,7 +3,6 @@
#include "fmt-merge-msg.h"
#include "gettext.h"
#include "parse-options.h"
-#include "wrapper.h"
static const char * const fmt_merge_msg_usage[] = {
N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
diff --git a/builtin/gc.c b/builtin/gc.c
index c9f8557335..15b60273fc 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -39,7 +39,6 @@
#include "hook.h"
#include "setup.h"
#include "trace2.h"
-#include "wrapper.h"
#define FAILED_RUN "failed to run %s"
diff --git a/builtin/get-tar-commit-id.c b/builtin/get-tar-commit-id.c
index 564cfcac4f..a458a925f1 100644
--- a/builtin/get-tar-commit-id.c
+++ b/builtin/get-tar-commit-id.c
@@ -6,7 +6,6 @@
#include "tar.h"
#include "builtin.h"
#include "quote.h"
-#include "wrapper.h"
static const char builtin_get_tar_commit_id_usage[] =
"git get-tar-commit-id";
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index bb67e16655..1e065e9ce3 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -24,7 +24,6 @@
#include "replace-object.h"
#include "promisor-remote.h"
#include "setup.h"
-#include "wrapper.h"
static const char index_pack_usage[] =
"git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--[no-]rev-index] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 6183f3fb3f..81da7fcd75 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -15,7 +15,6 @@
#include "parse-options.h"
#include "setup.h"
#include "worktree.h"
-#include "wrapper.h"
#ifndef DEFAULT_GIT_TEMPLATE_DIR
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
diff --git a/builtin/merge.c b/builtin/merge.c
index 8da3e46abb..b800d4e189 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -52,7 +52,6 @@
#include "commit-reach.h"
#include "wt-status.h"
#include "commit-graph.h"
-#include "wrapper.h"
#define DEFAULT_TWOHEAD (1<<0)
#define DEFAULT_OCTOPUS (1<<1)
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index a5b466839b..79f3a2f300 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -43,7 +43,6 @@
#include "promisor-remote.h"
#include "pack-mtimes.h"
#include "parse-options.h"
-#include "wrapper.h"
/*
* Objects we are going to pack are collected in the `to_pack` structure.
diff --git a/builtin/rebase.c b/builtin/rebase.c
index ace1d5e8d1..e28e24b4fd 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -36,7 +36,6 @@
#include "reset.h"
#include "trace2.h"
#include "hook.h"
-#include "wrapper.h"
static char const * const builtin_rebase_usage[] = {
N_("git rebase [-i] [options] [--exec <cmd>] "
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index d22180435c..a548424c93 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -38,7 +38,6 @@
#include "worktree.h"
#include "shallow.h"
#include "parse-options.h"
-#include "wrapper.h"
static const char * const receive_pack_usage[] = {
N_("git receive-pack <git-dir>"),
diff --git a/builtin/rerere.c b/builtin/rerere.c
index d4a03707b1..24c7875572 100644
--- a/builtin/rerere.c
+++ b/builtin/rerere.c
@@ -6,7 +6,6 @@
#include "parse-options.h"
#include "string-list.h"
#include "rerere.h"
-#include "wrapper.h"
#include "xdiff/xdiff.h"
#include "xdiff-interface.h"
#include "pathspec.h"
diff --git a/builtin/unpack-file.c b/builtin/unpack-file.c
index b35a4b9dfe..3b8b8da5f6 100644
--- a/builtin/unpack-file.c
+++ b/builtin/unpack-file.c
@@ -3,7 +3,6 @@
#include "hex.h"
#include "object-name.h"
#include "object-store.h"
-#include "wrapper.h"
static char *create_temp_file(struct object_id *oid)
{
diff --git a/builtin/worktree.c b/builtin/worktree.c
index a61bc32189..dce9106757 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -19,7 +19,6 @@
#include "submodule.h"
#include "utf8.h"
#include "worktree.h"
-#include "wrapper.h"
#include "quote.h"
#define BUILTIN_WORKTREE_ADD_USAGE \
diff --git a/bulk-checkin.c b/bulk-checkin.c
index d843279715..9192298db6 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -17,7 +17,6 @@
#include "packfile.h"
#include "object-file.h"
#include "object-store.h"
-#include "wrapper.h"
static int odb_transaction_nesting;
diff --git a/combine-diff.c b/combine-diff.c
index f7e9fb5747..cd5f39f22f 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -16,7 +16,6 @@
#include "userdiff.h"
#include "oid-array.h"
#include "revision.h"
-#include "wrapper.h"
static int compare_paths(const struct combine_diff_path *one,
const struct diff_filespec *two)
diff --git a/commit-graph.c b/commit-graph.c
index 43558b4d9b..b07e22ce66 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -24,7 +24,6 @@
#include "json-writer.h"
#include "trace2.h"
#include "chunk-format.h"
-#include "wrapper.h"
void git_test_write_commit_graph_or_die(void)
{
diff --git a/compat/terminal.c b/compat/terminal.c
index d87e321189..83d95e8656 100644
--- a/compat/terminal.c
+++ b/compat/terminal.c
@@ -6,7 +6,6 @@
#include "run-command.h"
#include "string-list.h"
#include "hashmap.h"
-#include "wrapper.h"
#if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
diff --git a/config.c b/config.c
index 43b0d3fb57..b7cccbe245 100644
--- a/config.c
+++ b/config.c
@@ -35,7 +35,6 @@
#include "setup.h"
#include "trace2.h"
#include "worktree.h"
-#include "wrapper.h"
#include "write-or-die.h"
struct config_source {
diff --git a/convert.c b/convert.c
index 5a2ea5308d..93e9ebe6d8 100644
--- a/convert.c
+++ b/convert.c
@@ -14,7 +14,6 @@
#include "trace.h"
#include "utf8.h"
#include "ll-merge.h"
-#include "wrapper.h"
/*
* convert.c - convert a file when checking it out and checking it in.
diff --git a/copy.c b/copy.c
index c3250f0822..4de6a110f0 100644
--- a/copy.c
+++ b/copy.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "wrapper.h"
int copy_fd(int ifd, int ofd)
{
diff --git a/csum-file.c b/csum-file.c
index 82ae2973d3..ddfc685d52 100644
--- a/csum-file.c
+++ b/csum-file.c
@@ -10,7 +10,6 @@
#include "git-compat-util.h"
#include "progress.h"
#include "csum-file.h"
-#include "wrapper.h"
static void verify_buffer_or_die(struct hashfile *f,
const void *buf,
diff --git a/daemon.c b/daemon.c
index 75c3c06457..4047b0cf27 100644
--- a/daemon.c
+++ b/daemon.c
@@ -9,7 +9,6 @@
#include "setup.h"
#include "strbuf.h"
#include "string-list.h"
-#include "wrapper.h"
#ifdef NO_INITGROUPS
#define initgroups(x, y) (0) /* nothing */
diff --git a/diff.c b/diff.c
index 067846b13c..4033358cf4 100644
--- a/diff.c
+++ b/diff.c
@@ -40,7 +40,6 @@
#include "object-name.h"
#include "setup.h"
#include "strmap.h"
-#include "wrapper.h"
#ifdef NO_FAST_WORKING_DIRECTORY
#define FAST_WORKING_DIRECTORY 0
diff --git a/dir.c b/dir.c
index aa840995c4..f235df015e 100644
--- a/dir.c
+++ b/dir.c
@@ -26,7 +26,6 @@
#include "setup.h"
#include "submodule-config.h"
#include "trace2.h"
-#include "wrapper.h"
/*
* Tells read_directory_recursive how a file or directory should be treated.
diff --git a/editor.c b/editor.c
index b34e10606d..d6c00f2b6d 100644
--- a/editor.c
+++ b/editor.c
@@ -10,7 +10,6 @@
#include "strvec.h"
#include "run-command.h"
#include "sigchain.h"
-#include "wrapper.h"
#ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "vi"
diff --git a/entry.c b/entry.c
index d89e61fa64..eaee78a526 100644
--- a/entry.c
+++ b/entry.c
@@ -11,7 +11,6 @@
#include "fsmonitor.h"
#include "entry.h"
#include "parallel-checkout.h"
-#include "wrapper.h"
static void create_directories(const char *path, int path_len,
const struct checkout *state)
diff --git a/environment.c b/environment.c
index 8a96997539..330277ff43 100644
--- a/environment.c
+++ b/environment.c
@@ -27,7 +27,6 @@
#include "setup.h"
#include "shallow.h"
#include "trace.h"
-#include "wrapper.h"
#include "write-or-die.h"
int trust_executable_bit = 1;
diff --git a/fetch-pack.c b/fetch-pack.c
index 6fa6e8af9a..5e3e875670 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -32,7 +32,6 @@
#include "commit-graph.h"
#include "sigchain.h"
#include "mergesort.h"
-#include "wrapper.h"
static int transfer_unpack_limit = -1;
static int fetch_unpack_limit = -1;
diff --git a/gpg-interface.c b/gpg-interface.c
index f3ac5acdd9..75ce234ddf 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -10,7 +10,6 @@
#include "sigchain.h"
#include "tempfile.h"
#include "alias.h"
-#include "wrapper.h"
static int git_gpg_config(const char *, const char *, void *);
diff --git a/grep.c b/grep.c
index 073559f2cd..b86462a12a 100644
--- a/grep.c
+++ b/grep.c
@@ -11,7 +11,6 @@
#include "commit.h"
#include "quote.h"
#include "help.h"
-#include "wrapper.h"
static int grep_source_load(struct grep_source *gs);
static int grep_source_is_binary(struct grep_source *gs,
diff --git a/http-backend.c b/http-backend.c
index ac146d85c5..4491cdee39 100644
--- a/http-backend.c
+++ b/http-backend.c
@@ -18,7 +18,6 @@
#include "object-store.h"
#include "protocol.h"
#include "date.h"
-#include "wrapper.h"
#include "write-or-die.h"
static const char content_type[] = "Content-Type";
diff --git a/imap-send.c b/imap-send.c
index a62424e90a..ad5474643c 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -29,7 +29,6 @@
#include "run-command.h"
#include "parse-options.h"
#include "setup.h"
-#include "wrapper.h"
#if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
typedef void *SSL;
#endif
diff --git a/ll-merge.c b/ll-merge.c
index 28bc94c45d..f0763f7016 100644
--- a/ll-merge.c
+++ b/ll-merge.c
@@ -12,7 +12,6 @@
#include "run-command.h"
#include "ll-merge.h"
#include "quote.h"
-#include "wrapper.h"
struct ll_merge_driver;
diff --git a/merge-recursive.c b/merge-recursive.c
index 9875bdb11c..8bf84c9339 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -33,7 +33,6 @@
#include "tag.h"
#include "tree-walk.h"
#include "unpack-trees.h"
-#include "wrapper.h"
#include "xdiff-interface.h"
struct merge_options_internal {
diff --git a/notes-merge.c b/notes-merge.c
index 233e49e319..434090634e 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -19,7 +19,6 @@
#include "trace.h"
#include "notes-utils.h"
#include "commit-reach.h"
-#include "wrapper.h"
struct notes_merge_pair {
struct object_id obj, base, local, remote;
diff --git a/object-file.c b/object-file.c
index af18e38527..43c1585aa7 100644
--- a/object-file.c
+++ b/object-file.c
@@ -42,7 +42,6 @@
#include "setup.h"
#include "submodule.h"
#include "fsck.h"
-#include "wrapper.h"
/* The maximum size for an object header. */
#define MAX_HEADER_LEN 32
diff --git a/pack-write.c b/pack-write.c
index 3b3ce89de6..7dd8882830 100644
--- a/pack-write.c
+++ b/pack-write.c
@@ -10,7 +10,6 @@
#include "oidmap.h"
#include "pack-objects.h"
#include "pack-revindex.h"
-#include "wrapper.h"
void reset_pack_idx_option(struct pack_idx_option *opts)
{
diff --git a/packfile.c b/packfile.c
index 57df0c1956..96727e63a8 100644
--- a/packfile.c
+++ b/packfile.c
@@ -24,7 +24,6 @@
#include "commit-graph.h"
#include "pack-revindex.h"
#include "promisor-remote.h"
-#include "wrapper.h"
char *odb_pack_name(struct strbuf *buf,
const unsigned char *hash,
diff --git a/parallel-checkout.c b/parallel-checkout.c
index 50fd7fe31e..38c4dc665d 100644
--- a/parallel-checkout.c
+++ b/parallel-checkout.c
@@ -12,7 +12,6 @@
#include "streaming.h"
#include "thread-utils.h"
#include "trace2.h"
-#include "wrapper.h"
struct pc_worker {
struct child_process cp;
diff --git a/path.c b/path.c
index 7c1cd8182a..dff215ac69 100644
--- a/path.c
+++ b/path.c
@@ -18,7 +18,6 @@
#include "object-store.h"
#include "lockfile.h"
#include "exec-cmd.h"
-#include "wrapper.h"
static int get_st_mode_bits(const char *path, int *mode)
{
diff --git a/pkt-line.c b/pkt-line.c
index 3561d85358..9cf6f12c4e 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -4,7 +4,6 @@
#include "hex.h"
#include "run-command.h"
#include "trace.h"
-#include "wrapper.h"
#include "write-or-die.h"
char packet_buffer[LARGE_PACKET_MAX];
diff --git a/read-cache.c b/read-cache.c
index e919af3c77..7fa89c684d 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -38,7 +38,6 @@
#include "csum-file.h"
#include "promisor-remote.h"
#include "hook.h"
-#include "wrapper.h"
/* Mask for the name length in ce_flags in the on-disk index */
diff --git a/rebase-interactive.c b/rebase-interactive.c
index 789f407361..8c52d2170a 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -10,7 +10,6 @@
#include "config.h"
#include "dir.h"
#include "object-name.h"
-#include "wrapper.h"
static const char edit_todo_list_advice[] =
N_("You can fix this with 'git rebase --edit-todo' "
diff --git a/refs.c b/refs.c
index d2a98e1c21..379217b03e 100644
--- a/refs.c
+++ b/refs.c
@@ -28,7 +28,6 @@
#include "sigchain.h"
#include "date.h"
#include "commit.h"
-#include "wrapper.h"
/*
* List of all available backends
diff --git a/rerere.c b/rerere.c
index 7abc94bf44..7ed2493891 100644
--- a/rerere.c
+++ b/rerere.c
@@ -17,7 +17,6 @@
#include "object-store.h"
#include "hash-lookup.h"
#include "strmap.h"
-#include "wrapper.h"
#define RESOLVED 0
#define PUNTED 1
diff --git a/send-pack.c b/send-pack.c
index 2089143555..97344b629e 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -14,7 +14,6 @@
#include "quote.h"
#include "transport.h"
#include "version.h"
-#include "wrapper.h"
#include "oid-array.h"
#include "gpg-interface.h"
#include "shallow.h"
diff --git a/sequencer.c b/sequencer.c
index c88d1d9553..e773b4999b 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -45,7 +45,6 @@
#include "rebase-interactive.h"
#include "reset.h"
#include "branch.h"
-#include "wrapper.h"
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
diff --git a/server-info.c b/server-info.c
index 68098ddd1a..67ce4ad529 100644
--- a/server-info.c
+++ b/server-info.c
@@ -12,7 +12,6 @@
#include "object-file.h"
#include "object-store.h"
#include "strbuf.h"
-#include "wrapper.h"
struct update_info_ctx {
FILE *cur_fp;
diff --git a/setup.c b/setup.c
index 59abc16ba6..a546da755f 100644
--- a/setup.c
+++ b/setup.c
@@ -12,7 +12,6 @@
#include "promisor-remote.h"
#include "quote.h"
#include "trace2.h"
-#include "wrapper.h"
static int inside_git_dir = -1;
static int inside_work_tree = -1;
diff --git a/shallow.c b/shallow.c
index 128f56179e..9ff8923ccf 100644
--- a/shallow.c
+++ b/shallow.c
@@ -18,7 +18,6 @@
#include "commit-reach.h"
#include "shallow.h"
#include "trace.h"
-#include "wrapper.h"
void set_alternate_shallow_file(struct repository *r, const char *path, int override)
{
diff --git a/strbuf.c b/strbuf.c
index 729378ec82..d486189303 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -9,7 +9,6 @@
#include "string-list.h"
#include "utf8.h"
#include "date.h"
-#include "wrapper.h"
int starts_with(const char *str, const char *prefix)
{
diff --git a/streaming.c b/streaming.c
index 21e39585e8..83b1530419 100644
--- a/streaming.c
+++ b/streaming.c
@@ -10,7 +10,6 @@
#include "object-store.h"
#include "replace-object.h"
#include "packfile.h"
-#include "wrapper.h"
typedef int (*open_istream_fn)(struct git_istream *,
struct repository *,
diff --git a/t/helper/test-delta.c b/t/helper/test-delta.c
index e7d134ec25..6bc787a474 100644
--- a/t/helper/test-delta.c
+++ b/t/helper/test-delta.c
@@ -11,7 +11,6 @@
#include "test-tool.h"
#include "git-compat-util.h"
#include "delta.h"
-#include "wrapper.h"
static const char usage_str[] =
"test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
diff --git a/t/helper/test-fsmonitor-client.c b/t/helper/test-fsmonitor-client.c
index 14522b4c47..a3427e048b 100644
--- a/t/helper/test-fsmonitor-client.c
+++ b/t/helper/test-fsmonitor-client.c
@@ -10,7 +10,6 @@
#include "setup.h"
#include "thread-utils.h"
#include "trace2.h"
-#include "wrapper.h"
#ifndef HAVE_FSMONITOR_DAEMON_BACKEND
int cmd__fsmonitor_client(int argc UNUSED, const char **argv UNUSED)
diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c
index a4c24d0e42..e4c3e8f01c 100644
--- a/t/helper/test-read-cache.c
+++ b/t/helper/test-read-cache.c
@@ -3,7 +3,6 @@
#include "cache.h"
#include "config.h"
#include "setup.h"
-#include "wrapper.h"
int cmd__read_cache(int argc, const char **argv)
{
diff --git a/tag.c b/tag.c
index 96dbd5b2d5..fb0eaa5595 100644
--- a/tag.c
+++ b/tag.c
@@ -10,7 +10,6 @@
#include "gpg-interface.h"
#include "hex.h"
#include "packfile.h"
-#include "wrapper.h"
const char *tag_type = "tag";
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..423266e4c7 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -46,7 +46,6 @@
#include "path.h"
#include "tempfile.h"
#include "sigchain.h"
-#include "wrapper.h"
static VOLATILE_LIST_HEAD(tempfile_list);
diff --git a/trace.c b/trace.c
index 592c141d78..971a68abe8 100644
--- a/trace.c
+++ b/trace.c
@@ -27,7 +27,6 @@
#include "quote.h"
#include "setup.h"
#include "trace.h"
-#include "wrapper.h"
struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
diff --git a/transport-helper.c b/transport-helper.c
index 6b816940dc..28a0309e06 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -18,7 +18,6 @@
#include "refspec.h"
#include "transport-internal.h"
#include "protocol.h"
-#include "wrapper.h"
static int debug;
diff --git a/transport.c b/transport.c
index 67afdae57c..cd4b7f5528 100644
--- a/transport.c
+++ b/transport.c
@@ -30,7 +30,6 @@
#include "object-store.h"
#include "color.h"
#include "bundle-uri.h"
-#include "wrapper.h"
static int transport_use_color = -1;
static char transport_colors[][COLOR_MAXLEN] = {
diff --git a/upload-pack.c b/upload-pack.c
index 08633dc121..7990a2037c 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -32,7 +32,6 @@
#include "commit-graph.h"
#include "commit-reach.h"
#include "shallow.h"
-#include "wrapper.h"
#include "write-or-die.h"
/* Remember to update object flag allocation in object.h */
diff --git a/usage.c b/usage.c
index 46d99f8bd4..09f0ed509b 100644
--- a/usage.c
+++ b/usage.c
@@ -6,7 +6,6 @@
#include "git-compat-util.h"
#include "gettext.h"
#include "trace2.h"
-#include "wrapper.h"
static void vreportf(const char *prefix, const char *err, va_list params)
{
diff --git a/worktree.c b/worktree.c
index b5ee71c5eb..7afeaf49fe 100644
--- a/worktree.c
+++ b/worktree.c
@@ -11,7 +11,6 @@
#include "dir.h"
#include "wt-status.h"
#include "config.h"
-#include "wrapper.h"
void free_worktrees(struct worktree **worktrees)
{
diff --git a/wrapper.c b/wrapper.c
index c130d7518b..98321531ec 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -6,7 +6,6 @@
#include "config.h"
#include "gettext.h"
#include "trace2.h"
-#include "wrapper.h"
static intmax_t count_fsync_writeout_only;
static intmax_t count_fsync_hardware_flush;
diff --git a/write-or-die.c b/write-or-die.c
index cc9e0787a1..d8355c0c3e 100644
--- a/write-or-die.c
+++ b/write-or-die.c
@@ -1,7 +1,6 @@
#include "git-compat-util.h"
#include "config.h"
#include "run-command.h"
-#include "wrapper.h"
#include "write-or-die.h"
/*
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH 6/6] common: move alloc macros to common.h
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
` (4 preceding siblings ...)
2023-05-16 17:09 ` [PATCH 5/6] treewide: remove unnecessary includes for wrapper.h Calvin Wan
@ 2023-05-16 17:09 ` Calvin Wan
2023-05-16 17:54 ` [PATCH 0/6] git-compat-util cleanups Junio C Hamano
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
7 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 17:09 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
alloc_nr, ALLOC_GROW, and ALLOC_GROW_BY are commonly used macros for
dynamic array allocation. Moving these macros to common.h focuses
alloc.[ch] to allocation for Git objects and additionally allows us to
remove inclusions to alloc.h from files that solely used the above
macros.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
add-patch.c | 1 -
alias.c | 1 -
alloc.h | 75 ------------------------------
apply.c | 1 -
archive-tar.c | 1 -
archive.c | 1 -
attr.c | 1 -
builtin/blame.c | 1 -
builtin/cat-file.c | 1 -
builtin/checkout--worker.c | 1 -
builtin/config.c | 1 -
builtin/credential-cache--daemon.c | 1 -
builtin/fetch-pack.c | 1 -
builtin/fsmonitor--daemon.c | 1 -
builtin/grep.c | 1 -
builtin/index-pack.c | 1 -
builtin/log.c | 1 -
builtin/merge.c | 1 -
builtin/mktree.c | 1 -
builtin/mv.c | 1 -
builtin/name-rev.c | 1 -
builtin/pack-objects.c | 1 -
builtin/repack.c | 1 -
builtin/rev-parse.c | 1 -
builtin/revert.c | 1 -
builtin/rm.c | 1 -
builtin/submodule--helper.c | 1 -
bulk-checkin.c | 1 -
cache-tree.c | 1 -
chunk-format.c | 1 -
commit-reach.c | 1 -
common.h | 75 ++++++++++++++++++++++++++++++
config.c | 1 -
daemon.c | 1 -
delta-islands.c | 1 -
diff.c | 1 -
diffcore-rename.c | 1 -
dir-iterator.c | 1 -
dir.c | 1 -
ewah/bitmap.c | 1 -
ewah/ewah_bitmap.c | 1 -
fetch-pack.c | 1 -
fmt-merge-msg.c | 1 -
fsck.c | 1 -
help.c | 1 -
http-backend.c | 1 -
line-log.c | 1 -
list-objects-filter-options.c | 1 -
list-objects-filter.c | 1 -
midx.c | 1 -
object-file.c | 1 -
oid-array.c | 1 -
oidtree.c | 1 -
pack-bitmap-write.c | 1 -
pack-bitmap.c | 1 -
pack-objects.c | 1 -
packfile.c | 1 -
parallel-checkout.c | 1 -
pretty.c | 1 -
prio-queue.c | 1 -
quote.c | 1 -
read-cache.c | 1 -
ref-filter.c | 1 -
reflog-walk.c | 1 -
refs.c | 1 -
refspec.c | 1 -
remote-curl.c | 1 -
remote.c | 1 -
rerere.c | 1 -
revision.c | 1 -
sequencer.c | 1 -
server-info.c | 1 -
shallow.c | 1 -
sigchain.c | 1 -
sparse-index.c | 1 -
split-index.c | 1 -
strbuf.c | 1 -
string-list.c | 1 -
strvec.c | 1 -
submodule-config.c | 1 -
submodule.c | 1 -
t/helper/test-reach.c | 1 -
trace2/tr2_tls.c | 1 -
trailer.c | 1 -
transport.c | 1 -
tree-walk.c | 1 -
userdiff.c | 1 -
worktree.c | 1 -
88 files changed, 75 insertions(+), 161 deletions(-)
diff --git a/add-patch.c b/add-patch.c
index 8d770d203f..b153d4a37d 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1,7 +1,6 @@
#include "cache.h"
#include "add-interactive.h"
#include "advice.h"
-#include "alloc.h"
#include "editor.h"
#include "environment.h"
#include "gettext.h"
diff --git a/alias.c b/alias.c
index 54a1a23d2c..38e1339936 100644
--- a/alias.c
+++ b/alias.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "alias.h"
-#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "strbuf.h"
diff --git a/alloc.h b/alloc.h
index 4312db4bd0..3f4a0ad310 100644
--- a/alloc.h
+++ b/alloc.h
@@ -17,79 +17,4 @@ 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
diff --git a/apply.c b/apply.c
index ef6020b18f..f06eaffa09 100644
--- a/apply.c
+++ b/apply.c
@@ -9,7 +9,6 @@
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "object-store.h"
#include "blob.h"
diff --git a/archive-tar.c b/archive-tar.c
index 4cd81d8161..8d41c4c581 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -2,7 +2,6 @@
* Copyright (c) 2005, 2006 Rene Scharfe
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "git-zlib.h"
diff --git a/archive.c b/archive.c
index 8570cf37ff..835f9765f1 100644
--- a/archive.c
+++ b/archive.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "environment.h"
diff --git a/attr.c b/attr.c
index 2d8aeb8b58..56d7c89392 100644
--- a/attr.c
+++ b/attr.c
@@ -7,7 +7,6 @@
*/
#include "cache.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "exec-cmd.h"
diff --git a/builtin/blame.c b/builtin/blame.c
index 2df6039a6e..421a2ed4ca 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -6,7 +6,6 @@
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "color.h"
#include "builtin.h"
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 0bafc14e6c..83178a24b3 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -5,7 +5,6 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "builtin.h"
diff --git a/builtin/checkout--worker.c b/builtin/checkout--worker.c
index 2120dd1d30..bad7c29eb3 100644
--- a/builtin/checkout--worker.c
+++ b/builtin/checkout--worker.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "gettext.h"
diff --git a/builtin/config.c b/builtin/config.c
index 71bb376fb3..04e2bb0dc7 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "color.h"
#include "editor.h"
diff --git a/builtin/credential-cache--daemon.c b/builtin/credential-cache--daemon.c
index 4e571d9951..2122571827 100644
--- a/builtin/credential-cache--daemon.c
+++ b/builtin/credential-cache--daemon.c
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "gettext.h"
#include "object-file.h"
#include "parse-options.h"
diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
index 3ba0fe5a39..44c05ee86c 100644
--- a/builtin/fetch-pack.c
+++ b/builtin/fetch-pack.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "object-file.h"
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index f6dd9a784c..d4b666bf9c 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/builtin/grep.c b/builtin/grep.c
index b86c754def..3e7a485927 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -4,7 +4,6 @@
* Copyright (c) 2006 Junio C Hamano
*/
#include "cache.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "repository.h"
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 1e065e9ce3..c05d8a70e0 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "delta.h"
#include "environment.h"
diff --git a/builtin/log.c b/builtin/log.c
index 4f162ff4d0..6ac4081d4c 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -6,7 +6,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/builtin/merge.c b/builtin/merge.c
index b800d4e189..82b832fcf9 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -10,7 +10,6 @@
#include "cache.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "editor.h"
#include "environment.h"
diff --git a/builtin/mktree.c b/builtin/mktree.c
index 09a7bd5c5c..aa00c70feb 100644
--- a/builtin/mktree.c
+++ b/builtin/mktree.c
@@ -4,7 +4,6 @@
* Copyright (c) Junio C Hamano, 2006, 2009
*/
#include "builtin.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "quote.h"
diff --git a/builtin/mv.c b/builtin/mv.c
index 32935af48e..54f0df021b 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -7,7 +7,6 @@
#include "builtin.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 593f0506a1..dba4a3aa07 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 79f3a2f300..a82eb04fee 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/builtin/repack.c b/builtin/repack.c
index bb7bf60e7c..4e0943a543 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "dir.h"
#include "environment.h"
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 852e49e340..9a3783597f 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -6,7 +6,6 @@
#define USE_THE_INDEX_VARIABLE
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "commit.h"
#include "environment.h"
diff --git a/builtin/revert.c b/builtin/revert.c
index 0240ec8593..8247e6592e 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "parse-options.h"
diff --git a/builtin/rm.c b/builtin/rm.c
index d36072252e..1a1e409262 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -5,7 +5,6 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
-#include "alloc.h"
#include "advice.h"
#include "config.h"
#include "lockfile.h"
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 6bf8d666ce..2e478104ab 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1,7 +1,6 @@
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/bulk-checkin.c b/bulk-checkin.c
index 9192298db6..68a881a233 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -2,7 +2,6 @@
* Copyright (c) 2011, Google Inc.
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "bulk-checkin.h"
#include "environment.h"
#include "gettext.h"
diff --git a/cache-tree.c b/cache-tree.c
index ebfe649b33..2a2c5ad498 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "environment.h"
#include "hex.h"
#include "lockfile.h"
diff --git a/chunk-format.c b/chunk-format.c
index 60a73c1b14..0cb125eb38 100644
--- a/chunk-format.c
+++ b/chunk-format.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "chunk-format.h"
#include "csum-file.h"
#include "gettext.h"
diff --git a/commit-reach.c b/commit-reach.c
index 70bde8af05..812bc6bf92 100644
--- a/commit-reach.c
+++ b/commit-reach.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "commit-graph.h"
#include "decorate.h"
diff --git a/common.h b/common.h
index 4aa99bc98b..a4a985474d 100644
--- a/common.h
+++ b/common.h
@@ -199,6 +199,81 @@ static inline int cast_size_t_to_int(size_t a)
*/
#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
+#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)
+
#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
diff --git a/config.c b/config.c
index b7cccbe245..2672be0888 100644
--- a/config.c
+++ b/config.c
@@ -8,7 +8,6 @@
#include "cache.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "date.h"
#include "branch.h"
#include "config.h"
diff --git a/daemon.c b/daemon.c
index 4047b0cf27..2a9ea57a41 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1,6 +1,5 @@
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "pkt-line.h"
diff --git a/delta-islands.c b/delta-islands.c
index c824a5f6a4..332f0f7c45 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "attr.h"
#include "object.h"
#include "blob.h"
diff --git a/diff.c b/diff.c
index 4033358cf4..9c3d89dd07 100644
--- a/diff.c
+++ b/diff.c
@@ -3,7 +3,6 @@
*/
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "environment.h"
diff --git a/diffcore-rename.c b/diffcore-rename.c
index 8e2e7a3ad7..29979b4162 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -3,7 +3,6 @@
* Copyright (C) 2005 Junio C Hamano
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "diff.h"
#include "diffcore.h"
#include "object-store.h"
diff --git a/dir-iterator.c b/dir-iterator.c
index fb7c47f0e8..278b04243a 100644
--- a/dir-iterator.c
+++ b/dir-iterator.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "iterator.h"
#include "dir-iterator.h"
diff --git a/dir.c b/dir.c
index f235df015e..d2b1fa20e2 100644
--- a/dir.c
+++ b/dir.c
@@ -7,7 +7,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "dir.h"
diff --git a/ewah/bitmap.c b/ewah/bitmap.c
index 12d6aa398e..7b525b1ecd 100644
--- a/ewah/bitmap.c
+++ b/ewah/bitmap.c
@@ -17,7 +17,6 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "ewok.h"
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
diff --git a/ewah/ewah_bitmap.c b/ewah/ewah_bitmap.c
index c6d4ffc87c..8785cbc54a 100644
--- a/ewah/ewah_bitmap.c
+++ b/ewah/ewah_bitmap.c
@@ -17,7 +17,6 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "ewok.h"
#include "ewok_rlw.h"
diff --git a/fetch-pack.c b/fetch-pack.c
index 5e3e875670..48438c7d86 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "environment.h"
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index 5af0d4715b..9939bf2703 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "refs.h"
diff --git a/fsck.c b/fsck.c
index adbe8bf59e..3864af088e 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "hex.h"
#include "object-store.h"
#include "repository.h"
diff --git a/help.c b/help.c
index 5d7637dce9..c81d43a5fa 100644
--- a/help.c
+++ b/help.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "exec-cmd.h"
diff --git a/http-backend.c b/http-backend.c
index 4491cdee39..de5b72658e 100644
--- a/http-backend.c
+++ b/http-backend.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "git-zlib.h"
diff --git a/line-log.c b/line-log.c
index 10c19daec4..5cb82d731f 100644
--- a/line-log.c
+++ b/line-log.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "line-range.h"
#include "hex.h"
#include "tag.h"
diff --git a/list-objects-filter-options.c b/list-objects-filter-options.c
index 2a3b7881af..8a08b7af49 100644
--- a/list-objects-filter-options.c
+++ b/list-objects-filter-options.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "config.h"
#include "gettext.h"
diff --git a/list-objects-filter.c b/list-objects-filter.c
index 5d270ce598..2742e4a87d 100644
--- a/list-objects-filter.c
+++ b/list-objects-filter.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "gettext.h"
#include "hex.h"
diff --git a/midx.c b/midx.c
index 281a1406a8..59dd343275 100644
--- a/midx.c
+++ b/midx.c
@@ -1,6 +1,5 @@
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "csum-file.h"
#include "dir.h"
diff --git a/object-file.c b/object-file.c
index 43c1585aa7..f565070be3 100644
--- a/object-file.c
+++ b/object-file.c
@@ -8,7 +8,6 @@
*/
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "environment.h"
diff --git a/oid-array.c b/oid-array.c
index e8228c777b..8e4717746c 100644
--- a/oid-array.c
+++ b/oid-array.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "oid-array.h"
#include "hash-lookup.h"
diff --git a/oidtree.c b/oidtree.c
index 7d57b7b19e..daef175dc7 100644
--- a/oidtree.c
+++ b/oidtree.c
@@ -4,7 +4,6 @@
*/
#include "git-compat-util.h"
#include "oidtree.h"
-#include "alloc.h"
#include "hash.h"
struct oidtree_iter_data {
diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c
index faf67c94d3..335cc3c781 100644
--- a/pack-bitmap-write.c
+++ b/pack-bitmap-write.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/pack-bitmap.c b/pack-bitmap.c
index e0fad723bf..133c1a6556 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "gettext.h"
#include "hex.h"
diff --git a/pack-objects.c b/pack-objects.c
index ccab09fe65..1b8052bece 100644
--- a/pack-objects.c
+++ b/pack-objects.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "object.h"
#include "pack.h"
#include "pack-objects.h"
diff --git a/packfile.c b/packfile.c
index 96727e63a8..57d6b57107 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/parallel-checkout.c b/parallel-checkout.c
index 38c4dc665d..906e1ced61 100644
--- a/parallel-checkout.c
+++ b/parallel-checkout.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "gettext.h"
diff --git a/pretty.c b/pretty.c
index 0bb938021b..c162466ec8 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "commit.h"
#include "environment.h"
diff --git a/prio-queue.c b/prio-queue.c
index dc2476be53..450775a374 100644
--- a/prio-queue.c
+++ b/prio-queue.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "prio-queue.h"
static inline int compare(struct prio_queue *queue, int i, int j)
diff --git a/quote.c b/quote.c
index 43c739671e..3c05194496 100644
--- a/quote.c
+++ b/quote.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "path.h"
#include "quote.h"
#include "strbuf.h"
diff --git a/read-cache.c b/read-cache.c
index 7fa89c684d..ddcd613ff3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -4,7 +4,6 @@
* Copyright (C) Linus Torvalds, 2005
*/
#include "cache.h"
-#include "alloc.h"
#include "config.h"
#include "diff.h"
#include "diffcore.h"
diff --git a/ref-filter.c b/ref-filter.c
index 10aab14f03..2961cd6b20 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/reflog-walk.c b/reflog-walk.c
index 4ba1a10c82..d8dd6e7cdf 100644
--- a/reflog-walk.c
+++ b/reflog-walk.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "refs.h"
#include "diff.h"
diff --git a/refs.c b/refs.c
index 379217b03e..75171b46b2 100644
--- a/refs.c
+++ b/refs.c
@@ -4,7 +4,6 @@
#include "git-compat-util.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "hashmap.h"
diff --git a/refspec.c b/refspec.c
index 7b5c305514..80119bb0e5 100644
--- a/refspec.c
+++ b/refspec.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "strvec.h"
diff --git a/remote-curl.c b/remote-curl.c
index acf7b2bb40..8a976a0253 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/remote.c b/remote.c
index 0764fca0db..cae782c86f 100644
--- a/remote.c
+++ b/remote.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/rerere.c b/rerere.c
index 7ed2493891..66a0d55ead 100644
--- a/rerere.c
+++ b/rerere.c
@@ -1,6 +1,5 @@
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "hex.h"
diff --git a/revision.c b/revision.c
index b33cc1d106..f0554d9352 100644
--- a/revision.c
+++ b/revision.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/sequencer.c b/sequencer.c
index e773b4999b..23ea4aff4d 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -1,7 +1,6 @@
#include "cache.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/server-info.c b/server-info.c
index 67ce4ad529..5f216449bc 100644
--- a/server-info.c
+++ b/server-info.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "dir.h"
#include "environment.h"
#include "hex.h"
diff --git a/shallow.c b/shallow.c
index 9ff8923ccf..c168764d13 100644
--- a/shallow.c
+++ b/shallow.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "hex.h"
#include "repository.h"
#include "tempfile.h"
diff --git a/sigchain.c b/sigchain.c
index ee778c0580..66123bdbab 100644
--- a/sigchain.c
+++ b/sigchain.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "sigchain.h"
#define SIGCHAIN_MAX_SIGNALS 32
diff --git a/sparse-index.c b/sparse-index.c
index 886054729e..8f6fcd8250 100644
--- a/sparse-index.c
+++ b/sparse-index.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "repository.h"
diff --git a/split-index.c b/split-index.c
index 5602b74994..6467dc9e23 100644
--- a/split-index.c
+++ b/split-index.c
@@ -1,5 +1,4 @@
#include "cache.h"
-#include "alloc.h"
#include "gettext.h"
#include "mem-pool.h"
#include "split-index.h"
diff --git a/strbuf.c b/strbuf.c
index d486189303..5700eb86d4 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/string-list.c b/string-list.c
index db473f273e..ccc105da04 100644
--- a/string-list.c
+++ b/string-list.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "string-list.h"
-#include "alloc.h"
void string_list_init_nodup(struct string_list *list)
{
diff --git a/strvec.c b/strvec.c
index 17d54b6c3b..89dc9e7e75 100644
--- a/strvec.c
+++ b/strvec.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "strvec.h"
-#include "alloc.h"
#include "hex.h"
#include "strbuf.h"
diff --git a/submodule-config.c b/submodule-config.c
index 7fc0812b64..30c8508a79 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "environment.h"
#include "gettext.h"
diff --git a/submodule.c b/submodule.c
index 2e78f51349..4da39e0ede 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1,6 +1,5 @@
#include "cache.h"
#include "abspath.h"
-#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "submodule-config.h"
diff --git a/t/helper/test-reach.c b/t/helper/test-reach.c
index 5b6f217441..119f4908cf 100644
--- a/t/helper/test-reach.c
+++ b/t/helper/test-reach.c
@@ -1,5 +1,4 @@
#include "test-tool.h"
-#include "alloc.h"
#include "commit.h"
#include "commit-reach.h"
#include "config.h"
diff --git a/trace2/tr2_tls.c b/trace2/tr2_tls.c
index 9f46ae12f5..601c9e5036 100644
--- a/trace2/tr2_tls.c
+++ b/trace2/tr2_tls.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "thread-utils.h"
#include "trace.h"
#include "trace2/tr2_tls.h"
diff --git a/trailer.c b/trailer.c
index a2c3ed6f28..2170e01f6a 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/transport.c b/transport.c
index cd4b7f5528..370b18a4ee 100644
--- a/transport.c
+++ b/transport.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "hex.h"
diff --git a/tree-walk.c b/tree-walk.c
index 2993c48c2f..e8f24c8b3f 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -1,6 +1,5 @@
#include "cache.h"
#include "tree-walk.h"
-#include "alloc.h"
#include "dir.h"
#include "gettext.h"
#include "hex.h"
diff --git a/userdiff.c b/userdiff.c
index eaec6ebb5e..a9f078ade0 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "userdiff.h"
#include "attr.h"
diff --git a/worktree.c b/worktree.c
index 7afeaf49fe..0e464241b6 100644
--- a/worktree.c
+++ b/worktree.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "repository.h"
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* Re: [PATCH 0/6] git-compat-util cleanups
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
` (5 preceding siblings ...)
2023-05-16 17:09 ` [PATCH 6/6] common: move alloc macros to common.h Calvin Wan
@ 2023-05-16 17:54 ` Junio C Hamano
2023-05-16 19:00 ` Calvin Wan
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
7 siblings, 1 reply; 29+ messages in thread
From: Junio C Hamano @ 2023-05-16 17:54 UTC (permalink / raw)
To: Calvin Wan; +Cc: git
Calvin Wan <calvinwan@google.com> writes:
> This series focuses on cleaning up and reducing the scope of
> git-compat-util.h by moving headers to their respective files and
> separating out functionality from git-compat-util.h to a new file,
> common.h. I go into more detail in patch 3 as to why I believe this
> separation is useful.
>
> By the end of this series, git-compat-util.h includes common.h which
> includes wrapper.h and usage.h. Since virtually every file includes
> git-compat-util.h and the large majority of files use functions defined
> in common.h, wrapper.h, and usage.h, I believe it makes sense that those
> are also automatically included with git-compat-util.h.
>
> While this series does not intend to draw clearer boundaries for
> common.h, I am open to ideas for how it can be cleaned up more and if
> there is a better name for the file.
This seems to have been based on 'master' around Apr 25th of this
year, like 0807e578 (Merge branch 'en/header-split-cache-h',
2023-04-25). If this were written some time ago and have been
tested internally at $WORK or something, *not* rebasing on a later
tip of 'master' (which you did) is good, but at the same time, it
would be nice to hear on which commit the series is designed to be
applied. To prepare for the start of the next cycle, however, it
may be even better to rebase it on the tip of more recent 'master'
and test it internally (again), and sending the result out as v2
would very much be appreciated ;-)
Thanks.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 0/6] git-compat-util cleanups
2023-05-16 17:54 ` [PATCH 0/6] git-compat-util cleanups Junio C Hamano
@ 2023-05-16 19:00 ` Calvin Wan
0 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-16 19:00 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
On Tue, May 16, 2023 at 10:54 AM Junio C Hamano <gitster@pobox.com> wrote:
> This seems to have been based on 'master' around Apr 25th of this
> year, like 0807e578 (Merge branch 'en/header-split-cache-h',
> 2023-04-25). If this were written some time ago and have been
> tested internally at $WORK or something, *not* rebasing on a later
> tip of 'master' (which you did) is good, but at the same time, it
> would be nice to hear on which commit the series is designed to be
> applied. To prepare for the start of the next cycle, however, it
> may be even better to rebase it on the tip of more recent 'master'
> and test it internally (again), and sending the result out as v2
> would very much be appreciated ;-)
ok I'll go ahead and do that
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h
2023-05-16 17:09 ` [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
@ 2023-05-16 21:18 ` Junio C Hamano
0 siblings, 0 replies; 29+ messages in thread
From: Junio C Hamano @ 2023-05-16 21:18 UTC (permalink / raw)
To: Calvin Wan; +Cc: git
Calvin Wan <calvinwan@google.com> writes:
> While functions like starts_with() probably should not belong in the
> boundaries of the strbuf library, this commit focuses on first splitting
> out headers from git-compat-util.h.
Yeah, compat-util.h started out as a (surprise) "compatibility aid"
that isolates the knowledge of platform specific inclusion order,
what feature macros to define before including which system header
file, etc. but over the years, it accumulated some common niceties
that are too small to deserve their own header files.
starts_with() and skip_prefix() are two examples of such.
It is hard to imagine skip_to_optional_arg_default() should belong
to strbuf API, but if you are booting it out of compat-util,
declaring it in strbuf.h may be the least bad choice, as the
implementation sits in strbuf.c (which is a strange place, but that
is not the fault of this series).
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 2/6] wrapper.h: move declarations for wrapper.c functions from git-compat-util.h
2023-05-16 17:09 ` [PATCH 2/6] wrapper.h: move declarations for wrapper.c " Calvin Wan
@ 2023-05-16 22:08 ` Junio C Hamano
0 siblings, 0 replies; 29+ messages in thread
From: Junio C Hamano @ 2023-05-16 22:08 UTC (permalink / raw)
To: Calvin Wan; +Cc: git
Calvin Wan <calvinwan@google.com> writes:
> Since the functions in wrapper.c are widely used across the codebase,
> include it by default in git-compat-util.h. A future patch will remove
> now unnecessary inclusions of wrapper.h from other files.
The description taken alone implies the move in the other direction
(i.e. taking decls from wrapper.h and moving them to
git-compat-util.h), but that the patch actually does makes sense.
Slim compat-util.h by moving decls for wrapper functions that are
defined in wrapper.c to wrapper.h, but to avoid impacting the users
right away, include wrapper.h in the compat-util.h that these users
expect to find these decls in.
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
> git-compat-util.h | 113 +---------------------------------------------
> wrapper.h | 111 +++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 112 insertions(+), 112 deletions(-)
>
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 51af0a53aa..9898fe9374 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -626,8 +626,7 @@ static inline int git_has_dir_sep(const char *path)
> #include "compat/bswap.h"
>
> #include "wildmatch.h"
> -
> -struct strbuf;
> +#include "wrapper.h"
>
> /* General helper functions */
> NORETURN void usage(const char *err);
> @@ -1047,36 +1046,6 @@ static inline int cast_size_t_to_int(size_t a)
> # define xalloca(size) (xmalloc(size))
> # define xalloca_free(p) (free(p))
> #endif
> -char *xstrdup(const char *str);
> -void *xmalloc(size_t size);
> -void *xmallocz(size_t size);
> -void *xmallocz_gently(size_t size);
> -void *xmemdupz(const void *data, size_t len);
> -char *xstrndup(const char *str, size_t len);
> -void *xrealloc(void *ptr, size_t size);
> -void *xcalloc(size_t nmemb, size_t size);
> -void xsetenv(const char *name, const char *value, int overwrite);
> -void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> -const char *mmap_os_err(void);
> -void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> -int xopen(const char *path, int flags, ...);
> -ssize_t xread(int fd, void *buf, size_t len);
> -ssize_t xwrite(int fd, const void *buf, size_t len);
> -ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
> -int xdup(int fd);
> -FILE *xfopen(const char *path, const char *mode);
> -FILE *xfdopen(int fd, const char *mode);
> -int xmkstemp(char *temp_filename);
> -int xmkstemp_mode(char *temp_filename, int mode);
> -char *xgetcwd(void);
> -FILE *fopen_for_writing(const char *path);
> -FILE *fopen_or_warn(const char *path, const char *mode);
> -
> -/*
> - * Like strncmp, but only return zero if s is NUL-terminated and exactly len
> - * characters long. If it is not, consider it greater than t.
> - */
> -int xstrncmpz(const char *s, const char *t, size_t len);
>
> /*
> * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
> @@ -1178,15 +1147,10 @@ static inline size_t xsize_t(off_t len)
> return (size_t) len;
> }
>
> -__attribute__((format (printf, 3, 4)))
> -int xsnprintf(char *dst, size_t max, const char *fmt, ...);
> -
> #ifndef HOST_NAME_MAX
> #define HOST_NAME_MAX 256
> #endif
>
> -int xgethostname(char *buf, size_t len);
> -
> /* in ctype.c, for kwset users */
> extern const unsigned char tolower_trans_tbl[256];
>
> @@ -1427,72 +1391,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);
> #endif
> #endif
>
> -enum fsync_action {
> - FSYNC_WRITEOUT_ONLY,
> - FSYNC_HARDWARE_FLUSH
> -};
> -
> -/*
> - * Issues an fsync against the specified file according to the specified mode.
> - *
> - * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
> - * systems to flush the OS cache without issuing a flush command to the storage
> - * controller. If those interfaces are unavailable, the function fails with
> - * ENOSYS.
> - *
> - * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
> - * changes are durable. It is not expected to fail.
> - */
> -int git_fsync(int fd, enum fsync_action action);
> -
> -/*
> - * Writes out trace statistics for fsync using the trace2 API.
> - */
> -void trace_git_fsync_stats(void);
> -
> -/*
> - * Preserves errno, prints a message, but gives no warning for ENOENT.
> - * Returns 0 on success, which includes trying to unlink an object that does
> - * not exist.
> - */
> -int unlink_or_warn(const char *path);
> - /*
> - * Tries to unlink file. Returns 0 if unlink succeeded
> - * or the file already didn't exist. Returns -1 and
> - * appends a message to err suitable for
> - * 'error("%s", err->buf)' on error.
> - */
> -int unlink_or_msg(const char *file, struct strbuf *err);
> -/*
> - * Preserves errno, prints a message, but gives no warning for ENOENT.
> - * Returns 0 on success, which includes trying to remove a directory that does
> - * not exist.
> - */
> -int rmdir_or_warn(const char *path);
> -/*
> - * Calls the correct function out of {unlink,rmdir}_or_warn based on
> - * the supplied file mode.
> - */
> -int remove_or_warn(unsigned int mode, const char *path);
> -
> -/*
> - * Call access(2), but warn for any error except "missing file"
> - * (ENOENT or ENOTDIR).
> - */
> -#define ACCESS_EACCES_OK (1U << 0)
> -int access_or_warn(const char *path, int mode, unsigned flag);
> -int access_or_die(const char *path, int mode, unsigned flag);
> -
> -/* Warn on an inaccessible file if errno indicates this is an error */
> -int warn_on_fopen_errors(const char *path);
> -
> -/*
> - * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
> - * may be racy. Do not use this as protection against an attacker who can
> - * simultaneously create paths.
> - */
> -int open_nofollow(const char *path, int flags);
> -
> #ifndef SHELL_PATH
> # define SHELL_PATH "/bin/sh"
> #endif
> @@ -1632,13 +1530,4 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
> ((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
> #endif /* !__GNUC__ */
>
> -void sleep_millisec(int millisec);
> -
> -/*
> - * Generate len bytes from the system cryptographically secure PRNG.
> - * Returns 0 on success and -1 on error, setting errno. The inability to
> - * satisfy the full request is an error.
> - */
> -int csprng_bytes(void *buf, size_t len);
> -
> #endif
> diff --git a/wrapper.h b/wrapper.h
> index f0c7d0616d..c85b1328d1 100644
> --- a/wrapper.h
> +++ b/wrapper.h
> @@ -1,6 +1,42 @@
> #ifndef WRAPPER_H
> #define WRAPPER_H
>
> +char *xstrdup(const char *str);
> +void *xmalloc(size_t size);
> +void *xmallocz(size_t size);
> +void *xmallocz_gently(size_t size);
> +void *xmemdupz(const void *data, size_t len);
> +char *xstrndup(const char *str, size_t len);
> +void *xrealloc(void *ptr, size_t size);
> +void *xcalloc(size_t nmemb, size_t size);
> +void xsetenv(const char *name, const char *value, int overwrite);
> +void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> +const char *mmap_os_err(void);
> +void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
> +int xopen(const char *path, int flags, ...);
> +ssize_t xread(int fd, void *buf, size_t len);
> +ssize_t xwrite(int fd, const void *buf, size_t len);
> +ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
> +int xdup(int fd);
> +FILE *xfopen(const char *path, const char *mode);
> +FILE *xfdopen(int fd, const char *mode);
> +int xmkstemp(char *temp_filename);
> +int xmkstemp_mode(char *temp_filename, int mode);
> +char *xgetcwd(void);
> +FILE *fopen_for_writing(const char *path);
> +FILE *fopen_or_warn(const char *path, const char *mode);
> +
> +/*
> + * Like strncmp, but only return zero if s is NUL-terminated and exactly len
> + * characters long. If it is not, consider it greater than t.
> + */
> +int xstrncmpz(const char *s, const char *t, size_t len);
> +
> +__attribute__((format (printf, 3, 4)))
> +int xsnprintf(char *dst, size_t max, const char *fmt, ...);
> +
> +int xgethostname(char *buf, size_t len);
> +
> /* set default permissions by passing mode arguments to open(2) */
> int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
> int git_mkstemp_mode(char *pattern, int mode);
> @@ -33,4 +69,79 @@ void write_file(const char *path, const char *fmt, ...);
> /* Return 1 if the file is empty or does not exists, 0 otherwise. */
> int is_empty_or_missing_file(const char *filename);
>
> +enum fsync_action {
> + FSYNC_WRITEOUT_ONLY,
> + FSYNC_HARDWARE_FLUSH
> +};
> +
> +/*
> + * Issues an fsync against the specified file according to the specified mode.
> + *
> + * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
> + * systems to flush the OS cache without issuing a flush command to the storage
> + * controller. If those interfaces are unavailable, the function fails with
> + * ENOSYS.
> + *
> + * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
> + * changes are durable. It is not expected to fail.
> + */
> +int git_fsync(int fd, enum fsync_action action);
> +
> +/*
> + * Writes out trace statistics for fsync using the trace2 API.
> + */
> +void trace_git_fsync_stats(void);
> +
> +/*
> + * Preserves errno, prints a message, but gives no warning for ENOENT.
> + * Returns 0 on success, which includes trying to unlink an object that does
> + * not exist.
> + */
> +int unlink_or_warn(const char *path);
> + /*
> + * Tries to unlink file. Returns 0 if unlink succeeded
> + * or the file already didn't exist. Returns -1 and
> + * appends a message to err suitable for
> + * 'error("%s", err->buf)' on error.
> + */
> +int unlink_or_msg(const char *file, struct strbuf *err);
> +/*
> + * Preserves errno, prints a message, but gives no warning for ENOENT.
> + * Returns 0 on success, which includes trying to remove a directory that does
> + * not exist.
> + */
> +int rmdir_or_warn(const char *path);
> +/*
> + * Calls the correct function out of {unlink,rmdir}_or_warn based on
> + * the supplied file mode.
> + */
> +int remove_or_warn(unsigned int mode, const char *path);
> +
> +/*
> + * Call access(2), but warn for any error except "missing file"
> + * (ENOENT or ENOTDIR).
> + */
> +#define ACCESS_EACCES_OK (1U << 0)
> +int access_or_warn(const char *path, int mode, unsigned flag);
> +int access_or_die(const char *path, int mode, unsigned flag);
> +
> +/* Warn on an inaccessible file if errno indicates this is an error */
> +int warn_on_fopen_errors(const char *path);
> +
> +/*
> + * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
> + * may be racy. Do not use this as protection against an attacker who can
> + * simultaneously create paths.
> + */
> +int open_nofollow(const char *path, int flags);
> +
> +void sleep_millisec(int millisec);
> +
> +/*
> + * Generate len bytes from the system cryptographically secure PRNG.
> + * Returns 0 on success and -1 on error, setting errno. The inability to
> + * satisfy the full request is an error.
> + */
> +int csprng_bytes(void *buf, size_t len);
> +
> #endif /* WRAPPER_H */
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 3/6] common.h: move non-compat specific macros and functions from git-compat-util.h
2023-05-16 17:09 ` [PATCH 3/6] common.h: move non-compat specific macros and " Calvin Wan
@ 2023-05-16 22:30 ` Junio C Hamano
2023-05-17 17:26 ` Calvin Wan
0 siblings, 1 reply; 29+ messages in thread
From: Junio C Hamano @ 2023-05-16 22:30 UTC (permalink / raw)
To: Calvin Wan; +Cc: git
Calvin Wan <calvinwan@google.com> writes:
> git-compat-util.h has grown to a large and difficult to read file partly
> because its set of functionality is overloaded. Besides being a
> compatibility file that ensures Git's operation across different
> operating systems, it has also become a dumping ground for commonly used
> macros and static inline functions.
Yeah, compat-util.h started out as a (surprise) "compatibility aid"
that isolates the knowledge of platform specific inclusion order,
what feature macros to define before including which system header
file, etc. "A dumping ground for common stuff"---aptly put ;-).
> This commit separates out macros and static inline functions that are
> compatible across operating systems into common.h.
It sounds a bit too generic a name to me, but OK.
At some level I am more or less indifferent, in the sense that both
compat-util and common need to be included by a .c file anyway,
whether it is _our_ source, or in a future third-party source that
wants to link with the libified part of Git, so it does notreally
make much of a difference if ARRAY_SIZE() is defined in which one of
these two headers. But presumably such a future third-party source
may arrange their own system support using their own counterpart to
compat-util without using ours, and if that is what motivates this
change, I could understand why a more-or-less git-agnostic common.h
could be a good idea.
Everything, other than sane-ctype macros, does look like a generally
useful git-agnostic helper functions. If we were to move in this
direction, I probably would prefer to separate sane-ctype macros in
a separate header, simply because they are _designed_ to override
system supplied ones and some third-party source may not want to use
such a "feature".
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 3/6] common.h: move non-compat specific macros and functions from git-compat-util.h
2023-05-16 22:30 ` Junio C Hamano
@ 2023-05-17 17:26 ` Calvin Wan
0 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-17 17:26 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
> At some level I am more or less indifferent, in the sense that both
> compat-util and common need to be included by a .c file anyway,
> whether it is _our_ source, or in a future third-party source that
> wants to link with the libified part of Git, so it does notreally
> make much of a difference if ARRAY_SIZE() is defined in which one of
> these two headers. But presumably such a future third-party source
> may arrange their own system support using their own counterpart to
> compat-util without using ours, and if that is what motivates this
> change, I could understand why a more-or-less git-agnostic common.h
> could be a good idea.
Also, a later patch moves ALLOC_GROW and other macros from alloc.h to
common.h. I thought it would make more sense to have a common file for
these macros to be moved to rather than adding to git-compat-util for
the sake of only removing the alloc.h dependency from certain files.
> Everything, other than sane-ctype macros, does look like a generally
> useful git-agnostic helper functions. If we were to move in this
> direction, I probably would prefer to separate sane-ctype macros in
> a separate header, simply because they are _designed_ to override
> system supplied ones and some third-party source may not want to use
> such a "feature".
Ah that makes sense that they would belong in a separate file like
ctype.h -- I can split those macros out.
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH v2 0/7] git-compat-util cleanups
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
` (6 preceding siblings ...)
2023-05-16 17:54 ` [PATCH 0/6] git-compat-util cleanups Junio C Hamano
@ 2023-05-23 19:27 ` Calvin Wan
2023-05-23 19:29 ` [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
` (7 more replies)
7 siblings, 8 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:27 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Changes since v1:
- rebased onto 8afb38ac80f6
- add a patch to split up sane-ctype macros into a separate file
rather than into common.h as suggested by Junio
===
This series focuses on cleaning up and reducing the scope of
git-compat-util.h by moving headers to their respective files and
separating out functionality from git-compat-util.h to a new file,
common.h. I go into more detail in patch 4 as to why I believe this
separation is useful.
By the end of this series, git-compat-util.h includes common.h which
includes wrapper.h, and usage.h. Since virtually every file includes
git-compat-util.h and the large majority of files use functions defined
in common.h, wrapper.h, and usage.h, I believe it makes sense that those
are also automatically included with git-compat-util.h.
While this series does not intend to draw clearer boundaries for
common.h, I am open to ideas for how it can be cleaned up more and if
there is a better name for the file.
Calvin Wan (7):
strbuf.h: move declarations for strbuf.c functions from
git-compat-util.h
wrapper.h: move declarations for wrapper.c functions from
git-compat-util.h
sane-ctype.h: move sane-ctype macros from git-compat-util.h
common.h: move non-compat specific macros and functions from
git-compat-util.h
usage.h: move declarations for usage.c functions from
git-compat-util.h
treewide: remove unnecessary includes for wrapper.h
common: move alloc macros to common.h
add-patch.c | 1 -
alias.c | 1 -
alloc.h | 75 ----
apply.c | 2 -
archive-tar.c | 1 -
archive.c | 1 -
attr.c | 1 -
builtin/am.c | 1 -
builtin/bisect.c | 1 -
builtin/blame.c | 1 -
builtin/branch.c | 1 -
builtin/bugreport.c | 1 -
builtin/cat-file.c | 1 -
builtin/checkout--worker.c | 1 -
builtin/clone.c | 1 -
builtin/config.c | 2 -
builtin/credential-cache--daemon.c | 1 -
builtin/credential-cache.c | 1 -
builtin/difftool.c | 1 -
builtin/fast-import.c | 1 -
builtin/fetch-pack.c | 1 -
builtin/fmt-merge-msg.c | 1 -
builtin/fsmonitor--daemon.c | 1 -
builtin/gc.c | 1 -
builtin/get-tar-commit-id.c | 1 -
builtin/grep.c | 1 -
builtin/index-pack.c | 2 -
builtin/init-db.c | 1 -
builtin/log.c | 1 -
builtin/merge.c | 2 -
builtin/mktree.c | 1 -
builtin/mv.c | 1 -
builtin/name-rev.c | 1 -
builtin/pack-objects.c | 2 -
builtin/rebase.c | 1 -
builtin/receive-pack.c | 1 -
builtin/repack.c | 1 -
builtin/rerere.c | 1 -
builtin/rev-parse.c | 1 -
builtin/revert.c | 1 -
builtin/rm.c | 1 -
builtin/submodule--helper.c | 1 -
builtin/symbolic-ref.c | 1 +
builtin/unpack-file.c | 1 -
builtin/unpack-objects.c | 1 +
builtin/worktree.c | 1 -
bulk-checkin.c | 2 -
cache-tree.c | 1 -
chunk-format.c | 1 -
combine-diff.c | 1 -
commit-graph.c | 1 -
commit-reach.c | 1 -
common.h | 486 +++++++++++++++++++++
compat/terminal.c | 1 -
config.c | 2 -
convert.c | 1 -
copy.c | 1 -
csum-file.c | 1 -
daemon.c | 2 -
delta-islands.c | 1 -
diff.c | 2 -
diffcore-rename.c | 1 -
dir-iterator.c | 1 -
dir.c | 2 -
editor.c | 1 -
entry.c | 1 -
environment.c | 1 -
ewah/bitmap.c | 1 -
ewah/ewah_bitmap.c | 1 -
fetch-pack.c | 2 -
fmt-merge-msg.c | 1 -
fsck.c | 1 -
git-compat-util.h | 664 +----------------------------
gpg-interface.c | 1 -
grep.c | 1 -
help.c | 1 -
http-backend.c | 2 -
imap-send.c | 1 -
line-log.c | 1 -
list-objects-filter-options.c | 1 -
list-objects-filter.c | 1 -
merge-ll.c | 1 -
merge-recursive.c | 1 -
midx.c | 1 -
notes-merge.c | 1 -
object-file.c | 2 -
oid-array.c | 1 -
oidtree.c | 1 -
pack-bitmap-write.c | 1 -
pack-bitmap.c | 1 -
pack-objects.c | 1 -
pack-write.c | 1 -
packfile.c | 2 -
parallel-checkout.c | 2 -
path.c | 1 -
pkt-line.c | 1 -
pretty.c | 1 -
prio-queue.c | 1 -
quote.c | 1 -
read-cache.c | 2 -
rebase-interactive.c | 1 -
ref-filter.c | 1 -
reflog-walk.c | 1 -
refs.c | 2 -
refspec.c | 1 -
remote-curl.c | 1 -
remote.c | 1 -
rerere.c | 2 -
revision.c | 1 -
sane-ctype.h | 69 +++
send-pack.c | 1 -
sequencer.c | 2 -
server-info.c | 2 -
setup.c | 1 -
shallow.c | 2 -
sigchain.c | 1 -
sparse-index.c | 1 -
split-index.c | 1 -
strbuf.c | 2 -
strbuf.h | 32 ++
streaming.c | 1 -
string-list.c | 1 -
strvec.c | 1 -
submodule-config.c | 1 -
submodule.c | 1 -
t/helper/test-delta.c | 1 -
t/helper/test-fsmonitor-client.c | 1 -
t/helper/test-reach.c | 1 -
t/helper/test-read-cache.c | 1 -
tag.c | 1 -
tempfile.c | 1 -
trace.c | 1 -
trace2/tr2_tls.c | 1 -
trailer.c | 1 -
transport-helper.c | 1 -
transport.c | 2 -
tree-walk.c | 1 -
upload-pack.c | 1 -
usage.c | 1 -
usage.h | 60 +++
userdiff.c | 1 -
versioncmp.c | 1 +
worktree.c | 2 -
wrapper.c | 1 -
wrapper.h | 111 +++++
write-or-die.c | 1 -
146 files changed, 764 insertions(+), 896 deletions(-)
create mode 100644 common.h
create mode 100644 sane-ctype.h
create mode 100644 usage.h
Range-diff against v1:
1: c3f998df91 ! 1: a2b7a28e6e strbuf.h: move declarations for strbuf.c functions from git-compat-util.h
@@ Commit message
boundaries of the strbuf library, this commit focuses on first splitting
out headers from git-compat-util.h.
+ ## builtin/symbolic-ref.c ##
+@@
+ #include "gettext.h"
+ #include "refs.h"
+ #include "parse-options.h"
++#include "strbuf.h"
+
+ static const char * const git_symbolic_ref_usage[] = {
+ N_("git symbolic-ref [-m <reason>] <name> <ref>"),
+
+ ## builtin/unpack-objects.c ##
+@@
+ #include "blob.h"
+ #include "commit.h"
+ #include "replace-object.h"
++#include "strbuf.h"
+ #include "tag.h"
+ #include "tree.h"
+ #include "tree-walk.h"
+
## git-compat-util.h ##
@@ git-compat-util.h: void set_warn_routine(report_fn routine);
report_fn get_warn_routine(void);
@@ strbuf.h: char *xstrvfmt(const char *fmt, va_list ap);
+}
+
#endif /* STRBUF_H */
+
+ ## versioncmp.c ##
+@@
+ #include "git-compat-util.h"
+ #include "config.h"
++#include "strbuf.h"
+ #include "string-list.h"
+ #include "versioncmp.h"
+
2: 71176763ff ! 2: b12fb0e2ff wrapper.h: move declarations for wrapper.c functions from git-compat-util.h
@@ Commit message
## git-compat-util.h ##
@@ git-compat-util.h: static inline int git_has_dir_sep(const char *path)
+
#include "compat/bswap.h"
- #include "wildmatch.h"
--
-struct strbuf;
+#include "wrapper.h"
-: ---------- > 3: 016f671836 sane-ctype.h: move sane-ctype macros from git-compat-util.h
3: 6c905444cd ! 4: 244650e5c9 common.h: move non-compat specific macros and functions from git-compat-util.h
@@ common.h (new)
+ return (size_t) len;
+}
+
-+/* in ctype.c, for kwset users */
-+extern const unsigned char tolower_trans_tbl[256];
-+
-+/* Sane ctype - no locale, and works with signed chars */
-+#undef isascii
-+#undef isspace
-+#undef isdigit
-+#undef isalpha
-+#undef isalnum
-+#undef isprint
-+#undef islower
-+#undef isupper
-+#undef tolower
-+#undef toupper
-+#undef iscntrl
-+#undef ispunct
-+#undef isxdigit
-+
-+extern const unsigned char sane_ctype[256];
-+extern const signed char hexval_table[256];
-+#define GIT_SPACE 0x01
-+#define GIT_DIGIT 0x02
-+#define GIT_ALPHA 0x04
-+#define GIT_GLOB_SPECIAL 0x08
-+#define GIT_REGEX_SPECIAL 0x10
-+#define GIT_PATHSPEC_MAGIC 0x20
-+#define GIT_CNTRL 0x40
-+#define GIT_PUNCT 0x80
-+#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
-+#define isascii(x) (((x) & ~0x7f) == 0)
-+#define isspace(x) sane_istest(x,GIT_SPACE)
-+#define isdigit(x) sane_istest(x,GIT_DIGIT)
-+#define isalpha(x) sane_istest(x,GIT_ALPHA)
-+#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
-+#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
-+#define islower(x) sane_iscase(x, 1)
-+#define isupper(x) sane_iscase(x, 0)
-+#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
-+#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
-+#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
-+#define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
-+ GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
-+#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
-+#define tolower(x) sane_case((unsigned char)(x), 0x20)
-+#define toupper(x) sane_case((unsigned char)(x), 0)
-+#define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
-+
-+static inline int sane_case(int x, int high)
-+{
-+ if (sane_istest(x, GIT_ALPHA))
-+ x = (x & ~0x20) | high;
-+ return x;
-+}
-+
-+static inline int sane_iscase(int x, int is_lower)
-+{
-+ if (!sane_istest(x, GIT_ALPHA))
-+ return 0;
-+
-+ if (is_lower)
-+ return (x & 0x20) != 0;
-+ else
-+ return (x & 0x20) == 0;
-+}
-+
+/*
+ * Like skip_prefix, but compare case-insensitively. Note that the comparison
+ * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
@@ git-compat-util.h: static inline int cast_size_t_to_int(size_t a)
#define HOST_NAME_MAX 256
#endif
--/* in ctype.c, for kwset users */
--extern const unsigned char tolower_trans_tbl[256];
--
--/* Sane ctype - no locale, and works with signed chars */
--#undef isascii
--#undef isspace
--#undef isdigit
--#undef isalpha
--#undef isalnum
--#undef isprint
--#undef islower
--#undef isupper
--#undef tolower
--#undef toupper
--#undef iscntrl
--#undef ispunct
--#undef isxdigit
--
--extern const unsigned char sane_ctype[256];
--extern const signed char hexval_table[256];
--#define GIT_SPACE 0x01
--#define GIT_DIGIT 0x02
--#define GIT_ALPHA 0x04
--#define GIT_GLOB_SPECIAL 0x08
--#define GIT_REGEX_SPECIAL 0x10
--#define GIT_PATHSPEC_MAGIC 0x20
--#define GIT_CNTRL 0x40
--#define GIT_PUNCT 0x80
--#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
--#define isascii(x) (((x) & ~0x7f) == 0)
--#define isspace(x) sane_istest(x,GIT_SPACE)
--#define isdigit(x) sane_istest(x,GIT_DIGIT)
--#define isalpha(x) sane_istest(x,GIT_ALPHA)
--#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
--#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
--#define islower(x) sane_iscase(x, 1)
--#define isupper(x) sane_iscase(x, 0)
--#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
--#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
--#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
--#define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
-- GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
--#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
--#define tolower(x) sane_case((unsigned char)(x), 0x20)
--#define toupper(x) sane_case((unsigned char)(x), 0)
--#define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
--
--static inline int sane_case(int x, int high)
--{
-- if (sane_istest(x, GIT_ALPHA))
-- x = (x & ~0x20) | high;
-- return x;
--}
--
--static inline int sane_iscase(int x, int is_lower)
--{
-- if (!sane_istest(x, GIT_ALPHA))
-- return 0;
--
-- if (is_lower)
-- return (x & 0x20) != 0;
-- else
-- return (x & 0x20) == 0;
--}
--
+ #include "sane-ctype.h"
+
-/*
- * Like skip_prefix, but compare case-insensitively. Note that the comparison
- * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
4: 280a7492ec ! 5: 91a1253ee6 usage.h: move declarations for usage.c functions from git-compat-util.h
@@ common.h
## git-compat-util.h ##
@@ git-compat-util.h: static inline int git_has_dir_sep(const char *path)
+
#include "compat/bswap.h"
- #include "wildmatch.h"
-#include "wrapper.h"
-
-/* General helper functions */
@@ git-compat-util.h: static inline int git_has_dir_sep(const char *path)
-int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
-void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-
+-
#ifndef NO_OPENSSL
#ifdef APPLE_COMMON_CRYPTO
+ #include "compat/apple-common-crypto.h"
@@ git-compat-util.h: void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
#include <openssl/rand.h>
#endif
5: 9b29a77a90 ! 6: c9850a7c18 treewide: remove unnecessary includes for wrapper.h
@@ Commit message
## apply.c ##
@@
- #include "apply.h"
- #include "entry.h"
- #include "setup.h"
+ #include "symlinks.h"
+ #include "wildmatch.h"
+ #include "ws.h"
-#include "wrapper.h"
struct gitdiff_data {
@@ apply.c
## builtin/am.c ##
@@
- #include "pager.h"
+ #include "path.h"
#include "repository.h"
#include "pretty.h"
-#include "wrapper.h"
@@ builtin/clone.c
## builtin/config.c ##
@@
- #include "quote.h"
#include "setup.h"
+ #include "strbuf.h"
#include "worktree.h"
-#include "wrapper.h"
@@ builtin/config.c
## builtin/credential-cache.c ##
@@
- #include "builtin.h"
- #include "gettext.h"
#include "parse-options.h"
+ #include "path.h"
+ #include "strbuf.h"
-#include "wrapper.h"
#include "write-or-die.h"
@@ builtin/gc.c
## builtin/get-tar-commit-id.c ##
@@
+ #include "commit.h"
#include "tar.h"
- #include "builtin.h"
#include "quote.h"
-#include "wrapper.h"
@@ builtin/index-pack.c
## builtin/init-db.c ##
@@
- #include "parse-options.h"
+ #include "path.h"
#include "setup.h"
- #include "worktree.h"
+ #include "strbuf.h"
-#include "wrapper.h"
- #ifndef DEFAULT_GIT_TEMPLATE_DIR
- #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
+ static int guess_repository_type(const char *git_dir)
+ {
## builtin/merge.c ##
@@
@@ builtin/receive-pack.c
## builtin/rerere.c ##
@@
- #include "parse-options.h"
+ #include "repository.h"
#include "string-list.h"
#include "rerere.h"
-#include "wrapper.h"
@@ builtin/unpack-file.c
@@
#include "hex.h"
#include "object-name.h"
- #include "object-store.h"
+ #include "object-store-ll.h"
-#include "wrapper.h"
static char *create_temp_file(struct object_id *oid)
@@ bulk-checkin.c
@@
#include "packfile.h"
#include "object-file.h"
- #include "object-store.h"
+ #include "object-store-ll.h"
-#include "wrapper.h"
static int odb_transaction_nesting;
@@ combine-diff.c
## commit-graph.c ##
@@
- #include "json-writer.h"
#include "trace2.h"
+ #include "tree.h"
#include "chunk-format.h"
-#include "wrapper.h"
@@ compat/terminal.c
## config.c ##
@@
- #include "setup.h"
- #include "trace2.h"
+ #include "wildmatch.h"
#include "worktree.h"
+ #include "ws.h"
-#include "wrapper.h"
#include "write-or-die.h"
@@ convert.c
@@
#include "trace.h"
#include "utf8.h"
- #include "ll-merge.h"
+ #include "merge-ll.h"
-#include "wrapper.h"
/*
@@ convert.c
## copy.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
+ #include "copy.h"
+ #include "path.h"
-#include "wrapper.h"
int copy_fd(int ifd, int ofd)
@@ copy.c
## csum-file.c ##
@@
- #include "git-compat-util.h"
#include "progress.h"
#include "csum-file.h"
+ #include "hash.h"
-#include "wrapper.h"
static void verify_buffer_or_die(struct hashfile *f,
@@ daemon.c
## diff.c ##
@@
- #include "object-name.h"
#include "setup.h"
#include "strmap.h"
+ #include "ws.h"
-#include "wrapper.h"
#ifdef NO_FAST_WORKING_DIRECTORY
@@ diff.c
## dir.c ##
@@
- #include "setup.h"
- #include "submodule-config.h"
+ #include "symlinks.h"
#include "trace2.h"
+ #include "tree.h"
-#include "wrapper.h"
/*
@@ gpg-interface.c
#include "tempfile.h"
#include "alias.h"
-#include "wrapper.h"
+ #include "environment.h"
static int git_gpg_config(const char *, const char *, void *);
-
## grep.c ##
@@
@@ grep.c
## http-backend.c ##
@@
- #include "object-store.h"
+ #include "object-store-ll.h"
#include "protocol.h"
#include "date.h"
-#include "wrapper.h"
@@ http-backend.c
## imap-send.c ##
@@
- #include "run-command.h"
#include "parse-options.h"
#include "setup.h"
+ #include "strbuf.h"
-#include "wrapper.h"
#if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
typedef void *SSL;
#endif
- ## ll-merge.c ##
+ ## merge-ll.c ##
@@
- #include "run-command.h"
- #include "ll-merge.h"
+ #include "merge-ll.h"
#include "quote.h"
+ #include "strbuf.h"
-#include "wrapper.h"
struct ll_merge_driver;
@@ object-file.c
## pack-write.c ##
@@
- #include "oidmap.h"
- #include "pack-objects.h"
#include "pack-revindex.h"
+ #include "path.h"
+ #include "strbuf.h"
-#include "wrapper.h"
void reset_pack_idx_option(struct pack_idx_option *opts)
@@ packfile.c
## parallel-checkout.c ##
@@
- #include "streaming.h"
+ #include "symlinks.h"
#include "thread-utils.h"
#include "trace2.h"
-#include "wrapper.h"
@@ parallel-checkout.c
## path.c ##
@@
- #include "object-store.h"
+ #include "object-store-ll.h"
#include "lockfile.h"
#include "exec-cmd.h"
-#include "wrapper.h"
@@ rebase-interactive.c
## refs.c ##
@@
- #include "sigchain.h"
#include "date.h"
#include "commit.h"
+ #include "wildmatch.h"
-#include "wrapper.h"
/*
@@ refs.c
## rerere.c ##
@@
- #include "object-store.h"
+ #include "object-store-ll.h"
#include "hash-lookup.h"
#include "strmap.h"
-#include "wrapper.h"
@@ sequencer.c
## server-info.c ##
@@
- #include "object-file.h"
- #include "object-store.h"
+ #include "object-store-ll.h"
+ #include "server-info.h"
#include "strbuf.h"
-#include "wrapper.h"
@@ server-info.c
## setup.c ##
@@
- #include "promisor-remote.h"
#include "quote.h"
#include "trace2.h"
+ #include "worktree.h"
-#include "wrapper.h"
static int inside_git_dir = -1;
@@ setup.c
## shallow.c ##
@@
- #include "commit-reach.h"
#include "shallow.h"
+ #include "statinfo.h"
#include "trace.h"
-#include "wrapper.h"
@@ strbuf.c
## streaming.c ##
@@
- #include "object-store.h"
+ #include "object-store-ll.h"
#include "replace-object.h"
#include "packfile.h"
-#include "wrapper.h"
@@ t/helper/test-fsmonitor-client.c
## t/helper/test-read-cache.c ##
@@
- #include "cache.h"
- #include "config.h"
+ #include "read-cache-ll.h"
+ #include "repository.h"
#include "setup.h"
-#include "wrapper.h"
@@ transport-helper.c
## transport.c ##
@@
- #include "object-store.h"
+ #include "object-store-ll.h"
#include "color.h"
#include "bundle-uri.h"
-#include "wrapper.h"
@@ worktree.c
## wrapper.c ##
@@
- #include "config.h"
- #include "gettext.h"
+ #include "repository.h"
+ #include "strbuf.h"
#include "trace2.h"
-#include "wrapper.h"
6: 9604b66c72 ! 7: 067b28ec09 common: move alloc macros to common.h
@@ Commit message
## add-patch.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "add-interactive.h"
#include "advice.h"
-#include "alloc.h"
@@ alloc.h: void *alloc_object_node(struct repository *r);
## apply.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
+ #include "base85.h"
#include "config.h"
- #include "object-store.h"
- #include "blob.h"
+ #include "object-store-ll.h"
## archive-tar.c ##
@@
@@ attr.c
@@
*/
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
@@ builtin/cat-file.c
@@
*/
#define USE_THE_INDEX_VARIABLE
- #include "cache.h"
+ #include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
- #include "builtin.h"
+ #include "diff.h"
## builtin/checkout--worker.c ##
@@
@@ builtin/grep.c
@@
* Copyright (c) 2006 Junio C Hamano
*/
- #include "cache.h"
+ #include "builtin.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
@@ builtin/log.c
## builtin/merge.c ##
@@
- #include "cache.h"
+ #include "builtin.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
@@ builtin/repack.c
## builtin/rev-parse.c ##
@@
#define USE_THE_INDEX_VARIABLE
- #include "cache.h"
+ #include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
@@ bulk-checkin.c
## cache-tree.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "hex.h"
@@ common.h: static inline int cast_size_t_to_int(size_t a)
## config.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
@@ config.c
## daemon.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
- #include "pkt-line.h"
+ #include "path.h"
## delta-islands.c ##
@@
@@ delta-islands.c
## diff.c ##
@@
*/
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
+ #include "base85.h"
#include "config.h"
#include "convert.h"
- #include "environment.h"
## diffcore-rename.c ##
@@
@@ diffcore-rename.c
-#include "alloc.h"
#include "diff.h"
#include "diffcore.h"
- #include "object-store.h"
+ #include "object-store-ll.h"
## dir-iterator.c ##
@@
@@ fetch-pack.c
-#include "alloc.h"
#include "repository.h"
#include "config.h"
- #include "environment.h"
+ #include "date.h"
## fmt-merge-msg.c ##
@@
@@ fsck.c
@@
#include "git-compat-util.h"
-#include "alloc.h"
+ #include "date.h"
+ #include "dir.h"
#include "hex.h"
- #include "object-store.h"
- #include "repository.h"
## help.c ##
@@
@@ list-objects-filter.c
## midx.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
@@ midx.c
## object-file.c ##
@@
*/
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
@@ pack-objects.c
## packfile.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
@@ packfile.c
## parallel-checkout.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "entry.h"
@@ read-cache.c
@@
* Copyright (C) Linus Torvalds, 2005
*/
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
+ #include "bulk-checkin.h"
#include "config.h"
- #include "diff.h"
- #include "diffcore.h"
+ #include "date.h"
## ref-filter.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
- #include "hex.h"
+ #include "gpg-interface.h"
## reflog-walk.c ##
@@
@@ refspec.c
#include "git-compat-util.h"
-#include "alloc.h"
#include "gettext.h"
+ #include "hash.h"
#include "hex.h"
- #include "strvec.h"
## remote-curl.c ##
@@
@@ remote.c
## rerere.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
+ #include "copy.h"
#include "gettext.h"
- #include "hex.h"
## revision.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
@@ revision.c
## sequencer.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
+ #include "copy.h"
#include "environment.h"
- #include "gettext.h"
## server-info.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "environment.h"
@@ server-info.c
## shallow.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "hex.h"
#include "repository.h"
@@ sigchain.c
## sparse-index.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
- #include "repository.h"
+ #include "name-hash.h"
## split-index.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
-#include "alloc.h"
#include "gettext.h"
+ #include "hash.h"
#include "mem-pool.h"
- #include "split-index.h"
## strbuf.c ##
@@
#include "git-compat-util.h"
- #include "abspath.h"
-#include "alloc.h"
- #include "environment.h"
#include "gettext.h"
#include "hex.h"
+ #include "path.h"
## string-list.c ##
@@
@@ submodule-config.c
## submodule.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "repository.h"
@@ transport.c
## tree-walk.c ##
@@
- #include "cache.h"
+ #include "git-compat-util.h"
#include "tree-walk.h"
-#include "alloc.h"
#include "dir.h"
@@ worktree.c
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
- #include "repository.h"
+ #include "path.h"
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
@ 2023-05-23 19:29 ` Calvin Wan
2023-05-26 21:06 ` Jonathan Tan
2023-05-23 19:29 ` [PATCH v2 2/7] wrapper.h: move declarations for wrapper.c " Calvin Wan
` (6 subsequent siblings)
7 siblings, 1 reply; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:29 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
While functions like starts_with() probably should not belong in the
boundaries of the strbuf library, this commit focuses on first splitting
out headers from git-compat-util.h.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
builtin/symbolic-ref.c | 1 +
builtin/unpack-objects.c | 1 +
git-compat-util.h | 32 --------------------------------
strbuf.h | 32 ++++++++++++++++++++++++++++++++
versioncmp.c | 1 +
5 files changed, 35 insertions(+), 32 deletions(-)
diff --git a/builtin/symbolic-ref.c b/builtin/symbolic-ref.c
index a61fa3c0f8..c9defe4d2e 100644
--- a/builtin/symbolic-ref.c
+++ b/builtin/symbolic-ref.c
@@ -3,6 +3,7 @@
#include "gettext.h"
#include "refs.h"
#include "parse-options.h"
+#include "strbuf.h"
static const char * const git_symbolic_ref_usage[] = {
N_("git symbolic-ref [-m <reason>] <name> <ref>"),
diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c
index b4b46ae729..0510e60e6e 100644
--- a/builtin/unpack-objects.c
+++ b/builtin/unpack-objects.c
@@ -12,6 +12,7 @@
#include "blob.h"
#include "commit.h"
#include "replace-object.h"
+#include "strbuf.h"
#include "tag.h"
#include "tree.h"
#include "tree-walk.h"
diff --git a/git-compat-util.h b/git-compat-util.h
index 1889da7986..fe9e86bad0 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -677,9 +677,6 @@ void set_warn_routine(report_fn routine);
report_fn get_warn_routine(void);
void set_die_is_recursing_routine(int (*routine)(void));
-int starts_with(const char *str, const char *prefix);
-int istarts_with(const char *str, const char *prefix);
-
/*
* If the string "str" begins with the string found in "prefix", return 1.
* The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
@@ -708,29 +705,6 @@ static inline int skip_prefix(const char *str, const char *prefix,
return 0;
}
-/*
- * If the string "str" is the same as the string in "prefix", then the "arg"
- * parameter is set to the "def" parameter and 1 is returned.
- * If the string "str" begins with the string found in "prefix" and then a
- * "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1"
- * (i.e., to the point in the string right after the prefix and the "=" sign),
- * and 1 is returned.
- *
- * Otherwise, return 0 and leave "arg" untouched.
- *
- * When we accept both a "--key" and a "--key=<val>" option, this function
- * can be used instead of !strcmp(arg, "--key") and then
- * skip_prefix(arg, "--key=", &arg) to parse such an option.
- */
-int skip_to_optional_arg_default(const char *str, const char *prefix,
- const char **arg, const char *def);
-
-static inline int skip_to_optional_arg(const char *str, const char *prefix,
- const char **arg)
-{
- return skip_to_optional_arg_default(str, prefix, arg, "");
-}
-
/*
* Like skip_prefix, but promises never to read past "len" bytes of the input
* buffer, and returns the remaining number of bytes in "out" via "outlen".
@@ -775,12 +749,6 @@ static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
return strip_suffix_mem(str, len, suffix);
}
-static inline int ends_with(const char *str, const char *suffix)
-{
- size_t len;
- return strip_suffix(str, suffix, &len);
-}
-
#define SWAP(a, b) do { \
void *_swap_a_ptr = &(a); \
void *_swap_b_ptr = &(b); \
diff --git a/strbuf.h b/strbuf.h
index 8903195416..28b3038e83 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -698,4 +698,36 @@ char *xstrvfmt(const char *fmt, va_list ap);
__attribute__((format (printf, 1, 2)))
char *xstrfmt(const char *fmt, ...);
+int starts_with(const char *str, const char *prefix);
+int istarts_with(const char *str, const char *prefix);
+
+/*
+ * If the string "str" is the same as the string in "prefix", then the "arg"
+ * parameter is set to the "def" parameter and 1 is returned.
+ * If the string "str" begins with the string found in "prefix" and then a
+ * "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1"
+ * (i.e., to the point in the string right after the prefix and the "=" sign),
+ * and 1 is returned.
+ *
+ * Otherwise, return 0 and leave "arg" untouched.
+ *
+ * When we accept both a "--key" and a "--key=<val>" option, this function
+ * can be used instead of !strcmp(arg, "--key") and then
+ * skip_prefix(arg, "--key=", &arg) to parse such an option.
+ */
+int skip_to_optional_arg_default(const char *str, const char *prefix,
+ const char **arg, const char *def);
+
+static inline int skip_to_optional_arg(const char *str, const char *prefix,
+ const char **arg)
+{
+ return skip_to_optional_arg_default(str, prefix, arg, "");
+}
+
+static inline int ends_with(const char *str, const char *suffix)
+{
+ size_t len;
+ return strip_suffix(str, suffix, &len);
+}
+
#endif /* STRBUF_H */
diff --git a/versioncmp.c b/versioncmp.c
index 74cc7c43f0..45e676cbca 100644
--- a/versioncmp.c
+++ b/versioncmp.c
@@ -1,5 +1,6 @@
#include "git-compat-util.h"
#include "config.h"
+#include "strbuf.h"
#include "string-list.h"
#include "versioncmp.h"
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH v2 2/7] wrapper.h: move declarations for wrapper.c functions from git-compat-util.h
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
2023-05-23 19:29 ` [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
@ 2023-05-23 19:29 ` Calvin Wan
2023-05-26 21:25 ` Jonathan Tan
2023-05-23 19:29 ` [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros " Calvin Wan
` (5 subsequent siblings)
7 siblings, 1 reply; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:29 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Since the functions in wrapper.c are widely used across the codebase,
include it by default in git-compat-util.h. A future patch will remove
now unnecessary inclusions of wrapper.h from other files.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
git-compat-util.h | 112 +---------------------------------------------
wrapper.h | 111 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 112 insertions(+), 111 deletions(-)
diff --git a/git-compat-util.h b/git-compat-util.h
index fe9e86bad0..f8e68baf29 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -625,7 +625,7 @@ static inline int git_has_dir_sep(const char *path)
#include "compat/bswap.h"
-struct strbuf;
+#include "wrapper.h"
/* General helper functions */
NORETURN void usage(const char *err);
@@ -1045,36 +1045,6 @@ static inline int cast_size_t_to_int(size_t a)
# define xalloca(size) (xmalloc(size))
# define xalloca_free(p) (free(p))
#endif
-char *xstrdup(const char *str);
-void *xmalloc(size_t size);
-void *xmallocz(size_t size);
-void *xmallocz_gently(size_t size);
-void *xmemdupz(const void *data, size_t len);
-char *xstrndup(const char *str, size_t len);
-void *xrealloc(void *ptr, size_t size);
-void *xcalloc(size_t nmemb, size_t size);
-void xsetenv(const char *name, const char *value, int overwrite);
-void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
-const char *mmap_os_err(void);
-void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
-int xopen(const char *path, int flags, ...);
-ssize_t xread(int fd, void *buf, size_t len);
-ssize_t xwrite(int fd, const void *buf, size_t len);
-ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
-int xdup(int fd);
-FILE *xfopen(const char *path, const char *mode);
-FILE *xfdopen(int fd, const char *mode);
-int xmkstemp(char *temp_filename);
-int xmkstemp_mode(char *temp_filename, int mode);
-char *xgetcwd(void);
-FILE *fopen_for_writing(const char *path);
-FILE *fopen_or_warn(const char *path, const char *mode);
-
-/*
- * Like strncmp, but only return zero if s is NUL-terminated and exactly len
- * characters long. If it is not, consider it greater than t.
- */
-int xstrncmpz(const char *s, const char *t, size_t len);
/*
* FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
@@ -1176,15 +1146,10 @@ static inline size_t xsize_t(off_t len)
return (size_t) len;
}
-__attribute__((format (printf, 3, 4)))
-int xsnprintf(char *dst, size_t max, const char *fmt, ...);
-
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
-int xgethostname(char *buf, size_t len);
-
/* in ctype.c, for kwset users */
extern const unsigned char tolower_trans_tbl[256];
@@ -1425,72 +1390,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);
#endif
#endif
-enum fsync_action {
- FSYNC_WRITEOUT_ONLY,
- FSYNC_HARDWARE_FLUSH
-};
-
-/*
- * Issues an fsync against the specified file according to the specified mode.
- *
- * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
- * systems to flush the OS cache without issuing a flush command to the storage
- * controller. If those interfaces are unavailable, the function fails with
- * ENOSYS.
- *
- * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
- * changes are durable. It is not expected to fail.
- */
-int git_fsync(int fd, enum fsync_action action);
-
-/*
- * Writes out trace statistics for fsync using the trace2 API.
- */
-void trace_git_fsync_stats(void);
-
-/*
- * Preserves errno, prints a message, but gives no warning for ENOENT.
- * Returns 0 on success, which includes trying to unlink an object that does
- * not exist.
- */
-int unlink_or_warn(const char *path);
- /*
- * Tries to unlink file. Returns 0 if unlink succeeded
- * or the file already didn't exist. Returns -1 and
- * appends a message to err suitable for
- * 'error("%s", err->buf)' on error.
- */
-int unlink_or_msg(const char *file, struct strbuf *err);
-/*
- * Preserves errno, prints a message, but gives no warning for ENOENT.
- * Returns 0 on success, which includes trying to remove a directory that does
- * not exist.
- */
-int rmdir_or_warn(const char *path);
-/*
- * Calls the correct function out of {unlink,rmdir}_or_warn based on
- * the supplied file mode.
- */
-int remove_or_warn(unsigned int mode, const char *path);
-
-/*
- * Call access(2), but warn for any error except "missing file"
- * (ENOENT or ENOTDIR).
- */
-#define ACCESS_EACCES_OK (1U << 0)
-int access_or_warn(const char *path, int mode, unsigned flag);
-int access_or_die(const char *path, int mode, unsigned flag);
-
-/* Warn on an inaccessible file if errno indicates this is an error */
-int warn_on_fopen_errors(const char *path);
-
-/*
- * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
- * may be racy. Do not use this as protection against an attacker who can
- * simultaneously create paths.
- */
-int open_nofollow(const char *path, int flags);
-
#ifndef SHELL_PATH
# define SHELL_PATH "/bin/sh"
#endif
@@ -1630,13 +1529,4 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
#endif /* !__GNUC__ */
-void sleep_millisec(int millisec);
-
-/*
- * Generate len bytes from the system cryptographically secure PRNG.
- * Returns 0 on success and -1 on error, setting errno. The inability to
- * satisfy the full request is an error.
- */
-int csprng_bytes(void *buf, size_t len);
-
#endif
diff --git a/wrapper.h b/wrapper.h
index f0c7d0616d..c85b1328d1 100644
--- a/wrapper.h
+++ b/wrapper.h
@@ -1,6 +1,42 @@
#ifndef WRAPPER_H
#define WRAPPER_H
+char *xstrdup(const char *str);
+void *xmalloc(size_t size);
+void *xmallocz(size_t size);
+void *xmallocz_gently(size_t size);
+void *xmemdupz(const void *data, size_t len);
+char *xstrndup(const char *str, size_t len);
+void *xrealloc(void *ptr, size_t size);
+void *xcalloc(size_t nmemb, size_t size);
+void xsetenv(const char *name, const char *value, int overwrite);
+void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
+const char *mmap_os_err(void);
+void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
+int xopen(const char *path, int flags, ...);
+ssize_t xread(int fd, void *buf, size_t len);
+ssize_t xwrite(int fd, const void *buf, size_t len);
+ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
+int xdup(int fd);
+FILE *xfopen(const char *path, const char *mode);
+FILE *xfdopen(int fd, const char *mode);
+int xmkstemp(char *temp_filename);
+int xmkstemp_mode(char *temp_filename, int mode);
+char *xgetcwd(void);
+FILE *fopen_for_writing(const char *path);
+FILE *fopen_or_warn(const char *path, const char *mode);
+
+/*
+ * Like strncmp, but only return zero if s is NUL-terminated and exactly len
+ * characters long. If it is not, consider it greater than t.
+ */
+int xstrncmpz(const char *s, const char *t, size_t len);
+
+__attribute__((format (printf, 3, 4)))
+int xsnprintf(char *dst, size_t max, const char *fmt, ...);
+
+int xgethostname(char *buf, size_t len);
+
/* set default permissions by passing mode arguments to open(2) */
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
int git_mkstemp_mode(char *pattern, int mode);
@@ -33,4 +69,79 @@ void write_file(const char *path, const char *fmt, ...);
/* Return 1 if the file is empty or does not exists, 0 otherwise. */
int is_empty_or_missing_file(const char *filename);
+enum fsync_action {
+ FSYNC_WRITEOUT_ONLY,
+ FSYNC_HARDWARE_FLUSH
+};
+
+/*
+ * Issues an fsync against the specified file according to the specified mode.
+ *
+ * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
+ * systems to flush the OS cache without issuing a flush command to the storage
+ * controller. If those interfaces are unavailable, the function fails with
+ * ENOSYS.
+ *
+ * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
+ * changes are durable. It is not expected to fail.
+ */
+int git_fsync(int fd, enum fsync_action action);
+
+/*
+ * Writes out trace statistics for fsync using the trace2 API.
+ */
+void trace_git_fsync_stats(void);
+
+/*
+ * Preserves errno, prints a message, but gives no warning for ENOENT.
+ * Returns 0 on success, which includes trying to unlink an object that does
+ * not exist.
+ */
+int unlink_or_warn(const char *path);
+ /*
+ * Tries to unlink file. Returns 0 if unlink succeeded
+ * or the file already didn't exist. Returns -1 and
+ * appends a message to err suitable for
+ * 'error("%s", err->buf)' on error.
+ */
+int unlink_or_msg(const char *file, struct strbuf *err);
+/*
+ * Preserves errno, prints a message, but gives no warning for ENOENT.
+ * Returns 0 on success, which includes trying to remove a directory that does
+ * not exist.
+ */
+int rmdir_or_warn(const char *path);
+/*
+ * Calls the correct function out of {unlink,rmdir}_or_warn based on
+ * the supplied file mode.
+ */
+int remove_or_warn(unsigned int mode, const char *path);
+
+/*
+ * Call access(2), but warn for any error except "missing file"
+ * (ENOENT or ENOTDIR).
+ */
+#define ACCESS_EACCES_OK (1U << 0)
+int access_or_warn(const char *path, int mode, unsigned flag);
+int access_or_die(const char *path, int mode, unsigned flag);
+
+/* Warn on an inaccessible file if errno indicates this is an error */
+int warn_on_fopen_errors(const char *path);
+
+/*
+ * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
+ * may be racy. Do not use this as protection against an attacker who can
+ * simultaneously create paths.
+ */
+int open_nofollow(const char *path, int flags);
+
+void sleep_millisec(int millisec);
+
+/*
+ * Generate len bytes from the system cryptographically secure PRNG.
+ * Returns 0 on success and -1 on error, setting errno. The inability to
+ * satisfy the full request is an error.
+ */
+int csprng_bytes(void *buf, size_t len);
+
#endif /* WRAPPER_H */
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros from git-compat-util.h
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
2023-05-23 19:29 ` [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
2023-05-23 19:29 ` [PATCH v2 2/7] wrapper.h: move declarations for wrapper.c " Calvin Wan
@ 2023-05-23 19:29 ` Calvin Wan
2023-05-26 21:19 ` Jonathan Tan
2023-05-23 19:29 ` [PATCH v2 4/7] common.h: move non-compat specific macros and functions " Calvin Wan
` (4 subsequent siblings)
7 siblings, 1 reply; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:29 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Splitting these macros from git-compat-util.h cleans up the file and
allows future third-party sources to not use these overrides if they do
not wish to.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
git-compat-util.h | 65 +-------------------------------------------
sane-ctype.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 64 deletions(-)
create mode 100644 sane-ctype.h
diff --git a/git-compat-util.h b/git-compat-util.h
index f8e68baf29..9d3c21acbb 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1150,70 +1150,7 @@ static inline size_t xsize_t(off_t len)
#define HOST_NAME_MAX 256
#endif
-/* in ctype.c, for kwset users */
-extern const unsigned char tolower_trans_tbl[256];
-
-/* Sane ctype - no locale, and works with signed chars */
-#undef isascii
-#undef isspace
-#undef isdigit
-#undef isalpha
-#undef isalnum
-#undef isprint
-#undef islower
-#undef isupper
-#undef tolower
-#undef toupper
-#undef iscntrl
-#undef ispunct
-#undef isxdigit
-
-extern const unsigned char sane_ctype[256];
-extern const signed char hexval_table[256];
-#define GIT_SPACE 0x01
-#define GIT_DIGIT 0x02
-#define GIT_ALPHA 0x04
-#define GIT_GLOB_SPECIAL 0x08
-#define GIT_REGEX_SPECIAL 0x10
-#define GIT_PATHSPEC_MAGIC 0x20
-#define GIT_CNTRL 0x40
-#define GIT_PUNCT 0x80
-#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
-#define isascii(x) (((x) & ~0x7f) == 0)
-#define isspace(x) sane_istest(x,GIT_SPACE)
-#define isdigit(x) sane_istest(x,GIT_DIGIT)
-#define isalpha(x) sane_istest(x,GIT_ALPHA)
-#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
-#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
-#define islower(x) sane_iscase(x, 1)
-#define isupper(x) sane_iscase(x, 0)
-#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
-#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
-#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
-#define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
- GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
-#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
-#define tolower(x) sane_case((unsigned char)(x), 0x20)
-#define toupper(x) sane_case((unsigned char)(x), 0)
-#define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
-
-static inline int sane_case(int x, int high)
-{
- if (sane_istest(x, GIT_ALPHA))
- x = (x & ~0x20) | high;
- return x;
-}
-
-static inline int sane_iscase(int x, int is_lower)
-{
- if (!sane_istest(x, GIT_ALPHA))
- return 0;
-
- if (is_lower)
- return (x & 0x20) != 0;
- else
- return (x & 0x20) == 0;
-}
+#include "sane-ctype.h"
/*
* Like skip_prefix, but compare case-insensitively. Note that the comparison
diff --git a/sane-ctype.h b/sane-ctype.h
new file mode 100644
index 0000000000..df06bcbe14
--- /dev/null
+++ b/sane-ctype.h
@@ -0,0 +1,69 @@
+#ifndef SANE_CTYPE_H
+#define SANE_CTYPE_H
+
+/* in ctype.c, for kwset users */
+extern const unsigned char tolower_trans_tbl[256];
+
+/* Sane ctype - no locale, and works with signed chars */
+#undef isascii
+#undef isspace
+#undef isdigit
+#undef isalpha
+#undef isalnum
+#undef isprint
+#undef islower
+#undef isupper
+#undef tolower
+#undef toupper
+#undef iscntrl
+#undef ispunct
+#undef isxdigit
+
+extern const unsigned char sane_ctype[256];
+extern const signed char hexval_table[256];
+#define GIT_SPACE 0x01
+#define GIT_DIGIT 0x02
+#define GIT_ALPHA 0x04
+#define GIT_GLOB_SPECIAL 0x08
+#define GIT_REGEX_SPECIAL 0x10
+#define GIT_PATHSPEC_MAGIC 0x20
+#define GIT_CNTRL 0x40
+#define GIT_PUNCT 0x80
+#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
+#define isascii(x) (((x) & ~0x7f) == 0)
+#define isspace(x) sane_istest(x,GIT_SPACE)
+#define isdigit(x) sane_istest(x,GIT_DIGIT)
+#define isalpha(x) sane_istest(x,GIT_ALPHA)
+#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
+#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
+#define islower(x) sane_iscase(x, 1)
+#define isupper(x) sane_iscase(x, 0)
+#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
+#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
+#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
+#define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
+ GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
+#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
+#define tolower(x) sane_case((unsigned char)(x), 0x20)
+#define toupper(x) sane_case((unsigned char)(x), 0)
+#define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
+
+static inline int sane_case(int x, int high)
+{
+ if (sane_istest(x, GIT_ALPHA))
+ x = (x & ~0x20) | high;
+ return x;
+}
+
+static inline int sane_iscase(int x, int is_lower)
+{
+ if (!sane_istest(x, GIT_ALPHA))
+ return 0;
+
+ if (is_lower)
+ return (x & 0x20) != 0;
+ else
+ return (x & 0x20) == 0;
+}
+
+#endif
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH v2 4/7] common.h: move non-compat specific macros and functions from git-compat-util.h
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
` (2 preceding siblings ...)
2023-05-23 19:29 ` [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros " Calvin Wan
@ 2023-05-23 19:29 ` Calvin Wan
2023-05-23 19:29 ` [PATCH v2 5/7] usage.h: move declarations for usage.c " Calvin Wan
` (3 subsequent siblings)
7 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:29 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
git-compat-util.h has grown to a large and difficult to read file partly
because its set of functionality is overloaded. Besides being a
compatibility file that ensures Git's operation across different
operating systems, it has also become a dumping ground for commonly used
macros and static inline functions.
This commit separates out macros and static inline functions that are
compatible across operating systems into common.h. common.h is
reincluded back into git-compat-util.h so that other files do not also
have to include it. The eventual goal is for common.h to be separated
out into more reasonable boundaries, but for now this should be a good
first step towards that goal.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
common.h | 410 ++++++++++++++++++++++++++++++++++++++++++++++
git-compat-util.h | 402 +--------------------------------------------
2 files changed, 412 insertions(+), 400 deletions(-)
create mode 100644 common.h
diff --git a/common.h b/common.h
new file mode 100644
index 0000000000..0f164c1071
--- /dev/null
+++ b/common.h
@@ -0,0 +1,410 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include "git-compat-util.h"
+#include "wrapper.h"
+
+/*
+ * ARRAY_SIZE - get the number of elements in a visible array
+ * @x: the array whose size you want.
+ *
+ * This does not work on pointers, or arrays declared as [], or
+ * function parameters. With correct compiler support, such usage
+ * will cause a build error (see the build_assert_or_zero macro).
+ */
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))
+
+#define bitsizeof(x) (CHAR_BIT * sizeof(x))
+
+#define maximum_signed_value_of_type(a) \
+ (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
+
+#define maximum_unsigned_value_of_type(a) \
+ (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
+
+/*
+ * Signed integer overflow is undefined in C, so here's a helper macro
+ * to detect if the sum of two integers will overflow.
+ *
+ * Requires: a >= 0, typeof(a) equals typeof(b)
+ */
+#define signed_add_overflows(a, b) \
+ ((b) > maximum_signed_value_of_type(a) - (a))
+
+#define unsigned_add_overflows(a, b) \
+ ((b) > maximum_unsigned_value_of_type(a) - (a))
+
+/*
+ * Returns true if the multiplication of "a" and "b" will
+ * overflow. The types of "a" and "b" must match and must be unsigned.
+ * Note that this macro evaluates "a" twice!
+ */
+#define unsigned_mult_overflows(a, b) \
+ ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
+
+/*
+ * Returns true if the left shift of "a" by "shift" bits will
+ * overflow. The type of "a" must be unsigned.
+ */
+#define unsigned_left_shift_overflows(a, shift) \
+ ((shift) < bitsizeof(a) && \
+ (a) > maximum_unsigned_value_of_type(a) >> (shift))
+
+#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
+#define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
+
+#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
+
+/* Approximation of the length of the decimal representation of this type. */
+#define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
+
+/*
+ * If the string "str" begins with the string found in "prefix", return 1.
+ * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
+ * the string right after the prefix).
+ *
+ * Otherwise, return 0 and leave "out" untouched.
+ *
+ * Examples:
+ *
+ * [extract branch name, fail if not a branch]
+ * if (!skip_prefix(ref, "refs/heads/", &branch)
+ * return -1;
+ *
+ * [skip prefix if present, otherwise use whole string]
+ * skip_prefix(name, "refs/heads/", &name);
+ */
+static inline int skip_prefix(const char *str, const char *prefix,
+ const char **out)
+{
+ do {
+ if (!*prefix) {
+ *out = str;
+ return 1;
+ }
+ } while (*str++ == *prefix++);
+ return 0;
+}
+
+/*
+ * Like skip_prefix, but promises never to read past "len" bytes of the input
+ * buffer, and returns the remaining number of bytes in "out" via "outlen".
+ */
+static inline int skip_prefix_mem(const char *buf, size_t len,
+ const char *prefix,
+ const char **out, size_t *outlen)
+{
+ size_t prefix_len = strlen(prefix);
+ if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
+ *out = buf + prefix_len;
+ *outlen = len - prefix_len;
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * If buf ends with suffix, return 1 and subtract the length of the suffix
+ * from *len. Otherwise, return 0 and leave *len untouched.
+ */
+static inline int strip_suffix_mem(const char *buf, size_t *len,
+ const char *suffix)
+{
+ size_t suflen = strlen(suffix);
+ if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
+ return 0;
+ *len -= suflen;
+ return 1;
+}
+
+/*
+ * If str ends with suffix, return 1 and set *len to the size of the string
+ * without the suffix. Otherwise, return 0 and set *len to the size of the
+ * string.
+ *
+ * Note that we do _not_ NUL-terminate str to the new length.
+ */
+static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
+{
+ *len = strlen(str);
+ return strip_suffix_mem(str, len, suffix);
+}
+
+#define SWAP(a, b) do { \
+ void *_swap_a_ptr = &(a); \
+ void *_swap_b_ptr = &(b); \
+ unsigned char _swap_buffer[sizeof(a)]; \
+ memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
+ memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
+ BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
+ memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
+} while (0)
+
+static inline size_t st_add(size_t a, size_t b)
+{
+ if (unsigned_add_overflows(a, b))
+ die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
+ (uintmax_t)a, (uintmax_t)b);
+ return a + b;
+}
+#define st_add3(a,b,c) st_add(st_add((a),(b)),(c))
+#define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d))
+
+static inline size_t st_mult(size_t a, size_t b)
+{
+ if (unsigned_mult_overflows(a, b))
+ die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
+ (uintmax_t)a, (uintmax_t)b);
+ return a * b;
+}
+
+static inline size_t st_sub(size_t a, size_t b)
+{
+ if (a < b)
+ die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
+ (uintmax_t)a, (uintmax_t)b);
+ return a - b;
+}
+
+static inline size_t st_left_shift(size_t a, unsigned shift)
+{
+ if (unsigned_left_shift_overflows(a, shift))
+ die("size_t overflow: %"PRIuMAX" << %u",
+ (uintmax_t)a, shift);
+ return a << shift;
+}
+
+static inline unsigned long cast_size_t_to_ulong(size_t a)
+{
+ if (a != (unsigned long)a)
+ die("object too large to read on this platform: %"
+ PRIuMAX" is cut off to %lu",
+ (uintmax_t)a, (unsigned long)a);
+ return (unsigned long)a;
+}
+
+static inline int cast_size_t_to_int(size_t a)
+{
+ if (a > INT_MAX)
+ die("number too large to represent as int on this platform: %"PRIuMAX,
+ (uintmax_t)a);
+ return (int)a;
+}
+
+
+/*
+ * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
+ * that ptr is used twice, so don't pass e.g. ptr++.
+ */
+#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
+
+#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
+#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
+#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
+
+#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
+ BARF_UNLESS_COPYABLE((dst), (src)))
+static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
+{
+ if (n)
+ memcpy(dst, src, st_mult(size, n));
+}
+
+#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
+ BARF_UNLESS_COPYABLE((dst), (src)))
+static inline void move_array(void *dst, const void *src, size_t n, size_t size)
+{
+ if (n)
+ memmove(dst, src, st_mult(size, n));
+}
+
+#define DUP_ARRAY(dst, src, n) do { \
+ size_t dup_array_n_ = (n); \
+ COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \
+} while (0)
+
+/*
+ * These functions help you allocate structs with flex arrays, and copy
+ * the data directly into the array. For example, if you had:
+ *
+ * struct foo {
+ * int bar;
+ * char name[FLEX_ARRAY];
+ * };
+ *
+ * you can do:
+ *
+ * struct foo *f;
+ * FLEX_ALLOC_MEM(f, name, src, len);
+ *
+ * to allocate a "foo" with the contents of "src" in the "name" field.
+ * The resulting struct is automatically zero'd, and the flex-array field
+ * is NUL-terminated (whether the incoming src buffer was or not).
+ *
+ * The FLEXPTR_* variants operate on structs that don't use flex-arrays,
+ * but do want to store a pointer to some extra data in the same allocated
+ * block. For example, if you have:
+ *
+ * struct foo {
+ * char *name;
+ * int bar;
+ * };
+ *
+ * you can do:
+ *
+ * struct foo *f;
+ * FLEXPTR_ALLOC_STR(f, name, src);
+ *
+ * and "name" will point to a block of memory after the struct, which will be
+ * freed along with the struct (but the pointer can be repointed anywhere).
+ *
+ * The *_STR variants accept a string parameter rather than a ptr/len
+ * combination.
+ *
+ * Note that these macros will evaluate the first parameter multiple
+ * times, and it must be assignable as an lvalue.
+ */
+#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
+} while (0)
+#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
+ size_t flex_array_len_ = (len); \
+ (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
+ memcpy((x) + 1, (buf), flex_array_len_); \
+ (x)->ptrname = (void *)((x)+1); \
+} while(0)
+#define FLEX_ALLOC_STR(x, flexname, str) \
+ FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
+#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
+ FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
+
+static inline char *xstrdup_or_null(const char *str)
+{
+ return str ? xstrdup(str) : NULL;
+}
+
+static inline size_t xsize_t(off_t len)
+{
+ if (len < 0 || (uintmax_t) len > SIZE_MAX)
+ die("Cannot handle files this big");
+ return (size_t) len;
+}
+
+/*
+ * Like skip_prefix, but compare case-insensitively. Note that the comparison
+ * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
+ * locale-specific conversions).
+ */
+static inline int skip_iprefix(const char *str, const char *prefix,
+ const char **out)
+{
+ do {
+ if (!*prefix) {
+ *out = str;
+ return 1;
+ }
+ } while (tolower(*str++) == tolower(*prefix++));
+ return 0;
+}
+
+/*
+ * Like skip_prefix_mem, but compare case-insensitively. Note that the
+ * comparison is done via tolower(), so it is strictly ASCII (no multi-byte
+ * characters or locale-specific conversions).
+ */
+static inline int skip_iprefix_mem(const char *buf, size_t len,
+ const char *prefix,
+ const char **out, size_t *outlen)
+{
+ do {
+ if (!*prefix) {
+ *out = buf;
+ *outlen = len;
+ return 1;
+ }
+ } while (len-- > 0 && tolower(*buf++) == tolower(*prefix++));
+ return 0;
+}
+
+static inline int strtoul_ui(char const *s, int base, unsigned int *result)
+{
+ unsigned long ul;
+ char *p;
+
+ errno = 0;
+ /* negative values would be accepted by strtoul */
+ if (strchr(s, '-'))
+ return -1;
+ ul = strtoul(s, &p, base);
+ if (errno || *p || p == s || (unsigned int) ul != ul)
+ return -1;
+ *result = ul;
+ return 0;
+}
+
+static inline int strtol_i(char const *s, int base, int *result)
+{
+ long ul;
+ char *p;
+
+ errno = 0;
+ ul = strtol(s, &p, base);
+ if (errno || *p || p == s || (int) ul != ul)
+ return -1;
+ *result = ul;
+ return 0;
+}
+
+static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
+ size_t nmatch, regmatch_t pmatch[], int eflags)
+{
+ assert(nmatch > 0 && pmatch);
+ pmatch[0].rm_so = 0;
+ pmatch[0].rm_eo = size;
+ return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
+}
+
+/*
+ * Our code often opens a path to an optional file, to work on its
+ * contents when we can successfully open it. We can ignore a failure
+ * to open if such an optional file does not exist, but we do want to
+ * report a failure in opening for other reasons (e.g. we got an I/O
+ * error, or the file is there, but we lack the permission to open).
+ *
+ * Call this function after seeing an error from open() or fopen() to
+ * see if the errno indicates a missing file that we can safely ignore.
+ */
+static inline int is_missing_file_error(int errno_)
+{
+ return (errno_ == ENOENT || errno_ == ENOTDIR);
+}
+
+/*
+ * container_of - Get the address of an object containing a field.
+ *
+ * @ptr: pointer to the field.
+ * @type: type of the object.
+ * @member: name of the field within the object.
+ */
+#define container_of(ptr, type, member) \
+ ((type *) ((char *)(ptr) - offsetof(type, member)))
+
+/*
+ * helper function for `container_of_or_null' to avoid multiple
+ * evaluation of @ptr
+ */
+static inline void *container_of_or_null_offset(void *ptr, size_t offset)
+{
+ return ptr ? (char *)ptr - offset : NULL;
+}
+
+/*
+ * like `container_of', but allows returned value to be NULL
+ */
+#define container_of_or_null(ptr, type, member) \
+ (type *)container_of_or_null_offset(ptr, offsetof(type, member))
+
+
+#endif
diff --git a/git-compat-util.h b/git-compat-util.h
index 9d3c21acbb..f73c8727d1 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -106,51 +106,6 @@ struct strbuf;
BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \
sizeof(*(dst)) == sizeof(*(src)))
#endif
-/*
- * ARRAY_SIZE - get the number of elements in a visible array
- * @x: the array whose size you want.
- *
- * This does not work on pointers, or arrays declared as [], or
- * function parameters. With correct compiler support, such usage
- * will cause a build error (see the build_assert_or_zero macro).
- */
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))
-
-#define bitsizeof(x) (CHAR_BIT * sizeof(x))
-
-#define maximum_signed_value_of_type(a) \
- (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
-
-#define maximum_unsigned_value_of_type(a) \
- (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
-
-/*
- * Signed integer overflow is undefined in C, so here's a helper macro
- * to detect if the sum of two integers will overflow.
- *
- * Requires: a >= 0, typeof(a) equals typeof(b)
- */
-#define signed_add_overflows(a, b) \
- ((b) > maximum_signed_value_of_type(a) - (a))
-
-#define unsigned_add_overflows(a, b) \
- ((b) > maximum_unsigned_value_of_type(a) - (a))
-
-/*
- * Returns true if the multiplication of "a" and "b" will
- * overflow. The types of "a" and "b" must match and must be unsigned.
- * Note that this macro evaluates "a" twice!
- */
-#define unsigned_mult_overflows(a, b) \
- ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
-
-/*
- * Returns true if the left shift of "a" by "shift" bits will
- * overflow. The type of "a" must be unsigned.
- */
-#define unsigned_left_shift_overflows(a, shift) \
- ((shift) < bitsizeof(a) && \
- (a) > maximum_unsigned_value_of_type(a) >> (shift))
#ifdef __GNUC__
#define TYPEOF(x) (__typeof__(x))
@@ -158,14 +113,6 @@ struct strbuf;
#define TYPEOF(x)
#endif
-#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
-#define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
-
-#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
-
-/* Approximation of the length of the decimal representation of this type. */
-#define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
-
#ifdef __MINGW64__
#define _POSIX_C_SOURCE 1
#elif defined(__sun__)
@@ -677,88 +624,6 @@ void set_warn_routine(report_fn routine);
report_fn get_warn_routine(void);
void set_die_is_recursing_routine(int (*routine)(void));
-/*
- * If the string "str" begins with the string found in "prefix", return 1.
- * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
- * the string right after the prefix).
- *
- * Otherwise, return 0 and leave "out" untouched.
- *
- * Examples:
- *
- * [extract branch name, fail if not a branch]
- * if (!skip_prefix(ref, "refs/heads/", &branch)
- * return -1;
- *
- * [skip prefix if present, otherwise use whole string]
- * skip_prefix(name, "refs/heads/", &name);
- */
-static inline int skip_prefix(const char *str, const char *prefix,
- const char **out)
-{
- do {
- if (!*prefix) {
- *out = str;
- return 1;
- }
- } while (*str++ == *prefix++);
- return 0;
-}
-
-/*
- * Like skip_prefix, but promises never to read past "len" bytes of the input
- * buffer, and returns the remaining number of bytes in "out" via "outlen".
- */
-static inline int skip_prefix_mem(const char *buf, size_t len,
- const char *prefix,
- const char **out, size_t *outlen)
-{
- size_t prefix_len = strlen(prefix);
- if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
- *out = buf + prefix_len;
- *outlen = len - prefix_len;
- return 1;
- }
- return 0;
-}
-
-/*
- * If buf ends with suffix, return 1 and subtract the length of the suffix
- * from *len. Otherwise, return 0 and leave *len untouched.
- */
-static inline int strip_suffix_mem(const char *buf, size_t *len,
- const char *suffix)
-{
- size_t suflen = strlen(suffix);
- if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
- return 0;
- *len -= suflen;
- return 1;
-}
-
-/*
- * If str ends with suffix, return 1 and set *len to the size of the string
- * without the suffix. Otherwise, return 0 and set *len to the size of the
- * string.
- *
- * Note that we do _not_ NUL-terminate str to the new length.
- */
-static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
-{
- *len = strlen(str);
- return strip_suffix_mem(str, len, suffix);
-}
-
-#define SWAP(a, b) do { \
- void *_swap_a_ptr = &(a); \
- void *_swap_b_ptr = &(b); \
- unsigned char _swap_buffer[sizeof(a)]; \
- memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
- memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
- BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
- memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
-} while (0)
-
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
#ifndef PROT_READ
@@ -964,57 +829,6 @@ const char *inet_ntop(int af, const void *src, char *dst, size_t size);
int git_atexit(void (*handler)(void));
#endif
-static inline size_t st_add(size_t a, size_t b)
-{
- if (unsigned_add_overflows(a, b))
- die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
- (uintmax_t)a, (uintmax_t)b);
- return a + b;
-}
-#define st_add3(a,b,c) st_add(st_add((a),(b)),(c))
-#define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d))
-
-static inline size_t st_mult(size_t a, size_t b)
-{
- if (unsigned_mult_overflows(a, b))
- die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
- (uintmax_t)a, (uintmax_t)b);
- return a * b;
-}
-
-static inline size_t st_sub(size_t a, size_t b)
-{
- if (a < b)
- die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
- (uintmax_t)a, (uintmax_t)b);
- return a - b;
-}
-
-static inline size_t st_left_shift(size_t a, unsigned shift)
-{
- if (unsigned_left_shift_overflows(a, shift))
- die("size_t overflow: %"PRIuMAX" << %u",
- (uintmax_t)a, shift);
- return a << shift;
-}
-
-static inline unsigned long cast_size_t_to_ulong(size_t a)
-{
- if (a != (unsigned long)a)
- die("object too large to read on this platform: %"
- PRIuMAX" is cut off to %lu",
- (uintmax_t)a, (unsigned long)a);
- return (unsigned long)a;
-}
-
-static inline int cast_size_t_to_int(size_t a)
-{
- if (a > INT_MAX)
- die("number too large to represent as int on this platform: %"PRIuMAX,
- (uintmax_t)a);
- return (int)a;
-}
-
/*
* Limit size of IO chunks, because huge chunks only cause pain. OS X
* 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
@@ -1046,177 +860,12 @@ static inline int cast_size_t_to_int(size_t a)
# define xalloca_free(p) (free(p))
#endif
-/*
- * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
- * that ptr is used twice, so don't pass e.g. ptr++.
- */
-#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
-
-#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
-#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
-#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
-
-#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
- BARF_UNLESS_COPYABLE((dst), (src)))
-static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
-{
- if (n)
- memcpy(dst, src, st_mult(size, n));
-}
-
-#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
- BARF_UNLESS_COPYABLE((dst), (src)))
-static inline void move_array(void *dst, const void *src, size_t n, size_t size)
-{
- if (n)
- memmove(dst, src, st_mult(size, n));
-}
-
-#define DUP_ARRAY(dst, src, n) do { \
- size_t dup_array_n_ = (n); \
- COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \
-} while (0)
-
-/*
- * These functions help you allocate structs with flex arrays, and copy
- * the data directly into the array. For example, if you had:
- *
- * struct foo {
- * int bar;
- * char name[FLEX_ARRAY];
- * };
- *
- * you can do:
- *
- * struct foo *f;
- * FLEX_ALLOC_MEM(f, name, src, len);
- *
- * to allocate a "foo" with the contents of "src" in the "name" field.
- * The resulting struct is automatically zero'd, and the flex-array field
- * is NUL-terminated (whether the incoming src buffer was or not).
- *
- * The FLEXPTR_* variants operate on structs that don't use flex-arrays,
- * but do want to store a pointer to some extra data in the same allocated
- * block. For example, if you have:
- *
- * struct foo {
- * char *name;
- * int bar;
- * };
- *
- * you can do:
- *
- * struct foo *f;
- * FLEXPTR_ALLOC_STR(f, name, src);
- *
- * and "name" will point to a block of memory after the struct, which will be
- * freed along with the struct (but the pointer can be repointed anywhere).
- *
- * The *_STR variants accept a string parameter rather than a ptr/len
- * combination.
- *
- * Note that these macros will evaluate the first parameter multiple
- * times, and it must be assignable as an lvalue.
- */
-#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
- size_t flex_array_len_ = (len); \
- (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
- memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
-} while (0)
-#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
- size_t flex_array_len_ = (len); \
- (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
- memcpy((x) + 1, (buf), flex_array_len_); \
- (x)->ptrname = (void *)((x)+1); \
-} while(0)
-#define FLEX_ALLOC_STR(x, flexname, str) \
- FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
-#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
- FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
-
-static inline char *xstrdup_or_null(const char *str)
-{
- return str ? xstrdup(str) : NULL;
-}
-
-static inline size_t xsize_t(off_t len)
-{
- if (len < 0 || (uintmax_t) len > SIZE_MAX)
- die("Cannot handle files this big");
- return (size_t) len;
-}
-
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
#include "sane-ctype.h"
-/*
- * Like skip_prefix, but compare case-insensitively. Note that the comparison
- * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
- * locale-specific conversions).
- */
-static inline int skip_iprefix(const char *str, const char *prefix,
- const char **out)
-{
- do {
- if (!*prefix) {
- *out = str;
- return 1;
- }
- } while (tolower(*str++) == tolower(*prefix++));
- return 0;
-}
-
-/*
- * Like skip_prefix_mem, but compare case-insensitively. Note that the
- * comparison is done via tolower(), so it is strictly ASCII (no multi-byte
- * characters or locale-specific conversions).
- */
-static inline int skip_iprefix_mem(const char *buf, size_t len,
- const char *prefix,
- const char **out, size_t *outlen)
-{
- do {
- if (!*prefix) {
- *out = buf;
- *outlen = len;
- return 1;
- }
- } while (len-- > 0 && tolower(*buf++) == tolower(*prefix++));
- return 0;
-}
-
-static inline int strtoul_ui(char const *s, int base, unsigned int *result)
-{
- unsigned long ul;
- char *p;
-
- errno = 0;
- /* negative values would be accepted by strtoul */
- if (strchr(s, '-'))
- return -1;
- ul = strtoul(s, &p, base);
- if (errno || *p || p == s || (unsigned int) ul != ul)
- return -1;
- *result = ul;
- return 0;
-}
-
-static inline int strtol_i(char const *s, int base, int *result)
-{
- long ul;
- char *p;
-
- errno = 0;
- ul = strtol(s, &p, base);
- if (errno || *p || p == s || (int) ul != ul)
- return -1;
- *result = ul;
- return 0;
-}
-
void git_stable_qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
#ifdef INTERNAL_QSORT
@@ -1249,15 +898,6 @@ int git_qsort_s(void *base, size_t nmemb, size_t size,
#error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
#endif
-static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
- size_t nmatch, regmatch_t pmatch[], int eflags)
-{
- assert(nmatch > 0 && pmatch);
- pmatch[0].rm_so = 0;
- pmatch[0].rm_eo = size;
- return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
-}
-
#ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
int git_regcomp(regex_t *preg, const char *pattern, int cflags);
#define regcomp git_regcomp
@@ -1366,21 +1006,6 @@ int git_access(const char *path, int mode);
# endif
#endif
-/*
- * Our code often opens a path to an optional file, to work on its
- * contents when we can successfully open it. We can ignore a failure
- * to open if such an optional file does not exist, but we do want to
- * report a failure in opening for other reasons (e.g. we got an I/O
- * error, or the file is there, but we lack the permission to open).
- *
- * Call this function after seeing an error from open() or fopen() to
- * see if the errno indicates a missing file that we can safely ignore.
- */
-static inline int is_missing_file_error(int errno_)
-{
- return (errno_ == ENOENT || errno_ == ENOTDIR);
-}
-
int cmd_main(int, const char **);
/*
@@ -1422,37 +1047,14 @@ int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
uLong *sourceLen);
#endif
+#include "common.h"
+
/*
* This include must come after system headers, since it introduces macros that
* replace system names.
*/
#include "banned.h"
-/*
- * container_of - Get the address of an object containing a field.
- *
- * @ptr: pointer to the field.
- * @type: type of the object.
- * @member: name of the field within the object.
- */
-#define container_of(ptr, type, member) \
- ((type *) ((char *)(ptr) - offsetof(type, member)))
-
-/*
- * helper function for `container_of_or_null' to avoid multiple
- * evaluation of @ptr
- */
-static inline void *container_of_or_null_offset(void *ptr, size_t offset)
-{
- return ptr ? (char *)ptr - offset : NULL;
-}
-
-/*
- * like `container_of', but allows returned value to be NULL
- */
-#define container_of_or_null(ptr, type, member) \
- (type *)container_of_or_null_offset(ptr, offsetof(type, member))
-
/*
* like offsetof(), but takes a pointer to a variable of type which
* contains @member, instead of a specified type.
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH v2 5/7] usage.h: move declarations for usage.c functions from git-compat-util.h
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
` (3 preceding siblings ...)
2023-05-23 19:29 ` [PATCH v2 4/7] common.h: move non-compat specific macros and functions " Calvin Wan
@ 2023-05-23 19:29 ` Calvin Wan
2023-05-23 19:29 ` [PATCH v2 6/7] treewide: remove unnecessary includes for wrapper.h Calvin Wan
` (2 subsequent siblings)
7 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:29 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
common.h | 1 +
git-compat-util.h | 55 -------------------------------------------
usage.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 55 deletions(-)
create mode 100644 usage.h
diff --git a/common.h b/common.h
index 0f164c1071..e3b673847d 100644
--- a/common.h
+++ b/common.h
@@ -3,6 +3,7 @@
#include "git-compat-util.h"
#include "wrapper.h"
+#include "usage.h"
/*
* ARRAY_SIZE - get the number of elements in a visible array
diff --git a/git-compat-util.h b/git-compat-util.h
index f73c8727d1..481dac22b0 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -572,20 +572,6 @@ static inline int git_has_dir_sep(const char *path)
#include "compat/bswap.h"
-#include "wrapper.h"
-
-/* General helper functions */
-NORETURN void usage(const char *err);
-NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
-NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
-NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int die_message(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
-int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
-void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
-
#ifndef NO_OPENSSL
#ifdef APPLE_COMMON_CRYPTO
#include "compat/apple-common-crypto.h"
@@ -600,30 +586,6 @@ void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
#include <openssl/rand.h>
#endif
-/*
- * Let callers be aware of the constant return value; this can help
- * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
- * because other compilers may be confused by this.
- */
-#if defined(__GNUC__)
-static inline int const_error(void)
-{
- return -1;
-}
-#define error(...) (error(__VA_ARGS__), const_error())
-#define error_errno(...) (error_errno(__VA_ARGS__), const_error())
-#endif
-
-typedef void (*report_fn)(const char *, va_list params);
-
-void set_die_routine(NORETURN_PTR report_fn routine);
-report_fn get_die_message_routine(void);
-void set_error_routine(report_fn routine);
-report_fn get_error_routine(void);
-void set_warn_routine(report_fn routine);
-report_fn get_warn_routine(void);
-void set_die_is_recursing_routine(int (*routine)(void));
-
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
#ifndef PROT_READ
@@ -942,23 +904,6 @@ int git_regcomp(regex_t *preg, const char *pattern, int cflags);
#endif
#endif
-/* usage.c: only to be used for testing BUG() implementation (see test-tool) */
-extern int BUG_exit_code;
-
-/* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */
-extern int bug_called_must_BUG;
-
-__attribute__((format (printf, 3, 4))) NORETURN
-void BUG_fl(const char *file, int line, const char *fmt, ...);
-#define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
-__attribute__((format (printf, 3, 4)))
-void bug_fl(const char *file, int line, const char *fmt, ...);
-#define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__)
-#define BUG_if_bug(...) do { \
- if (bug_called_must_BUG) \
- BUG_fl(__FILE__, __LINE__, __VA_ARGS__); \
-} while (0)
-
#ifndef FSYNC_METHOD_DEFAULT
#ifdef __APPLE__
#define FSYNC_METHOD_DEFAULT FSYNC_METHOD_WRITEOUT_ONLY
diff --git a/usage.h b/usage.h
new file mode 100644
index 0000000000..770076e684
--- /dev/null
+++ b/usage.h
@@ -0,0 +1,60 @@
+#ifndef USAGE_H
+#define USAGE_H
+
+#include "git-compat-util.h"
+
+/* General helper functions */
+NORETURN void usage(const char *err);
+NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
+NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
+NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int die_message(const char *err, ...) __attribute__((format (printf, 1, 2)));
+int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+
+void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
+void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+
+/*
+ * Let callers be aware of the constant return value; this can help
+ * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
+ * because other compilers may be confused by this.
+ */
+#if defined(__GNUC__)
+static inline int const_error(void)
+{
+ return -1;
+}
+#define error(...) (error(__VA_ARGS__), const_error())
+#define error_errno(...) (error_errno(__VA_ARGS__), const_error())
+#endif
+
+typedef void (*report_fn)(const char *, va_list params);
+
+void set_die_routine(NORETURN_PTR report_fn routine);
+report_fn get_die_message_routine(void);
+void set_error_routine(report_fn routine);
+report_fn get_error_routine(void);
+void set_warn_routine(report_fn routine);
+report_fn get_warn_routine(void);
+void set_die_is_recursing_routine(int (*routine)(void));
+
+/* usage.c: only to be used for testing BUG() implementation (see test-tool) */
+extern int BUG_exit_code;
+
+/* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */
+extern int bug_called_must_BUG;
+
+__attribute__((format (printf, 3, 4))) NORETURN
+void BUG_fl(const char *file, int line, const char *fmt, ...);
+#define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
+__attribute__((format (printf, 3, 4)))
+void bug_fl(const char *file, int line, const char *fmt, ...);
+#define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__)
+#define BUG_if_bug(...) do { \
+ if (bug_called_must_BUG) \
+ BUG_fl(__FILE__, __LINE__, __VA_ARGS__); \
+} while (0)
+
+#endif /* USAGE_H */
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH v2 6/7] treewide: remove unnecessary includes for wrapper.h
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
` (4 preceding siblings ...)
2023-05-23 19:29 ` [PATCH v2 5/7] usage.h: move declarations for usage.c " Calvin Wan
@ 2023-05-23 19:29 ` Calvin Wan
2023-05-23 19:29 ` [PATCH v2 7/7] common: move alloc macros to common.h Calvin Wan
2023-05-26 21:03 ` [PATCH v2 0/7] git-compat-util cleanups Jonathan Tan
7 siblings, 0 replies; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:29 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
apply.c | 1 -
builtin/am.c | 1 -
builtin/bisect.c | 1 -
builtin/branch.c | 1 -
builtin/bugreport.c | 1 -
builtin/clone.c | 1 -
builtin/config.c | 1 -
builtin/credential-cache.c | 1 -
builtin/difftool.c | 1 -
builtin/fast-import.c | 1 -
builtin/fmt-merge-msg.c | 1 -
builtin/gc.c | 1 -
builtin/get-tar-commit-id.c | 1 -
builtin/index-pack.c | 1 -
builtin/init-db.c | 1 -
builtin/merge.c | 1 -
builtin/pack-objects.c | 1 -
builtin/rebase.c | 1 -
builtin/receive-pack.c | 1 -
builtin/rerere.c | 1 -
builtin/unpack-file.c | 1 -
builtin/worktree.c | 1 -
bulk-checkin.c | 1 -
combine-diff.c | 1 -
commit-graph.c | 1 -
compat/terminal.c | 1 -
config.c | 1 -
convert.c | 1 -
copy.c | 1 -
csum-file.c | 1 -
daemon.c | 1 -
diff.c | 1 -
dir.c | 1 -
editor.c | 1 -
entry.c | 1 -
environment.c | 1 -
fetch-pack.c | 1 -
gpg-interface.c | 1 -
grep.c | 1 -
http-backend.c | 1 -
imap-send.c | 1 -
merge-ll.c | 1 -
merge-recursive.c | 1 -
notes-merge.c | 1 -
object-file.c | 1 -
pack-write.c | 1 -
packfile.c | 1 -
parallel-checkout.c | 1 -
path.c | 1 -
pkt-line.c | 1 -
read-cache.c | 1 -
rebase-interactive.c | 1 -
refs.c | 1 -
rerere.c | 1 -
send-pack.c | 1 -
sequencer.c | 1 -
server-info.c | 1 -
setup.c | 1 -
shallow.c | 1 -
strbuf.c | 1 -
streaming.c | 1 -
t/helper/test-delta.c | 1 -
t/helper/test-fsmonitor-client.c | 1 -
t/helper/test-read-cache.c | 1 -
tag.c | 1 -
tempfile.c | 1 -
trace.c | 1 -
transport-helper.c | 1 -
transport.c | 1 -
upload-pack.c | 1 -
usage.c | 1 -
worktree.c | 1 -
wrapper.c | 1 -
write-or-die.c | 1 -
74 files changed, 74 deletions(-)
diff --git a/apply.c b/apply.c
index 8bd0109fcc..99c2a91de7 100644
--- a/apply.c
+++ b/apply.c
@@ -37,7 +37,6 @@
#include "symlinks.h"
#include "wildmatch.h"
#include "ws.h"
-#include "wrapper.h"
struct gitdiff_data {
struct strbuf *root;
diff --git a/builtin/am.c b/builtin/am.c
index 5fab159599..dcb89439b1 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -44,7 +44,6 @@
#include "path.h"
#include "repository.h"
#include "pretty.h"
-#include "wrapper.h"
/**
* Returns the length of the first line of msg.
diff --git a/builtin/bisect.c b/builtin/bisect.c
index 6478df3489..65478ef40f 100644
--- a/builtin/bisect.c
+++ b/builtin/bisect.c
@@ -15,7 +15,6 @@
#include "prompt.h"
#include "quote.h"
#include "revision.h"
-#include "wrapper.h"
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
diff --git a/builtin/branch.c b/builtin/branch.c
index 6249fa1809..ff8a875c03 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -28,7 +28,6 @@
#include "worktree.h"
#include "help.h"
#include "commit-reach.h"
-#include "wrapper.h"
static const char * const builtin_branch_usage[] = {
N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"),
diff --git a/builtin/bugreport.c b/builtin/bugreport.c
index daf6c23657..d2ae5c305d 100644
--- a/builtin/bugreport.c
+++ b/builtin/bugreport.c
@@ -11,7 +11,6 @@
#include "diagnose.h"
#include "object-file.h"
#include "setup.h"
-#include "wrapper.h"
static void get_system_info(struct strbuf *sys_info)
{
diff --git a/builtin/clone.c b/builtin/clone.c
index 687a686269..3fc0e5d7c0 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -45,7 +45,6 @@
#include "hook.h"
#include "bundle.h"
#include "bundle-uri.h"
-#include "wrapper.h"
/*
* Overall FIXMEs:
diff --git a/builtin/config.c b/builtin/config.c
index 30f6d9fe67..c01f136a2b 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -14,7 +14,6 @@
#include "setup.h"
#include "strbuf.h"
#include "worktree.h"
-#include "wrapper.h"
static const char *const builtin_config_usage[] = {
N_("git config [<options>]"),
diff --git a/builtin/credential-cache.c b/builtin/credential-cache.c
index ff3a47badb..43b9d0e5b1 100644
--- a/builtin/credential-cache.c
+++ b/builtin/credential-cache.c
@@ -3,7 +3,6 @@
#include "parse-options.h"
#include "path.h"
#include "strbuf.h"
-#include "wrapper.h"
#include "write-or-die.h"
#ifndef NO_UNIX_SOCKETS
diff --git a/builtin/difftool.c b/builtin/difftool.c
index e2c9ab7d5d..aeb0f92409 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -32,7 +32,6 @@
#include "dir.h"
#include "entry.h"
#include "setup.h"
-#include "wrapper.h"
static int trust_exit_code;
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index 2ee19c7373..4dbb10aff3 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -26,7 +26,6 @@
#include "commit-reach.h"
#include "khash.h"
#include "date.h"
-#include "wrapper.h"
#define PACK_ID_BITS 16
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index cc81241642..0f9855b680 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -3,7 +3,6 @@
#include "fmt-merge-msg.h"
#include "gettext.h"
#include "parse-options.h"
-#include "wrapper.h"
static const char * const fmt_merge_msg_usage[] = {
N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
diff --git a/builtin/gc.c b/builtin/gc.c
index 91eec7703a..19d73067aa 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -41,7 +41,6 @@
#include "hook.h"
#include "setup.h"
#include "trace2.h"
-#include "wrapper.h"
#define FAILED_RUN "failed to run %s"
diff --git a/builtin/get-tar-commit-id.c b/builtin/get-tar-commit-id.c
index 9303e386cc..20d0dfe9cf 100644
--- a/builtin/get-tar-commit-id.c
+++ b/builtin/get-tar-commit-id.c
@@ -5,7 +5,6 @@
#include "commit.h"
#include "tar.h"
#include "quote.h"
-#include "wrapper.h"
static const char builtin_get_tar_commit_id_usage[] =
"git get-tar-commit-id";
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 820860265d..f58f9db69d 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -25,7 +25,6 @@
#include "replace-object.h"
#include "promisor-remote.h"
#include "setup.h"
-#include "wrapper.h"
static const char index_pack_usage[] =
"git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--[no-]rev-index] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 0d8bd4d721..cb727c826f 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -13,7 +13,6 @@
#include "path.h"
#include "setup.h"
#include "strbuf.h"
-#include "wrapper.h"
static int guess_repository_type(const char *git_dir)
{
diff --git a/builtin/merge.c b/builtin/merge.c
index 41fe0d8e5e..2f2f0a0b4c 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -53,7 +53,6 @@
#include "commit-reach.h"
#include "wt-status.h"
#include "commit-graph.h"
-#include "wrapper.h"
#define DEFAULT_TWOHEAD (1<<0)
#define DEFAULT_OCTOPUS (1<<1)
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index af9352b228..6409c07c7d 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -43,7 +43,6 @@
#include "promisor-remote.h"
#include "pack-mtimes.h"
#include "parse-options.h"
-#include "wrapper.h"
/*
* Objects we are going to pack are collected in the `to_pack` structure.
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 555c5d3f02..6c23e7b25c 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -37,7 +37,6 @@
#include "reset.h"
#include "trace2.h"
#include "hook.h"
-#include "wrapper.h"
static char const * const builtin_rebase_usage[] = {
N_("git rebase [-i] [options] [--exec <cmd>] "
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 2b2faa5d18..2d7f0355dc 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -40,7 +40,6 @@
#include "worktree.h"
#include "shallow.h"
#include "parse-options.h"
-#include "wrapper.h"
static const char * const receive_pack_usage[] = {
N_("git receive-pack <git-dir>"),
diff --git a/builtin/rerere.c b/builtin/rerere.c
index 0458db9cad..07a9d37275 100644
--- a/builtin/rerere.c
+++ b/builtin/rerere.c
@@ -6,7 +6,6 @@
#include "repository.h"
#include "string-list.h"
#include "rerere.h"
-#include "wrapper.h"
#include "xdiff/xdiff.h"
#include "xdiff-interface.h"
#include "pathspec.h"
diff --git a/builtin/unpack-file.c b/builtin/unpack-file.c
index 6842a6c499..c129e2bb6c 100644
--- a/builtin/unpack-file.c
+++ b/builtin/unpack-file.c
@@ -3,7 +3,6 @@
#include "hex.h"
#include "object-name.h"
#include "object-store-ll.h"
-#include "wrapper.h"
static char *create_temp_file(struct object_id *oid)
{
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 4780b3b5f5..cc6d143ad0 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -24,7 +24,6 @@
#include "submodule.h"
#include "utf8.h"
#include "worktree.h"
-#include "wrapper.h"
#include "quote.h"
#define BUILTIN_WORKTREE_ADD_USAGE \
diff --git a/bulk-checkin.c b/bulk-checkin.c
index e2f71db0f6..fec6816259 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -17,7 +17,6 @@
#include "packfile.h"
#include "object-file.h"
#include "object-store-ll.h"
-#include "wrapper.h"
static int odb_transaction_nesting;
diff --git a/combine-diff.c b/combine-diff.c
index 11e9d7494a..f90f442482 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -17,7 +17,6 @@
#include "userdiff.h"
#include "oid-array.h"
#include "revision.h"
-#include "wrapper.h"
static int compare_paths(const struct combine_diff_path *one,
const struct diff_filespec *two)
diff --git a/commit-graph.c b/commit-graph.c
index 8f43eb12d4..55d288cb3b 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -26,7 +26,6 @@
#include "trace2.h"
#include "tree.h"
#include "chunk-format.h"
-#include "wrapper.h"
void git_test_write_commit_graph_or_die(void)
{
diff --git a/compat/terminal.c b/compat/terminal.c
index d87e321189..83d95e8656 100644
--- a/compat/terminal.c
+++ b/compat/terminal.c
@@ -6,7 +6,6 @@
#include "run-command.h"
#include "string-list.h"
#include "hashmap.h"
-#include "wrapper.h"
#if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
diff --git a/config.c b/config.c
index e08e2332a6..cc97c18141 100644
--- a/config.c
+++ b/config.c
@@ -39,7 +39,6 @@
#include "wildmatch.h"
#include "worktree.h"
#include "ws.h"
-#include "wrapper.h"
#include "write-or-die.h"
struct config_source {
diff --git a/convert.c b/convert.c
index 3d8325d49e..4aef90a7b6 100644
--- a/convert.c
+++ b/convert.c
@@ -16,7 +16,6 @@
#include "trace.h"
#include "utf8.h"
#include "merge-ll.h"
-#include "wrapper.h"
/*
* convert.c - convert a file when checking it out and checking it in.
diff --git a/copy.c b/copy.c
index 882c79cffb..23d84c6c1d 100644
--- a/copy.c
+++ b/copy.c
@@ -1,7 +1,6 @@
#include "git-compat-util.h"
#include "copy.h"
#include "path.h"
-#include "wrapper.h"
int copy_fd(int ifd, int ofd)
{
diff --git a/csum-file.c b/csum-file.c
index daf9b06dff..cd01713244 100644
--- a/csum-file.c
+++ b/csum-file.c
@@ -11,7 +11,6 @@
#include "progress.h"
#include "csum-file.h"
#include "hash.h"
-#include "wrapper.h"
static void verify_buffer_or_die(struct hashfile *f,
const void *buf,
diff --git a/daemon.c b/daemon.c
index 7139cc201d..3722edf46c 100644
--- a/daemon.c
+++ b/daemon.c
@@ -10,7 +10,6 @@
#include "setup.h"
#include "strbuf.h"
#include "string-list.h"
-#include "wrapper.h"
#ifdef NO_INITGROUPS
#define initgroups(x, y) (0) /* nothing */
diff --git a/diff.c b/diff.c
index 351d1d0591..10c8597ba0 100644
--- a/diff.c
+++ b/diff.c
@@ -43,7 +43,6 @@
#include "setup.h"
#include "strmap.h"
#include "ws.h"
-#include "wrapper.h"
#ifdef NO_FAST_WORKING_DIRECTORY
#define FAST_WORKING_DIRECTORY 0
diff --git a/dir.c b/dir.c
index 3acac7beb1..d270a1be36 100644
--- a/dir.c
+++ b/dir.c
@@ -32,7 +32,6 @@
#include "symlinks.h"
#include "trace2.h"
#include "tree.h"
-#include "wrapper.h"
/*
* Tells read_directory_recursive how a file or directory should be treated.
diff --git a/editor.c b/editor.c
index 38c5dbbb79..b67b802ddf 100644
--- a/editor.c
+++ b/editor.c
@@ -11,7 +11,6 @@
#include "strvec.h"
#include "run-command.h"
#include "sigchain.h"
-#include "wrapper.h"
#ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "vi"
diff --git a/entry.c b/entry.c
index f9a7c726a4..43767f9043 100644
--- a/entry.c
+++ b/entry.c
@@ -14,7 +14,6 @@
#include "fsmonitor.h"
#include "entry.h"
#include "parallel-checkout.h"
-#include "wrapper.h"
static void create_directories(const char *path, int path_len,
const struct checkout *state)
diff --git a/environment.c b/environment.c
index 5e9b331279..73b5b59ecf 100644
--- a/environment.c
+++ b/environment.c
@@ -28,7 +28,6 @@
#include "setup.h"
#include "shallow.h"
#include "trace.h"
-#include "wrapper.h"
#include "write-or-die.h"
int trust_executable_bit = 1;
diff --git a/fetch-pack.c b/fetch-pack.c
index a432eacab9..84a24ff9b1 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -34,7 +34,6 @@
#include "commit-graph.h"
#include "sigchain.h"
#include "mergesort.h"
-#include "wrapper.h"
static int transfer_unpack_limit = -1;
static int fetch_unpack_limit = -1;
diff --git a/gpg-interface.c b/gpg-interface.c
index 6a3817bbca..206ffdd79e 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -12,7 +12,6 @@
#include "sigchain.h"
#include "tempfile.h"
#include "alias.h"
-#include "wrapper.h"
#include "environment.h"
static int git_gpg_config(const char *, const char *, void *);
diff --git a/grep.c b/grep.c
index ebcd647478..a3cc8224db 100644
--- a/grep.c
+++ b/grep.c
@@ -12,7 +12,6 @@
#include "commit.h"
#include "quote.h"
#include "help.h"
-#include "wrapper.h"
static int grep_source_load(struct grep_source *gs);
static int grep_source_is_binary(struct grep_source *gs,
diff --git a/http-backend.c b/http-backend.c
index 895fac35f7..01d0405597 100644
--- a/http-backend.c
+++ b/http-backend.c
@@ -19,7 +19,6 @@
#include "object-store-ll.h"
#include "protocol.h"
#include "date.h"
-#include "wrapper.h"
#include "write-or-die.h"
static const char content_type[] = "Content-Type";
diff --git a/imap-send.c b/imap-send.c
index d1f312a07c..ef0b6a3f03 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -30,7 +30,6 @@
#include "parse-options.h"
#include "setup.h"
#include "strbuf.h"
-#include "wrapper.h"
#if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
typedef void *SSL;
#endif
diff --git a/merge-ll.c b/merge-ll.c
index 740b8c6bfd..ad26d7e260 100644
--- a/merge-ll.c
+++ b/merge-ll.c
@@ -13,7 +13,6 @@
#include "merge-ll.h"
#include "quote.h"
#include "strbuf.h"
-#include "wrapper.h"
struct ll_merge_driver;
diff --git a/merge-recursive.c b/merge-recursive.c
index 43f6b2d036..6a4081bb0f 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -38,7 +38,6 @@
#include "tag.h"
#include "tree-walk.h"
#include "unpack-trees.h"
-#include "wrapper.h"
#include "xdiff-interface.h"
struct merge_options_internal {
diff --git a/notes-merge.c b/notes-merge.c
index 071947894e..8799b522a5 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -20,7 +20,6 @@
#include "trace.h"
#include "notes-utils.h"
#include "commit-reach.h"
-#include "wrapper.h"
struct notes_merge_pair {
struct object_id obj, base, local, remote;
diff --git a/object-file.c b/object-file.c
index 8d87720dd5..527b740018 100644
--- a/object-file.c
+++ b/object-file.c
@@ -44,7 +44,6 @@
#include "setup.h"
#include "submodule.h"
#include "fsck.h"
-#include "wrapper.h"
/* The maximum size for an object header. */
#define MAX_HEADER_LEN 32
diff --git a/pack-write.c b/pack-write.c
index af48813a9b..b19ddf15b2 100644
--- a/pack-write.c
+++ b/pack-write.c
@@ -12,7 +12,6 @@
#include "pack-revindex.h"
#include "path.h"
#include "strbuf.h"
-#include "wrapper.h"
void reset_pack_idx_option(struct pack_idx_option *opts)
{
diff --git a/packfile.c b/packfile.c
index c60aeb05ec..7683a6dbea 100644
--- a/packfile.c
+++ b/packfile.c
@@ -24,7 +24,6 @@
#include "commit-graph.h"
#include "pack-revindex.h"
#include "promisor-remote.h"
-#include "wrapper.h"
char *odb_pack_name(struct strbuf *buf,
const unsigned char *hash,
diff --git a/parallel-checkout.c b/parallel-checkout.c
index 602fbf19d3..8637723461 100644
--- a/parallel-checkout.c
+++ b/parallel-checkout.c
@@ -15,7 +15,6 @@
#include "symlinks.h"
#include "thread-utils.h"
#include "trace2.h"
-#include "wrapper.h"
struct pc_worker {
struct child_process cp;
diff --git a/path.c b/path.c
index 044a50bad0..67e2690efe 100644
--- a/path.c
+++ b/path.c
@@ -18,7 +18,6 @@
#include "object-store-ll.h"
#include "lockfile.h"
#include "exec-cmd.h"
-#include "wrapper.h"
static int get_st_mode_bits(const char *path, int *mode)
{
diff --git a/pkt-line.c b/pkt-line.c
index 62b4208b66..6e4166132d 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -5,7 +5,6 @@
#include "hex.h"
#include "run-command.h"
#include "trace.h"
-#include "wrapper.h"
#include "write-or-die.h"
char packet_buffer[LARGE_PACKET_MAX];
diff --git a/read-cache.c b/read-cache.c
index b9a995e5a1..140b4f96a0 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -46,7 +46,6 @@
#include "csum-file.h"
#include "promisor-remote.h"
#include "hook.h"
-#include "wrapper.h"
/* Mask for the name length in ce_flags in the on-disk index */
diff --git a/rebase-interactive.c b/rebase-interactive.c
index f286404d4b..d9718409b3 100644
--- a/rebase-interactive.c
+++ b/rebase-interactive.c
@@ -11,7 +11,6 @@
#include "config.h"
#include "dir.h"
#include "object-name.h"
-#include "wrapper.h"
static const char edit_todo_list_advice[] =
N_("You can fix this with 'git rebase --edit-todo' "
diff --git a/refs.c b/refs.c
index 3065e514fd..1cba800add 100644
--- a/refs.c
+++ b/refs.c
@@ -30,7 +30,6 @@
#include "date.h"
#include "commit.h"
#include "wildmatch.h"
-#include "wrapper.h"
/*
* List of all available backends
diff --git a/rerere.c b/rerere.c
index e2b8597f88..4227c9612a 100644
--- a/rerere.c
+++ b/rerere.c
@@ -20,7 +20,6 @@
#include "object-store-ll.h"
#include "hash-lookup.h"
#include "strmap.h"
-#include "wrapper.h"
#define RESOLVED 0
#define PUNTED 1
diff --git a/send-pack.c b/send-pack.c
index 9510bef856..89aca9d829 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -15,7 +15,6 @@
#include "quote.h"
#include "transport.h"
#include "version.h"
-#include "wrapper.h"
#include "oid-array.h"
#include "gpg-interface.h"
#include "shallow.h"
diff --git a/sequencer.c b/sequencer.c
index 6a5498281e..5891466d91 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -49,7 +49,6 @@
#include "rebase-interactive.h"
#include "reset.h"
#include "branch.h"
-#include "wrapper.h"
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
diff --git a/server-info.c b/server-info.c
index 382e481a2b..f350713ecf 100644
--- a/server-info.c
+++ b/server-info.c
@@ -14,7 +14,6 @@
#include "object-store-ll.h"
#include "server-info.h"
#include "strbuf.h"
-#include "wrapper.h"
struct update_info_ctx {
FILE *cur_fp;
diff --git a/setup.c b/setup.c
index 3c383c972c..1b2f16229b 100644
--- a/setup.c
+++ b/setup.c
@@ -17,7 +17,6 @@
#include "quote.h"
#include "trace2.h"
#include "worktree.h"
-#include "wrapper.h"
static int inside_git_dir = -1;
static int inside_work_tree = -1;
diff --git a/shallow.c b/shallow.c
index f3ef94d4c9..2fad3504b7 100644
--- a/shallow.c
+++ b/shallow.c
@@ -20,7 +20,6 @@
#include "shallow.h"
#include "statinfo.h"
#include "trace.h"
-#include "wrapper.h"
void set_alternate_shallow_file(struct repository *r, const char *path, int override)
{
diff --git a/strbuf.c b/strbuf.c
index 37fd5247b3..77e205d0be 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -7,7 +7,6 @@
#include "string-list.h"
#include "utf8.h"
#include "date.h"
-#include "wrapper.h"
int starts_with(const char *str, const char *prefix)
{
diff --git a/streaming.c b/streaming.c
index 49791ab958..10adf625b2 100644
--- a/streaming.c
+++ b/streaming.c
@@ -10,7 +10,6 @@
#include "object-store-ll.h"
#include "replace-object.h"
#include "packfile.h"
-#include "wrapper.h"
typedef int (*open_istream_fn)(struct git_istream *,
struct repository *,
diff --git a/t/helper/test-delta.c b/t/helper/test-delta.c
index e7d134ec25..6bc787a474 100644
--- a/t/helper/test-delta.c
+++ b/t/helper/test-delta.c
@@ -11,7 +11,6 @@
#include "test-tool.h"
#include "git-compat-util.h"
#include "delta.h"
-#include "wrapper.h"
static const char usage_str[] =
"test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
diff --git a/t/helper/test-fsmonitor-client.c b/t/helper/test-fsmonitor-client.c
index 58d1dc5fc8..8280984d08 100644
--- a/t/helper/test-fsmonitor-client.c
+++ b/t/helper/test-fsmonitor-client.c
@@ -11,7 +11,6 @@
#include "setup.h"
#include "thread-utils.h"
#include "trace2.h"
-#include "wrapper.h"
#ifndef HAVE_FSMONITOR_DAEMON_BACKEND
int cmd__fsmonitor_client(int argc UNUSED, const char **argv UNUSED)
diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c
index 56c2d25f35..1acd362346 100644
--- a/t/helper/test-read-cache.c
+++ b/t/helper/test-read-cache.c
@@ -4,7 +4,6 @@
#include "read-cache-ll.h"
#include "repository.h"
#include "setup.h"
-#include "wrapper.h"
int cmd__read_cache(int argc, const char **argv)
{
diff --git a/tag.c b/tag.c
index c356d58890..c1537e30cb 100644
--- a/tag.c
+++ b/tag.c
@@ -11,7 +11,6 @@
#include "gpg-interface.h"
#include "hex.h"
#include "packfile.h"
-#include "wrapper.h"
const char *tag_type = "tag";
diff --git a/tempfile.c b/tempfile.c
index 6c88a63b42..ecdebf1afb 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -47,7 +47,6 @@
#include "path.h"
#include "tempfile.h"
#include "sigchain.h"
-#include "wrapper.h"
static VOLATILE_LIST_HEAD(tempfile_list);
diff --git a/trace.c b/trace.c
index 592c141d78..971a68abe8 100644
--- a/trace.c
+++ b/trace.c
@@ -27,7 +27,6 @@
#include "quote.h"
#include "setup.h"
#include "trace.h"
-#include "wrapper.h"
struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
diff --git a/transport-helper.c b/transport-helper.c
index 5c0bc6a896..49811ef176 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -19,7 +19,6 @@
#include "refspec.h"
#include "transport-internal.h"
#include "protocol.h"
-#include "wrapper.h"
static int debug;
diff --git a/transport.c b/transport.c
index 4dc187a388..0a5794a944 100644
--- a/transport.c
+++ b/transport.c
@@ -30,7 +30,6 @@
#include "object-store-ll.h"
#include "color.h"
#include "bundle-uri.h"
-#include "wrapper.h"
static int transport_use_color = -1;
static char transport_colors[][COLOR_MAXLEN] = {
diff --git a/upload-pack.c b/upload-pack.c
index 3a176a7209..3dc92d5aac 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -32,7 +32,6 @@
#include "commit-graph.h"
#include "commit-reach.h"
#include "shallow.h"
-#include "wrapper.h"
#include "write-or-die.h"
/* Remember to update object flag allocation in object.h */
diff --git a/usage.c b/usage.c
index 46d99f8bd4..09f0ed509b 100644
--- a/usage.c
+++ b/usage.c
@@ -6,7 +6,6 @@
#include "git-compat-util.h"
#include "gettext.h"
#include "trace2.h"
-#include "wrapper.h"
static void vreportf(const char *prefix, const char *err, va_list params)
{
diff --git a/worktree.c b/worktree.c
index f9aa371ca7..2018c127ca 100644
--- a/worktree.c
+++ b/worktree.c
@@ -12,7 +12,6 @@
#include "dir.h"
#include "wt-status.h"
#include "config.h"
-#include "wrapper.h"
void free_worktrees(struct worktree **worktrees)
{
diff --git a/wrapper.c b/wrapper.c
index 67f5f5dbe1..22be9812a7 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -9,7 +9,6 @@
#include "repository.h"
#include "strbuf.h"
#include "trace2.h"
-#include "wrapper.h"
static intmax_t count_fsync_writeout_only;
static intmax_t count_fsync_hardware_flush;
diff --git a/write-or-die.c b/write-or-die.c
index cc9e0787a1..d8355c0c3e 100644
--- a/write-or-die.c
+++ b/write-or-die.c
@@ -1,7 +1,6 @@
#include "git-compat-util.h"
#include "config.h"
#include "run-command.h"
-#include "wrapper.h"
#include "write-or-die.h"
/*
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* [PATCH v2 7/7] common: move alloc macros to common.h
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
` (5 preceding siblings ...)
2023-05-23 19:29 ` [PATCH v2 6/7] treewide: remove unnecessary includes for wrapper.h Calvin Wan
@ 2023-05-23 19:29 ` Calvin Wan
2023-05-26 21:27 ` Jonathan Tan
2023-05-26 21:03 ` [PATCH v2 0/7] git-compat-util cleanups Jonathan Tan
7 siblings, 1 reply; 29+ messages in thread
From: Calvin Wan @ 2023-05-23 19:29 UTC (permalink / raw)
To: git; +Cc: Calvin Wan
alloc_nr, ALLOC_GROW, and ALLOC_GROW_BY are commonly used macros for
dynamic array allocation. Moving these macros to common.h focuses
alloc.[ch] to allocation for Git objects and additionally allows us to
remove inclusions to alloc.h from files that solely used the above
macros.
Signed-off-by: Calvin Wan <calvinwan@google.com>
---
add-patch.c | 1 -
alias.c | 1 -
alloc.h | 75 ------------------------------
apply.c | 1 -
archive-tar.c | 1 -
archive.c | 1 -
attr.c | 1 -
builtin/blame.c | 1 -
builtin/cat-file.c | 1 -
builtin/checkout--worker.c | 1 -
builtin/config.c | 1 -
builtin/credential-cache--daemon.c | 1 -
builtin/fetch-pack.c | 1 -
builtin/fsmonitor--daemon.c | 1 -
builtin/grep.c | 1 -
builtin/index-pack.c | 1 -
builtin/log.c | 1 -
builtin/merge.c | 1 -
builtin/mktree.c | 1 -
builtin/mv.c | 1 -
builtin/name-rev.c | 1 -
builtin/pack-objects.c | 1 -
builtin/repack.c | 1 -
builtin/rev-parse.c | 1 -
builtin/revert.c | 1 -
builtin/rm.c | 1 -
builtin/submodule--helper.c | 1 -
bulk-checkin.c | 1 -
cache-tree.c | 1 -
chunk-format.c | 1 -
commit-reach.c | 1 -
common.h | 75 ++++++++++++++++++++++++++++++
config.c | 1 -
daemon.c | 1 -
delta-islands.c | 1 -
diff.c | 1 -
diffcore-rename.c | 1 -
dir-iterator.c | 1 -
dir.c | 1 -
ewah/bitmap.c | 1 -
ewah/ewah_bitmap.c | 1 -
fetch-pack.c | 1 -
fmt-merge-msg.c | 1 -
fsck.c | 1 -
help.c | 1 -
http-backend.c | 1 -
line-log.c | 1 -
list-objects-filter-options.c | 1 -
list-objects-filter.c | 1 -
midx.c | 1 -
object-file.c | 1 -
oid-array.c | 1 -
oidtree.c | 1 -
pack-bitmap-write.c | 1 -
pack-bitmap.c | 1 -
pack-objects.c | 1 -
packfile.c | 1 -
parallel-checkout.c | 1 -
pretty.c | 1 -
prio-queue.c | 1 -
quote.c | 1 -
read-cache.c | 1 -
ref-filter.c | 1 -
reflog-walk.c | 1 -
refs.c | 1 -
refspec.c | 1 -
remote-curl.c | 1 -
remote.c | 1 -
rerere.c | 1 -
revision.c | 1 -
sequencer.c | 1 -
server-info.c | 1 -
shallow.c | 1 -
sigchain.c | 1 -
sparse-index.c | 1 -
split-index.c | 1 -
strbuf.c | 1 -
string-list.c | 1 -
strvec.c | 1 -
submodule-config.c | 1 -
submodule.c | 1 -
t/helper/test-reach.c | 1 -
trace2/tr2_tls.c | 1 -
trailer.c | 1 -
transport.c | 1 -
tree-walk.c | 1 -
userdiff.c | 1 -
worktree.c | 1 -
88 files changed, 75 insertions(+), 161 deletions(-)
diff --git a/add-patch.c b/add-patch.c
index ba629add62..bfe19876cd 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1,7 +1,6 @@
#include "git-compat-util.h"
#include "add-interactive.h"
#include "advice.h"
-#include "alloc.h"
#include "editor.h"
#include "environment.h"
#include "gettext.h"
diff --git a/alias.c b/alias.c
index 54a1a23d2c..38e1339936 100644
--- a/alias.c
+++ b/alias.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "alias.h"
-#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "strbuf.h"
diff --git a/alloc.h b/alloc.h
index 4312db4bd0..3f4a0ad310 100644
--- a/alloc.h
+++ b/alloc.h
@@ -17,79 +17,4 @@ 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
diff --git a/apply.c b/apply.c
index 99c2a91de7..6b9ce54f7d 100644
--- a/apply.c
+++ b/apply.c
@@ -9,7 +9,6 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "base85.h"
#include "config.h"
#include "object-store-ll.h"
diff --git a/archive-tar.c b/archive-tar.c
index fc06ff4c5d..704bf0612e 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -2,7 +2,6 @@
* Copyright (c) 2005, 2006 Rene Scharfe
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "gettext.h"
#include "git-zlib.h"
diff --git a/archive.c b/archive.c
index 1817cca9f4..ca11db185b 100644
--- a/archive.c
+++ b/archive.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "environment.h"
diff --git a/attr.c b/attr.c
index 7d39ac4a29..e9c81b6e07 100644
--- a/attr.c
+++ b/attr.c
@@ -7,7 +7,6 @@
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "exec-cmd.h"
diff --git a/builtin/blame.c b/builtin/blame.c
index e811e7fbfb..9a3f9facea 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -6,7 +6,6 @@
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "color.h"
#include "builtin.h"
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 8ce5492d55..6266861365 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -5,7 +5,6 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "diff.h"
diff --git a/builtin/checkout--worker.c b/builtin/checkout--worker.c
index c655dc4b13..6b62b5375b 100644
--- a/builtin/checkout--worker.c
+++ b/builtin/checkout--worker.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "gettext.h"
diff --git a/builtin/config.c b/builtin/config.c
index c01f136a2b..49b82b5323 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "color.h"
#include "editor.h"
diff --git a/builtin/credential-cache--daemon.c b/builtin/credential-cache--daemon.c
index 756c5f02ae..db8c49605c 100644
--- a/builtin/credential-cache--daemon.c
+++ b/builtin/credential-cache--daemon.c
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "gettext.h"
#include "object-file.h"
#include "parse-options.h"
diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
index 3ba0fe5a39..44c05ee86c 100644
--- a/builtin/fetch-pack.c
+++ b/builtin/fetch-pack.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "object-file.h"
diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 74d1d6a585..0194f1e263 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -1,6 +1,5 @@
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/builtin/grep.c b/builtin/grep.c
index 72e70b3a48..c60cf2007f 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -4,7 +4,6 @@
* Copyright (c) 2006 Junio C Hamano
*/
#include "builtin.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "repository.h"
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index f58f9db69d..57812a276a 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "delta.h"
#include "environment.h"
diff --git a/builtin/log.c b/builtin/log.c
index 486548c57e..c1cf36c38e 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -6,7 +6,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/builtin/merge.c b/builtin/merge.c
index 2f2f0a0b4c..059f3ecb84 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -10,7 +10,6 @@
#include "builtin.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "editor.h"
#include "environment.h"
diff --git a/builtin/mktree.c b/builtin/mktree.c
index 0eea810c7e..9a22d4e277 100644
--- a/builtin/mktree.c
+++ b/builtin/mktree.c
@@ -4,7 +4,6 @@
* Copyright (c) Junio C Hamano, 2006, 2009
*/
#include "builtin.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "quote.h"
diff --git a/builtin/mv.c b/builtin/mv.c
index ae462bd7d4..fa84fcb20d 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -7,7 +7,6 @@
#include "builtin.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index c3b722b36f..c706fa3720 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 6409c07c7d..869a21b38f 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/builtin/repack.c b/builtin/repack.c
index 6c896c9c80..60ae514fd5 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -1,5 +1,4 @@
#include "builtin.h"
-#include "alloc.h"
#include "config.h"
#include "dir.h"
#include "environment.h"
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 4a219ea93b..161077aa79 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -6,7 +6,6 @@
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "commit.h"
#include "environment.h"
diff --git a/builtin/revert.c b/builtin/revert.c
index b3c6d75a1d..66d12e8c6f 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "parse-options.h"
diff --git a/builtin/rm.c b/builtin/rm.c
index 463eeabcea..dff819ae50 100644
--- a/builtin/rm.c
+++ b/builtin/rm.c
@@ -5,7 +5,6 @@
*/
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
-#include "alloc.h"
#include "advice.h"
#include "config.h"
#include "lockfile.h"
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index baf6ff88c3..e22cf4e4ee 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1,7 +1,6 @@
#define USE_THE_INDEX_VARIABLE
#include "builtin.h"
#include "abspath.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/bulk-checkin.c b/bulk-checkin.c
index fec6816259..73bff3a23d 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -2,7 +2,6 @@
* Copyright (c) 2011, Google Inc.
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "bulk-checkin.h"
#include "environment.h"
#include "gettext.h"
diff --git a/cache-tree.c b/cache-tree.c
index 84d7491420..641427ed41 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "hex.h"
#include "lockfile.h"
diff --git a/chunk-format.c b/chunk-format.c
index e7d613c907..140dfa0dcc 100644
--- a/chunk-format.c
+++ b/chunk-format.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "chunk-format.h"
#include "csum-file.h"
#include "gettext.h"
diff --git a/commit-reach.c b/commit-reach.c
index 70bde8af05..812bc6bf92 100644
--- a/commit-reach.c
+++ b/commit-reach.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "commit-graph.h"
#include "decorate.h"
diff --git a/common.h b/common.h
index e3b673847d..b0c68eb2a2 100644
--- a/common.h
+++ b/common.h
@@ -199,6 +199,81 @@ static inline int cast_size_t_to_int(size_t a)
*/
#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
+#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)
+
#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
diff --git a/config.c b/config.c
index cc97c18141..09851a6909 100644
--- a/config.c
+++ b/config.c
@@ -8,7 +8,6 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "date.h"
#include "branch.h"
#include "config.h"
diff --git a/daemon.c b/daemon.c
index 3722edf46c..1edc9a8510 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "path.h"
diff --git a/delta-islands.c b/delta-islands.c
index c824a5f6a4..332f0f7c45 100644
--- a/delta-islands.c
+++ b/delta-islands.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "attr.h"
#include "object.h"
#include "blob.h"
diff --git a/diff.c b/diff.c
index 10c8597ba0..8709d87b86 100644
--- a/diff.c
+++ b/diff.c
@@ -3,7 +3,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "base85.h"
#include "config.h"
#include "convert.h"
diff --git a/diffcore-rename.c b/diffcore-rename.c
index 926b554bd5..5a6e2bcac7 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -3,7 +3,6 @@
* Copyright (C) 2005 Junio C Hamano
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "diff.h"
#include "diffcore.h"
#include "object-store-ll.h"
diff --git a/dir-iterator.c b/dir-iterator.c
index fb7c47f0e8..278b04243a 100644
--- a/dir-iterator.c
+++ b/dir-iterator.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "iterator.h"
#include "dir-iterator.h"
diff --git a/dir.c b/dir.c
index d270a1be36..c9dc69fc24 100644
--- a/dir.c
+++ b/dir.c
@@ -7,7 +7,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "dir.h"
diff --git a/ewah/bitmap.c b/ewah/bitmap.c
index 12d6aa398e..7b525b1ecd 100644
--- a/ewah/bitmap.c
+++ b/ewah/bitmap.c
@@ -17,7 +17,6 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "ewok.h"
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
diff --git a/ewah/ewah_bitmap.c b/ewah/ewah_bitmap.c
index c6d4ffc87c..8785cbc54a 100644
--- a/ewah/ewah_bitmap.c
+++ b/ewah/ewah_bitmap.c
@@ -17,7 +17,6 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "ewok.h"
#include "ewok_rlw.h"
diff --git a/fetch-pack.c b/fetch-pack.c
index 84a24ff9b1..6198f3adaf 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "date.h"
diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
index ac9c1b9c6c..ff8884eecd 100644
--- a/fmt-merge-msg.c
+++ b/fmt-merge-msg.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "refs.h"
diff --git a/fsck.c b/fsck.c
index a219d6f2c0..2035c4e00b 100644
--- a/fsck.c
+++ b/fsck.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "date.h"
#include "dir.h"
#include "hex.h"
diff --git a/help.c b/help.c
index 5d7637dce9..c81d43a5fa 100644
--- a/help.c
+++ b/help.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "builtin.h"
#include "exec-cmd.h"
diff --git a/http-backend.c b/http-backend.c
index 01d0405597..ff07b87e64 100644
--- a/http-backend.c
+++ b/http-backend.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "git-zlib.h"
diff --git a/line-log.c b/line-log.c
index 2eff914bf3..790ab73212 100644
--- a/line-log.c
+++ b/line-log.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "line-range.h"
#include "hex.h"
#include "tag.h"
diff --git a/list-objects-filter-options.c b/list-objects-filter-options.c
index 2a3b7881af..8a08b7af49 100644
--- a/list-objects-filter-options.c
+++ b/list-objects-filter-options.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "config.h"
#include "gettext.h"
diff --git a/list-objects-filter.c b/list-objects-filter.c
index e075a66c99..9327ccd505 100644
--- a/list-objects-filter.c
+++ b/list-objects-filter.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "gettext.h"
#include "hex.h"
diff --git a/midx.c b/midx.c
index db459e448b..3a16acabbc 100644
--- a/midx.c
+++ b/midx.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "csum-file.h"
#include "dir.h"
diff --git a/object-file.c b/object-file.c
index 527b740018..5ebe1b00c5 100644
--- a/object-file.c
+++ b/object-file.c
@@ -8,7 +8,6 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "convert.h"
#include "environment.h"
diff --git a/oid-array.c b/oid-array.c
index e8228c777b..8e4717746c 100644
--- a/oid-array.c
+++ b/oid-array.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "oid-array.h"
#include "hash-lookup.h"
diff --git a/oidtree.c b/oidtree.c
index 7d57b7b19e..daef175dc7 100644
--- a/oidtree.c
+++ b/oidtree.c
@@ -4,7 +4,6 @@
*/
#include "git-compat-util.h"
#include "oidtree.h"
-#include "alloc.h"
#include "hash.h"
struct oidtree_iter_data {
diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c
index d86f4e739a..f6757c3cbf 100644
--- a/pack-bitmap-write.c
+++ b/pack-bitmap-write.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/pack-bitmap.c b/pack-bitmap.c
index 07dda2b22e..1ff813a5ba 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "gettext.h"
#include "hex.h"
diff --git a/pack-objects.c b/pack-objects.c
index ccab09fe65..1b8052bece 100644
--- a/pack-objects.c
+++ b/pack-objects.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "object.h"
#include "pack.h"
#include "pack-objects.h"
diff --git a/packfile.c b/packfile.c
index 7683a6dbea..0f80c8ba1f 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
diff --git a/parallel-checkout.c b/parallel-checkout.c
index 8637723461..b5a714c711 100644
--- a/parallel-checkout.c
+++ b/parallel-checkout.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "entry.h"
#include "gettext.h"
diff --git a/pretty.c b/pretty.c
index d4bc4b0286..c417dcbf72 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "commit.h"
#include "environment.h"
diff --git a/prio-queue.c b/prio-queue.c
index dc2476be53..450775a374 100644
--- a/prio-queue.c
+++ b/prio-queue.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "prio-queue.h"
static inline int compare(struct prio_queue *queue, int i, int j)
diff --git a/quote.c b/quote.c
index 43c739671e..3c05194496 100644
--- a/quote.c
+++ b/quote.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "path.h"
#include "quote.h"
#include "strbuf.h"
diff --git a/read-cache.c b/read-cache.c
index 140b4f96a0..53d71134e2 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -4,7 +4,6 @@
* Copyright (C) Linus Torvalds, 2005
*/
#include "git-compat-util.h"
-#include "alloc.h"
#include "bulk-checkin.h"
#include "config.h"
#include "date.h"
diff --git a/ref-filter.c b/ref-filter.c
index 771fa6c133..33d5576968 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "gpg-interface.h"
diff --git a/reflog-walk.c b/reflog-walk.c
index d337e64431..d216f6f966 100644
--- a/reflog-walk.c
+++ b/reflog-walk.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "commit.h"
#include "refs.h"
#include "diff.h"
diff --git a/refs.c b/refs.c
index 1cba800add..bf6624adff 100644
--- a/refs.c
+++ b/refs.c
@@ -4,7 +4,6 @@
#include "git-compat-util.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "hashmap.h"
diff --git a/refspec.c b/refspec.c
index 57f6c2aaf9..d60932f4de 100644
--- a/refspec.c
+++ b/refspec.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "gettext.h"
#include "hash.h"
#include "hex.h"
diff --git a/remote-curl.c b/remote-curl.c
index acf7b2bb40..8a976a0253 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/remote.c b/remote.c
index 6538b6037d..e5e165de1c 100644
--- a/remote.c
+++ b/remote.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/rerere.c b/rerere.c
index 4227c9612a..7070f75014 100644
--- a/rerere.c
+++ b/rerere.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "config.h"
#include "copy.h"
#include "gettext.h"
diff --git a/revision.c b/revision.c
index c53e9f84ae..fa219afaad 100644
--- a/revision.c
+++ b/revision.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/sequencer.c b/sequencer.c
index 5891466d91..8f18001dab 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -1,7 +1,6 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "copy.h"
#include "environment.h"
diff --git a/server-info.c b/server-info.c
index f350713ecf..e2fe0f9143 100644
--- a/server-info.c
+++ b/server-info.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "environment.h"
#include "hex.h"
diff --git a/shallow.c b/shallow.c
index 2fad3504b7..5413719fd4 100644
--- a/shallow.c
+++ b/shallow.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "hex.h"
#include "repository.h"
#include "tempfile.h"
diff --git a/sigchain.c b/sigchain.c
index ee778c0580..66123bdbab 100644
--- a/sigchain.c
+++ b/sigchain.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "sigchain.h"
#define SIGCHAIN_MAX_SIGNALS 32
diff --git a/sparse-index.c b/sparse-index.c
index 90d0462256..1fdb07a9e6 100644
--- a/sparse-index.c
+++ b/sparse-index.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "name-hash.h"
diff --git a/split-index.c b/split-index.c
index 0ee3865a55..8c38687c04 100644
--- a/split-index.c
+++ b/split-index.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "gettext.h"
#include "hash.h"
#include "mem-pool.h"
diff --git a/strbuf.c b/strbuf.c
index 77e205d0be..8f64beb2a1 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "gettext.h"
#include "hex.h"
#include "path.h"
diff --git a/string-list.c b/string-list.c
index 0f8ac117fd..954569f381 100644
--- a/string-list.c
+++ b/string-list.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "string-list.h"
-#include "alloc.h"
void string_list_init_nodup(struct string_list *list)
{
diff --git a/strvec.c b/strvec.c
index 17d54b6c3b..89dc9e7e75 100644
--- a/strvec.c
+++ b/strvec.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "strvec.h"
-#include "alloc.h"
#include "hex.h"
#include "strbuf.h"
diff --git a/submodule-config.c b/submodule-config.c
index 8a247b3be0..d39d365f1b 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "dir.h"
#include "environment.h"
#include "gettext.h"
diff --git a/submodule.c b/submodule.c
index cba36d6080..e7a907bc25 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "repository.h"
#include "config.h"
#include "submodule-config.h"
diff --git a/t/helper/test-reach.c b/t/helper/test-reach.c
index ef58f10c2d..3e173399a0 100644
--- a/t/helper/test-reach.c
+++ b/t/helper/test-reach.c
@@ -1,5 +1,4 @@
#include "test-tool.h"
-#include "alloc.h"
#include "commit.h"
#include "commit-reach.h"
#include "config.h"
diff --git a/trace2/tr2_tls.c b/trace2/tr2_tls.c
index 9f46ae12f5..601c9e5036 100644
--- a/trace2/tr2_tls.c
+++ b/trace2/tr2_tls.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "thread-utils.h"
#include "trace.h"
#include "trace2/tr2_tls.h"
diff --git a/trailer.c b/trailer.c
index a2c3ed6f28..2170e01f6a 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "gettext.h"
diff --git a/transport.c b/transport.c
index 0a5794a944..219af8fd50 100644
--- a/transport.c
+++ b/transport.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "advice.h"
-#include "alloc.h"
#include "config.h"
#include "environment.h"
#include "hex.h"
diff --git a/tree-walk.c b/tree-walk.c
index 42ed86ef58..6c07913f3f 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "tree-walk.h"
-#include "alloc.h"
#include "dir.h"
#include "gettext.h"
#include "hex.h"
diff --git a/userdiff.c b/userdiff.c
index 664c7c1402..e399543823 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -1,5 +1,4 @@
#include "git-compat-util.h"
-#include "alloc.h"
#include "config.h"
#include "userdiff.h"
#include "attr.h"
diff --git a/worktree.c b/worktree.c
index 2018c127ca..7410dce811 100644
--- a/worktree.c
+++ b/worktree.c
@@ -1,6 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
-#include "alloc.h"
#include "environment.h"
#include "gettext.h"
#include "path.h"
--
2.40.1.698.g37aff9b760-goog
^ permalink raw reply related [flat|nested] 29+ messages in thread
* Re: [PATCH v2 0/7] git-compat-util cleanups
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
` (6 preceding siblings ...)
2023-05-23 19:29 ` [PATCH v2 7/7] common: move alloc macros to common.h Calvin Wan
@ 2023-05-26 21:03 ` Jonathan Tan
7 siblings, 0 replies; 29+ messages in thread
From: Jonathan Tan @ 2023-05-26 21:03 UTC (permalink / raw)
To: Calvin Wan; +Cc: Jonathan Tan, git
Calvin Wan <calvinwan@google.com> writes:
> Changes since v1:
> - rebased onto 8afb38ac80f6
I couldn't find this object. Calvin and I resolved this off-list, so I
can proceed with the review, but remember to rebase onto a more stable
base in version 3.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h
2023-05-23 19:29 ` [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
@ 2023-05-26 21:06 ` Jonathan Tan
0 siblings, 0 replies; 29+ messages in thread
From: Jonathan Tan @ 2023-05-26 21:06 UTC (permalink / raw)
To: Calvin Wan; +Cc: Jonathan Tan, git
Calvin Wan <calvinwan@google.com> writes:
> While functions like starts_with() probably should not belong in the
> boundaries of the strbuf library, this commit focuses on first splitting
> out headers from git-compat-util.h.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
I know we don't follow the 50-character rule for commit message headers
all the time, but I don't think it's necessary to break it here for
a relatively simple-to-explain change. Maybe "git-compat-util: move
strbuf.c funcs to its header". Same for the other patches.
The patch itself looks good.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros from git-compat-util.h
2023-05-23 19:29 ` [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros " Calvin Wan
@ 2023-05-26 21:19 ` Jonathan Tan
2023-05-28 12:04 ` Phillip Wood
2023-06-01 4:12 ` Junio C Hamano
0 siblings, 2 replies; 29+ messages in thread
From: Jonathan Tan @ 2023-05-26 21:19 UTC (permalink / raw)
To: Calvin Wan; +Cc: Jonathan Tan, git
Calvin Wan <calvinwan@google.com> writes:
> Splitting these macros from git-compat-util.h cleans up the file and
> allows future third-party sources to not use these overrides if they do
> not wish to.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
> ---
> git-compat-util.h | 65 +-------------------------------------------
> sane-ctype.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++
Any specific reason for the "sane-" prefix? I think it would make more
sense if it was just named ctype.h: see below.
> -/* in ctype.c, for kwset users */
> -extern const unsigned char tolower_trans_tbl[256];
I'm not sure what this has to do with sanity, but this is indeed defined
in ctype.c, so it's easier to justify moving this out if the criterion
was "what's in ctype.c" rather than "what's related to sane ctypes".
> -extern const signed char hexval_table[256];
And this one has nothing to do with ctypes or sanity, but rather, what's
considered to be a hex character. I think we need another patch to move
this to hex.h.
> -#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
Same for this one.
With the above suggestions, I think we do get what we want - a split
between things that make ctype more sane and between other compat stuff,
and also a split between those two and more platform-agnostic things
like what a hex character is.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH v2 2/7] wrapper.h: move declarations for wrapper.c functions from git-compat-util.h
2023-05-23 19:29 ` [PATCH v2 2/7] wrapper.h: move declarations for wrapper.c " Calvin Wan
@ 2023-05-26 21:25 ` Jonathan Tan
0 siblings, 0 replies; 29+ messages in thread
From: Jonathan Tan @ 2023-05-26 21:25 UTC (permalink / raw)
To: Calvin Wan; +Cc: Jonathan Tan, git
Calvin Wan <calvinwan@google.com> writes:
> Since the functions in wrapper.c are widely used across the codebase,
> include it by default in git-compat-util.h. A future patch will remove
> now unnecessary inclusions of wrapper.h from other files.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
Thanks. So currently, if you wanted to xmmap, you don't have to include
wrapper.h, but if you wanted to read_in_full, you do. Making Git code
treat functions like those 2 in the same way makes sense to me.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH v2 7/7] common: move alloc macros to common.h
2023-05-23 19:29 ` [PATCH v2 7/7] common: move alloc macros to common.h Calvin Wan
@ 2023-05-26 21:27 ` Jonathan Tan
0 siblings, 0 replies; 29+ messages in thread
From: Jonathan Tan @ 2023-05-26 21:27 UTC (permalink / raw)
To: Calvin Wan; +Cc: Jonathan Tan, git
Calvin Wan <calvinwan@google.com> writes:
> alloc_nr, ALLOC_GROW, and ALLOC_GROW_BY are commonly used macros for
> dynamic array allocation. Moving these macros to common.h focuses
> alloc.[ch] to allocation for Git objects and additionally allows us to
> remove inclusions to alloc.h from files that solely used the above
> macros.
>
> Signed-off-by: Calvin Wan <calvinwan@google.com>
Ah, indeed, "alloc.h" was used for 2 different allocation-related
things. This change makes sense, of course.
Thanks for this patch set. Overall, it looks good (except for some minor
comments that I've given in other emails).
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros from git-compat-util.h
2023-05-26 21:19 ` Jonathan Tan
@ 2023-05-28 12:04 ` Phillip Wood
2023-05-30 17:29 ` Jonathan Tan
2023-06-01 4:12 ` Junio C Hamano
1 sibling, 1 reply; 29+ messages in thread
From: Phillip Wood @ 2023-05-28 12:04 UTC (permalink / raw)
To: Jonathan Tan, Calvin Wan; +Cc: git
Hi Calvin and Jonathan
On 26/05/2023 22:19, Jonathan Tan wrote:
> Calvin Wan <calvinwan@google.com> writes:
>> Splitting these macros from git-compat-util.h cleans up the file and
>> allows future third-party sources to not use these overrides if they do
>> not wish to.
I think splitting these out makes sense. If you have time to add a
comment highlighting the differences from the standard library versions
that would be really helpful. As far as I know the differences are
- our versions are locale independent
- our isspace() does not consider '\v' or '\f' to be space
- our versions expect a signed or unsigned char rather than an int
as their argument
- our versions do not accept EOF
- our versions do not require an explicit cast to avoid undefined
behavior when passing char on systems where char is signed
>> Signed-off-by: Calvin Wan <calvinwan@google.com>
>> ---
>> git-compat-util.h | 65 +-------------------------------------------
>> sane-ctype.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++
>
> Any specific reason for the "sane-" prefix? I think it would make more
> sense if it was just named ctype.h: see below.
I don't have a strong opinion either way but "sane-ctype.h" makes it
clearer why we're doing something different to the standard library.
>> -/* in ctype.c, for kwset users */
>> -extern const unsigned char tolower_trans_tbl[256];
>
> I'm not sure what this has to do with sanity, but this is indeed defined
> in ctype.c, so it's easier to justify moving this out if the criterion
> was "what's in ctype.c" rather than "what's related to sane ctypes".
There is only one user of this so it could be moved to
diffcore-pickaxe.c or kwset.c where it would be more discoverable if
anyone needs to add another user in the future.
>> -extern const signed char hexval_table[256];
>
> And this one has nothing to do with ctypes or sanity, but rather, what's
> considered to be a hex character. I think we need another patch to move
> this to hex.h.
Isn't "what's considered to be a hex character" related to ctypes and
sanity though as it is used by the "sane" definition of isxdigit().
>> -#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
>
> Same for this one.
I'd much rather keep all of our "sane" ctype replacements in one place
as Calvin is proposing. isxdigit() is defined in <ctype.h> so it should
be in our "sane" version of that header.
Best Wishes
Phillip
> With the above suggestions, I think we do get what we want - a split
> between things that make ctype more sane and between other compat stuff,
> and also a split between those two and more platform-agnostic things
> like what a hex character is.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros from git-compat-util.h
2023-05-28 12:04 ` Phillip Wood
@ 2023-05-30 17:29 ` Jonathan Tan
0 siblings, 0 replies; 29+ messages in thread
From: Jonathan Tan @ 2023-05-30 17:29 UTC (permalink / raw)
To: Phillip Wood; +Cc: Jonathan Tan, Calvin Wan, git
Phillip Wood <phillip.wood123@gmail.com> writes:
> > Any specific reason for the "sane-" prefix? I think it would make more
> > sense if it was just named ctype.h: see below.
>
> I don't have a strong opinion either way but "sane-ctype.h" makes it
> clearer why we're doing something different to the standard library.
Ah...that's true.
> >> -extern const signed char hexval_table[256];
> >
> > And this one has nothing to do with ctypes or sanity, but rather, what's
> > considered to be a hex character. I think we need another patch to move
> > this to hex.h.
>
> Isn't "what's considered to be a hex character" related to ctypes and
> sanity though as it is used by the "sane" definition of isxdigit().
>
> >> -#define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
> >
> > Same for this one.
>
> I'd much rather keep all of our "sane" ctype replacements in one place
> as Calvin is proposing. isxdigit() is defined in <ctype.h> so it should
> be in our "sane" version of that header.
Good point.
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros from git-compat-util.h
2023-05-26 21:19 ` Jonathan Tan
2023-05-28 12:04 ` Phillip Wood
@ 2023-06-01 4:12 ` Junio C Hamano
1 sibling, 0 replies; 29+ messages in thread
From: Junio C Hamano @ 2023-06-01 4:12 UTC (permalink / raw)
To: Jonathan Tan; +Cc: Calvin Wan, git
Jonathan Tan <jonathantanmy@google.com> writes:
> Any specific reason for the "sane-" prefix? I think it would make more
> sense if it was just named ctype.h: see below.
I think it is a good idea to keep the sane prefix for two reasons
that were described in 4546738b (Unlocalized isspace and friends,
2005-10-13):
Do our own ctype.h, just to get the sane semantics: we want
locale-independence, _and_ we want the right signed behaviour. Plus we
only use a very small subset of ctype.h anyway (isspace, isalpha,
isdigit and isalnum).
^ permalink raw reply [flat|nested] 29+ messages in thread
end of thread, other threads:[~2023-06-01 4:13 UTC | newest]
Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-16 17:09 [PATCH 0/6] git-compat-util cleanups Calvin Wan
2023-05-16 17:09 ` [PATCH 1/6] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
2023-05-16 21:18 ` Junio C Hamano
2023-05-16 17:09 ` [PATCH 2/6] wrapper.h: move declarations for wrapper.c " Calvin Wan
2023-05-16 22:08 ` Junio C Hamano
2023-05-16 17:09 ` [PATCH 3/6] common.h: move non-compat specific macros and " Calvin Wan
2023-05-16 22:30 ` Junio C Hamano
2023-05-17 17:26 ` Calvin Wan
2023-05-16 17:09 ` [PATCH 4/6] usage.h: move declarations for usage.c " Calvin Wan
2023-05-16 17:09 ` [PATCH 5/6] treewide: remove unnecessary includes for wrapper.h Calvin Wan
2023-05-16 17:09 ` [PATCH 6/6] common: move alloc macros to common.h Calvin Wan
2023-05-16 17:54 ` [PATCH 0/6] git-compat-util cleanups Junio C Hamano
2023-05-16 19:00 ` Calvin Wan
2023-05-23 19:27 ` [PATCH v2 0/7] " Calvin Wan
2023-05-23 19:29 ` [PATCH v2 1/7] strbuf.h: move declarations for strbuf.c functions from git-compat-util.h Calvin Wan
2023-05-26 21:06 ` Jonathan Tan
2023-05-23 19:29 ` [PATCH v2 2/7] wrapper.h: move declarations for wrapper.c " Calvin Wan
2023-05-26 21:25 ` Jonathan Tan
2023-05-23 19:29 ` [PATCH v2 3/7] sane-ctype.h: move sane-ctype macros " Calvin Wan
2023-05-26 21:19 ` Jonathan Tan
2023-05-28 12:04 ` Phillip Wood
2023-05-30 17:29 ` Jonathan Tan
2023-06-01 4:12 ` Junio C Hamano
2023-05-23 19:29 ` [PATCH v2 4/7] common.h: move non-compat specific macros and functions " Calvin Wan
2023-05-23 19:29 ` [PATCH v2 5/7] usage.h: move declarations for usage.c " Calvin Wan
2023-05-23 19:29 ` [PATCH v2 6/7] treewide: remove unnecessary includes for wrapper.h Calvin Wan
2023-05-23 19:29 ` [PATCH v2 7/7] common: move alloc macros to common.h Calvin Wan
2023-05-26 21:27 ` Jonathan Tan
2023-05-26 21:03 ` [PATCH v2 0/7] git-compat-util cleanups Jonathan Tan
Code repositories for project(s) associated with this public inbox
https://80x24.org/pub/scm/git/git.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).