-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.c
2357 lines (2135 loc) · 57 KB
/
diff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005 Junio C Hamano
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "cache.h"
#include "quote.h"
#include "diff.h"
#include "diffcore.h"
#include "delta.h"
#include "xdiff-interface.h"
static int use_size_cache;
static int diff_rename_limit_default = -1;
static int diff_use_color_default = 0;
enum color_diff {
DIFF_RESET = 0,
DIFF_PLAIN = 1,
DIFF_METAINFO = 2,
DIFF_FRAGINFO = 3,
DIFF_FILE_OLD = 4,
DIFF_FILE_NEW = 5,
};
#define COLOR_NORMAL ""
#define COLOR_BOLD "\033[1m"
#define COLOR_DIM "\033[2m"
#define COLOR_UL "\033[4m"
#define COLOR_BLINK "\033[5m"
#define COLOR_REVERSE "\033[7m"
#define COLOR_RESET "\033[m"
#define COLOR_BLACK "\033[30m"
#define COLOR_RED "\033[31m"
#define COLOR_GREEN "\033[32m"
#define COLOR_YELLOW "\033[33m"
#define COLOR_BLUE "\033[34m"
#define COLOR_MAGENTA "\033[35m"
#define COLOR_CYAN "\033[36m"
#define COLOR_WHITE "\033[37m"
static const char *diff_colors[] = {
[DIFF_RESET] = COLOR_RESET,
[DIFF_PLAIN] = COLOR_NORMAL,
[DIFF_METAINFO] = COLOR_BOLD,
[DIFF_FRAGINFO] = COLOR_CYAN,
[DIFF_FILE_OLD] = COLOR_RED,
[DIFF_FILE_NEW] = COLOR_GREEN,
};
static int parse_diff_color_slot(const char *var, int ofs)
{
if (!strcasecmp(var+ofs, "plain"))
return DIFF_PLAIN;
if (!strcasecmp(var+ofs, "meta"))
return DIFF_METAINFO;
if (!strcasecmp(var+ofs, "frag"))
return DIFF_FRAGINFO;
if (!strcasecmp(var+ofs, "old"))
return DIFF_FILE_OLD;
if (!strcasecmp(var+ofs, "new"))
return DIFF_FILE_NEW;
die("bad config variable '%s'", var);
}
static const char *parse_diff_color_value(const char *value, const char *var)
{
if (!strcasecmp(value, "normal"))
return COLOR_NORMAL;
if (!strcasecmp(value, "bold"))
return COLOR_BOLD;
if (!strcasecmp(value, "dim"))
return COLOR_DIM;
if (!strcasecmp(value, "ul"))
return COLOR_UL;
if (!strcasecmp(value, "blink"))
return COLOR_BLINK;
if (!strcasecmp(value, "reverse"))
return COLOR_REVERSE;
if (!strcasecmp(value, "reset"))
return COLOR_RESET;
if (!strcasecmp(value, "black"))
return COLOR_BLACK;
if (!strcasecmp(value, "red"))
return COLOR_RED;
if (!strcasecmp(value, "green"))
return COLOR_GREEN;
if (!strcasecmp(value, "yellow"))
return COLOR_YELLOW;
if (!strcasecmp(value, "blue"))
return COLOR_BLUE;
if (!strcasecmp(value, "magenta"))
return COLOR_MAGENTA;
if (!strcasecmp(value, "cyan"))
return COLOR_CYAN;
if (!strcasecmp(value, "white"))
return COLOR_WHITE;
die("bad config value '%s' for variable '%s'", value, var);
}
int git_diff_config(const char *var, const char *value)
{
if (!strcmp(var, "diff.renamelimit")) {
diff_rename_limit_default = git_config_int(var, value);
return 0;
}
if (!strcmp(var, "diff.color")) {
if (!value)
diff_use_color_default = 1; /* bool */
else if (!strcasecmp(value, "auto"))
diff_use_color_default = isatty(1);
else if (!strcasecmp(value, "never"))
diff_use_color_default = 0;
else if (!strcasecmp(value, "always"))
diff_use_color_default = 1;
else
diff_use_color_default = git_config_bool(var, value);
return 0;
}
if (!strncmp(var, "diff.color.", 11)) {
int slot = parse_diff_color_slot(var, 11);
diff_colors[slot] = parse_diff_color_value(value, var);
return 0;
}
return git_default_config(var, value);
}
static char *quote_one(const char *str)
{
int needlen;
char *xp;
if (!str)
return NULL;
needlen = quote_c_style(str, NULL, NULL, 0);
if (!needlen)
return strdup(str);
xp = xmalloc(needlen + 1);
quote_c_style(str, xp, NULL, 0);
return xp;
}
static char *quote_two(const char *one, const char *two)
{
int need_one = quote_c_style(one, NULL, NULL, 1);
int need_two = quote_c_style(two, NULL, NULL, 1);
char *xp;
if (need_one + need_two) {
if (!need_one) need_one = strlen(one);
if (!need_two) need_one = strlen(two);
xp = xmalloc(need_one + need_two + 3);
xp[0] = '"';
quote_c_style(one, xp + 1, NULL, 1);
quote_c_style(two, xp + need_one + 1, NULL, 1);
strcpy(xp + need_one + need_two + 1, "\"");
return xp;
}
need_one = strlen(one);
need_two = strlen(two);
xp = xmalloc(need_one + need_two + 1);
strcpy(xp, one);
strcpy(xp + need_one, two);
return xp;
}
static const char *external_diff(void)
{
static const char *external_diff_cmd = NULL;
static int done_preparing = 0;
if (done_preparing)
return external_diff_cmd;
external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
done_preparing = 1;
return external_diff_cmd;
}
#define TEMPFILE_PATH_LEN 50
static struct diff_tempfile {
const char *name; /* filename external diff should read from */
char hex[41];
char mode[10];
char tmp_path[TEMPFILE_PATH_LEN];
} diff_temp[2];
static int count_lines(const char *data, int size)
{
int count, ch, completely_empty = 1, nl_just_seen = 0;
count = 0;
while (0 < size--) {
ch = *data++;
if (ch == '\n') {
count++;
nl_just_seen = 1;
completely_empty = 0;
}
else {
nl_just_seen = 0;
completely_empty = 0;
}
}
if (completely_empty)
return 0;
if (!nl_just_seen)
count++; /* no trailing newline */
return count;
}
static void print_line_count(int count)
{
switch (count) {
case 0:
printf("0,0");
break;
case 1:
printf("1");
break;
default:
printf("1,%d", count);
break;
}
}
static void copy_file(int prefix, const char *data, int size)
{
int ch, nl_just_seen = 1;
while (0 < size--) {
ch = *data++;
if (nl_just_seen)
putchar(prefix);
putchar(ch);
if (ch == '\n')
nl_just_seen = 1;
else
nl_just_seen = 0;
}
if (!nl_just_seen)
printf("\n\\ No newline at end of file\n");
}
static void emit_rewrite_diff(const char *name_a,
const char *name_b,
struct diff_filespec *one,
struct diff_filespec *two)
{
int lc_a, lc_b;
diff_populate_filespec(one, 0);
diff_populate_filespec(two, 0);
lc_a = count_lines(one->data, one->size);
lc_b = count_lines(two->data, two->size);
printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
print_line_count(lc_a);
printf(" +");
print_line_count(lc_b);
printf(" @@\n");
if (lc_a)
copy_file('-', one->data, one->size);
if (lc_b)
copy_file('+', two->data, two->size);
}
static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
{
if (!DIFF_FILE_VALID(one)) {
mf->ptr = (char *)""; /* does not matter */
mf->size = 0;
return 0;
}
else if (diff_populate_filespec(one, 0))
return -1;
mf->ptr = one->data;
mf->size = one->size;
return 0;
}
struct emit_callback {
struct xdiff_emit_state xm;
int nparents, color_diff;
const char **label_path;
};
static inline const char *get_color(int diff_use_color, enum color_diff ix)
{
if (diff_use_color)
return diff_colors[ix];
return "";
}
static void fn_out_consume(void *priv, char *line, unsigned long len)
{
int i;
struct emit_callback *ecbdata = priv;
const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO);
const char *reset = get_color(ecbdata->color_diff, DIFF_RESET);
if (ecbdata->label_path[0]) {
printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
}
/* This is not really necessary for now because
* this codepath only deals with two-way diffs.
*/
for (i = 0; i < len && line[i] == '@'; i++)
;
if (2 <= i && i < len && line[i] == ' ') {
ecbdata->nparents = i - 1;
set = get_color(ecbdata->color_diff, DIFF_FRAGINFO);
}
else if (len < ecbdata->nparents)
set = reset;
else {
int nparents = ecbdata->nparents;
int color = DIFF_PLAIN;
for (i = 0; i < nparents && len; i++) {
if (line[i] == '-')
color = DIFF_FILE_OLD;
else if (line[i] == '+')
color = DIFF_FILE_NEW;
}
set = get_color(ecbdata->color_diff, color);
}
if (len > 0 && line[len-1] == '\n')
len--;
printf("%s%.*s%s\n", set, (int) len, line, reset);
}
static char *pprint_rename(const char *a, const char *b)
{
const char *old = a;
const char *new = b;
char *name = NULL;
int pfx_length, sfx_length;
int len_a = strlen(a);
int len_b = strlen(b);
/* Find common prefix */
pfx_length = 0;
while (*old && *new && *old == *new) {
if (*old == '/')
pfx_length = old - a + 1;
old++;
new++;
}
/* Find common suffix */
old = a + len_a;
new = b + len_b;
sfx_length = 0;
while (a <= old && b <= new && *old == *new) {
if (*old == '/')
sfx_length = len_a - (old - a);
old--;
new--;
}
/*
* pfx{mid-a => mid-b}sfx
* {pfx-a => pfx-b}sfx
* pfx{sfx-a => sfx-b}
* name-a => name-b
*/
if (pfx_length + sfx_length) {
int a_midlen = len_a - pfx_length - sfx_length;
int b_midlen = len_b - pfx_length - sfx_length;
if (a_midlen < 0) a_midlen = 0;
if (b_midlen < 0) b_midlen = 0;
name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
sprintf(name, "%.*s{%.*s => %.*s}%s",
pfx_length, a,
a_midlen, a + pfx_length,
b_midlen, b + pfx_length,
a + len_a - sfx_length);
}
else {
name = xmalloc(len_a + len_b + 5);
sprintf(name, "%s => %s", a, b);
}
return name;
}
struct diffstat_t {
struct xdiff_emit_state xm;
int nr;
int alloc;
struct diffstat_file {
char *name;
unsigned is_unmerged:1;
unsigned is_binary:1;
unsigned is_renamed:1;
unsigned int added, deleted;
} **files;
};
static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
const char *name_a,
const char *name_b)
{
struct diffstat_file *x;
x = xcalloc(sizeof (*x), 1);
if (diffstat->nr == diffstat->alloc) {
diffstat->alloc = alloc_nr(diffstat->alloc);
diffstat->files = xrealloc(diffstat->files,
diffstat->alloc * sizeof(x));
}
diffstat->files[diffstat->nr++] = x;
if (name_b) {
x->name = pprint_rename(name_a, name_b);
x->is_renamed = 1;
}
else
x->name = strdup(name_a);
return x;
}
static void diffstat_consume(void *priv, char *line, unsigned long len)
{
struct diffstat_t *diffstat = priv;
struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
if (line[0] == '+')
x->added++;
else if (line[0] == '-')
x->deleted++;
}
static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
static const char minuses[]= "----------------------------------------------------------------------";
const char mime_boundary_leader[] = "------------";
static void show_stats(struct diffstat_t* data)
{
int i, len, add, del, total, adds = 0, dels = 0;
int max, max_change = 0, max_len = 0;
int total_files = data->nr;
if (data->nr == 0)
return;
for (i = 0; i < data->nr; i++) {
struct diffstat_file *file = data->files[i];
len = strlen(file->name);
if (max_len < len)
max_len = len;
if (file->is_binary || file->is_unmerged)
continue;
if (max_change < file->added + file->deleted)
max_change = file->added + file->deleted;
}
for (i = 0; i < data->nr; i++) {
const char *prefix = "";
char *name = data->files[i]->name;
int added = data->files[i]->added;
int deleted = data->files[i]->deleted;
if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
char *qname = xmalloc(len + 1);
quote_c_style(name, qname, NULL, 0);
free(name);
data->files[i]->name = name = qname;
}
/*
* "scale" the filename
*/
len = strlen(name);
max = max_len;
if (max > 50)
max = 50;
if (len > max) {
char *slash;
prefix = "...";
max -= 3;
name += len - max;
slash = strchr(name, '/');
if (slash)
name = slash;
}
len = max;
/*
* scale the add/delete
*/
max = max_change;
if (max + len > 70)
max = 70 - len;
if (data->files[i]->is_binary) {
printf(" %s%-*s | Bin\n", prefix, len, name);
goto free_diffstat_file;
}
else if (data->files[i]->is_unmerged) {
printf(" %s%-*s | Unmerged\n", prefix, len, name);
goto free_diffstat_file;
}
else if (!data->files[i]->is_renamed &&
(added + deleted == 0)) {
total_files--;
goto free_diffstat_file;
}
add = added;
del = deleted;
total = add + del;
adds += add;
dels += del;
if (max_change > 0) {
total = (total * max + max_change / 2) / max_change;
add = (add * max + max_change / 2) / max_change;
del = total - add;
}
printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
len, name, added + deleted,
add, pluses, del, minuses);
free_diffstat_file:
free(data->files[i]->name);
free(data->files[i]);
}
free(data->files);
printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
total_files, adds, dels);
}
struct checkdiff_t {
struct xdiff_emit_state xm;
const char *filename;
int lineno;
};
static void checkdiff_consume(void *priv, char *line, unsigned long len)
{
struct checkdiff_t *data = priv;
if (line[0] == '+') {
int i, spaces = 0;
data->lineno++;
/* check space before tab */
for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
if (line[i] == ' ')
spaces++;
if (line[i - 1] == '\t' && spaces)
printf("%s:%d: space before tab:%.*s\n",
data->filename, data->lineno, (int)len, line);
/* check white space at line end */
if (line[len - 1] == '\n')
len--;
if (isspace(line[len - 1]))
printf("%s:%d: white space at end: %.*s\n",
data->filename, data->lineno, (int)len, line);
} else if (line[0] == ' ')
data->lineno++;
else if (line[0] == '@') {
char *plus = strchr(line, '+');
if (plus)
data->lineno = strtol(plus, NULL, 10);
else
die("invalid diff");
}
}
static unsigned char *deflate_it(char *data,
unsigned long size,
unsigned long *result_size)
{
int bound;
unsigned char *deflated;
z_stream stream;
memset(&stream, 0, sizeof(stream));
deflateInit(&stream, Z_BEST_COMPRESSION);
bound = deflateBound(&stream, size);
deflated = xmalloc(bound);
stream.next_out = deflated;
stream.avail_out = bound;
stream.next_in = (unsigned char *)data;
stream.avail_in = size;
while (deflate(&stream, Z_FINISH) == Z_OK)
; /* nothing */
deflateEnd(&stream);
*result_size = stream.total_out;
return deflated;
}
static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
{
void *cp;
void *delta;
void *deflated;
void *data;
unsigned long orig_size;
unsigned long delta_size;
unsigned long deflate_size;
unsigned long data_size;
printf("GIT binary patch\n");
/* We could do deflated delta, or we could do just deflated two,
* whichever is smaller.
*/
delta = NULL;
deflated = deflate_it(two->ptr, two->size, &deflate_size);
if (one->size && two->size) {
delta = diff_delta(one->ptr, one->size,
two->ptr, two->size,
&delta_size, deflate_size);
if (delta) {
void *to_free = delta;
orig_size = delta_size;
delta = deflate_it(delta, delta_size, &delta_size);
free(to_free);
}
}
if (delta && delta_size < deflate_size) {
printf("delta %lu\n", orig_size);
free(deflated);
data = delta;
data_size = delta_size;
}
else {
printf("literal %lu\n", two->size);
free(delta);
data = deflated;
data_size = deflate_size;
}
/* emit data encoded in base85 */
cp = data;
while (data_size) {
int bytes = (52 < data_size) ? 52 : data_size;
char line[70];
data_size -= bytes;
if (bytes <= 26)
line[0] = bytes + 'A' - 1;
else
line[0] = bytes - 26 + 'a' - 1;
encode_85(line + 1, cp, bytes);
cp = (char *) cp + bytes;
puts(line);
}
printf("\n");
free(data);
}
#define FIRST_FEW_BYTES 8000
static int mmfile_is_binary(mmfile_t *mf)
{
long sz = mf->size;
if (FIRST_FEW_BYTES < sz)
sz = FIRST_FEW_BYTES;
if (memchr(mf->ptr, 0, sz))
return 1;
return 0;
}
static void builtin_diff(const char *name_a,
const char *name_b,
struct diff_filespec *one,
struct diff_filespec *two,
const char *xfrm_msg,
struct diff_options *o,
int complete_rewrite)
{
mmfile_t mf1, mf2;
const char *lbl[2];
char *a_one, *b_two;
const char *set = get_color(o->color_diff, DIFF_METAINFO);
const char *reset = get_color(o->color_diff, DIFF_RESET);
a_one = quote_two("a/", name_a);
b_two = quote_two("b/", name_b);
lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
if (lbl[0][0] == '/') {
/* /dev/null */
printf("%snew file mode %06o%s\n", set, two->mode, reset);
if (xfrm_msg && xfrm_msg[0])
printf("%s%s%s\n", set, xfrm_msg, reset);
}
else if (lbl[1][0] == '/') {
printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
if (xfrm_msg && xfrm_msg[0])
printf("%s%s%s\n", set, xfrm_msg, reset);
}
else {
if (one->mode != two->mode) {
printf("%sold mode %06o%s\n", set, one->mode, reset);
printf("%snew mode %06o%s\n", set, two->mode, reset);
}
if (xfrm_msg && xfrm_msg[0])
printf("%s%s%s\n", set, xfrm_msg, reset);
/*
* we do not run diff between different kind
* of objects.
*/
if ((one->mode ^ two->mode) & S_IFMT)
goto free_ab_and_return;
if (complete_rewrite) {
emit_rewrite_diff(name_a, name_b, one, two);
goto free_ab_and_return;
}
}
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
die("unable to read files to diff");
if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) {
/* Quite common confusing case */
if (mf1.size == mf2.size &&
!memcmp(mf1.ptr, mf2.ptr, mf1.size))
goto free_ab_and_return;
if (o->binary)
emit_binary_diff(&mf1, &mf2);
else
printf("Binary files %s and %s differ\n",
lbl[0], lbl[1]);
}
else {
/* Crazy xdl interfaces.. */
const char *diffopts = getenv("GIT_DIFF_OPTS");
xpparam_t xpp;
xdemitconf_t xecfg;
xdemitcb_t ecb;
struct emit_callback ecbdata;
memset(&ecbdata, 0, sizeof(ecbdata));
ecbdata.label_path = lbl;
ecbdata.color_diff = o->color_diff;
xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
xecfg.ctxlen = o->context;
xecfg.flags = XDL_EMIT_FUNCNAMES;
if (!diffopts)
;
else if (!strncmp(diffopts, "--unified=", 10))
xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
else if (!strncmp(diffopts, "-u", 2))
xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
ecb.outf = xdiff_outf;
ecb.priv = &ecbdata;
ecbdata.xm.consume = fn_out_consume;
xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
}
free_ab_and_return:
free(a_one);
free(b_two);
return;
}
static void builtin_diffstat(const char *name_a, const char *name_b,
struct diff_filespec *one,
struct diff_filespec *two,
struct diffstat_t *diffstat,
struct diff_options *o,
int complete_rewrite)
{
mmfile_t mf1, mf2;
struct diffstat_file *data;
data = diffstat_add(diffstat, name_a, name_b);
if (!one || !two) {
data->is_unmerged = 1;
return;
}
if (complete_rewrite) {
diff_populate_filespec(one, 0);
diff_populate_filespec(two, 0);
data->deleted = count_lines(one->data, one->size);
data->added = count_lines(two->data, two->size);
return;
}
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
die("unable to read files to diff");
if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
data->is_binary = 1;
else {
/* Crazy xdl interfaces.. */
xpparam_t xpp;
xdemitconf_t xecfg;
xdemitcb_t ecb;
xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
xecfg.ctxlen = 0;
xecfg.flags = 0;
ecb.outf = xdiff_outf;
ecb.priv = diffstat;
xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
}
}
static void builtin_checkdiff(const char *name_a, const char *name_b,
struct diff_filespec *one,
struct diff_filespec *two)
{
mmfile_t mf1, mf2;
struct checkdiff_t data;
if (!two)
return;
memset(&data, 0, sizeof(data));
data.xm.consume = checkdiff_consume;
data.filename = name_b ? name_b : name_a;
data.lineno = 0;
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
die("unable to read files to diff");
if (mmfile_is_binary(&mf2))
return;
else {
/* Crazy xdl interfaces.. */
xpparam_t xpp;
xdemitconf_t xecfg;
xdemitcb_t ecb;
xpp.flags = XDF_NEED_MINIMAL;
xecfg.ctxlen = 0;
xecfg.flags = 0;
ecb.outf = xdiff_outf;
ecb.priv = &data;
xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
}
}
struct diff_filespec *alloc_filespec(const char *path)
{
int namelen = strlen(path);
struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
memset(spec, 0, sizeof(*spec));
spec->path = (char *)(spec + 1);
memcpy(spec->path, path, namelen+1);
return spec;
}
void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
unsigned short mode)
{
if (mode) {
spec->mode = canon_mode(mode);
memcpy(spec->sha1, sha1, 20);
spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
}
}
/*
* Given a name and sha1 pair, if the dircache tells us the file in
* the work tree has that object contents, return true, so that
* prepare_temp_file() does not have to inflate and extract.
*/
static int work_tree_matches(const char *name, const unsigned char *sha1)
{
struct cache_entry *ce;
struct stat st;
int pos, len;
/* We do not read the cache ourselves here, because the
* benchmark with my previous version that always reads cache
* shows that it makes things worse for diff-tree comparing
* two linux-2.6 kernel trees in an already checked out work
* tree. This is because most diff-tree comparisons deal with
* only a small number of files, while reading the cache is
* expensive for a large project, and its cost outweighs the
* savings we get by not inflating the object to a temporary
* file. Practically, this code only helps when we are used
* by diff-cache --cached, which does read the cache before
* calling us.
*/
if (!active_cache)
return 0;
len = strlen(name);
pos = cache_name_pos(name, len);
if (pos < 0)
return 0;
ce = active_cache[pos];
if ((lstat(name, &st) < 0) ||
!S_ISREG(st.st_mode) || /* careful! */
ce_match_stat(ce, &st, 0) ||
memcmp(sha1, ce->sha1, 20))
return 0;
/* we return 1 only when we can stat, it is a regular file,
* stat information matches, and sha1 recorded in the cache
* matches. I.e. we know the file in the work tree really is
* the same as the <name, sha1> pair.
*/
return 1;
}
static struct sha1_size_cache {
unsigned char sha1[20];
unsigned long size;
} **sha1_size_cache;
static int sha1_size_cache_nr, sha1_size_cache_alloc;
static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
int find_only,
unsigned long size)
{
int first, last;
struct sha1_size_cache *e;
first = 0;
last = sha1_size_cache_nr;
while (last > first) {
int cmp, next = (last + first) >> 1;
e = sha1_size_cache[next];
cmp = memcmp(e->sha1, sha1, 20);
if (!cmp)
return e;
if (cmp < 0) {
last = next;
continue;
}
first = next+1;
}
/* not found */
if (find_only)
return NULL;
/* insert to make it at "first" */
if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
sha1_size_cache = xrealloc(sha1_size_cache,
sha1_size_cache_alloc *
sizeof(*sha1_size_cache));
}
sha1_size_cache_nr++;
if (first < sha1_size_cache_nr)
memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
(sha1_size_cache_nr - first - 1) *
sizeof(*sha1_size_cache));
e = xmalloc(sizeof(struct sha1_size_cache));
sha1_size_cache[first] = e;
memcpy(e->sha1, sha1, 20);
e->size = size;
return e;
}
/*
* While doing rename detection and pickaxe operation, we may need to
* grab the data for the blob (or file) for our own in-core comparison.
* diff_filespec has data and size fields for this purpose.
*/
int diff_populate_filespec(struct diff_filespec *s, int size_only)
{
int err = 0;
if (!DIFF_FILE_VALID(s))
die("internal error: asking to populate invalid file.");
if (S_ISDIR(s->mode))
return -1;
if (!use_size_cache)
size_only = 0;
if (s->data)
return err;
if (!s->sha1_valid ||
work_tree_matches(s->path, s->sha1)) {
struct stat st;
int fd;
if (lstat(s->path, &st) < 0) {
if (errno == ENOENT) {
err_empty:
err = -1;
empty:
s->data = (char *)"";
s->size = 0;
return err;
}
}
s->size = st.st_size;
if (!s->size)
goto empty;
if (size_only)
return 0;
if (S_ISLNK(st.st_mode)) {
int ret;
s->data = xmalloc(s->size);
s->should_free = 1;
ret = readlink(s->path, s->data, s->size);