@matheusbsbs / matheus-bagliano / commits / 663af3422a
Fork 0

Full rework of quote_c_style and write_name_quoted.

* quote_c_style works on a strbuf instead of a wild buffer. * quote_c_style is now clever enough to not add double quotes if not needed. * write_name_quoted inherits those advantages, but also take a different set of arguments. Now instead of asking for quotes or not, you pass a "terminator". If it's \0 then we assume you don't want to escape, else C escaping is performed. In any case, the terminator is also appended to the stream. It also no longer takes the prefix/prefix_len arguments, as it's seldomly used, and makes some optimizations harder. * write_name_quotedpfx is created to work like write_name_quoted and take the prefix/prefix_len arguments. Thanks to those API changes, diff.c has somehow lost weight, thanks to the removal of functions that were wrappers around the old write_name_quoted trying to give it a semantics like the new one, but performing a lot of allocations for this goal. Now we always write directly to the stream, no intermediate allocation is performed. As a side effect of the refactor in builtin-apply.c, the length of the bar graphs in diffstats are not affected anymore by the fact that the path was clipped. Signed-off-by: Pierre Habouzit <madcoder@debian.org>

Pierre Habouzit committed Sep 20, 2007 at 00:42 UTC 663af3422a648e87945e4d8c0cc3e13671f2bbde
10 files changed +273 -368
builtin-apply.c
+30 -47
@@ -163,15 +163,14 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
163 fputs(pre, output);
164 if (patch->old_name && patch->new_name &&
165 strcmp(patch->old_name, patch->new_name)) {
166 - write_name_quoted(NULL, 0, patch->old_name, 1, output);
166 + quote_c_style(patch->old_name, NULL, output, 0);
167 fputs(" => ", output);
168 - write_name_quoted(NULL, 0, patch->new_name, 1, output);
169 - }
170 - else {
168 + quote_c_style(patch->new_name, NULL, output, 0);
169 + } else {
170 const char *n = patch->new_name;
171 if (!n)
172 n = patch->old_name;
174 - write_name_quoted(NULL, 0, n, 1, output);
173 + quote_c_style(n, NULL, output, 0);
174 }
175 fputs(post, output);
176 }
@@ -1379,61 +1378,50 @@ static const char minuses[]= "--------------------------------------------------
1378
1379 static void show_stats(struct patch *patch)
1380 {
1382 - const char *prefix = "";
1383 - char *name = patch->new_name;
1384 - char *qname = NULL;
1385 - int len, max, add, del, total;
1386 -
1387 - if (!name)
1388 - name = patch->old_name;
1381 + struct strbuf qname;
1382 + char *cp = patch->new_name ? patch->new_name : patch->old_name;
1383 + int max, add, del;
1384
1390 - if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
1391 - qname = xmalloc(len + 1);
1392 - quote_c_style(name, qname, NULL, 0);
1393 - name = qname;
1394 - }
1385 + strbuf_init(&qname, 0);
1386 + quote_c_style(cp, &qname, NULL, 0);
1387
1388 /*
1389 * "scale" the filename
1390 */
1399 - len = strlen(name);
1391 max = max_len;
1392 if (max > 50)
1393 max = 50;
1403 - if (len > max) {
1404 - char *slash;
1405 - prefix = "...";
1406 - max -= 3;
1407 - name += len - max;
1408 - slash = strchr(name, '/');
1409 - if (slash)
1410 - name = slash;
1394 +
1395 + if (qname.len > max) {
1396 + cp = strchr(qname.buf + qname.len + 3 - max, '/');
1397 + if (!cp)
1398 + cp = qname.buf + qname.len + 3 - max;
1399 + strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
1400 + }
1401 +
1402 + if (patch->is_binary) {
1403 + printf(" %-*s | Bin\n", max, qname.buf);
1404 + strbuf_release(&qname);
1405 + return;
1406 }
1412 - len = max;
1407 +
1408 + printf(" %-*s |", max, qname.buf);
1409 + strbuf_release(&qname);
1410
1411 /*
1412 * scale the add/delete
1413 */
1417 - max = max_change;
1418 - if (max + len > 70)
1419 - max = 70 - len;
1420 -
1414 + max = max + max_change > 70 ? 70 - max : max_change;
1415 add = patch->lines_added;
1416 del = patch->lines_deleted;
1423 - total = add + del;
1417
1418 if (max_change > 0) {
1426 - total = (total * max + max_change / 2) / max_change;
1419 + int total = ((add + del) * max + max_change / 2) / max_change;
1420 add = (add * max + max_change / 2) / max_change;
1421 del = total - add;
1422 }
1430 - if (patch->is_binary)
1431 - printf(" %s%-*s | Bin\n", prefix, len, name);
1432 - else
1433 - printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
1434 - len, name, patch->lines_added + patch->lines_deleted,
1435 - add, pluses, del, minuses);
1436 - free(qname);
1423 + printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
1424 + add, pluses, del, minuses);
1425 }
1426
1427 static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
@@ -2228,13 +2216,8 @@ static void numstat_patch_list(struct patch *patch)
2216 if (patch->is_binary)
2217 printf("-\t-\t");
2218 else
2231 - printf("%d\t%d\t",
2232 - patch->lines_added, patch->lines_deleted);
2233 - if (line_termination && quote_c_style(name, NULL, NULL, 0))
2234 - quote_c_style(name, NULL, stdout, 0);
2235 - else
2236 - fputs(name, stdout);
2237 - putchar(line_termination);
2219 + printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
2220 + write_name_quoted(name, stdout, line_termination);
2221 }
2222 }
2223
builtin-blame.c
+1 -2
@@ -1430,8 +1430,7 @@ static void get_commit_info(struct commit *commit,
1430 static void write_filename_info(const char *path)
1431 {
1432 printf("filename ");
1433 - write_name_quoted(NULL, 0, path, 1, stdout);
1434 - putchar('\n');
1433 + write_name_quoted(path, stdout, '\n');
1434 }
1435
1436 /*
builtin-check-attr.c
+1 -1
@@ -56,7 +56,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
56 else if (ATTR_UNSET(value))
57 value = "unspecified";
58
59 - write_name_quoted("", 0, argv[i], 1, stdout);
59 + quote_c_style(argv[i], NULL, stdout, 0);
60 printf(": %s: %s\n", argv[j+1], value);
61 }
62 }
builtin-checkout-index.c
+1 -3
@@ -66,9 +66,7 @@ static void write_tempfile_record(const char *name, int prefix_length)
66 fputs(topath[checkout_stage], stdout);
67
68 putchar('\t');
69 - write_name_quoted("", 0, name + prefix_length,
70 - line_termination, stdout);
71 - putchar(line_termination);
69 + write_name_quoted(name + prefix_length, stdout, line_termination);
70
71 for (i = 0; i < 4; i++) {
72 topath[i][0] = 0;
builtin-ls-files.c
+3 -10
@@ -84,8 +84,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
84 return;
85
86 fputs(tag, stdout);
87 - write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
88 - putchar(line_terminator);
87 + write_name_quoted(ent->name + offset, stdout, line_terminator);
88 }
89
90 static void show_other_files(struct dir_struct *dir)
@@ -208,21 +207,15 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
207
208 if (!show_stage) {
209 fputs(tag, stdout);
211 - write_name_quoted("", 0, ce->name + offset,
212 - line_terminator, stdout);
213 - putchar(line_terminator);
214 - }
215 - else {
210 + } else {
211 printf("%s%06o %s %d\t",
212 tag,
213 ntohl(ce->ce_mode),
214 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
215 : sha1_to_hex(ce->sha1),
216 ce_stage(ce));
222 - write_name_quoted("", 0, ce->name + offset,
223 - line_terminator, stdout);
224 - putchar(line_terminator);
217 }
218 + write_name_quoted(ce->name + offset, stdout, line_terminator);
219 }
220
221 static void show_files(struct dir_struct *dir, const char *prefix)
builtin-ls-tree.c
+2 -4
@@ -112,10 +112,8 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
112 abbrev ? find_unique_abbrev(sha1, abbrev)
113 : sha1_to_hex(sha1));
114 }
115 - write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
116 - pathname,
117 - line_termination, stdout);
118 - putchar(line_termination);
115 + write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
116 + pathname, stdout, line_termination);
117 return retval;
118 }
119
combine-diff.c
+2 -14
@@ -650,10 +650,7 @@ static void dump_quoted_path(const char *prefix, const char *path,
650 const char *c_meta, const char *c_reset)
651 {
652 printf("%s%s", c_meta, prefix);
653 - if (quote_c_style(path, NULL, NULL, 0))
654 - quote_c_style(path, NULL, stdout, 0);
655 - else
656 - printf("%s", path);
653 + quote_c_style(path, NULL, stdout, 0);
654 printf("%s\n", c_reset);
655 }
656
@@ -900,16 +897,7 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re
897 putchar(inter_name_termination);
898 }
899
903 - if (line_termination) {
904 - if (quote_c_style(p->path, NULL, NULL, 0))
905 - quote_c_style(p->path, NULL, stdout, 0);
906 - else
907 - printf("%s", p->path);
908 - putchar(line_termination);
909 - }
910 - else {
911 - printf("%s%c", p->path, line_termination);
912 - }
900 + write_name_quoted(p->path, stdout, line_termination);
901 }
902
903 void show_combined_diff(struct combine_diff_path *p,
diff.c
+110 -193
@@ -181,44 +181,23 @@ int git_diff_ui_config(const char *var, const char *value)
181 return git_default_config(var, value);
182 }
183
184 -static char *quote_one(const char *str)
185 -{
186 - int needlen;
187 - char *xp;
188 -
189 - if (!str)
190 - return NULL;
191 - needlen = quote_c_style(str, NULL, NULL, 0);
192 - if (!needlen)
193 - return xstrdup(str);
194 - xp = xmalloc(needlen + 1);
195 - quote_c_style(str, xp, NULL, 0);
196 - return xp;
197 -}
198 -
184 static char *quote_two(const char *one, const char *two)
185 {
186 int need_one = quote_c_style(one, NULL, NULL, 1);
187 int need_two = quote_c_style(two, NULL, NULL, 1);
203 - char *xp;
188 + struct strbuf res;
189
190 + strbuf_init(&res, 0);
191 if (need_one + need_two) {
206 - if (!need_one) need_one = strlen(one);
207 - if (!need_two) need_one = strlen(two);
208 -
209 - xp = xmalloc(need_one + need_two + 3);
210 - xp[0] = '"';
211 - quote_c_style(one, xp + 1, NULL, 1);
212 - quote_c_style(two, xp + need_one + 1, NULL, 1);
213 - strcpy(xp + need_one + need_two + 1, "\"");
214 - return xp;
192 + strbuf_addch(&res, '"');
193 + quote_c_style(one, &res, NULL, 1);
194 + quote_c_style(two, &res, NULL, 1);
195 + strbuf_addch(&res, '"');
196 + } else {
197 + strbuf_addstr(&res, one);
198 + strbuf_addstr(&res, two);
199 }
216 - need_one = strlen(one);
217 - need_two = strlen(two);
218 - xp = xmalloc(need_one + need_two + 1);
219 - strcpy(xp, one);
220 - strcpy(xp + need_one, two);
221 - return xp;
200 + return res.buf;
201 }
202
203 static const char *external_diff(void)
@@ -670,27 +649,20 @@ static char *pprint_rename(const char *a, const char *b)
649 {
650 const char *old = a;
651 const char *new = b;
673 - char *name = NULL;
652 + struct strbuf name;
653 int pfx_length, sfx_length;
654 int len_a = strlen(a);
655 int len_b = strlen(b);
656 + int a_midlen, b_midlen;
657 int qlen_a = quote_c_style(a, NULL, NULL, 0);
658 int qlen_b = quote_c_style(b, NULL, NULL, 0);
659
660 + strbuf_init(&name, 0);
661 if (qlen_a || qlen_b) {
681 - if (qlen_a) len_a = qlen_a;
682 - if (qlen_b) len_b = qlen_b;
683 - name = xmalloc( len_a + len_b + 5 );
684 - if (qlen_a)
685 - quote_c_style(a, name, NULL, 0);
686 - else
687 - memcpy(name, a, len_a);
688 - memcpy(name + len_a, " => ", 4);
689 - if (qlen_b)
690 - quote_c_style(b, name + len_a + 4, NULL, 0);
691 - else
692 - memcpy(name + len_a + 4, b, len_b + 1);
693 - return name;
662 + quote_c_style(a, &name, NULL, 0);
663 + strbuf_addstr(&name, " => ");
664 + quote_c_style(b, &name, NULL, 0);
665 + return name.buf;
666 }
667
668 /* Find common prefix */
@@ -719,24 +691,26 @@ static char *pprint_rename(const char *a, const char *b)
691 * pfx{sfx-a => sfx-b}
692 * name-a => name-b
693 */
694 + a_midlen = len_a - pfx_length - sfx_length;
695 + b_midlen = len_b - pfx_length - sfx_length;
696 + if (a_midlen < 0)
697 + a_midlen = 0;
698 + if (b_midlen < 0)
699 + b_midlen = 0;
700 +
701 + strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
702 if (pfx_length + sfx_length) {
723 - int a_midlen = len_a - pfx_length - sfx_length;
724 - int b_midlen = len_b - pfx_length - sfx_length;
725 - if (a_midlen < 0) a_midlen = 0;
726 - if (b_midlen < 0) b_midlen = 0;
727 -
728 - name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
729 - sprintf(name, "%.*s{%.*s => %.*s}%s",
730 - pfx_length, a,
731 - a_midlen, a + pfx_length,
732 - b_midlen, b + pfx_length,
733 - a + len_a - sfx_length);
703 + strbuf_add(&name, a, pfx_length);
704 + strbuf_addch(&name, '{');
705 }
735 - else {
736 - name = xmalloc(len_a + len_b + 5);
737 - sprintf(name, "%s => %s", a, b);
706 + strbuf_add(&name, a + pfx_length, a_midlen);
707 + strbuf_addstr(&name, " => ");
708 + strbuf_add(&name, b + pfx_length, b_midlen);
709 + if (pfx_length + sfx_length) {
710 + strbuf_addch(&name, '}');
711 + strbuf_add(&name, a + len_a - sfx_length, sfx_length);
712 }
739 - return name;
713 + return name.buf;
714 }
715
716 struct diffstat_t {
@@ -849,12 +823,13 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
823 int change = file->added + file->deleted;
824
825 if (!file->is_renamed) { /* renames are already quoted by pprint_rename */
852 - len = quote_c_style(file->name, NULL, NULL, 0);
853 - if (len) {
854 - char *qname = xmalloc(len + 1);
855 - quote_c_style(file->name, qname, NULL, 0);
826 + struct strbuf buf;
827 + strbuf_init(&buf, 0);
828 + if (quote_c_style(file->name, &buf, NULL, 0)) {
829 free(file->name);
857 - file->name = qname;
830 + file->name = buf.buf;
831 + } else {
832 + strbuf_release(&buf);
833 }
834 }
835
@@ -992,12 +967,12 @@ static void show_numstat(struct diffstat_t* data, struct diff_options *options)
967 printf("-\t-\t");
968 else
969 printf("%d\t%d\t", file->added, file->deleted);
995 - if (options->line_termination && !file->is_renamed &&
996 - quote_c_style(file->name, NULL, NULL, 0))
997 - quote_c_style(file->name, NULL, stdout, 0);
998 - else
970 + if (!file->is_renamed) {
971 + write_name_quoted(file->name, stdout, options->line_termination);
972 + } else {
973 fputs(file->name, stdout);
1000 - putchar(options->line_termination);
974 + putchar(options->line_termination);
975 + }
976 }
977 }
978
@@ -1941,50 +1916,46 @@ static int similarity_index(struct diff_filepair *p)
1916 static void run_diff(struct diff_filepair *p, struct diff_options *o)
1917 {
1918 const char *pgm = external_diff();
1944 - char msg[PATH_MAX*2+300], *xfrm_msg;
1945 - struct diff_filespec *one;
1946 - struct diff_filespec *two;
1919 + struct strbuf msg;
1920 + char *xfrm_msg;
1921 + struct diff_filespec *one = p->one;
1922 + struct diff_filespec *two = p->two;
1923 const char *name;
1924 const char *other;
1949 - char *name_munged, *other_munged;
1925 int complete_rewrite = 0;
1951 - int len;
1926 +
1927
1928 if (DIFF_PAIR_UNMERGED(p)) {
1954 - /* unmerged */
1929 run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1930 return;
1931 }
1932
1959 - name = p->one->path;
1933 + name = p->one->path;
1934 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1961 - name_munged = quote_one(name);
1962 - other_munged = quote_one(other);
1963 - one = p->one; two = p->two;
1964 -
1935 diff_fill_sha1_info(one);
1936 diff_fill_sha1_info(two);
1937
1968 - len = 0;
1938 + strbuf_init(&msg, PATH_MAX * 2 + 300);
1939 switch (p->status) {
1940 case DIFF_STATUS_COPIED:
1971 - len += snprintf(msg + len, sizeof(msg) - len,
1972 - "similarity index %d%%\n"
1973 - "copy from %s\n"
1974 - "copy to %s\n",
1975 - similarity_index(p), name_munged, other_munged);
1941 + strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
1942 + strbuf_addstr(&msg, "\ncopy from ");
1943 + quote_c_style(name, &msg, NULL, 0);
1944 + strbuf_addstr(&msg, "\ncopy to ");
1945 + quote_c_style(other, &msg, NULL, 0);
1946 + strbuf_addch(&msg, '\n');
1947 break;
1948 case DIFF_STATUS_RENAMED:
1978 - len += snprintf(msg + len, sizeof(msg) - len,
1979 - "similarity index %d%%\n"
1980 - "rename from %s\n"
1981 - "rename to %s\n",
1982 - similarity_index(p), name_munged, other_munged);
1949 + strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
1950 + strbuf_addstr(&msg, "\nrename from ");
1951 + quote_c_style(name, &msg, NULL, 0);
1952 + strbuf_addstr(&msg, "\nrename to ");
1953 + quote_c_style(other, &msg, NULL, 0);
1954 + strbuf_addch(&msg, '\n');
1955 break;
1956 case DIFF_STATUS_MODIFIED:
1957 if (p->score) {
1986 - len += snprintf(msg + len, sizeof(msg) - len,
1987 - "dissimilarity index %d%%\n",
1958 + strbuf_addf(&msg, "dissimilarity index %d%%\n",
1959 similarity_index(p));
1960 complete_rewrite = 1;
1961 break;
@@ -2004,19 +1975,17 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
1975 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
1976 abbrev = 40;
1977 }
2007 - len += snprintf(msg + len, sizeof(msg) - len,
2008 - "index %.*s..%.*s",
1978 + strbuf_addf(&msg, "index %.*s..%.*s",
1979 abbrev, sha1_to_hex(one->sha1),
1980 abbrev, sha1_to_hex(two->sha1));
1981 if (one->mode == two->mode)
2012 - len += snprintf(msg + len, sizeof(msg) - len,
2013 - " %06o", one->mode);
2014 - len += snprintf(msg + len, sizeof(msg) - len, "\n");
1982 + strbuf_addf(&msg, " %06o", one->mode);
1983 + strbuf_addch(&msg, '\n');
1984 }
1985
2017 - if (len)
2018 - msg[--len] = 0;
2019 - xfrm_msg = len ? msg : NULL;
1986 + if (msg.len)
1987 + strbuf_setlen(&msg, msg.len - 1);
1988 + xfrm_msg = msg.len ? msg.buf : NULL;
1989
1990 if (!pgm &&
1991 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
@@ -2035,8 +2004,7 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
2004 run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
2005 complete_rewrite);
2006
2038 - free(name_munged);
2039 - free(other_munged);
2007 + strbuf_release(&msg);
2008 }
2009
2010 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
@@ -2492,72 +2460,30 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2460 return sha1_to_hex(sha1);
2461 }
2462
2495 -static void diff_flush_raw(struct diff_filepair *p,
2496 - struct diff_options *options)
2463 +static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
2464 {
2498 - int two_paths;
2499 - char status[10];
2500 - int abbrev = options->abbrev;
2501 - const char *path_one, *path_two;
2502 - int inter_name_termination = '\t';
2503 - int line_termination = options->line_termination;
2504 -
2505 - if (!line_termination)
2506 - inter_name_termination = 0;
2465 + int line_termination = opt->line_termination;
2466 + int inter_name_termination = line_termination ? '\t' : '\0';
2467
2508 - path_one = p->one->path;
2509 - path_two = p->two->path;
2510 - if (line_termination) {
2511 - path_one = quote_one(path_one);
2512 - path_two = quote_one(path_two);
2468 + if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
2469 + printf(":%06o %06o %s ", p->one->mode, p->two->mode,
2470 + diff_unique_abbrev(p->one->sha1, opt->abbrev));
2471 + printf("%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev));
2472 }
2514 -
2515 - if (p->score)
2516 - sprintf(status, "%c%03d", p->status, similarity_index(p));
2517 - else {
2518 - status[0] = p->status;
2519 - status[1] = 0;
2520 - }
2521 - switch (p->status) {
2522 - case DIFF_STATUS_COPIED:
2523 - case DIFF_STATUS_RENAMED:
2524 - two_paths = 1;
2525 - break;
2526 - case DIFF_STATUS_ADDED:
2527 - case DIFF_STATUS_DELETED:
2528 - two_paths = 0;
2529 - break;
2530 - default:
2531 - two_paths = 0;
2532 - break;
2533 - }
2534 - if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
2535 - printf(":%06o %06o %s ",
2536 - p->one->mode, p->two->mode,
2537 - diff_unique_abbrev(p->one->sha1, abbrev));
2538 - printf("%s ",
2539 - diff_unique_abbrev(p->two->sha1, abbrev));
2473 + if (p->score) {
2474 + printf("%c%03d%c", p->status, similarity_index(p),
2475 + inter_name_termination);
2476 + } else {
2477 + printf("%c%c", p->status, inter_name_termination);
2478 }
2541 - printf("%s%c%s", status, inter_name_termination,
2542 - two_paths || p->one->mode ? path_one : path_two);
2543 - if (two_paths)
2544 - printf("%c%s", inter_name_termination, path_two);
2545 - putchar(line_termination);
2546 - if (path_one != p->one->path)
2547 - free((void*)path_one);
2548 - if (path_two != p->two->path)
2549 - free((void*)path_two);
2550 -}
2479
2552 -static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt)
2553 -{
2554 - char *path = p->two->path;
2555 -
2556 - if (opt->line_termination)
2557 - path = quote_one(p->two->path);
2558 - printf("%s%c", path, opt->line_termination);
2559 - if (p->two->path != path)
2560 - free(path);
2480 + if (p->status == DIFF_STATUS_COPIED || p->status == DIFF_STATUS_RENAMED) {
2481 + write_name_quoted(p->one->path, stdout, inter_name_termination);
2482 + write_name_quoted(p->two->path, stdout, line_termination);
2483 + } else {
2484 + const char *path = p->one->mode ? p->one->path : p->two->path;
2485 + write_name_quoted(path, stdout, line_termination);
2486 + }
2487 }
2488
2489 int diff_unmodified_pair(struct diff_filepair *p)
@@ -2567,14 +2493,11 @@ int diff_unmodified_pair(struct diff_filepair *p)
2493 * let transformers to produce diff_filepairs any way they want,
2494 * and filter and clean them up here before producing the output.
2495 */
2570 - struct diff_filespec *one, *two;
2496 + struct diff_filespec *one = p->one, *two = p->two;
2497
2498 if (DIFF_PAIR_UNMERGED(p))
2499 return 0; /* unmerged is interesting */
2500
2575 - one = p->one;
2576 - two = p->two;
2577 -
2501 /* deletion, addition, mode or type change
2502 * and rename are all interesting.
2503 */
@@ -2763,32 +2686,27 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2686 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2687 diff_flush_raw(p, opt);
2688 else if (fmt & DIFF_FORMAT_NAME)
2766 - diff_flush_name(p, opt);
2689 + write_name_quoted(p->two->path, stdout, opt->line_termination);
2690 }
2691
2692 static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2693 {
2771 - char *name = quote_one(fs->path);
2694 if (fs->mode)
2773 - printf(" %s mode %06o %s\n", newdelete, fs->mode, name);
2695 + printf(" %s mode %06o ", newdelete, fs->mode);
2696 else
2775 - printf(" %s %s\n", newdelete, name);
2776 - free(name);
2697 + printf(" %s ", newdelete);
2698 + write_name_quoted(fs->path, stdout, '\n');
2699 }
2700
2701
2702 static void show_mode_change(struct diff_filepair *p, int show_name)
2703 {
2704 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2705 + printf(" mode change %06o => %06o%c", p->one->mode, p->two->mode,
2706 + show_name ? ' ' : '\n');
2707 if (show_name) {
2784 - char *name = quote_one(p->two->path);
2785 - printf(" mode change %06o => %06o %s\n",
2786 - p->one->mode, p->two->mode, name);
2787 - free(name);
2708 + write_name_quoted(p->two->path, stdout, '\n');
2709 }
2789 - else
2790 - printf(" mode change %06o => %06o\n",
2791 - p->one->mode, p->two->mode);
2710 }
2711 }
2712
@@ -2818,12 +2736,11 @@ static void diff_summary(struct diff_filepair *p)
2736 break;
2737 default:
2738 if (p->score) {
2821 - char *name = quote_one(p->two->path);
2822 - printf(" rewrite %s (%d%%)\n", name,
2823 - similarity_index(p));
2824 - free(name);
2825 - show_mode_change(p, 0);
2826 - } else show_mode_change(p, 1);
2739 + puts(" rewrite ");
2740 + write_name_quoted(p->two->path, stdout, ' ');
2741 + printf("(%d%%)\n", similarity_index(p));
2742 + }
2743 + show_mode_change(p, !p->score);
2744 break;
2745 }
2746 }
@@ -2837,14 +2754,14 @@ struct patch_id_t {
2754 static int remove_space(char *line, int len)
2755 {
2756 int i;
2840 - char *dst = line;
2841 - unsigned char c;
2757 + char *dst = line;
2758 + unsigned char c;
2759
2843 - for (i = 0; i < len; i++)
2844 - if (!isspace((c = line[i])))
2845 - *dst++ = c;
2760 + for (i = 0; i < len; i++)
2761 + if (!isspace((c = line[i])))
2762 + *dst++ = c;
2763
2847 - return dst - line;
2764 + return dst - line;
2765 }
2766
2767 static void patch_id_consume(void *priv, char *line, unsigned long len)
quote.c
+119 -90
@@ -157,83 +157,143 @@ char *sq_dequote(char *arg)
157 }
158 }
159
160 +/* 1 means: quote as octal
161 + * 0 means: quote as octal if (quote_path_fully)
162 + * -1 means: never quote
163 + * c: quote as "\\c"
164 + */
165 +#define X8(x) x, x, x, x, x, x, x, x
166 +#define X16(x) X8(x), X8(x)
167 +static signed char const sq_lookup[256] = {
168 + /* 0 1 2 3 4 5 6 7 */
169 + /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
170 + /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
171 + /* 0x10 */ X16(1),
172 + /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
173 + /* 0x28 */ X16(-1), X16(-1), X16(-1),
174 + /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
175 + /* 0x60 */ X16(-1), X8(-1),
176 + /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
177 + /* 0x80 */ /* set to 0 */
178 +};
179 +
180 +static inline int sq_must_quote(char c) {
181 + return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
182 +}
183 +
184 +/* returns the longest prefix not needing a quote up to maxlen if positive.
185 + This stops at the first \0 because it's marked as a character needing an
186 + escape */
187 +static size_t next_quote_pos(const char *s, ssize_t maxlen)
188 +{
189 + size_t len;
190 + if (maxlen < 0) {
191 + for (len = 0; !sq_must_quote(s[len]); len++);
192 + } else {
193 + for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
194 + }
195 + return len;
196 +}
197 +
198 /*
199 * C-style name quoting.
200 *
163 - * Does one of three things:
164 - *
165 - * (1) if outbuf and outfp are both NULL, inspect the input name and
166 - * counts the number of bytes that are needed to hold c_style
167 - * quoted version of name, counting the double quotes around
168 - * it but not terminating NUL, and returns it. However, if name
169 - * does not need c_style quoting, it returns 0.
170 - *
171 - * (2) if outbuf is not NULL, it must point at a buffer large enough
172 - * to hold the c_style quoted version of name, enclosing double
173 - * quotes, and terminating NUL. Fills outbuf with c_style quoted
174 - * version of name enclosed in double-quote pair. Return value
175 - * is undefined.
201 + * (1) if sb and fp are both NULL, inspect the input name and counts the
202 + * number of bytes that are needed to hold c_style quoted version of name,
203 + * counting the double quotes around it but not terminating NUL, and
204 + * returns it.
205 + * However, if name does not need c_style quoting, it returns 0.
206 *
177 - * (3) if outfp is not NULL, outputs c_style quoted version of name,
178 - * but not enclosed in double-quote pair. Return value is undefined.
207 + * (2) if sb or fp are not NULL, it emits the c_style quoted version
208 + * of name, enclosed with double quotes if asked and needed only.
209 + * Return value is the same as in (1).
210 */
180 -
181 -static int quote_c_style_counted(const char *name, int namelen,
182 - char *outbuf, FILE *outfp, int no_dq)
211 +static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
212 + struct strbuf *sb, FILE *fp, int no_dq)
213 {
214 #undef EMIT
185 -#define EMIT(c) \
186 - (outbuf ? (*outbuf++ = (c)) : outfp ? fputc(c, outfp) : (count++))
187 -
188 -#define EMITQ() EMIT('\\')
215 +#define EMIT(c) \
216 + do { \
217 + if (sb) strbuf_addch(sb, (c)); \
218 + if (fp) fputc((c), fp); \
219 + count++; \
220 + } while (0)
221 +#define EMITBUF(s, l) \
222 + do { \
223 + if (sb) strbuf_add(sb, (s), (l)); \
224 + if (fp) fwrite((s), (l), 1, fp); \
225 + count += (l); \
226 + } while (0)
227 +
228 + size_t len, count = 0;
229 + const char *p = name;
230
190 - const char *sp;
191 - unsigned char ch;
192 - int count = 0, needquote = 0;
231 + for (;;) {
232 + int ch;
233
194 - if (!no_dq)
195 - EMIT('"');
196 - for (sp = name; sp < name + namelen; sp++) {
197 - ch = *sp;
198 - if (!ch)
234 + len = next_quote_pos(p, maxlen);
235 + if (len == maxlen || !p[len])
236 break;
200 - if ((ch < ' ') || (ch == '"') || (ch == '\\') ||
201 - (quote_path_fully && (ch >= 0177))) {
202 - needquote = 1;
203 - switch (ch) {
204 - case '\a': EMITQ(); ch = 'a'; break;
205 - case '\b': EMITQ(); ch = 'b'; break;
206 - case '\f': EMITQ(); ch = 'f'; break;
207 - case '\n': EMITQ(); ch = 'n'; break;
208 - case '\r': EMITQ(); ch = 'r'; break;
209 - case '\t': EMITQ(); ch = 't'; break;
210 - case '\v': EMITQ(); ch = 'v'; break;
211 -
212 - case '\\': /* fallthru */
213 - case '"': EMITQ(); break;
214 - default:
215 - /* octal */
216 - EMITQ();
217 - EMIT(((ch >> 6) & 03) + '0');
218 - EMIT(((ch >> 3) & 07) + '0');
219 - ch = (ch & 07) + '0';
220 - break;
221 - }
237 +
238 + if (!no_dq && p == name)
239 + EMIT('"');
240 +
241 + EMITBUF(p, len);
242 + EMIT('\\');
243 + p += len;
244 + ch = (unsigned char)*p++;
245 + if (sq_lookup[ch] >= ' ') {
246 + EMIT(sq_lookup[ch]);
247 + } else {
248 + EMIT(((ch >> 6) & 03) + '0');
249 + EMIT(((ch >> 3) & 07) + '0');
250 + EMIT(((ch >> 0) & 07) + '0');
251 }
223 - EMIT(ch);
252 }
253 +
254 + EMITBUF(p, len);
255 + if (p == name) /* no ending quote needed */
256 + return 0;
257 +
258 if (!no_dq)
259 EMIT('"');
227 - if (outbuf)
228 - *outbuf = 0;
260 + return count;
261 +}
262
230 - return needquote ? count : 0;
263 +size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
264 +{
265 + return quote_c_style_counted(name, -1, sb, fp, nodq);
266 }
267
233 -int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
268 +void write_name_quoted(const char *name, FILE *fp, int terminator)
269 {
235 - int cnt = strlen(name);
236 - return quote_c_style_counted(name, cnt, outbuf, outfp, no_dq);
270 + if (terminator) {
271 + quote_c_style(name, NULL, fp, 0);
272 + } else {
273 + fputs(name, fp);
274 + }
275 + fputc(terminator, fp);
276 +}
277 +
278 +extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
279 + const char *name, FILE *fp, int terminator)
280 +{
281 + int needquote = 0;
282 +
283 + if (terminator) {
284 + needquote = next_quote_pos(pfx, pfxlen) < pfxlen
285 + || name[next_quote_pos(name, -1)];
286 + }
287 + if (needquote) {
288 + fputc('"', fp);
289 + quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
290 + quote_c_style(name, NULL, fp, 1);
291 + fputc('"', fp);
292 + } else {
293 + fwrite(pfx, pfxlen, 1, fp);
294 + fputs(name, fp);
295 + }
296 + fputc(terminator, fp);
297 }
298
299 /*
@@ -306,37 +366,6 @@ int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
366 return -1;
367 }
368
309 -void write_name_quoted(const char *prefix, int prefix_len,
310 - const char *name, int quote, FILE *out)
311 -{
312 - int needquote;
313 -
314 - if (!quote) {
315 - no_quote:
316 - if (prefix_len)
317 - fprintf(out, "%.*s", prefix_len, prefix);
318 - fputs(name, out);
319 - return;
320 - }
321 -
322 - needquote = 0;
323 - if (prefix_len)
324 - needquote = quote_c_style_counted(prefix, prefix_len,
325 - NULL, NULL, 0);
326 - if (!needquote)
327 - needquote = quote_c_style(name, NULL, NULL, 0);
328 - if (needquote) {
329 - fputc('"', out);
330 - if (prefix_len)
331 - quote_c_style_counted(prefix, prefix_len,
332 - NULL, out, 1);
333 - quote_c_style(name, NULL, out, 1);
334 - fputc('"', out);
335 - }
336 - else
337 - goto no_quote;
338 -}
339 -
369 /* quoting as a string literal for other languages */
370
371 void perl_quote_print(FILE *stream, const char *src)
quote.h
+4 -4
@@ -44,11 +44,11 @@ extern int add_to_string(char **ptrp, int *sizep, const char *str, int quote);
44 extern char *sq_dequote(char *);
45
46 extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
47 -extern int quote_c_style(const char *name, char *outbuf, FILE *outfp,
48 - int nodq);
47 +extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
48
50 -extern void write_name_quoted(const char *prefix, int prefix_len,
51 - const char *name, int quote, FILE *out);
49 +extern void write_name_quoted(const char *name, FILE *, int terminator);
50 +extern void write_name_quotedpfx(const char *pfx, size_t pfxlen,
51 + const char *name, FILE *, int terminator);
52
53 /* quoting as a string literal for other languages */
54 extern void perl_quote_print(FILE *stream, const char *src);