Skip to contentSkip to conversation

repository object

PATCH Superseded Brandon Williams 15 participants 214 messages May 31, 2017 – Jun 28, 2017 Kbuild Page 1 of 3

lore
Share & tools
lore
#0 lore raw

cover repository object

Given the vast interest expressed when I sent out my RFC series I decided it would be worth it to invest more time to making a repository object a reality.

This series is an extension of the last series I sent out (in that ls-files is converted to working on submodules in-process using repository objects instead of spawning a child process to do the work). The big difference from the RFC series is that I went through and did the work to migrate key repository state from global variables in 'environment.c' to being stored in a repository object itself. I migrated the bits of state that seemed reasonable for this series, there is still a lot of global state which could be migrated in the future.

I do think that we need to be slightly cautious about moving global state into the repository object though, I don't want 'struct repo' to simply become a kitchen sink where everything gets dumped. But this is just a warning for the future.

Since this is a v1 I'm fairly certain that it still has a lot of rough edges (like I think I need to write better commit messages, and we should probably have more comments documenting object fields/contract) but I want to get the review process started sooner rather than later since I'm sure people will have opinions (e.g. should it be called 'struct repo' or 'struct repository'?!).

So here it is! Thank you so much for taking the time review this, any and all comments would be appreciated.

- Brandon

Patches [01-14] -> Introducing the Repository object and migrating some state
to be stored in 'the_repository'.

Patches [15-31] -> Converting ls-files to use 'struct repo' in order to recurse
submodules in-process.

Brandon Williams (31):
config: create config.h
config: don't include config.h by default
config: don't implicitly use gitdir
setup: don't perform lazy initialization of repository state
environment: remove namespace_len variable
repo: introduce the repository object
environment: place key repository state in the_repository
environment: store worktree in the_repository
setup: add comment indicating a hack
config: migrate the_configset to the_repository
repo: add index_state to struct repo
submodule-config: store the_submodule_cache in the_repository
repo: add repo_read_gitmodules
submodule: convert is_submodule_initialized to work on a repository
convert: convert get_cached_convert_stats_ascii to take an index
convert: convert crlf_to_git to take an index
convert: convert convert_to_git_filter_fd to take an index
convert: convert convert_to_git to take an index
convert: convert renormalize_buffer to take an index
tree: convert read_tree to take an index parameter
ls-files: convert overlay_tree_on_cache to take an index
ls-files: convert write_eolinfo to take an index
ls-files: convert show_killed_files to take an index
ls-files: convert show_other_files to take an index
ls-files: convert show_ru_info to take an index
ls-files: convert ce_excluded to take an index
ls-files: convert prune_cache to take an index
ls-files: convert show_files to take an index
ls-files: factor out debug info into a function
ls-files: factor out tag calculation
ls-files: use repository object

 Makefile                               |   1 +
 advice.c                               |   1 +
 alias.c                                |   1 +
 apply.c                                |   3 +-
 archive-tar.c                          |   1 +
 archive-zip.c                          |   1 +
 archive.c                              |   1 +
 attr.c                                 |   1 +
 bisect.c                               |   1 +
 branch.c                               |   1 +
 builtin/add.c                          |   1 +
 builtin/am.c                           |   1 +
 builtin/blame.c                        |   3 +-
 builtin/branch.c                       |   1 +
 builtin/cat-file.c                     |   1 +
 builtin/check-attr.c                   |   1 +
 builtin/check-ignore.c                 |   1 +
 builtin/check-mailmap.c                |   1 +
 builtin/checkout-index.c               |   1 +
 builtin/checkout.c                     |   1 +
 builtin/clean.c                        |   1 +
 builtin/clone.c                        |   1 +
 builtin/column.c                       |   1 +
 builtin/commit-tree.c                  |   1 +
 builtin/commit.c                       |   4 +-
 builtin/config.c                       |   3 +
 builtin/count-objects.c                |   1 +
 builtin/describe.c                     |   1 +
 builtin/diff-files.c                   |   1 +
 builtin/diff-index.c                   |   1 +
 builtin/diff-tree.c                    |   1 +
 builtin/diff.c                         |   1 +
 builtin/difftool.c                     |   1 +
 builtin/fast-export.c                  |   1 +
 builtin/fetch.c                        |   1 +
 builtin/fmt-merge-msg.c                |   1 +
 builtin/for-each-ref.c                 |   1 +
 builtin/fsck.c                         |   1 +
 builtin/gc.c                           |   1 +
 builtin/grep.c                         |   4 +-
 builtin/hash-object.c                  |   1 +
 builtin/help.c                         |   1 +
 builtin/index-pack.c                   |   1 +
 builtin/init-db.c                      |   1 +
 builtin/log.c                          |   1 +
 builtin/ls-files.c                     | 343 ++++++++++++++++-----------------
 builtin/ls-tree.c                      |   1 +
 builtin/merge-base.c                   |   1 +
 builtin/merge-file.c                   |   1 +
 builtin/merge.c                        |   1 +
 builtin/mv.c                           |   1 +
 builtin/name-rev.c                     |   1 +
 builtin/notes.c                        |   1 +
 builtin/pack-objects.c                 |   1 +
 builtin/patch-id.c                     |   1 +
 builtin/pull.c                         |   1 +
 builtin/push.c                         |   1 +
 builtin/read-tree.c                    |   1 +
 builtin/rebase--helper.c               |   1 +
 builtin/receive-pack.c                 |   1 +
 builtin/reflog.c                       |   1 +
 builtin/remote.c                       |   1 +
 builtin/repack.c                       |   1 +
 builtin/replace.c                      |   1 +
 builtin/rerere.c                       |   1 +
 builtin/reset.c                        |   1 +
 builtin/rev-list.c                     |   1 +
 builtin/rev-parse.c                    |   1 +
 builtin/revert.c                       |   1 +
 builtin/rm.c                           |   1 +
 builtin/send-pack.c                    |   1 +
 builtin/shortlog.c                     |   1 +
 builtin/show-branch.c                  |   1 +
 builtin/stripspace.c                   |   1 +
 builtin/submodule--helper.c            |  10 +-
 builtin/symbolic-ref.c                 |   1 +
 builtin/tag.c                          |   1 +
 builtin/unpack-file.c                  |   1 +
 builtin/unpack-objects.c               |   1 +
 builtin/update-index.c                 |   1 +
 builtin/update-ref.c                   |   1 +
 builtin/update-server-info.c           |   1 +
 builtin/var.c                          |   1 +
 builtin/verify-commit.c                |   1 +
 builtin/verify-pack.c                  |   1 +
 builtin/verify-tag.c                   |   1 +
 builtin/worktree.c                     |   1 +
 builtin/write-tree.c                   |   1 +
 cache.h                                | 196 +------------------
 color.c                                |   1 +
 column.c                               |   1 +
 combine-diff.c                         |   2 +-
 config.c                               | 186 +++++++++++-------
 config.h                               | 204 ++++++++++++++++++++
 connect.c                              |   1 +
 convert.c                              |  32 +--
 convert.h                              |  19 +-
 credential-cache--daemon.c             |   1 +
 credential.c                           |   1 +
 daemon.c                               |   1 +
 diff.c                                 |   7 +-
 dir.c                                  |   3 +-
 environment.c                          | 101 +++-------
 fast-import.c                          |   1 +
 fetch-pack.c                           |   1 +
 git.c                                  |   3 +-
 gpg-interface.c                        |   1 +
 graph.c                                |   1 +
 grep.c                                 |   1 +
 help.c                                 |   1 +
 http-backend.c                         |   1 +
 http-fetch.c                           |   1 +
 http.c                                 |   1 +
 ident.c                                |   1 +
 imap-send.c                            |   1 +
 ll-merge.c                             |   3 +-
 log-tree.c                             |   1 +
 mailinfo.c                             |   1 +
 merge-recursive.c                      |   5 +-
 notes-utils.c                          |   1 +
 notes.c                                |   1 +
 pager.c                                |   1 +
 parse-options.c                        |   1 +
 path.c                                 |  11 +-
 pathspec.c                             |   1 +
 pretty.c                               |   1 +
 prompt.c                               |   1 +
 read-cache.c                           |   1 +
 refs.c                                 |   1 +
 refs/files-backend.c                   |   1 +
 remote-curl.c                          |   1 +
 remote.c                               |   1 +
 repo.c                                 | 192 ++++++++++++++++++
 repo.h                                 |  45 +++++
 rerere.c                               |   1 +
 send-pack.c                            |   1 +
 sequencer.c                            |   1 +
 setup.c                                |  34 ++++
 sha1_file.c                            |   7 +-
 sha1_name.c                            |   1 +
 submodule-config.c                     |  72 +++++--
 submodule-config.h                     |  10 +
 submodule.c                            |  22 +--
 submodule.h                            |   3 +-
 t/helper/test-config.c                 |   1 +
 t/helper/test-submodule-config.c       |   1 +
 t/t3007-ls-files-recurse-submodules.sh |  39 ++++
 trailer.c                              |   1 +
 transport.c                            |   1 +
 tree.c                                 |  28 ++-
 tree.h                                 |   3 +-
 unpack-trees.c                         |   1 +
 upload-pack.c                          |   1 +
 userdiff.c                             |   1 +
 versioncmp.c                           |   1 +
 wrapper.c                              |   1 +
 xdiff-interface.c                      |   1 +
 157 files changed, 1130 insertions(+), 593 deletions(-)
 create mode 100644 config.h
 create mode 100644 repo.c
 create mode 100644 repo.h
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#1 lore raw

PATCH 1/31 config: create config.h

Move all config related declarations from cache.h to a new config.h header file. This makes cache.h smaller and allows for the opportunity in a following patch to only include config.h when needed.

---
 cache.h  | 190 +------------------------------------------------------------
 config.h | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+), 189 deletions(-)
 create mode 100644 config.h
cache.h +1 -189
index ae4c45d37..a27fee458 100644
@@ -11,6 +11,7 @@
 #include "string-list.h"
 #include "pack-revindex.h"
 #include "hash.h"
+#include "config.h"
 
 #ifndef platform_SHA_CTX
 /*
@@ -1866,188 +1867,9 @@ extern int packed_object_info(struct packed_git *pack, off_t offset, struct obje
 /* Dumb servers support */
 extern int update_server_info(int);
 
-/* git_config_parse_key() returns these negated: */
-#define CONFIG_INVALID_KEY 1
-#define CONFIG_NO_SECTION_OR_NAME 2
-/* git_config_set_gently(), git_config_set_multivar_gently() return the above or these: */
-#define CONFIG_NO_LOCK -1
-#define CONFIG_INVALID_FILE 3
-#define CONFIG_NO_WRITE 4
-#define CONFIG_NOTHING_SET 5
-#define CONFIG_INVALID_PATTERN 6
-#define CONFIG_GENERIC_ERROR 7
-
-#define CONFIG_REGEX_NONE ((void *)1)
-
-struct git_config_source {
-	unsigned int use_stdin:1;
-	const char *file;
-	const char *blob;
-};
-
-enum config_origin_type {
-	CONFIG_ORIGIN_BLOB,
-	CONFIG_ORIGIN_FILE,
-	CONFIG_ORIGIN_STDIN,
-	CONFIG_ORIGIN_SUBMODULE_BLOB,
-	CONFIG_ORIGIN_CMDLINE
-};
-
-struct config_options {
-	unsigned int respect_includes : 1;
-	const char *git_dir;
-};
-
-typedef int (*config_fn_t)(const char *, const char *, void *);
-extern int git_default_config(const char *, const char *, void *);
-extern int git_config_from_file(config_fn_t fn, const char *, void *);
-extern int git_config_from_mem(config_fn_t fn, const enum config_origin_type,
-					const char *name, const char *buf, size_t len, void *data);
-extern int git_config_from_blob_sha1(config_fn_t fn, const char *name,
-				     const unsigned char *sha1, void *data);
-extern void git_config_push_parameter(const char *text);
-extern int git_config_from_parameters(config_fn_t fn, void *data);
-extern void read_early_config(config_fn_t cb, void *data);
-extern void git_config(config_fn_t fn, void *);
-extern int git_config_with_options(config_fn_t fn, void *,
-				   struct git_config_source *config_source,
-				   const struct config_options *opts);
-extern int git_parse_ulong(const char *, unsigned long *);
-extern int git_parse_maybe_bool(const char *);
-extern int git_config_int(const char *, const char *);
-extern int64_t git_config_int64(const char *, const char *);
-extern unsigned long git_config_ulong(const char *, const char *);
-extern ssize_t git_config_ssize_t(const char *, const char *);
-extern int git_config_bool_or_int(const char *, const char *, int *);
-extern int git_config_bool(const char *, const char *);
-extern int git_config_maybe_bool(const char *, const char *);
-extern int git_config_string(const char **, const char *, const char *);
-extern int git_config_pathname(const char **, const char *, const char *);
-extern int git_config_set_in_file_gently(const char *, const char *, const char *);
-extern void git_config_set_in_file(const char *, const char *, const char *);
-extern int git_config_set_gently(const char *, const char *);
-extern void git_config_set(const char *, const char *);
-extern int git_config_parse_key(const char *, char **, int *);
-extern int git_config_key_is_valid(const char *key);
-extern int git_config_set_multivar_gently(const char *, const char *, const char *, int);
-extern void git_config_set_multivar(const char *, const char *, const char *, int);
-extern int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, int);
-extern void git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
-extern int git_config_rename_section(const char *, const char *);
-extern int git_config_rename_section_in_file(const char *, const char *, const char *);
-extern const char *git_etc_gitconfig(void);
-extern int git_env_bool(const char *, int);
-extern unsigned long git_env_ulong(const char *, unsigned long);
-extern int git_config_system(void);
-extern int config_error_nonbool(const char *);
-#if defined(__GNUC__)
-#define config_error_nonbool(s) (config_error_nonbool(s), const_error())
-#endif
 extern const char *get_log_output_encoding(void);
 extern const char *get_commit_output_encoding(void);
 
-extern int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
-
-enum config_scope {
-	CONFIG_SCOPE_UNKNOWN = 0,
-	CONFIG_SCOPE_SYSTEM,
-	CONFIG_SCOPE_GLOBAL,
-	CONFIG_SCOPE_REPO,
-	CONFIG_SCOPE_CMDLINE,
-};
-
-extern enum config_scope current_config_scope(void);
-extern const char *current_config_origin_type(void);
-extern const char *current_config_name(void);
-
-struct config_include_data {
-	int depth;
-	config_fn_t fn;
-	void *data;
-	const struct config_options *opts;
-};
-#define CONFIG_INCLUDE_INIT { 0 }
-extern int git_config_include(const char *name, const char *value, void *data);
-
-/*
- * Match and parse a config key of the form:
- *
- *   section.(subsection.)?key
- *
- * (i.e., what gets handed to a config_fn_t). The caller provides the section;
- * we return -1 if it does not match, 0 otherwise. The subsection and key
- * out-parameters are filled by the function (and *subsection is NULL if it is
- * missing).
- *
- * If the subsection pointer-to-pointer passed in is NULL, returns 0 only if
- * there is no subsection at all.
- */
-extern int parse_config_key(const char *var,
-			    const char *section,
-			    const char **subsection, int *subsection_len,
-			    const char **key);
-
-struct config_set_element {
-	struct hashmap_entry ent;
-	char *key;
-	struct string_list value_list;
-};
-
-struct configset_list_item {
-	struct config_set_element *e;
-	int value_index;
-};
-
-/*
- * the contents of the list are ordered according to their
- * position in the config files and order of parsing the files.
- * (i.e. key-value pair at the last position of .git/config will
- * be at the last item of the list)
- */
-struct configset_list {
-	struct configset_list_item *items;
-	unsigned int nr, alloc;
-};
-
-struct config_set {
-	struct hashmap config_hash;
-	int hash_initialized;
-	struct configset_list list;
-};
-
-extern void git_configset_init(struct config_set *cs);
-extern int git_configset_add_file(struct config_set *cs, const char *filename);
-extern int git_configset_get_value(struct config_set *cs, const char *key, const char **value);
-extern const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
-extern void git_configset_clear(struct config_set *cs);
-extern int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest);
-extern int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
-extern int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
-extern int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
-extern int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
-extern int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
-extern int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
-extern int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
-
-extern int git_config_get_value(const char *key, const char **value);
-extern const struct string_list *git_config_get_value_multi(const char *key);
-extern void git_config_clear(void);
-extern void git_config_iter(config_fn_t fn, void *data);
-extern int git_config_get_string_const(const char *key, const char **dest);
-extern int git_config_get_string(const char *key, char **dest);
-extern int git_config_get_int(const char *key, int *dest);
-extern int git_config_get_ulong(const char *key, unsigned long *dest);
-extern int git_config_get_bool(const char *key, int *dest);
-extern int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest);
-extern int git_config_get_maybe_bool(const char *key, int *dest);
-extern int git_config_get_pathname(const char *key, const char **dest);
-extern int git_config_get_untracked_cache(void);
-extern int git_config_get_split_index(void);
-extern int git_config_get_max_percent_split_change(void);
-
-/* This dies if the configured or default date is in the future */
-extern int git_config_get_expiry(const char *key, const char **output);
-
 /*
  * This is a hack for test programs like test-dump-untracked-cache to
  * ensure that they do not modify the untracked cache when reading it.
@@ -2055,16 +1877,6 @@ extern int git_config_get_expiry(const char *key, const char **output);
  */
 extern int ignore_untracked_cache_config;
 
-struct key_value_info {
-	const char *filename;
-	int linenr;
-	enum config_origin_type origin_type;
-	enum config_scope scope;
-};
-
-extern NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3)));
-extern NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr);
-
 extern int committer_ident_sufficiently_given(void);
 extern int author_ident_sufficiently_given(void);
 
config.h new file +194 -0

194 lines not on this page · raw message

Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#2 lore raw

PATCH 2/31 config: don't include config.h by default

In reply to Brandon Williams

Stop including config.h by default in cache.h. Instead only include config.h in those files which require use of the config system.

---
 advice.c                         | 1 +
 alias.c                          | 1 +
 apply.c                          | 1 +
 archive-tar.c                    | 1 +
 archive-zip.c                    | 1 +
 archive.c                        | 1 +
 attr.c                           | 1 +
 bisect.c                         | 1 +
 branch.c                         | 1 +
 builtin/add.c                    | 1 +
 builtin/am.c                     | 1 +
 builtin/blame.c                  | 1 +
 builtin/branch.c                 | 1 +
 builtin/cat-file.c               | 1 +
 builtin/check-attr.c             | 1 +
 builtin/check-ignore.c           | 1 +
 builtin/check-mailmap.c          | 1 +
 builtin/checkout-index.c         | 1 +
 builtin/checkout.c               | 1 +
 builtin/clean.c                  | 1 +
 builtin/clone.c                  | 1 +
 builtin/column.c                 | 1 +
 builtin/commit-tree.c            | 1 +
 builtin/commit.c                 | 1 +
 builtin/config.c                 | 1 +
 builtin/count-objects.c          | 1 +
 builtin/describe.c               | 1 +
 builtin/diff-files.c             | 1 +
 builtin/diff-index.c             | 1 +
 builtin/diff-tree.c              | 1 +
 builtin/diff.c                   | 1 +
 builtin/difftool.c               | 1 +
 builtin/fast-export.c            | 1 +
 builtin/fetch.c                  | 1 +
 builtin/fmt-merge-msg.c          | 1 +
 builtin/for-each-ref.c           | 1 +
 builtin/fsck.c                   | 1 +
 builtin/gc.c                     | 1 +
 builtin/grep.c                   | 1 +
 builtin/hash-object.c            | 1 +
 builtin/help.c                   | 1 +
 builtin/index-pack.c             | 1 +
 builtin/init-db.c                | 1 +
 builtin/log.c                    | 1 +
 builtin/ls-files.c               | 1 +
 builtin/ls-tree.c                | 1 +
 builtin/merge-base.c             | 1 +
 builtin/merge-file.c             | 1 +
 builtin/merge.c                  | 1 +
 builtin/mv.c                     | 1 +
 builtin/name-rev.c               | 1 +
 builtin/notes.c                  | 1 +
 builtin/pack-objects.c           | 1 +
 builtin/patch-id.c               | 1 +
 builtin/pull.c                   | 1 +
 builtin/push.c                   | 1 +
 builtin/read-tree.c              | 1 +
 builtin/rebase--helper.c         | 1 +
 builtin/receive-pack.c           | 1 +
 builtin/reflog.c                 | 1 +
 builtin/remote.c                 | 1 +
 builtin/repack.c                 | 1 +
 builtin/replace.c                | 1 +
 builtin/rerere.c                 | 1 +
 builtin/reset.c                  | 1 +
 builtin/rev-list.c               | 1 +
 builtin/rev-parse.c              | 1 +
 builtin/revert.c                 | 1 +
 builtin/rm.c                     | 1 +
 builtin/send-pack.c              | 1 +
 builtin/shortlog.c               | 1 +
 builtin/show-branch.c            | 1 +
 builtin/stripspace.c             | 1 +
 builtin/submodule--helper.c      | 1 +
 builtin/symbolic-ref.c           | 1 +
 builtin/tag.c                    | 1 +
 builtin/unpack-file.c            | 1 +
 builtin/unpack-objects.c         | 1 +
 builtin/update-index.c           | 1 +
 builtin/update-ref.c             | 1 +
 builtin/update-server-info.c     | 1 +
 builtin/var.c                    | 1 +
 builtin/verify-commit.c          | 1 +
 builtin/verify-pack.c            | 1 +
 builtin/verify-tag.c             | 1 +
 builtin/worktree.c               | 1 +
 builtin/write-tree.c             | 1 +
 cache.h                          | 1 -
 color.c                          | 1 +
 column.c                         | 1 +
 config.c                         | 1 +
 connect.c                        | 1 +
 convert.c                        | 1 +
 credential-cache--daemon.c       | 1 +
 credential.c                     | 1 +
 daemon.c                         | 1 +
 diff.c                           | 1 +
 dir.c                            | 1 +
 environment.c                    | 1 +
 fast-import.c                    | 1 +
 fetch-pack.c                     | 1 +
 git.c                            | 1 +
 gpg-interface.c                  | 1 +
 graph.c                          | 1 +
 grep.c                           | 1 +
 help.c                           | 1 +
 http-backend.c                   | 1 +
 http-fetch.c                     | 1 +
 http.c                           | 1 +
 ident.c                          | 1 +
 imap-send.c                      | 1 +
 ll-merge.c                       | 1 +
 log-tree.c                       | 1 +
 mailinfo.c                       | 1 +
 merge-recursive.c                | 1 +
 notes-utils.c                    | 1 +
 notes.c                          | 1 +
 pager.c                          | 1 +
 parse-options.c                  | 1 +
 pathspec.c                       | 1 +
 pretty.c                         | 1 +
 prompt.c                         | 1 +
 read-cache.c                     | 1 +
 refs.c                           | 1 +
 refs/files-backend.c             | 1 +
 remote-curl.c                    | 1 +
 remote.c                         | 1 +
 rerere.c                         | 1 +
 send-pack.c                      | 1 +
 sequencer.c                      | 1 +
 setup.c                          | 1 +
 sha1_file.c                      | 1 +
 sha1_name.c                      | 1 +
 submodule-config.c               | 1 +
 submodule.c                      | 1 +
 t/helper/test-config.c           | 1 +
 t/helper/test-submodule-config.c | 1 +
 trailer.c                        | 1 +
 transport.c                      | 1 +
 unpack-trees.c                   | 1 +
 upload-pack.c                    | 1 +
 userdiff.c                       | 1 +
 versioncmp.c                     | 1 +
 wrapper.c                        | 1 +
 xdiff-interface.c                | 1 +
 145 files changed, 144 insertions(+), 1 deletion(-)
advice.c +1 -0
index b84ae4960..3fa04fca0 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 
 int advice_push_update_rejected = 1;
 int advice_push_non_ff_current = 1;
alias.c +1 -0
index 3b90397a9..052f34136 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 
 char *alias_lookup(const char *alias)
 {
apply.c +1 -0
index c49cef063..87db9618d 100644
@@ -8,6 +8,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "blob.h"
 #include "delta.h"
 #include "diff.h"
archive-tar.c +1 -0
index 073e60ebd..c6ed96ee7 100644
@@ -2,6 +2,7 @@
  * Copyright (c) 2005, 2006 Rene Scharfe
  */
 #include "cache.h"
+#include "config.h"
 #include "tar.h"
 #include "archive.h"
 #include "streaming.h"
archive-zip.c +1 -0
index 27563e9e2..e8913e5a2 100644
@@ -2,6 +2,7 @@
  * Copyright (c) 2006 Rene Scharfe
  */
 #include "cache.h"
+#include "config.h"
 #include "archive.h"
 #include "streaming.h"
 #include "utf8.h"
archive.c +1 -0
index b15a922da..60b3035a7 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "tree-walk.h"
attr.c +1 -0
index 7e2134471..9f8b02936 100644
@@ -9,6 +9,7 @@
 
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "exec_cmd.h"
 #include "attr.h"
 #include "dir.h"
bisect.c +1 -0
index d88f9bc3d..c2acb9455 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "diff.h"
 #include "revision.h"
branch.c +1 -0
index 985316eb7..a8a548ccf 100644
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "cache.h"
+#include "config.h"
 #include "branch.h"
 #include "refs.h"
 #include "remote.h"
builtin/add.c +1 -0
index d9a2491e4..f2415e99f 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2006 Linus Torvalds
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "lockfile.h"
 #include "dir.h"
builtin/am.c +1 -0
index 0f63dcab1..0b6b1265d 100644
@@ -4,6 +4,7 @@
  * Based on git-am.sh by Junio C Hamano.
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "parse-options.h"
builtin/blame.c +1 -0
index 1043e5376..c0ae49298 100644
@@ -6,6 +6,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "builtin.h"
 #include "blob.h"
builtin/branch.c +1 -0
index 83fcda43d..c958e9325 100644
@@ -6,6 +6,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "color.h"
 #include "refs.h"
 #include "commit.h"
builtin/cat-file.c +1 -0
index 9af863e79..7bc45f600 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 #include "userdiff.h"
builtin/check-attr.c +1 -0
index 4d01ca0c8..91444dc04 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "quote.h"
 #include "parse-options.h"
builtin/check-ignore.c +1 -0
index c7b8c0889..3e280b9c7 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "quote.h"
 #include "pathspec.h"
builtin/check-mailmap.c +1 -0
index cf0f54f6b..cdce144f3 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "mailmap.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/checkout-index.c +1 -0
index 07631d0c9..39c8be05d 100644
@@ -5,6 +5,7 @@
  *
  */
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "quote.h"
 #include "cache-tree.h"
builtin/checkout.c +1 -0
index a6b2af39d..45fab5a4d 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "parse-options.h"
 #include "refs.h"
builtin/clean.c +1 -0
index 329b68c40..09adf0551 100644
@@ -8,6 +8,7 @@
 
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/clone.c +1 -0
index 743f16ae2..4922a5496 100644
@@ -9,6 +9,7 @@
  */
 
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "parse-options.h"
 #include "fetch-pack.h"
builtin/column.c +1 -0
index 33314b4d7..0c3223d64 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "strbuf.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/commit-tree.c +1 -0
index f39c2b273..a4a923d7c 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "tree.h"
 #include "builtin.h"
builtin/commit.c +1 -0
index da1ba4c86..805da4915 100644
@@ -6,6 +6,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "cache-tree.h"
 #include "color.h"
builtin/config.c +1 -0
index 7f6c25d4d..753c40a5c 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "color.h"
 #include "parse-options.h"
 #include "urlmatch.h"
builtin/count-objects.c +1 -0
index acb05940f..1d82e61f2 100644
@@ -5,6 +5,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "builtin.h"
 #include "parse-options.h"
builtin/describe.c +1 -0
index 893c8789f..70eb14460 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "commit.h"
 #include "tag.h"
builtin/diff-files.c +1 -0
index 15c61fd8d..29d3e4f41 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "revision.h"
builtin/diff-index.c +1 -0
index 1af373d00..e3d418361 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "revision.h"
builtin/diff-tree.c +1 -0
index 5ea1c1431..9af942b6b 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "log-tree.h"
builtin/diff.c +1 -0
index 8c03ddaf5..b85af112a 100644
@@ -4,6 +4,7 @@
  * Copyright (c) 2006 Junio C Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "color.h"
 #include "commit.h"
builtin/difftool.c +1 -0
index b9a892f26..9199227f6 100644
@@ -12,6 +12,7 @@
  * Copyright (C) 2016 Johannes Schindelin
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "run-command.h"
 #include "exec_cmd.h"
builtin/fast-export.c +1 -0
index 24e29ad7e..0dfd980dd 100644
@@ -5,6 +5,7 @@
  */
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "object.h"
builtin/fetch.c +1 -0
index d4d573b98..284595911 100644
@@ -2,6 +2,7 @@
  * "git fetch"
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "builtin.h"
builtin/fmt-merge-msg.c +1 -0
index 70137b0b7..10cbb4341 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "diff.h"
builtin/for-each-ref.c +1 -0
index eca365bf8..52be99cba 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "object.h"
 #include "parse-options.h"
builtin/fsck.c +1 -0
index cb2ba6cd1..90593491e 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "tree.h"
 #include "blob.h"
builtin/gc.c +1 -0
index f484eda43..bd91f136f 100644
@@ -11,6 +11,7 @@
  */
 
 #include "builtin.h"
+#include "config.h"
 #include "tempfile.h"
 #include "lockfile.h"
 #include "parse-options.h"
builtin/grep.c +1 -0
index c6c26e9b9..0f4a1e5a3 100644
@@ -4,6 +4,7 @@
  * Copyright (c) 2006 Junio C Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "blob.h"
 #include "tree.h"
 #include "commit.h"
builtin/hash-object.c +1 -0
index bbeaf20bc..d04baf999 100644
@@ -5,6 +5,7 @@
  * Copyright (C) Junio C Hamano, 2005
  */
 #include "builtin.h"
+#include "config.h"
 #include "blob.h"
 #include "quote.h"
 #include "parse-options.h"
builtin/help.c +1 -0
index 49f7a07f8..334a8494a 100644
@@ -2,6 +2,7 @@
  * Builtin help command
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "parse-options.h"
builtin/index-pack.c +1 -0
index 04b9dcaf0..edc1a91d8 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "delta.h"
 #include "pack.h"
 #include "csum-file.h"
builtin/init-db.c +1 -0
index 8a6acb0ec..47823f9aa 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "builtin.h"
 #include "exec_cmd.h"
builtin/log.c +1 -0
index a440601ef..72a9fd968 100644
@@ -5,6 +5,7 @@
  *		 2006 Junio Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "color.h"
 #include "commit.h"
builtin/ls-files.c +1 -0
index b376afc31..c4357dc30 100644
@@ -6,6 +6,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "quote.h"
 #include "dir.h"
 #include "builtin.h"
builtin/ls-tree.c +1 -0
index ee7b293b1..ef965408e 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "blob.h"
 #include "tree.h"
 #include "commit.h"
builtin/merge-base.c +1 -0
index 0c36a70ad..6dbd167d3 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "diff.h"
builtin/merge-file.c +1 -0
index 47dde7c39..b08803e61 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "xdiff/xdiff.h"
 #include "xdiff-interface.h"
 #include "parse-options.h"
builtin/merge.c +1 -0
index a4a098f40..2512370c1 100644
@@ -7,6 +7,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "builtin.h"
 #include "lockfile.h"
builtin/mv.c +1 -0
index 61d20037a..dcf6736b5 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2006 Johannes Schindelin
  */
 #include "builtin.h"
+#include "config.h"
 #include "pathspec.h"
 #include "lockfile.h"
 #include "dir.h"
builtin/name-rev.c +1 -0
index 7fc7e66e8..e21715f1d 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "tag.h"
 #include "refs.h"
builtin/notes.c +1 -0
index f2847c41e..231ad1f01 100644
@@ -8,6 +8,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "notes.h"
 #include "blob.h"
builtin/pack-objects.c +1 -0
index 80439047a..9fe1fa8e1 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "object.h"
 #include "blob.h"
builtin/patch-id.c +1 -0
index 81552e02e..970d0d30b 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 
 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
 {
builtin/pull.c +1 -0
index 318c273eb..af325d9fc 100644
@@ -6,6 +6,7 @@
  * Fetch one or more remote refs and merge it/them into the current HEAD.
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 #include "exec_cmd.h"
builtin/push.c +1 -0
index a597759d8..76aa713d2 100644
@@ -2,6 +2,7 @@
  * "git push"
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "run-command.h"
 #include "builtin.h"
builtin/read-tree.c +1 -0
index 78d319365..6eb2a0e57 100644
@@ -5,6 +5,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "object.h"
 #include "tree.h"
builtin/rebase--helper.c +1 -0
index ca1ebb2fa..c82b4dce6 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "sequencer.h"
 
builtin/receive-pack.c +1 -0
index b1706a573..71c0c768d 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "pack.h"
 #include "refs.h"
builtin/reflog.c +1 -0
index 920c16dac..44cdc2dbd 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "commit.h"
 #include "refs.h"
builtin/remote.c +1 -0
index addf97ad2..a470ed7c6 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "parse-options.h"
 #include "transport.h"
 #include "remote.h"
builtin/repack.c +1 -0
index 38ba4ef82..f17a68a17 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "parse-options.h"
 #include "run-command.h"
builtin/replace.c +1 -0
index c921bc976..80a15cf35 100644
@@ -9,6 +9,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "refs.h"
 #include "parse-options.h"
builtin/rerere.c +1 -0
index 1bf72423b..ffb66e290 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/reset.c +1 -0
index 430602d10..d0b604103 100644
@@ -8,6 +8,7 @@
  * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
  */
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "tag.h"
 #include "object.h"
builtin/rev-list.c +1 -0
index 718c6059c..f8e2b7bf5 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "diff.h"
 #include "revision.h"
builtin/rev-parse.c +1 -0
index efdc14473..c78b7b33d 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "quote.h"
builtin/revert.c +1 -0
index 345d9586a..16028b9ea 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 #include "diff.h"
builtin/rm.c +1 -0
index 7c323d012..824d1de9e 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds 2006
  */
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "dir.h"
 #include "cache-tree.h"
builtin/send-pack.c +1 -0
index b8e2e74fe..633e0c3cd 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "pkt-line.h"
builtin/shortlog.c +1 -0
index 7cff1839f..43c4799ea 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "diff.h"
 #include "string-list.h"
builtin/show-branch.c +1 -0
index 4a6cc6f49..e4cf1b5bb 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "builtin.h"
builtin/stripspace.c +1 -0
index 1e62a008c..bdf032886 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "strbuf.h"
 
builtin/submodule--helper.c +1 -0
index 8cc648d85..4dcbfb952 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "quote.h"
 #include "pathspec.h"
builtin/symbolic-ref.c +1 -0
index 70addef15..df75cb9d4 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "cache.h"
 #include "refs.h"
 #include "parse-options.h"
builtin/tag.c +1 -0
index 1f74a56db..01154ea8d 100644
@@ -7,6 +7,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "refs.h"
 #include "tag.h"
builtin/unpack-file.c +1 -0
index 6fc6bcdf7..73f133419 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 
 static char *create_temp_file(unsigned char *sha1)
 {
builtin/unpack-objects.c +1 -0
index 8bc999776..193f8b9d5 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "object.h"
 #include "delta.h"
 #include "pack.h"
builtin/update-index.c +1 -0
index ebfc09faa..0a4c23648 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "quote.h"
 #include "cache-tree.h"
builtin/update-ref.c +1 -0
index 0b2ecf41a..40ccfc193 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "builtin.h"
 #include "parse-options.h"
builtin/update-server-info.c +1 -0
index 6c8cc3edc..873070e51 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 
builtin/var.c +1 -0
index aedbb53a2..6c6f46b4a 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Eric Biederman, 2005
  */
 #include "builtin.h"
+#include "config.h"
 
 static const char var_usage[] = "git var (-l | <variable>)";
 
builtin/verify-commit.c +1 -0
index 05b734e6d..ba38ac9b1 100644
@@ -6,6 +6,7 @@
  * Based on git-verify-tag
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "commit.h"
 #include "run-command.h"
builtin/verify-pack.c +1 -0
index c94e15693..c2a1a5c50 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "parse-options.h"
 
builtin/verify-tag.c +1 -0
index 5199553d9..f9a5f7535 100644
@@ -6,6 +6,7 @@
  * Based on git-verify-tag.sh
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "tag.h"
 #include "run-command.h"
builtin/worktree.c +1 -0
index 793306ea5..0c5476ee9 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "dir.h"
 #include "parse-options.h"
builtin/write-tree.c +1 -0
index 084c0df78..bd0a78aa3 100644
@@ -5,6 +5,7 @@
  */
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "tree.h"
 #include "cache-tree.h"
 #include "parse-options.h"
cache.h +0 -1
index a27fee458..272f8e021 100644
@@ -11,7 +11,6 @@
 #include "string-list.h"
 #include "pack-revindex.h"
 #include "hash.h"
-#include "config.h"
 
 #ifndef platform_SHA_CTX
 /*
color.c +1 -0
index dee61557e..31b6207a0 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "color.h"
 
 static int git_use_color_default = GIT_COLOR_AUTO;
column.c +1 -0
index d55ead18e..ff7bdab1a 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
config.c +1 -0
index 146cb3452..2390f98e3 100644
@@ -6,6 +6,7 @@
  *
  */
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "exec_cmd.h"
 #include "strbuf.h"
connect.c +1 -0
index cd21a1b6f..efddb30ea 100644
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "cache.h"
+#include "config.h"
 #include "pkt-line.h"
 #include "quote.h"
 #include "refs.h"
convert.c +1 -0
index f1e168bc3..5f4a4b1f5 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "run-command.h"
 #include "quote.h"
credential-cache--daemon.c +1 -0
index f3814cc47..0d5c62509 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "credential.h"
 #include "unix-socket.h"
credential.c +1 -0
index aa996669f..67a523353 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "credential.h"
 #include "string-list.h"
 #include "run-command.h"
daemon.c +1 -0
index ac7181a48..30747075f 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "pkt-line.h"
 #include "run-command.h"
 #include "strbuf.h"
diff.c +1 -0
index f3546536b..2f2467c6d 100644
@@ -2,6 +2,7 @@
  * Copyright (C) 2005 Junio C Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "quote.h"
 #include "diff.h"
dir.c +1 -0
index 3f3167e55..0c26a53d2 100644
@@ -9,6 +9,7 @@
  */
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "attr.h"
 #include "refs.h"
environment.c +1 -0
index aa478e71d..d40b21fb7 100644
@@ -8,6 +8,7 @@
  * are.
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "fmt-merge-msg.h"
 #include "commit.h"
fast-import.c +1 -0
index e69d21968..2881d4898 100644
@@ -154,6 +154,7 @@ Format of STDIN stream:
 
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "object.h"
 #include "blob.h"
fetch-pack.c +1 -0
index cd86865be..fbbc99c88 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "refs.h"
 #include "pkt-line.h"
git.c +1 -0
index 8ff44f081..594436e43 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "exec_cmd.h"
 #include "help.h"
 #include "run-command.h"
gpg-interface.c +1 -0
index e44cc27da..8ab32df45 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "gpg-interface.h"
graph.c +1 -0
index 8b9049dd2..e7e20650d 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "color.h"
 #include "graph.h"
grep.c +1 -0
index 47cee4506..d5211fc5a 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "grep.h"
 #include "userdiff.h"
 #include "xdiff-interface.h"
help.c +1 -0
index db7f3d79a..5d44f811a 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "run-command.h"
http-backend.c +1 -0
index ba5ff1aa2..519025d2c 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "pkt-line.h"
 #include "object.h"
http-fetch.c +1 -0
index 3b556d661..8af380050 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "exec_cmd.h"
 #include "http.h"
 #include "walker.h"
http.c +1 -0
index d2e11ec6f..013bb0cc6 100644
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "http.h"
+#include "config.h"
 #include "pack.h"
 #include "sideband.h"
 #include "run-command.h"
ident.c +1 -0
index bea871c8e..d41fc9119 100644
@@ -6,6 +6,7 @@
  * Copyright (C) 2005 Linus Torvalds
  */
 #include "cache.h"
+#include "config.h"
 
 static struct strbuf git_default_name = STRBUF_INIT;
 static struct strbuf git_default_email = STRBUF_INIT;
imap-send.c +1 -0
index 857591660..59e9b12d2 100644
@@ -23,6 +23,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "credential.h"
 #include "exec_cmd.h"
 #include "run-command.h"
ll-merge.c +1 -0
index ac0d4a5d7..24ff94e1d 100644
@@ -5,6 +5,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "xdiff-interface.h"
 #include "run-command.h"
log-tree.c +1 -0
index a4ec11c2b..7e1dfd749 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "tag.h"
mailinfo.c +1 -0
index f92cb9f72..f59162453 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "utf8.h"
 #include "strbuf.h"
 #include "mailinfo.h"
merge-recursive.c +1 -0
index ae5238d82..c2494f34f 100644
@@ -4,6 +4,7 @@
  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
  */
 #include "cache.h"
+#include "config.h"
 #include "advice.h"
 #include "lockfile.h"
 #include "cache-tree.h"
notes-utils.c +1 -0
index 325ff3daa..ebde2273c 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "notes-utils.h"
notes.c +1 -0
index 542563b28..dbcfef4d7 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "notes.h"
 #include "blob.h"
 #include "tree.h"
pager.c +1 -0
index c113d898a..4dd9e1b26 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "sigchain.h"
 
parse-options.c +1 -0
index a23a1e67f..cbf84a604 100644
@@ -1,6 +1,7 @@
 #include "git-compat-util.h"
 #include "parse-options.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "color.h"
 #include "utf8.h"
pathspec.c +1 -0
index 828405021..ecc5331c2 100644
@@ -1,5 +1,6 @@
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "pathspec.h"
 #include "attr.h"
pretty.c +1 -0
index 09701bd2f..9c9f81b5b 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "utf8.h"
 #include "diff.h"
prompt.c +1 -0
index 75406390c..6d5885d00 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
read-cache.c +1 -0
index 92eb15c00..2650ad98a 100644
@@ -5,6 +5,7 @@
  */
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "lockfile.h"
 #include "cache-tree.h"
refs.c +1 -0
index 8af9641aa..7dfdc08bf 100644
@@ -3,6 +3,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "hashmap.h"
 #include "lockfile.h"
 #include "iterator.h"
refs/files-backend.c +1 -0
index cb1f528cb..f88b94484 100644
@@ -1,4 +1,5 @@
 #include "../cache.h"
+#include "../config.h"
 #include "../refs.h"
 #include "refs-internal.h"
 #include "ref-cache.h"
remote-curl.c +1 -0
index ece45993d..0053b0954 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "remote.h"
 #include "strbuf.h"
 #include "walker.h"
remote.c +1 -0
index fdc52d802..a84e68767 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "remote.h"
 #include "refs.h"
 #include "commit.h"
rerere.c +1 -0
index 3bd55caf3..344d6aa81 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "string-list.h"
 #include "rerere.h"
send-pack.c +1 -0
index 78bb34ebe..ed3cee321 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "pkt-line.h"
sequencer.c +1 -0
index a23b948ac..b9962dc89 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "sequencer.h"
 #include "dir.h"
setup.c +1 -0
index e3f7699a9..e99a82cbe 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "string-list.h"
 
sha1_file.c +1 -0
index 59a4ed2ed..44561e0b9 100644
@@ -7,6 +7,7 @@
  * creation etc.
  */
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 #include "lockfile.h"
 #include "delta.h"
sha1_name.c +1 -0
index 389276e9d..0ad85ec54 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "tag.h"
 #include "commit.h"
 #include "tree.h"
submodule-config.c +1 -0
index 4f58491dd..d8f8d5ea3 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "submodule-config.h"
 #include "submodule.h"
 #include "strbuf.h"
submodule.c +1 -0
index bf5a93d16..95328de61 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "submodule-config.h"
 #include "submodule.h"
 #include "dir.h"
t/helper/test-config.c +1 -0
index 8e3ed6a76..1a7b8bd3d 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 
 /*
t/helper/test-submodule-config.c +1 -0
index 2f144d539..c6c57bba0 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "submodule-config.h"
 #include "submodule.h"
 
trailer.c +1 -0
index 11f0b9fb4..751b56c00 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 #include "run-command.h"
 #include "commit.h"
transport.c +1 -0
index 9bfcf870f..b9995306f 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "transport.h"
 #include "run-command.h"
 #include "pkt-line.h"
unpack-trees.c +1 -0
index d38c37e38..dd535bc84 100644
@@ -1,5 +1,6 @@
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "tree.h"
 #include "tree-walk.h"
upload-pack.c +1 -0
index 5330c02c1..7efff2fbf 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "pkt-line.h"
 #include "sideband.h"
userdiff.c +1 -0
index 8b732e40b..2c1502f71 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "userdiff.h"
 #include "attr.h"
 
versioncmp.c +1 -0
index 9f81dc106..069ee94a4 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 
 /*
wrapper.c +1 -0
index d83741770..487a9f753 100644
@@ -2,6 +2,7 @@
  * Various trivial helper wrappers around standard functions
  */
 #include "cache.h"
+#include "config.h"
 
 static void do_nothing(size_t size)
 {
xdiff-interface.c +1 -0
index 060038c2d..5ac07d734 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "xdiff-interface.h"
 #include "xdiff/xtypes.h"
 #include "xdiff/xdiffi.h"
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#3 lore raw

PATCH 3/31 config: don't implicitly use gitdir

In reply to Brandon Williams

Commit 2185fde56 (config: handle conditional include when $GIT_DIR is not set up) added a 'git_dir' field to the config_options struct. Let's use this option field explicitly all the time instead of occasionally falling back to calling 'git_pathdup("config")' to get the path to the local repository configuration. This allows 'go_git_config_sequence()' to not implicitly rely on global repository state.

---
 builtin/config.c | 2 ++
 config.c         | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
builtin/config.c +2 -0
index 753c40a5c..90f49a6ee 100644
@@ -539,6 +539,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		config_options.respect_includes = !given_config_source.file;
 	else
 		config_options.respect_includes = respect_includes_opt;
+	if (have_git_dir())
+		config_options.git_dir = get_git_common_dir();
 
 	if (end_null) {
 		term = '\0';
config.c +2 -2
index 2390f98e3..ff09b27b8 100644
@@ -1548,8 +1548,6 @@ static int do_git_config_sequence(const struct config_options *opts,
 
 	if (opts->git_dir)
 		repo_config = mkpathdup("%s/config", opts->git_dir);
-	else if (have_git_dir())
-		repo_config = git_pathdup("config");
 	else
 		repo_config = NULL;
 
@@ -1613,6 +1611,8 @@ static void git_config_raw(config_fn_t fn, void *data)
 	struct config_options opts = {0};
 
 	opts.respect_includes = 1;
+	if (have_git_dir())
+		opts.git_dir = get_git_common_dir();
 	if (git_config_with_options(fn, data, NULL, &opts) < 0)
 		/*
 		 * git_config_with_options() normally returns only
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#4 lore raw

Re: [PATCH 03/31] config: don't implicitly use gitdir

Hi Brandon,

Quoted 6 lines · Brandon Williams

Commit 2185fde56 (config: handle conditional include when $GIT_DIR is not set up) added a 'git_dir' field to the config_options struct. Let's use this option field explicitly all the time instead of occasionally falling back to calling 'git_pathdup("config")' to get the path to the local repository configuration. This allows 'go_git_config_sequence()' to not implicitly rely on global repository state.

Oh wow. git_pathdup()...

Sadly, I lack the time to review the entire patch series, but this here change definitely looks correct to me.

Thanks,
Dscho

#5 lore raw

Re: [PATCH 03/31] config: don't implicitly use gitdir

Quoted 11 lines · Johannes Schindelin

Hi Brandon,

Quoted 6 lines · Brandon Williams

Commit 2185fde56 (config: handle conditional include when $GIT_DIR is not set up) added a 'git_dir' field to the config_options struct. Let's use this option field explicitly all the time instead of occasionally falling back to calling 'git_pathdup("config")' to get the path to the local repository configuration. This allows 'go_git_config_sequence()' to not implicitly rely on global repository state.

Quoted 4 lines

Oh wow. git_pathdup()...

Sadly, I lack the time to review the entire patch series, but this here change definitely looks correct to me.

Thanks for taking a look. I understand you don't have time to review the series in whole but I should also mention that patch 04/31 may be of interest to you as it deals with setup being slightly more robust and since you've spent a great deal of time in that bit of code any input would be nice. Last prod I promise ;)

Signature · 1 line
Brandon Williams
#6 lore raw

Re: [PATCH 03/31] config: don't implicitly use gitdir

Hi Brandon,

Quoted 13 lines · Brandon Williams
Quoted 12 lines · Johannes Schindelin
Quoted 6 lines · Brandon Williams

Commit 2185fde56 (config: handle conditional include when $GIT_DIR is not set up) added a 'git_dir' field to the config_options struct. Let's use this option field explicitly all the time instead of occasionally falling back to calling 'git_pathdup("config")' to get the path to the local repository configuration. This allows 'go_git_config_sequence()' to not implicitly rely on global repository state.

Oh wow. git_pathdup()...

Quoted 8 lines
Quoted 2 lines

Sadly, I lack the time to review the entire patch series, but this here change definitely looks correct to me.

Thanks for taking a look. I understand you don't have time to review the series in whole but I should also mention that patch 04/31 may be of interest to you as it deals with setup being slightly more robust and since you've spent a great deal of time in that bit of code any input would be nice. Last prod I promise ;)

I had a quick look over the patch and I like the direction.

I also briefly scanned the discussion and have to admit that I am not knee-deep in those exceptional code paths that do strange^Wuncommon things, so I feel a bit unqualified to comment about those.

Ciao,
Dscho

#7 lore raw

Re: [PATCH 03/31] config: don't implicitly use gitdir

Quoted 26 lines · Johannes Schindelin

Hi Brandon,

Quoted 13 lines · Brandon Williams
Quoted 12 lines · Johannes Schindelin
Quoted 6 lines · Brandon Williams

Commit 2185fde56 (config: handle conditional include when $GIT_DIR is not set up) added a 'git_dir' field to the config_options struct. Let's use this option field explicitly all the time instead of occasionally falling back to calling 'git_pathdup("config")' to get the path to the local repository configuration. This allows 'go_git_config_sequence()' to not implicitly rely on global repository state.

Oh wow. git_pathdup()...

Quoted 8 lines
Quoted 2 lines

Sadly, I lack the time to review the entire patch series, but this here change definitely looks correct to me.

Thanks for taking a look. I understand you don't have time to review the series in whole but I should also mention that patch 04/31 may be of interest to you as it deals with setup being slightly more robust and since you've spent a great deal of time in that bit of code any input would be nice. Last prod I promise ;)

Quoted 5 lines

I had a quick look over the patch and I like the direction.

I also briefly scanned the discussion and have to admit that I am not knee-deep in those exceptional code paths that do strange^Wuncommon things, so I feel a bit unqualified to comment about those.

All good, thanks for looking!

Signature · 1 line
Brandon Williams
#8 lore raw

PATCH 4/31 setup: don't perform lazy initialization of repository state

In reply to Brandon Williams

Under some circumstances (bogus GIT_DIR value or the discovered gitdir is '.git') 'setup_git_directory()' won't initialize key repository state. This leads to inconsistent state after running the setup code. To account for this inconsistent state, lazy initialization is done once a caller asks for the repository's gitdir or some other piece of repository state. This is confusing and can be error prone.

Instead let's tighten the expected outcome of 'setup_git_directory()' and ensure that it initializes repository state in all cases that would have been handled by lazy initialization.

This also lets us drop the requirement to have 'have_git_dir()' check if the environment variable GIT_DIR was set as that will be handled by the end of the setup code.

---
 cache.h       |  2 ++
 environment.c | 17 ++++++++---------
 setup.c       | 14 ++++++++++++++
 3 files changed, 24 insertions(+), 9 deletions(-)
cache.h +2 -0
index 272f8e021..d41aab82f 100644
@@ -462,6 +462,8 @@ static inline enum object_type object_type(unsigned int mode)
  */
 extern const char * const local_repo_env[];
 
+extern void setup_git_env(void);
+
 /*
  * Returns true iff we have a configured git repository (either via
  * setup_git_directory, or in the environment via $GIT_DIR).
environment.c +8 -9
index d40b21fb7..a73b08f5d 100644
@@ -160,7 +160,7 @@ static char *git_path_from_env(const char *envvar, const char *git_dir,
 	return xstrdup(value);
 }
 
-static void setup_git_env(void)
+void setup_git_env(void)
 {
 	struct strbuf sb = STRBUF_INIT;
 	const char *gitfile;
@@ -205,28 +205,27 @@ int is_bare_repository(void)
 int have_git_dir(void)
 {
 	return startup_info->have_repository
-		|| git_dir
-		|| getenv(GIT_DIR_ENVIRONMENT);
+		|| git_dir;
 }
 
 const char *get_git_dir(void)
 {
 	if (!git_dir)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_dir;
 }
 
 const char *get_git_common_dir(void)
 {
 	if (!git_dir)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_common_dir;
 }
 
 const char *get_git_namespace(void)
 {
 	if (!namespace)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return namespace;
 }
 
@@ -276,7 +275,7 @@ const char *get_git_work_tree(void)
 char *get_object_directory(void)
 {
 	if (!git_object_dir)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_object_dir;
 }
 
@@ -316,14 +315,14 @@ int odb_pack_keep(const char *name)
 char *get_index_file(void)
 {
 	if (!git_index_file)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_index_file;
 }
 
 char *get_graft_file(void)
 {
 	if (!git_graft_file)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_graft_file;
 }
 
setup.c +14 -0
index e99a82cbe..b2e05145c 100644
@@ -1063,6 +1063,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	startup_info->have_repository = !nongit_ok || !*nongit_ok;
 	startup_info->prefix = prefix;
 
+	/*
+	 * Not all paths through the setup code will call 'set_git_dir()' (which
+	 * directly sets up the environment) so in order to guarantee that the
+	 * environment is in a consistent state after setup, explicitly setup
+	 * the environment if we have a repository.
+	 *
+	 * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
+	 * code paths so we also need to explicitly setup the environment if
+	 * the user has set GIT_DIR.  It may be beneficial to disallow bogus
+	 * GIT_DIR values at some point in the future.
+	 */
+	if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT))
+		setup_git_env();
+
 	strbuf_release(&dir);
 	strbuf_release(&gitdir);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#9 lore raw

Re: [PATCH 04/31] setup: don't perform lazy initialization of repository state

Quoted 10 lines · Brandon Williams

Under some circumstances (bogus GIT_DIR value or the discovered gitdir is '.git') 'setup_git_directory()' won't initialize key repository state. This leads to inconsistent state after running the setup code. To account for this inconsistent state, lazy initialization is done once a caller asks for the repository's gitdir or some other piece of repository state. This is confusing and can be error prone.

Instead let's tighten the expected outcome of 'setup_git_directory()' and ensure that it initializes repository state in all cases that would have been handled by lazy initialization.

Lazy init is usually there for a reason. (As in: it took too long to perform it at all times, so it has been optimized to only perform the init when needed).

Reading the code of setup_git_env, this is the additional cost incurred to us:
* reading a file ('git_dir' to determine if it is a gitlink or actual directory)
* reading another file to determine the commondir
* some environment variable reading (<10).
Reading the environment should be fast, though in the implementation of libc that I just looked up, it is still a linear search, so O(n) with n the number of environment variables. (No actual syscalls though.)

I would think that this is ok as it incurs not much cost. Maybe the lazyloading implementation was just a habit of the contributors at the time.

Quoted 4 lines

const char *get_git_dir(void)

 {
        if (!git_dir)

- setup_git_env();

NEEDSWORK: eventually (say after release 2.16)
we can remove all these

    if (!git_dir)
        BUG(...);

calls. I will read on, as maybe this series touches this further

#10 lore raw

Re: [PATCH 04/31] setup: don't perform lazy initialization of repository state

Quoted 8 lines · Stefan Beller
Quoted 7 lines · Brandon Williams

Under some circumstances (bogus GIT_DIR value or the discovered gitdir is '.git') 'setup_git_directory()' won't initialize key repository state. This leads to inconsistent state after running the setup code. To account for this inconsistent state, lazy initialization is done once a caller asks for the repository's gitdir or some other piece of repository state. This is confusing and can be error prone.

Quoted 6 lines
Quoted 3 lines

Instead let's tighten the expected outcome of 'setup_git_directory()' and ensure that it initializes repository state in all cases that would have been handled by lazy initialization.

Lazy init is usually there for a reason. (As in: it took too long to perform it at all times, so it has been optimized to only perform the init when needed).

In the case of setup_git_env(), I think it was less about doing work and more that we didn't want to have to do explicit setup in each program. But over the years we've moved away from that, and in fact if you hit the lazy initialization these days you'll generally BUG() anyway.

_But_ I suspect there are still some cases you'd need to handle. For instance, it's still OK to skip calling setup_git_directory() if you've got $GIT_DIR in the environment (which is why we have have_git_dir() instead of checking startup_info->have_repository).

I think it would be nice to do away with that, too, but we're not quite there yet (and if I am reading this patch correctly, we'd probably hit these BUGs in such cases).

-Peff

#11 lore raw

Re: [PATCH 04/31] setup: don't perform lazy initialization of repository state

Quoted 22 lines · Jeff King
Quoted 8 lines · Stefan Beller
Quoted 7 lines · Brandon Williams

Under some circumstances (bogus GIT_DIR value or the discovered gitdir is '.git') 'setup_git_directory()' won't initialize key repository state. This leads to inconsistent state after running the setup code. To account for this inconsistent state, lazy initialization is done once a caller asks for the repository's gitdir or some other piece of repository state. This is confusing and can be error prone.

Quoted 6 lines
Quoted 3 lines

Instead let's tighten the expected outcome of 'setup_git_directory()' and ensure that it initializes repository state in all cases that would have been handled by lazy initialization.

Lazy init is usually there for a reason. (As in: it took too long to perform it at all times, so it has been optimized to only perform the init when needed).

In the case of setup_git_env(), I think it was less about doing work and more that we didn't want to have to do explicit setup in each program. But over the years we've moved away from that, and in fact if you hit the lazy initialization these days you'll generally BUG() anyway.

Quoted 4 lines

_But_ I suspect there are still some cases you'd need to handle. For instance, it's still OK to skip calling setup_git_directory() if you've got $GIT_DIR in the environment (which is why we have have_git_dir() instead of checking startup_info->have_repository).

Yes there are a couple places that rely on the lazy initialization but that's not due to setup not being run. Rather it has to do with GIT_DIR being set to a bogus directory so when setup is run gently it does nothing. Then at a later point in time the command tries to access files in the gitdir (which triggers lazy init of the git environment).

So I think that explicitly doing the 'lazy init' portion (which ensures that the env gets setup even if GIT_DIR is bogus) at the end of setup should be sufficient, least it seems to be so though perhaps we can't rely on our tests to tell us that.

Quoted 6 lines

I think it would be nice to do away with that, too, but we're not quite there yet (and if I am reading this patch correctly, we'd probably hit these BUGs in such cases).

-Peff

Signature · 1 line
Brandon Williams
#12 lore raw

Re: [PATCH 04/31] setup: don't perform lazy initialization of repository state

Quoted 16 lines · Brandon Williams
Quoted 9 lines

In the case of setup_git_env(), I think it was less about doing work and more that we didn't want to have to do explicit setup in each program. But over the years we've moved away from that, and in fact if you hit the lazy initialization these days you'll generally BUG() anyway.

_But_ I suspect there are still some cases you'd need to handle. For instance, it's still OK to skip calling setup_git_directory() if you've got $GIT_DIR in the environment (which is why we have have_git_dir() instead of checking startup_info->have_repository).

Yes there are a couple places that rely on the lazy initialization but that's not due to setup not being run. Rather it has to do with GIT_DIR being set to a bogus directory so when setup is run gently it does nothing. Then at a later point in time the command tries to access files in the gitdir (which triggers lazy init of the git environment).

Quoted 4 lines

So I think that explicitly doing the 'lazy init' portion (which ensures that the env gets setup even if GIT_DIR is bogus) at the end of setup should be sufficient, least it seems to be so though perhaps we can't rely on our tests to tell us that.

In have_git_dir() we do:

int have_git_dir(void)

  {
	  return startup_info->have_repository
		  || git_dir
		  || getenv(GIT_DIR_ENVIRONMENT);

}

and generally call that right before calling a function that might do setup_git_env(). We can assume that if have_repository is set that we have an actual git_dir (we can check all of the assignment spots and verify that they always set it in tandem with the global git_dir). And obviously if git_dir is set, we're good.

But if that third condition ever triggers, it's because we're relying on the lazy setup to copy it into git_dir. And if I understand correctly, that would turn into a BUG with your patch.

I guess my question is: does that third condition actually trigger in practice? I added those conditions in b9605bc4f (config: only read .git/config from configured repos, 2016-09-12). I remember there being some reason for needing those back then, but the commit message doesn't say. But if I remove them and just check have_repository (either on top of that commit or on top of the current master) the tests all still pass.

So I'm not sure at this point what the case was that motivated it, or if it really was just an abundance of caution. But if there is such a case, I suspect it's broken by your patch series.

I'm not sure we have a good way to find out, though. Like the current BUG() in setup_git_env, I think we'll just have to cook the patches for a long time, see if anybody triggers it, and deal with it on a case by case basis.

I do kind of wonder if we should simplify have_git_dir(), if those other conditions aren't actually triggering.

-Peff

#13 lore raw

Re: [PATCH 04/31] setup: don't perform lazy initialization of repository state

Quoted 42 lines · Jeff King
Quoted 16 lines · Brandon Williams
Quoted 9 lines

In the case of setup_git_env(), I think it was less about doing work and more that we didn't want to have to do explicit setup in each program. But over the years we've moved away from that, and in fact if you hit the lazy initialization these days you'll generally BUG() anyway.

_But_ I suspect there are still some cases you'd need to handle. For instance, it's still OK to skip calling setup_git_directory() if you've got $GIT_DIR in the environment (which is why we have have_git_dir() instead of checking startup_info->have_repository).

Yes there are a couple places that rely on the lazy initialization but that's not due to setup not being run. Rather it has to do with GIT_DIR being set to a bogus directory so when setup is run gently it does nothing. Then at a later point in time the command tries to access files in the gitdir (which triggers lazy init of the git environment).

Quoted 4 lines

So I think that explicitly doing the 'lazy init' portion (which ensures that the env gets setup even if GIT_DIR is bogus) at the end of setup should be sufficient, least it seems to be so though perhaps we can't rely on our tests to tell us that.

In have_git_dir() we do:

int have_git_dir(void)

  {
	  return startup_info->have_repository
		  || git_dir
		  || getenv(GIT_DIR_ENVIRONMENT);

}

and generally call that right before calling a function that might do setup_git_env(). We can assume that if have_repository is set that we have an actual git_dir (we can check all of the assignment spots and verify that they always set it in tandem with the global git_dir). And obviously if git_dir is set, we're good.

But if that third condition ever triggers, it's because we're relying on the lazy setup to copy it into git_dir. And if I understand correctly, that would turn into a BUG with your patch.

Quoted 7 lines

I guess my question is: does that third condition actually trigger in practice? I added those conditions in b9605bc4f (config: only read .git/config from configured repos, 2016-09-12). I remember there being some reason for needing those back then, but the commit message doesn't say. But if I remove them and just check have_repository (either on top of that commit or on top of the current master) the tests all still pass.

The only instance I saw the third condition trigger was when GIT_DIR was set to a bogus gitdir so when reading the early config a bogus gitdir/config file was attempted to be read resulting in a noop. If GIT_DIR is set to a valid gitdir then the other condition in read_early_config (which does repository discovery itself) would correctly catch and read from the repository's config.

Quoted 5 lines

So I'm not sure at this point what the case was that motivated it, or if it really was just an abundance of caution. But if there is such a case, I suspect it's broken by your patch series.

Quoted 7 lines

I'm not sure we have a good way to find out, though. Like the current BUG() in setup_git_env, I think we'll just have to cook the patches for a long time, see if anybody triggers it, and deal with it on a case by case basis.

I do kind of wonder if we should simplify have_git_dir(), if those other conditions aren't actually triggering.

If this ends up being that big of an issue I'm sure that it would be easy to adapt this and revert to doing lazy-init. I just saw how clunky it is and was trying to work to start the process of cleaning it up so that the code becomes easier to reason about. It seemed to make things easier for me to package them in a struct at least (and reason about how the config system would work once stored in a repository struct).

Signature · 1 line
Brandon Williams
#14 lore raw

PATCH 5/31 environment: remove namespace_len variable

In reply to Brandon Williams

Use 'skip_prefix' instead of 'starts_with' so that we can drop the need to keep around 'namespace_len'.

---
 environment.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
environment.c +4 -5
index a73b08f5d..e035f6372 100644
@@ -98,7 +98,6 @@ char *git_work_tree_cfg;
 static char *work_tree;
 
 static const char *namespace;
-static size_t namespace_len;
 
 static const char *super_prefix;
 
@@ -190,7 +189,6 @@ void setup_git_env(void)
 	git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
 							  : "refs/replace/");
 	namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
-	namespace_len = strlen(namespace);
 	shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
 	if (shallow_file)
 		set_alternate_shallow_file(shallow_file, 0);
@@ -231,9 +229,10 @@ const char *get_git_namespace(void)
 
 const char *strip_namespace(const char *namespaced_ref)
 {
-	if (!starts_with(namespaced_ref, get_git_namespace()))
-		return NULL;
-	return namespaced_ref + namespace_len;
+	const char *out;
+	if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
+		return out;
+	return NULL;
 }
 
 const char *get_super_prefix(void)
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#15 lore raw

Re: [PATCH 05/31] environment: remove namespace_len variable

Quoted 2 lines · Brandon Williams

Use 'skip_prefix' instead of 'starts_with' so that we can drop the need to keep around 'namespace_len'.

Looks correct.

Performance wise:
The strip_namespace function is only called from {receive/upload}-pack and http-backend, so all network operations, whose actual operations should far outweight the tiny CPU saving that this reverts. We should be fine?

Quoted 42 lines
---
 environment.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
environment.c +4 -6
index a73b08f5d..e035f6372 100644
@@ -98,7 +98,6 @@ char *git_work_tree_cfg;
 static char *work_tree;
 
 static const char *namespace;
-static size_t namespace_len;
 
 static const char *super_prefix;
 
@@ -190,7 +189,6 @@ void setup_git_env(void)
        git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
                                                          : "refs/replace/");
        namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
-       namespace_len = strlen(namespace);
        shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
        if (shallow_file)
                set_alternate_shallow_file(shallow_file, 0);
@@ -231,9 +229,10 @@ const char *get_git_namespace(void)
 
 const char *strip_namespace(const char *namespaced_ref)
 {
-       if (!starts_with(namespaced_ref, get_git_namespace()))
-               return NULL;
-       return namespaced_ref + namespace_len;
+       const char *out;
+       if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
+               return out;
+       return NULL;
 }
 
 const char *get_super_prefix(void)
--

2.13.0.506.g27d5fe0cd-goog

#16 lore raw

Re: [PATCH 05/31] environment: remove namespace_len variable

Quoted 11 lines · Stefan Beller
Quoted 2 lines · Brandon Williams

Use 'skip_prefix' instead of 'starts_with' so that we can drop the need to keep around 'namespace_len'.

Looks correct.

Performance wise:
The strip_namespace function is only called from {receive/upload}-pack and http-backend, so all network operations, whose actual operations should far outweight the tiny CPU saving that this reverts. We should be fine?

Actually there should be zero performance degradation with this change (maybe a very small improvement) because both functions need to do a char by char comparison, just skip_prefix returns a pointer into the string with the prefix already stripped. So no need to do a strlen() calculation on 'namespace' as well as no additon operation adding 'namespace_len' to 'namespaced_ref'.

Quoted 43 lines
Quoted 43 lines
---
 environment.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
environment.c +4 -6
index a73b08f5d..e035f6372 100644
@@ -98,7 +98,6 @@ char *git_work_tree_cfg;
 static char *work_tree;
 
 static const char *namespace;
-static size_t namespace_len;
 
 static const char *super_prefix;
 
@@ -190,7 +189,6 @@ void setup_git_env(void)
        git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
                                                          : "refs/replace/");
        namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
-       namespace_len = strlen(namespace);
        shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
        if (shallow_file)
                set_alternate_shallow_file(shallow_file, 0);
@@ -231,9 +229,10 @@ const char *get_git_namespace(void)
 
 const char *strip_namespace(const char *namespaced_ref)
 {
-       if (!starts_with(namespaced_ref, get_git_namespace()))
-               return NULL;
-       return namespaced_ref + namespace_len;
+       const char *out;
+       if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
+               return out;
+       return NULL;
 }
 
 const char *get_super_prefix(void)
--

2.13.0.506.g27d5fe0cd-goog

Signature · 1 line
Brandon Williams
#17 lore raw

PATCH 6/31 repo: introduce the repository object

In reply to Brandon Williams

Introduce the repository object 'struct repo' which can be used hold all state pertaining to a git repository.

The aim of object-ifying the repository is to (1) make the code base more readable and easier to reason about and (2) allow for working on multiple repositories, specifically submodules, within the same process.

TODO: Add more motivating points for adding a repository object?

---
 Makefile      |   1 +
 cache.h       |   1 +
 environment.c |   2 +-
 repo.c        | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 repo.h        |  25 ++++++++++++
 5 files changed, 152 insertions(+), 1 deletion(-)
 create mode 100644 repo.c
 create mode 100644 repo.h
Makefile +1 -0
index 2ed6db728..3d3d556ef 100644
@@ -821,6 +821,7 @@ LIB_OBJS += refs/ref-cache.o
 LIB_OBJS += ref-filter.o
 LIB_OBJS += remote.o
 LIB_OBJS += replace_object.o
+LIB_OBJS += repo.o
 LIB_OBJS += rerere.o
 LIB_OBJS += resolve-undo.o
 LIB_OBJS += revision.o
cache.h +1 -0
index d41aab82f..c0b4c8d83 100644
@@ -483,6 +483,7 @@ extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
 extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
 extern int get_common_dir(struct strbuf *sb, const char *gitdir);
+extern char *expand_namespace(const char *raw_namespace);
 extern const char *get_git_namespace(void);
 extern const char *strip_namespace(const char *namespaced_ref);
 extern const char *get_super_prefix(void);
environment.c +1 -1
index e035f6372..77900b31a 100644
@@ -127,7 +127,7 @@ const char * const local_repo_env[] = {
 	NULL
 };
 
-static char *expand_namespace(const char *raw_namespace)
+char *expand_namespace(const char *raw_namespace)
 {
 	struct strbuf buf = STRBUF_INIT;
 	struct strbuf **components, **c;
repo.c new file +124 -0

124 lines not on this page · raw message

repo.h new file +25 -0
new file mode 100644
index 000000000..453049e67
@@ -0,0 +1,25 @@
+#ifndef REPO_H
+#define REPO_H
+
+struct repo {
+	/* Environment */
+	char *gitdir;
+	char *commondir;
+	char *objectdir;
+	char *index_file;
+	char *graft_file;
+	char *namespace;
+
+	/* Configurations */
+	unsigned ignore_env:1;
+	/* Indicates if a repository has a different 'commondir' from 'gitdir' */
+	unsigned different_commondir:1;
+};
+
+extern struct repo the_repository;
+
+extern void repo_set_gitdir(struct repo *repo, const char *path);
+extern int repo_init(struct repo *repo, const char *path);
+extern void repo_clear(struct repo *repo);
+
+#endif /* REPO_H */
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#18 lore raw

Re: [PATCH 06/31] repo: introduce the repository object

Quoted 8 lines · Brandon Williams

Introduce the repository object 'struct repo' which can be used hold all state pertaining to a git repository.

The aim of object-ifying the repository is to (1) make the code base more readable and easier to reason about and (2) allow for working on multiple repositories, specifically submodules, within the same process.

TODO: Add more motivating points for adding a repository object?

Yes please (or delete this line).
https://public-inbox.org/git/alpine.DEB.2.21.1.1705221501540.3610@virtualbox/

repo.c line 6

Quoted 8 lines

+++ b/repo.c

repo.c new file +6 -0
@@ -0,0 +1,124 @@
+#include "cache.h"
+#include "repo.h"
+
+/*
+ * This may be the wrong place for this.
+ * It may be better to go in env.c or setup for the time being?

In env.c we say:
/*
* We put all the git config variables in this same object
* file, so that programs can link against the config parser
* without having to link against all the rest of git. *
* In particular, no need to bring in libz etc unless needed,
* even if you might want to know where the git directory etc
* are.
*/

And setup.c only has a few variables that matter there locally. So I would think having 'the_repository' in repo.c is acceptable.

Quoted 53 lines
repo.c new file +53 -0
+ */
+struct repo the_repository;
+
+static char *git_path_from_env(const char *envvar, const char *git_dir,
+                              const char *path, int fromenv)
+{
+       if (fromenv) {
+               const char *value = getenv(envvar);
+               if (value)
+                       return xstrdup(value);
+       }
+
+       return xstrfmt("%s/%s", git_dir, path);
+}
+
+static int find_common_dir(struct strbuf *sb, const char *gitdir, int fromenv)
+{
+       if (fromenv) {
+               const char *value = getenv(GIT_COMMON_DIR_ENVIRONMENT);
+               if (value) {
+                       strbuf_addstr(sb, value);
+                       return 1;
+               }
+       }
+
+       return get_common_dir_noenv(sb, gitdir);
+}
+
+/* called after setting gitdir */
+static void repo_setup_env(struct repo *repo)
+{
+       struct strbuf sb = STRBUF_INIT;
+
+       if (!repo->gitdir)
+               BUG("gitdir wasn't set before setting up the environment");
+
+       repo->different_commondir = find_common_dir(&sb, repo->gitdir,
+                                                   !repo->ignore_env);
+       repo->commondir = strbuf_detach(&sb, NULL);
+       repo->objectdir = git_path_from_env(DB_ENVIRONMENT, repo->commondir,
+                                           "objects", !repo->ignore_env);
+       repo->index_file = git_path_from_env(INDEX_ENVIRONMENT, repo->gitdir,
+                                            "index", !repo->ignore_env);
+       repo->graft_file = git_path_from_env(GRAFT_ENVIRONMENT, repo->commondir,
+                                            "info/grafts", !repo->ignore_env);
+       repo->namespace = expand_namespace(repo->ignore_env ? NULL :
+                                          getenv(GIT_NAMESPACE_ENVIRONMENT));
+}
+
+static void repo_clear_env(struct repo *repo)
+{
+       free(repo->gitdir);
+       repo->gitdir = NULL;

repo.c line 69

Quoted 10 lines
repo.c new file +10 -0
+       free(repo->commondir);
+       repo->commondir = NULL;
+       free(repo->objectdir);
+       repo->objectdir = NULL;
+       free(repo->index_file);
+       repo->index_file = NULL;
+       free(repo->graft_file);
+       repo->graft_file = NULL;
+       free(repo->namespace);
+       repo->namespace = NULL;

I wonder if we can defer the NULL assignments to
repo_clear, where we would just do a
memset(repo, 0, sizeof(struct repo));

repo.c line 112

Quoted 4 lines
repo.c new file +4 -0
+
+       repo_set_gitdir(repo, resolved_gitdir);
+
+       /* NEEDSWORK: Verify repository format version */

Care to elaborate on this? I do not understand why we would want to check the format version here?

repo.h line 23

Quoted 4 lines
repo.h new file +4 -0
+
+extern void repo_set_gitdir(struct repo *repo, const char *path);
+extern int repo_init(struct repo *repo, const char *path);
+extern void repo_clear(struct repo *repo);

The init and clear method seem obvious to me, but what do we need the repo_set_gitdir for externally? I would assume the repo auto-discovers its gitdir on its own?

#19 lore raw

Re: [PATCH 06/31] repo: introduce the repository object

Quoted 33 lines · Stefan Beller
Quoted 8 lines · Brandon Williams

Introduce the repository object 'struct repo' which can be used hold all state pertaining to a git repository.

The aim of object-ifying the repository is to (1) make the code base more readable and easier to reason about and (2) allow for working on multiple repositories, specifically submodules, within the same process.

TODO: Add more motivating points for adding a repository object?

Yes please (or delete this line).
https://public-inbox.org/git/alpine.DEB.2.21.1.1705221501540.3610@virtualbox/

repo.c line 6

Quoted 8 lines

+++ b/repo.c

repo.c new file +6 -0
@@ -0,0 +1,124 @@
+#include "cache.h"
+#include "repo.h"
+
+/*
+ * This may be the wrong place for this.
+ * It may be better to go in env.c or setup for the time being?

In env.c we say:
/*
* We put all the git config variables in this same object
* file, so that programs can link against the config parser
* without having to link against all the rest of git. *
* In particular, no need to bring in libz etc unless needed,
* even if you might want to know where the git directory etc
* are.
*/

Quoted 2 lines

And setup.c only has a few variables that matter there locally. So I would think having 'the_repository' in repo.c is acceptable.

And perhaps (far down the road) 'the_repoository' could be removed such that builtin commands take a pointer to a repo object as a parameter.

Quoted 60 lines
Quoted 60 lines
repo.c new file +59 -0
+ */
+struct repo the_repository;
+
+static char *git_path_from_env(const char *envvar, const char *git_dir,
+                              const char *path, int fromenv)
+{
+       if (fromenv) {
+               const char *value = getenv(envvar);
+               if (value)
+                       return xstrdup(value);
+       }
+
+       return xstrfmt("%s/%s", git_dir, path);
+}
+
+static int find_common_dir(struct strbuf *sb, const char *gitdir, int fromenv)
+{
+       if (fromenv) {
+               const char *value = getenv(GIT_COMMON_DIR_ENVIRONMENT);
+               if (value) {
+                       strbuf_addstr(sb, value);
+                       return 1;
+               }
+       }
+
+       return get_common_dir_noenv(sb, gitdir);
+}
+
+/* called after setting gitdir */
+static void repo_setup_env(struct repo *repo)
+{
+       struct strbuf sb = STRBUF_INIT;
+
+       if (!repo->gitdir)
+               BUG("gitdir wasn't set before setting up the environment");
+
+       repo->different_commondir = find_common_dir(&sb, repo->gitdir,
+                                                   !repo->ignore_env);
+       repo->commondir = strbuf_detach(&sb, NULL);
+       repo->objectdir = git_path_from_env(DB_ENVIRONMENT, repo->commondir,
+                                           "objects", !repo->ignore_env);
+       repo->index_file = git_path_from_env(INDEX_ENVIRONMENT, repo->gitdir,
+                                            "index", !repo->ignore_env);
+       repo->graft_file = git_path_from_env(GRAFT_ENVIRONMENT, repo->commondir,
+                                            "info/grafts", !repo->ignore_env);
+       repo->namespace = expand_namespace(repo->ignore_env ? NULL :
+                                          getenv(GIT_NAMESPACE_ENVIRONMENT));
+}
+
+static void repo_clear_env(struct repo *repo)
+{
+       free(repo->gitdir);
+       repo->gitdir = NULL;
+       free(repo->commondir);
+       repo->commondir = NULL;
+       free(repo->objectdir);
+       repo->objectdir = NULL;
+       free(repo->index_file);
+       repo->index_file = NULL;
Quoted 9 lines

repo.c line 69

Quoted 4 lines
repo.c new file +4 -0
+       free(repo->graft_file);
+       repo->graft_file = NULL;
+       free(repo->namespace);
+       repo->namespace = NULL;

I wonder if we can defer the NULL assignments to
repo_clear, where we would just do a
memset(repo, 0, sizeof(struct repo));

Yeah perhaps, clearing the env should either be done when setting gitdir again (so setting up the env will happen again and we don't need to clear the fields) or clearing the struct as a whole so using memset would work.

Quoted 7 lines

repo.c line 112

Quoted 4 lines
repo.c new file +4 -0
+
+       repo_set_gitdir(repo, resolved_gitdir);
+
+       /* NEEDSWORK: Verify repository format version */

Care to elaborate on this? I do not understand why we would want to check the format version here?

When opening up a repository git needs to check if it understands the repository format and all extensions. If it doesn't git needs to bail out and not operate on the repository. So a part of initializing a repo object would be to verify that it understands the repository format version.

Quoted 9 lines

repo.h line 23

Quoted 5 lines
repo.h new file +4 -0
+
+extern void repo_set_gitdir(struct repo *repo, const char *path);
+extern int repo_init(struct repo *repo, const char *path);
+extern void repo_clear(struct repo *repo);

The init and clear method seem obvious to me, but what do we need the repo_set_gitdir for externally? I would assume the repo auto-discovers its gitdir on its own?

Well I didn't completely overhaul the setup code in this series so I really just hooked into where the setup code already sets the gitdir, hence why this function is exposed atm.

Signature · 1 line
Brandon Williams
#20 lore raw

Re: [PATCH 06/31] repo: introduce the repository object

Quoted 8 lines
Quoted 7 lines

repo.c line 112

Quoted 4 lines
repo.c new file +4 -0
+
+       repo_set_gitdir(repo, resolved_gitdir);
+
+       /* NEEDSWORK: Verify repository format version */

Care to elaborate on this? I do not understand why we would want to check the format version here?

Quoted 5 lines

When opening up a repository git needs to check if it understands the repository format and all extensions. If it doesn't git needs to bail out and not operate on the repository. So a part of initializing a repo object would be to verify that it understands the repository format version.

Ah yes. Conceptually I understand why and what the repo format version check is. At the time of asking the question I was confused whether this is the right place, but after some thought this is the best place to put it.

And this NEEDSWORK is really just telling the reader that it needs to be moved here, not copied. (That was another brain fart I had upon reading this comment).

Thanks,
Stefan

#21 lore raw

PATCH 7/31 environment: place key repository state in the_repository

In reply to Brandon Williams

Migrate 'git_dir', 'git_common_dir', 'git_object_dir', 'git_index_file', 'git_graft_file', and 'namespace' to be stored in 'the_repository'.

---
 cache.h       |  1 -
 environment.c | 65 ++++++++++++++---------------------------------------------
 path.c        | 11 +++++-----
 setup.c       | 17 ++++++++++++++--
 4 files changed, 36 insertions(+), 58 deletions(-)
cache.h +0 -1
index c0b4c8d83..02ab5f801 100644
@@ -769,7 +769,6 @@ extern int core_apply_sparse_checkout;
 extern int precomposed_unicode;
 extern int protect_hfs;
 extern int protect_ntfs;
-extern int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
 
 /*
  * Include broken refs in all ref iterations, which will
environment.c +15 -50

141 lines not on this page · raw message

path.c +6 -5
index c1cb1cf62..a1a7c2653 100644
@@ -2,6 +2,7 @@
  * Utilities for paths and pathnames
  */
 #include "cache.h"
+#include "repo.h"
 #include "strbuf.h"
 #include "string-list.h"
 #include "dir.h"
@@ -355,7 +356,7 @@ void report_linked_checkout_garbage(void)
 	const struct common_dir *p;
 	int len;
 
-	if (!git_common_dir_env)
+	if (!the_repository.different_commondir)
 		return;
 	strbuf_addf(&sb, "%s/", get_git_dir());
 	len = sb.len;
@@ -374,17 +375,17 @@ void report_linked_checkout_garbage(void)
 static void adjust_git_path(struct strbuf *buf, int git_dir_len)
 {
 	const char *base = buf->buf + git_dir_len;
-	if (git_graft_env && is_dir_file(base, "info", "grafts"))
+	if (is_dir_file(base, "info", "grafts"))
 		strbuf_splice(buf, 0, buf->len,
 			      get_graft_file(), strlen(get_graft_file()));
-	else if (git_index_env && !strcmp(base, "index"))
+	else if (!strcmp(base, "index"))
 		strbuf_splice(buf, 0, buf->len,
 			      get_index_file(), strlen(get_index_file()));
-	else if (git_db_env && dir_prefix(base, "objects"))
+	else if (dir_prefix(base, "objects"))
 		replace_dir(buf, git_dir_len + 7, get_object_directory());
 	else if (git_hooks_path && dir_prefix(base, "hooks"))
 		replace_dir(buf, git_dir_len + 5, git_hooks_path);
-	else if (git_common_dir_env)
+	else if (the_repository.different_commondir)
 		update_common_dir(buf, git_dir_len, NULL);
 }
 
setup.c +15 -2
index b2e05145c..dba3abd00 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "repo.h"
 #include "config.h"
 #include "dir.h"
 #include "string-list.h"
@@ -376,6 +377,11 @@ void setup_work_tree(void)
 	if (getenv(GIT_WORK_TREE_ENVIRONMENT))
 		setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
 
+	/*
+	 * NEEDSWORK: this call can essentially be set_git_dir(get_git_dir())
+	 * which can cause some problems when trying to free the old value of
+	 * gitdir.
+	 */
 	set_git_dir(remove_leading_path(git_dir, work_tree));
 	initialized = 1;
 }
@@ -1074,8 +1080,15 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	 * the user has set GIT_DIR.  It may be beneficial to disallow bogus
 	 * GIT_DIR values at some point in the future.
 	 */
-	if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT))
-		setup_git_env();
+	if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT)) {
+		if (!the_repository.gitdir) {
+			const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
+			if (!gitdir)
+				gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
+			repo_set_gitdir(&the_repository, gitdir);
+			setup_git_env();
+		}
+	}
 
 	strbuf_release(&dir);
 	strbuf_release(&gitdir);
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#22 lore raw

PATCH 8/31 environment: store worktree in the_repository

In reply to Brandon Williams

Migrate 'work_tree' to be stored in 'the_repository'.

---
 environment.c | 9 ++++-----
 repo.c        | 7 +++++++
 repo.h        | 2 ++
 3 files changed, 13 insertions(+), 5 deletions(-)
environment.c +4 -5
index a0519f4f3..2722ebb9f 100644
@@ -96,7 +96,6 @@ int ignore_untracked_cache_config;
 
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
 char *git_work_tree_cfg;
-static char *work_tree;
 
 static const char *super_prefix;
 
@@ -220,19 +219,19 @@ void set_git_work_tree(const char *new_work_tree)
 {
 	if (git_work_tree_initialized) {
 		new_work_tree = real_path(new_work_tree);
-		if (strcmp(new_work_tree, work_tree))
+		if (strcmp(new_work_tree, the_repository.worktree))
 			die("internal error: work tree has already been set\n"
 			    "Current worktree: %s\nNew worktree: %s",
-			    work_tree, new_work_tree);
+			    the_repository.worktree, new_work_tree);
 		return;
 	}
 	git_work_tree_initialized = 1;
-	work_tree = real_pathdup(new_work_tree, 1);
+	repo_set_worktree(&the_repository, new_work_tree);
 }
 
 const char *get_git_work_tree(void)
 {
-	return work_tree;
+	return the_repository.worktree;
 }
 
 char *get_object_directory(void)
repo.c +7 -0
index f7c3617a9..789ffdd78 100644
@@ -84,6 +84,11 @@ void repo_set_gitdir(struct repo *repo, const char *path)
 	repo_setup_env(repo);
 }
 
+void repo_set_worktree(struct repo *repo, const char *path)
+{
+	repo->worktree = real_pathdup(path, 1);
+}
+
 int repo_init(struct repo *repo, const char *gitdir)
 {
 	int error = 0;
@@ -121,4 +126,6 @@ int repo_init(struct repo *repo, const char *gitdir)
 void repo_clear(struct repo *repo)
 {
 	repo_clear_env(repo);
+	free(repo->worktree);
+	repo->worktree = NULL;
 }
repo.h +2 -0
index 453049e67..95d021049 100644
@@ -9,6 +9,7 @@ struct repo {
 	char *index_file;
 	char *graft_file;
 	char *namespace;
+	char *worktree;
 
 	/* Configurations */
 	unsigned ignore_env:1;
@@ -19,6 +20,7 @@ struct repo {
 extern struct repo the_repository;
 
 extern void repo_set_gitdir(struct repo *repo, const char *path);
+extern void repo_set_worktree(struct repo *repo, const char *path);
 extern int repo_init(struct repo *repo, const char *path);
 extern void repo_clear(struct repo *repo);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#23 lore raw

PATCH 9/31 setup: add comment indicating a hack

In reply to Brandon Williams

'GIT_TOPLEVEL_PREFIX_ENVIRONMENT' was added in (b58a68c1c setup: allow for prefix to be passed to git commands) to aid in fixing a bug where 'ls-files' and 'grep' were not able to properly recurse when called from within a subdirectory. Add a 'NEEDSWORK' comment indicating that this envvar should be removed once 'ls-files' and 'grep' can recurse in-process.

---
 setup.c | 6 ++++++
 1 file changed, 6 insertions(+)
setup.c +6 -0
index dba3abd00..c3d21345d 100644
@@ -1057,6 +1057,12 @@ const char *setup_git_directory_gently(int *nongit_ok)
 		die("BUG: unhandled setup_git_directory_1() result");
 	}
 
+	/*
+	 * NEEDSWORK: This was a hack in order to get ls-files and grep to have
+	 * properly formated output when recursing submodules.  Once ls-files
+	 * and grep have been changed to perform this recursing in-process this
+	 * needs to be removed.
+	 */
 	env_prefix = getenv(GIT_TOPLEVEL_PREFIX_ENVIRONMENT);
 	if (env_prefix)
 		prefix = env_prefix;
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#24 lore raw

PATCH 10/31 config: migrate the_configset to the_repository

In reply to Brandon Williams

Migrate the default config to be stored within 'the_repository'.

---
 config.c | 185 ++++++++++++++++++++++++++++++++++++++-------------------------
 config.h |   1 +
 repo.c   |  21 ++++++++
 repo.h   |  11 ++++
 4 files changed, 146 insertions(+), 72 deletions(-)
config.c +113 -72
index ff09b27b8..930333e89 100644
@@ -7,6 +7,7 @@
  */
 #include "cache.h"
 #include "config.h"
+#include "repo.h"
 #include "lockfile.h"
 #include "exec_cmd.h"
 #include "strbuf.h"
@@ -72,13 +73,6 @@ static int core_compression_seen;
 static int pack_compression_seen;
 static int zlib_compression_seen;
 
-/*
- * Default config_set that contains key-value pairs from the usual set of config
- * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
- * config file and the global /etc/gitconfig)
- */
-static struct config_set the_config_set;
-
 static int config_file_fgetc(struct config_source *conf)
 {
 	return getc_unlocked(conf->u.file);
@@ -1606,28 +1600,6 @@ int git_config_with_options(config_fn_t fn, void *data,
 	return do_git_config_sequence(opts, fn, data);
 }
 
-static void git_config_raw(config_fn_t fn, void *data)
-{
-	struct config_options opts = {0};
-
-	opts.respect_includes = 1;
-	if (have_git_dir())
-		opts.git_dir = get_git_common_dir();
-	if (git_config_with_options(fn, data, NULL, &opts) < 0)
-		/*
-		 * git_config_with_options() normally returns only
-		 * zero, as most errors are fatal, and
-		 * non-fatal potential errors are guarded by "if"
-		 * statements that are entered only when no error is
-		 * possible.
-		 *
-		 * If we ever encounter a non-fatal error, it means
-		 * something went really wrong and we should stop
-		 * immediately.
-		 */
-		die(_("unknown error occurred while reading the configuration files"));
-}
-
 static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
 {
 	int i, value_index;
@@ -1676,14 +1648,6 @@ void read_early_config(config_fn_t cb, void *data)
 	strbuf_release(&buf);
 }
 
-static void git_config_check_init(void);
-
-void git_config(config_fn_t fn, void *data)
-{
-	git_config_check_init();
-	configset_iter(&the_config_set, fn, data);
-}
-
 static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
 {
 	struct config_set_element k;
@@ -1782,7 +1746,7 @@ void git_configset_clear(struct config_set *cs)
 	cs->list.items = NULL;
 }
 
-static int config_set_callback(const char *key, const char *value, void *cb)
+int config_set_callback(const char *key, const char *value, void *cb)
 {
 	struct config_set *cs = cb;
 	configset_add_value(cs, key, value);
@@ -1893,87 +1857,164 @@ int git_configset_get_pathname(struct config_set *cs, const char *key, const cha
 		return 1;
 }
 
-static void git_config_check_init(void)
+/* Functions use to read configuration from a repository */
+static void git_config_check_init(struct repo *repository)
 {
-	if (the_config_set.hash_initialized)
+	if (repository->config && repository->config->hash_initialized)
 		return;
-	git_configset_init(&the_config_set);
-	git_config_raw(config_set_callback, &the_config_set);
+	repo_read_config(repository);
 }
 
-void git_config_clear(void)
+static void repo_config_clear(struct repo *repository)
 {
-	if (!the_config_set.hash_initialized)
+	if (!repository->config || !repository->config->hash_initialized)
 		return;
-	git_configset_clear(&the_config_set);
+	git_configset_clear(repository->config);
 }
 
-int git_config_get_value(const char *key, const char **value)
+static void repo_config(struct repo *repository, config_fn_t fn, void *data)
 {
-	git_config_check_init();
-	return git_configset_get_value(&the_config_set, key, value);
+	git_config_check_init(repository);
+	configset_iter(repository->config, fn, data);
 }
 
-const struct string_list *git_config_get_value_multi(const char *key)
+static int repo_config_get_value(struct repo *repository,
+				 const char *key, const char **value)
 {
-	git_config_check_init();
-	return git_configset_get_value_multi(&the_config_set, key);
+	git_config_check_init(repository);
+	return git_configset_get_value(repository->config, key, value);
 }
 
-int git_config_get_string_const(const char *key, const char **dest)
+static const struct string_list *repo_config_get_value_multi(struct repo *repository,
+							     const char *key)
+{
+	git_config_check_init(repository);
+	return git_configset_get_value_multi(repository->config, key);
+}
+
+static int repo_config_get_string_const(struct repo *repository,
+					const char *key, const char **dest)
 {
 	int ret;
-	git_config_check_init();
-	ret = git_configset_get_string_const(&the_config_set, key, dest);
+	git_config_check_init(repository);
+	ret = git_configset_get_string_const(repository->config, key, dest);
 	if (ret < 0)
 		git_die_config(key, NULL);
 	return ret;
 }
 
+static int repo_config_get_string(struct repo *repository,
+				  const char *key, char **dest)
+{
+	git_config_check_init(repository);
+	return repo_config_get_string_const(repository, key, (const char **)dest);
+}
+
+static int repo_config_get_int(struct repo *repository,
+			       const char *key, int *dest)
+{
+	git_config_check_init(repository);
+	return git_configset_get_int(repository->config, key, dest);
+}
+
+static int repo_config_get_ulong(struct repo *repository,
+				const char *key, unsigned long *dest)
+{
+	git_config_check_init(repository);
+	return git_configset_get_ulong(repository->config, key, dest);
+}
+
+static int repo_config_get_bool(struct repo *repository,
+				const char *key, int *dest)
+{
+	git_config_check_init(repository);
+	return git_configset_get_bool(repository->config, key, dest);
+}
+
+static int repo_config_get_bool_or_int(struct repo *repository,
+				       const char *key, int *is_bool, int *dest)
+{
+	git_config_check_init(repository);
+	return git_configset_get_bool_or_int(repository->config, key, is_bool, dest);
+}
+
+static int repo_config_get_maybe_bool(struct repo *repository,
+				      const char *key, int *dest)
+{
+	git_config_check_init(repository);
+	return git_configset_get_maybe_bool(repository->config, key, dest);
+}
+
+static int repo_config_get_pathname(struct repo *repository,
+				    const char *key, const char **dest)
+{
+	int ret;
+	git_config_check_init(repository);
+	ret = git_configset_get_pathname(repository->config, key, dest);
+	if (ret < 0)
+		git_die_config(key, NULL);
+	return ret;
+}
+
+/* Functions used historically to read configuration from 'the_repository' */
+void git_config(config_fn_t fn, void *data)
+{
+	repo_config(&the_repository, fn, data);
+}
+
+void git_config_clear(void)
+{
+	repo_config_clear(&the_repository);
+}
+
+int git_config_get_value(const char *key, const char **value)
+{
+	return repo_config_get_value(&the_repository, key, value);
+}
+
+const struct string_list *git_config_get_value_multi(const char *key)
+{
+	return repo_config_get_value_multi(&the_repository, key);
+}
+
+int git_config_get_string_const(const char *key, const char **dest)
+{
+	return repo_config_get_string_const(&the_repository, key, dest);
+}
+
 int git_config_get_string(const char *key, char **dest)
 {
-	git_config_check_init();
-	return git_config_get_string_const(key, (const char **)dest);
+	return repo_config_get_string(&the_repository, key, dest);
 }
 
 int git_config_get_int(const char *key, int *dest)
 {
-	git_config_check_init();
-	return git_configset_get_int(&the_config_set, key, dest);
+	return repo_config_get_int(&the_repository, key, dest);
 }
 
 int git_config_get_ulong(const char *key, unsigned long *dest)
 {
-	git_config_check_init();
-	return git_configset_get_ulong(&the_config_set, key, dest);
+	return repo_config_get_ulong(&the_repository, key, dest);
 }
 
 int git_config_get_bool(const char *key, int *dest)
 {
-	git_config_check_init();
-	return git_configset_get_bool(&the_config_set, key, dest);
+	return repo_config_get_bool(&the_repository, key, dest);
 }
 
 int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
 {
-	git_config_check_init();
-	return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
+	return repo_config_get_bool_or_int(&the_repository, key, is_bool, dest);
 }
 
 int git_config_get_maybe_bool(const char *key, int *dest)
 {
-	git_config_check_init();
-	return git_configset_get_maybe_bool(&the_config_set, key, dest);
+	return repo_config_get_maybe_bool(&the_repository, key, dest);
 }
 
 int git_config_get_pathname(const char *key, const char **dest)
 {
-	int ret;
-	git_config_check_init();
-	ret = git_configset_get_pathname(&the_config_set, key, dest);
-	if (ret < 0)
-		git_die_config(key, NULL);
-	return ret;
+	return repo_config_get_pathname(&the_repository, key, dest);
 }
 
 int git_config_get_expiry(const char *key, const char **output)
config.h +1 -0
index f7f8b66c5..4cd9e2915 100644
@@ -149,6 +149,7 @@ struct config_set {
 };
 
 extern void git_configset_init(struct config_set *cs);
+extern int config_set_callback(const char *key, const char *value, void *cb);
 extern int git_configset_add_file(struct config_set *cs, const char *filename);
 extern int git_configset_get_value(struct config_set *cs, const char *key, const char **value);
 extern const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
repo.c +21 -0
index 789ffdd78..c67cad5a2 100644
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "repo.h"
+#include "config.h"
 
 /*
  * This may be the wrong place for this.
@@ -89,6 +90,20 @@ void repo_set_worktree(struct repo *repo, const char *path)
 	repo->worktree = real_pathdup(path, 1);
 }
 
+void repo_read_config(struct repo *repo)
+{
+	struct config_options opts = { 1, repo->commondir };
+
+	if (!repo->config)
+		repo->config = xcalloc(1, sizeof(struct config_set));
+	else
+		git_configset_clear(repo->config);
+
+	git_configset_init(repo->config);
+
+	git_config_with_options(config_set_callback, repo->config, NULL, &opts);
+}
+
 int repo_init(struct repo *repo, const char *gitdir)
 {
 	int error = 0;
@@ -128,4 +143,10 @@ void repo_clear(struct repo *repo)
 	repo_clear_env(repo);
 	free(repo->worktree);
 	repo->worktree = NULL;
+
+	if (repo->config) {
+		git_configset_clear(repo->config);
+		free(repo->config);
+		repo->config = NULL;
+	}
 }
repo.h +11 -0
index 95d021049..284452832 100644
@@ -1,6 +1,8 @@
 #ifndef REPO_H
 #define REPO_H
 
+struct config_set;
+
 struct repo {
 	/* Environment */
 	char *gitdir;
@@ -11,6 +13,14 @@ struct repo {
 	char *namespace;
 	char *worktree;
 
+	/* Subsystems */
+	/*
+	 * Repository's config which contains key-value pairs from the usual
+	 * set of config config files (i.e repo specific .git/config, user wide
+	 * ~/.gitconfig, XDG config file and the global /etc/gitconfig)
+	 */
+	struct config_set *config;
+
 	/* Configurations */
 	unsigned ignore_env:1;
 	/* Indicates if a repository has a different 'commondir' from 'gitdir' */
@@ -21,6 +31,7 @@ extern struct repo the_repository;
 
 extern void repo_set_gitdir(struct repo *repo, const char *path);
 extern void repo_set_worktree(struct repo *repo, const char *path);
+extern void repo_read_config(struct repo *repo);
 extern int repo_init(struct repo *repo, const char *path);
 extern void repo_clear(struct repo *repo);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#25 lore raw

PATCH 11/31 repo: add index_state to struct repo

In reply to Brandon Williams
---
 repo.c | 17 +++++++++++++++++
 repo.h |  3 +++
 2 files changed, 20 insertions(+)
repo.c +17 -0
index c67cad5a2..c79d29534 100644
@@ -104,6 +104,17 @@ void repo_read_config(struct repo *repo)
 	git_config_with_options(config_set_callback, repo->config, NULL, &opts);
 }
 
+void repo_read_index(struct repo *repo)
+{
+	if (!repo->index)
+		repo->index = xcalloc(1, sizeof(struct index_state));
+	else
+		discard_index(repo->index);
+
+	if (read_index_from(repo->index, repo->index_file) < 0)
+		die(_("failure reading index"));
+}
+
 int repo_init(struct repo *repo, const char *gitdir)
 {
 	int error = 0;
@@ -149,4 +160,10 @@ void repo_clear(struct repo *repo)
 		free(repo->config);
 		repo->config = NULL;
 	}
+
+	if (repo->index) {
+		discard_index(repo->index);
+		free(repo->index);
+		repo->index = NULL;
+	}
 }
repo.h +3 -0
index 284452832..756cda9e1 100644
@@ -2,6 +2,7 @@
 #define REPO_H
 
 struct config_set;
+struct index_state;
 
 struct repo {
 	/* Environment */
@@ -20,6 +21,7 @@ struct repo {
 	 * ~/.gitconfig, XDG config file and the global /etc/gitconfig)
 	 */
 	struct config_set *config;
+	struct index_state *index;
 
 	/* Configurations */
 	unsigned ignore_env:1;
@@ -32,6 +34,7 @@ extern struct repo the_repository;
 extern void repo_set_gitdir(struct repo *repo, const char *path);
 extern void repo_set_worktree(struct repo *repo, const char *path);
 extern void repo_read_config(struct repo *repo);
+extern void repo_read_index(struct repo *repo);
 extern int repo_init(struct repo *repo, const char *path);
 extern void repo_clear(struct repo *repo);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#26 lore raw

PATCH 12/31 submodule-config: store the_submodule_cache in the_repository

In reply to Brandon Williams

Refactor how 'the_submodule_cache' is handled so that it can be stored inside of a repository object. Also migrate 'the_submodule_cache' to be stored in 'the_repository'.

---
 repo.c             |  6 +++++
 repo.h             |  2 ++
 submodule-config.c | 71 ++++++++++++++++++++++++++++++++++++++++--------------
 submodule-config.h | 10 ++++++++
 4 files changed, 71 insertions(+), 18 deletions(-)
repo.c +6 -0
index c79d29534..13b7d244f 100644
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "repo.h"
 #include "config.h"
+#include "submodule-config.h"
 
 /*
  * This may be the wrong place for this.
@@ -166,4 +167,9 @@ void repo_clear(struct repo *repo)
 		free(repo->index);
 		repo->index = NULL;
 	}
+
+	if (repo->submodule_cache) {
+		submodule_cache_free(repo->submodule_cache);
+		repo->submodule_cache = NULL;
+	}
 }
repo.h +2 -0
index 756cda9e1..ebce2a408 100644
@@ -3,6 +3,7 @@
 
 struct config_set;
 struct index_state;
+struct submodule_cache;
 
 struct repo {
 	/* Environment */
@@ -22,6 +23,7 @@ struct repo {
 	 */
 	struct config_set *config;
 	struct index_state *index;
+	struct submodule_cache *submodule_cache;
 
 	/* Configurations */
 	unsigned ignore_env:1;
submodule-config.c +53 -18

142 lines not on this page · raw message

submodule-config.h +10 -0
index d434ecdb4..2bfe78d89 100644
@@ -22,14 +22,24 @@ struct submodule {
 	int recommend_shallow;
 };
 
+struct submodule_cache;
+struct repo;
+
+extern void submodule_cache_free(struct submodule_cache *cache);
+
 extern int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);
 extern int parse_update_recurse_submodules_arg(const char *opt, const char *arg);
 extern int parse_push_recurse_submodules_arg(const char *opt, const char *arg);
 extern int parse_submodule_config_option(const char *var, const char *value);
+extern int submodule_config_option(struct repo *repository,
+				   const char *var, const char *value);
 extern const struct submodule *submodule_from_name(
 		const unsigned char *commit_or_tree, const char *name);
 extern const struct submodule *submodule_from_path(
 		const unsigned char *commit_or_tree, const char *path);
+extern const struct submodule *submodule_from_cache(struct repo *repository,
+						    const unsigned char *treeish_name,
+						    const char *key);
 extern int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
 				      unsigned char *gitmodules_sha1,
 				      struct strbuf *rev);
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#27 lore raw

PATCH 13/31 repo: add repo_read_gitmodules

In reply to Brandon Williams

Teach the repo object to be able to populate the submodule_cache by reading the repository's gitmodules file.

---
 repo.c | 14 ++++++++++++++
 repo.h |  1 +
 2 files changed, 15 insertions(+)
repo.c +14 -0
index 13b7d244f..6f7b2015f 100644
@@ -116,6 +116,20 @@ void repo_read_index(struct repo *repo)
 		die(_("failure reading index"));
 }
 
+static int gitmodules_cb(const char *var, const char *value, void *data)
+{
+	struct repo *repo = data;
+	return submodule_config_option(repo, var, value);
+}
+
+void repo_read_gitmodules(struct repo *repo)
+{
+	char *gitmodules_path = xstrfmt("%s/.gitmodules", repo->worktree);
+
+	git_config_from_file(gitmodules_cb, gitmodules_path, repo);
+	free(gitmodules_path);
+}
+
 int repo_init(struct repo *repo, const char *gitdir)
 {
 	int error = 0;
repo.h +1 -0
index ebce2a408..ad0184eaa 100644
@@ -37,6 +37,7 @@ extern void repo_set_gitdir(struct repo *repo, const char *path);
 extern void repo_set_worktree(struct repo *repo, const char *path);
 extern void repo_read_config(struct repo *repo);
 extern void repo_read_index(struct repo *repo);
+extern void repo_read_gitmodules(struct repo *repo);
 extern int repo_init(struct repo *repo, const char *path);
 extern void repo_clear(struct repo *repo);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#28 lore raw

PATCH 14/31 submodule: convert is_submodule_initialized to work on a repository

In reply to Brandon Williams

Convert 'is_submodule_initialized()' to take a repository object and while we're at it, lets rename the function to 'is_submodule_active()' and remove the NEEDSWORK comment.

---
 builtin/grep.c              |  3 ++-
 builtin/submodule--helper.c |  9 +++++----
 config.c                    | 12 ++++++------
 config.h                    |  9 +++++++++
 submodule.c                 | 21 +++++++++------------
 submodule.h                 |  3 ++-
 6 files changed, 33 insertions(+), 24 deletions(-)
builtin/grep.c +2 -1
index 0f4a1e5a3..c3086bc0b 100644
@@ -4,6 +4,7 @@
  * Copyright (c) 2006 Junio C Hamano
  */
 #include "cache.h"
+#include "repo.h"
 #include "config.h"
 #include "blob.h"
 #include "tree.h"
@@ -626,7 +627,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
 static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
 			  const char *filename, const char *path)
 {
-	if (!is_submodule_initialized(path))
+	if (!is_submodule_active(&the_repository, path))
 		return 0;
 	if (!is_submodule_populated_gently(path, NULL)) {
 		/*
builtin/submodule--helper.c +5 -4
index 4dcbfb952..d2d6fa914 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "repo.h"
 #include "cache.h"
 #include "config.h"
 #include "parse-options.h"
@@ -280,7 +281,7 @@ static void module_list_active(struct module_list *list)
 	for (i = 0; i < list->nr; i++) {
 		const struct cache_entry *ce = list->entries[i];
 
-		if (!is_submodule_initialized(ce->name))
+		if (!is_submodule_active(&the_repository, ce->name))
 			continue;
 
 		ALLOC_GROW(active_modules.entries,
@@ -362,7 +363,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
 	 *
 	 * Set active flag for the submodule being initialized
 	 */
-	if (!is_submodule_initialized(path)) {
+	if (!is_submodule_active(&the_repository, path)) {
 		strbuf_reset(&sb);
 		strbuf_addf(&sb, "submodule.%s.active", sub->name);
 		git_config_set_gently(sb.buf, "true");
@@ -817,7 +818,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
 	}
 
 	/* Check if the submodule has been initialized. */
-	if (!is_submodule_initialized(ce->name)) {
+	if (!is_submodule_active(&the_repository, ce->name)) {
 		next_submodule_warn_missing(suc, out, displaypath);
 		goto cleanup;
 	}
@@ -1193,7 +1194,7 @@ static int is_active(int argc, const char **argv, const char *prefix)
 
 	gitmodules_config();
 
-	return !is_submodule_initialized(argv[1]);
+	return !is_submodule_active(&the_repository, argv[1]);
 }
 
 #define SUPPORT_SUPER_PREFIX (1<<0)
config.c +6 -6
index 930333e89..977bba233 100644
@@ -1885,8 +1885,8 @@ static int repo_config_get_value(struct repo *repository,
 	return git_configset_get_value(repository->config, key, value);
 }
 
-static const struct string_list *repo_config_get_value_multi(struct repo *repository,
-							     const char *key)
+const struct string_list *repo_config_get_value_multi(struct repo *repository,
+						      const char *key)
 {
 	git_config_check_init(repository);
 	return git_configset_get_value_multi(repository->config, key);
@@ -1903,8 +1903,8 @@ static int repo_config_get_string_const(struct repo *repository,
 	return ret;
 }
 
-static int repo_config_get_string(struct repo *repository,
-				  const char *key, char **dest)
+int repo_config_get_string(struct repo *repository,
+			   const char *key, char **dest)
 {
 	git_config_check_init(repository);
 	return repo_config_get_string_const(repository, key, (const char **)dest);
@@ -1924,8 +1924,8 @@ static int repo_config_get_ulong(struct repo *repository,
 	return git_configset_get_ulong(repository->config, key, dest);
 }
 
-static int repo_config_get_bool(struct repo *repository,
-				const char *key, int *dest)
+int repo_config_get_bool(struct repo *repository,
+			 const char *key, int *dest)
 {
 	git_config_check_init(repository);
 	return git_configset_get_bool(repository->config, key, dest);
config.h +9 -0
index 4cd9e2915..c4406b728 100644
@@ -163,6 +163,15 @@ extern int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
 extern int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
 extern int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
 
+struct repo;
+
+extern const struct string_list *repo_config_get_value_multi(struct repo *repository,
+							     const char *key);
+extern int repo_config_get_string(struct repo *repository,
+				  const char *key, char **dest);
+extern int repo_config_get_bool(struct repo *repository,
+				const char *key, int *dest);
+
 extern int git_config_get_value(const char *key, const char **value);
 extern const struct string_list *git_config_get_value_multi(const char *key);
 extern void git_config_clear(void);
submodule.c +9 -12
index 95328de61..84600bde7 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "repo.h"
 #include "config.h"
 #include "submodule-config.h"
 #include "submodule.h"
@@ -215,21 +216,17 @@ void gitmodules_config_sha1(const unsigned char *commit_sha1)
 }
 
 /*
- * NEEDSWORK: With the addition of different configuration options to determine
- * if a submodule is of interests, the validity of this function's name comes
- * into question.  Once the dust has settled and more concrete terminology is
- * decided upon, come up with a more proper name for this function.  One
- * potential candidate could be 'is_submodule_active()'.
- *
  * Determine if a submodule has been initialized at a given 'path'
  */
-int is_submodule_initialized(const char *path)
+int is_submodule_active(struct repo *repo, const char *path)
 {
 	int ret = 0;
 	char *key = NULL;
 	char *value = NULL;
 	const struct string_list *sl;
-	const struct submodule *module = submodule_from_path(null_sha1, path);
+	const struct submodule *module;
+	
+	module = submodule_from_cache(repo, null_sha1, path);
 
 	/* early return if there isn't a path->module mapping */
 	if (!module)
@@ -237,14 +234,14 @@ int is_submodule_initialized(const char *path)
 
 	/* submodule.<name>.active is set */
 	key = xstrfmt("submodule.%s.active", module->name);
-	if (!git_config_get_bool(key, &ret)) {
+	if (!repo_config_get_bool(repo, key, &ret)) {
 		free(key);
 		return ret;
 	}
 	free(key);
 
 	/* submodule.active is set */
-	sl = git_config_get_value_multi("submodule.active");
+	sl = repo_config_get_value_multi(repo, "submodule.active");
 	if (sl) {
 		struct pathspec ps;
 		struct argv_array args = ARGV_ARRAY_INIT;
@@ -264,7 +261,7 @@ int is_submodule_initialized(const char *path)
 
 	/* fallback to checking if the URL is set */
 	key = xstrfmt("submodule.%s.url", module->name);
-	ret = !git_config_get_string(key, &value);
+	ret = !repo_config_get_string(repo, key, &value);
 
 	free(value);
 	free(key);
@@ -1469,7 +1466,7 @@ int submodule_move_head(const char *path,
 	const struct submodule *sub;
 	int *error_code_ptr, error_code;
 
-	if (!is_submodule_initialized(path))
+	if (!is_submodule_active(&the_repository, path))
 		return 0;
 
 	if (flags & SUBMODULE_MOVE_HEAD_FORCE)
submodule.h +2 -1
index 8fb0f2549..5c30eea55 100644
@@ -1,6 +1,7 @@
 #ifndef SUBMODULE_H
 #define SUBMODULE_H
 
+struct repo;
 struct diff_options;
 struct argv_array;
 struct oid_array;
@@ -41,7 +42,7 @@ extern void set_diffopt_flags_from_submodule_config(struct diff_options *,
 extern int submodule_config(const char *var, const char *value, void *cb);
 extern void gitmodules_config(void);
 extern void gitmodules_config_sha1(const unsigned char *commit_sha1);
-extern int is_submodule_initialized(const char *path);
+extern int is_submodule_active(struct repo *repo, const char *path);
 /*
  * Determine if a submodule has been populated at a given 'path' by checking if
  * the <path>/.git resolves to a valid git repository.
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#29 lore raw

PATCH 15/31 convert: convert get_cached_convert_stats_ascii to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 3 ++-
 convert.c          | 5 +++--
 convert.h          | 5 ++++-
 3 files changed, 9 insertions(+), 4 deletions(-)
builtin/ls-files.c +2 -1
index c4357dc30..f16ce0053 100644
@@ -64,7 +64,8 @@ static void write_eolinfo(const struct cache_entry *ce, const char *path)
 		const char *w_txt = "";
 		const char *a_txt = get_convert_attr_ascii(path);
 		if (ce && S_ISREG(ce->ce_mode))
-			i_txt = get_cached_convert_stats_ascii(ce->name);
+			i_txt = get_cached_convert_stats_ascii(&the_index,
+							       ce->name);
 		if (!lstat(path, &st) && S_ISREG(st.st_mode))
 			w_txt = get_wt_convert_stats_ascii(path);
 		printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
convert.c +3 -2
index 5f4a4b1f5..574003023 100644
@@ -135,11 +135,12 @@ static const char *gather_convert_stats_ascii(const char *data, unsigned long si
 	}
 }
 
-const char *get_cached_convert_stats_ascii(const char *path)
+const char *get_cached_convert_stats_ascii(const struct index_state *istate,
+					   const char *path)
 {
 	const char *ret;
 	unsigned long sz;
-	void *data = read_blob_data_from_cache(path, &sz);
+	void *data = read_blob_data_from_index(istate, path, &sz);
 	ret = gather_convert_stats_ascii(data, sz);
 	free(data);
 	return ret;
convert.h +4 -1
index 82871a11d..667b7dfe0 100644
@@ -4,6 +4,8 @@
 #ifndef CONVERT_H
 #define CONVERT_H
 
+struct index_state;
+
 enum safe_crlf {
 	SAFE_CRLF_FALSE = 0,
 	SAFE_CRLF_FAIL = 1,
@@ -33,7 +35,8 @@ enum eol {
 };
 
 extern enum eol core_eol;
-extern const char *get_cached_convert_stats_ascii(const char *path);
+extern const char *get_cached_convert_stats_ascii(const struct index_state *istate,
+						  const char *path);
 extern const char *get_wt_convert_stats_ascii(const char *path);
 extern const char *get_convert_attr_ascii(const char *path);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#30 lore raw

PATCH 16/31 convert: convert crlf_to_git to take an index

In reply to Brandon Williams
---
 convert.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
convert.c +8 -6
index 574003023..ff3e72657 100644
@@ -219,13 +219,13 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
 	}
 }
 
-static int has_cr_in_index(const char *path)
+static int has_cr_in_index(const struct index_state *istate, const char *path)
 {
 	unsigned long sz;
 	void *data;
 	int has_cr;
 
-	data = read_blob_data_from_cache(path, &sz);
+	data = read_blob_data_from_index(istate, path, &sz);
 	if (!data)
 		return 0;
 	has_cr = memchr(data, '\r', sz) != NULL;
@@ -255,7 +255,8 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
 
 }
 
-static int crlf_to_git(const char *path, const char *src, size_t len,
+static int crlf_to_git(const struct index_state *istate,
+		       const char *path, const char *src, size_t len,
 		       struct strbuf *buf,
 		       enum crlf_action crlf_action, enum safe_crlf checksafe)
 {
@@ -287,7 +288,8 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
 		 * unless we want to renormalize in a merge or
 		 * cherry-pick.
 		 */
-		if ((checksafe != SAFE_CRLF_RENORMALIZE) && has_cr_in_index(path))
+		if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
+		    has_cr_in_index(istate, path))
 			convert_crlf_into_lf = 0;
 	}
 	if ((checksafe == SAFE_CRLF_WARN ||
@@ -1099,7 +1101,7 @@ int convert_to_git(const char *path, const char *src, size_t len,
 		src = dst->buf;
 		len = dst->len;
 	}
-	ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
+	ret |= crlf_to_git(&the_index, path, src, len, dst, ca.crlf_action, checksafe);
 	if (ret && dst) {
 		src = dst->buf;
 		len = dst->len;
@@ -1119,7 +1121,7 @@ void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
 	if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
 		die("%s: clean filter '%s' failed", path, ca.drv->name);
 
-	crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
+	crlf_to_git(&the_index, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
 	ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
 }
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#31 lore raw

PATCH 17/31 convert: convert convert_to_git_filter_fd to take an index

In reply to Brandon Williams
---
 convert.c   | 5 +++--
 convert.h   | 3 ++-
 sha1_file.c | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)
convert.c +3 -2
index ff3e72657..824b606fa 100644
@@ -1109,7 +1109,8 @@ int convert_to_git(const char *path, const char *src, size_t len,
 	return ret | ident_to_git(path, src, len, dst, ca.ident);
 }
 
-void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
+void convert_to_git_filter_fd(const struct index_state *istate,
+			      const char *path, int fd, struct strbuf *dst,
 			      enum safe_crlf checksafe)
 {
 	struct conv_attrs ca;
@@ -1121,7 +1122,7 @@ void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
 	if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
 		die("%s: clean filter '%s' failed", path, ca.drv->name);
 
-	crlf_to_git(&the_index, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
+	crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
 	ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
 }
 
convert.h +2 -1
index 667b7dfe0..3a813a797 100644
@@ -52,7 +52,8 @@ static inline int would_convert_to_git(const char *path)
 	return convert_to_git(path, NULL, 0, NULL, 0);
 }
 /* Precondition: would_convert_to_git_filter_fd(path) == true */
-extern void convert_to_git_filter_fd(const char *path, int fd,
+extern void convert_to_git_filter_fd(const struct index_state *istate,
+				     const char *path, int fd,
 				     struct strbuf *dst,
 				     enum safe_crlf checksafe);
 extern int would_convert_to_git_filter_fd(const char *path);
sha1_file.c +1 -1
index 44561e0b9..80e9ef3bb 100644
@@ -3581,7 +3581,7 @@ static int index_stream_convert_blob(unsigned char *sha1, int fd,
 	assert(path);
 	assert(would_convert_to_git_filter_fd(path));
 
-	convert_to_git_filter_fd(path, fd, &sbuf,
+	convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
 				 write_object ? safe_crlf : SAFE_CRLF_FALSE);
 
 	if (write_object)
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#32 lore raw

PATCH 18/31 convert: convert convert_to_git to take an index

In reply to Brandon Williams
---
 apply.c         | 2 +-
 builtin/blame.c | 2 +-
 combine-diff.c  | 2 +-
 convert.c       | 7 ++++---
 convert.h       | 8 +++++---
 diff.c          | 6 +++---
 dir.c           | 2 +-
 sha1_file.c     | 4 ++--
 8 files changed, 18 insertions(+), 15 deletions(-)
apply.c +1 -1
index 87db9618d..8eca54325 100644
@@ -2268,7 +2268,7 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
 	case S_IFREG:
 		if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
 			return error(_("unable to open or read %s"), path);
-		convert_to_git(path, buf->buf, buf->len, buf, 0);
+		convert_to_git(&the_index, path, buf->buf, buf->len, buf, 0);
 		return 0;
 	default:
 		return -1;
builtin/blame.c +1 -1
index c0ae49298..317d2ec37 100644
@@ -2384,7 +2384,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
 		if (strbuf_read(&buf, 0, 0) < 0)
 			die_errno("failed to read from stdin");
 	}
-	convert_to_git(path, buf.buf, buf.len, &buf, 0);
+	convert_to_git(&the_index, path, buf.buf, buf.len, &buf, 0);
 	origin->file.ptr = buf.buf;
 	origin->file.size = buf.len;
 	pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash);
combine-diff.c +1 -1
index 2848034fe..74f723af3 100644
@@ -1053,7 +1053,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 			if (is_file) {
 				struct strbuf buf = STRBUF_INIT;
 
-				if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
+				if (convert_to_git(&the_index, elem->path, result, len, &buf, safe_crlf)) {
 					free(result);
 					result = strbuf_detach(&buf, &len);
 					result_size = len;
convert.c +4 -3
index 824b606fa..5af6fdf3f 100644
@@ -1085,7 +1085,8 @@ const char *get_convert_attr_ascii(const char *path)
 	return "";
 }
 
-int convert_to_git(const char *path, const char *src, size_t len,
+int convert_to_git(const struct index_state *istate,
+		   const char *path, const char *src, size_t len,
                    struct strbuf *dst, enum safe_crlf checksafe)
 {
 	int ret = 0;
@@ -1101,7 +1102,7 @@ int convert_to_git(const char *path, const char *src, size_t len,
 		src = dst->buf;
 		len = dst->len;
 	}
-	ret |= crlf_to_git(&the_index, path, src, len, dst, ca.crlf_action, checksafe);
+	ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
 	if (ret && dst) {
 		src = dst->buf;
 		len = dst->len;
@@ -1172,7 +1173,7 @@ int renormalize_buffer(const char *path, const char *src, size_t len, struct str
 		src = dst->buf;
 		len = dst->len;
 	}
-	return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE);
+	return ret | convert_to_git(&the_index, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
 }
 
 /*****************************************************************
convert.h +5 -3
index 3a813a797..60cb41d6a 100644
@@ -41,15 +41,17 @@ extern const char *get_wt_convert_stats_ascii(const char *path);
 extern const char *get_convert_attr_ascii(const char *path);
 
 /* returns 1 if *dst was used */
-extern int convert_to_git(const char *path, const char *src, size_t len,
+extern int convert_to_git(const struct index_state *istate,
+			  const char *path, const char *src, size_t len,
 			  struct strbuf *dst, enum safe_crlf checksafe);
 extern int convert_to_working_tree(const char *path, const char *src,
 				   size_t len, struct strbuf *dst);
 extern int renormalize_buffer(const char *path, const char *src, size_t len,
 			      struct strbuf *dst);
-static inline int would_convert_to_git(const char *path)
+static inline int would_convert_to_git(const struct index_state *istate,
+				       const char *path)
 {
-	return convert_to_git(path, NULL, 0, NULL, 0);
+	return convert_to_git(istate, path, NULL, 0, NULL, 0);
 }
 /* Precondition: would_convert_to_git_filter_fd(path) == true */
 extern void convert_to_git_filter_fd(const struct index_state *istate,
diff.c +3 -3
index 2f2467c6d..87ed6d6d3 100644
@@ -2756,7 +2756,7 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
 	 * Similarly, if we'd have to convert the file contents anyway, that
 	 * makes the optimization not worthwhile.
 	 */
-	if (!want_file && would_convert_to_git(name))
+	if (!want_file && would_convert_to_git(&the_index, name))
 		return 0;
 
 	len = strlen(name);
@@ -2878,7 +2878,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 		 * point if the path requires us to run the content
 		 * conversion.
 		 */
-		if (size_only && !would_convert_to_git(s->path))
+		if (size_only && !would_convert_to_git(&the_index, s->path))
 			return 0;
 
 		/*
@@ -2905,7 +2905,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
 		/*
 		 * Convert from working tree format to canonical git format
 		 */
-		if (convert_to_git(s->path, s->data, s->size, &buf, crlf_warn)) {
+		if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, crlf_warn)) {
 			size_t size = 0;
 			munmap(s->data, s->size);
 			s->should_munmap = 0;
dir.c +1 -1
index 0c26a53d2..8dc74c5d2 100644
@@ -796,7 +796,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,
 				 (pos = index_name_pos(istate, fname, strlen(fname))) >= 0 &&
 				 !ce_stage(istate->cache[pos]) &&
 				 ce_uptodate(istate->cache[pos]) &&
-				 !would_convert_to_git(fname))
+				 !would_convert_to_git(istate, fname))
 				hashcpy(sha1_stat->sha1,
 					istate->cache[pos]->oid.hash);
 			else
sha1_file.c +2 -2
index 80e9ef3bb..a900b2804 100644
@@ -3547,7 +3547,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
 	 */
 	if ((type == OBJ_BLOB) && path) {
 		struct strbuf nbuf = STRBUF_INIT;
-		if (convert_to_git(path, buf, size, &nbuf,
+		if (convert_to_git(&the_index, path, buf, size, &nbuf,
 				   write_object ? safe_crlf : SAFE_CRLF_FALSE)) {
 			buf = strbuf_detach(&nbuf, &size);
 			re_allocated = 1;
@@ -3669,7 +3669,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st,
 	else if (!S_ISREG(st->st_mode))
 		ret = index_pipe(sha1, fd, type, path, flags);
 	else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
-		 (path && would_convert_to_git(path)))
+		 (path && would_convert_to_git(&the_index, path)))
 		ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
 				 flags);
 	else
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#33 lore raw

PATCH 19/31 convert: convert renormalize_buffer to take an index

In reply to Brandon Williams
---
 convert.c         | 6 ++++--
 convert.h         | 3 ++-
 ll-merge.c        | 2 +-
 merge-recursive.c | 4 ++--
 4 files changed, 9 insertions(+), 6 deletions(-)
convert.c +4 -2
index 5af6fdf3f..7d2a519da 100644
@@ -1,3 +1,4 @@
+#define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "attr.h"
@@ -1166,14 +1167,15 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc
 	return convert_to_working_tree_internal(path, src, len, dst, 0);
 }
 
-int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
+int renormalize_buffer(const struct index_state *istate, const char *path,
+		       const char *src, size_t len, struct strbuf *dst)
 {
 	int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
 	if (ret) {
 		src = dst->buf;
 		len = dst->len;
 	}
-	return ret | convert_to_git(&the_index, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
+	return ret | convert_to_git(istate, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
 }
 
 /*****************************************************************
convert.h +2 -1
index 60cb41d6a..cecf59d1a 100644
@@ -46,7 +46,8 @@ extern int convert_to_git(const struct index_state *istate,
 			  struct strbuf *dst, enum safe_crlf checksafe);
 extern int convert_to_working_tree(const char *path, const char *src,
 				   size_t len, struct strbuf *dst);
-extern int renormalize_buffer(const char *path, const char *src, size_t len,
+extern int renormalize_buffer(const struct index_state *istate,
+			      const char *path, const char *src, size_t len,
 			      struct strbuf *dst);
 static inline int would_convert_to_git(const struct index_state *istate,
 				       const char *path)
ll-merge.c +1 -1
index 24ff94e1d..b9576efe9 100644
@@ -340,7 +340,7 @@ static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr
 static void normalize_file(mmfile_t *mm, const char *path)
 {
 	struct strbuf strbuf = STRBUF_INIT;
-	if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
+	if (renormalize_buffer(&the_index, path, mm->ptr, mm->size, &strbuf)) {
 		free(mm->ptr);
 		mm->size = strbuf.len;
 		mm->ptr = strbuf_detach(&strbuf, NULL);
merge-recursive.c +2 -2
index c2494f34f..a4b3858d0 100644
@@ -1640,8 +1640,8 @@ static int blob_unchanged(struct merge_options *opt,
 	 * performed.  Comparison can be skipped if both files are
 	 * unchanged since their sha1s have already been compared.
 	 */
-	if (renormalize_buffer(path, o.buf, o.len, &o) |
-	    renormalize_buffer(path, a.buf, a.len, &a))
+	if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
+	    renormalize_buffer(&the_index, path, a.buf, a.len, &a))
 		ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
 
 error_return:
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#34 lore raw

PATCH 20/31 tree: convert read_tree to take an index parameter

In reply to Brandon Williams
---
 builtin/ls-files.c |  2 +-
 tree.c             | 28 ++++++++++++++++++----------
 tree.h             |  3 ++-
 3 files changed, 21 insertions(+), 12 deletions(-)
builtin/ls-files.c +1 -1
index f16ce0053..620487a77 100644
@@ -461,7 +461,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
 			       PATHSPEC_PREFER_CWD, prefix, matchbuf);
 	} else
 		memset(&pathspec, 0, sizeof(pathspec));
-	if (read_tree(tree, 1, &pathspec))
+	if (read_tree(tree, 1, &pathspec, &the_index))
 		die("unable to read tree entries %s", tree_name);
 
 	for (i = 0; i < active_nr; i++) {
tree.c +18 -10
index 603b29ee8..dd69423d9 100644
@@ -1,3 +1,4 @@
+#define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "cache-tree.h"
 #include "tree.h"
@@ -8,7 +9,11 @@
 
 const char *tree_type = "tree";
 
-static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
+static int read_one_entry_opt(struct index_state *istate,
+			      const unsigned char *sha1,
+			      const char *base, int baselen,
+			      const char *pathname,
+			      unsigned mode, int stage, int opt)
 {
 	int len;
 	unsigned int size;
@@ -27,14 +32,15 @@ static int read_one_entry_opt(const unsigned char *sha1, const char *base, int b
 	memcpy(ce->name, base, baselen);
 	memcpy(ce->name + baselen, pathname, len+1);
 	hashcpy(ce->oid.hash, sha1);
-	return add_cache_entry(ce, opt);
+	return add_index_entry(istate, ce, opt);
 }
 
 static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
 			  const char *pathname, unsigned mode, int stage,
 			  void *context)
 {
-	return read_one_entry_opt(sha1, base->buf, base->len, pathname,
+	struct index_state *istate = context;
+	return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
 				  mode, stage,
 				  ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
 }
@@ -47,7 +53,8 @@ static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
 				const char *pathname, unsigned mode, int stage,
 				void *context)
 {
-	return read_one_entry_opt(sha1, base->buf, base->len, pathname,
+	struct index_state *istate = context;
+	return read_one_entry_opt(istate, sha1, base->buf, base->len, pathname,
 				  mode, stage,
 				  ADD_CACHE_JUST_APPEND);
 }
@@ -144,7 +151,8 @@ static int cmp_cache_name_compare(const void *a_, const void *b_)
 				  ce2->name, ce2->ce_namelen, ce_stage(ce2));
 }
 
-int read_tree(struct tree *tree, int stage, struct pathspec *match)
+int read_tree(struct tree *tree, int stage, struct pathspec *match,
+	      struct index_state *istate)
 {
 	read_tree_fn_t fn = NULL;
 	int i, err;
@@ -164,23 +172,23 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)
 	 * do it the original slow way, otherwise, append and then
 	 * sort at the end.
 	 */
-	for (i = 0; !fn && i < active_nr; i++) {
-		const struct cache_entry *ce = active_cache[i];
+	for (i = 0; !fn && i < istate->cache_nr; i++) {
+		const struct cache_entry *ce = istate->cache[i];
 		if (ce_stage(ce) == stage)
 			fn = read_one_entry;
 	}
 
 	if (!fn)
 		fn = read_one_entry_quick;
-	err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
+	err = read_tree_recursive(tree, "", 0, stage, match, fn, istate);
 	if (fn == read_one_entry || err)
 		return err;
 
 	/*
 	 * Sort the cache entry -- we need to nuke the cache tree, though.
 	 */
-	cache_tree_free(&active_cache_tree);
-	QSORT(active_cache, active_nr, cmp_cache_name_compare);
+	cache_tree_free(&istate->cache_tree);
+	QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
 	return 0;
 }
 
tree.h +2 -1
index 0d4734b94..744e6dc2a 100644
@@ -34,6 +34,7 @@ extern int read_tree_recursive(struct tree *tree,
 			       int stage, const struct pathspec *pathspec,
 			       read_tree_fn_t fn, void *context);
 
-extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec);
+extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec,
+		     struct index_state *istate);
 
 #endif /* TREE_H */
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#35 lore raw

PATCH 21/31 ls-files: convert overlay_tree_on_cache to take an index

In reply to Brandon Williams
---
 builtin/commit.c   |  3 ++-
 builtin/ls-files.c | 15 ++++++++-------
 cache.h            |  3 ++-
 3 files changed, 12 insertions(+), 9 deletions(-)
builtin/commit.c +2 -1
index 805da4915..3d98084fb 100644
@@ -254,7 +254,8 @@ static int list_paths(struct string_list *list, const char *with_tree,
 
 	if (with_tree) {
 		char *max_prefix = common_prefix(pattern);
-		overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
+		overlay_tree_on_index(&the_index, with_tree,
+				      max_prefix ? max_prefix : prefix);
 		free(max_prefix);
 	}
 
builtin/ls-files.c +8 -7
index 620487a77..a5ceeb052 100644
@@ -432,7 +432,8 @@ static int get_common_prefix_len(const char *common_prefix)
  * that were given from the command line.  We are not
  * going to write this index out.
  */
-void overlay_tree_on_cache(const char *tree_name, const char *prefix)
+void overlay_tree_on_index(struct index_state *istate,
+			   const char *tree_name, const char *prefix)
 {
 	struct tree *tree;
 	struct object_id oid;
@@ -447,8 +448,8 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
 		die("bad tree-ish %s", tree_name);
 
 	/* Hoist the unmerged entries up to stage #3 to make room */
-	for (i = 0; i < active_nr; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < istate->cache_nr; i++) {
+		struct cache_entry *ce = istate->cache[i];
 		if (!ce_stage(ce))
 			continue;
 		ce->ce_flags |= CE_STAGEMASK;
@@ -461,11 +462,11 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
 			       PATHSPEC_PREFER_CWD, prefix, matchbuf);
 	} else
 		memset(&pathspec, 0, sizeof(pathspec));
-	if (read_tree(tree, 1, &pathspec, &the_index))
+	if (read_tree(tree, 1, &pathspec, istate))
 		die("unable to read tree entries %s", tree_name);
 
-	for (i = 0; i < active_nr; i++) {
-		struct cache_entry *ce = active_cache[i];
+	for (i = 0; i < istate->cache_nr; i++) {
+		struct cache_entry *ce = istate->cache[i];
 		switch (ce_stage(ce)) {
 		case 0:
 			last_stage0 = ce;
@@ -680,7 +681,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 		 */
 		if (show_stage || show_unmerged)
 			die("ls-files --with-tree is incompatible with -s or -u");
-		overlay_tree_on_cache(with_tree, max_prefix);
+		overlay_tree_on_index(&the_index, with_tree, max_prefix);
 	}
 	show_files(&dir);
 	if (show_resolve_undo)
cache.h +2 -1
index 02ab5f801..73724a3ad 100644
@@ -1993,7 +1993,8 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
 #define ws_tab_width(rule)     ((rule) & WS_TAB_WIDTH_MASK)
 
 /* ls-files */
-void overlay_tree_on_cache(const char *tree_name, const char *prefix);
+void overlay_tree_on_index(struct index_state *istate,
+			   const char *tree_name, const char *prefix);
 
 char *alias_lookup(const char *alias);
 int split_cmdline(char *cmdline, const char ***argv);
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#36 lore raw

PATCH 22/31 ls-files: convert write_eolinfo to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
builtin/ls-files.c +6 -7
index a5ceeb052..c37e9de11 100644
@@ -54,17 +54,16 @@ static const char *tag_modified = "";
 static const char *tag_skip_worktree = "";
 static const char *tag_resolve_undo = "";
 
-static void write_eolinfo(const struct cache_entry *ce, const char *path)
+static void write_eolinfo(const struct index_state *istate,
+			  const struct cache_entry *ce, const char *path)
 {
-	if (!show_eol)
-		return;
-	else {
+	if (show_eol) {
 		struct stat st;
 		const char *i_txt = "";
 		const char *w_txt = "";
 		const char *a_txt = get_convert_attr_ascii(path);
 		if (ce && S_ISREG(ce->ce_mode))
-			i_txt = get_cached_convert_stats_ascii(&the_index,
+			i_txt = get_cached_convert_stats_ascii(istate,
 							       ce->name);
 		if (!lstat(path, &st) && S_ISREG(st.st_mode))
 			w_txt = get_wt_convert_stats_ascii(path);
@@ -106,7 +105,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
 		return;
 
 	fputs(tag, stdout);
-	write_eolinfo(NULL, ent->name);
+	write_eolinfo(NULL, NULL, ent->name);
 	write_name(ent->name);
 }
 
@@ -276,7 +275,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 			       find_unique_abbrev(ce->oid.hash, abbrev),
 			       ce_stage(ce));
 		}
-		write_eolinfo(ce, ce->name);
+		write_eolinfo(&the_index, ce, ce->name);
 		write_name(ce->name);
 		if (debug_mode) {
 			const struct stat_data *sd = &ce->ce_stat_data;
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#37 lore raw

PATCH 23/31 ls-files: convert show_killed_files to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
builtin/ls-files.c +11 -10
index c37e9de11..f9578cf9f 100644
@@ -121,7 +121,8 @@ static void show_other_files(struct dir_struct *dir)
 	}
 }
 
-static void show_killed_files(struct dir_struct *dir)
+static void show_killed_files(const struct index_state *istate,
+			      const struct dir_struct *dir)
 {
 	int i;
 	for (i = 0; i < dir->nr; i++) {
@@ -135,29 +136,29 @@ static void show_killed_files(struct dir_struct *dir)
 				/* If ent->name is prefix of an entry in the
 				 * cache, it will be killed.
 				 */
-				pos = cache_name_pos(ent->name, ent->len);
+				pos = index_name_pos(istate, ent->name, ent->len);
 				if (0 <= pos)
 					die("BUG: killed-file %.*s not found",
 						ent->len, ent->name);
 				pos = -pos - 1;
-				while (pos < active_nr &&
-				       ce_stage(active_cache[pos]))
+				while (pos < istate->cache_nr &&
+				       ce_stage(istate->cache[pos]))
 					pos++; /* skip unmerged */
-				if (active_nr <= pos)
+				if (istate->cache_nr <= pos)
 					break;
 				/* pos points at a name immediately after
 				 * ent->name in the cache.  Does it expect
 				 * ent->name to be a directory?
 				 */
-				len = ce_namelen(active_cache[pos]);
+				len = ce_namelen(istate->cache[pos]);
 				if ((ent->len < len) &&
-				    !strncmp(active_cache[pos]->name,
+				    !strncmp(istate->cache[pos]->name,
 					     ent->name, ent->len) &&
-				    active_cache[pos]->name[ent->len] == '/')
+				    istate->cache[pos]->name[ent->len] == '/')
 					killed = 1;
 				break;
 			}
-			if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
+			if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
 				/* If any of the leading directories in
 				 * ent->name is registered in the cache,
 				 * ent->name will be killed.
@@ -338,7 +339,7 @@ static void show_files(struct dir_struct *dir)
 		if (show_others)
 			show_other_files(dir);
 		if (show_killed)
-			show_killed_files(dir);
+			show_killed_files(&the_index, dir);
 	}
 	if (show_cached || show_stage) {
 		for (i = 0; i < active_nr; i++) {
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#38 lore raw

PATCH 24/31 ls-files: convert show_other_files to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
builtin/ls-files.c +4 -3
index f9578cf9f..d00ca7810 100644
@@ -109,13 +109,14 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
 	write_name(ent->name);
 }
 
-static void show_other_files(struct dir_struct *dir)
+static void show_other_files(const struct index_state *istate,
+			     const struct dir_struct *dir)
 {
 	int i;
 
 	for (i = 0; i < dir->nr; i++) {
 		struct dir_entry *ent = dir->entries[i];
-		if (!cache_name_is_other(ent->name, ent->len))
+		if (!index_name_is_other(istate, ent->name, ent->len))
 			continue;
 		show_dir_entry(tag_other, ent);
 	}
@@ -337,7 +338,7 @@ static void show_files(struct dir_struct *dir)
 			dir->flags |= DIR_COLLECT_KILLED_ONLY;
 		fill_directory(dir, &the_index, &pathspec);
 		if (show_others)
-			show_other_files(dir);
+			show_other_files(&the_index, dir);
 		if (show_killed)
 			show_killed_files(&the_index, dir);
 	}
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#39 lore raw

PATCH 25/31 ls-files: convert show_ru_info to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
builtin/ls-files.c +4 -4
index d00ca7810..2838e2f75 100644
@@ -293,14 +293,14 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 	strbuf_release(&name);
 }
 
-static void show_ru_info(void)
+static void show_ru_info(const struct index_state *istate)
 {
 	struct string_list_item *item;
 
-	if (!the_index.resolve_undo)
+	if (!istate->resolve_undo)
 		return;
 
-	for_each_string_list_item(item, the_index.resolve_undo) {
+	for_each_string_list_item(item, istate->resolve_undo) {
 		const char *path = item->string;
 		struct resolve_undo_info *ui = item->util;
 		int i, len;
@@ -686,7 +686,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	}
 	show_files(&dir);
 	if (show_resolve_undo)
-		show_ru_info();
+		show_ru_info(&the_index);
 
 	if (ps_matched) {
 		int bad;
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#40 lore raw

PATCH 26/31 ls-files: convert ce_excluded to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
builtin/ls-files.c +5 -4
index 2838e2f75..289c6b2a5 100644
@@ -322,10 +322,11 @@ static void show_ru_info(const struct index_state *istate)
 	}
 }
 
-static int ce_excluded(struct dir_struct *dir, const struct cache_entry *ce)
+static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
+		       const struct cache_entry *ce)
 {
 	int dtype = ce_to_dtype(ce);
-	return is_excluded(dir, &the_index, ce->name, &dtype);
+	return is_excluded(dir, istate, ce->name, &dtype);
 }
 
 static void show_files(struct dir_struct *dir)
@@ -346,7 +347,7 @@ static void show_files(struct dir_struct *dir)
 		for (i = 0; i < active_nr; i++) {
 			const struct cache_entry *ce = active_cache[i];
 			if ((dir->flags & DIR_SHOW_IGNORED) &&
-			    !ce_excluded(dir, ce))
+			    !ce_excluded(dir, &the_index, ce))
 				continue;
 			if (show_unmerged && !ce_stage(ce))
 				continue;
@@ -362,7 +363,7 @@ static void show_files(struct dir_struct *dir)
 			struct stat st;
 			int err;
 			if ((dir->flags & DIR_SHOW_IGNORED) &&
-			    !ce_excluded(dir, ce))
+			    !ce_excluded(dir, &the_index, ce))
 				continue;
 			if (ce->ce_flags & CE_UPDATE)
 				continue;
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#41 lore raw

PATCH 27/31 ls-files: convert prune_cache to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
builtin/ls-files.c +8 -7
index 289c6b2a5..e2d8fb7f6 100644
@@ -381,30 +381,31 @@ static void show_files(struct dir_struct *dir)
 /*
  * Prune the index to only contain stuff starting with "prefix"
  */
-static void prune_cache(const char *prefix, size_t prefixlen)
+static void prune_index(struct index_state *istate,
+			const char *prefix, size_t prefixlen)
 {
 	int pos;
 	unsigned int first, last;
 
 	if (!prefix)
 		return;
-	pos = cache_name_pos(prefix, prefixlen);
+	pos = index_name_pos(istate, prefix, prefixlen);
 	if (pos < 0)
 		pos = -pos-1;
 	first = pos;
-	last = active_nr;
+	last = istate->cache_nr;
 	while (last > first) {
 		int next = (last + first) >> 1;
-		const struct cache_entry *ce = active_cache[next];
+		const struct cache_entry *ce = istate->cache[next];
 		if (!strncmp(ce->name, prefix, prefixlen)) {
 			first = next+1;
 			continue;
 		}
 		last = next;
 	}
-	memmove(active_cache, active_cache + pos,
+	memmove(istate->cache, istate->cache + pos,
 		(last - pos) * sizeof(struct cache_entry *));
-	active_nr = last - pos;
+	istate->cache_nr = last - pos;
 }
 
 static int get_common_prefix_len(const char *common_prefix)
@@ -662,7 +663,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 		max_prefix = common_prefix(&pathspec);
 	max_prefix_len = get_common_prefix_len(max_prefix);
 
-	prune_cache(max_prefix, max_prefix_len);
+	prune_index(&the_index, max_prefix, max_prefix_len);
 
 	/* Treat unmatching pathspec elements as errors */
 	if (pathspec.nr && error_unmatch)
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#42 lore raw

PATCH 28/31 ls-files: convert show_files to take an index

In reply to Brandon Williams
---
 builtin/ls-files.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
builtin/ls-files.c +12 -12
index e2d8fb7f6..3061af2c5 100644
@@ -329,7 +329,7 @@ static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
 	return is_excluded(dir, istate, ce->name, &dtype);
 }
 
-static void show_files(struct dir_struct *dir)
+static void show_files(struct index_state *istate, struct dir_struct *dir)
 {
 	int i;
 
@@ -337,17 +337,17 @@ static void show_files(struct dir_struct *dir)
 	if (show_others || show_killed) {
 		if (!show_others)
 			dir->flags |= DIR_COLLECT_KILLED_ONLY;
-		fill_directory(dir, &the_index, &pathspec);
+		fill_directory(dir, istate, &pathspec);
 		if (show_others)
-			show_other_files(&the_index, dir);
+			show_other_files(istate, dir);
 		if (show_killed)
-			show_killed_files(&the_index, dir);
+			show_killed_files(istate, dir);
 	}
 	if (show_cached || show_stage) {
-		for (i = 0; i < active_nr; i++) {
-			const struct cache_entry *ce = active_cache[i];
+		for (i = 0; i < istate->cache_nr; i++) {
+			const struct cache_entry *ce = istate->cache[i];
 			if ((dir->flags & DIR_SHOW_IGNORED) &&
-			    !ce_excluded(dir, &the_index, ce))
+			    !ce_excluded(dir, istate, ce))
 				continue;
 			if (show_unmerged && !ce_stage(ce))
 				continue;
@@ -358,12 +358,12 @@ static void show_files(struct dir_struct *dir)
 		}
 	}
 	if (show_deleted || show_modified) {
-		for (i = 0; i < active_nr; i++) {
-			const struct cache_entry *ce = active_cache[i];
+		for (i = 0; i < istate->cache_nr; i++) {
+			const struct cache_entry *ce = istate->cache[i];
 			struct stat st;
 			int err;
 			if ((dir->flags & DIR_SHOW_IGNORED) &&
-			    !ce_excluded(dir, &the_index, ce))
+			    !ce_excluded(dir, istate, ce))
 				continue;
 			if (ce->ce_flags & CE_UPDATE)
 				continue;
@@ -372,7 +372,7 @@ static void show_files(struct dir_struct *dir)
 			err = lstat(ce->name, &st);
 			if (show_deleted && err)
 				show_ce_entry(tag_removed, ce);
-			if (show_modified && ce_modified(ce, &st, 0))
+			if (show_modified && ie_modified(istate, ce, &st, 0))
 				show_ce_entry(tag_modified, ce);
 		}
 	}
@@ -686,7 +686,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 			die("ls-files --with-tree is incompatible with -s or -u");
 		overlay_tree_on_index(&the_index, with_tree, max_prefix);
 	}
-	show_files(&dir);
+	show_files(&the_index, &dir);
 	if (show_resolve_undo)
 		show_ru_info(&the_index);
 
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#43 lore raw

PATCH 29/31 ls-files: factor out debug info into a function

In reply to Brandon Williams
---
 builtin/ls-files.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
builtin/ls-files.c +14 -9
index 3061af2c5..2849c9a65 100644
@@ -94,6 +94,19 @@ static void write_name(const char *name)
 	strbuf_release(&full_name);
 }
 
+static void print_debug(const struct cache_entry *ce)
+{
+	if (debug_mode) {
+		const struct stat_data *sd = &ce->ce_stat_data;
+
+		printf("  ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
+		printf("  mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
+		printf("  dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
+		printf("  uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
+		printf("  size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
+	}
+}
+
 static void show_dir_entry(const char *tag, struct dir_entry *ent)
 {
 	int len = max_prefix_len;
@@ -279,15 +292,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 		}
 		write_eolinfo(&the_index, ce, ce->name);
 		write_name(ce->name);
-		if (debug_mode) {
-			const struct stat_data *sd = &ce->ce_stat_data;
-
-			printf("  ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
-			printf("  mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
-			printf("  dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino);
-			printf("  uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid);
-			printf("  size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags);
-		}
+		print_debug(ce);
 	}
 
 	strbuf_release(&name);
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#44 lore raw

PATCH 30/31 ls-files: factor out tag calculation

In reply to Brandon Williams
---
 builtin/ls-files.c | 41 +++++++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 16 deletions(-)
builtin/ls-files.c +25 -16
index 2849c9a65..6a0302a28 100644
@@ -94,6 +94,30 @@ static void write_name(const char *name)
 	strbuf_release(&full_name);
 }
 
+static const char *get_tag(const struct cache_entry *ce, const char *tag)
+{
+	static char alttag[4];
+
+	if (tag && *tag && show_valid_bit && (ce->ce_flags & CE_VALID)) {
+		memcpy(alttag, tag, 3);
+
+		if (isalpha(tag[0])) {
+			alttag[0] = tolower(tag[0]);
+		} else if (tag[0] == '?') {
+			alttag[0] = '!';
+		} else {
+			alttag[0] = 'v';
+			alttag[1] = tag[0];
+			alttag[2] = ' ';
+			alttag[3] = 0;
+		}
+
+		tag = alttag;
+	}
+
+	return tag;
+}
+
 static void print_debug(const struct cache_entry *ce)
 {
 	if (debug_mode) {
@@ -264,22 +288,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 				  len, ps_matched,
 				  S_ISDIR(ce->ce_mode) ||
 				  S_ISGITLINK(ce->ce_mode))) {
-		if (tag && *tag && show_valid_bit &&
-		    (ce->ce_flags & CE_VALID)) {
-			static char alttag[4];
-			memcpy(alttag, tag, 3);
-			if (isalpha(tag[0]))
-				alttag[0] = tolower(tag[0]);
-			else if (tag[0] == '?')
-				alttag[0] = '!';
-			else {
-				alttag[0] = 'v';
-				alttag[1] = tag[0];
-				alttag[2] = ' ';
-				alttag[3] = 0;
-			}
-			tag = alttag;
-		}
+		tag = get_tag(ce, tag);
 
 		if (!show_stage) {
 			fputs(tag, stdout);
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#45 lore raw

PATCH 31/31 ls-files: use repository object

In reply to Brandon Williams

Convert ls-files to use a repository struct and recurse submodules inprocess.

---
 builtin/ls-files.c                     | 211 ++++++++++++++-------------------
 git.c                                  |   2 +-
 repo.c                                 |   3 +
 repo.h                                 |   1 +
 t/t3007-ls-files-recurse-submodules.sh |  39 ++++++
 5 files changed, 135 insertions(+), 121 deletions(-)
builtin/ls-files.c +91 -120
index 6a0302a28..46962815f 100644
@@ -5,6 +5,7 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
+#define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "quote.h"
@@ -17,6 +18,7 @@
 #include "pathspec.h"
 #include "run-command.h"
 #include "submodule.h"
+#include "repo.h"
 
 static int abbrev;
 static int show_deleted;
@@ -32,10 +34,8 @@ static int line_terminator = '\n';
 static int debug_mode;
 static int show_eol;
 static int recurse_submodules;
-static struct argv_array submodule_options = ARGV_ARRAY_INIT;
 
 static const char *prefix;
-static const char *super_prefix;
 static int max_prefix_len;
 static int prefix_len;
 static struct pathspec pathspec;
@@ -73,25 +73,12 @@ static void write_eolinfo(const struct index_state *istate,
 
 static void write_name(const char *name)
 {
-	/*
-	 * Prepend the super_prefix to name to construct the full_name to be
-	 * written.
-	 */
-	struct strbuf full_name = STRBUF_INIT;
-	if (super_prefix) {
-		strbuf_addstr(&full_name, super_prefix);
-		strbuf_addstr(&full_name, name);
-		name = full_name.buf;
-	}
-
 	/*
 	 * With "--full-name", prefix_len=0; this caller needs to pass
 	 * an empty string in that case (a NULL is good for "").
 	 */
 	write_name_quoted_relative(name, prefix_len ? prefix : NULL,
 				   stdout, line_terminator);
-
-	strbuf_release(&full_name);
 }
 
 static const char *get_tag(const struct cache_entry *ce, const char *tag)
@@ -210,101 +197,59 @@ static void show_killed_files(const struct index_state *istate,
 	}
 }
 
-/*
- * Compile an argv_array with all of the options supported by --recurse_submodules
- */
-static void compile_submodule_options(const char **argv,
-				      const struct dir_struct *dir,
-				      int show_tag)
-{
-	if (line_terminator == '\0')
-		argv_array_push(&submodule_options, "-z");
-	if (show_tag)
-		argv_array_push(&submodule_options, "-t");
-	if (show_valid_bit)
-		argv_array_push(&submodule_options, "-v");
-	if (show_cached)
-		argv_array_push(&submodule_options, "--cached");
-	if (show_eol)
-		argv_array_push(&submodule_options, "--eol");
-	if (debug_mode)
-		argv_array_push(&submodule_options, "--debug");
-
-	/* Add Pathspecs */
-	argv_array_push(&submodule_options, "--");
-	for (; *argv; argv++)
-		argv_array_push(&submodule_options, *argv);
-}
+static void show_files(struct repo *repo, struct dir_struct *dir);
 
-/**
- * Recursively call ls-files on a submodule
- */
-static void show_gitlink(const struct cache_entry *ce)
+static void show_submodule(const struct repo *superproject,
+			   struct dir_struct *dir, const char *path)
 {
-	struct child_process cp = CHILD_PROCESS_INIT;
-	int status;
-	char *dir;
-
-	prepare_submodule_repo_env(&cp.env_array);
-	argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
-
-	if (prefix_len)
-		argv_array_pushf(&cp.env_array, "%s=%s",
-				 GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
-				 prefix);
-	argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
-			 super_prefix ? super_prefix : "",
-			 ce->name);
-	argv_array_push(&cp.args, "ls-files");
-	argv_array_push(&cp.args, "--recurse-submodules");
-
-	/* add supported options */
-	argv_array_pushv(&cp.args, submodule_options.argv);
-
-	cp.git_cmd = 1;
-	dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
-	cp.dir = dir;
-	status = run_command(&cp);
-	free(dir);
-	if (status)
-		exit(status);
+	struct repo submodule;
+	char *gitdir = mkpathdup("%s/%s", superproject->worktree, path);
+	repo_init(&submodule, gitdir);
+
+	repo_read_index(&submodule);
+	repo_read_gitmodules(&submodule);
+
+	if (superproject->submodule_prefix)
+		submodule.submodule_prefix = xstrfmt("%s%s/", superproject->submodule_prefix, path);
+	else
+		submodule.submodule_prefix = xstrfmt("%s/", path);
+	show_files(&submodule, dir);
+
+	repo_clear(&submodule);
+	free(gitdir);
 }
 
-static void show_ce_entry(const char *tag, const struct cache_entry *ce)
+static void show_ce(struct repo *repo, struct dir_struct *dir,
+		    const struct cache_entry *ce, const char *fullname,
+		    const char *tag)
 {
-	struct strbuf name = STRBUF_INIT;
-	int len = max_prefix_len;
-	if (super_prefix)
-		strbuf_addstr(&name, super_prefix);
-	strbuf_addstr(&name, ce->name);
-
-	if (len > ce_namelen(ce))
+	if (max_prefix_len > strlen(fullname))
 		die("git ls-files: internal error - cache entry not superset of prefix");
 
 	if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
-	    submodule_path_match(&pathspec, name.buf, ps_matched)) {
-		show_gitlink(ce);
-	} else if (match_pathspec(&pathspec, name.buf, name.len,
-				  len, ps_matched,
+	    submodule_path_match(&pathspec, fullname, ps_matched) &&
+	    is_submodule_active(repo, ce->name)) {
+		show_submodule(repo, dir, ce->name);
+	} else if (match_pathspec(&pathspec, fullname, strlen(fullname),
+				  max_prefix_len, ps_matched,
 				  S_ISDIR(ce->ce_mode) ||
 				  S_ISGITLINK(ce->ce_mode))) {
 		tag = get_tag(ce, tag);
 
-		if (!show_stage) {
-			fputs(tag, stdout);
-		} else {
+		if (show_stage) {
 			printf("%s%06o %s %d\t",
 			       tag,
 			       ce->ce_mode,
 			       find_unique_abbrev(ce->oid.hash, abbrev),
 			       ce_stage(ce));
+		} else {
+			fputs(tag, stdout);
 		}
-		write_eolinfo(&the_index, ce, ce->name);
-		write_name(ce->name);
+
+		write_eolinfo(repo->index, ce, fullname);
+		write_name(fullname);
 		print_debug(ce);
 	}
-
-	strbuf_release(&name);
 }
 
 static void show_ru_info(const struct index_state *istate)
@@ -337,59 +282,80 @@ static void show_ru_info(const struct index_state *istate)
 }
 
 static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
-		       const struct cache_entry *ce)
+		       const char *fullname, const struct cache_entry *ce)
 {
 	int dtype = ce_to_dtype(ce);
-	return is_excluded(dir, istate, ce->name, &dtype);
+	return is_excluded(dir, istate, fullname, &dtype);
 }
 
-static void show_files(struct index_state *istate, struct dir_struct *dir)
+static void construct_fullname(struct strbuf *out, const struct repo *repo,
+			       const struct cache_entry *ce)
+{
+	strbuf_reset(out);
+	if (repo->submodule_prefix)
+		strbuf_addstr(out, repo->submodule_prefix);
+	strbuf_addstr(out, ce->name);
+
+}
+
+static void show_files(struct repo *repo, struct dir_struct *dir)
 {
 	int i;
+	struct strbuf fullname = STRBUF_INIT;
 
 	/* For cached/deleted files we don't need to even do the readdir */
 	if (show_others || show_killed) {
 		if (!show_others)
 			dir->flags |= DIR_COLLECT_KILLED_ONLY;
-		fill_directory(dir, istate, &pathspec);
+		fill_directory(dir, repo->index, &pathspec);
 		if (show_others)
-			show_other_files(istate, dir);
+			show_other_files(repo->index, dir);
 		if (show_killed)
-			show_killed_files(istate, dir);
+			show_killed_files(repo->index, dir);
 	}
 	if (show_cached || show_stage) {
-		for (i = 0; i < istate->cache_nr; i++) {
-			const struct cache_entry *ce = istate->cache[i];
+		for (i = 0; i < repo->index->cache_nr; i++) {
+			const struct cache_entry *ce = repo->index->cache[i];
+
+			construct_fullname(&fullname, repo, ce);
+
 			if ((dir->flags & DIR_SHOW_IGNORED) &&
-			    !ce_excluded(dir, istate, ce))
+			    !ce_excluded(dir, repo->index, fullname.buf, ce))
 				continue;
 			if (show_unmerged && !ce_stage(ce))
 				continue;
 			if (ce->ce_flags & CE_UPDATE)
 				continue;
-			show_ce_entry(ce_stage(ce) ? tag_unmerged :
-				(ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
+			show_ce(repo, dir, ce, fullname.buf,
+				ce_stage(ce) ? tag_unmerged :
+				(ce_skip_worktree(ce) ? tag_skip_worktree :
+				 tag_cached));
 		}
 	}
 	if (show_deleted || show_modified) {
-		for (i = 0; i < istate->cache_nr; i++) {
-			const struct cache_entry *ce = istate->cache[i];
+		for (i = 0; i < repo->index->cache_nr; i++) {
+			const struct cache_entry *ce = repo->index->cache[i];
 			struct stat st;
 			int err;
+
+			construct_fullname(&fullname, repo, ce);
+
 			if ((dir->flags & DIR_SHOW_IGNORED) &&
-			    !ce_excluded(dir, istate, ce))
+			    !ce_excluded(dir, repo->index, fullname.buf, ce))
 				continue;
 			if (ce->ce_flags & CE_UPDATE)
 				continue;
 			if (ce_skip_worktree(ce))
 				continue;
-			err = lstat(ce->name, &st);
+			err = lstat(fullname.buf, &st);
 			if (show_deleted && err)
-				show_ce_entry(tag_removed, ce);
-			if (show_modified && ie_modified(istate, ce, &st, 0))
-				show_ce_entry(tag_modified, ce);
+				show_ce(repo, dir, ce, fullname.buf, tag_removed);
+			if (show_modified && ie_modified(repo->index, ce, &st, 0))
+				show_ce(repo, dir, ce, fullname.buf, tag_modified);
 		}
 	}
+
+	strbuf_release(&fullname);
 }
 
 /*
@@ -544,7 +510,7 @@ static int option_parse_exclude_standard(const struct option *opt,
 int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 {
 	int require_work_tree = 0, show_tag = 0, i;
-	const char *max_prefix;
+	char *max_prefix;
 	struct dir_struct dir;
 	struct exclude_list *el;
 	struct string_list exclude_list = STRING_LIST_INIT_NODUP;
@@ -614,11 +580,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	prefix = cmd_prefix;
 	if (prefix)
 		prefix_len = strlen(prefix);
-	super_prefix = get_super_prefix();
 	git_config(git_default_config, NULL);
 
-	if (read_cache() < 0)
-		die("index file corrupt");
+	repo_read_index(&the_repository);
 
 	argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
 			ls_files_usage, 0);
@@ -650,12 +614,14 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	if (require_work_tree && !is_inside_work_tree())
 		setup_work_tree();
 
-	if (recurse_submodules)
-		compile_submodule_options(argv, &dir, show_tag);
+	if (recurse_submodules) {
+		repo_read_gitmodules(&the_repository);
+	}
 
 	if (recurse_submodules &&
 	    (show_stage || show_deleted || show_others || show_unmerged ||
-	     show_killed || show_modified || show_resolve_undo || with_tree))
+	     show_killed || show_modified || show_resolve_undo || with_tree ||
+	     show_eol))
 		die("ls-files --recurse-submodules unsupported mode");
 
 	if (recurse_submodules && error_unmatch)
@@ -669,7 +635,10 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	/*
 	 * Find common prefix for all pathspec's
 	 * This is used as a performance optimization which unfortunately cannot
-	 * be done when recursing into submodules
+	 * be done when recursing into submodules because when a pathspec is
+	 * given which spans repository boundaries you can't simply remove the
+	 * submodule entry because the pathspec may match something inside the
+	 * submodule.
 	 */
 	if (recurse_submodules)
 		max_prefix = NULL;
@@ -677,7 +646,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 		max_prefix = common_prefix(&pathspec);
 	max_prefix_len = get_common_prefix_len(max_prefix);
 
-	prune_index(&the_index, max_prefix, max_prefix_len);
+	prune_index(the_repository.index, max_prefix, max_prefix_len);
 
 	/* Treat unmatching pathspec elements as errors */
 	if (pathspec.nr && error_unmatch)
@@ -698,11 +667,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 		 */
 		if (show_stage || show_unmerged)
 			die("ls-files --with-tree is incompatible with -s or -u");
-		overlay_tree_on_index(&the_index, with_tree, max_prefix);
+		overlay_tree_on_index(the_repository.index, with_tree, max_prefix);
 	}
-	show_files(&the_index, &dir);
+
+	show_files(&the_repository, &dir);
+
 	if (show_resolve_undo)
-		show_ru_info(&the_index);
+		show_ru_info(the_repository.index);
 
 	if (ps_matched) {
 		int bad;
git.c +1 -1
index 594436e43..16c6e325d 100644
@@ -442,7 +442,7 @@ static struct cmd_struct commands[] = {
 	{ "init-db", cmd_init_db },
 	{ "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY },
 	{ "log", cmd_log, RUN_SETUP },
-	{ "ls-files", cmd_ls_files, RUN_SETUP | SUPPORT_SUPER_PREFIX },
+	{ "ls-files", cmd_ls_files, RUN_SETUP },
 	{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
 	{ "ls-tree", cmd_ls_tree, RUN_SETUP },
 	{ "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY },
repo.c +3 -0
index 6f7b2015f..01001e577 100644
@@ -154,6 +154,7 @@ int repo_init(struct repo *repo, const char *gitdir)
 	}
 
 	repo_set_gitdir(repo, resolved_gitdir);
+	repo_set_worktree(repo, gitdir);
 
 	/* NEEDSWORK: Verify repository format version */
 
@@ -169,6 +170,8 @@ void repo_clear(struct repo *repo)
 	repo_clear_env(repo);
 	free(repo->worktree);
 	repo->worktree = NULL;
+	free(repo->submodule_prefix);
+	repo->submodule_prefix = NULL;
 
 	if (repo->config) {
 		git_configset_clear(repo->config);
repo.h +1 -0
index ad0184eaa..cb83f765b 100644
@@ -14,6 +14,7 @@ struct repo {
 	char *graft_file;
 	char *namespace;
 	char *worktree;
+	char *submodule_prefix;
 
 	/* Subsystems */
 	/*
t/t3007-ls-files-recurse-submodules.sh +39 -0
index ebb956fd1..318b5bce7 100755
@@ -135,6 +135,45 @@ test_expect_success '--recurse-submodules and pathspecs setup' '
 	test_cmp expect actual
 '
 
+test_expect_success 'inactive submodule' '
+	test_when_finished "git config --bool submodule.submodule.active true" &&
+	test_when_finished "git -C submodule config --bool submodule.subsub.active true" &&
+	git config --bool submodule.submodule.active "false" &&
+
+	cat >expect <<-\EOF &&
+	.gitmodules
+	a
+	b/b
+	h.txt
+	sib/file
+	sub/file
+	submodule
+	EOF
+
+	git ls-files --recurse-submodules >actual &&
+	test_cmp expect actual &&
+
+	git config --bool submodule.submodule.active "true" &&
+	git -C submodule config --bool submodule.subsub.active "false" &&
+
+	cat >expect <<-\EOF &&
+	.gitmodules
+	a
+	b/b
+	h.txt
+	sib/file
+	sub/file
+	submodule/.gitmodules
+	submodule/c
+	submodule/f.TXT
+	submodule/g.txt
+	submodule/subsub
+	EOF
+
+	git ls-files --recurse-submodules >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success '--recurse-submodules and pathspecs' '
 	cat >expect <<-\EOF &&
 	h.txt
Signature · 1 line
2.13.0.506.g27d5fe0cd-goog
#46 lore raw

Re: [PATCH 31/31] ls-files: use repository object

Quoted 15 lines · Brandon Williams

Convert ls-files to use a repository struct and recurse submodules inprocess.

builtin/ls-files.c +8 -0
+static void show_submodule(const struct repo *superproject,
+                          struct dir_struct *dir, const char *path)
 {
+       struct repo submodule;
+       char *gitdir = mkpathdup("%s/%s", superproject->worktree, path);
+       repo_init(&submodule, gitdir);
+
+       repo_read_index(&submodule);
+       repo_read_gitmodules(&submodule);

builtin/ls-files.c static void show_killed_files(const struct index_state *istate, · line 220

Quoted 10 lines
builtin/ls-files.c +9 -0
+
+       if (superproject->submodule_prefix)
+               submodule.submodule_prefix = xstrfmt("%s%s/", superproject->submodule_prefix, path);
+       else
+               submodule.submodule_prefix = xstrfmt("%s/", path);
+       show_files(&submodule, dir);
+
+       repo_clear(&submodule);
+       free(gitdir);
 }

I like how it seems easy now to do work in another repository. :)

git.c static struct cmd_struct commands[] = { · line 445

Quoted 2 lines
git.c +1 -1
-       { "ls-files", cmd_ls_files, RUN_SETUP | SUPPORT_SUPER_PREFIX },
+       { "ls-files", cmd_ls_files, RUN_SETUP },

With this step, we can get rid of SUPPORT_SUPER_PREFIX eventually.

I do not have comments on the patches in the middle, but they cleared up some of the questions that I asked in the early patches.

Thanks,
Stefan

#47 lore raw

Re: [PATCH 31/31] ls-files: use repository object

Quoted 20 lines · Stefan Beller
Quoted 19 lines · Brandon Williams

Convert ls-files to use a repository struct and recurse submodules inprocess.

builtin/ls-files.c +12 -0
+static void show_submodule(const struct repo *superproject,
+                          struct dir_struct *dir, const char *path)
 {
+       struct repo submodule;
+       char *gitdir = mkpathdup("%s/%s", superproject->worktree, path);
+       repo_init(&submodule, gitdir);
+
+       repo_read_index(&submodule);
+       repo_read_gitmodules(&submodule);
+
+       if (superproject->submodule_prefix)
+               submodule.submodule_prefix = xstrfmt("%s%s/", superproject->submodule_prefix, path);
+       else
Quoted 8 lines

builtin/ls-files.c static void show_killed_files(const struct index_state *istate, · line 220

Quoted 6 lines
builtin/ls-files.c +5 -0
+               submodule.submodule_prefix = xstrfmt("%s/", path);
+       show_files(&submodule, dir);
+
+       repo_clear(&submodule);
+       free(gitdir);
 }

I like how it seems easy now to do work in another repository. :)

It really does make working with another repo easy! No more compiling argv options and spawning child processes :D

Quoted 5 lines

git.c static struct cmd_struct commands[] = { · line 445

Quoted 3 lines
git.c +1 -1
-       { "ls-files", cmd_ls_files, RUN_SETUP | SUPPORT_SUPER_PREFIX },
+       { "ls-files", cmd_ls_files, RUN_SETUP },

With this step, we can get rid of SUPPORT_SUPER_PREFIX eventually.

Yes Ideally most of the little hacks I introduced when originally teaching ls-files and grep to recurse could be removed.

Quoted 6 lines

I do not have comments on the patches in the middle, but they cleared up some of the questions that I asked in the early patches.

Thanks,
Stefan

Signature · 1 line
Brandon Williams
#48 lore raw
In reply to Brandon Williams
Quoted 11 lines · Brandon Williams

Given the vast interest expressed when I sent out my RFC series I decided it would be worth it to invest more time to making a repository object a reality.

This series is an extension of the last series I sent out (in that ls-files is converted to working on submodules in-process using repository objects instead of spawning a child process to do the work). The big difference from the RFC series is that I went through and did the work to migrate key repository state from global variables in 'environment.c' to being stored in a repository object itself. I migrated the bits of state that seemed reasonable for this series, there is still a lot of global state which could be migrated in the future.

Quoted 4 lines

I do think that we need to be slightly cautious about moving global state into the repository object though, I don't want 'struct repo' to simply become a kitchen sink where everything gets dumped. But this is just a warning for the future.

Or in other words:
You want to have another struct e.g. 'the_command_line_arguments', which would carry the verbosity/color options for example as they are not related to a repo object, but to the current command being run?

Quoted 5 lines

Since this is a v1 I'm fairly certain that it still has a lot of rough edges (like I think I need to write better commit messages, and we should probably have more comments documenting object fields/contract) but I want to get the review process started sooner rather than later since I'm sure people will have opinions (e.g. should it be called 'struct repo' or 'struct repository'?!).

IMHO this is the most obvious, but bikesheddable part of the series. ;) Keep it short as everyone knows what a 'repo' is.

#49 lore raw
Quoted 17 lines · Stefan Beller
Quoted 11 lines · Brandon Williams

Given the vast interest expressed when I sent out my RFC series I decided it would be worth it to invest more time to making a repository object a reality.

This series is an extension of the last series I sent out (in that ls-files is converted to working on submodules in-process using repository objects instead of spawning a child process to do the work). The big difference from the RFC series is that I went through and did the work to migrate key repository state from global variables in 'environment.c' to being stored in a repository object itself. I migrated the bits of state that seemed reasonable for this series, there is still a lot of global state which could be migrated in the future.

Quoted 4 lines

I do think that we need to be slightly cautious about moving global state into the repository object though, I don't want 'struct repo' to simply become a kitchen sink where everything gets dumped. But this is just a warning for the future.

Quoted 4 lines

Or in other words:
You want to have another struct e.g. 'the_command_line_arguments', which would carry the verbosity/color options for example as they are not related to a repo object, but to the current command being run?

Yes exactly. Library code that needs to operate on a repository would then be able to take arguments like:

some_library_function(struct repo *repo, struct lib_opts *ops)

Much like how the grep machinery takes a grep_opts struct.

Quoted 8 lines
Quoted 6 lines

Since this is a v1 I'm fairly certain that it still has a lot of rough edges (like I think I need to write better commit messages, and we should probably have more comments documenting object fields/contract) but I want to get the review process started sooner rather than later since I'm sure people will have opinions (e.g. should it be called 'struct repo' or 'struct repository'?!).

IMHO this is the most obvious, but bikesheddable part of the series. ;)

I know, that's why I mentioned it ;)

Quoted 1 line

Keep it short as everyone knows what a 'repo' is.

Signature · 1 line
Brandon Williams
#50 lore raw
In reply to Brandon Williams
Quoted 25 lines · Brandon Williams

Given the vast interest expressed when I sent out my RFC series I decided it would be worth it to invest more time to making a repository object a reality.

This series is an extension of the last series I sent out (in that ls-files is converted to working on submodules in-process using repository objects instead of spawning a child process to do the work). The big difference from the RFC series is that I went through and did the work to migrate key repository state from global variables in 'environment.c' to being stored in a repository object itself. I migrated the bits of state that seemed reasonable for this series, there is still a lot of global state which could be migrated in the future.

I do think that we need to be slightly cautious about moving global state into the repository object though, I don't want 'struct repo' to simply become a kitchen sink where everything gets dumped. But this is just a warning for the future.

Since this is a v1 I'm fairly certain that it still has a lot of rough edges (like I think I need to write better commit messages, and we should probably have more comments documenting object fields/contract) but I want to get the review process started sooner rather than later since I'm sure people will have opinions (e.g. should it be called 'struct repo' or 'struct repository'?!).

So here it is! Thank you so much for taking the time review this, any and all comments would be appreciated.

Quoted 7 lines

- Brandon

Patches [01-14] -> Introducing the Repository object and migrating some state
to be stored in 'the_repository'.

Patches [15-31] -> Converting ls-files to use 'struct repo' in order to recurse
submodules in-process.

For those who don't like reviewing patches in email form, you can find this series at:
https://github.com/bmwill/git/tree/repository-object

Signature · 1 line
Brandon Williams
#51 lore raw
Quoted 4 lines · Brandon Williams

For those who don't like reviewing patches in email form, you can find this series at:
https://github.com/bmwill/git/tree/repository-object

I was just asking Brandon for a remote to pull these patches from.

As I was looking at the very first patch, I'd like to lobby for trying out origin/sb/diff-color-moved, as it will tell you about code movements. :)

Thanks,
Stefan

#52 lore raw
Quoted 6 lines · Stefan Beller
Quoted 4 lines · Brandon Williams

For those who don't like reviewing patches in email form, you can find this series at:
https://github.com/bmwill/git/tree/repository-object

Quoted 7 lines

I was just asking Brandon for a remote to pull these patches from.

As I was looking at the very first patch, I'd like to lobby for trying out origin/sb/diff-color-moved, as it will tell you about code movements. :)

Thanks,
Stefan

Yep, I'll be doing so.

Thanks,
Jake

#53 lore raw

v2 cover repository object

In reply to Brandon Williams

When I sent out my RFC series there seemed to be a lot of interest but I haven't seen many people jump to review this series. Despite lack of review I wanted to get out another version which includes some changes to fix things that were bugging me about the series. Hopfully this v2 will prod some more people to take a look.

The meat of the series is really from 04-15 and patch 32 which converts ls-files to recurse using a repository object. So if you're pressed for time you can focus on those patches.

You can find this series at: https://github.com/bmwill/git/tree/repository-object

What's different in v2:
* 'struct repo' has been renamed 'struct repository'. Really just an aesthetic change and it looks more "official" without having a shortened name.
* Better commit messages on a few of the patches which were soarly lacking.
* I found that 'git_config_iter' was being exported without an implementation so I added a patch to remove it.
* repo_init is a bit cleaner and is written in such a way to not call 'die()', instead it returns a non-zero return value upon failure.

Thanks for your help!

Brandon Williams (32):
config: create config.h
config: remove git_config_iter
config: don't include config.h by default
config: don't implicitly use gitdir
setup: don't perform lazy initialization of repository state
environment: remove namespace_len variable
repository: introduce the repository object
environment: place key repository state in the_repository
environment: store worktree in the_repository
setup: add comment indicating a hack
config: read config from a repository object
repository: add index_state to struct repo
submodule-config: store the_submodule_cache in the_repository
submodule: add repo_read_gitmodules
submodule: convert is_submodule_initialized to work on a repository
convert: convert get_cached_convert_stats_ascii to take an index
convert: convert crlf_to_git to take an index
convert: convert convert_to_git_filter_fd to take an index
convert: convert convert_to_git to take an index
convert: convert renormalize_buffer to take an index
tree: convert read_tree to take an index parameter
ls-files: convert overlay_tree_on_cache to take an index
ls-files: convert write_eolinfo to take an index
ls-files: convert show_killed_files to take an index
ls-files: convert show_other_files to take an index
ls-files: convert show_ru_info to take an index
ls-files: convert ce_excluded to take an index
ls-files: convert prune_cache to take an index
ls-files: convert show_files to take an index
ls-files: factor out debug info into a function
ls-files: factor out tag calculation
ls-files: use repository object

 Makefile                               |   1 +
 advice.c                               |   1 +
 alias.c                                |   1 +
 apply.c                                |   3 +-
 archive-tar.c                          |   1 +
 archive-zip.c                          |   1 +
 archive.c                              |   1 +
 attr.c                                 |   1 +
 bisect.c                               |   1 +
 branch.c                               |   1 +
 builtin/add.c                          |   1 +
 builtin/am.c                           |   1 +
 builtin/blame.c                        |   3 +-
 builtin/branch.c                       |   1 +
 builtin/cat-file.c                     |   1 +
 builtin/check-attr.c                   |   1 +
 builtin/check-ignore.c                 |   1 +
 builtin/check-mailmap.c                |   1 +
 builtin/checkout-index.c               |   1 +
 builtin/checkout.c                     |   1 +
 builtin/clean.c                        |   1 +
 builtin/clone.c                        |   1 +
 builtin/column.c                       |   1 +
 builtin/commit-tree.c                  |   1 +
 builtin/commit.c                       |   4 +-
 builtin/config.c                       |   3 +
 builtin/count-objects.c                |   1 +
 builtin/describe.c                     |   1 +
 builtin/diff-files.c                   |   1 +
 builtin/diff-index.c                   |   1 +
 builtin/diff-tree.c                    |   1 +
 builtin/diff.c                         |   1 +
 builtin/difftool.c                     |   1 +
 builtin/fast-export.c                  |   1 +
 builtin/fetch.c                        |   1 +
 builtin/fmt-merge-msg.c                |   1 +
 builtin/for-each-ref.c                 |   1 +
 builtin/fsck.c                         |   1 +
 builtin/gc.c                           |   1 +
 builtin/grep.c                         |   4 +-
 builtin/hash-object.c                  |   1 +
 builtin/help.c                         |   1 +
 builtin/index-pack.c                   |   1 +
 builtin/init-db.c                      |   1 +
 builtin/log.c                          |   1 +
 builtin/ls-files.c                     | 330 ++++++++++++++++-----------------
 builtin/ls-tree.c                      |   1 +
 builtin/merge-base.c                   |   1 +
 builtin/merge-file.c                   |   1 +
 builtin/merge.c                        |   1 +
 builtin/mv.c                           |   1 +
 builtin/name-rev.c                     |   1 +
 builtin/notes.c                        |   1 +
 builtin/pack-objects.c                 |   1 +
 builtin/patch-id.c                     |   1 +
 builtin/pull.c                         |   1 +
 builtin/push.c                         |   1 +
 builtin/read-tree.c                    |   1 +
 builtin/rebase--helper.c               |   1 +
 builtin/receive-pack.c                 |   1 +
 builtin/reflog.c                       |   1 +
 builtin/remote.c                       |   1 +
 builtin/repack.c                       |   1 +
 builtin/replace.c                      |   1 +
 builtin/rerere.c                       |   1 +
 builtin/reset.c                        |   1 +
 builtin/rev-list.c                     |   1 +
 builtin/rev-parse.c                    |   1 +
 builtin/revert.c                       |   1 +
 builtin/rm.c                           |   1 +
 builtin/send-pack.c                    |   1 +
 builtin/shortlog.c                     |   1 +
 builtin/show-branch.c                  |   1 +
 builtin/stripspace.c                   |   1 +
 builtin/submodule--helper.c            |  10 +-
 builtin/symbolic-ref.c                 |   1 +
 builtin/tag.c                          |   1 +
 builtin/unpack-file.c                  |   1 +
 builtin/unpack-objects.c               |   1 +
 builtin/update-index.c                 |   1 +
 builtin/update-ref.c                   |   1 +
 builtin/update-server-info.c           |   1 +
 builtin/var.c                          |   1 +
 builtin/verify-commit.c                |   1 +
 builtin/verify-pack.c                  |   1 +
 builtin/verify-tag.c                   |   1 +
 builtin/worktree.c                     |   1 +
 builtin/write-tree.c                   |   1 +
 cache.h                                | 196 +-------------------
 color.c                                |   1 +
 column.c                               |   1 +
 combine-diff.c                         |   2 +-
 config.c                               | 210 ++++++++++++++-------
 config.h                               | 217 ++++++++++++++++++++++
 connect.c                              |   1 +
 convert.c                              |  32 ++--
 convert.h                              |  19 +-
 credential-cache--daemon.c             |   1 +
 credential.c                           |   1 +
 daemon.c                               |   1 +
 diff.c                                 |   7 +-
 dir.c                                  |   3 +-
 environment.c                          | 101 ++++------
 fast-import.c                          |   1 +
 fetch-pack.c                           |   1 +
 git.c                                  |   3 +-
 gpg-interface.c                        |   1 +
 graph.c                                |   1 +
 grep.c                                 |   1 +
 help.c                                 |   1 +
 http-backend.c                         |   1 +
 http-fetch.c                           |   1 +
 http.c                                 |   1 +
 ident.c                                |   1 +
 imap-send.c                            |   1 +
 ll-merge.c                             |   3 +-
 log-tree.c                             |   1 +
 mailinfo.c                             |   1 +
 merge-recursive.c                      |   5 +-
 notes-utils.c                          |   1 +
 notes.c                                |   1 +
 pager.c                                |   1 +
 parse-options.c                        |   1 +
 path.c                                 |  11 +-
 pathspec.c                             |   1 +
 pretty.c                               |   1 +
 prompt.c                               |   1 +
 read-cache.c                           |   1 +
 refs.c                                 |   1 +
 refs/files-backend.c                   |   1 +
 remote-curl.c                          |   1 +
 remote.c                               |   1 +
 repository.c                           | 216 +++++++++++++++++++++
 repository.h                           |  45 +++++
 rerere.c                               |   1 +
 send-pack.c                            |   1 +
 sequencer.c                            |   1 +
 setup.c                                |  34 ++++
 sha1_file.c                            |   7 +-
 sha1_name.c                            |   1 +
 submodule-config.c                     |  71 +++++--
 submodule-config.h                     |  10 +
 submodule.c                            |  36 ++--
 submodule.h                            |   4 +-
 t/helper/test-config.c                 |   1 +
 t/helper/test-submodule-config.c       |   1 +
 t/t3007-ls-files-recurse-submodules.sh |  39 ++++
 trailer.c                              |   1 +
 transport.c                            |   1 +
 tree.c                                 |  28 ++-
 tree.h                                 |   3 +-
 unpack-trees.c                         |   1 +
 upload-pack.c                          |   1 +
 userdiff.c                             |   1 +
 versioncmp.c                           |   1 +
 wrapper.c                              |   1 +
 xdiff-interface.c                      |   1 +
 157 files changed, 1199 insertions(+), 586 deletions(-)
 create mode 100644 config.h
 create mode 100644 repository.c
 create mode 100644 repository.h
Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#54 lore raw

PATCH v2 1/32 config: create config.h

Move all config related declarations from cache.h to a new config.h header file. This makes cache.h smaller and allows for the opportunity in a following patch to only include config.h when needed.

---
 cache.h  | 190 +------------------------------------------------------------
 config.h | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 195 insertions(+), 189 deletions(-)
 create mode 100644 config.h
cache.h +1 -189
index ae4c45d37..a27fee458 100644
@@ -11,6 +11,7 @@
 #include "string-list.h"
 #include "pack-revindex.h"
 #include "hash.h"
+#include "config.h"
 
 #ifndef platform_SHA_CTX
 /*
@@ -1866,188 +1867,9 @@ extern int packed_object_info(struct packed_git *pack, off_t offset, struct obje
 /* Dumb servers support */
 extern int update_server_info(int);
 
-/* git_config_parse_key() returns these negated: */
-#define CONFIG_INVALID_KEY 1
-#define CONFIG_NO_SECTION_OR_NAME 2
-/* git_config_set_gently(), git_config_set_multivar_gently() return the above or these: */
-#define CONFIG_NO_LOCK -1
-#define CONFIG_INVALID_FILE 3
-#define CONFIG_NO_WRITE 4
-#define CONFIG_NOTHING_SET 5
-#define CONFIG_INVALID_PATTERN 6
-#define CONFIG_GENERIC_ERROR 7
-
-#define CONFIG_REGEX_NONE ((void *)1)
-
-struct git_config_source {
-	unsigned int use_stdin:1;
-	const char *file;
-	const char *blob;
-};
-
-enum config_origin_type {
-	CONFIG_ORIGIN_BLOB,
-	CONFIG_ORIGIN_FILE,
-	CONFIG_ORIGIN_STDIN,
-	CONFIG_ORIGIN_SUBMODULE_BLOB,
-	CONFIG_ORIGIN_CMDLINE
-};
-
-struct config_options {
-	unsigned int respect_includes : 1;
-	const char *git_dir;
-};
-
-typedef int (*config_fn_t)(const char *, const char *, void *);
-extern int git_default_config(const char *, const char *, void *);
-extern int git_config_from_file(config_fn_t fn, const char *, void *);
-extern int git_config_from_mem(config_fn_t fn, const enum config_origin_type,
-					const char *name, const char *buf, size_t len, void *data);
-extern int git_config_from_blob_sha1(config_fn_t fn, const char *name,
-				     const unsigned char *sha1, void *data);
-extern void git_config_push_parameter(const char *text);
-extern int git_config_from_parameters(config_fn_t fn, void *data);
-extern void read_early_config(config_fn_t cb, void *data);
-extern void git_config(config_fn_t fn, void *);
-extern int git_config_with_options(config_fn_t fn, void *,
-				   struct git_config_source *config_source,
-				   const struct config_options *opts);
-extern int git_parse_ulong(const char *, unsigned long *);
-extern int git_parse_maybe_bool(const char *);
-extern int git_config_int(const char *, const char *);
-extern int64_t git_config_int64(const char *, const char *);
-extern unsigned long git_config_ulong(const char *, const char *);
-extern ssize_t git_config_ssize_t(const char *, const char *);
-extern int git_config_bool_or_int(const char *, const char *, int *);
-extern int git_config_bool(const char *, const char *);
-extern int git_config_maybe_bool(const char *, const char *);
-extern int git_config_string(const char **, const char *, const char *);
-extern int git_config_pathname(const char **, const char *, const char *);
-extern int git_config_set_in_file_gently(const char *, const char *, const char *);
-extern void git_config_set_in_file(const char *, const char *, const char *);
-extern int git_config_set_gently(const char *, const char *);
-extern void git_config_set(const char *, const char *);
-extern int git_config_parse_key(const char *, char **, int *);
-extern int git_config_key_is_valid(const char *key);
-extern int git_config_set_multivar_gently(const char *, const char *, const char *, int);
-extern void git_config_set_multivar(const char *, const char *, const char *, int);
-extern int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, int);
-extern void git_config_set_multivar_in_file(const char *, const char *, const char *, const char *, int);
-extern int git_config_rename_section(const char *, const char *);
-extern int git_config_rename_section_in_file(const char *, const char *, const char *);
-extern const char *git_etc_gitconfig(void);
-extern int git_env_bool(const char *, int);
-extern unsigned long git_env_ulong(const char *, unsigned long);
-extern int git_config_system(void);
-extern int config_error_nonbool(const char *);
-#if defined(__GNUC__)
-#define config_error_nonbool(s) (config_error_nonbool(s), const_error())
-#endif
 extern const char *get_log_output_encoding(void);
 extern const char *get_commit_output_encoding(void);
 
-extern int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
-
-enum config_scope {
-	CONFIG_SCOPE_UNKNOWN = 0,
-	CONFIG_SCOPE_SYSTEM,
-	CONFIG_SCOPE_GLOBAL,
-	CONFIG_SCOPE_REPO,
-	CONFIG_SCOPE_CMDLINE,
-};
-
-extern enum config_scope current_config_scope(void);
-extern const char *current_config_origin_type(void);
-extern const char *current_config_name(void);
-
-struct config_include_data {
-	int depth;
-	config_fn_t fn;
-	void *data;
-	const struct config_options *opts;
-};
-#define CONFIG_INCLUDE_INIT { 0 }
-extern int git_config_include(const char *name, const char *value, void *data);
-
-/*
- * Match and parse a config key of the form:
- *
- *   section.(subsection.)?key
- *
- * (i.e., what gets handed to a config_fn_t). The caller provides the section;
- * we return -1 if it does not match, 0 otherwise. The subsection and key
- * out-parameters are filled by the function (and *subsection is NULL if it is
- * missing).
- *
- * If the subsection pointer-to-pointer passed in is NULL, returns 0 only if
- * there is no subsection at all.
- */
-extern int parse_config_key(const char *var,
-			    const char *section,
-			    const char **subsection, int *subsection_len,
-			    const char **key);
-
-struct config_set_element {
-	struct hashmap_entry ent;
-	char *key;
-	struct string_list value_list;
-};
-
-struct configset_list_item {
-	struct config_set_element *e;
-	int value_index;
-};
-
-/*
- * the contents of the list are ordered according to their
- * position in the config files and order of parsing the files.
- * (i.e. key-value pair at the last position of .git/config will
- * be at the last item of the list)
- */
-struct configset_list {
-	struct configset_list_item *items;
-	unsigned int nr, alloc;
-};
-
-struct config_set {
-	struct hashmap config_hash;
-	int hash_initialized;
-	struct configset_list list;
-};
-
-extern void git_configset_init(struct config_set *cs);
-extern int git_configset_add_file(struct config_set *cs, const char *filename);
-extern int git_configset_get_value(struct config_set *cs, const char *key, const char **value);
-extern const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
-extern void git_configset_clear(struct config_set *cs);
-extern int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest);
-extern int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
-extern int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
-extern int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
-extern int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
-extern int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
-extern int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
-extern int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
-
-extern int git_config_get_value(const char *key, const char **value);
-extern const struct string_list *git_config_get_value_multi(const char *key);
-extern void git_config_clear(void);
-extern void git_config_iter(config_fn_t fn, void *data);
-extern int git_config_get_string_const(const char *key, const char **dest);
-extern int git_config_get_string(const char *key, char **dest);
-extern int git_config_get_int(const char *key, int *dest);
-extern int git_config_get_ulong(const char *key, unsigned long *dest);
-extern int git_config_get_bool(const char *key, int *dest);
-extern int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest);
-extern int git_config_get_maybe_bool(const char *key, int *dest);
-extern int git_config_get_pathname(const char *key, const char **dest);
-extern int git_config_get_untracked_cache(void);
-extern int git_config_get_split_index(void);
-extern int git_config_get_max_percent_split_change(void);
-
-/* This dies if the configured or default date is in the future */
-extern int git_config_get_expiry(const char *key, const char **output);
-
 /*
  * This is a hack for test programs like test-dump-untracked-cache to
  * ensure that they do not modify the untracked cache when reading it.
@@ -2055,16 +1877,6 @@ extern int git_config_get_expiry(const char *key, const char **output);
  */
 extern int ignore_untracked_cache_config;
 
-struct key_value_info {
-	const char *filename;
-	int linenr;
-	enum config_origin_type origin_type;
-	enum config_scope scope;
-};
-
-extern NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3)));
-extern NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr);
-
 extern int committer_ident_sufficiently_given(void);
 extern int author_ident_sufficiently_given(void);
 
config.h new file +194 -0

194 lines not on this page · raw message

Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#55 lore raw

PATCH v2 2/32 config: remove git_config_iter

In reply to Brandon Williams

Since there is no implementation of the function 'git_config_iter' lets stop exporting it and remove the prototype from config.h.

---
 config.h | 1 -
 1 file changed, 1 deletion(-)
config.h +0 -1
index f7f8b66c5..c70599bd5 100644
@@ -165,7 +165,6 @@ extern int git_configset_get_pathname(struct config_set *cs, const char *key, co
 extern int git_config_get_value(const char *key, const char **value);
 extern const struct string_list *git_config_get_value_multi(const char *key);
 extern void git_config_clear(void);
-extern void git_config_iter(config_fn_t fn, void *data);
 extern int git_config_get_string_const(const char *key, const char **dest);
 extern int git_config_get_string(const char *key, char **dest);
 extern int git_config_get_int(const char *key, int *dest);
Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#56 lore raw

PATCH v2 3/32 config: don't include config.h by default

In reply to Brandon Williams

Stop including config.h by default in cache.h. Instead only include config.h in those files which require use of the config system.

---
 advice.c                         | 1 +
 alias.c                          | 1 +
 apply.c                          | 1 +
 archive-tar.c                    | 1 +
 archive-zip.c                    | 1 +
 archive.c                        | 1 +
 attr.c                           | 1 +
 bisect.c                         | 1 +
 branch.c                         | 1 +
 builtin/add.c                    | 1 +
 builtin/am.c                     | 1 +
 builtin/blame.c                  | 1 +
 builtin/branch.c                 | 1 +
 builtin/cat-file.c               | 1 +
 builtin/check-attr.c             | 1 +
 builtin/check-ignore.c           | 1 +
 builtin/check-mailmap.c          | 1 +
 builtin/checkout-index.c         | 1 +
 builtin/checkout.c               | 1 +
 builtin/clean.c                  | 1 +
 builtin/clone.c                  | 1 +
 builtin/column.c                 | 1 +
 builtin/commit-tree.c            | 1 +
 builtin/commit.c                 | 1 +
 builtin/config.c                 | 1 +
 builtin/count-objects.c          | 1 +
 builtin/describe.c               | 1 +
 builtin/diff-files.c             | 1 +
 builtin/diff-index.c             | 1 +
 builtin/diff-tree.c              | 1 +
 builtin/diff.c                   | 1 +
 builtin/difftool.c               | 1 +
 builtin/fast-export.c            | 1 +
 builtin/fetch.c                  | 1 +
 builtin/fmt-merge-msg.c          | 1 +
 builtin/for-each-ref.c           | 1 +
 builtin/fsck.c                   | 1 +
 builtin/gc.c                     | 1 +
 builtin/grep.c                   | 1 +
 builtin/hash-object.c            | 1 +
 builtin/help.c                   | 1 +
 builtin/index-pack.c             | 1 +
 builtin/init-db.c                | 1 +
 builtin/log.c                    | 1 +
 builtin/ls-files.c               | 1 +
 builtin/ls-tree.c                | 1 +
 builtin/merge-base.c             | 1 +
 builtin/merge-file.c             | 1 +
 builtin/merge.c                  | 1 +
 builtin/mv.c                     | 1 +
 builtin/name-rev.c               | 1 +
 builtin/notes.c                  | 1 +
 builtin/pack-objects.c           | 1 +
 builtin/patch-id.c               | 1 +
 builtin/pull.c                   | 1 +
 builtin/push.c                   | 1 +
 builtin/read-tree.c              | 1 +
 builtin/rebase--helper.c         | 1 +
 builtin/receive-pack.c           | 1 +
 builtin/reflog.c                 | 1 +
 builtin/remote.c                 | 1 +
 builtin/repack.c                 | 1 +
 builtin/replace.c                | 1 +
 builtin/rerere.c                 | 1 +
 builtin/reset.c                  | 1 +
 builtin/rev-list.c               | 1 +
 builtin/rev-parse.c              | 1 +
 builtin/revert.c                 | 1 +
 builtin/rm.c                     | 1 +
 builtin/send-pack.c              | 1 +
 builtin/shortlog.c               | 1 +
 builtin/show-branch.c            | 1 +
 builtin/stripspace.c             | 1 +
 builtin/submodule--helper.c      | 1 +
 builtin/symbolic-ref.c           | 1 +
 builtin/tag.c                    | 1 +
 builtin/unpack-file.c            | 1 +
 builtin/unpack-objects.c         | 1 +
 builtin/update-index.c           | 1 +
 builtin/update-ref.c             | 1 +
 builtin/update-server-info.c     | 1 +
 builtin/var.c                    | 1 +
 builtin/verify-commit.c          | 1 +
 builtin/verify-pack.c            | 1 +
 builtin/verify-tag.c             | 1 +
 builtin/worktree.c               | 1 +
 builtin/write-tree.c             | 1 +
 cache.h                          | 1 -
 color.c                          | 1 +
 column.c                         | 1 +
 config.c                         | 1 +
 connect.c                        | 1 +
 convert.c                        | 1 +
 credential-cache--daemon.c       | 1 +
 credential.c                     | 1 +
 daemon.c                         | 1 +
 diff.c                           | 1 +
 dir.c                            | 1 +
 environment.c                    | 1 +
 fast-import.c                    | 1 +
 fetch-pack.c                     | 1 +
 git.c                            | 1 +
 gpg-interface.c                  | 1 +
 graph.c                          | 1 +
 grep.c                           | 1 +
 help.c                           | 1 +
 http-backend.c                   | 1 +
 http-fetch.c                     | 1 +
 http.c                           | 1 +
 ident.c                          | 1 +
 imap-send.c                      | 1 +
 ll-merge.c                       | 1 +
 log-tree.c                       | 1 +
 mailinfo.c                       | 1 +
 merge-recursive.c                | 1 +
 notes-utils.c                    | 1 +
 notes.c                          | 1 +
 pager.c                          | 1 +
 parse-options.c                  | 1 +
 pathspec.c                       | 1 +
 pretty.c                         | 1 +
 prompt.c                         | 1 +
 read-cache.c                     | 1 +
 refs.c                           | 1 +
 refs/files-backend.c             | 1 +
 remote-curl.c                    | 1 +
 remote.c                         | 1 +
 rerere.c                         | 1 +
 send-pack.c                      | 1 +
 sequencer.c                      | 1 +
 setup.c                          | 1 +
 sha1_file.c                      | 1 +
 sha1_name.c                      | 1 +
 submodule-config.c               | 1 +
 submodule.c                      | 1 +
 t/helper/test-config.c           | 1 +
 t/helper/test-submodule-config.c | 1 +
 trailer.c                        | 1 +
 transport.c                      | 1 +
 unpack-trees.c                   | 1 +
 upload-pack.c                    | 1 +
 userdiff.c                       | 1 +
 versioncmp.c                     | 1 +
 wrapper.c                        | 1 +
 xdiff-interface.c                | 1 +
 145 files changed, 144 insertions(+), 1 deletion(-)
advice.c +1 -0
index b84ae4960..3fa04fca0 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 
 int advice_push_update_rejected = 1;
 int advice_push_non_ff_current = 1;
alias.c +1 -0
index 3b90397a9..052f34136 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 
 char *alias_lookup(const char *alias)
 {
apply.c +1 -0
index c49cef063..87db9618d 100644
@@ -8,6 +8,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "blob.h"
 #include "delta.h"
 #include "diff.h"
archive-tar.c +1 -0
index 073e60ebd..c6ed96ee7 100644
@@ -2,6 +2,7 @@
  * Copyright (c) 2005, 2006 Rene Scharfe
  */
 #include "cache.h"
+#include "config.h"
 #include "tar.h"
 #include "archive.h"
 #include "streaming.h"
archive-zip.c +1 -0
index 27563e9e2..e8913e5a2 100644
@@ -2,6 +2,7 @@
  * Copyright (c) 2006 Rene Scharfe
  */
 #include "cache.h"
+#include "config.h"
 #include "archive.h"
 #include "streaming.h"
 #include "utf8.h"
archive.c +1 -0
index b15a922da..60b3035a7 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "tree-walk.h"
attr.c +1 -0
index 7e2134471..9f8b02936 100644
@@ -9,6 +9,7 @@
 
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "exec_cmd.h"
 #include "attr.h"
 #include "dir.h"
bisect.c +1 -0
index d88f9bc3d..c2acb9455 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "diff.h"
 #include "revision.h"
branch.c +1 -0
index 985316eb7..a8a548ccf 100644
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "cache.h"
+#include "config.h"
 #include "branch.h"
 #include "refs.h"
 #include "remote.h"
builtin/add.c +1 -0
index d9a2491e4..f2415e99f 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2006 Linus Torvalds
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "lockfile.h"
 #include "dir.h"
builtin/am.c +1 -0
index 0f63dcab1..0b6b1265d 100644
@@ -4,6 +4,7 @@
  * Based on git-am.sh by Junio C Hamano.
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "parse-options.h"
builtin/blame.c +1 -0
index 1043e5376..c0ae49298 100644
@@ -6,6 +6,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "builtin.h"
 #include "blob.h"
builtin/branch.c +1 -0
index 83fcda43d..c958e9325 100644
@@ -6,6 +6,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "color.h"
 #include "refs.h"
 #include "commit.h"
builtin/cat-file.c +1 -0
index 9af863e79..7bc45f600 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 #include "userdiff.h"
builtin/check-attr.c +1 -0
index 4d01ca0c8..91444dc04 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "quote.h"
 #include "parse-options.h"
builtin/check-ignore.c +1 -0
index c7b8c0889..3e280b9c7 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "quote.h"
 #include "pathspec.h"
builtin/check-mailmap.c +1 -0
index cf0f54f6b..cdce144f3 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "mailmap.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/checkout-index.c +1 -0
index 07631d0c9..39c8be05d 100644
@@ -5,6 +5,7 @@
  *
  */
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "quote.h"
 #include "cache-tree.h"
builtin/checkout.c +1 -0
index a6b2af39d..45fab5a4d 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "parse-options.h"
 #include "refs.h"
builtin/clean.c +1 -0
index 329b68c40..09adf0551 100644
@@ -8,6 +8,7 @@
 
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/clone.c +1 -0
index 743f16ae2..4922a5496 100644
@@ -9,6 +9,7 @@
  */
 
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "parse-options.h"
 #include "fetch-pack.h"
builtin/column.c +1 -0
index 33314b4d7..0c3223d64 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "strbuf.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/commit-tree.c +1 -0
index f39c2b273..a4a923d7c 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "tree.h"
 #include "builtin.h"
builtin/commit.c +1 -0
index da1ba4c86..805da4915 100644
@@ -6,6 +6,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "cache-tree.h"
 #include "color.h"
builtin/config.c +1 -0
index 7f6c25d4d..753c40a5c 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "color.h"
 #include "parse-options.h"
 #include "urlmatch.h"
builtin/count-objects.c +1 -0
index acb05940f..1d82e61f2 100644
@@ -5,6 +5,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "builtin.h"
 #include "parse-options.h"
builtin/describe.c +1 -0
index 893c8789f..70eb14460 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "commit.h"
 #include "tag.h"
builtin/diff-files.c +1 -0
index 15c61fd8d..29d3e4f41 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "revision.h"
builtin/diff-index.c +1 -0
index 1af373d00..e3d418361 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "revision.h"
builtin/diff-tree.c +1 -0
index 5ea1c1431..9af942b6b 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "log-tree.h"
builtin/diff.c +1 -0
index 8c03ddaf5..b85af112a 100644
@@ -4,6 +4,7 @@
  * Copyright (c) 2006 Junio C Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "color.h"
 #include "commit.h"
builtin/difftool.c +1 -0
index b9a892f26..9199227f6 100644
@@ -12,6 +12,7 @@
  * Copyright (C) 2016 Johannes Schindelin
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "run-command.h"
 #include "exec_cmd.h"
builtin/fast-export.c +1 -0
index 24e29ad7e..0dfd980dd 100644
@@ -5,6 +5,7 @@
  */
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "object.h"
builtin/fetch.c +1 -0
index d4d573b98..284595911 100644
@@ -2,6 +2,7 @@
  * "git fetch"
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "builtin.h"
builtin/fmt-merge-msg.c +1 -0
index 70137b0b7..10cbb4341 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "commit.h"
 #include "diff.h"
builtin/for-each-ref.c +1 -0
index eca365bf8..52be99cba 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "object.h"
 #include "parse-options.h"
builtin/fsck.c +1 -0
index cb2ba6cd1..90593491e 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "tree.h"
 #include "blob.h"
builtin/gc.c +1 -0
index f484eda43..bd91f136f 100644
@@ -11,6 +11,7 @@
  */
 
 #include "builtin.h"
+#include "config.h"
 #include "tempfile.h"
 #include "lockfile.h"
 #include "parse-options.h"
builtin/grep.c +1 -0
index c6c26e9b9..0f4a1e5a3 100644
@@ -4,6 +4,7 @@
  * Copyright (c) 2006 Junio C Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "blob.h"
 #include "tree.h"
 #include "commit.h"
builtin/hash-object.c +1 -0
index bbeaf20bc..d04baf999 100644
@@ -5,6 +5,7 @@
  * Copyright (C) Junio C Hamano, 2005
  */
 #include "builtin.h"
+#include "config.h"
 #include "blob.h"
 #include "quote.h"
 #include "parse-options.h"
builtin/help.c +1 -0
index 49f7a07f8..334a8494a 100644
@@ -2,6 +2,7 @@
  * Builtin help command
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "parse-options.h"
builtin/index-pack.c +1 -0
index 04b9dcaf0..edc1a91d8 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "delta.h"
 #include "pack.h"
 #include "csum-file.h"
builtin/init-db.c +1 -0
index 8a6acb0ec..47823f9aa 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "builtin.h"
 #include "exec_cmd.h"
builtin/log.c +1 -0
index a440601ef..72a9fd968 100644
@@ -5,6 +5,7 @@
  *		 2006 Junio Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "color.h"
 #include "commit.h"
builtin/ls-files.c +1 -0
index b376afc31..c4357dc30 100644
@@ -6,6 +6,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "quote.h"
 #include "dir.h"
 #include "builtin.h"
builtin/ls-tree.c +1 -0
index ee7b293b1..ef965408e 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "blob.h"
 #include "tree.h"
 #include "commit.h"
builtin/merge-base.c +1 -0
index 0c36a70ad..6dbd167d3 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "diff.h"
builtin/merge-file.c +1 -0
index 47dde7c39..b08803e61 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "xdiff/xdiff.h"
 #include "xdiff-interface.h"
 #include "parse-options.h"
builtin/merge.c +1 -0
index a4a098f40..2512370c1 100644
@@ -7,6 +7,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "builtin.h"
 #include "lockfile.h"
builtin/mv.c +1 -0
index 61d20037a..dcf6736b5 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2006 Johannes Schindelin
  */
 #include "builtin.h"
+#include "config.h"
 #include "pathspec.h"
 #include "lockfile.h"
 #include "dir.h"
builtin/name-rev.c +1 -0
index 7fc7e66e8..e21715f1d 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "tag.h"
 #include "refs.h"
builtin/notes.c +1 -0
index f2847c41e..231ad1f01 100644
@@ -8,6 +8,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "notes.h"
 #include "blob.h"
builtin/pack-objects.c +1 -0
index 80439047a..9fe1fa8e1 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "object.h"
 #include "blob.h"
builtin/patch-id.c +1 -0
index 81552e02e..970d0d30b 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 
 static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result)
 {
builtin/pull.c +1 -0
index 318c273eb..af325d9fc 100644
@@ -6,6 +6,7 @@
  * Fetch one or more remote refs and merge it/them into the current HEAD.
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 #include "exec_cmd.h"
builtin/push.c +1 -0
index a597759d8..76aa713d2 100644
@@ -2,6 +2,7 @@
  * "git push"
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "run-command.h"
 #include "builtin.h"
builtin/read-tree.c +1 -0
index 78d319365..6eb2a0e57 100644
@@ -5,6 +5,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "object.h"
 #include "tree.h"
builtin/rebase--helper.c +1 -0
index ca1ebb2fa..c82b4dce6 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "sequencer.h"
 
builtin/receive-pack.c +1 -0
index b1706a573..71c0c768d 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "pack.h"
 #include "refs.h"
builtin/reflog.c +1 -0
index 920c16dac..44cdc2dbd 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "commit.h"
 #include "refs.h"
builtin/remote.c +1 -0
index addf97ad2..a470ed7c6 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "parse-options.h"
 #include "transport.h"
 #include "remote.h"
builtin/repack.c +1 -0
index 38ba4ef82..f17a68a17 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "parse-options.h"
 #include "run-command.h"
builtin/replace.c +1 -0
index c921bc976..80a15cf35 100644
@@ -9,6 +9,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "refs.h"
 #include "parse-options.h"
builtin/rerere.c +1 -0
index 1bf72423b..ffb66e290 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "parse-options.h"
 #include "string-list.h"
builtin/reset.c +1 -0
index 430602d10..d0b604103 100644
@@ -8,6 +8,7 @@
  * Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
  */
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "tag.h"
 #include "object.h"
builtin/rev-list.c +1 -0
index 718c6059c..f8e2b7bf5 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "diff.h"
 #include "revision.h"
builtin/rev-parse.c +1 -0
index efdc14473..c78b7b33d 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "quote.h"
builtin/revert.c +1 -0
index 345d9586a..16028b9ea 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 #include "diff.h"
builtin/rm.c +1 -0
index 7c323d012..824d1de9e 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds 2006
  */
 #include "builtin.h"
+#include "config.h"
 #include "lockfile.h"
 #include "dir.h"
 #include "cache-tree.h"
builtin/send-pack.c +1 -0
index b8e2e74fe..633e0c3cd 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "pkt-line.h"
builtin/shortlog.c +1 -0
index 7cff1839f..43c4799ea 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "diff.h"
 #include "string-list.h"
builtin/show-branch.c +1 -0
index 4a6cc6f49..e4cf1b5bb 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "builtin.h"
builtin/stripspace.c +1 -0
index 1e62a008c..bdf032886 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "strbuf.h"
 
builtin/submodule--helper.c +1 -0
index 8cc648d85..4dcbfb952 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "parse-options.h"
 #include "quote.h"
 #include "pathspec.h"
builtin/symbolic-ref.c +1 -0
index 70addef15..df75cb9d4 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "cache.h"
 #include "refs.h"
 #include "parse-options.h"
builtin/tag.c +1 -0
index 1f74a56db..01154ea8d 100644
@@ -7,6 +7,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "refs.h"
 #include "tag.h"
builtin/unpack-file.c +1 -0
index 6fc6bcdf7..73f133419 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 
 static char *create_temp_file(unsigned char *sha1)
 {
builtin/unpack-objects.c +1 -0
index 8bc999776..193f8b9d5 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "object.h"
 #include "delta.h"
 #include "pack.h"
builtin/update-index.c +1 -0
index ebfc09faa..0a4c23648 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "quote.h"
 #include "cache-tree.h"
builtin/update-ref.c +1 -0
index 0b2ecf41a..40ccfc193 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "builtin.h"
 #include "parse-options.h"
builtin/update-server-info.c +1 -0
index 6c8cc3edc..873070e51 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "parse-options.h"
 
builtin/var.c +1 -0
index aedbb53a2..6c6f46b4a 100644
@@ -4,6 +4,7 @@
  * Copyright (C) Eric Biederman, 2005
  */
 #include "builtin.h"
+#include "config.h"
 
 static const char var_usage[] = "git var (-l | <variable>)";
 
builtin/verify-commit.c +1 -0
index 05b734e6d..ba38ac9b1 100644
@@ -6,6 +6,7 @@
  * Based on git-verify-tag
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "commit.h"
 #include "run-command.h"
builtin/verify-pack.c +1 -0
index c94e15693..c2a1a5c50 100644
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "parse-options.h"
 
builtin/verify-tag.c +1 -0
index 5199553d9..f9a5f7535 100644
@@ -6,6 +6,7 @@
  * Based on git-verify-tag.sh
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "tag.h"
 #include "run-command.h"
builtin/worktree.c +1 -0
index 793306ea5..0c5476ee9 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "dir.h"
 #include "parse-options.h"
builtin/write-tree.c +1 -0
index 084c0df78..bd0a78aa3 100644
@@ -5,6 +5,7 @@
  */
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "tree.h"
 #include "cache-tree.h"
 #include "parse-options.h"
cache.h +0 -1
index a27fee458..272f8e021 100644
@@ -11,7 +11,6 @@
 #include "string-list.h"
 #include "pack-revindex.h"
 #include "hash.h"
-#include "config.h"
 
 #ifndef platform_SHA_CTX
 /*
color.c +1 -0
index dee61557e..31b6207a0 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "color.h"
 
 static int git_use_color_default = GIT_COLOR_AUTO;
column.c +1 -0
index d55ead18e..ff7bdab1a 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
config.c +1 -0
index 146cb3452..2390f98e3 100644
@@ -6,6 +6,7 @@
  *
  */
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "exec_cmd.h"
 #include "strbuf.h"
connect.c +1 -0
index cd21a1b6f..efddb30ea 100644
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "cache.h"
+#include "config.h"
 #include "pkt-line.h"
 #include "quote.h"
 #include "refs.h"
convert.c +1 -0
index f1e168bc3..5f4a4b1f5 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "run-command.h"
 #include "quote.h"
credential-cache--daemon.c +1 -0
index f3814cc47..0d5c62509 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "credential.h"
 #include "unix-socket.h"
credential.c +1 -0
index aa996669f..67a523353 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "credential.h"
 #include "string-list.h"
 #include "run-command.h"
daemon.c +1 -0
index ac7181a48..30747075f 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "pkt-line.h"
 #include "run-command.h"
 #include "strbuf.h"
diff.c +1 -0
index f3546536b..2f2467c6d 100644
@@ -2,6 +2,7 @@
  * Copyright (C) 2005 Junio C Hamano
  */
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "quote.h"
 #include "diff.h"
dir.c +1 -0
index 3f3167e55..0c26a53d2 100644
@@ -9,6 +9,7 @@
  */
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "attr.h"
 #include "refs.h"
environment.c +1 -0
index aa478e71d..d40b21fb7 100644
@@ -8,6 +8,7 @@
  * are.
  */
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "fmt-merge-msg.h"
 #include "commit.h"
fast-import.c +1 -0
index e69d21968..2881d4898 100644
@@ -154,6 +154,7 @@ Format of STDIN stream:
 
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "object.h"
 #include "blob.h"
fetch-pack.c +1 -0
index cd86865be..fbbc99c88 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "refs.h"
 #include "pkt-line.h"
git.c +1 -0
index 8ff44f081..594436e43 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "exec_cmd.h"
 #include "help.h"
 #include "run-command.h"
gpg-interface.c +1 -0
index e44cc27da..8ab32df45 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "gpg-interface.h"
graph.c +1 -0
index 8b9049dd2..e7e20650d 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "color.h"
 #include "graph.h"
grep.c +1 -0
index 47cee4506..d5211fc5a 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "grep.h"
 #include "userdiff.h"
 #include "xdiff-interface.h"
help.c +1 -0
index db7f3d79a..5d44f811a 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "run-command.h"
http-backend.c +1 -0
index ba5ff1aa2..519025d2c 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "pkt-line.h"
 #include "object.h"
http-fetch.c +1 -0
index 3b556d661..8af380050 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "exec_cmd.h"
 #include "http.h"
 #include "walker.h"
http.c +1 -0
index d2e11ec6f..013bb0cc6 100644
@@ -1,5 +1,6 @@
 #include "git-compat-util.h"
 #include "http.h"
+#include "config.h"
 #include "pack.h"
 #include "sideband.h"
 #include "run-command.h"
ident.c +1 -0
index bea871c8e..d41fc9119 100644
@@ -6,6 +6,7 @@
  * Copyright (C) 2005 Linus Torvalds
  */
 #include "cache.h"
+#include "config.h"
 
 static struct strbuf git_default_name = STRBUF_INIT;
 static struct strbuf git_default_email = STRBUF_INIT;
imap-send.c +1 -0
index 857591660..59e9b12d2 100644
@@ -23,6 +23,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "credential.h"
 #include "exec_cmd.h"
 #include "run-command.h"
ll-merge.c +1 -0
index ac0d4a5d7..24ff94e1d 100644
@@ -5,6 +5,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "attr.h"
 #include "xdiff-interface.h"
 #include "run-command.h"
log-tree.c +1 -0
index a4ec11c2b..7e1dfd749 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "diff.h"
 #include "commit.h"
 #include "tag.h"
mailinfo.c +1 -0
index f92cb9f72..f59162453 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "utf8.h"
 #include "strbuf.h"
 #include "mailinfo.h"
merge-recursive.c +1 -0
index ae5238d82..c2494f34f 100644
@@ -4,6 +4,7 @@
  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
  */
 #include "cache.h"
+#include "config.h"
 #include "advice.h"
 #include "lockfile.h"
 #include "cache-tree.h"
notes-utils.c +1 -0
index 325ff3daa..ebde2273c 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "notes-utils.h"
notes.c +1 -0
index 542563b28..dbcfef4d7 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "notes.h"
 #include "blob.h"
 #include "tree.h"
pager.c +1 -0
index c113d898a..4dd9e1b26 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "sigchain.h"
 
parse-options.c +1 -0
index a23a1e67f..cbf84a604 100644
@@ -1,6 +1,7 @@
 #include "git-compat-util.h"
 #include "parse-options.h"
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "color.h"
 #include "utf8.h"
pathspec.c +1 -0
index 828405021..ecc5331c2 100644
@@ -1,5 +1,6 @@
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "pathspec.h"
 #include "attr.h"
pretty.c +1 -0
index 09701bd2f..9c9f81b5b 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "utf8.h"
 #include "diff.h"
prompt.c +1 -0
index 75406390c..6d5885d00 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "prompt.h"
read-cache.c +1 -0
index 92eb15c00..2650ad98a 100644
@@ -5,6 +5,7 @@
  */
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "lockfile.h"
 #include "cache-tree.h"
refs.c +1 -0
index 8af9641aa..7dfdc08bf 100644
@@ -3,6 +3,7 @@
  */
 
 #include "cache.h"
+#include "config.h"
 #include "hashmap.h"
 #include "lockfile.h"
 #include "iterator.h"
refs/files-backend.c +1 -0
index cb1f528cb..f88b94484 100644
@@ -1,4 +1,5 @@
 #include "../cache.h"
+#include "../config.h"
 #include "../refs.h"
 #include "refs-internal.h"
 #include "ref-cache.h"
remote-curl.c +1 -0
index ece45993d..0053b0954 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "remote.h"
 #include "strbuf.h"
 #include "walker.h"
remote.c +1 -0
index fdc52d802..a84e68767 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "remote.h"
 #include "refs.h"
 #include "commit.h"
rerere.c +1 -0
index 3bd55caf3..344d6aa81 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "string-list.h"
 #include "rerere.h"
send-pack.c +1 -0
index 78bb34ebe..ed3cee321 100644
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "pkt-line.h"
sequencer.c +1 -0
index a23b948ac..b9962dc89 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "lockfile.h"
 #include "sequencer.h"
 #include "dir.h"
setup.c +1 -0
index e3f7699a9..e99a82cbe 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "string-list.h"
 
sha1_file.c +1 -0
index 59a4ed2ed..44561e0b9 100644
@@ -7,6 +7,7 @@
  * creation etc.
  */
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 #include "lockfile.h"
 #include "delta.h"
sha1_name.c +1 -0
index 389276e9d..0ad85ec54 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "tag.h"
 #include "commit.h"
 #include "tree.h"
submodule-config.c +1 -0
index 4f58491dd..d8f8d5ea3 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "submodule-config.h"
 #include "submodule.h"
 #include "strbuf.h"
submodule.c +1 -0
index bf5a93d16..95328de61 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "submodule-config.h"
 #include "submodule.h"
 #include "dir.h"
t/helper/test-config.c +1 -0
index 8e3ed6a76..1a7b8bd3d 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 
 /*
t/helper/test-submodule-config.c +1 -0
index 2f144d539..c6c57bba0 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "submodule-config.h"
 #include "submodule.h"
 
trailer.c +1 -0
index 11f0b9fb4..751b56c00 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 #include "run-command.h"
 #include "commit.h"
transport.c +1 -0
index 9bfcf870f..b9995306f 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "transport.h"
 #include "run-command.h"
 #include "pkt-line.h"
unpack-trees.c +1 -0
index d38c37e38..dd535bc84 100644
@@ -1,5 +1,6 @@
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "dir.h"
 #include "tree.h"
 #include "tree-walk.h"
upload-pack.c +1 -0
index 5330c02c1..7efff2fbf 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "refs.h"
 #include "pkt-line.h"
 #include "sideband.h"
userdiff.c +1 -0
index 8b732e40b..2c1502f71 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "userdiff.h"
 #include "attr.h"
 
versioncmp.c +1 -0
index 9f81dc106..069ee94a4 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 
 /*
wrapper.c +1 -0
index d83741770..487a9f753 100644
@@ -2,6 +2,7 @@
  * Various trivial helper wrappers around standard functions
  */
 #include "cache.h"
+#include "config.h"
 
 static void do_nothing(size_t size)
 {
xdiff-interface.c +1 -0
index 060038c2d..5ac07d734 100644
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "xdiff-interface.h"
 #include "xdiff/xtypes.h"
 #include "xdiff/xdiffi.h"
Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#57 lore raw

PATCH v2 4/32 config: don't implicitly use gitdir

In reply to Brandon Williams

Commit 2185fde56 (config: handle conditional include when $GIT_DIR is not set up) added a 'git_dir' field to the config_options struct. Let's use this option field explicitly all the time instead of occasionally falling back to calling 'git_pathdup("config")' to get the path to the local repository configuration. This allows 'do_git_config_sequence()' to not implicitly rely on global repository state.

---
 builtin/config.c | 2 ++
 config.c         | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
builtin/config.c +2 -0
index 753c40a5c..90f49a6ee 100644
@@ -539,6 +539,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		config_options.respect_includes = !given_config_source.file;
 	else
 		config_options.respect_includes = respect_includes_opt;
+	if (have_git_dir())
+		config_options.git_dir = get_git_common_dir();
 
 	if (end_null) {
 		term = '\0';
config.c +2 -2
index 2390f98e3..ff09b27b8 100644
@@ -1548,8 +1548,6 @@ static int do_git_config_sequence(const struct config_options *opts,
 
 	if (opts->git_dir)
 		repo_config = mkpathdup("%s/config", opts->git_dir);
-	else if (have_git_dir())
-		repo_config = git_pathdup("config");
 	else
 		repo_config = NULL;
 
@@ -1613,6 +1611,8 @@ static void git_config_raw(config_fn_t fn, void *data)
 	struct config_options opts = {0};
 
 	opts.respect_includes = 1;
+	if (have_git_dir())
+		opts.git_dir = get_git_common_dir();
 	if (git_config_with_options(fn, data, NULL, &opts) < 0)
 		/*
 		 * git_config_with_options() normally returns only
Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#58 lore raw

Re: [PATCH v2 04/32] config: don't implicitly use gitdir

Quoted 40 lines · Brandon Williams

Commit 2185fde56 (config: handle conditional include when $GIT_DIR is not set up) added a 'git_dir' field to the config_options struct. Let's use this option field explicitly all the time instead of occasionally falling back to calling 'git_pathdup("config")' to get the path to the local repository configuration. This allows 'do_git_config_sequence()' to not implicitly rely on global repository state.

---
 builtin/config.c | 2 ++
 config.c         | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)
builtin/config.c +2 -0
index 753c40a5c..90f49a6ee 100644
@@ -539,6 +539,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		config_options.respect_includes = !given_config_source.file;
 	else
 		config_options.respect_includes = respect_includes_opt;
+	if (have_git_dir())
+		config_options.git_dir = get_git_common_dir();
 
 	if (end_null) {
 		term = '\0';
config.c +0 -2
index 2390f98e3..ff09b27b8 100644
@@ -1548,8 +1548,6 @@ static int do_git_config_sequence(const struct config_options *opts,
 
 	if (opts->git_dir)
 		repo_config = mkpathdup("%s/config", opts->git_dir);
-	else if (have_git_dir())
-		repo_config = git_pathdup("config");
 	else
 		repo_config = NULL;
 

config.c static void git_config_raw(config_fn_t fn, void *data) · line 1618

Quoted 8 lines
config.c +2 -0
@@ -1613,6 +1611,8 @@ static void git_config_raw(config_fn_t fn, void *data)
 	struct config_options opts = {0};
 
 	opts.respect_includes = 1;
+	if (have_git_dir())
+		opts.git_dir = get_git_common_dir();
 	if (git_config_with_options(fn, data, NULL, &opts) < 0)
 		/*
 		 * git_config_with_options() normally returns only

I missed a case in 'include_by_gitdir'.

Given that these config changes touch a lot of files (making a config.h files and including it everyone) I think I'll break this into its own series so that it can get reviewed and merged in faster. This is especially due to this series breaking pu right now.

Signature · 1 line
Brandon Williams
#59 lore raw

PATCH v2 5/32 setup: don't perform lazy initialization of repository state

In reply to Brandon Williams

Under some circumstances (bogus GIT_DIR value or the discovered gitdir is '.git') 'setup_git_directory()' won't initialize key repository state. This leads to inconsistent state after running the setup code. To account for this inconsistent state, lazy initialization is done once a caller asks for the repository's gitdir or some other piece of repository state. This is confusing and can be error prone.

Instead let's tighten the expected outcome of 'setup_git_directory()' and ensure that it initializes repository state in all cases that would have been handled by lazy initialization.

This also lets us drop the requirement to have 'have_git_dir()' check if the environment variable GIT_DIR was set as that will be handled by the end of the setup code.

---
 cache.h       |  2 ++
 environment.c | 17 ++++++++---------
 setup.c       | 14 ++++++++++++++
 3 files changed, 24 insertions(+), 9 deletions(-)
cache.h +2 -0
index 272f8e021..d41aab82f 100644
@@ -462,6 +462,8 @@ static inline enum object_type object_type(unsigned int mode)
  */
 extern const char * const local_repo_env[];
 
+extern void setup_git_env(void);
+
 /*
  * Returns true iff we have a configured git repository (either via
  * setup_git_directory, or in the environment via $GIT_DIR).
environment.c +8 -9
index d40b21fb7..a73b08f5d 100644
@@ -160,7 +160,7 @@ static char *git_path_from_env(const char *envvar, const char *git_dir,
 	return xstrdup(value);
 }
 
-static void setup_git_env(void)
+void setup_git_env(void)
 {
 	struct strbuf sb = STRBUF_INIT;
 	const char *gitfile;
@@ -205,28 +205,27 @@ int is_bare_repository(void)
 int have_git_dir(void)
 {
 	return startup_info->have_repository
-		|| git_dir
-		|| getenv(GIT_DIR_ENVIRONMENT);
+		|| git_dir;
 }
 
 const char *get_git_dir(void)
 {
 	if (!git_dir)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_dir;
 }
 
 const char *get_git_common_dir(void)
 {
 	if (!git_dir)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_common_dir;
 }
 
 const char *get_git_namespace(void)
 {
 	if (!namespace)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return namespace;
 }
 
@@ -276,7 +275,7 @@ const char *get_git_work_tree(void)
 char *get_object_directory(void)
 {
 	if (!git_object_dir)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_object_dir;
 }
 
@@ -316,14 +315,14 @@ int odb_pack_keep(const char *name)
 char *get_index_file(void)
 {
 	if (!git_index_file)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_index_file;
 }
 
 char *get_graft_file(void)
 {
 	if (!git_graft_file)
-		setup_git_env();
+		BUG("git environment hasn't been setup");
 	return git_graft_file;
 }
 
setup.c +14 -0
index e99a82cbe..b2e05145c 100644
@@ -1063,6 +1063,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	startup_info->have_repository = !nongit_ok || !*nongit_ok;
 	startup_info->prefix = prefix;
 
+	/*
+	 * Not all paths through the setup code will call 'set_git_dir()' (which
+	 * directly sets up the environment) so in order to guarantee that the
+	 * environment is in a consistent state after setup, explicitly setup
+	 * the environment if we have a repository.
+	 *
+	 * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
+	 * code paths so we also need to explicitly setup the environment if
+	 * the user has set GIT_DIR.  It may be beneficial to disallow bogus
+	 * GIT_DIR values at some point in the future.
+	 */
+	if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT))
+		setup_git_env();
+
 	strbuf_release(&dir);
 	strbuf_release(&gitdir);
 
Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#60 lore raw

PATCH v2 6/32 environment: remove namespace_len variable

In reply to Brandon Williams

Use 'skip_prefix' instead of 'starts_with' so that we can drop the need to keep around 'namespace_len'.

---
 environment.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
environment.c +4 -5
index a73b08f5d..e035f6372 100644
@@ -98,7 +98,6 @@ char *git_work_tree_cfg;
 static char *work_tree;
 
 static const char *namespace;
-static size_t namespace_len;
 
 static const char *super_prefix;
 
@@ -190,7 +189,6 @@ void setup_git_env(void)
 	git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
 							  : "refs/replace/");
 	namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
-	namespace_len = strlen(namespace);
 	shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
 	if (shallow_file)
 		set_alternate_shallow_file(shallow_file, 0);
@@ -231,9 +229,10 @@ const char *get_git_namespace(void)
 
 const char *strip_namespace(const char *namespaced_ref)
 {
-	if (!starts_with(namespaced_ref, get_git_namespace()))
-		return NULL;
-	return namespaced_ref + namespace_len;
+	const char *out;
+	if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
+		return out;
+	return NULL;
 }
 
 const char *get_super_prefix(void)
Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#61 lore raw

PATCH v2 7/32 repository: introduce the repository object

In reply to Brandon Williams

Introduce the repository object 'struct repository' which can be used to hold all state pertaining to a git repository.

Some of the benefits of object-ifying a repository are:

1. Make the code base more readable and easier to reason about.

2. Allow for working on multiple repositories, specifically submodules, within the same process. Currently the process for working on a submodule involves setting up an argv_array of options for a particular command and then launching a child process to execute the command in the context of the submodule. This is clunky and can require lots of little hacks in order to ensure correctness. Ideally it would be nice to simply pass a repository and an options struct to a command.

3. Eliminating reliance on global state will make it easier to enable the use of threading to improve performance.

---
 Makefile      |   1 +
 cache.h       |   1 +
 environment.c |   2 +-
 repository.c  | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 repository.h  |  25 +++++++++
 5 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 repository.c
 create mode 100644 repository.h
Makefile +1 -0
index 2ed6db728..ac82d93f6 100644
@@ -821,6 +821,7 @@ LIB_OBJS += refs/ref-cache.o
 LIB_OBJS += ref-filter.o
 LIB_OBJS += remote.o
 LIB_OBJS += replace_object.o
+LIB_OBJS += repository.o
 LIB_OBJS += rerere.o
 LIB_OBJS += resolve-undo.o
 LIB_OBJS += revision.o
cache.h +1 -0
index d41aab82f..c0b4c8d83 100644
@@ -483,6 +483,7 @@ extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
 extern int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
 extern int get_common_dir(struct strbuf *sb, const char *gitdir);
+extern char *expand_namespace(const char *raw_namespace);
 extern const char *get_git_namespace(void);
 extern const char *strip_namespace(const char *namespaced_ref);
 extern const char *get_super_prefix(void);
environment.c +1 -1
index e035f6372..77900b31a 100644
@@ -127,7 +127,7 @@ const char * const local_repo_env[] = {
 	NULL
 };
 
-static char *expand_namespace(const char *raw_namespace)
+char *expand_namespace(const char *raw_namespace)
 {
 	struct strbuf buf = STRBUF_INIT;
 	struct strbuf **components, **c;
repository.c new file +171 -0

171 lines not on this page · raw message

repository.h new file +25 -0
new file mode 100644
index 000000000..174ab0f2d
@@ -0,0 +1,25 @@
+#ifndef REPOSITORY_H
+#define REPOSITORY_H
+
+struct repository {
+	/* Environment */
+	char *gitdir;
+	char *commondir;
+	char *objectdir;
+	char *index_file;
+	char *graft_file;
+	char *namespace;
+
+	/* Configurations */
+	unsigned ignore_env:1;
+	/* Indicates if a repository has a different 'commondir' from 'gitdir' */
+	unsigned different_commondir:1;
+};
+
+extern struct repository *the_repository;
+
+extern void repo_set_gitdir(struct repository *repo, const char *path);
+extern int repo_init(struct repository *repo, const char *path);
+extern void repo_clear(struct repository *repo);
+
+#endif /* REPOSITORY_H */
Signature · 1 line
2.13.1.508.gb3defc5cc-goog
#62 lore raw

cover Add a freez() wrapper

Quoted 8 lines · Brandon Williams

Introduce the repository object 'struct repository' which can be used to hold all state pertaining to a git repository.
[...]

repository.c new file +5 -0
+static void repo_clear_env(struct repository *repo)
+{
+       free(repo->gitdir);
+       repo->gitdir = NULL;
+       free(repo->commondir);

repository.c line 164

Quoted 10 lines
repository.c new file +10 -0
+       repo->commondir = NULL;
+       free(repo->objectdir);
+       repo->objectdir = NULL;
+       free(repo->index_file);
+       repo->index_file = NULL;
+       free(repo->graft_file);
+       repo->graft_file = NULL;
+       free(repo->namespace);
+       repo->namespace = NULL;
+}

When seeing this I wondered if it couldn't be less noisy as:

Review

Quoted 6 lines
(quoted diff) +6 -0
+       freez(repo->gitdir);
+       freez(repo->commondir);
+       freez(repo->objectdir);
+       freez(repo->index_file);
+       freez(repo->graft_file);
+       freez(repo->namespace);

It's not a problem with your patch, but something I thought would be nice to have in general, so here's a patch series to implement that.

Ævar Arnfjörð Bjarmason (2):
git-compat-util: add a freez() wrapper around free(x); x = NULL *.[ch] refactoring: make use of the freez() wrapper

 alias.c                  |  6 ++----
 apply.c                  |  3 +--
 attr.c                   |  6 ++----
 blame.c                  |  3 +--
 branch.c                 |  3 +--
 builtin/am.c             | 18 +++++-------------
 builtin/clean.c          |  6 ++----
 builtin/config.c         |  6 ++----
 builtin/index-pack.c     |  6 ++----
 builtin/pack-objects.c   | 12 ++++--------
 builtin/unpack-objects.c |  3 +--
 builtin/worktree.c       |  6 ++----
 commit-slab.h            |  3 +--
 commit.c                 |  3 +--
 config.c                 |  3 +--
 credential.c             |  9 +++------
 diff-lib.c               |  3 +--
 diff.c                   |  6 ++----
 diffcore-rename.c        |  6 ++----
 dir.c                    |  9 +++------
 fast-import.c            |  6 ++----
 git-compat-util.h        |  1 +
 gpg-interface.c          | 15 +++++----------
 grep.c                   | 12 ++++--------
 help.c                   |  3 +--
 http-push.c              | 24 ++++++++----------------
 http.c                   | 15 +++++----------
 imap-send.c              |  3 +--
 line-log.c               |  6 ++----
 ll-merge.c               |  3 +--
 mailinfo.c               |  3 +--
 object.c                 |  3 +--
 pathspec.c               |  3 +--
 prio-queue.c             |  3 +--
 read-cache.c             |  6 ++----
 ref-filter.c             |  3 +--
 refs/files-backend.c     |  3 +--
 refs/ref-cache.c         |  3 +--
 remote-testsvn.c         |  3 +--
 rerere.c                 |  3 +--
 sequencer.c              |  3 +--
 sha1-array.c             |  3 +--
 sha1_file.c              |  3 +--
 split-index.c            |  3 +--
 transport-helper.c       | 27 +++++++++------------------
 transport.c              |  3 +--
 tree-diff.c              |  6 ++----
 tree-walk.c              |  3 +--
 tree.c                   |  3 +--
 49 files changed, 98 insertions(+), 197 deletions(-)
Signature · 1 line
2.13.0.506.g27d5fe0cd
#63 lore raw

PATCH 1/2 git-compat-util: add a freez() wrapper around free(x); x = NULL

Add a freez() wrapper for the common pattern of freeing a pointer and assigning NULL to it right afterwards.

The implementation is the same as the (currently unused) XDL_PTRFREE macro in xdiff/xmacros.h added in commit 3443546f6e ("Use a *real* built-in diff generator", 2006-03-24).

---
 git-compat-util.h | 1 +
 1 file changed, 1 insertion(+)
git-compat-util.h +1 -0
index 4b7dcf21ad..21e3710755 100644
@@ -787,6 +787,7 @@ extern char *xstrdup(const char *str);
 extern void *xmalloc(size_t size);
 extern void *xmallocz(size_t size);
 extern void *xmallocz_gently(size_t size);
+#define freez(p) do { if (p) { free(p); (p) = NULL; } } while (0)
 extern void *xmemdupz(const void *data, size_t len);
 extern char *xstrndup(const char *str, size_t len);
 extern void *xrealloc(void *ptr, size_t size);
Signature · 1 line
2.13.0.506.g27d5fe0cd
#64 lore raw

Re: [PATCH 1/2] git-compat-util: add a freez() wrapper around free(x); x = NULL

git-compat-util.h extern char *xstrdup(const char *str); · line 790

Quoted 7 lines · Ævar Arnfjörð Bjarmason
git-compat-util.h +1 -0
@@ -787,6 +787,7 @@ extern char *xstrdup(const char *str);
 extern void *xmalloc(size_t size);
 extern void *xmallocz(size_t size);
 extern void *xmallocz_gently(size_t size);
+#define freez(p) do { if (p) { free(p); (p) = NULL; } } while (0)

I think we already rely on free(NULL) working, see
http://public-inbox.org/git/alpine.DEB.2.20.1608301948310.129229@virtualbox/
for example, so this could just be:

#define freez(p) do { free(p); (p) = NULL; } while (0)

and yeah FREEZ() would be even better than freez().

#65 lore raw

PATCH 2/2 *.[ch] refactoring: make use of the freez() wrapper

In reply to Ævar Arnfjörð Bjarmason

Replace occurrences of free(p); p = NULL with freez(p). This introduces no functional changes, but cuts the number of lines spent on this cleanup in half.

---
 alias.c                  |  6 ++----
 apply.c                  |  3 +--
 attr.c                   |  6 ++----
 blame.c                  |  3 +--
 branch.c                 |  3 +--
 builtin/am.c             | 18 +++++-------------
 builtin/clean.c          |  6 ++----
 builtin/config.c         |  6 ++----
 builtin/index-pack.c     |  6 ++----
 builtin/pack-objects.c   | 12 ++++--------
 builtin/unpack-objects.c |  3 +--
 builtin/worktree.c       |  6 ++----
 commit-slab.h            |  3 +--
 commit.c                 |  3 +--
 config.c                 |  3 +--
 credential.c             |  9 +++------
 diff-lib.c               |  3 +--
 diff.c                   |  6 ++----
 diffcore-rename.c        |  6 ++----
 dir.c                    |  9 +++------
 fast-import.c            |  6 ++----
 gpg-interface.c          | 15 +++++----------
 grep.c                   | 12 ++++--------
 help.c                   |  3 +--
 http-push.c              | 24 ++++++++----------------
 http.c                   | 15 +++++----------
 imap-send.c              |  3 +--
 line-log.c               |  6 ++----
 ll-merge.c               |  3 +--
 mailinfo.c               |  3 +--
 object.c                 |  3 +--
 pathspec.c               |  3 +--
 prio-queue.c             |  3 +--
 read-cache.c             |  6 ++----
 ref-filter.c             |  3 +--
 refs/files-backend.c     |  3 +--
 refs/ref-cache.c         |  3 +--
 remote-testsvn.c         |  3 +--
 rerere.c                 |  3 +--
 sequencer.c              |  3 +--
 sha1-array.c             |  3 +--
 sha1_file.c              |  3 +--
 split-index.c            |  3 +--
 transport-helper.c       | 27 +++++++++------------------
 transport.c              |  3 +--
 tree-diff.c              |  6 ++----
 tree-walk.c              |  3 +--
 tree.c                   |  3 +--
 48 files changed, 97 insertions(+), 197 deletions(-)
alias.c +2 -4
index 3b90397a99..99cd547b6b 100644
@@ -47,8 +47,7 @@ int split_cmdline(char *cmdline, const char ***argv)
 				src++;
 				c = cmdline[src];
 				if (!c) {
-					free(*argv);
-					*argv = NULL;
+					freez(*argv);
 					return -SPLIT_CMDLINE_BAD_ENDING;
 				}
 			}
@@ -60,8 +59,7 @@ int split_cmdline(char *cmdline, const char ***argv)
 	cmdline[dst] = 0;
 
 	if (quoted) {
-		free(*argv);
-		*argv = NULL;
+		freez(*argv);
 		return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
 	}
 
apply.c +1 -2
index c49cef0637..f987e1e961 100644
@@ -3705,8 +3705,7 @@ static int check_preimage(struct apply_state *state,
  is_new:
 	patch->is_new = 1;
 	patch->is_delete = 0;
-	free(patch->old_name);
-	patch->old_name = NULL;
+	freez(patch->old_name);
 	return 0;
 }
 
attr.c +2 -4
index 7e2134471c..cd92a02e98 100644
@@ -638,13 +638,11 @@ void attr_check_reset(struct attr_check *check)
 
 void attr_check_clear(struct attr_check *check)
 {
-	free(check->items);
-	check->items = NULL;
+	freez(check->items);
 	check->alloc = 0;
 	check->nr = 0;
 
-	free(check->all_attrs);
-	check->all_attrs = NULL;
+	freez(check->all_attrs);
 	check->all_attrs_nr = 0;
 
 	drop_attr_stack(&check->stack);
blame.c +1 -2
index 843c845cba..dc1688889c 100644
@@ -314,8 +314,7 @@ static void fill_origin_blob(struct diff_options *opt,
 static void drop_origin_blob(struct blame_origin *o)
 {
 	if (o->file.ptr) {
-		free(o->file.ptr);
-		o->file.ptr = NULL;
+		freez(o->file.ptr);
 	}
 }
 
branch.c +1 -2
index 985316eb76..ef2ae0b49e 100644
@@ -24,8 +24,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 		} else {
 			free(tracking->spec.src);
 			if (tracking->src) {
-				free(tracking->src);
-				tracking->src = NULL;
+				freez(tracking->src);
 			}
 		}
 		tracking->spec.src = NULL;
builtin/am.c +5 -13
index 5ee146bfb3..f5441cc990 100644
@@ -483,8 +483,7 @@ static int run_applypatch_msg_hook(struct am_state *state)
 	ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
 
 	if (!ret) {
-		free(state->msg);
-		state->msg = NULL;
+		freez(state->msg);
 		if (read_commit_msg(state) < 0)
 			die(_("'%s' was deleted by the applypatch-msg hook"),
 				am_path(state, "final-commit"));
@@ -1073,17 +1072,10 @@ static void am_next(struct am_state *state)
 {
 	struct object_id head;
 
-	free(state->author_name);
-	state->author_name = NULL;
-
-	free(state->author_email);
-	state->author_email = NULL;
-
-	free(state->author_date);
-	state->author_date = NULL;
-
-	free(state->msg);
-	state->msg = NULL;
+	freez(state->author_name);
+	freez(state->author_email);
+	freez(state->author_date);
+	freez(state->msg);
 	state->msg_len = 0;
 
 	unlink(am_path(state, "author-script"));
builtin/clean.c +2 -4
index 142bf668cf..cdf7b0911e 100644
@@ -837,8 +837,7 @@ static void interactive_main_loop(void)
 			int ret;
 			ret = menus[*chosen].fn();
 			if (ret != MENU_RETURN_NO_LOOP) {
-				free(chosen);
-				chosen = NULL;
+				freez(chosen);
 				if (!del_list.nr) {
 					clean_print_color(CLEAN_COLOR_ERROR);
 					printf_ln(_("No more files to clean, exiting."));
@@ -851,8 +850,7 @@ static void interactive_main_loop(void)
 			quit_cmd();
 		}
 
-		free(chosen);
-		chosen = NULL;
+		freez(chosen);
 		break;
 	}
 }
builtin/config.c +2 -4
index 7f6c25d4d9..e9adac9622 100644
@@ -214,8 +214,7 @@ static int get_value(const char *key_, const char *regex_)
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			error("invalid key pattern: %s", key_);
-			free(key_regexp);
-			key_regexp = NULL;
+			freez(key_regexp);
 			ret = CONFIG_INVALID_PATTERN;
 			goto free_strings;
 		}
@@ -235,8 +234,7 @@ static int get_value(const char *key_, const char *regex_)
 		regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(regexp, regex_, REG_EXTENDED)) {
 			error("invalid pattern: %s", regex_);
-			free(regexp);
-			regexp = NULL;
+			freez(regexp);
 			ret = CONFIG_INVALID_PATTERN;
 			goto free_strings;
 		}
builtin/index-pack.c +2 -4
index 04b9dcaf0f..4165e99484 100644
@@ -388,8 +388,7 @@ static struct base_data *alloc_base_data(void)
 static void free_base_data(struct base_data *c)
 {
 	if (c->data) {
-		free(c->data);
-		c->data = NULL;
+		freez(c->data);
 		get_thread_data()->base_cache_used -= c->size;
 	}
 }
@@ -605,8 +604,7 @@ static void *unpack_data(struct object_entry *obj,
 	git_inflate_end(&stream);
 	free(inbuf);
 	if (consume) {
-		free(data);
-		data = NULL;
+		freez(data);
 	}
 	return data;
 }
builtin/pack-objects.c +4 -8
index f672225def..fb43c1b101 100644
@@ -264,8 +264,7 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 		 * make sure no cached delta data remains from a
 		 * previous attempt before a pack split occurred.
 		 */
-		free(entry->delta_data);
-		entry->delta_data = NULL;
+		freez(entry->delta_data);
 		entry->z_delta_size = 0;
 	} else if (entry->delta_data) {
 		size = entry->delta_size;
@@ -1375,12 +1374,10 @@ static void cleanup_preferred_base(void)
 		if (!pbase_tree_cache[i])
 			continue;
 		free(pbase_tree_cache[i]->tree_data);
-		free(pbase_tree_cache[i]);
-		pbase_tree_cache[i] = NULL;
+		freez(pbase_tree_cache[i]);
 	}
 
-	free(done_pbase_paths);
-	done_pbase_paths = NULL;
+	freez(done_pbase_paths);
 	done_pbase_paths_num = done_pbase_paths_alloc = 0;
 }
 
@@ -1970,8 +1967,7 @@ static unsigned long free_unpacked(struct unpacked *n)
 	n->index = NULL;
 	if (n->data) {
 		freed_mem += n->entry->size;
-		free(n->data);
-		n->data = NULL;
+		freez(n->data);
 	}
 	n->entry = NULL;
 	n->depth = 0;
builtin/unpack-objects.c +1 -2
index 8bc9997767..236ee6ebfc 100644
@@ -112,8 +112,7 @@ static void *get_data(unsigned long size)
 			break;
 		if (ret != Z_OK) {
 			error("inflate returned %d", ret);
-			free(buf);
-			buf = NULL;
+			freez(buf);
 			if (!recover)
 				exit(1);
 			has_errors = 1;
builtin/worktree.c +2 -4
index 793306ea51..dffed4666e 100644
@@ -299,10 +299,8 @@ static int add_worktree(const char *path, const char *refname,
 	}
 
 	is_junk = 0;
-	free(junk_work_tree);
-	free(junk_git_dir);
-	junk_work_tree = NULL;
-	junk_git_dir = NULL;
+	freez(junk_work_tree);
+	freez(junk_git_dir);
 
 done:
 	if (ret || !opts->keep_locked) {
commit-slab.h +1 -2
index 42d16dcded..ffa2a30f53 100644
@@ -82,8 +82,7 @@ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s)		\
 	for (i = 0; i < s->slab_count; i++)				\
 		free(s->slab[i]);					\
 	s->slab_count = 0;						\
-	free(s->slab);							\
-	s->slab = NULL;							\
+	freez(s->slab);							\
 }									\
 									\
 static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s,	\
commit.c +1 -2
index 713f09feb0..851f1f3516 100644
@@ -287,8 +287,7 @@ void free_commit_buffer(struct commit *commit)
 {
 	struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
 	if (v) {
-		free(v->buffer);
-		v->buffer = NULL;
+		freez(v->buffer);
 		v->size = 0;
 	}
 }
config.c +1 -2
index 146cb3452a..9ab9260e0f 100644
@@ -395,8 +395,7 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *basele
 
 out_free_ret_1:
 	if (store_key) {
-		free(*store_key);
-		*store_key = NULL;
+		freez(*store_key);
 	}
 	return -CONFIG_INVALID_KEY;
 }
credential.c +3 -6
index aa996669fc..120fb996cb 100644
@@ -93,8 +93,7 @@ static void credential_apply_config(struct credential *c)
 	c->configured = 1;
 
 	if (!c->use_http_path && proto_is_http(c->protocol)) {
-		free(c->path);
-		c->path = NULL;
+		freez(c->path);
 	}
 }
 
@@ -314,10 +313,8 @@ void credential_reject(struct credential *c)
 	for (i = 0; i < c->helpers.nr; i++)
 		credential_do(c, c->helpers.items[i].string, "erase");
 
-	free(c->username);
-	c->username = NULL;
-	free(c->password);
-	c->password = NULL;
+	freez(c->username);
+	freez(c->password);
 	c->approved = 0;
 }
 
diff-lib.c +1 -2
index 2982bf055a..d275c2469f 100644
@@ -179,8 +179,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 				free(dpath);
 				continue;
 			}
-			free(dpath);
-			dpath = NULL;
+			freez(dpath);
 
 			/*
 			 * Show the diff for the 'ce' if we found the one
diff.c +2 -4
index 5275c4b780..8ae4a6325c 100644
@@ -1218,8 +1218,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
 			regfree(ecbdata->diff_words->word_regex);
 			free(ecbdata->diff_words->word_regex);
 		}
-		free(ecbdata->diff_words);
-		ecbdata->diff_words = NULL;
+		freez(ecbdata->diff_words);
 	}
 }
 
@@ -2951,8 +2950,7 @@ void diff_free_filespec_blob(struct diff_filespec *s)
 void diff_free_filespec_data(struct diff_filespec *s)
 {
 	diff_free_filespec_blob(s);
-	free(s->cnt_data);
-	s->cnt_data = NULL;
+	freez(s->cnt_data);
 }
 
 static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
diffcore-rename.c +2 -4
index f7444c86bd..99aab63c85 100644
@@ -667,11 +667,9 @@ void diffcore_rename(struct diff_options *options)
 	for (i = 0; i < rename_dst_nr; i++)
 		free_filespec(rename_dst[i].two);
 
-	free(rename_dst);
-	rename_dst = NULL;
+	freez(rename_dst);
 	rename_dst_nr = rename_dst_alloc = 0;
-	free(rename_src);
-	rename_src = NULL;
+	freez(rename_src);
 	rename_src_nr = rename_src_alloc = 0;
 	return;
 }
dir.c +3 -6
index 9efcf1eab6..441f98095a 100644
@@ -2117,8 +2117,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
 		for (i = j = 0; j < dir->nr; j++) {
 			if (i &&
 			    check_dir_entry_contains(dir->entries[i - 1], dir->entries[j])) {
-				free(dir->entries[j]);
-				dir->entries[j] = NULL;
+				freez(dir->entries[j]);
 			} else {
 				dir->entries[i++] = dir->entries[j];
 			}
@@ -2144,8 +2143,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
 		     dir->untracked->dir_invalidated))
 			istate->cache_changed |= UNTRACKED_CHANGED;
 		if (dir->untracked != istate->untracked) {
-			free(dir->untracked);
-			dir->untracked = NULL;
+			freez(dir->untracked);
 		}
 	}
 	return dir->nr;
@@ -2488,8 +2486,7 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
 	strbuf_addbuf(out, &untracked->ident);
 
 	strbuf_add(out, ouc, ouc_size(len));
-	free(ouc);
-	ouc = NULL;
+	freez(ouc);
 
 	if (!untracked->root) {
 		varint_len = encode_varint(0, varbuf);
fast-import.c +2 -4
index e69d219682..9d8f91eebd 100644
@@ -1063,8 +1063,7 @@ static void end_packfile(void)
 		close(pack_data->pack_fd);
 		unlink_or_warn(pack_data->pack_name);
 	}
-	free(pack_data);
-	pack_data = NULL;
+	freez(pack_data);
 	running = 0;
 
 	/* We can't carry a delta across packfiles. */
@@ -1149,8 +1148,7 @@ static int store_object(
 
 		/* We cannot carry a delta into the new pack. */
 		if (delta) {
-			free(delta);
-			delta = NULL;
+			freez(delta);
 
 			git_deflate_init(&s, pack_compression_level);
 			s.next_in = (void *)dat->buf;
gpg-interface.c +5 -10
index e44cc27da1..0baca17293 100644
@@ -13,16 +13,11 @@ static const char *gpg_program = "gpg";
 
 void signature_check_clear(struct signature_check *sigc)
 {
-	free(sigc->payload);
-	free(sigc->gpg_output);
-	free(sigc->gpg_status);
-	free(sigc->signer);
-	free(sigc->key);
-	sigc->payload = NULL;
-	sigc->gpg_output = NULL;
-	sigc->gpg_status = NULL;
-	sigc->signer = NULL;
-	sigc->key = NULL;
+	freez(sigc->payload);
+	freez(sigc->gpg_output);
+	freez(sigc->gpg_status);
+	freez(sigc->signer);
+	freez(sigc->key);
 }
 
 static struct {
grep.c +4 -8
index d03d424e5c..c74c5a8270 100644
@@ -1763,12 +1763,9 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
 
 void grep_source_clear(struct grep_source *gs)
 {
-	free(gs->name);
-	gs->name = NULL;
-	free(gs->path);
-	gs->path = NULL;
-	free(gs->identifier);
-	gs->identifier = NULL;
+	freez(gs->name);
+	freez(gs->path);
+	freez(gs->identifier);
 	grep_source_clear_data(gs);
 }
 
@@ -1778,8 +1775,7 @@ void grep_source_clear_data(struct grep_source *gs)
 	case GREP_SOURCE_FILE:
 	case GREP_SOURCE_SHA1:
 	case GREP_SOURCE_SUBMODULE:
-		free(gs->buf);
-		gs->buf = NULL;
+		freez(gs->buf);
 		gs->size = 0;
 		break;
 	case GREP_SOURCE_BUF:
help.c +1 -2
index db7f3d79a0..6783bb9b4b 100644
@@ -267,9 +267,8 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
 
 	for (i = 0; i < old->cnt; i++)
 		cmds->names[cmds->cnt++] = old->names[i];
-	free(old->names);
+	freez(old->names);
 	old->cnt = 0;
-	old->names = NULL;
 }
 
 /* An empirically derived magic number */
http-push.c +8 -16
index 67c4d4b472..89ae33464f 100644
@@ -291,8 +291,7 @@ static void start_mkcol(struct transfer_request *request)
 		request->state = RUN_MKCOL;
 	} else {
 		request->state = ABORTED;
-		free(request->url);
-		request->url = NULL;
+		freez(request->url);
 	}
 }
 #endif
@@ -409,8 +408,7 @@ static void start_put(struct transfer_request *request)
 		request->state = RUN_PUT;
 	} else {
 		request->state = ABORTED;
-		free(request->url);
-		request->url = NULL;
+		freez(request->url);
 	}
 }
 
@@ -432,8 +430,7 @@ static void start_move(struct transfer_request *request)
 		request->state = RUN_MOVE;
 	} else {
 		request->state = ABORTED;
-		free(request->url);
-		request->url = NULL;
+		freez(request->url);
 	}
 }
 
@@ -526,8 +523,7 @@ static void finish_request(struct transfer_request *request)
 
 	/* URL is reused for MOVE after PUT */
 	if (request->state != RUN_PUT) {
-		free(request->url);
-		request->url = NULL;
+		freez(request->url);
 	}
 
 	if (request->state == RUN_MKCOL) {
@@ -803,8 +799,7 @@ xml_start_tag(void *userData, const char *name, const char **atts)
 	}
 	xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
 
-	free(ctx->cdata);
-	ctx->cdata = NULL;
+	freez(ctx->cdata);
 
 	ctx->userFunc(ctx, 0);
 }
@@ -932,8 +927,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
 		free(lock->token);
 		free(lock->owner);
 		free(url);
-		free(lock);
-		lock = NULL;
+		freez(lock);
 	} else {
 		lock->url = url;
 		lock->start_time = time(NULL);
@@ -1105,8 +1099,7 @@ static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
 			ls->dentry_flags |= IS_DIR;
 		}
 	} else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
-		free(ls->dentry_name);
-		ls->dentry_name = NULL;
+		freez(ls->dentry_name);
 		ls->dentry_flags = 0;
 	}
 }
@@ -1547,8 +1540,7 @@ static void fetch_symref(const char *path, char **symref, struct object_id *oid)
 		    curl_errorstr);
 	free(url);
 
-	free(*symref);
-	*symref = NULL;
+	freez(*symref);
 	oidclr(oid);
 
 	if (buffer.len == 0)
http.c +5 -10
index d2e11ec6f0..28eeb0d5ba 100644
@@ -1026,8 +1026,7 @@ void http_cleanup(void)
 
 	if (proxy_auth.password) {
 		memset(proxy_auth.password, 0, strlen(proxy_auth.password));
-		free(proxy_auth.password);
-		proxy_auth.password = NULL;
+		freez(proxy_auth.password);
 	}
 
 	free((void *)curl_proxyuserpwd);
@@ -1038,13 +1037,11 @@ void http_cleanup(void)
 
 	if (cert_auth.password != NULL) {
 		memset(cert_auth.password, 0, strlen(cert_auth.password));
-		free(cert_auth.password);
-		cert_auth.password = NULL;
+		freez(cert_auth.password);
 	}
 	ssl_cert_password_required = 0;
 
-	free(cached_accept_language);
-	cached_accept_language = NULL;
+	freez(cached_accept_language);
 }
 
 struct active_request_slot *get_active_slot(void)
@@ -1896,8 +1893,7 @@ static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
 
 	if (http_get_file(url, tmp, NULL) != HTTP_OK) {
 		error("Unable to get pack index %s", url);
-		free(tmp);
-		tmp = NULL;
+		freez(tmp);
 	}
 
 	free(url);
@@ -2328,8 +2324,7 @@ void release_http_object_request(struct http_object_request *freq)
 		freq->localfile = -1;
 	}
 	if (freq->url != NULL) {
-		free(freq->url);
-		freq->url = NULL;
+		freez(freq->url);
 	}
 	if (freq->slot != NULL) {
 		freq->slot->callback_func = NULL;
imap-send.c +1 -2
index 857591660f..a96facf530 100644
@@ -776,8 +776,7 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
 			       offsetof(struct imap_cmd, next));
 			if (cmdp->cb.data) {
 				n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
-				free(cmdp->cb.data);
-				cmdp->cb.data = NULL;
+				freez(cmdp->cb.data);
 				if (n != (int)cmdp->cb.dlen)
 					return RESP_BAD;
 			} else if (cmdp->cb.cont) {
line-log.c +2 -4
index b9087814b8..bc7efb91da 100644
@@ -34,9 +34,8 @@ void range_set_init(struct range_set *rs, size_t prealloc)
 
 void range_set_release(struct range_set *rs)
 {
-	free(rs->ranges);
+	freez(rs->ranges);
 	rs->alloc = rs->nr = 0;
-	rs->ranges = NULL;
 }
 
 /* dst must be uninitialized! */
@@ -610,8 +609,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
 		line_log_data_insert(&ranges, full_name, begin, end);
 
 		free_filespec(spec);
-		free(ends);
-		ends = NULL;
+		freez(ends);
 	}
 
 	for (p = ranges; p; p = p->next)
ll-merge.c +1 -2
index ac0d4a5d78..279ac6319c 100644
@@ -209,8 +209,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
 	result->size = st.st_size;
 	result->ptr = xmallocz(result->size);
 	if (read_in_full(fd, result->ptr, result->size) != result->size) {
-		free(result->ptr);
-		result->ptr = NULL;
+		freez(result->ptr);
 		result->size = 0;
 	}
  close_bad:
mailinfo.c +1 -2
index f92cb9f729..b26689b56d 100644
@@ -919,8 +919,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
 		/* we hit an end boundary */
 		/* pop the current boundary off the stack */
 		strbuf_release(*(mi->content_top));
-		free(*(mi->content_top));
-		*(mi->content_top) = NULL;
+		freez(*(mi->content_top));
 
 		/* technically won't happen as is_multipart_boundary()
 		   will fail first.  But just in case..
object.c +1 -2
index 06ba3a11d8..2883d46a0d 100644
@@ -377,8 +377,7 @@ void object_array_clear(struct object_array *array)
 	int i;
 	for (i = 0; i < array->nr; i++)
 		object_array_release_entry(&array->objects[i]);
-	free(array->objects);
-	array->objects = NULL;
+	freez(array->objects);
 	array->nr = array->alloc = 0;
 }
 
pathspec.c +1 -2
index 828405021f..1f8ff7f4a9 100644
@@ -662,7 +662,6 @@ void clear_pathspec(struct pathspec *pathspec)
 			attr_check_free(pathspec->items[i].attr_check);
 	}
 
-	free(pathspec->items);
-	pathspec->items = NULL;
+	freez(pathspec->items);
 	pathspec->nr = 0;
 }
prio-queue.c +1 -2
index fc3860fdcb..59211eb2e8 100644
@@ -27,10 +27,9 @@ void prio_queue_reverse(struct prio_queue *queue)
 
 void clear_prio_queue(struct prio_queue *queue)
 {
-	free(queue->array);
+	freez(queue->array);
 	queue->nr = 0;
 	queue->alloc = 0;
-	queue->array = NULL;
 	queue->insertion_ctr = 0;
 }
 
read-cache.c +2 -4
index bc156a133e..8434ef5577 100644
@@ -1888,8 +1888,7 @@ int discard_index(struct index_state *istate)
 	free_name_hash(istate);
 	cache_tree_free(&(istate->cache_tree));
 	istate->initialized = 0;
-	free(istate->cache);
-	istate->cache = NULL;
+	freez(istate->cache);
 	istate->cache_alloc = 0;
 	discard_split_index(istate);
 	free_untracked_cache(istate->untracked);
@@ -2603,8 +2602,7 @@ void *read_blob_data_from_index(const struct index_state *istate,
 
 void stat_validity_clear(struct stat_validity *sv)
 {
-	free(sv->sd);
-	sv->sd = NULL;
+	freez(sv->sd);
 }
 
 int stat_validity_check(struct stat_validity *sv, const char *path)
ref-filter.c +1 -2
index ab32bc9c31..5b05d93575 100644
@@ -1891,8 +1891,7 @@ void ref_array_clear(struct ref_array *array)
 
 	for (i = 0; i < array->nr; i++)
 		free_array_item(array->items[i]);
-	free(array->items);
-	array->items = NULL;
+	freez(array->items);
 	array->nr = array->alloc = 0;
 }
 
refs/files-backend.c +1 -2
index d8b3f73147..dd90b090e8 100644
@@ -2944,8 +2944,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
 				       head_oid.hash, &head_type);
 
 	if (head_ref && !(head_type & REF_ISSYMREF)) {
-		free(head_ref);
-		head_ref = NULL;
+		freez(head_ref);
 	}
 
 	/*
refs/ref-cache.c +1 -2
index af2fcb2c12..ab12e0dc9d 100644
@@ -82,9 +82,8 @@ static void clear_ref_dir(struct ref_dir *dir)
 	int i;
 	for (i = 0; i < dir->nr; i++)
 		free_ref_entry(dir->entries[i]);
-	free(dir->entries);
+	freez(dir->entries);
 	dir->sorted = dir->nr = dir->alloc = 0;
-	dir->entries = NULL;
 }
 
 struct ref_entry *create_dir_entry(struct ref_cache *cache,
remote-testsvn.c +1 -2
index f87bf851ba..9b893a5f09 100644
@@ -66,8 +66,7 @@ static char *read_ref_note(const unsigned char sha1[20])
 	else if (!msglen || type != OBJ_BLOB) {
 		error("Note contains unusable content. "
 			"Is something else using this notes tree? %s", notes_ref);
-		free(msg);
-		msg = NULL;
+		freez(msg);
 	}
 	free_notes(NULL);
 	return msg;
rerere.c +1 -2
index 3bd55caf3b..43b62e6135 100644
@@ -39,9 +39,8 @@ static void free_rerere_dirs(void)
 		free(rerere_dir[i]->status);
 		free(rerere_dir[i]);
 	}
-	free(rerere_dir);
+	freez(rerere_dir);
 	rerere_dir_nr = rerere_dir_alloc = 0;
-	rerere_dir = NULL;
 }
 
 static void free_rerere_id(struct string_list_item *item)
sequencer.c +1 -2
index 924fb1d0c3..e542b3bd79 100644
@@ -1211,8 +1211,7 @@ struct todo_list {
 static void todo_list_release(struct todo_list *todo_list)
 {
 	strbuf_release(&todo_list->buf);
-	free(todo_list->items);
-	todo_list->items = NULL;
+	freez(todo_list->items);
 	todo_list->nr = todo_list->alloc = 0;
 }
 
sha1-array.c +1 -2
index 7d646ab5b8..c98506fd77 100644
@@ -35,8 +35,7 @@ int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
 
 void oid_array_clear(struct oid_array *array)
 {
-	free(array->oid);
-	array->oid = NULL;
+	freez(array->oid);
 	array->nr = 0;
 	array->alloc = 0;
 	array->sorted = 0;
sha1_file.c +1 -2
index 59a4ed2ed3..a34d904012 100644
@@ -610,8 +610,7 @@ char *compute_alternate_path(const char *path, struct strbuf *err)
 
 out:
 	if (seen_error) {
-		free(ref_git);
-		ref_git = NULL;
+		freez(ref_git);
 	}
 
 	return ref_git;
split-index.c +1 -2
index 49bd197f71..87c197617a 100644
@@ -174,10 +174,9 @@ void merge_base_index(struct index_state *istate)
 
 	ewah_free(si->delete_bitmap);
 	ewah_free(si->replace_bitmap);
-	free(si->saved_cache);
+	freez(si->saved_cache);
 	si->delete_bitmap  = NULL;
 	si->replace_bitmap = NULL;
-	si->saved_cache	   = NULL;
 	si->saved_cache_nr = 0;
 }
 
transport-helper.c +9 -18
index 36408046eb..a986589a14 100644
@@ -242,8 +242,7 @@ static int disconnect_helper(struct transport *transport)
 		close(data->helper->out);
 		fclose(data->out);
 		res = finish_command(data->helper);
-		free(data->helper);
-		data->helper = NULL;
+		freez(data->helper);
 	}
 	return res;
 }
@@ -711,43 +710,35 @@ static int push_update_ref_status(struct strbuf *buf,
 
 		if (!strcmp(msg, "no match")) {
 			status = REF_STATUS_NONE;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 		else if (!strcmp(msg, "up to date")) {
 			status = REF_STATUS_UPTODATE;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 		else if (!strcmp(msg, "non-fast forward")) {
 			status = REF_STATUS_REJECT_NONFASTFORWARD;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 		else if (!strcmp(msg, "already exists")) {
 			status = REF_STATUS_REJECT_ALREADY_EXISTS;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 		else if (!strcmp(msg, "fetch first")) {
 			status = REF_STATUS_REJECT_FETCH_FIRST;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 		else if (!strcmp(msg, "needs force")) {
 			status = REF_STATUS_REJECT_NEEDS_FORCE;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 		else if (!strcmp(msg, "stale info")) {
 			status = REF_STATUS_REJECT_STALE;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 		else if (!strcmp(msg, "forced update")) {
 			forced = 1;
-			free(msg);
-			msg = NULL;
+			freez(msg);
 		}
 	}
 
transport.c +1 -2
index 9bfcf870f9..1023a2d281 100644
@@ -1145,8 +1145,7 @@ void transport_unlock_pack(struct transport *transport)
 {
 	if (transport->pack_lockfile) {
 		unlink_or_warn(transport->pack_lockfile);
-		free(transport->pack_lockfile);
-		transport->pack_lockfile = NULL;
+		freez(transport->pack_lockfile);
 	}
 }
 
tree-diff.c +2 -4
index e164e532b2..022745d4bd 100644
@@ -140,8 +140,7 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
 	/* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
 	p = last->next;
 	if (p && (alloclen > (intptr_t)p->next)) {
-		free(p);
-		p = NULL;
+		freez(p);
 	}
 
 	if (!p) {
@@ -559,8 +558,7 @@ struct combine_diff_path *diff_tree_paths(
 	 * (see path_appendnew() for details about why)
 	 */
 	if (p->next) {
-		free(p->next);
-		p->next = NULL;
+		freez(p->next);
 	}
 
 	return p;
tree-walk.c +1 -2
index 6a42e402b0..9f162155fe 100644
@@ -479,8 +479,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
 	for (i = 0; i < n; i++)
 		free_extended_entry(tx + i);
 	free(tx);
-	free(traverse_path);
-	info->traverse_path = NULL;
+	freez(traverse_path);
 	strbuf_release(&base);
 	return error;
 }
tree.c +1 -2
index 603b29ee80..c03caabc0f 100644
@@ -226,8 +226,7 @@ int parse_tree_gently(struct tree *item, int quiet_on_missing)
 
 void free_tree_buffer(struct tree *tree)
 {
-	free(tree->buffer);
-	tree->buffer = NULL;
+	freez(tree->buffer);
 	tree->size = 0;
 	tree->object.parsed = 0;
 }
Signature · 1 line
2.13.0.506.g27d5fe0cd
#66 lore raw

Re: [PATCH 2/2] *.[ch] refactoring: make use of the freez() wrapper

Quoted 3 lines · Ævar Arnfjörð Bjarmason

Replace occurrences of free(p); p = NULL with freez(p). This introduces no functional changes, but cuts the number of lines spent on this cleanup in half.

It's even better than that. ;)

Quoted 1 line
 48 files changed, 97 insertions(+), 197 deletions(-)

The difference is in builtin/am.c where some empty lines are removed in am_next(), so no need to be alarmed.

The macro would be dangerous with things like "freez(ptr++)" but I couldn't find any such side-effects. In hindsight, I guess your commit message says as much since using "ptr++" for "p" would already be a bug.

I have no idea whether this conflicts with other topics, or any opinion on the best strategy for doing the conversion (all-at-once or "while we're here").

#67 lore raw

Re: [PATCH 2/2] *.[ch] refactoring: make use of the freez() wrapper

Quoted 9 lines · Martin Ågren
Quoted 3 lines · Ævar Arnfjörð Bjarmason

Replace occurrences of free(p); p = NULL with freez(p). This introduces no functional changes, but cuts the number of lines spent on this cleanup in half.

It's even better than that. ;)

Quoted 1 line
 48 files changed, 97 insertions(+), 197 deletions(-)
Quoted 7 lines

The difference is in builtin/am.c where some empty lines are removed in am_next(), so no need to be alarmed.

The macro would be dangerous with things like "freez(ptr++)" but I couldn't find any such side-effects. In hindsight, I guess your commit message says as much since using "ptr++" for "p" would already be a bug.

Yes, although perhaps we should call this FREEZ() or GIT_FREEZ() instead of freez() to make it clear that it's a macro.

Quoted 3 lines

I have no idea whether this conflicts with other topics, or any opinion on the best strategy for doing the conversion (all-at-once or "while we're here").

It has no conflicts with pu, so that's something, and passes all tests with & without that merge.

#68 lore raw

Re: [PATCH 2/2] *.[ch] refactoring: make use of the freez() wrapper

Quoted 10 lines · Ævar Arnfjörð Bjarmason
Quoted 9 lines · Martin Ågren
Quoted 3 lines · Ævar Arnfjörð Bjarmason

Replace occurrences of free(p); p = NULL with freez(p). This introduces no functional changes, but cuts the number of lines spent on this cleanup in half.

It's even better than that. ;)

Quoted 1 line
 48 files changed, 97 insertions(+), 197 deletions(-)
Quoted 7 lines
Quoted 7 lines

The difference is in builtin/am.c where some empty lines are removed in am_next(), so no need to be alarmed.

The macro would be dangerous with things like "freez(ptr++)" but I couldn't find any such side-effects. In hindsight, I guess your commit message says as much since using "ptr++" for "p" would already be a bug.

It also couldn't hurt to add a comment to the macro definition explaining that side-effect operators would be broken.

Quoted 10 lines

Yes, although perhaps we should call this FREEZ() or GIT_FREEZ() instead of freez() to make it clear that it's a macro.

Quoted 3 lines

I have no idea whether this conflicts with other topics, or any opinion on the best strategy for doing the conversion (all-at-once or "while we're here").

It has no conflicts with pu, so that's something, and passes all tests with & without that merge.

Signature · 1 line
Brandon Williams
#69 lore raw

Re: [PATCH 0/2] Add a freez() wrapper

In reply to Ævar Arnfjörð Bjarmason
Quoted 13 lines · Ævar Arnfjörð Bjarmason
Quoted 12 lines · Brandon Williams

Introduce the repository object 'struct repository' which can be used to hold all state pertaining to a git repository.
[...]

repository.c new file +9 -0
+static void repo_clear_env(struct repository *repo)
+{
+       free(repo->gitdir);
+       repo->gitdir = NULL;
+       free(repo->commondir);
+       repo->commondir = NULL;
+       free(repo->objectdir);
+       repo->objectdir = NULL;
+       free(repo->index_file);
Quoted 8 lines

repository.c line 164

Quoted 6 lines
repository.c new file +6 -0
+       repo->index_file = NULL;
+       free(repo->graft_file);
+       repo->graft_file = NULL;
+       free(repo->namespace);
+       repo->namespace = NULL;
+}

When seeing this I wondered if it couldn't be less noisy as:

One of the other alternatives is using memset, but that's difficult to only clear only a few fields.

Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +6 -0
+       freez(repo->gitdir);
+       freez(repo->commondir);
+       freez(repo->objectdir);
+       freez(repo->index_file);
+       freez(repo->graft_file);
+       freez(repo->namespace);

It's not a problem with your patch, but something I thought would be nice to have in general, so here's a patch series to implement that.

This also could makes things a little less error prone. Thanks!

Quoted 59 lines

Ævar Arnfjörð Bjarmason (2):
git-compat-util: add a freez() wrapper around free(x); x = NULL *.[ch] refactoring: make use of the freez() wrapper

 alias.c                  |  6 ++----
 apply.c                  |  3 +--
 attr.c                   |  6 ++----
 blame.c                  |  3 +--
 branch.c                 |  3 +--
 builtin/am.c             | 18 +++++-------------
 builtin/clean.c          |  6 ++----
 builtin/config.c         |  6 ++----
 builtin/index-pack.c     |  6 ++----
 builtin/pack-objects.c   | 12 ++++--------
 builtin/unpack-objects.c |  3 +--
 builtin/worktree.c       |  6 ++----
 commit-slab.h            |  3 +--
 commit.c                 |  3 +--
 config.c                 |  3 +--
 credential.c             |  9 +++------
 diff-lib.c               |  3 +--
 diff.c                   |  6 ++----
 diffcore-rename.c        |  6 ++----
 dir.c                    |  9 +++------
 fast-import.c            |  6 ++----
 git-compat-util.h        |  1 +
 gpg-interface.c          | 15 +++++----------
 grep.c                   | 12 ++++--------
 help.c                   |  3 +--
 http-push.c              | 24 ++++++++----------------
 http.c                   | 15 +++++----------
 imap-send.c              |  3 +--
 line-log.c               |  6 ++----
 ll-merge.c               |  3 +--
 mailinfo.c               |  3 +--
 object.c                 |  3 +--
 pathspec.c               |  3 +--
 prio-queue.c             |  3 +--
 read-cache.c             |  6 ++----
 ref-filter.c             |  3 +--
 refs/files-backend.c     |  3 +--
 refs/ref-cache.c         |  3 +--
 remote-testsvn.c         |  3 +--
 rerere.c                 |  3 +--
 sequencer.c              |  3 +--
 sha1-array.c             |  3 +--
 sha1_file.c              |  3 +--
 split-index.c            |  3 +--
 transport-helper.c       | 27 +++++++++------------------
 transport.c              |  3 +--
 tree-diff.c              |  6 ++----
 tree-walk.c              |  3 +--
 tree.c                   |  3 +--
 49 files changed, 98 insertions(+), 197 deletions(-)
Signature · 1 line
2.13.0.506.g27d5fe0cd
Signature · 1 line
Brandon Williams
#70 lore raw

v2 cover Add a FREEZ() wrapper macro

In reply to Ævar Arnfjörð Bjarmason

See <[email protected]>
(https://public-inbox.org/git/[email protected]/)
for v1.

Changes:

* Now called FREEZ() instead of freez().
* Documented the caveat of giving it ptr++ both in the commit message and in a comment.
* Moved the definition to be with other similar macros.
* Just unconditionally call free(ptr), even if ptr is NULL, as we do everywhere else.

Ævar Arnfjörð Bjarmason (2):
git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL *.[ch] refactoring: make use of the FREEZ() macro

 alias.c                  |  6 ++----
 apply.c                  |  3 +--
 attr.c                   |  6 ++----
 blame.c                  |  3 +--
 branch.c                 |  3 +--
 builtin/am.c             | 18 +++++-------------
 builtin/clean.c          |  6 ++----
 builtin/config.c         |  6 ++----
 builtin/index-pack.c     |  6 ++----
 builtin/pack-objects.c   | 12 ++++--------
 builtin/unpack-objects.c |  3 +--
 builtin/worktree.c       |  6 ++----
 commit-slab.h            |  3 +--
 commit.c                 |  3 +--
 config.c                 |  3 +--
 credential.c             |  9 +++------
 diff-lib.c               |  3 +--
 diff.c                   |  6 ++----
 diffcore-rename.c        |  6 ++----
 dir.c                    |  9 +++------
 fast-import.c            |  6 ++----
 git-compat-util.h        |  6 ++++++
 gpg-interface.c          | 15 +++++----------
 grep.c                   | 12 ++++--------
 help.c                   |  3 +--
 http-push.c              | 24 ++++++++----------------
 http.c                   | 15 +++++----------
 imap-send.c              |  3 +--
 line-log.c               |  6 ++----
 ll-merge.c               |  3 +--
 mailinfo.c               |  3 +--
 object.c                 |  3 +--
 pathspec.c               |  3 +--
 prio-queue.c             |  3 +--
 read-cache.c             |  6 ++----
 ref-filter.c             |  3 +--
 refs/files-backend.c     |  3 +--
 refs/ref-cache.c         |  3 +--
 remote-testsvn.c         |  3 +--
 rerere.c                 |  3 +--
 sequencer.c              |  3 +--
 sha1-array.c             |  3 +--
 sha1_file.c              |  3 +--
 split-index.c            |  3 +--
 transport-helper.c       | 27 +++++++++------------------
 transport.c              |  3 +--
 tree-diff.c              |  6 ++----
 tree-walk.c              |  3 +--
 tree.c                   |  3 +--
 49 files changed, 103 insertions(+), 197 deletions(-)
Signature · 1 line
2.13.1.508.gb3defc5cc
#71 lore raw

PATCH v2 1/2 git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Add a FREEZ() wrapper marco for the common pattern of freeing a pointer and assigning NULL to it right afterwards.

The implementation is similar to the (currently unused) XDL_PTRFREE macro in xdiff/xmacros.h added in commit 3443546f6e ("Use a *real* built-in diff generator", 2006-03-24). The only difference is that free() is called unconditionally, see [1].

1. <alpine.DEB.2.20.1608301948310.129229@virtualbox>
(http://public-inbox.org/git/alpine.DEB.2.20.1608301948310.129229@virtualbox/)

---
 git-compat-util.h | 6 ++++++
 1 file changed, 6 insertions(+)
git-compat-util.h +6 -0
index 4b7dcf21ad..ba2d0c8c80 100644
@@ -805,6 +805,12 @@ extern int xmkstemp_mode(char *template, int mode);
 extern char *xgetcwd(void);
 extern FILE *fopen_for_writing(const char *path);
 
+/*
+ * FREEZ(ptr) is like free(ptr) followed by ptr = NULL. Note that ptr
+ * is used twice, so don't pass e.g. ptr++.
+ */
+#define FREEZ(p) do { free(p); (p) = NULL; } while (0)
+
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
 
Signature · 1 line
2.13.1.508.gb3defc5cc
#72 lore raw

Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Hi,

Quoted 2 lines · Ævar Arnfjörð Bjarmason

Add a FREEZ() wrapper marco for the common pattern of freeing a pointer and assigning NULL to it right afterwards.

I'm conflicted. On one hand it makes code more concise and makes it easier for people to remember to assign NULL after freeing a variable. On the other hand it makes git more of a custom dialect of C, which may make the code harder to read and hack on for new contributors.

My feeling is that the costs outweigh the benefits, but I haven't thought it through thoroughly.

Quoted 7 lines

The implementation is similar to the (currently unused) XDL_PTRFREE macro in xdiff/xmacros.h added in commit 3443546f6e ("Use a *real* built-in diff generator", 2006-03-24). The only difference is that free() is called unconditionally, see [1].

1. <alpine.DEB.2.20.1608301948310.129229@virtualbox>
(http://public-inbox.org/git/alpine.DEB.2.20.1608301948310.129229@virtualbox/)

Nit: it's redundant to state the message-id twice.

Can this commit message include a summary of that conversation so someone trying to understand this patch with "git log" does not have to open a browser and re-read the thread? Is that thread being cited for the point that git uses 'free(NULL)' without worrying? I think that's okay to say without citation --- there are lots of examples throughout the git codebase.

Quoted 11 lines
---
 git-compat-util.h | 6 ++++++
 1 file changed, 6 insertions(+)
git-compat-util.h +0 -0
index 4b7dcf21ad..ba2d0c8c80 100644
@@ -805,6 +805,12 @@ extern int xmkstemp_mode(char *template, int mode);
 extern char *xgetcwd(void);

git-compat-util.h extern int xmkstemp_mode(char *template, int mode); · line 815

Quoted 10 lines
git-compat-util.h +6 -0
 extern FILE *fopen_for_writing(const char *path);
 
+/*
+ * FREEZ(ptr) is like free(ptr) followed by ptr = NULL. Note that ptr
+ * is used twice, so don't pass e.g. ptr++.
+ */
+#define FREEZ(p) do { free(p); (p) = NULL; } while (0)
+
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))

Golfing: it's possible to do this without ptr being used twice by introducing a helper function:

	static inline void freez_impl(void **p) {
		free(*p);
		*p = NULL;
	}
	#define FREEZ(p) freez_impl(&(p))

That way side-effectful callers like FREEZ(func() ? a : b) would work.

Another request: if rolling out another version of this series, could it go in a new thread? The cover letter can link to the old version for context. Each time I see a new reply to the repository object series my heart leaps for a moment, in the hope that we're getting closer to have a repository object in git.

I kind of wish that 'free' returned NULL so that callers could do

	p = free(p);

without requiring a custom helper. We could introduce a free_wrapper that works that way but that is probably not worth it, either.

Thanks for an interesting and pleasant read.

Sincerely,
Jonathan

#73 lore raw

Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Quoted 11 lines · Jonathan Nieder

Hi,

Quoted 2 lines · Ævar Arnfjörð Bjarmason

Add a FREEZ() wrapper marco for the common pattern of freeing a pointer and assigning NULL to it right afterwards.

I'm conflicted. On one hand it makes code more concise and makes it easier for people to remember to assign NULL after freeing a variable. On the other hand it makes git more of a custom dialect of C, which may make the code harder to read and hack on for new contributors.

I think this problem could be avoided by using a more explicit name, perhaps: "free_and_null"

Seeing the initial subject, I thought this series was short for "freeze" (like "creat").

However, I admit FREEZ caught my eye because I thought it was a way to freeze a repository, somehow :)

Quoted 2 lines

My feeling is that the costs outweigh the benefits, but I haven't thought it through thoroughly.

<snip>

Quoted 19 lines
Quoted 5 lines

index 4b7dcf21ad..ba2d0c8c80 100644

git-compat-util.h +0 -0
@@ -805,6 +805,12 @@ extern int xmkstemp_mode(char *template, int mode);
 extern char *xgetcwd(void);

git-compat-util.h extern int xmkstemp_mode(char *template, int mode); · line 815

Quoted 10 lines
git-compat-util.h +6 -0
 extern FILE *fopen_for_writing(const char *path);
 
+/*
+ * FREEZ(ptr) is like free(ptr) followed by ptr = NULL. Note that ptr
+ * is used twice, so don't pass e.g. ptr++.
+ */
+#define FREEZ(p) do { free(p); (p) = NULL; } while (0)
+
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))

Golfing: it's possible to do this without ptr being used twice by introducing a helper function:

Quoted 4 lines
	static inline void freez_impl(void **p) {
		free(*p);
		*p = NULL;
	}

Yes. I think it's prudent to avoid macros in case there are side effects.

Quoted 4 lines
	#define FREEZ(p) freez_impl(&(p))

That way side-effectful callers like FREEZ(func() ? a : b) would work.

I don't see the point of a macro wrapper, forcing the user to type out the '&' should drive home the point that the pointer gets set to NULL. I also find capitalization tiring-to-read because all characters are the same height.

<snip>

Quoted 6 lines

I kind of wish that 'free' returned NULL so that callers could do

	p = free(p);

without requiring a custom helper. We could introduce a free_wrapper that works that way but that is probably not worth it, either.

Sometimes I have wished similar things, too, but that means the same identifier shows up twice in one line and camouflages the code.

#74 lore raw

Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Quoted 13 lines · Eric Wong
Quoted 11 lines · Jonathan Nieder

Hi,

Quoted 2 lines · Ævar Arnfjörð Bjarmason

Add a FREEZ() wrapper marco for the common pattern of freeing a pointer and assigning NULL to it right afterwards.

I'm conflicted. On one hand it makes code more concise and makes it easier for people to remember to assign NULL after freeing a variable. On the other hand it makes git more of a custom dialect of C, which may make the code harder to read and hack on for new contributors.

Quoted 5 lines

I think this problem could be avoided by using a more explicit name, perhaps: "free_and_null"

Seeing the initial subject, I thought this series was short for "freeze" (like "creat").

Both match my thoughts exactly ;-)

Quoted 5 lines

...
I don't see the point of a macro wrapper, forcing the user to type out the '&' should drive home the point that the pointer gets set to NULL. I also find capitalization tiring-to-read because all characters are the same height.

Sounds sensible.

So make Jonathan's freez_impl a public API and rename it to free_and_null(), perhaps?

#75 lore raw

Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Eric Wong

I don't see the point of a macro wrapper, forcing the user to type out the '&' should drive home the point that the pointer gets set to NULL. I also find capitalization tiring-to-read because all characters are the same height.

Sounds sensible.

So make Jonathan's freez_impl a public API and rename it to free_and_null(), perhaps?

Perhaps... I think it needs to take "void *" to avoid warnings:

	static inline void free_and_null(void *ptrptr)
	{
		void **tmp = ptrptr;

		free(*tmp);
		*tmp = NULL;
	}

...At least I needed to do that for "mog_free_and_null" in cmogstored:

	https://bogomips.org/cmogstored.git/plain/alloc.c?h=v1.6.0

But heck, maybe that's covering up for something else I got
wrong in cmogstored *shrug* I don't know all of C.

#76 lore raw

Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Quoted 5 lines · Eric Wong
Quoted 2 lines

So make Jonathan's freez_impl a public API and rename it to free_and_null(), perhaps?

Perhaps... I think it needs to take "void *" to avoid warnings:

Quoted 7 lines
	static inline void free_and_null(void *ptrptr)
	{
		void **tmp = ptrptr;

		free(*tmp);
		*tmp = NULL;
	}

That unfortunately makes it very easy to get it wrong in the callers. Both:

  free_and_null(&p);

and

  free_and_null(p);

would be accepted by the compiler, but one of them causes undefined behavior.

Unfortunately using "void **" in the declaration doesn't work, because C's implicit casting rules don't apply to pointer-to-pointer types.

-Peff

#77 lore raw

Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Quoted 22 lines · Jeff King
Quoted 5 lines · Eric Wong
Quoted 2 lines

So make Jonathan's freez_impl a public API and rename it to free_and_null(), perhaps?

Perhaps... I think it needs to take "void *" to avoid warnings:

Quoted 7 lines
	static inline void free_and_null(void *ptrptr)
	{
		void **tmp = ptrptr;

		free(*tmp);
		*tmp = NULL;
	}

That unfortunately makes it very easy to get it wrong in the callers. Both:

  free_and_null(&p);

and

Quoted 7 lines
  free_and_null(p);

would be accepted by the compiler, but one of them causes undefined behavior.

Unfortunately using "void **" in the declaration doesn't work, because C's implicit casting rules don't apply to pointer-to-pointer types.

All true.

I still think the macro FREEZ() is too confusing a name to live; perhaps we can take Ævar's patch with s/FREEZ/FREE_AND_NULL/ and be done with it? By spelling it in all caps, readers will know that there is something special going on in that macro, and Eric's "forcing the readers to type & in front to let them be aware that the ptr variable is being manipulated" may become less necessary.

#78 lore raw

Re: [PATCH v2 1/2] git-compat-util: add a FREEZ() wrapper around free(ptr); ptr = NULL

Quoted 32 lines · Junio C Hamano
Quoted 22 lines · Jeff King
Quoted 5 lines · Eric Wong
Quoted 2 lines

So make Jonathan's freez_impl a public API and rename it to free_and_null(), perhaps?

Perhaps... I think it needs to take "void *" to avoid warnings:

Quoted 7 lines
     static inline void free_and_null(void *ptrptr)
     {
             void **tmp = ptrptr;

             free(*tmp);
             *tmp = NULL;
     }

That unfortunately makes it very easy to get it wrong in the callers. Both:

  free_and_null(&p);

and

Quoted 7 lines
  free_and_null(p);

would be accepted by the compiler, but one of them causes undefined behavior.

Unfortunately using "void **" in the declaration doesn't work, because C's implicit casting rules don't apply to pointer-to-pointer types.

Quoted 8 lines

All true.

I still think the macro FREEZ() is too confusing a name to live; perhaps we can take Ævar's patch with s/FREEZ/FREE_AND_NULL/ and be done with it? By spelling it in all caps, readers will know that there is something special going on in that macro, and Eric's "forcing the readers to type & in front to let them be aware that the ptr variable is being manipulated" may become less necessary.

I'll change it to FREE_AND_NULL and submit my patch as-is, my reading of the rest of this thread is that making it a function instead of a macro would be interesting, but has its own caveats that are likely better considered as part of its own series, whereas this just changes existing code to its macro-expanded functional equivalent.

#79 lore raw

v3 cover Add a FREE_AND_NULL() wrapper macro

On Thu, Jun 15 2017, Ævar Arnfjörð Bjarmason jotted:

Quoted 5 lines

I'll change it to FREE_AND_NULL and submit my patch as-is, my reading of the rest of this thread is that making it a function instead of a macro would be interesting, but has its own caveats that are likely better considered as part of its own series, whereas this just changes existing code to its macro-expanded functional equivalent.

Here's v3 with that change. Nothing but the macro name (and comments, commit messages etc. referring to it) have changed.

Ævar Arnfjörð Bjarmason (2):
git-compat-util: add a FREE_AND_NULL() wrapper around free(ptr); ptr =
NULL
*.[ch] refactoring: make use of the FREE_AND_NULL() macro

 alias.c                  |  6 ++----
 apply.c                  |  3 +--
 attr.c                   |  6 ++----
 blame.c                  |  3 +--
 branch.c                 |  3 +--
 builtin/am.c             | 18 +++++-------------
 builtin/clean.c          |  6 ++----
 builtin/config.c         |  6 ++----
 builtin/index-pack.c     |  6 ++----
 builtin/pack-objects.c   | 12 ++++--------
 builtin/unpack-objects.c |  3 +--
 builtin/worktree.c       |  6 ++----
 commit-slab.h            |  3 +--
 commit.c                 |  3 +--
 config.c                 |  3 +--
 credential.c             |  9 +++------
 diff-lib.c               |  3 +--
 diff.c                   |  6 ++----
 diffcore-rename.c        |  6 ++----
 dir.c                    |  9 +++------
 fast-import.c            |  6 ++----
 git-compat-util.h        |  6 ++++++
 gpg-interface.c          | 15 +++++----------
 grep.c                   | 12 ++++--------
 help.c                   |  3 +--
 http-push.c              | 24 ++++++++----------------
 http.c                   | 15 +++++----------
 imap-send.c              |  3 +--
 line-log.c               |  6 ++----
 ll-merge.c               |  3 +--
 mailinfo.c               |  3 +--
 object.c                 |  3 +--
 pathspec.c               |  3 +--
 prio-queue.c             |  3 +--
 read-cache.c             |  6 ++----
 ref-filter.c             |  3 +--
 refs/files-backend.c     |  3 +--
 refs/ref-cache.c         |  3 +--
 remote-testsvn.c         |  3 +--
 rerere.c                 |  3 +--
 sequencer.c              |  3 +--
 sha1-array.c             |  3 +--
 sha1_file.c              |  3 +--
 split-index.c            |  3 +--
 transport-helper.c       | 27 +++++++++------------------
 transport.c              |  3 +--
 tree-diff.c              |  6 ++----
 tree-walk.c              |  3 +--
 tree.c                   |  3 +--
 49 files changed, 103 insertions(+), 197 deletions(-)
Signature · 1 line
2.13.1.508.gb3defc5cc
#80 lore raw

PATCH v3 1/2 git-compat-util: add a FREE_AND_NULL() wrapper around free(ptr); ptr = NULL

Add a FREE_AND_NULL() wrapper marco for the common pattern of freeing a pointer and assigning NULL to it right afterwards.

The implementation is similar to the (currently unused) XDL_PTRFREE macro in xdiff/xmacros.h added in commit 3443546f6e ("Use a *real* built-in diff generator", 2006-03-24). The only difference is that free() is called unconditionally, see [1].

See [2] for a suggested alternative which does this via a function instead of a macro. As covered in replies to that message, while it's a viable approach, it would introduce caveats which this approach doesn't have, so that potential change is left to a future follow-up change.

This merely allows us to translate exactly what we're doing now to a less verbose & idiomatic form using a macro, while guaranteeing that we don't introduce any functional changes.

1. <alpine.DEB.2.20.1608301948310.129229@virtualbox>
(http://public-inbox.org/git/alpine.DEB.2.20.1608301948310.129229@virtualbox/)

2. <20170610032143.GA7880@starla>
(https://public-inbox.org/git/20170610032143.GA7880@starla/)

---
 git-compat-util.h | 6 ++++++
 1 file changed, 6 insertions(+)
git-compat-util.h +6 -0
index 51ba4e6b3b..047172d173 100644
@@ -808,6 +808,12 @@ extern char *xgetcwd(void);
 extern FILE *fopen_for_writing(const char *path);
 extern FILE *fopen_or_warn(const char *path, const char *mode);
 
+/*
+ * 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 REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
 
Signature · 1 line
2.13.1.508.gb3defc5cc
#81 lore raw

PATCH v3 2/2 *.[ch] refactoring: make use of the FREE_AND_NULL() macro

In reply to Ævar Arnfjörð Bjarmason

Replace occurrences of free(ptr); ptr = NULL with FREE_AND_NULL(ptr). This introduces no functional changes, but reduces the line count and establishes this pattern as a common idiom with a wrapper macro.

---
 alias.c                  |  6 ++----
 apply.c                  |  3 +--
 attr.c                   |  6 ++----
 blame.c                  |  3 +--
 branch.c                 |  3 +--
 builtin/am.c             | 18 +++++-------------
 builtin/clean.c          |  6 ++----
 builtin/config.c         |  6 ++----
 builtin/index-pack.c     |  6 ++----
 builtin/pack-objects.c   | 12 ++++--------
 builtin/unpack-objects.c |  3 +--
 builtin/worktree.c       |  6 ++----
 commit-slab.h            |  3 +--
 commit.c                 |  3 +--
 config.c                 |  3 +--
 credential.c             |  9 +++------
 diff-lib.c               |  3 +--
 diff.c                   |  6 ++----
 diffcore-rename.c        |  6 ++----
 dir.c                    |  9 +++------
 fast-import.c            |  6 ++----
 gpg-interface.c          | 15 +++++----------
 grep.c                   | 12 ++++--------
 help.c                   |  3 +--
 http-push.c              | 24 ++++++++----------------
 http.c                   | 15 +++++----------
 imap-send.c              |  3 +--
 line-log.c               |  6 ++----
 ll-merge.c               |  3 +--
 mailinfo.c               |  3 +--
 object.c                 |  3 +--
 pathspec.c               |  3 +--
 prio-queue.c             |  3 +--
 read-cache.c             |  6 ++----
 ref-filter.c             |  3 +--
 refs/files-backend.c     |  3 +--
 refs/ref-cache.c         |  3 +--
 remote-testsvn.c         |  3 +--
 rerere.c                 |  3 +--
 sequencer.c              |  3 +--
 sha1-array.c             |  3 +--
 sha1_file.c              |  3 +--
 split-index.c            |  3 +--
 transport-helper.c       | 27 +++++++++------------------
 transport.c              |  3 +--
 tree-diff.c              |  6 ++----
 tree-walk.c              |  3 +--
 tree.c                   |  3 +--
 48 files changed, 97 insertions(+), 197 deletions(-)
alias.c +2 -4
index 3b90397a99..911481855f 100644
@@ -47,8 +47,7 @@ int split_cmdline(char *cmdline, const char ***argv)
 				src++;
 				c = cmdline[src];
 				if (!c) {
-					free(*argv);
-					*argv = NULL;
+					FREE_AND_NULL(*argv);
 					return -SPLIT_CMDLINE_BAD_ENDING;
 				}
 			}
@@ -60,8 +59,7 @@ int split_cmdline(char *cmdline, const char ***argv)
 	cmdline[dst] = 0;
 
 	if (quoted) {
-		free(*argv);
-		*argv = NULL;
+		FREE_AND_NULL(*argv);
 		return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
 	}
 
apply.c +1 -2
index 854faa6779..e78de0affa 100644
@@ -3705,8 +3705,7 @@ static int check_preimage(struct apply_state *state,
  is_new:
 	patch->is_new = 1;
 	patch->is_delete = 0;
-	free(patch->old_name);
-	patch->old_name = NULL;
+	FREE_AND_NULL(patch->old_name);
 	return 0;
 }
 
attr.c +2 -4
index 821203e2a9..ebdcfb0b8a 100644
@@ -638,13 +638,11 @@ void attr_check_reset(struct attr_check *check)
 
 void attr_check_clear(struct attr_check *check)
 {
-	free(check->items);
-	check->items = NULL;
+	FREE_AND_NULL(check->items);
 	check->alloc = 0;
 	check->nr = 0;
 
-	free(check->all_attrs);
-	check->all_attrs = NULL;
+	FREE_AND_NULL(check->all_attrs);
 	check->all_attrs_nr = 0;
 
 	drop_attr_stack(&check->stack);
blame.c +1 -2
index 843c845cba..1183943960 100644
@@ -314,8 +314,7 @@ static void fill_origin_blob(struct diff_options *opt,
 static void drop_origin_blob(struct blame_origin *o)
 {
 	if (o->file.ptr) {
-		free(o->file.ptr);
-		o->file.ptr = NULL;
+		FREE_AND_NULL(o->file.ptr);
 	}
 }
 
branch.c +1 -2
index 985316eb76..2347cb8649 100644
@@ -24,8 +24,7 @@ static int find_tracked_branch(struct remote *remote, void *priv)
 		} else {
 			free(tracking->spec.src);
 			if (tracking->src) {
-				free(tracking->src);
-				tracking->src = NULL;
+				FREE_AND_NULL(tracking->src);
 			}
 		}
 		tracking->spec.src = NULL;
builtin/am.c +5 -13
index 8881d73615..80368b6fe6 100644
@@ -483,8 +483,7 @@ static int run_applypatch_msg_hook(struct am_state *state)
 	ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
 
 	if (!ret) {
-		free(state->msg);
-		state->msg = NULL;
+		FREE_AND_NULL(state->msg);
 		if (read_commit_msg(state) < 0)
 			die(_("'%s' was deleted by the applypatch-msg hook"),
 				am_path(state, "final-commit"));
@@ -1073,17 +1072,10 @@ static void am_next(struct am_state *state)
 {
 	struct object_id head;
 
-	free(state->author_name);
-	state->author_name = NULL;
-
-	free(state->author_email);
-	state->author_email = NULL;
-
-	free(state->author_date);
-	state->author_date = NULL;
-
-	free(state->msg);
-	state->msg = NULL;
+	FREE_AND_NULL(state->author_name);
+	FREE_AND_NULL(state->author_email);
+	FREE_AND_NULL(state->author_date);
+	FREE_AND_NULL(state->msg);
 	state->msg_len = 0;
 
 	unlink(am_path(state, "author-script"));
builtin/clean.c +2 -4
index 142bf668cf..bff5a07330 100644
@@ -837,8 +837,7 @@ static void interactive_main_loop(void)
 			int ret;
 			ret = menus[*chosen].fn();
 			if (ret != MENU_RETURN_NO_LOOP) {
-				free(chosen);
-				chosen = NULL;
+				FREE_AND_NULL(chosen);
 				if (!del_list.nr) {
 					clean_print_color(CLEAN_COLOR_ERROR);
 					printf_ln(_("No more files to clean, exiting."));
@@ -851,8 +850,7 @@ static void interactive_main_loop(void)
 			quit_cmd();
 		}
 
-		free(chosen);
-		chosen = NULL;
+		FREE_AND_NULL(chosen);
 		break;
 	}
 }
builtin/config.c +2 -4
index 7f6c25d4d9..18c2158630 100644
@@ -214,8 +214,7 @@ static int get_value(const char *key_, const char *regex_)
 		key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(key_regexp, key, REG_EXTENDED)) {
 			error("invalid key pattern: %s", key_);
-			free(key_regexp);
-			key_regexp = NULL;
+			FREE_AND_NULL(key_regexp);
 			ret = CONFIG_INVALID_PATTERN;
 			goto free_strings;
 		}
@@ -235,8 +234,7 @@ static int get_value(const char *key_, const char *regex_)
 		regexp = (regex_t*)xmalloc(sizeof(regex_t));
 		if (regcomp(regexp, regex_, REG_EXTENDED)) {
 			error("invalid pattern: %s", regex_);
-			free(regexp);
-			regexp = NULL;
+			FREE_AND_NULL(regexp);
 			ret = CONFIG_INVALID_PATTERN;
 			goto free_strings;
 		}
builtin/index-pack.c +2 -4
index 04b9dcaf0f..f3b16520c2 100644
@@ -388,8 +388,7 @@ static struct base_data *alloc_base_data(void)
 static void free_base_data(struct base_data *c)
 {
 	if (c->data) {
-		free(c->data);
-		c->data = NULL;
+		FREE_AND_NULL(c->data);
 		get_thread_data()->base_cache_used -= c->size;
 	}
 }
@@ -605,8 +604,7 @@ static void *unpack_data(struct object_entry *obj,
 	git_inflate_end(&stream);
 	free(inbuf);
 	if (consume) {
-		free(data);
-		data = NULL;
+		FREE_AND_NULL(data);
 	}
 	return data;
 }
builtin/pack-objects.c +4 -8
index f672225def..790615b2c3 100644
@@ -264,8 +264,7 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 		 * make sure no cached delta data remains from a
 		 * previous attempt before a pack split occurred.
 		 */
-		free(entry->delta_data);
-		entry->delta_data = NULL;
+		FREE_AND_NULL(entry->delta_data);
 		entry->z_delta_size = 0;
 	} else if (entry->delta_data) {
 		size = entry->delta_size;
@@ -1375,12 +1374,10 @@ static void cleanup_preferred_base(void)
 		if (!pbase_tree_cache[i])
 			continue;
 		free(pbase_tree_cache[i]->tree_data);
-		free(pbase_tree_cache[i]);
-		pbase_tree_cache[i] = NULL;
+		FREE_AND_NULL(pbase_tree_cache[i]);
 	}
 
-	free(done_pbase_paths);
-	done_pbase_paths = NULL;
+	FREE_AND_NULL(done_pbase_paths);
 	done_pbase_paths_num = done_pbase_paths_alloc = 0;
 }
 
@@ -1970,8 +1967,7 @@ static unsigned long free_unpacked(struct unpacked *n)
 	n->index = NULL;
 	if (n->data) {
 		freed_mem += n->entry->size;
-		free(n->data);
-		n->data = NULL;
+		FREE_AND_NULL(n->data);
 	}
 	n->entry = NULL;
 	n->depth = 0;
builtin/unpack-objects.c +1 -2
index 8bc9997767..60bfe48789 100644
@@ -112,8 +112,7 @@ static void *get_data(unsigned long size)
 			break;
 		if (ret != Z_OK) {
 			error("inflate returned %d", ret);
-			free(buf);
-			buf = NULL;
+			FREE_AND_NULL(buf);
 			if (!recover)
 				exit(1);
 			has_errors = 1;
builtin/worktree.c +2 -4
index 793306ea51..41d1c007a4 100644
@@ -299,10 +299,8 @@ static int add_worktree(const char *path, const char *refname,
 	}
 
 	is_junk = 0;
-	free(junk_work_tree);
-	free(junk_git_dir);
-	junk_work_tree = NULL;
-	junk_git_dir = NULL;
+	FREE_AND_NULL(junk_work_tree);
+	FREE_AND_NULL(junk_git_dir);
 
 done:
 	if (ret || !opts->keep_locked) {
commit-slab.h +1 -2
index 42d16dcded..3286c3581b 100644
@@ -82,8 +82,7 @@ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s)		\
 	for (i = 0; i < s->slab_count; i++)				\
 		free(s->slab[i]);					\
 	s->slab_count = 0;						\
-	free(s->slab);							\
-	s->slab = NULL;							\
+	FREE_AND_NULL(s->slab);							\
 }									\
 									\
 static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s,	\
commit.c +1 -2
index 99846d9bf4..cbfd689939 100644
@@ -287,8 +287,7 @@ void free_commit_buffer(struct commit *commit)
 {
 	struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
 	if (v) {
-		free(v->buffer);
-		v->buffer = NULL;
+		FREE_AND_NULL(v->buffer);
 		v->size = 0;
 	}
 }
config.c +1 -2
index 34a139c40b..0501a60dd2 100644
@@ -395,8 +395,7 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *basele
 
 out_free_ret_1:
 	if (store_key) {
-		free(*store_key);
-		*store_key = NULL;
+		FREE_AND_NULL(*store_key);
 	}
 	return -CONFIG_INVALID_KEY;
 }
credential.c +3 -6
index aa996669fc..924ab58538 100644
@@ -93,8 +93,7 @@ static void credential_apply_config(struct credential *c)
 	c->configured = 1;
 
 	if (!c->use_http_path && proto_is_http(c->protocol)) {
-		free(c->path);
-		c->path = NULL;
+		FREE_AND_NULL(c->path);
 	}
 }
 
@@ -314,10 +313,8 @@ void credential_reject(struct credential *c)
 	for (i = 0; i < c->helpers.nr; i++)
 		credential_do(c, c->helpers.items[i].string, "erase");
 
-	free(c->username);
-	c->username = NULL;
-	free(c->password);
-	c->password = NULL;
+	FREE_AND_NULL(c->username);
+	FREE_AND_NULL(c->password);
 	c->approved = 0;
 }
 
diff-lib.c +1 -2
index 76c8f185cd..7528f4163f 100644
@@ -179,8 +179,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
 				free(dpath);
 				continue;
 			}
-			free(dpath);
-			dpath = NULL;
+			FREE_AND_NULL(dpath);
 
 			/*
 			 * Show the diff for the 'ce' if we found the one
diff.c +2 -4
index e35cf6c704..3561adf49f 100644
@@ -1218,8 +1218,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
 			regfree(ecbdata->diff_words->word_regex);
 			free(ecbdata->diff_words->word_regex);
 		}
-		free(ecbdata->diff_words);
-		ecbdata->diff_words = NULL;
+		FREE_AND_NULL(ecbdata->diff_words);
 	}
 }
 
@@ -2951,8 +2950,7 @@ void diff_free_filespec_blob(struct diff_filespec *s)
 void diff_free_filespec_data(struct diff_filespec *s)
 {
 	diff_free_filespec_blob(s);
-	free(s->cnt_data);
-	s->cnt_data = NULL;
+	FREE_AND_NULL(s->cnt_data);
 }
 
 static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
diffcore-rename.c +2 -4
index f7444c86bd..6d8daf9c00 100644
@@ -667,11 +667,9 @@ void diffcore_rename(struct diff_options *options)
 	for (i = 0; i < rename_dst_nr; i++)
 		free_filespec(rename_dst[i].two);
 
-	free(rename_dst);
-	rename_dst = NULL;
+	FREE_AND_NULL(rename_dst);
 	rename_dst_nr = rename_dst_alloc = 0;
-	free(rename_src);
-	rename_src = NULL;
+	FREE_AND_NULL(rename_src);
 	rename_src_nr = rename_src_alloc = 0;
 	return;
 }
dir.c +3 -6
index 1759063817..7dc168b806 100644
@@ -2117,8 +2117,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
 		for (i = j = 0; j < dir->nr; j++) {
 			if (i &&
 			    check_dir_entry_contains(dir->entries[i - 1], dir->entries[j])) {
-				free(dir->entries[j]);
-				dir->entries[j] = NULL;
+				FREE_AND_NULL(dir->entries[j]);
 			} else {
 				dir->entries[i++] = dir->entries[j];
 			}
@@ -2144,8 +2143,7 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
 		     dir->untracked->dir_invalidated))
 			istate->cache_changed |= UNTRACKED_CHANGED;
 		if (dir->untracked != istate->untracked) {
-			free(dir->untracked);
-			dir->untracked = NULL;
+			FREE_AND_NULL(dir->untracked);
 		}
 	}
 	return dir->nr;
@@ -2488,8 +2486,7 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra
 	strbuf_addbuf(out, &untracked->ident);
 
 	strbuf_add(out, ouc, ouc_size(len));
-	free(ouc);
-	ouc = NULL;
+	FREE_AND_NULL(ouc);
 
 	if (!untracked->root) {
 		varint_len = encode_varint(0, varbuf);
fast-import.c +2 -4
index 9a22fc92c0..eeab927d5a 100644
@@ -1063,8 +1063,7 @@ static void end_packfile(void)
 		close(pack_data->pack_fd);
 		unlink_or_warn(pack_data->pack_name);
 	}
-	free(pack_data);
-	pack_data = NULL;
+	FREE_AND_NULL(pack_data);
 	running = 0;
 
 	/* We can't carry a delta across packfiles. */
@@ -1149,8 +1148,7 @@ static int store_object(
 
 		/* We cannot carry a delta into the new pack. */
 		if (delta) {
-			free(delta);
-			delta = NULL;
+			FREE_AND_NULL(delta);
 
 			git_deflate_init(&s, pack_compression_level);
 			s.next_in = (void *)dat->buf;
gpg-interface.c +5 -10
index e44cc27da1..3f377f89d0 100644
@@ -13,16 +13,11 @@ static const char *gpg_program = "gpg";
 
 void signature_check_clear(struct signature_check *sigc)
 {
-	free(sigc->payload);
-	free(sigc->gpg_output);
-	free(sigc->gpg_status);
-	free(sigc->signer);
-	free(sigc->key);
-	sigc->payload = NULL;
-	sigc->gpg_output = NULL;
-	sigc->gpg_status = NULL;
-	sigc->signer = NULL;
-	sigc->key = NULL;
+	FREE_AND_NULL(sigc->payload);
+	FREE_AND_NULL(sigc->gpg_output);
+	FREE_AND_NULL(sigc->gpg_status);
+	FREE_AND_NULL(sigc->signer);
+	FREE_AND_NULL(sigc->key);
 }
 
 static struct {
grep.c +4 -8
index d03d424e5c..909f35a984 100644
@@ -1763,12 +1763,9 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
 
 void grep_source_clear(struct grep_source *gs)
 {
-	free(gs->name);
-	gs->name = NULL;
-	free(gs->path);
-	gs->path = NULL;
-	free(gs->identifier);
-	gs->identifier = NULL;
+	FREE_AND_NULL(gs->name);
+	FREE_AND_NULL(gs->path);
+	FREE_AND_NULL(gs->identifier);
 	grep_source_clear_data(gs);
 }
 
@@ -1778,8 +1775,7 @@ void grep_source_clear_data(struct grep_source *gs)
 	case GREP_SOURCE_FILE:
 	case GREP_SOURCE_SHA1:
 	case GREP_SOURCE_SUBMODULE:
-		free(gs->buf);
-		gs->buf = NULL;
+		FREE_AND_NULL(gs->buf);
 		gs->size = 0;
 		break;
 	case GREP_SOURCE_BUF:
help.c +1 -2
index db7f3d79a0..a2f48c0df3 100644
@@ -267,9 +267,8 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
 
 	for (i = 0; i < old->cnt; i++)
 		cmds->names[cmds->cnt++] = old->names[i];
-	free(old->names);
+	FREE_AND_NULL(old->names);
 	old->cnt = 0;
-	old->names = NULL;
 }
 
 /* An empirically derived magic number */
http-push.c +8 -16
index 67c4d4b472..c91f40a610 100644
@@ -291,8 +291,7 @@ static void start_mkcol(struct transfer_request *request)
 		request->state = RUN_MKCOL;
 	} else {
 		request->state = ABORTED;
-		free(request->url);
-		request->url = NULL;
+		FREE_AND_NULL(request->url);
 	}
 }
 #endif
@@ -409,8 +408,7 @@ static void start_put(struct transfer_request *request)
 		request->state = RUN_PUT;
 	} else {
 		request->state = ABORTED;
-		free(request->url);
-		request->url = NULL;
+		FREE_AND_NULL(request->url);
 	}
 }
 
@@ -432,8 +430,7 @@ static void start_move(struct transfer_request *request)
 		request->state = RUN_MOVE;
 	} else {
 		request->state = ABORTED;
-		free(request->url);
-		request->url = NULL;
+		FREE_AND_NULL(request->url);
 	}
 }
 
@@ -526,8 +523,7 @@ static void finish_request(struct transfer_request *request)
 
 	/* URL is reused for MOVE after PUT */
 	if (request->state != RUN_PUT) {
-		free(request->url);
-		request->url = NULL;
+		FREE_AND_NULL(request->url);
 	}
 
 	if (request->state == RUN_MKCOL) {
@@ -803,8 +799,7 @@ xml_start_tag(void *userData, const char *name, const char **atts)
 	}
 	xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
 
-	free(ctx->cdata);
-	ctx->cdata = NULL;
+	FREE_AND_NULL(ctx->cdata);
 
 	ctx->userFunc(ctx, 0);
 }
@@ -932,8 +927,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
 		free(lock->token);
 		free(lock->owner);
 		free(url);
-		free(lock);
-		lock = NULL;
+		FREE_AND_NULL(lock);
 	} else {
 		lock->url = url;
 		lock->start_time = time(NULL);
@@ -1105,8 +1099,7 @@ static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
 			ls->dentry_flags |= IS_DIR;
 		}
 	} else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
-		free(ls->dentry_name);
-		ls->dentry_name = NULL;
+		FREE_AND_NULL(ls->dentry_name);
 		ls->dentry_flags = 0;
 	}
 }
@@ -1547,8 +1540,7 @@ static void fetch_symref(const char *path, char **symref, struct object_id *oid)
 		    curl_errorstr);
 	free(url);
 
-	free(*symref);
-	*symref = NULL;
+	FREE_AND_NULL(*symref);
 	oidclr(oid);
 
 	if (buffer.len == 0)
http.c +5 -10
index d2e11ec6f0..77f46bef0f 100644
@@ -1026,8 +1026,7 @@ void http_cleanup(void)
 
 	if (proxy_auth.password) {
 		memset(proxy_auth.password, 0, strlen(proxy_auth.password));
-		free(proxy_auth.password);
-		proxy_auth.password = NULL;
+		FREE_AND_NULL(proxy_auth.password);
 	}
 
 	free((void *)curl_proxyuserpwd);
@@ -1038,13 +1037,11 @@ void http_cleanup(void)
 
 	if (cert_auth.password != NULL) {
 		memset(cert_auth.password, 0, strlen(cert_auth.password));
-		free(cert_auth.password);
-		cert_auth.password = NULL;
+		FREE_AND_NULL(cert_auth.password);
 	}
 	ssl_cert_password_required = 0;
 
-	free(cached_accept_language);
-	cached_accept_language = NULL;
+	FREE_AND_NULL(cached_accept_language);
 }
 
 struct active_request_slot *get_active_slot(void)
@@ -1896,8 +1893,7 @@ static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
 
 	if (http_get_file(url, tmp, NULL) != HTTP_OK) {
 		error("Unable to get pack index %s", url);
-		free(tmp);
-		tmp = NULL;
+		FREE_AND_NULL(tmp);
 	}
 
 	free(url);
@@ -2328,8 +2324,7 @@ void release_http_object_request(struct http_object_request *freq)
 		freq->localfile = -1;
 	}
 	if (freq->url != NULL) {
-		free(freq->url);
-		freq->url = NULL;
+		FREE_AND_NULL(freq->url);
 	}
 	if (freq->slot != NULL) {
 		freq->slot->callback_func = NULL;
imap-send.c +1 -2
index 857591660f..40ddb496e6 100644
@@ -776,8 +776,7 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
 			       offsetof(struct imap_cmd, next));
 			if (cmdp->cb.data) {
 				n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
-				free(cmdp->cb.data);
-				cmdp->cb.data = NULL;
+				FREE_AND_NULL(cmdp->cb.data);
 				if (n != (int)cmdp->cb.dlen)
 					return RESP_BAD;
 			} else if (cmdp->cb.cont) {
line-log.c +2 -4
index b9087814b8..2d60c347ca 100644
@@ -34,9 +34,8 @@ void range_set_init(struct range_set *rs, size_t prealloc)
 
 void range_set_release(struct range_set *rs)
 {
-	free(rs->ranges);
+	FREE_AND_NULL(rs->ranges);
 	rs->alloc = rs->nr = 0;
-	rs->ranges = NULL;
 }
 
 /* dst must be uninitialized! */
@@ -610,8 +609,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
 		line_log_data_insert(&ranges, full_name, begin, end);
 
 		free_filespec(spec);
-		free(ends);
-		ends = NULL;
+		FREE_AND_NULL(ends);
 	}
 
 	for (p = ranges; p; p = p->next)
ll-merge.c +1 -2
index ac0d4a5d78..91ff519cef 100644
@@ -209,8 +209,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
 	result->size = st.st_size;
 	result->ptr = xmallocz(result->size);
 	if (read_in_full(fd, result->ptr, result->size) != result->size) {
-		free(result->ptr);
-		result->ptr = NULL;
+		FREE_AND_NULL(result->ptr);
 		result->size = 0;
 	}
  close_bad:
mailinfo.c +1 -2
index f92cb9f729..44f829edb1 100644
@@ -919,8 +919,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
 		/* we hit an end boundary */
 		/* pop the current boundary off the stack */
 		strbuf_release(*(mi->content_top));
-		free(*(mi->content_top));
-		*(mi->content_top) = NULL;
+		FREE_AND_NULL(*(mi->content_top));
 
 		/* technically won't happen as is_multipart_boundary()
 		   will fail first.  But just in case..
object.c +1 -2
index 06ba3a11d8..f818777412 100644
@@ -377,8 +377,7 @@ void object_array_clear(struct object_array *array)
 	int i;
 	for (i = 0; i < array->nr; i++)
 		object_array_release_entry(&array->objects[i]);
-	free(array->objects);
-	array->objects = NULL;
+	FREE_AND_NULL(array->objects);
 	array->nr = array->alloc = 0;
 }
 
pathspec.c +1 -2
index 828405021f..54aab8bc01 100644
@@ -662,7 +662,6 @@ void clear_pathspec(struct pathspec *pathspec)
 			attr_check_free(pathspec->items[i].attr_check);
 	}
 
-	free(pathspec->items);
-	pathspec->items = NULL;
+	FREE_AND_NULL(pathspec->items);
 	pathspec->nr = 0;
 }
prio-queue.c +1 -2
index fc3860fdcb..126d096727 100644
@@ -27,10 +27,9 @@ void prio_queue_reverse(struct prio_queue *queue)
 
 void clear_prio_queue(struct prio_queue *queue)
 {
-	free(queue->array);
+	FREE_AND_NULL(queue->array);
 	queue->nr = 0;
 	queue->alloc = 0;
-	queue->array = NULL;
 	queue->insertion_ctr = 0;
 }
 
read-cache.c +2 -4
index bc156a133e..66c413657e 100644
@@ -1888,8 +1888,7 @@ int discard_index(struct index_state *istate)
 	free_name_hash(istate);
 	cache_tree_free(&(istate->cache_tree));
 	istate->initialized = 0;
-	free(istate->cache);
-	istate->cache = NULL;
+	FREE_AND_NULL(istate->cache);
 	istate->cache_alloc = 0;
 	discard_split_index(istate);
 	free_untracked_cache(istate->untracked);
@@ -2603,8 +2602,7 @@ void *read_blob_data_from_index(const struct index_state *istate,
 
 void stat_validity_clear(struct stat_validity *sv)
 {
-	free(sv->sd);
-	sv->sd = NULL;
+	FREE_AND_NULL(sv->sd);
 }
 
 int stat_validity_check(struct stat_validity *sv, const char *path)
ref-filter.c +1 -2
index ab32bc9c31..72e6cb8ecc 100644
@@ -1891,8 +1891,7 @@ void ref_array_clear(struct ref_array *array)
 
 	for (i = 0; i < array->nr; i++)
 		free_array_item(array->items[i]);
-	free(array->items);
-	array->items = NULL;
+	FREE_AND_NULL(array->items);
 	array->nr = array->alloc = 0;
 }
 
refs/files-backend.c +1 -2
index d8b3f73147..c1bd99d60f 100644
@@ -2944,8 +2944,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
 				       head_oid.hash, &head_type);
 
 	if (head_ref && !(head_type & REF_ISSYMREF)) {
-		free(head_ref);
-		head_ref = NULL;
+		FREE_AND_NULL(head_ref);
 	}
 
 	/*
refs/ref-cache.c +1 -2
index af2fcb2c12..76bb723c86 100644
@@ -82,9 +82,8 @@ static void clear_ref_dir(struct ref_dir *dir)
 	int i;
 	for (i = 0; i < dir->nr; i++)
 		free_ref_entry(dir->entries[i]);
-	free(dir->entries);
+	FREE_AND_NULL(dir->entries);
 	dir->sorted = dir->nr = dir->alloc = 0;
-	dir->entries = NULL;
 }
 
 struct ref_entry *create_dir_entry(struct ref_cache *cache,
remote-testsvn.c +1 -2
index 50404ef343..078b0c3139 100644
@@ -66,8 +66,7 @@ static char *read_ref_note(const unsigned char sha1[20])
 	else if (!msglen || type != OBJ_BLOB) {
 		error("Note contains unusable content. "
 			"Is something else using this notes tree? %s", notes_ref);
-		free(msg);
-		msg = NULL;
+		FREE_AND_NULL(msg);
 	}
 	free_notes(NULL);
 	return msg;
rerere.c +1 -2
index c26c29f87a..61f3804a1e 100644
@@ -39,9 +39,8 @@ static void free_rerere_dirs(void)
 		free(rerere_dir[i]->status);
 		free(rerere_dir[i]);
 	}
-	free(rerere_dir);
+	FREE_AND_NULL(rerere_dir);
 	rerere_dir_nr = rerere_dir_alloc = 0;
-	rerere_dir = NULL;
 }
 
 static void free_rerere_id(struct string_list_item *item)
sequencer.c +1 -2
index 5282fb849c..070b42f240 100644
@@ -1211,8 +1211,7 @@ struct todo_list {
 static void todo_list_release(struct todo_list *todo_list)
 {
 	strbuf_release(&todo_list->buf);
-	free(todo_list->items);
-	todo_list->items = NULL;
+	FREE_AND_NULL(todo_list->items);
 	todo_list->nr = todo_list->alloc = 0;
 }
 
sha1-array.c +1 -2
index 7d646ab5b8..838b3bf847 100644
@@ -35,8 +35,7 @@ int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
 
 void oid_array_clear(struct oid_array *array)
 {
-	free(array->oid);
-	array->oid = NULL;
+	FREE_AND_NULL(array->oid);
 	array->nr = 0;
 	array->alloc = 0;
 	array->sorted = 0;
sha1_file.c +1 -2
index 59a4ed2ed3..d67c88bb8d 100644
@@ -610,8 +610,7 @@ char *compute_alternate_path(const char *path, struct strbuf *err)
 
 out:
 	if (seen_error) {
-		free(ref_git);
-		ref_git = NULL;
+		FREE_AND_NULL(ref_git);
 	}
 
 	return ref_git;
split-index.c +1 -2
index 49bd197f71..20477c6a48 100644
@@ -174,10 +174,9 @@ void merge_base_index(struct index_state *istate)
 
 	ewah_free(si->delete_bitmap);
 	ewah_free(si->replace_bitmap);
-	free(si->saved_cache);
+	FREE_AND_NULL(si->saved_cache);
 	si->delete_bitmap  = NULL;
 	si->replace_bitmap = NULL;
-	si->saved_cache	   = NULL;
 	si->saved_cache_nr = 0;
 }
 
transport-helper.c +9 -18
index 36408046eb..33cff38cc0 100644
@@ -242,8 +242,7 @@ static int disconnect_helper(struct transport *transport)
 		close(data->helper->out);
 		fclose(data->out);
 		res = finish_command(data->helper);
-		free(data->helper);
-		data->helper = NULL;
+		FREE_AND_NULL(data->helper);
 	}
 	return res;
 }
@@ -711,43 +710,35 @@ static int push_update_ref_status(struct strbuf *buf,
 
 		if (!strcmp(msg, "no match")) {
 			status = REF_STATUS_NONE;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 		else if (!strcmp(msg, "up to date")) {
 			status = REF_STATUS_UPTODATE;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 		else if (!strcmp(msg, "non-fast forward")) {
 			status = REF_STATUS_REJECT_NONFASTFORWARD;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 		else if (!strcmp(msg, "already exists")) {
 			status = REF_STATUS_REJECT_ALREADY_EXISTS;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 		else if (!strcmp(msg, "fetch first")) {
 			status = REF_STATUS_REJECT_FETCH_FIRST;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 		else if (!strcmp(msg, "needs force")) {
 			status = REF_STATUS_REJECT_NEEDS_FORCE;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 		else if (!strcmp(msg, "stale info")) {
 			status = REF_STATUS_REJECT_STALE;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 		else if (!strcmp(msg, "forced update")) {
 			forced = 1;
-			free(msg);
-			msg = NULL;
+			FREE_AND_NULL(msg);
 		}
 	}
 
transport.c +1 -2
index 9bfcf870f9..cf35088001 100644
@@ -1145,8 +1145,7 @@ void transport_unlock_pack(struct transport *transport)
 {
 	if (transport->pack_lockfile) {
 		unlink_or_warn(transport->pack_lockfile);
-		free(transport->pack_lockfile);
-		transport->pack_lockfile = NULL;
+		FREE_AND_NULL(transport->pack_lockfile);
 	}
 }
 
tree-diff.c +2 -4
index e164e532b2..1e1b393d08 100644
@@ -140,8 +140,7 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
 	/* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
 	p = last->next;
 	if (p && (alloclen > (intptr_t)p->next)) {
-		free(p);
-		p = NULL;
+		FREE_AND_NULL(p);
 	}
 
 	if (!p) {
@@ -559,8 +558,7 @@ struct combine_diff_path *diff_tree_paths(
 	 * (see path_appendnew() for details about why)
 	 */
 	if (p->next) {
-		free(p->next);
-		p->next = NULL;
+		FREE_AND_NULL(p->next);
 	}
 
 	return p;
tree-walk.c +1 -2
index 6a42e402b0..9f67b89c87 100644
@@ -479,8 +479,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
 	for (i = 0; i < n; i++)
 		free_extended_entry(tx + i);
 	free(tx);
-	free(traverse_path);
-	info->traverse_path = NULL;
+	FREE_AND_NULL(traverse_path);
 	strbuf_release(&base);
 	return error;
 }
tree.c +1 -2
index 603b29ee80..614f91586d 100644
@@ -226,8 +226,7 @@ int parse_tree_gently(struct tree *item, int quiet_on_missing)
 
 void free_tree_buffer(struct tree *tree)
 {
-	free(tree->buffer);
-	tree->buffer = NULL;
+	FREE_AND_NULL(tree->buffer);
 	tree->size = 0;
 	tree->object.parsed = 0;
 }
Signature · 1 line
2.13.1.508.gb3defc5cc
#82 lore raw

Re: [PATCH v3 0/2] Add a FREE_AND_NULL() wrapper macro

In reply to Ævar Arnfjörð Bjarmason
Quoted 7 lines · Ævar Arnfjörð Bjarmason

On Thu, Jun 15 2017, Ævar Arnfjörð Bjarmason jotted:

Quoted 5 lines

I'll change it to FREE_AND_NULL and submit my patch as-is, my reading of the rest of this thread is that making it a function instead of a macro would be interesting, but has its own caveats that are likely better considered as part of its own series, whereas this just changes existing code to its macro-expanded functional equivalent.

Quoted 7 lines

Here's v3 with that change. Nothing but the macro name (and comments, commit messages etc. referring to it) have changed.

Ævar Arnfjörð Bjarmason (2):
git-compat-util: add a FREE_AND_NULL() wrapper around free(ptr); ptr =
NULL
*.[ch] refactoring: make use of the FREE_AND_NULL() macro

Thanks.

Perhaps somebody wants to do a follow-up patch on top of these two patches to add .cocci rule e.g.

	@@
	type T;
	T *ptr;
	@@
	- free(ptr);
	- ptr = NULL;
	+ FREE_AND_NULL(ptr);

so that we can periodically sweep new candidates, to which this macro can be applied?

#83 lore raw

v4 cover Add a FREE_AND_NULL() wrapper macro

I didn't know about the coccinelle semantic patch facility. This adds coccinelle rules to do the code changes, and adds subsequent follow-up commits which apply that change, with the series ending with a patch that I manually authored.

This fixes a bug in earlier versions of the series. I was mistakenly freeing the wrong thing in the tree-walk.c change, that change is now gone, and all the changes not made by coccinelle are in one commit, making it easier to review.

Ævar Arnfjörð Bjarmason (6):
git-compat-util: add a FREE_AND_NULL() wrapper around free(ptr); ptr =
NULL
coccinelle: add a rule to make "type" code use FREE_AND_NULL()
coccinelle: make use of the "type" FREE_AND_NULL() rule
coccinelle: add a rule to make "expression" code use FREE_AND_NULL()
coccinelle: make use of the "expression" FREE_AND_NULL() rule *.[ch] refactoring: make use of the FREE_AND_NULL() macro

 alias.c                       |  6 ++----
 apply.c                       |  3 +--
 attr.c                        |  6 ++----
 blame.c                       |  3 +--
 branch.c                      |  3 +--
 builtin/am.c                  | 18 +++++-------------
 builtin/clean.c               |  6 ++----
 builtin/config.c              |  6 ++----
 builtin/index-pack.c          |  6 ++----
 builtin/pack-objects.c        | 12 ++++--------
 builtin/unpack-objects.c      |  3 +--
 builtin/worktree.c            |  6 ++----
 commit-slab.h                 |  3 +--
 commit.c                      |  3 +--
 config.c                      |  3 +--
 contrib/coccinelle/free.cocci | 15 +++++++++++++++
 credential.c                  |  9 +++------
 diff-lib.c                    |  3 +--
 diff.c                        |  6 ++----
 diffcore-rename.c             |  6 ++----
 dir.c                         |  9 +++------
 fast-import.c                 |  6 ++----
 git-compat-util.h             |  6 ++++++
 gpg-interface.c               | 15 +++++----------
 grep.c                        | 12 ++++--------
 help.c                        |  3 +--
 http-push.c                   | 24 ++++++++----------------
 http.c                        | 15 +++++----------
 imap-send.c                   |  3 +--
 line-log.c                    |  6 ++----
 ll-merge.c                    |  3 +--
 mailinfo.c                    |  3 +--
 object.c                      |  3 +--
 pathspec.c                    |  3 +--
 prio-queue.c                  |  3 +--
 read-cache.c                  |  6 ++----
 ref-filter.c                  |  3 +--
 refs/files-backend.c          |  3 +--
 refs/ref-cache.c              |  3 +--
 remote-testsvn.c              |  3 +--
 rerere.c                      |  3 +--
 sequencer.c                   |  3 +--
 sha1-array.c                  |  3 +--
 sha1_file.c                   |  3 +--
 split-index.c                 |  3 +--
 transport-helper.c            | 27 +++++++++------------------
 transport.c                   |  3 +--
 tree-diff.c                   |  6 ++----
 tree.c                        |  3 +--
 49 files changed, 117 insertions(+), 195 deletions(-)
Signature · 1 line
2.13.1.508.gb3defc5cc
#84 lore raw

PATCH v4 1/6 git-compat-util: add a FREE_AND_NULL() wrapper around free(ptr); ptr = NULL

Add a FREE_AND_NULL() wrapper marco for the common pattern of freeing a pointer and assigning NULL to it right afterwards.

The implementation is similar to the (currently unused) XDL_PTRFREE macro in xdiff/xmacros.h added in commit 3443546f6e ("Use a *real* built-in diff generator", 2006-03-24). The only difference is that free() is called unconditionally, see [1].

See [2] for a suggested alternative which does this via a function instead of a macro. As covered in replies to that message, while it's a viable approach, it would introduce caveats which this approach doesn't have, so that potential change is left to a future follow-up change.

This merely allows us to translate exactly what we're doing now to a less verbose & idiomatic form using a macro, while guaranteeing that we don't introduce any functional changes.

1. <alpine.DEB.2.20.1608301948310.129229@virtualbox>
(http://public-inbox.org/git/alpine.DEB.2.20.1608301948310.129229@virtualbox/)

2. <20170610032143.GA7880@starla>
(https://public-inbox.org/git/20170610032143.GA7880@starla/)

---
 git-compat-util.h | 6 ++++++
 1 file changed, 6 insertions(+)
git-compat-util.h +6 -0
index 51ba4e6b3b..047172d173 100644
@@ -808,6 +808,12 @@ extern char *xgetcwd(void);
 extern FILE *fopen_for_writing(const char *path);
 extern FILE *fopen_or_warn(const char *path, const char *mode);
 
+/*
+ * 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 REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
 
Signature · 1 line
2.13.1.508.gb3defc5cc
#85 lore raw

PATCH v4 2/6 coccinelle: add a rule to make "type" code use FREE_AND_NULL()

In reply to Ævar Arnfjörð Bjarmason
---
 contrib/coccinelle/free.cocci | 8 ++++++++
 1 file changed, 8 insertions(+)
contrib/coccinelle/free.cocci +8 -0
index c03ba737e5..35fb992621 100644
@@ -9,3 +9,11 @@ expression E;
 @@
 - if (!E)
   free(E);
+
+@@
+type T;
+T *ptr;
+@@
+- free(ptr);
+- ptr = NULL;
++ FREE_AND_NULL(ptr);
Signature · 1 line
2.13.1.508.gb3defc5cc