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)
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 */
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 {
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
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
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;
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) &&
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,
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)
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
*/
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
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
}
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)