]> git.ipfire.org Git - thirdparty/git.git/blobdiff - quote.c
Sync with maint
[thirdparty/git.git] / quote.c
diff --git a/quote.c b/quote.c
index de2922ddd63d6fc001822d116387c8ced7d7630d..24a58ba454fdfd04dd441d787ea8202ea772c02f 100644 (file)
--- a/quote.c
+++ b/quote.c
@@ -43,6 +43,28 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
        free(to_free);
 }
 
+void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
+{
+       static const char ok_punct[] = "+,-./:=@_^";
+       const char *p;
+
+       /* Avoid losing a zero-length string by adding '' */
+       if (!*src) {
+               strbuf_addstr(dst, "''");
+               return;
+       }
+
+       for (p = src; *p; p++) {
+               if (!isalpha(*p) && !isdigit(*p) && !strchr(ok_punct, *p)) {
+                       sq_quote_buf(dst, src);
+                       return;
+               }
+       }
+
+       /* if we get here, we did not need quoting */
+       strbuf_addstr(dst, src);
+}
+
 void sq_quotef(struct strbuf *dst, const char *fmt, ...)
 {
        struct strbuf src = STRBUF_INIT;
@@ -56,7 +78,7 @@ void sq_quotef(struct strbuf *dst, const char *fmt, ...)
        strbuf_release(&src);
 }
 
-void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
+void sq_quote_argv(struct strbuf *dst, const char **argv)
 {
        int i;
 
@@ -65,8 +87,32 @@ void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
        for (i = 0; argv[i]; ++i) {
                strbuf_addch(dst, ' ');
                sq_quote_buf(dst, argv[i]);
-               if (maxlen && dst->len > maxlen)
-                       die("Too many or long arguments");
+       }
+}
+
+/*
+ * Legacy function to append each argv value, quoted as necessasry,
+ * with whitespace before each value.  This results in a leading
+ * space in the result.
+ */
+void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
+{
+       if (argv[0])
+               strbuf_addch(dst, ' ');
+       sq_append_quote_argv_pretty(dst, argv);
+}
+
+/*
+ * Append each argv value, quoted as necessary, with whitespace between them.
+ */
+void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
+{
+       int i;
+
+       for (i = 0; argv[i]; i++) {
+               if (i > 0)
+                       strbuf_addch(dst, ' ');
+               sq_quote_buf_pretty(dst, argv[i]);
        }
 }
 
@@ -94,9 +140,15 @@ static char *sq_dequote_step(char *arg, char **next)
                                *next = NULL;
                        return arg;
                case '\\':
-                       c = *++src;
-                       if (need_bs_quote(c) && *++src == '\'') {
-                               *dst++ = c;
+                       /*
+                        * Allow backslashed characters outside of
+                        * single-quotes only if they need escaping,
+                        * and only if we resume the single-quoted part
+                        * afterward.
+                        */
+                       if (need_bs_quote(src[1]) && src[2] == '\'') {
+                               *dst++ = src[1];
+                               src += 2;
                                continue;
                        }
                /* Fallthrough */
@@ -204,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, int no_dq)
 {
 #undef EMIT
 #define EMIT(c)                                 \