2 * Copyright (C) 2005 Junio C Hamano
9 static char *diff_opts = "-pu";
11 static const char *external_diff(void)
13 static char *external_diff_cmd = NULL;
14 static int done_preparing = 0;
17 return external_diff_cmd;
20 * Default values above are meant to match the
21 * Linux kernel development style. Examples of
22 * alternative styles you can specify via environment
27 if (getenv("GIT_EXTERNAL_DIFF"))
28 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
30 /* In case external diff fails... */
31 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
34 return external_diff_cmd;
37 /* Help to copy the thing properly quoted for the shell safety.
38 * any single quote is replaced with '\'', and the caller is
39 * expected to enclose the result within a single quote pair.
42 * original sq_expand result
43 * name ==> name ==> 'name'
44 * a b ==> a b ==> 'a b'
45 * a'b ==> a'\''b ==> 'a'\''b'
47 static char *sq_expand(const char *src)
49 static char *buf = NULL;
54 /* count bytes needed to store the quoted string. */
55 for (cnt = 1, cp = src; *cp; cnt++, cp++)
61 while ((c = *src++)) {
65 bp = strcpy(bp, "'\\''");
73 static struct diff_tempfile {
80 static void builtin_diff(const char *name,
81 struct diff_tempfile *temp)
84 const char *diff_cmd = "diff -L'%s%s%s' -L'%s%s%s'";
85 const char *diff_arg = "'%s' '%s'";
86 const char *input_name_sq[2];
90 const char *name_sq = sq_expand(name);
93 /* diff_cmd and diff_arg have 8 %s in total which makes
94 * the sum of these strings 16 bytes larger than required.
95 * we use 2 spaces around diff-opts, and we need to count
96 * terminating NUL, so we subtract 13 here.
98 int cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
99 strlen(diff_arg) - 13);
100 for (i = 0; i < 2; i++) {
101 input_name_sq[i] = sq_expand(temp[i].name);
102 if (!strcmp(temp[i].name, "/dev/null")) {
103 path0[i] = "/dev/null";
107 path0[i] = i ? "l/" : "k/";
109 sprintf(mode[i], " (mode:%s)", temp[i].mode);
111 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
112 strlen(mode[i]) + strlen(input_name_sq[i]));
115 cmd = xmalloc(cmd_size);
118 next_at += snprintf(cmd+next_at, cmd_size-next_at,
120 path0[0], path1[0], mode[0],
121 path0[1], path1[1], mode[1]);
122 next_at += snprintf(cmd+next_at, cmd_size-next_at,
124 next_at += snprintf(cmd+next_at, cmd_size-next_at,
125 diff_arg, input_name_sq[0], input_name_sq[1]);
127 execlp("/bin/sh","sh", "-c", cmd, NULL);
130 static void prepare_temp_file(const char *name,
131 struct diff_tempfile *temp,
132 struct diff_spec *one)
134 static unsigned char null_sha1[20] = { 0, };
136 if (!one->file_valid) {
138 temp->name = "/dev/null";
139 strcpy(temp->hex, ".");
140 strcpy(temp->mode, ".");
144 if (one->sha1_valid &&
145 !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
150 if (!one->sha1_valid) {
152 temp->name = one->u.name;
153 if (stat(temp->name, &st) < 0) {
155 goto not_a_valid_file;
156 die("stat(%s): %s", temp->name, strerror(errno));
158 strcpy(temp->hex, ".");
159 sprintf(temp->mode, "%06o",
160 S_IFREG |ce_permissions(st.st_mode));
168 blob = read_sha1_file(one->u.sha1, type, &size);
169 if (!blob || strcmp(type, "blob"))
170 die("unable to read blob object for %s (%s)",
171 name, sha1_to_hex(one->u.sha1));
173 strcpy(temp->tmp_path, ".diff_XXXXXX");
174 fd = mkstemp(temp->tmp_path);
176 die("unable to create temp-file");
177 if (write(fd, blob, size) != size)
178 die("unable to write temp-file");
181 temp->name = temp->tmp_path;
182 strcpy(temp->hex, sha1_to_hex(one->u.sha1));
184 sprintf(temp->mode, "%06o", one->mode);
188 static void remove_tempfile(void)
192 for (i = 0; i < 2; i++)
193 if (diff_temp[i].name == diff_temp[i].tmp_path) {
194 unlink(diff_temp[i].name);
195 diff_temp[i].name = NULL;
199 /* An external diff command takes:
201 * diff-cmd name infile1 infile1-sha1 infile1-mode \
202 * infile2 infile2-sha1 infile2-mode.
205 void run_external_diff(const char *name,
206 struct diff_spec *one,
207 struct diff_spec *two)
209 struct diff_tempfile *temp = diff_temp;
211 static int atexit_asked = 0;
214 prepare_temp_file(name, &temp[0], one);
215 prepare_temp_file(name, &temp[1], two);
216 if (! atexit_asked &&
217 (temp[0].name == temp[0].tmp_path ||
218 temp[1].name == temp[1].tmp_path)) {
220 atexit(remove_tempfile);
227 die("unable to fork");
229 const char *pgm = external_diff();
234 temp[0].name, temp[0].hex, temp[0].mode,
235 temp[1].name, temp[1].hex, temp[1].mode,
238 execlp(pgm, pgm, name, NULL);
241 * otherwise we use the built-in one.
244 builtin_diff(name, temp);
246 printf("* Unmerged path %s\n", name);
249 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
250 die("diff program failed");
255 void diff_addremove(int addremove, unsigned mode,
256 const unsigned char *sha1,
257 const char *base, const char *path)
259 char concatpath[PATH_MAX];
260 struct diff_spec spec[2], *one, *two;
262 memcpy(spec[0].u.sha1, sha1, 20);
264 spec[0].sha1_valid = spec[0].file_valid = 1;
265 spec[1].file_valid = 0;
267 if (addremove == '+') {
268 one = spec + 1; two = spec;
270 one = spec; two = one + 1;
274 strcpy(concatpath, base);
275 strcat(concatpath, path);
277 run_external_diff(path ? concatpath : base, one, two);
280 void diff_change(unsigned old_mode, unsigned new_mode,
281 const unsigned char *old_sha1,
282 const unsigned char *new_sha1,
283 const char *base, const char *path) {
284 char concatpath[PATH_MAX];
285 struct diff_spec spec[2];
287 memcpy(spec[0].u.sha1, old_sha1, 20);
288 spec[0].mode = old_mode;
289 memcpy(spec[1].u.sha1, new_sha1, 20);
290 spec[1].mode = new_mode;
291 spec[0].sha1_valid = spec[0].file_valid = 1;
292 spec[1].sha1_valid = spec[1].file_valid = 1;
295 strcpy(concatpath, base);
296 strcat(concatpath, path);
298 run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
301 void diff_unmerge(const char *path)
303 run_external_diff(path, NULL, NULL);