Skip to content

Commit 196b272

Browse files
committed
environment: migrate encoding configs to struct repository
The global variables 'git_commit_encoding' and 'git_log_output_encoding' prevent libification. Migrate these variables into 'struct repository'. Note that these variables are not migrated to 'repo_settings'. As user preferences, they must be parsed eagerly to maintain immediate UX feedback. While the core read and write APIs (updated in previous patches) now utilize the 'struct repository' pointer, the config parsing step ('git_default_i18n_config') currently parses these values into 'the_repository', since safely plumbing a repository pointer through the 'git_default_config' callback machinery is highly invasive due to the diverse and custom structures currently passed via the 'void *cb' parameter. Signed-off-by: Tian Yuchen <a3205153416@gmail.com>
1 parent b5f0335 commit 196b272

20 files changed

Lines changed: 59 additions & 57 deletions

builtin/am.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ static int parse_mail(struct am_state *state, const char *mail)
12161216
setup_mailinfo(the_repository, &mi);
12171217

12181218
if (state->utf8)
1219-
mi.metainfo_charset = get_commit_output_encoding();
1219+
mi.metainfo_charset = get_commit_output_encoding(the_repository);
12201220
else
12211221
mi.metainfo_charset = NULL;
12221222

@@ -1355,7 +1355,7 @@ static void get_commit_info(struct am_state *state, struct commit *commit)
13551355
struct ident_split id;
13561356

13571357
buffer = repo_logmsg_reencode(the_repository, commit, NULL,
1358-
get_commit_output_encoding());
1358+
get_commit_output_encoding(the_repository));
13591359

13601360
ident_line = find_commit_header(buffer, "author", &ident_len);
13611361
if (!ident_line)

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static void get_commit_info(struct commit *commit, struct commit_info *ret)
202202
const char *subject, *encoding;
203203
const char *message;
204204

205-
encoding = get_log_output_encoding();
205+
encoding = get_log_output_encoding(the_repository);
206206
message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
207207
get_ac_line(message, "\nauthor ",
208208
&ret->author, &ret->author_mail,

builtin/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
794794
c = lookup_commit_reference_by_name(squash_message);
795795
if (!c)
796796
die(_("could not lookup commit '%s'"), squash_message);
797-
ctx.output_encoding = get_commit_output_encoding();
797+
ctx.output_encoding = get_commit_output_encoding(the_repository);
798798
repo_format_commit_message(the_repository, c,
799799
"squash! %s\n\n", &sb,
800800
&ctx);
@@ -829,7 +829,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
829829
commit = lookup_commit_reference_by_name(fixup_commit);
830830
if (!commit)
831831
die(_("could not lookup commit '%s'"), fixup_commit);
832-
ctx.output_encoding = get_commit_output_encoding();
832+
ctx.output_encoding = get_commit_output_encoding(the_repository);
833833
fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
834834
repo_format_commit_message(the_repository, commit, fmt, &sb,
835835
&ctx);
@@ -1235,7 +1235,7 @@ static const char *read_commit_message(const char *name)
12351235
commit = lookup_commit_reference_by_name(name);
12361236
if (!commit)
12371237
die(_("could not lookup commit '%s'"), name);
1238-
out_enc = get_commit_output_encoding();
1238+
out_enc = get_commit_output_encoding(the_repository);
12391239
return repo_logmsg_reencode(the_repository, commit, NULL, out_enc);
12401240
}
12411241

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ static void show_tagger(const char *buf, struct rev_info *rev)
568568

569569
pp.fmt = rev->commit_format;
570570
pp.date_mode = rev->date_mode;
571-
pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
571+
pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding(the_repository));
572572
fprintf(rev->diffopt.file, "%s", out.buf);
573573
strbuf_release(&out);
574574
}

builtin/mailinfo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static int parse_opt_quoted_cr(const struct option *opt, const char *arg, int un
5252
int cmd_mailinfo(int argc,
5353
const char **argv,
5454
const char *prefix,
55-
struct repository *repo UNUSED)
55+
struct repository *repo)
5656
{
5757
struct metainfo_charset meta_charset;
5858
struct mailinfo mi;
@@ -83,7 +83,7 @@ int cmd_mailinfo(int argc,
8383
OPT_END()
8484
};
8585

86-
setup_mailinfo(the_repository, &mi);
86+
setup_mailinfo(repo, &mi);
8787
meta_charset.policy = CHARSET_DEFAULT;
8888

8989
argc = parse_options(argc, argv, prefix, options, mailinfo_usage, 0);
@@ -93,7 +93,7 @@ int cmd_mailinfo(int argc,
9393

9494
switch (meta_charset.policy) {
9595
case CHARSET_DEFAULT:
96-
mi.metainfo_charset = get_commit_output_encoding();
96+
mi.metainfo_charset = get_commit_output_encoding(repo);
9797
break;
9898
case CHARSET_NO_REENCODE:
9999
mi.metainfo_charset = NULL;

builtin/rev-list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static void show_commit(struct commit *commit, void *data)
304304
ctx.date_mode = revs->date_mode;
305305
ctx.date_mode_explicit = revs->date_mode_explicit;
306306
ctx.fmt = revs->commit_format;
307-
ctx.output_encoding = get_log_output_encoding();
307+
ctx.output_encoding = get_log_output_encoding(the_repository);
308308
ctx.color = revs->diffopt.use_color;
309309
ctx.rev = revs;
310310
pretty_print_commit(the_repository, &ctx, commit, &buf);

builtin/shortlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
248248
ctx.fmt = CMIT_FMT_USERFORMAT;
249249
ctx.abbrev = log->abbrev;
250250
ctx.date_mode = log->date_mode;
251-
ctx.output_encoding = get_log_output_encoding();
251+
ctx.output_encoding = get_log_output_encoding(the_repository);
252252

253253
if (!log->summary) {
254254
if (log->user_format)

bundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static void write_bundle_prerequisites(struct commit *commit, void *data)
461461
write_or_die(bpi->fd, buf.buf, buf.len);
462462

463463
ctx.fmt = CMIT_FMT_ONELINE;
464-
ctx.output_encoding = get_log_output_encoding();
464+
ctx.output_encoding = get_log_output_encoding(the_repository);
465465
strbuf_reset(&buf);
466466
pretty_print_commit(the_repository, &ctx, commit, &buf);
467467
strbuf_trim(&buf);

commit.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,8 @@ N_("Warning: commit message did not conform to UTF-8.\n"
16631663
"You may want to amend it after fixing the message, or set the config\n"
16641664
"variable i18n.commitEncoding to the encoding your project uses.\n");
16651665

1666-
static void write_commit_tree(struct strbuf *buffer, const char *msg, size_t msg_len,
1666+
static void write_commit_tree(struct repository *r, struct strbuf *buffer,
1667+
const char *msg, size_t msg_len,
16671668
const struct object_id *tree,
16681669
const struct object_id *parents, size_t parents_len,
16691670
const char *author, const char *committer,
@@ -1673,7 +1674,7 @@ static void write_commit_tree(struct strbuf *buffer, const char *msg, size_t msg
16731674
size_t i;
16741675

16751676
/* Not having i18n.commitencoding is the same as having utf-8 */
1676-
encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1677+
encoding_is_utf8 = is_encoding_utf8(get_commit_output_encoding(r));
16771678

16781679
strbuf_grow(buffer, 8192); /* should avoid reallocs for the headers */
16791680
strbuf_addf(buffer, "tree %s\n", oid_to_hex(tree));
@@ -1694,7 +1695,7 @@ static void write_commit_tree(struct strbuf *buffer, const char *msg, size_t msg
16941695
committer = git_committer_info(IDENT_STRICT);
16951696
strbuf_addf(buffer, "committer %s\n", committer);
16961697
if (!encoding_is_utf8)
1697-
strbuf_addf(buffer, "encoding %s\n", git_commit_encoding);
1698+
strbuf_addf(buffer, "encoding %s\n", get_commit_output_encoding(r));
16981699

16991700
while (extra) {
17001701
add_extra_header(buffer, extra);
@@ -1722,7 +1723,7 @@ int commit_tree_extended(struct repository *r, const char *msg, size_t msg_len,
17221723
size_t i, nparents;
17231724

17241725
/* Not having i18n.commitencoding is the same as having utf-8 */
1725-
encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1726+
encoding_is_utf8 = is_encoding_utf8(get_commit_output_encoding(r));
17261727

17271728
odb_assert_oid_type(the_repository->objects, tree, OBJ_TREE);
17281729

@@ -1735,7 +1736,7 @@ int commit_tree_extended(struct repository *r, const char *msg, size_t msg_len,
17351736
for (const struct commit_list *p = parents; p; p = p->next)
17361737
oidcpy(&parent_buf[i++], &p->item->object.oid);
17371738

1738-
write_commit_tree(&buffer, msg, msg_len, tree, parent_buf, nparents, author, committer, extra);
1739+
write_commit_tree(r, &buffer, msg, msg_len, tree, parent_buf, nparents, author, committer, extra);
17391740
if (sign_commit && sign_commit_to_strbuf(&sig, &buffer, sign_commit)) {
17401741
result = -1;
17411742
goto out;
@@ -1763,7 +1764,7 @@ int commit_tree_extended(struct repository *r, const char *msg, size_t msg_len,
17631764
free(mapped_parents);
17641765
goto out;
17651766
}
1766-
write_commit_tree(&compat_buffer, msg, msg_len, &mapped_tree,
1767+
write_commit_tree(r, &compat_buffer, msg, msg_len, &mapped_tree,
17671768
mapped_parents, nparents, author, committer, compat_extra);
17681769
free_commit_extra_headers(compat_extra);
17691770
free(mapped_parents);

environment.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,14 @@ const char *strip_namespace(const char *namespaced_ref)
197197
return NULL;
198198
}
199199

200-
const char *get_log_output_encoding(void)
200+
const char *get_log_output_encoding(struct repository *r)
201201
{
202-
return git_log_output_encoding ? git_log_output_encoding
203-
: get_commit_output_encoding();
202+
return r->log_output_encoding ? r->log_output_encoding : get_commit_output_encoding(r);
204203
}
205204

206-
const char *get_commit_output_encoding(void)
205+
const char *get_commit_output_encoding(struct repository *r)
207206
{
208-
return git_commit_encoding ? git_commit_encoding : "UTF-8";
207+
return r->commit_encoding ? r->commit_encoding : "UTF-8";
209208
}
210209

211210
int use_optional_locks(void)
@@ -569,16 +568,14 @@ static int git_default_sparse_config(const char *var, const char *value)
569568
static int git_default_i18n_config(const char *var, const char *value)
570569
{
571570
if (!strcmp(var, "i18n.commitencoding")) {
572-
FREE_AND_NULL(git_commit_encoding);
573-
return git_config_string(&git_commit_encoding, var, value);
571+
FREE_AND_NULL(the_repository->commit_encoding);
572+
return git_config_string(&the_repository->commit_encoding, var, value);
574573
}
575-
576574
if (!strcmp(var, "i18n.logoutputencoding")) {
577-
FREE_AND_NULL(git_log_output_encoding);
578-
return git_config_string(&git_log_output_encoding, var, value);
575+
FREE_AND_NULL(the_repository->log_output_encoding);
576+
return git_config_string(&the_repository->log_output_encoding, var, value);
579577
}
580578

581-
/* Add other config variables here and to Documentation/config.adoc. */
582579
return 0;
583580
}
584581

0 commit comments

Comments
 (0)