true built-in diff: run everything in-core.
This stops using temporary files when we are using the built-in diff (including the complete rewrite). Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Mar 25, 2006 at 23:12 UTC
cebff98dbe3fd6177337ae4d440b81ffed797608
1 file changed
+94
-162
diff.c
+94
-162
@@ -10,8 +10,6 @@
10
#include "diffcore.h"
11
#include "xdiff/xdiff.h"
12
13
-static const char *diff_opts = "-pu";
14
-
13
static int use_size_cache;
14
15
int diff_rename_limit_default = -1;
@@ -70,25 +68,10 @@ static const char *external_diff(void)
68
{
69
static const char *external_diff_cmd = NULL;
70
static int done_preparing = 0;
73
- const char *env_diff_opts;
71
72
if (done_preparing)
73
return external_diff_cmd;
77
-
78
- /*
79
- * Default values above are meant to match the
80
- * Linux kernel development style. Examples of
81
- * alternative styles you can specify via environment
82
- * variables are:
83
- *
84
- * GIT_DIFF_OPTS="-c";
85
- */
74
external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
87
-
88
- /* In case external diff fails... */
89
- env_diff_opts = getenv("GIT_DIFF_OPTS");
90
- if (env_diff_opts) diff_opts = env_diff_opts;
91
-
75
done_preparing = 1;
76
return external_diff_cmd;
77
}
@@ -102,13 +85,12 @@ static struct diff_tempfile {
85
char tmp_path[TEMPFILE_PATH_LEN];
86
} diff_temp[2];
87
105
-static int count_lines(const char *filename)
88
+static int count_lines(const char *data, int size)
89
{
107
- FILE *in;
90
int count, ch, completely_empty = 1, nl_just_seen = 0;
109
- in = fopen(filename, "r");
91
count = 0;
111
- while ((ch = fgetc(in)) != EOF)
92
+ while (0 < size--) {
93
+ ch = *data++;
94
if (ch == '\n') {
95
count++;
96
nl_just_seen = 1;
@@ -118,7 +100,7 @@ static int count_lines(const char *filename)
100
nl_just_seen = 0;
101
completely_empty = 0;
102
}
121
- fclose(in);
103
+ }
104
if (completely_empty)
105
return 0;
106
if (!nl_just_seen)
@@ -141,12 +123,11 @@ static void print_line_count(int count)
123
}
124
}
125
144
-static void copy_file(int prefix, const char *filename)
126
+static void copy_file(int prefix, const char *data, int size)
127
{
146
- FILE *in;
128
int ch, nl_just_seen = 1;
148
- in = fopen(filename, "r");
149
- while ((ch = fgetc(in)) != EOF) {
129
+ while (0 < size--) {
130
+ ch = *data++;
131
if (nl_just_seen)
132
putchar(prefix);
133
putchar(ch);
@@ -155,60 +136,41 @@ static void copy_file(int prefix, const char *filename)
136
else
137
nl_just_seen = 0;
138
}
158
- fclose(in);
139
if (!nl_just_seen)
140
printf("\n\\ No newline at end of file\n");
141
}
142
143
static void emit_rewrite_diff(const char *name_a,
144
const char *name_b,
165
- struct diff_tempfile *temp)
145
+ struct diff_filespec *one,
146
+ struct diff_filespec *two)
147
{
148
/* Use temp[i].name as input, name_a and name_b as labels */
149
int lc_a, lc_b;
169
- lc_a = count_lines(temp[0].name);
170
- lc_b = count_lines(temp[1].name);
150
+ lc_a = count_lines(one->data, one->size);
151
+ lc_b = count_lines(two->data, two->size);
152
printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
153
print_line_count(lc_a);
154
printf(" +");
155
print_line_count(lc_b);
156
printf(" @@\n");
157
if (lc_a)
177
- copy_file('-', temp[0].name);
158
+ copy_file('-', one->data, one->size);
159
if (lc_b)
179
- copy_file('+', temp[1].name);
160
+ copy_file('+', two->data, two->size);
161
}
162
182
-static int fill_mmfile(mmfile_t *mf, const char *file)
163
+static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
164
{
184
- int fd = open(file, O_RDONLY);
185
- struct stat st;
186
- char *buf;
187
- unsigned long size;
188
-
189
- mf->ptr = NULL;
190
- mf->size = 0;
191
- if (fd < 0)
165
+ if (!DIFF_FILE_VALID(one)) {
166
+ mf->ptr = ""; /* does not matter */
167
+ mf->size = 0;
168
return 0;
193
- fstat(fd, &st);
194
- size = st.st_size;
195
- buf = xmalloc(size);
196
- mf->ptr = buf;
197
- mf->size = size;
198
- while (size) {
199
- int retval = read(fd, buf, size);
200
- if (retval < 0) {
201
- if (errno == EINTR || errno == EAGAIN)
202
- continue;
203
- break;
204
- }
205
- if (!retval)
206
- break;
207
- buf += retval;
208
- size -= retval;
169
}
210
- mf->size -= size;
211
- close(fd);
170
+ else if (diff_populate_filespec(one, 0))
171
+ return -1;
172
+ mf->ptr = one->data;
173
+ mf->size = one->size;
174
return 0;
175
}
176
@@ -243,69 +205,37 @@ static int mmfile_is_binary(mmfile_t *mf)
205
return 0;
206
}
207
246
-static const char *builtin_diff(const char *name_a,
208
+static void builtin_diff(const char *name_a,
209
const char *name_b,
248
- struct diff_tempfile *temp,
210
+ struct diff_filespec *one,
211
+ struct diff_filespec *two,
212
const char *xfrm_msg,
250
- int complete_rewrite,
251
- const char **args)
213
+ int complete_rewrite)
214
{
253
- int i, next_at, cmd_size;
215
mmfile_t mf1, mf2;
255
- const char *const diff_cmd = "diff -L%s -L%s";
256
- const char *const diff_arg = "-- %s %s||:"; /* "||:" is to return 0 */
257
- const char *input_name_sq[2];
258
- const char *label_path[2];
259
- char *cmd;
260
-
261
- /* diff_cmd and diff_arg have 4 %s in total which makes
262
- * the sum of these strings 8 bytes larger than required.
263
- * we use 2 spaces around diff-opts, and we need to count
264
- * terminating NUL; we used to subtract 5 here, but we do not
265
- * care about small leaks in this subprocess that is about
266
- * to exec "diff" anymore.
267
- */
268
- cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg)
269
- + 128);
270
-
271
- for (i = 0; i < 2; i++) {
272
- input_name_sq[i] = sq_quote(temp[i].name);
273
- if (!strcmp(temp[i].name, "/dev/null"))
274
- label_path[i] = "/dev/null";
275
- else if (!i)
276
- label_path[i] = sq_quote(quote_two("a/", name_a));
277
- else
278
- label_path[i] = sq_quote(quote_two("b/", name_b));
279
- cmd_size += (strlen(label_path[i]) + strlen(input_name_sq[i]));
280
- }
281
-
282
- cmd = xmalloc(cmd_size);
283
-
284
- next_at = 0;
285
- next_at += snprintf(cmd+next_at, cmd_size-next_at,
286
- diff_cmd, label_path[0], label_path[1]);
287
- next_at += snprintf(cmd+next_at, cmd_size-next_at,
288
- " %s ", diff_opts);
289
- next_at += snprintf(cmd+next_at, cmd_size-next_at,
290
- diff_arg, input_name_sq[0], input_name_sq[1]);
291
-
292
- printf("diff --git %s %s\n",
293
- quote_two("a/", name_a), quote_two("b/", name_b));
294
- if (label_path[0][0] == '/') {
295
- /* dev/null */
296
- printf("new file mode %s\n", temp[1].mode);
216
+ const char *lbl[2];
217
+ char *a_one, *b_two;
218
+
219
+ a_one = quote_two("a/", name_a);
220
+ b_two = quote_two("b/", name_b);
221
+ lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
222
+ lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
223
+ printf("diff --git %s %s\n", a_one, b_two);
224
+ if (lbl[0][0] == '/') {
225
+ /* /dev/null */
226
+ printf("new file mode %06o\n", two->mode);
227
if (xfrm_msg && xfrm_msg[0])
228
puts(xfrm_msg);
229
}
300
- else if (label_path[1][0] == '/') {
301
- printf("deleted file mode %s\n", temp[0].mode);
230
+ else if (lbl[1][0] == '/') {
231
+ printf("deleted file mode %06o\n", one->mode);
232
if (xfrm_msg && xfrm_msg[0])
233
puts(xfrm_msg);
234
}
235
else {
306
- if (strcmp(temp[0].mode, temp[1].mode)) {
307
- printf("old mode %s\n", temp[0].mode);
308
- printf("new mode %s\n", temp[1].mode);
236
+ if (one->mode != two->mode) {
237
+ printf("old mode %06o\n", one->mode);
238
+ printf("new mode %06o\n", two->mode);
239
}
240
if (xfrm_msg && xfrm_msg[0])
241
puts(xfrm_msg);
@@ -313,27 +243,19 @@ static const char *builtin_diff(const char *name_a,
243
* we do not run diff between different kind
244
* of objects.
245
*/
316
- if (strncmp(temp[0].mode, temp[1].mode, 3))
317
- return NULL;
246
+ if ((one->mode ^ two->mode) & S_IFMT)
247
+ goto free_ab_and_return;
248
if (complete_rewrite) {
319
- emit_rewrite_diff(name_a, name_b, temp);
320
- return NULL;
249
+ emit_rewrite_diff(name_a, name_b, one, two);
250
+ goto free_ab_and_return;
251
}
252
}
253
324
- /* Un-quote the paths */
325
- if (label_path[0][0] != '/')
326
- label_path[0] = quote_two("a/", name_a);
327
- if (label_path[1][0] != '/')
328
- label_path[1] = quote_two("b/", name_b);
329
-
330
- if (fill_mmfile(&mf1, temp[0].name) < 0 ||
331
- fill_mmfile(&mf2, temp[1].name) < 0)
254
+ if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
255
die("unable to read files to diff");
256
257
if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
335
- printf("Binary files %s and %s differ\n",
336
- label_path[0], label_path[1]);
258
+ printf("Binary files %s and %s differ\n", lbl[0], lbl[1]);
259
else {
260
/* Crazy xdl interfaces.. */
261
const char *diffopts = getenv("GIT_DIFF_OPTS");
@@ -342,7 +264,7 @@ static const char *builtin_diff(const char *name_a,
264
xdemitcb_t ecb;
265
struct emit_callback ecbdata;
266
345
- ecbdata.label_path = label_path;
267
+ ecbdata.label_path = lbl;
268
xpp.flags = XDF_NEED_MINIMAL;
269
xecfg.ctxlen = 3;
270
if (!diffopts)
@@ -356,9 +278,10 @@ static const char *builtin_diff(const char *name_a,
278
xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
279
}
280
359
- free(mf1.ptr);
360
- free(mf2.ptr);
361
- return NULL;
281
+ free_ab_and_return:
282
+ free(a_one);
283
+ free(b_two);
284
+ return;
285
}
286
287
struct diff_filespec *alloc_filespec(const char *path)
@@ -716,6 +639,7 @@ static void run_external_diff(const char *pgm,
639
int retval;
640
static int atexit_asked = 0;
641
const char *othername;
642
+ const char **arg = &spawn_arg[0];
643
644
othername = (other? other : name);
645
if (one && two) {
@@ -730,36 +654,25 @@ static void run_external_diff(const char *pgm,
654
signal(SIGINT, remove_tempfile_on_signal);
655
}
656
733
- if (pgm) {
734
- const char **arg = &spawn_arg[0];
735
- if (one && two) {
736
- *arg++ = pgm;
737
- *arg++ = name;
738
- *arg++ = temp[0].name;
739
- *arg++ = temp[0].hex;
740
- *arg++ = temp[0].mode;
741
- *arg++ = temp[1].name;
742
- *arg++ = temp[1].hex;
743
- *arg++ = temp[1].mode;
744
- if (other) {
745
- *arg++ = other;
746
- *arg++ = xfrm_msg;
747
- }
748
- } else {
749
- *arg++ = pgm;
750
- *arg++ = name;
657
+ if (one && two) {
658
+ *arg++ = pgm;
659
+ *arg++ = name;
660
+ *arg++ = temp[0].name;
661
+ *arg++ = temp[0].hex;
662
+ *arg++ = temp[0].mode;
663
+ *arg++ = temp[1].name;
664
+ *arg++ = temp[1].hex;
665
+ *arg++ = temp[1].mode;
666
+ if (other) {
667
+ *arg++ = other;
668
+ *arg++ = xfrm_msg;
669
}
752
- *arg = NULL;
670
} else {
754
- if (one && two) {
755
- pgm = builtin_diff(name, othername, temp, xfrm_msg, complete_rewrite, spawn_arg);
756
- } else
757
- printf("* Unmerged path %s\n", name);
671
+ *arg++ = pgm;
672
+ *arg++ = name;
673
}
759
-
760
- retval = 0;
761
- if (pgm)
762
- retval = spawn_prog(pgm, spawn_arg);
674
+ *arg = NULL;
675
+ retval = spawn_prog(pgm, spawn_arg);
676
remove_tempfile();
677
if (retval) {
678
fprintf(stderr, "external diff died, stopping at %s.\n", name);
@@ -767,6 +680,26 @@ static void run_external_diff(const char *pgm,
680
}
681
}
682
683
+static void run_diff_cmd(const char *pgm,
684
+ const char *name,
685
+ const char *other,
686
+ struct diff_filespec *one,
687
+ struct diff_filespec *two,
688
+ const char *xfrm_msg,
689
+ int complete_rewrite)
690
+{
691
+ if (pgm) {
692
+ run_external_diff(pgm, name, other, one, two, xfrm_msg,
693
+ complete_rewrite);
694
+ return;
695
+ }
696
+ if (one && two)
697
+ builtin_diff(name, other ? other : name,
698
+ one, two, xfrm_msg, complete_rewrite);
699
+ else
700
+ printf("* Unmerged path %s\n", name);
701
+}
702
+
703
static void diff_fill_sha1_info(struct diff_filespec *one)
704
{
705
if (DIFF_FILE_VALID(one)) {
@@ -796,8 +729,7 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
729
730
if (DIFF_PAIR_UNMERGED(p)) {
731
/* unmerged */
799
- run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL,
800
- 0);
732
+ run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0);
733
return;
734
}
735
@@ -869,15 +801,15 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
801
* needs to be split into deletion and creation.
802
*/
803
struct diff_filespec *null = alloc_filespec(two->path);
872
- run_external_diff(NULL, name, other, one, null, xfrm_msg, 0);
804
+ run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0);
805
free(null);
806
null = alloc_filespec(one->path);
875
- run_external_diff(NULL, name, other, null, two, xfrm_msg, 0);
807
+ run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0);
808
free(null);
809
}
810
else
879
- run_external_diff(pgm, name, other, one, two, xfrm_msg,
880
- complete_rewrite);
811
+ run_diff_cmd(pgm, name, other, one, two, xfrm_msg,
812
+ complete_rewrite);
813
814
free(name_munged);
815
free(other_munged);