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;
198 prepare_temp_file(name
, &temp
[0], one
);
199 prepare_temp_file(name
, &temp
[1], two
);
200 if (! atexit_asked
&&
201 (temp
[0].name
== temp
[0].tmp_path
||
202 temp
[1].name
== temp
[1].tmp_path
)) {
204 atexit(remove_tempfile
);
211 die("unable to fork");
213 const char *pgm
= external_diff();
218 temp
[0].name
, temp
[0].hex
, temp
[0].mode
,
219 temp
[1].name
, temp
[1].hex
, temp
[1].mode
,
222 execlp(pgm
, pgm
, name
, NULL
);
225 * otherwise we use the built-in one.
228 builtin_diff(name
, temp
);
230 printf("* Unmerged path %s\n", name
);
233 if (waitpid(pid
, &status
, 0) < 0 || !WIFEXITED(status
))
234 die("diff program failed");
239 void diff_addremove(int addremove
, unsigned mode
,
240 const unsigned char *sha1
,
241 const char *base
, const char *path
)
243 char concatpath
[PATH_MAX
];
244 struct diff_spec spec
[2], *one
, *two
;
246 memcpy(spec
[0].u
.sha1
, sha1
, 20);
248 spec
[0].sha1_valid
= spec
[0].file_valid
= 1;
249 spec
[1].file_valid
= 0;
251 if (addremove
== '+') {
252 one
= spec
+ 1; two
= spec
;
254 one
= spec
; two
= one
+ 1;
258 strcpy(concatpath
, base
);
259 strcat(concatpath
, "/");
260 strcat(concatpath
, path
);
262 run_external_diff(path
? concatpath
: base
, one
, two
);
265 void diff_change(unsigned old_mode
, unsigned new_mode
,
266 const unsigned char *old_sha1
,
267 const unsigned char *new_sha1
,
268 const char *base
, const char *path
) {
269 char concatpath
[PATH_MAX
];
270 struct diff_spec spec
[2];
272 memcpy(spec
[0].u
.sha1
, old_sha1
, 20);
273 spec
[0].mode
= old_mode
;
274 memcpy(spec
[1].u
.sha1
, new_sha1
, 20);
275 spec
[1].mode
= new_mode
;
276 spec
[0].sha1_valid
= spec
[0].file_valid
= 1;
277 spec
[1].sha1_valid
= spec
[1].file_valid
= 1;
280 strcpy(concatpath
, base
);
281 strcat(concatpath
, "/");
282 strcat(concatpath
, path
);
284 run_external_diff(path
? concatpath
: base
, &spec
[0], &spec
[1]);
287 void diff_unmerge(const char *path
)
289 run_external_diff(path
, NULL
, NULL
);