Skip to content

Commit a734d0b

Browse files
dmpotgitster
authored andcommitted
Make private quote_path() in wt-status.c available as quote_path_relative()
Move quote_path() from wt-status.c to quote.c and rename it as quote_path_relative(), because it is a better name for a public function. Also, instead of handcrafted quoting, quote_c_style_counted() is now used, to make its quoting more consistent with the rest of the system, also honoring core.quotepath specified in configuration. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7941859 commit a734d0b

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

quote.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,48 @@ extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
260260
fputc(terminator, fp);
261261
}
262262

263+
/* quote path as relative to the given prefix */
264+
char *quote_path_relative(const char *in, int len,
265+
struct strbuf *out, const char *prefix)
266+
{
267+
int needquote;
268+
269+
if (len < 0)
270+
len = strlen(in);
271+
272+
/* "../" prefix itself does not need quoting, but "in" might. */
273+
needquote = next_quote_pos(in, len) < len;
274+
strbuf_setlen(out, 0);
275+
strbuf_grow(out, len);
276+
277+
if (needquote)
278+
strbuf_addch(out, '"');
279+
if (prefix) {
280+
int off = 0;
281+
while (prefix[off] && off < len && prefix[off] == in[off])
282+
if (prefix[off] == '/') {
283+
prefix += off + 1;
284+
in += off + 1;
285+
len -= off + 1;
286+
off = 0;
287+
} else
288+
off++;
289+
290+
for (; *prefix; prefix++)
291+
if (*prefix == '/')
292+
strbuf_addstr(out, "../");
293+
}
294+
295+
quote_c_style_counted (in, len, out, NULL, 1);
296+
297+
if (needquote)
298+
strbuf_addch(out, '"');
299+
if (!out->len)
300+
strbuf_addstr(out, "./");
301+
302+
return out->buf;
303+
}
304+
263305
/*
264306
* C-style name unquoting.
265307
*

quote.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ extern void write_name_quoted(const char *name, FILE *, int terminator);
4747
extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
4848
const char *name, FILE *, int terminator);
4949

50+
/* quote path as relative to the given prefix */
51+
char *quote_path_relative(const char *in, int len,
52+
struct strbuf *out, const char *prefix);
53+
5054
/* quoting as a string literal for other languages */
5155
extern void perl_quote_print(FILE *stream, const char *src);
5256
extern void python_quote_print(FILE *stream, const char *src);

wt-status.c

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "diff.h"
88
#include "revision.h"
99
#include "diffcore.h"
10+
#include "quote.h"
1011

1112
int wt_status_relative_paths = 1;
1213
int wt_status_use_color = -1;
@@ -82,51 +83,7 @@ static void wt_status_print_trailer(struct wt_status *s)
8283
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
8384
}
8485

85-
static char *quote_path(const char *in, int len,
86-
struct strbuf *out, const char *prefix)
87-
{
88-
if (len < 0)
89-
len = strlen(in);
90-
91-
strbuf_grow(out, len);
92-
strbuf_setlen(out, 0);
93-
if (prefix) {
94-
int off = 0;
95-
while (prefix[off] && off < len && prefix[off] == in[off])
96-
if (prefix[off] == '/') {
97-
prefix += off + 1;
98-
in += off + 1;
99-
len -= off + 1;
100-
off = 0;
101-
} else
102-
off++;
103-
104-
for (; *prefix; prefix++)
105-
if (*prefix == '/')
106-
strbuf_addstr(out, "../");
107-
}
108-
109-
for ( ; len > 0; in++, len--) {
110-
int ch = *in;
111-
112-
switch (ch) {
113-
case '\n':
114-
strbuf_addstr(out, "\\n");
115-
break;
116-
case '\r':
117-
strbuf_addstr(out, "\\r");
118-
break;
119-
default:
120-
strbuf_addch(out, ch);
121-
continue;
122-
}
123-
}
124-
125-
if (!out->len)
126-
strbuf_addstr(out, "./");
127-
128-
return out->buf;
129-
}
86+
#define quote_path quote_path_relative
13087

13188
static void wt_status_print_filepair(struct wt_status *s,
13289
int t, struct diff_filepair *p)

0 commit comments

Comments
 (0)