The global variable 'merge_log_config', set via the "merge.log" or "merge.summary" settings, is only used in 'cmd_fmt_merge_msg()' and 'cmd_merge()' to adjust the 'shortlog_len' variable. Remove 'merge_log_config' and introduce a function 'adjust_shortlog_len()' in fmt-merge-msg.c to handle the 'shortlog_len' variable. This change is part of an ongoing effort to eliminate global variables, improve modularity and help libify the codebase. Mentored-by: Christian Couder <christian.couder@xxxxxxxxx> Mentored-by: Ghanshyam Thakkar <shyamthakkar001@xxxxxxxxx> Signed-off-by: Ayush Chandekar <ayu.chandekar@xxxxxxxxx> --- builtin/fmt-merge-msg.c | 4 ++-- builtin/merge.c | 4 ++-- environment.c | 2 -- fmt-merge-msg.c | 30 ++++++++++++++++++++++-------- fmt-merge-msg.h | 3 ++- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c index 3b6aac2cf7..fed8163825 100644 --- a/builtin/fmt-merge-msg.c +++ b/builtin/fmt-merge-msg.c @@ -58,8 +58,8 @@ int cmd_fmt_merge_msg(int argc, 0); if (argc > 0) usage_with_options(fmt_merge_msg_usage, options); - if (shortlog_len < 0) - shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; + + adjust_shortlog_len(the_repository, &shortlog_len); if (inpath && strcmp(inpath, "-")) { in = fopen(inpath, "r"); diff --git a/builtin/merge.c b/builtin/merge.c index 18b22c0a26..e1cf2a6d63 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -1403,8 +1403,8 @@ int cmd_merge(int argc, parse_branch_merge_options(branch_mergeoptions); argc = parse_options(argc, argv, prefix, builtin_merge_options, builtin_merge_usage, 0); - if (shortlog_len < 0) - shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; + + adjust_shortlog_len(the_repository, &shortlog_len); if (verbosity < 0 && show_progress == -1) show_progress = 0; diff --git a/environment.c b/environment.c index 7c2480b22e..74c9838b31 100644 --- a/environment.c +++ b/environment.c @@ -20,7 +20,6 @@ #include "repository.h" #include "config.h" #include "refs.h" -#include "fmt-merge-msg.h" #include "commit.h" #include "strvec.h" #include "path.h" @@ -66,7 +65,6 @@ int grafts_keep_true_parents; int core_apply_sparse_checkout; int core_sparse_checkout_cone; int sparse_expect_files_outside_of_patterns; -int merge_log_config = -1; int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */ unsigned long pack_size_limit_cfg; int max_allowed_tree_depth = diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c index 40174efa3d..2ceaebb0ce 100644 --- a/fmt-merge-msg.c +++ b/fmt-merge-msg.c @@ -26,14 +26,7 @@ static struct string_list suppress_dest_patterns = STRING_LIST_INIT_DUP; int fmt_merge_msg_config(const char *key, const char *value, const struct config_context *ctx, void *cb) { - if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) { - int is_bool; - merge_log_config = git_config_bool_or_int(key, value, ctx->kvi, &is_bool); - if (!is_bool && merge_log_config < 0) - return error("%s: negative length %s", key, value); - if (is_bool && merge_log_config) - merge_log_config = DEFAULT_MERGE_LOG_LEN; - } else if (!strcmp(key, "merge.branchdesc")) { + if (!strcmp(key, "merge.branchdesc")) { use_branch_desc = git_config_bool(key, value); } else if (!strcmp(key, "merge.suppressdest")) { if (!value) @@ -645,6 +638,27 @@ static void find_merge_parents(struct merge_parents *result, result->nr = j; } +void adjust_shortlog_len(struct repository *r, int *shortlog_len) +{ + const char *keys[] = { "merge.log", "merge.summary", NULL}; + + if (*shortlog_len >= 0) + return; + + for (const char **key = keys; *key; ++key) { + int is_bool, value; + if (!repo_config_get_bool_or_int(r, *key, &is_bool, &value)) { + if (!is_bool && value < 0) { + error("%s: negative length %d", *key, value); + return; + } + *shortlog_len = (is_bool && value) ? DEFAULT_MERGE_LOG_LEN : value; + return; + } + } + + *shortlog_len = 0; +} int fmt_merge_msg(struct strbuf *in, struct strbuf *out, struct fmt_merge_msg_opts *opts) diff --git a/fmt-merge-msg.h b/fmt-merge-msg.h index 73ca3e4465..f54f00d26f 100644 --- a/fmt-merge-msg.h +++ b/fmt-merge-msg.h @@ -2,6 +2,7 @@ #define FMT_MERGE_MSG_H #include "strbuf.h" +#include "repository.h" #define DEFAULT_MERGE_LOG_LEN 20 @@ -12,9 +13,9 @@ struct fmt_merge_msg_opts { const char *into_name; }; -extern int merge_log_config; int fmt_merge_msg_config(const char *key, const char *value, const struct config_context *ctx, void *cb); +void adjust_shortlog_len(struct repository *r, int *shortlog_len); int fmt_merge_msg(struct strbuf *in, struct strbuf *out, struct fmt_merge_msg_opts *); -- 2.49.0