diff options
author | Junio C Hamano <gitster@pobox.com> | 2020-09-18 17:58:04 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-09-18 17:58:04 -0700 |
commit | 9d4e7ec4d9e695d95cc7474f59fba8cc38580e29 (patch) | |
tree | 22db63f4315c0e6698d67c879c73520ef4074c39 | |
parent | Merge branch 'os/collect-changed-submodules-optim' (diff) | |
parent | quote: turn 'nodq' parameter into a set of flags (diff) | |
download | tgif-9d4e7ec4d9e695d95cc7474f59fba8cc38580e29.tar.xz |
Merge branch 'jc/quote-path-cleanup'
"git status --short" quoted a path with SP in it when tracked, but
not those that are untracked, ignored or unmerged. They are all
shown quoted consistently.
* jc/quote-path-cleanup:
quote: turn 'nodq' parameter into a set of flags
quote: rename misnamed sq_lookup[] to cq_lookup[]
wt-status: consistently quote paths in "status --short" output
quote_path: code clarification
quote_path: optionally allow quoting a path with SP in it
quote_path: give flags parameter to quote_path()
quote_path: rename quote_path_relative() to quote_path()
-rw-r--r-- | builtin/clean.c | 22 | ||||
-rw-r--r-- | builtin/grep.c | 2 | ||||
-rw-r--r-- | diff.c | 8 | ||||
-rw-r--r-- | quote.c | 47 | ||||
-rw-r--r-- | quote.h | 11 | ||||
-rwxr-xr-x | t/t7508-status.sh | 27 | ||||
-rw-r--r-- | wt-status.c | 37 |
7 files changed, 94 insertions, 60 deletions
diff --git a/builtin/clean.c b/builtin/clean.c index e53ea52d89..687ab473c2 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -162,7 +162,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_nonbare_repository_dir(path)) { if (!quiet) { - quote_path_relative(path->buf, prefix, "ed); + quote_path(path->buf, prefix, "ed, 0); printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir), quoted.buf); } @@ -177,7 +177,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, res = dry_run ? 0 : rmdir(path->buf); if (res) { int saved_errno = errno; - quote_path_relative(path->buf, prefix, "ed); + quote_path(path->buf, prefix, "ed, 0); errno = saved_errno; warning_errno(_(msg_warn_remove_failed), quoted.buf); *dir_gone = 0; @@ -202,7 +202,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, if (remove_dirs(path, prefix, force_flag, dry_run, quiet, &gone)) ret = 1; if (gone) { - quote_path_relative(path->buf, prefix, "ed); + quote_path(path->buf, prefix, "ed, 0); string_list_append(&dels, quoted.buf); } else *dir_gone = 0; @@ -210,11 +210,11 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, } else { res = dry_run ? 0 : unlink(path->buf); if (!res) { - quote_path_relative(path->buf, prefix, "ed); + quote_path(path->buf, prefix, "ed, 0); string_list_append(&dels, quoted.buf); } else { int saved_errno = errno; - quote_path_relative(path->buf, prefix, "ed); + quote_path(path->buf, prefix, "ed, 0); errno = saved_errno; warning_errno(_(msg_warn_remove_failed), quoted.buf); *dir_gone = 0; @@ -238,7 +238,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag, *dir_gone = 1; else { int saved_errno = errno; - quote_path_relative(path->buf, prefix, "ed); + quote_path(path->buf, prefix, "ed, 0); errno = saved_errno; warning_errno(_(msg_warn_remove_failed), quoted.buf); *dir_gone = 0; @@ -266,7 +266,7 @@ static void pretty_print_dels(void) struct column_options copts; for_each_string_list_item(item, &del_list) { - qname = quote_path_relative(item->string, NULL, &buf); + qname = quote_path(item->string, NULL, &buf, 0); string_list_append(&list, qname); } @@ -753,7 +753,7 @@ static int ask_each_cmd(void) for_each_string_list_item(item, &del_list) { /* Ctrl-D should stop removing files */ if (!eof) { - qname = quote_path_relative(item->string, NULL, &buf); + qname = quote_path(item->string, NULL, &buf, 0); /* TRANSLATORS: Make sure to keep [y/N] as is */ printf(_("Remove %s [y/N]? "), qname); if (git_read_line_interactively(&confirm) == EOF) { @@ -1047,19 +1047,19 @@ int cmd_clean(int argc, const char **argv, const char *prefix) if (remove_dirs(&abs_path, prefix, rm_flags, dry_run, quiet, &gone)) errors++; if (gone && !quiet) { - qname = quote_path_relative(item->string, NULL, &buf); + qname = quote_path(item->string, NULL, &buf, 0); printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); } } else { res = dry_run ? 0 : unlink(abs_path.buf); if (res) { int saved_errno = errno; - qname = quote_path_relative(item->string, NULL, &buf); + qname = quote_path(item->string, NULL, &buf, 0); errno = saved_errno; warning_errno(_(msg_warn_remove_failed), qname); errors++; } else if (!quiet) { - qname = quote_path_relative(item->string, NULL, &buf); + qname = quote_path(item->string, NULL, &buf, 0); printf(dry_run ? _(msg_would_remove) : _(msg_remove), qname); } } diff --git a/builtin/grep.c b/builtin/grep.c index f58979bc3f..c8037388c6 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -319,7 +319,7 @@ static void grep_source_name(struct grep_opt *opt, const char *filename, } if (opt->relative && opt->prefix_length) - quote_path_relative(filename + tree_name_len, opt->prefix, out); + quote_path(filename + tree_name_len, opt->prefix, out, 0); else quote_c_style(filename + tree_name_len, out, NULL, 0); @@ -482,14 +482,14 @@ int git_diff_basic_config(const char *var, const char *value, void *cb) static char *quote_two(const char *one, const char *two) { - int need_one = quote_c_style(one, NULL, NULL, 1); - int need_two = quote_c_style(two, NULL, NULL, 1); + int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ); + int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ); struct strbuf res = STRBUF_INIT; if (need_one + need_two) { strbuf_addch(&res, '"'); - quote_c_style(one, &res, NULL, 1); - quote_c_style(two, &res, NULL, 1); + quote_c_style(one, &res, NULL, CQUOTE_NODQ); + quote_c_style(two, &res, NULL, CQUOTE_NODQ); strbuf_addch(&res, '"'); } else { strbuf_addstr(&res, one); @@ -210,7 +210,7 @@ int sq_dequote_to_strvec(char *arg, struct strvec *array) */ #define X8(x) x, x, x, x, x, x, x, x #define X16(x) X8(x), X8(x) -static signed char const sq_lookup[256] = { +static signed char const cq_lookup[256] = { /* 0 1 2 3 4 5 6 7 */ /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a', /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1, @@ -223,9 +223,9 @@ static signed char const sq_lookup[256] = { /* 0x80 */ /* set to 0 */ }; -static inline int sq_must_quote(char c) +static inline int cq_must_quote(char c) { - return sq_lookup[(unsigned char)c] + quote_path_fully > 0; + return cq_lookup[(unsigned char)c] + quote_path_fully > 0; } /* returns the longest prefix not needing a quote up to maxlen if positive. @@ -235,9 +235,9 @@ static size_t next_quote_pos(const char *s, ssize_t maxlen) { size_t len; if (maxlen < 0) { - for (len = 0; !sq_must_quote(s[len]); len++); + for (len = 0; !cq_must_quote(s[len]); len++); } else { - for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++); + for (len = 0; len < maxlen && !cq_must_quote(s[len]); len++); } return len; } @@ -256,7 +256,7 @@ static size_t next_quote_pos(const char *s, ssize_t maxlen) * Return value is the same as in (1). */ static size_t quote_c_style_counted(const char *name, ssize_t maxlen, - struct strbuf *sb, FILE *fp, int no_dq) + struct strbuf *sb, FILE *fp, unsigned flags) { #undef EMIT #define EMIT(c) \ @@ -272,6 +272,7 @@ static size_t quote_c_style_counted(const char *name, ssize_t maxlen, count += (l); \ } while (0) + int no_dq = !!(flags & CQUOTE_NODQ); size_t len, count = 0; const char *p = name; @@ -291,8 +292,8 @@ static size_t quote_c_style_counted(const char *name, ssize_t maxlen, ch = (unsigned char)*p++; if (maxlen >= 0) maxlen -= len + 1; - if (sq_lookup[ch] >= ' ') { - EMIT(sq_lookup[ch]); + if (cq_lookup[ch] >= ' ') { + EMIT(cq_lookup[ch]); } else { EMIT(((ch >> 6) & 03) + '0'); EMIT(((ch >> 3) & 07) + '0'); @@ -309,19 +310,21 @@ static size_t quote_c_style_counted(const char *name, ssize_t maxlen, return count; } -size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq) +size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, unsigned flags) { - return quote_c_style_counted(name, -1, sb, fp, nodq); + return quote_c_style_counted(name, -1, sb, fp, flags); } -void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq) +void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, + unsigned flags) { + int nodq = !!(flags & CQUOTE_NODQ); if (quote_c_style(prefix, NULL, NULL, 0) || quote_c_style(path, NULL, NULL, 0)) { if (!nodq) strbuf_addch(sb, '"'); - quote_c_style(prefix, sb, NULL, 1); - quote_c_style(path, sb, NULL, 1); + quote_c_style(prefix, sb, NULL, CQUOTE_NODQ); + quote_c_style(path, sb, NULL, CQUOTE_NODQ); if (!nodq) strbuf_addch(sb, '"'); } else { @@ -352,13 +355,25 @@ void write_name_quoted_relative(const char *name, const char *prefix, } /* quote path as relative to the given prefix */ -char *quote_path_relative(const char *in, const char *prefix, - struct strbuf *out) +char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags) { struct strbuf sb = STRBUF_INIT; const char *rel = relative_path(in, prefix, &sb); + int force_dq = ((flags & QUOTE_PATH_QUOTE_SP) && strchr(rel, ' ')); + strbuf_reset(out); - quote_c_style_counted(rel, strlen(rel), out, NULL, 0); + + /* + * If the caller wants us to enclose the output in a dq-pair + * whether quote_c_style_counted() needs to, we do it ourselves + * and tell quote_c_style_counted() not to. + */ + if (force_dq) + strbuf_addch(out, '"'); + quote_c_style_counted(rel, strlen(rel), out, NULL, + force_dq ? CQUOTE_NODQ : 0); + if (force_dq) + strbuf_addch(out, '"'); strbuf_release(&sb); return out->buf; @@ -64,16 +64,19 @@ struct strvec; int sq_dequote_to_strvec(char *arg, struct strvec *); int unquote_c_style(struct strbuf *, const char *quoted, const char **endp); -size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq); -void quote_two_c_style(struct strbuf *, const char *, const char *, int); + +/* Bits in the flags parameter to quote_c_style() */ +#define CQUOTE_NODQ 01 +size_t quote_c_style(const char *name, struct strbuf *, FILE *, unsigned); +void quote_two_c_style(struct strbuf *, const char *, const char *, unsigned); void write_name_quoted(const char *name, FILE *, int terminator); void write_name_quoted_relative(const char *name, const char *prefix, FILE *fp, int terminator); /* quote path as relative to the given prefix */ -char *quote_path_relative(const char *in, const char *prefix, - struct strbuf *out); +char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigned flags); +#define QUOTE_PATH_QUOTE_SP 01 /* quoting as a string literal for other languages */ void perl_quote_buf(struct strbuf *sb, const char *src); diff --git a/t/t7508-status.sh b/t/t7508-status.sh index 45e1f6ff68..963fed6821 100755 --- a/t/t7508-status.sh +++ b/t/t7508-status.sh @@ -814,6 +814,33 @@ test_expect_success 'status -s without relative paths' ' ' +cat >expect <<\EOF + M dir1/modified +A dir2/added +A "file with spaces" +?? dir1/untracked +?? dir2/modified +?? dir2/untracked +?? "file with spaces 2" +?? untracked +EOF + +test_expect_success 'status -s without relative paths' ' + test_when_finished "git rm --cached \"file with spaces\"; rm -f file*" && + >"file with spaces" && + >"file with spaces 2" && + >"expect with spaces" && + git add "file with spaces" && + + git status -s >output && + test_cmp expect output && + + git status -s --ignored >output && + grep "^!! \"expect with spaces\"$" output && + grep -v "^!! " output >output-wo-ignored && + test_cmp expect output-wo-ignored +' + test_expect_success 'dry-run of partial commit excluding new file in index' ' cat >expect <<EOF && On branch master diff --git a/wt-status.c b/wt-status.c index d9872d543b..160d14970a 100644 --- a/wt-status.c +++ b/wt-status.c @@ -259,8 +259,6 @@ static void wt_longstatus_print_trailer(struct wt_status *s) status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", ""); } -#define quote_path quote_path_relative - static const char *wt_status_unmerged_status_string(int stagemask) { switch (stagemask) { @@ -338,7 +336,7 @@ static void wt_longstatus_print_unmerged_data(struct wt_status *s, memset(padding, ' ', label_width); } - one = quote_path(it->string, s->prefix, &onebuf); + one = quote_path(it->string, s->prefix, &onebuf, 0); status_printf(s, color(WT_STATUS_HEADER, s), "\t"); how = wt_status_unmerged_status_string(d->stagemask); @@ -404,8 +402,8 @@ static void wt_longstatus_print_change_data(struct wt_status *s, if (d->rename_status == status) one_name = d->rename_source; - one = quote_path(one_name, s->prefix, &onebuf); - two = quote_path(two_name, s->prefix, &twobuf); + one = quote_path(one_name, s->prefix, &onebuf, 0); + two = quote_path(two_name, s->prefix, &twobuf, 0); status_printf(s, color(WT_STATUS_HEADER, s), "\t"); what = wt_status_diff_status_string(status); @@ -966,7 +964,7 @@ static void wt_longstatus_print_other(struct wt_status *s, struct string_list_item *it; const char *path; it = &(l->items[i]); - path = quote_path(it->string, s->prefix, &buf); + path = quote_path(it->string, s->prefix, &buf, 0); if (column_active(s->colopts)) { string_list_append(&output, path); continue; @@ -1850,7 +1848,7 @@ static void wt_shortstatus_unmerged(struct string_list_item *it, } else { struct strbuf onebuf = STRBUF_INIT; const char *one; - one = quote_path(it->string, s->prefix, &onebuf); + one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); printf(" %s\n", one); strbuf_release(&onebuf); } @@ -1879,21 +1877,12 @@ static void wt_shortstatus_status(struct string_list_item *it, const char *one; if (d->rename_source) { - one = quote_path(d->rename_source, s->prefix, &onebuf); - if (*one != '"' && strchr(one, ' ') != NULL) { - putchar('"'); - strbuf_addch(&onebuf, '"'); - one = onebuf.buf; - } + one = quote_path(d->rename_source, s->prefix, &onebuf, + QUOTE_PATH_QUOTE_SP); printf("%s -> ", one); strbuf_release(&onebuf); } - one = quote_path(it->string, s->prefix, &onebuf); - if (*one != '"' && strchr(one, ' ') != NULL) { - putchar('"'); - strbuf_addch(&onebuf, '"'); - one = onebuf.buf; - } + one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); printf("%s\n", one); strbuf_release(&onebuf); } @@ -1907,7 +1896,7 @@ static void wt_shortstatus_other(struct string_list_item *it, } else { struct strbuf onebuf = STRBUF_INIT; const char *one; - one = quote_path(it->string, s->prefix, &onebuf); + one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP); color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign); printf(" %s\n", one); strbuf_release(&onebuf); @@ -2224,9 +2213,9 @@ static void wt_porcelain_v2_print_changed_entry( */ sep_char = '\t'; eol_char = '\n'; - path = quote_path(it->string, s->prefix, &buf); + path = quote_path(it->string, s->prefix, &buf, 0); if (d->rename_source) - path_from = quote_path(d->rename_source, s->prefix, &buf_from); + path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0); } if (path_from) @@ -2312,7 +2301,7 @@ static void wt_porcelain_v2_print_unmerged_entry( if (s->null_termination) path_index = it->string; else - path_index = quote_path(it->string, s->prefix, &buf_index); + path_index = quote_path(it->string, s->prefix, &buf_index, 0); fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c", unmerged_prefix, key, submodule_token, @@ -2345,7 +2334,7 @@ static void wt_porcelain_v2_print_other( path = it->string; eol_char = '\0'; } else { - path = quote_path(it->string, s->prefix, &buf); + path = quote_path(it->string, s->prefix, &buf, 0); eol_char = '\n'; } |