Skip to content

Commit 5c32750

Browse files
tboegigitster
authored andcommitted
MacOS: precompose_argv_prefix()
The following sequence leads to a "BUG" assertion running under MacOS: DIR=git-test-restore-p Adiarnfd=$(printf 'A\314\210') DIRNAME=xx${Adiarnfd}yy mkdir $DIR && cd $DIR && git init && mkdir $DIRNAME && cd $DIRNAME && echo "Initial" >file && git add file && echo "One more line" >>file && echo y | git restore -p . Initialized empty Git repository in /tmp/git-test-restore-p/.git/ BUG: pathspec.c:495: error initializing pathspec_item Cannot close git diff-index --cached --numstat [snip] The command `git restore` is run from a directory inside a Git repo. Git needs to split the $CWD into 2 parts: The path to the repo and "the rest", if any. "The rest" becomes a "prefix" later used inside the pathspec code. As an example, "/path/to/repo/dir-inside-repå" would determine "/path/to/repo" as the root of the repo, the place where the configuration file .git/config is found. The rest becomes the prefix ("dir-inside-repå"), from where the pathspec machinery expands the ".", more about this later. If there is a decomposed form, (making the decomposing visible like this), "dir-inside-rep°a" doesn't match "dir-inside-repå". Git commands need to: (a) read the configuration variable "core.precomposeunicode" (b) precocompose argv[] (c) precompose the prefix, if there was any The first commit, 76759c7 "git on Mac OS and precomposed unicode" addressed (a) and (b). The call to precompose_argv() was added into parse-options.c, because that seemed to be a good place when the patch was written. Commands that don't use parse-options need to do (a) and (b) themselfs. The commands `diff-files`, `diff-index`, `diff-tree` and `diff` learned (a) and (b) in commit 90a78b8 "diff: run arguments through precompose_argv" Branch names (or refs in general) using decomposed code points resulting in decomposed file names had been fixed in commit 8e712ef "Honor core.precomposeUnicode in more places" The bug report from above shows 2 things: - more commands need to handle precomposed unicode - (c) should be implemented for all commands using pathspecs Solution: precompose_argv() now handles the prefix (if needed), and is renamed into precompose_argv_prefix(). Inside this function the config variable core.precomposeunicode is read into the global variable precomposed_unicode, as before. This reading is skipped if precomposed_unicode had been read before. The original patch for preocomposed unicode, 76759c7, placed precompose_argv() into parse-options.c Now add it into git.c::run_builtin() as well. Existing precompose calls in diff-files.c and others may become redundant, and if we audit the callflows that reach these places to make sure that they can never be reached without going through the new call added to run_builtin(), we might be able to remove these existing ones. But in this commit, we do not bother to do so and leave these precompose callsites as they are. Because precompose() is idempotent and can be called on an already precomposed string safely, this is safer than removing existing calls without fully vetting the callflows. There is certainly room for cleanups - this change intends to be a bug fix. Cleanups needs more tests in e.g. t/t3910-mac-os-precompose.sh, and should be done in future commits. [1] git-bugreport-2021-01-06-1209.txt (git can't deal with special characters) [2] https://lore.kernel.org/git/[email protected]/ Reported-by: Daniel Troger <[email protected]> Helped-By: Philippe Blain <[email protected]> Signed-off-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 71ca53e commit 5c32750

File tree

11 files changed

+59
-29
lines changed

11 files changed

+59
-29
lines changed

builtin/diff-files.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
3535
*/
3636
rev.diffopt.ita_invisible_in_index = 1;
3737

38-
precompose_argv(argc, argv);
38+
prefix = precompose_argv_prefix(argc, argv, prefix);
3939

4040
argc = setup_revisions(argc, argv, &rev, NULL);
4141
while (1 < argc && argv[1][0] == '-') {

builtin/diff-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
2525
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
2626
repo_init_revisions(the_repository, &rev, prefix);
2727
rev.abbrev = 0;
28-
precompose_argv(argc, argv);
28+
prefix = precompose_argv_prefix(argc, argv, prefix);
2929

3030
argc = setup_revisions(argc, argv, &rev, NULL);
3131
for (i = 1; i < argc; i++) {

builtin/diff-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
126126
memset(&s_r_opt, 0, sizeof(s_r_opt));
127127
s_r_opt.tweak = diff_tree_tweak_rev;
128128

129-
precompose_argv(argc, argv);
129+
prefix = precompose_argv_prefix(argc, argv, prefix);
130130
argc = setup_revisions(argc, argv, opt, &s_r_opt);
131131

132132
memset(&w, 0, sizeof(w));

builtin/diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
452452

453453
init_diff_ui_defaults();
454454
git_config(git_diff_ui_config, NULL);
455-
precompose_argv(argc, argv);
455+
prefix = precompose_argv_prefix(argc, argv, prefix);
456456

457457
repo_init_revisions(the_repository, &rev, prefix);
458458

builtin/submodule--helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ static int compute_summary_module_list(struct object_id *head_oid,
12571257
git_config(git_diff_basic_config, NULL);
12581258
init_revisions(&rev, info->prefix);
12591259
rev.abbrev = 0;
1260-
precompose_argv(diff_args.nr, diff_args.v);
1260+
precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
12611261
setup_revisions(diff_args.nr, diff_args.v, &rev, NULL);
12621262
rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
12631263
rev.diffopt.format_callback = submodule_summary_callback;

compat/precompose_utf8.c

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,46 @@ void probe_utf8_pathname_composition(void)
6060
strbuf_release(&path);
6161
}
6262

63-
64-
void precompose_argv(int argc, const char **argv)
63+
static inline const char *precompose_string_if_needed(const char *in)
6564
{
66-
int i = 0;
67-
const char *oldarg;
68-
char *newarg;
69-
iconv_t ic_precompose;
65+
size_t inlen;
66+
size_t outlen;
67+
if (has_non_ascii(in, (size_t)-1, &inlen)) {
68+
iconv_t ic_prec;
69+
char *out;
70+
if (precomposed_unicode < 0)
71+
git_config_get_bool("core.precomposeunicode", &precomposed_unicode);
72+
if (precomposed_unicode != 1)
73+
return in;
74+
ic_prec = iconv_open(repo_encoding, path_encoding);
75+
if (ic_prec == (iconv_t) -1)
76+
return in;
77+
78+
out = reencode_string_iconv(in, inlen, ic_prec, 0, &outlen);
79+
if (out) {
80+
if (outlen == inlen && !memcmp(in, out, outlen))
81+
free(out); /* no need to return indentical */
82+
else
83+
in = out;
84+
}
85+
iconv_close(ic_prec);
7086

71-
if (precomposed_unicode != 1)
72-
return;
87+
}
88+
return in;
89+
}
7390

74-
ic_precompose = iconv_open(repo_encoding, path_encoding);
75-
if (ic_precompose == (iconv_t) -1)
76-
return;
91+
const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix)
92+
{
93+
int i = 0;
7794

7895
while (i < argc) {
79-
size_t namelen;
80-
oldarg = argv[i];
81-
if (has_non_ascii(oldarg, (size_t)-1, &namelen)) {
82-
newarg = reencode_string_iconv(oldarg, namelen, ic_precompose, 0, NULL);
83-
if (newarg)
84-
argv[i] = newarg;
85-
}
96+
argv[i] = precompose_string_if_needed(argv[i]);
8697
i++;
8798
}
88-
iconv_close(ic_precompose);
99+
if (prefix) {
100+
prefix = precompose_string_if_needed(prefix);
101+
}
102+
return prefix;
89103
}
90104

91105

compat/precompose_utf8.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef struct {
2828
struct dirent_prec_psx *dirent_nfc;
2929
} PREC_DIR;
3030

31-
void precompose_argv(int argc, const char **argv);
31+
const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix);
3232
void probe_utf8_pathname_composition(void);
3333

3434
PREC_DIR *precompose_utf8_opendir(const char *dirname);

git-compat-util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ typedef unsigned long uintptr_t;
252252
#ifdef PRECOMPOSE_UNICODE
253253
#include "compat/precompose_utf8.h"
254254
#else
255-
static inline void precompose_argv(int argc, const char **argv)
255+
static inline const char *precompose_argv_prefix(int argc, const char **argv, const char *prefix)
256256
{
257-
; /* nothing */
257+
return prefix;
258258
}
259259
#define probe_utf8_pathname_composition()
260260
#endif

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
420420
int nongit_ok;
421421
prefix = setup_git_directory_gently(&nongit_ok);
422422
}
423-
423+
prefix = precompose_argv_prefix(argc, argv, prefix);
424424
if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) &&
425425
!(p->option & DELAY_PAGER_CONFIG))
426426
use_pager = check_pager_config(p->cmd);

parse-options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
869869
usage_with_options(usagestr, options);
870870
}
871871

872-
precompose_argv(argc, argv);
872+
precompose_argv_prefix(argc, argv, NULL);
873873
free(real_options);
874874
free(ctx.alias_groups);
875875
return parse_options_end(&ctx);

0 commit comments

Comments
 (0)