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>
Dmitry Potapov committed
Mar 7, 2008 at 05:30 UTC
a734d0b10bd0f5554abb3acdf11426040cfc4df0
3 files changed
+48
-45
quote.c
+42
@@ -260,6 +260,48 @@ extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
260
fputc(terminator, fp);
261
}
262
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
+
305
/*
306
* C-style name unquoting.
307
*
quote.h
+4
@@ -47,6 +47,10 @@ extern void write_name_quoted(const char *name, FILE *, int terminator);
47
extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
48
const char *name, FILE *, int terminator);
49
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
+
54
/* quoting as a string literal for other languages */
55
extern void perl_quote_print(FILE *stream, const char *src);
56
extern void python_quote_print(FILE *stream, const char *src);
wt-status.c
+2
-45
@@ -7,6 +7,7 @@
7
#include "diff.h"
8
#include "revision.h"
9
#include "diffcore.h"
10
+#include "quote.h"
11
12
int wt_status_relative_paths = 1;
13
int wt_status_use_color = -1;
@@ -82,51 +83,7 @@ static void wt_status_print_trailer(struct wt_status *s)
83
color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
84
}
85
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
87
88
static void wt_status_print_filepair(struct wt_status *s,
89
int t, struct diff_filepair *p)