globals: clean up some global usages
cover globals: clean up some global usages
Hi,
This patchset moves a few of the configuration parsing functions around so that they live in the source file which uses the variables that are initialized through those functions. This allows the variables involved to be static rather than global variables.
Additionally, a global which is only written to is removed (core_compression_level), a file-local variable declared extern is now static, and explicit 0 initialization of globals is removed.
In the course of making this patch, I found a number of other global variables which could be moved into being static if more configuration parsing functions were created and moved to the relevant file instead of being parsed as part of, say, "all core.* settings" in a single function. If wanted, I can prepare a patchset for these as well (or provide the list for discussion of how best to proceed, but it is currently just stored in a WIP commit I have locally as comments on each variable).
Thanks,
--Ben
---
Ben Boeckel (6):
branch: move git_default_branch_config to branch.c
mailmap: move git_default_mailmap_config to mailmap.c
apply: move apply_default_*whitespace to apply.c
config: remove core_compression_level refs/debug: declare file-local variable to be static
globals: remove explicit 0 initialization from globals
apply.c | 3 +++ branch.c | 32 ++++++++++++++++++++++++++++++++ branch.h | 5 +++++ cache.h | 6 ------ config.c | 43 +------------------------------------------ environment.c | 4 ---- mailmap.c | 16 ++++++++++++++-- mailmap.h | 2 ++ object-file.c | 2 +- progress.c | 2 +- refs/debug.c | 4 ++-- 11 files changed, 61 insertions(+), 58 deletions(-)
base-commit: eb27b338a3e71c7c4079fbac8aeae3f8fbb5c687
Signature · 1 line
2.31.1
PATCH 1/6 branch: move `git_default_branch_config` to `branch.c`
This allows branch-specific variables to be tracked locally in branch.c instead of globally in environment.c.
--- branch.c | 32 ++++++++++++++++++++++++++++++++ branch.h | 5 +++++ cache.h | 1 - config.c | 30 ------------------------------ environment.c | 1 - 5 files changed, 37 insertions(+), 32 deletions(-)
branch.c +32 -0
@@ -9,6 +9,38 @@#include "commit.h" #include "worktree.h" +static enum rebase_setup_type autorebase =AUTOREBASE_NEVER ; + +int git_default_branch_config (const char *var,const char *value) +{ +if (!strcmp (var,"branch.autosetupmerge" )) { +if (value && !strcasecmp (value,"always" )) { + git_branch_track =BRANCH_TRACK_ALWAYS ; +return 0 ; + } + git_branch_track =git_config_bool (var, value); +return 0 ; + } +if (!strcmp (var,"branch.autosetuprebase" )) { +if (!value) +return config_error_nonbool (var); +else if (!strcmp (value,"never" )) + autorebase =AUTOREBASE_NEVER ; +else if (!strcmp (value,"local" )) + autorebase =AUTOREBASE_LOCAL ; +else if (!strcmp (value,"remote" )) + autorebase =AUTOREBASE_REMOTE ; +else if (!strcmp (value,"always" )) + autorebase =AUTOREBASE_ALWAYS ; +else +return error (_ ("malformed value for %s" ), var); +return 0 ; + } + +/* Add other config variables here and to Documentation/config.txt. */ +return 0 ; +} +struct tracking {struct refspec_item spec;char *src;
branch.h +5 -0
@@ -17,6 +17,11 @@ extern enum branch_track git_branch_track;/* Functions for acting on the information about branches. */ +/* +* Parses branch-related configuration options. +*/ +int git_default_branch_config (const char *var,const char *value); +/* * Creates a new branch, where: *
cache.h +0 -1
@@ -1030,7 +1030,6 @@ enum push_default_type {PUSH_DEFAULT_UNSPECIFIED }; -extern enum rebase_setup_type autorebase;extern enum push_default_type push_default;enum object_creation_mode {
config.c +0 -30
@@ -1574,36 +1574,6 @@ static int git_default_i18n_config(const char *var, const char *value)return 0 ; } -static int git_default_branch_config (const char *var,const char *value) -{ -if (!strcmp (var,"branch.autosetupmerge" )) { -if (value && !strcasecmp (value,"always" )) { - git_branch_track =BRANCH_TRACK_ALWAYS ; -return 0 ; - } - git_branch_track =git_config_bool (var, value); -return 0 ; - } -if (!strcmp (var,"branch.autosetuprebase" )) { -if (!value) -return config_error_nonbool (var); -else if (!strcmp (value,"never" )) - autorebase =AUTOREBASE_NEVER ; -else if (!strcmp (value,"local" )) - autorebase =AUTOREBASE_LOCAL ; -else if (!strcmp (value,"remote" )) - autorebase =AUTOREBASE_REMOTE ; -else if (!strcmp (value,"always" )) - autorebase =AUTOREBASE_ALWAYS ; -else -return error (_ ("malformed value for %s" ), var); -return 0 ; - } - -/* Add other config variables here and to Documentation/config.txt. */ -return 0 ; -} -static int git_default_push_config (const char *var,const char *value) {if (!strcmp (var,"push.default" )) {
environment.c +0 -1
@@ -60,7 +60,6 @@ int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;char *check_roundtrip_encoding ="SHIFT-JIS" ;unsigned whitespace_rule_cfg =WS_DEFAULT_RULE ;enum branch_track git_branch_track =BRANCH_TRACK_REMOTE ; -enum rebase_setup_type autorebase =AUTOREBASE_NEVER ;enum push_default_type push_default =PUSH_DEFAULT_UNSPECIFIED ;#ifndef OBJECT_CREATION_MODE #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
Signature · 1 line
2.31.1
PATCH 2/6 mailmap: move `git_default_mailmap_config` to `mailmap.c`
In reply to Ben BoeckelThis allows mailmap-specific variables to be tracked locally in mailmap.c instead of globally.
--- cache.h | 2 -- config.c | 12 +----------- mailmap.c | 16 ++++++++++++++-- mailmap.h | 2 ++ 4 files changed, 17 insertions(+), 15 deletions(-)
cache.h +0 -2
@@ -1718,8 +1718,6 @@ int author_ident_sufficiently_given(void);extern const char *git_commit_encoding;extern const char *git_log_output_encoding; -extern const char *git_mailmap_file; -extern const char *git_mailmap_blob;/* IO helper functions */ void maybe_flush_or_die (FILE *,const char *);
config.c +1 -11
@@ -15,6 +15,7 @@#include "strbuf.h" #include "quote.h" #include "hashmap.h" +#include "mailmap.h" #include "string-list.h" #include "object-store.h" #include "utf8.h"
@@ -1603,17 +1604,6 @@ static int git_default_push_config(const char *var, const char *value)return 0 ; } -static int git_default_mailmap_config (const char *var,const char *value) -{ -if (!strcmp (var,"mailmap.file" )) -return git_config_pathname (&git_mailmap_file, var, value); -if (!strcmp (var,"mailmap.blob" )) -return git_config_string (&git_mailmap_blob, var, value); - -/* Add other config variables here and to Documentation/config.txt. */ -return 0 ; -} -int git_default_config (const char *var,const char *value,void *cb) {if (starts_with (var,"core." ))
mailmap.c +14 -2
@@ -1,4 +1,5 @@#include "cache.h" +#include "config.h" #include "string-list.h" #include "mailmap.h" #include "object-store.h"
@@ -12,8 +13,19 @@ static inline void debug_mm(const char *format, ...) {}static inline const char *debug_str (const char *s) {return s; }#endif -const char *git_mailmap_file; -const char *git_mailmap_blob; +static const char *git_mailmap_file; +static const char *git_mailmap_blob; + +int git_default_mailmap_config (const char *var,const char *value) +{ +if (!strcmp (var,"mailmap.file" )) +return git_config_pathname (&git_mailmap_file, var, value); +if (!strcmp (var,"mailmap.blob" )) +return git_config_string (&git_mailmap_blob, var, value); + +/* Add other config variables here and to Documentation/config.txt. */ +return 0 ; +}struct mailmap_info {char *name;
mailmap.h +2 -0
@@ -3,6 +3,8 @@struct string_list ; +int git_default_mailmap_config (const char *var,const char *value); +int read_mailmap (struct string_list *map);void clear_mailmap (struct string_list *map);
Signature · 1 line
2.31.1
PATCH 3/6 apply: move `apply_default_*whitespace` to `apply.c`
In reply to Ben BoeckelThese variables are only set and used in this file, so they can become static.
--- apply.c | 3 +++ cache.h | 2 -- environment.c | 2 -- 3 files changed, 3 insertions(+), 4 deletions(-)
apply.c +3 -0
@@ -23,6 +23,9 @@#include "apply.h" #include "entry.h" +static char *apply_default_whitespace; +static char *apply_default_ignorewhitespace; +struct gitdiff_data {struct strbuf *root;int linenr;
cache.h +0 -2
@@ -944,8 +944,6 @@ extern int assume_unchanged;extern int prefer_symlink_refs;extern int warn_ambiguous_refs;extern int warn_on_object_refname_ambiguity; -extern char *apply_default_whitespace; -extern char *apply_default_ignorewhitespace;extern const char *git_attributes_file;extern const char *git_hooks_path;extern int zlib_compression_level;
environment.c +0 -2
@@ -36,8 +36,6 @@ int repository_format_precious_objects;int repository_format_worktree_config;const char *git_commit_encoding;const char *git_log_output_encoding; -char *apply_default_whitespace; -char *apply_default_ignorewhitespace;const char *git_attributes_file;const char *git_hooks_path;int zlib_compression_level =Z_BEST_SPEED ;
Signature · 1 line
2.31.1
PATCH 4/6 config: remove `core_compression_level`
In reply to Ben BoeckelThis variable is just stored to after parsing core.compression and then ignored, so it can just be completely removed.
--- cache.h | 1 - config.c | 1 - environment.c | 1 - 3 files changed, 3 deletions(-)
cache.h +0 -1
@@ -947,7 +947,6 @@ extern int warn_on_object_refname_ambiguity;extern const char *git_attributes_file;extern const char *git_hooks_path;extern int zlib_compression_level; -extern int core_compression_level;extern int pack_compression_level;extern size_t packed_git_window_size;extern size_t packed_git_limit;
config.c +0 -1
@@ -1401,7 +1401,6 @@ static int git_default_core_config(const char *var, const char *value, void *cb) level =Z_DEFAULT_COMPRESSION ; else if (level < 0 || level > Z_BEST_COMPRESSION)die (_ ("bad zlib compression level %d" ), level); - core_compression_level = level; core_compression_seen =1 ;if (!zlib_compression_seen) zlib_compression_level = level;
environment.c +0 -1
@@ -39,7 +39,6 @@ const char *git_log_output_encoding;const char *git_attributes_file;const char *git_hooks_path;int zlib_compression_level =Z_BEST_SPEED ; -int core_compression_level;int pack_compression_level =Z_DEFAULT_COMPRESSION ;int fsync_object_files;size_t packed_git_window_size =DEFAULT_PACKED_GIT_WINDOW_SIZE ;
Signature · 1 line
2.31.1
PATCH 5/6 refs/debug: declare file-local variable to be static
In reply to Ben BoeckelDeclaring a variable in a source file as extern and then defining it later is not very useful. It can instead be declared as static.
--- refs/debug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
refs/debug.c +2 -2
@@ -9,7 +9,7 @@ struct debug_ref_store {struct ref_store *refs; }; -extern struct ref_storage_be refs_be_debug; +static struct ref_storage_be refs_be_debug;struct ref_store *maybe_debug_wrap_ref_store (const char *gitdir,struct ref_store *store) {
@@ -413,7 +413,7 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,return res; } -struct ref_storage_be refs_be_debug = { +static struct ref_storage_be refs_be_debug = {NULL ,"debug" ,NULL ,
Signature · 1 line
2.31.1
PATCH 6/6 globals: remove explicit `0` initialization from globals
In reply to Ben BoeckelGit relies on implicit zero-initialization of global variables.
--- object-file.c | 2 +- progress.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
object-file.c +1 -1
@@ -1503,7 +1503,7 @@ static int loose_object_info(struct repository *r,return (status <0 ) ? status :0 ; } -int obj_read_use_lock =0 ; +int obj_read_use_lock;pthread_mutex_t obj_read_mutex;void enable_obj_read_lock (void )
progress.c +1 -1
@@ -52,7 +52,7 @@ static volatile sig_atomic_t progress_update;* for 'test-tool progress'. */ int progress_testing; -uint64_t progress_test_ns =0 ; +uint64_t progress_test_ns;void progress_test_force_update (void ) { progress_update =1 ;
Signature · 1 line
2.31.1