pull --rebase: exit early when the working directory is dirty
[gitweb.git] / quote.c
diff --git a/quote.c b/quote.c
index 919d0920abfc0bd2becbcc35fd065514b9651281..d5cf9d8f94f37fe8ff9f964998c7f0525617e5bc 100644 (file)
--- a/quote.c
+++ b/quote.c
@@ -56,20 +56,13 @@ void sq_quote_print(FILE *stream, const char *src)
        fputc('\'', stream);
 }
 
-void sq_quote_argv(struct strbuf *dst, const char** argv, int count,
-                   size_t maxlen)
+void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
 {
        int i;
 
-       /* Count argv if needed. */
-       if (count < 0) {
-               for (count = 0; argv[count]; count++)
-                       ; /* just counting */
-       }
-
        /* Copy into destination buffer. */
-       strbuf_grow(dst, 32 * count);
-       for (i = 0; i < count; ++i) {
+       strbuf_grow(dst, 255);
+       for (i = 0; argv[i]; ++i) {
                strbuf_addch(dst, ' ');
                sq_quote_buf(dst, argv[i]);
                if (maxlen && dst->len > maxlen)
@@ -131,7 +124,8 @@ static signed char const sq_lookup[256] = {
        /* 0x80 */ /* set to 0 */
 };
 
-static inline int sq_must_quote(char c) {
+static inline int sq_must_quote(char c)
+{
        return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
 }
 
@@ -219,6 +213,22 @@ size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
        return quote_c_style_counted(name, -1, sb, fp, nodq);
 }
 
+void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int 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);
+               if (!nodq)
+                       strbuf_addch(sb, '"');
+       } else {
+               strbuf_addstr(sb, prefix);
+               strbuf_addstr(sb, path);
+       }
+}
+
 void write_name_quoted(const char *name, FILE *fp, int terminator)
 {
        if (terminator) {
@@ -250,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.
  *
@@ -278,7 +330,7 @@ int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
                switch (*quoted++) {
                  case '"':
                        if (endp)
-                               *endp = quoted + 1;
+                               *endp = quoted;
                        return 0;
                  case '\\':
                        break;