]> asedeno.scripts.mit.edu Git - git.git/commitdiff
Make private quote_path() in wt-status.c available as quote_path_relative()
authorDmitry Potapov <dpotapov@gmail.com>
Fri, 7 Mar 2008 02:30:58 +0000 (05:30 +0300)
committerJunio C Hamano <gitster@pobox.com>
Sat, 8 Mar 2008 05:22:25 +0000 (21:22 -0800)
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>
quote.c
quote.h
wt-status.c

diff --git a/quote.c b/quote.c
index d061626c34f1e62c538db6e931c29751be4fdbcf..e38ba39b6468fda8cc68aec73615ac9cce158daa 100644 (file)
--- a/quote.c
+++ b/quote.c
@@ -260,6 +260,48 @@ extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
        fputc(terminator, fp);
 }
 
+/* quote path as relative to the given prefix */
+char *quote_path_relative(const char *in, int len,
+                         struct strbuf *out, const char *prefix)
+{
+       int needquote;
+
+       if (len < 0)
+               len = strlen(in);
+
+       /* "../" prefix itself does not need quoting, but "in" might. */
+       needquote = next_quote_pos(in, len) < len;
+       strbuf_setlen(out, 0);
+       strbuf_grow(out, len);
+
+       if (needquote)
+               strbuf_addch(out, '"');
+       if (prefix) {
+               int off = 0;
+               while (prefix[off] && off < len && prefix[off] == in[off])
+                       if (prefix[off] == '/') {
+                               prefix += off + 1;
+                               in += off + 1;
+                               len -= off + 1;
+                               off = 0;
+                       } else
+                               off++;
+
+               for (; *prefix; prefix++)
+                       if (*prefix == '/')
+                               strbuf_addstr(out, "../");
+       }
+
+       quote_c_style_counted (in, len, out, NULL, 1);
+
+       if (needquote)
+               strbuf_addch(out, '"');
+       if (!out->len)
+               strbuf_addstr(out, "./");
+
+       return out->buf;
+}
+
 /*
  * C-style name unquoting.
  *
diff --git a/quote.h b/quote.h
index 4da110ec01331f346705199dde709436622967cb..c5eea6f18e2dfabd071b73e6507c34c2b7b5e39f 100644 (file)
--- a/quote.h
+++ b/quote.h
@@ -47,6 +47,10 @@ extern void write_name_quoted(const char *name, FILE *, int terminator);
 extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
                                  const char *name, FILE *, int terminator);
 
+/* quote path as relative to the given prefix */
+char *quote_path_relative(const char *in, int len,
+                         struct strbuf *out, const char *prefix);
+
 /* quoting as a string literal for other languages */
 extern void perl_quote_print(FILE *stream, const char *src);
 extern void python_quote_print(FILE *stream, const char *src);
index 32d780af1e4f6e701f75d9759dfd4ebec56e6425..701d13da7c7b2d3de7ca67fbd663d6e9da813151 100644 (file)
@@ -7,6 +7,7 @@
 #include "diff.h"
 #include "revision.h"
 #include "diffcore.h"
+#include "quote.h"
 
 int wt_status_relative_paths = 1;
 int wt_status_use_color = -1;
@@ -82,51 +83,7 @@ static void wt_status_print_trailer(struct wt_status *s)
        color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
 }
 
-static char *quote_path(const char *in, int len,
-                       struct strbuf *out, const char *prefix)
-{
-       if (len < 0)
-               len = strlen(in);
-
-       strbuf_grow(out, len);
-       strbuf_setlen(out, 0);
-       if (prefix) {
-               int off = 0;
-               while (prefix[off] && off < len && prefix[off] == in[off])
-                       if (prefix[off] == '/') {
-                               prefix += off + 1;
-                               in += off + 1;
-                               len -= off + 1;
-                               off = 0;
-                       } else
-                               off++;
-
-               for (; *prefix; prefix++)
-                       if (*prefix == '/')
-                               strbuf_addstr(out, "../");
-       }
-
-       for ( ; len > 0; in++, len--) {
-               int ch = *in;
-
-               switch (ch) {
-               case '\n':
-                       strbuf_addstr(out, "\\n");
-                       break;
-               case '\r':
-                       strbuf_addstr(out, "\\r");
-                       break;
-               default:
-                       strbuf_addch(out, ch);
-                       continue;
-               }
-       }
-
-       if (!out->len)
-               strbuf_addstr(out, "./");
-
-       return out->buf;
-}
+#define quote_path quote_path_relative
 
 static void wt_status_print_filepair(struct wt_status *s,
                                     int t, struct diff_filepair *p)