2 * Copyright (C) 2005 Junio C Hamano
9 static char *diff_cmd
= "diff -L'k/%s' -L'l/%s'";
10 static char *diff_opts
= "-pu";
12 static const char *external_diff(void)
14 static char *external_diff_cmd
= NULL
;
15 static int done_preparing
= 0;
18 return external_diff_cmd
;
21 * Default values above are meant to match the
22 * Linux kernel development style. Examples of
23 * alternative styles you can specify via environment
26 * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
29 if (getenv("GIT_EXTERNAL_DIFF"))
30 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
32 /* In case external diff fails... */
33 diff_cmd
= getenv("GIT_DIFF_CMD") ? : diff_cmd
;
34 diff_opts
= getenv("GIT_DIFF_OPTS") ? : diff_opts
;
37 return external_diff_cmd
;
40 /* Help to copy the thing properly quoted for the shell safety.
41 * any single quote is replaced with '\'', and the caller is
42 * expected to enclose the result within a single quote pair.
45 * original sq_expand result
46 * name ==> name ==> 'name'
47 * a b ==> a b ==> 'a b'
48 * a'b ==> a'\''b ==> 'a'\''b'
50 static char *sq_expand(const char *src
)
52 static char *buf
= NULL
;
57 /* count bytes needed to store the quoted string. */
58 for (cnt
= 1, cp
= src
; *cp
; cnt
++, cp
++)
64 while ((c
= *src
++)) {
68 bp
= strcpy(bp
, "'\\''");
76 static struct diff_tempfile
{
83 static void builtin_diff(const char *name
,
84 struct diff_tempfile
*temp
)
86 static char *diff_arg
= "'%s' '%s'";
87 const char *name_1_sq
= sq_expand(temp
[0].name
);
88 const char *name_2_sq
= sq_expand(temp
[1].name
);
89 const char *name_sq
= sq_expand(name
);
91 /* diff_cmd and diff_arg have 4 %s in total which makes
92 * the sum of these strings 8 bytes larger than required.
93 * we use 2 spaces around diff-opts, and we need to count
94 * terminating NUL, so we subtract 5 here.
96 int cmd_size
= (strlen(diff_cmd
) +
100 strlen(name_1_sq
) + strlen(name_2_sq
)
102 char *cmd
= xmalloc(cmd_size
);
105 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
106 diff_cmd
, name_sq
, name_sq
);
107 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
109 next_at
+= snprintf(cmd
+next_at
, cmd_size
-next_at
,
110 diff_arg
, name_1_sq
, name_2_sq
);
111 execlp("/bin/sh","sh", "-c", cmd
, NULL
);
114 static void prepare_temp_file(const char *name
,
115 struct diff_tempfile
*temp
,
116 struct diff_spec
*one
)
118 static unsigned char null_sha1
[20] = { 0, };
120 if (!one
->file_valid
) {
122 temp
->name
= "/dev/null";
123 strcpy(temp
->hex
, ".");
124 strcpy(temp
->mode
, ".");
128 if (one
->sha1_valid
&&
129 !memcmp(one
->u
.sha1
, null_sha1
, sizeof(null_sha1
))) {
134 if (!one
->sha1_valid
) {
136 temp
->name
= one
->u
.name
;
137 if (stat(temp
->name
, &st
) < 0) {
139 goto not_a_valid_file
;
140 die("stat(%s): %s", temp
->name
, strerror(errno
));
142 strcpy(temp
->hex
, ".");
143 sprintf(temp
->mode
, "%06o",
144 S_IFREG
|ce_permissions(st
.st_mode
));
152 blob
= read_sha1_file(one
->u
.sha1
, type
, &size
);
153 if (!blob
|| strcmp(type
, "blob"))
154 die("unable to read blob object for %s (%s)",
155 name
, sha1_to_hex(one
->u
.sha1
));
157 strcpy(temp
->tmp_path
, ".diff_XXXXXX");
158 fd
= mkstemp(temp
->tmp_path
);
160 die("unable to create temp-file");
161 if (write(fd
, blob
, size
) != size
)
162 die("unable to write temp-file");
165 temp
->name
= temp
->tmp_path
;
166 strcpy(temp
->hex
, sha1_to_hex(one
->u
.sha1
));
168 sprintf(temp
->mode
, "%06o", one
->mode
);
172 static void remove_tempfile(void)
176 for (i
= 0; i
< 2; i
++)
177 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
178 unlink(diff_temp
[i
].name
);
179 diff_temp
[i
].name
= NULL
;
183 /* An external diff command takes:
185 * diff-cmd name infile1 infile1-sha1 infile1-mode \
186 * infile2 infile2-sha1 infile2-mode.
189 void run_external_diff(const char *name
,
190 struct diff_spec
*one
,
191 struct diff_spec
*two
)
193 struct diff_tempfile
*temp
= diff_temp
;
195 static int atexit_asked
= 0;
197 prepare_temp_file(name
, &temp
[0], one
);
198 prepare_temp_file(name
, &temp
[1], two
);
199 if (! atexit_asked
&&
200 (temp
[0].name
== temp
[0].tmp_path
||
201 temp
[1].name
== temp
[1].tmp_path
)) {
203 atexit(remove_tempfile
);
209 die("unable to fork");
211 const char *pgm
= external_diff();
215 temp
[0].name
, temp
[0].hex
, temp
[0].mode
,
216 temp
[1].name
, temp
[1].hex
, temp
[1].mode
,
219 * otherwise we use the built-in one.
221 builtin_diff(name
, temp
);
224 if (waitpid(pid
, &status
, 0) < 0 || !WIFEXITED(status
))
225 die("diff program failed");
230 void show_diff_empty(const struct cache_entry
*ce
, int reverse
)
232 struct diff_spec spec
[2], *one
, *two
;
234 memcpy(spec
[0].u
.sha1
, ce
->sha1
, 20);
235 spec
[0].mode
= ntohl(ce
->ce_mode
);
236 spec
[0].sha1_valid
= spec
[0].file_valid
= 1;
237 spec
[1].file_valid
= 0;
240 one
= spec
+ 1; two
= spec
;
242 one
= spec
; two
= one
+ 1;
245 run_external_diff(ce
->name
, one
, two
);
248 void show_differences(const struct cache_entry
*ce
, int reverse
)
250 struct diff_spec spec
[2], *one
, *two
;
252 memcpy(spec
[0].u
.sha1
, ce
->sha1
, 20);
253 spec
[0].mode
= ntohl(ce
->ce_mode
);
254 spec
[0].sha1_valid
= spec
[0].file_valid
= 1;
256 spec
[1].u
.name
= ce
->name
; /* the name we stated */
257 spec
[1].sha1_valid
= 0;
258 spec
[1].file_valid
= 1;
261 one
= spec
+ 1; two
= spec
;
263 one
= spec
; two
= one
+ 1;
266 run_external_diff(ce
->name
, one
, two
);