[PATCH] Reworked external diff interface.
[git.git] / diff.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include "cache.h"
7 #include "diff.h"
8
9 static char *diff_cmd = "diff -L'k/%s' -L'l/%s'";
10 static char *diff_opts = "-pu";
11
12 static const char *external_diff(void)
13 {
14         static char *external_diff_cmd = NULL;
15         static int done_preparing = 0;
16
17         if (done_preparing)
18                 return external_diff_cmd;
19
20         /*
21          * Default values above are meant to match the
22          * Linux kernel development style.  Examples of
23          * alternative styles you can specify via environment
24          * variables are:
25          *
26          * GIT_DIFF_CMD="diff -L '%s' -L '%s'"
27          * GIT_DIFF_OPTS="-c";
28          */
29         if (getenv("GIT_EXTERNAL_DIFF"))
30                 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
31
32         /* In case external diff fails... */
33         diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
34         diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
35
36         done_preparing = 1;
37         return external_diff_cmd;
38 }
39
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.
43  *
44  * E.g.
45  *  original     sq_expand     result
46  *  name     ==> name      ==> 'name'
47  *  a b      ==> a b       ==> 'a b'
48  *  a'b      ==> a'\''b    ==> 'a'\''b'
49  */
50 static char *sq_expand(const char *src)
51 {
52         static char *buf = NULL;
53         int cnt, c;
54         const char *cp;
55         char *bp;
56
57         /* count bytes needed to store the quoted string. */ 
58         for (cnt = 1, cp = src; *cp; cnt++, cp++)
59                 if (*cp == '\'')
60                         cnt += 3;
61
62         buf = xmalloc(cnt);
63         bp = buf;
64         while ((c = *src++)) {
65                 if (c != '\'')
66                         *bp++ = c;
67                 else {
68                         bp = strcpy(bp, "'\\''");
69                         bp += 4;
70                 }
71         }
72         *bp = 0;
73         return buf;
74 }
75
76 static struct diff_tempfile {
77         const char *name;
78         char hex[41];
79         char mode[10];
80         char tmp_path[50];
81 } diff_temp[2];
82
83 static void builtin_diff(const char *name,
84                          struct diff_tempfile *temp)
85 {
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);
90
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.
95          */
96         int cmd_size = (strlen(diff_cmd) + 
97                         strlen(name_sq) * 2 +
98                         strlen(diff_opts) +
99                         strlen(diff_arg) +
100                         strlen(name_1_sq) + strlen(name_2_sq)
101                         - 5);
102         char *cmd = xmalloc(cmd_size);
103         int next_at = 0;
104
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,
108                             " %s ", diff_opts);
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);
112 }
113
114 static void prepare_temp_file(const char *name,
115                               struct diff_tempfile *temp,
116                               struct diff_spec *one)
117 {
118         static unsigned char null_sha1[20] = { 0, };
119
120         if (!one->file_valid) {
121         not_a_valid_file:
122                 temp->name = "/dev/null";
123                 strcpy(temp->hex, ".");
124                 strcpy(temp->mode, ".");
125                 return;
126         }
127
128         if (one->sha1_valid &&
129             !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
130                 one->sha1_valid = 0;
131                 one->u.name = name;
132         }
133
134         if (!one->sha1_valid) {
135                 struct stat st;
136                 temp->name = one->u.name;
137                 if (stat(temp->name, &st) < 0) {
138                         if (errno == ENOENT)
139                                 goto not_a_valid_file;
140                         die("stat(%s): %s", temp->name, strerror(errno));
141                 }
142                 strcpy(temp->hex, ".");
143                 sprintf(temp->mode, "%06o",
144                         S_IFREG |ce_permissions(st.st_mode));
145         }
146         else {
147                 int fd;
148                 void *blob;
149                 char type[20];
150                 unsigned long size;
151
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));
156
157                 strcpy(temp->tmp_path, ".diff_XXXXXX");
158                 fd = mkstemp(temp->tmp_path);
159                 if (fd < 0)
160                         die("unable to create temp-file");
161                 if (write(fd, blob, size) != size)
162                         die("unable to write temp-file");
163                 close(fd);
164                 free(blob);
165                 temp->name = temp->tmp_path;
166                 strcpy(temp->hex, sha1_to_hex(one->u.sha1));
167                 temp->hex[40] = 0;
168                 sprintf(temp->mode, "%06o", one->mode);
169         }
170 }
171
172 static void remove_tempfile(void)
173 {
174         int i;
175
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;
180                 }
181 }
182
183 /* An external diff command takes:
184  *
185  * diff-cmd name infile1 infile1-sha1 infile1-mode \
186  *               infile2 infile2-sha1 infile2-mode.
187  *
188  */
189 void run_external_diff(const char *name,
190                        struct diff_spec *one,
191                        struct diff_spec *two)
192 {
193         struct diff_tempfile *temp = diff_temp;
194         int pid, status;
195         static int atexit_asked = 0;
196
197         if (one && two) {
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)) {
203                         atexit_asked = 1;
204                         atexit(remove_tempfile);
205                 }
206         }
207
208         fflush(NULL);
209         pid = fork();
210         if (pid < 0)
211                 die("unable to fork");
212         if (!pid) {
213                 const char *pgm = external_diff();
214                 if (pgm) {
215                         if (one && two)
216                                 execlp(pgm, pgm,
217                                        name,
218                                        temp[0].name, temp[0].hex, temp[0].mode,
219                                        temp[1].name, temp[1].hex, temp[1].mode,
220                                        NULL);
221                         else
222                                 execlp(pgm, pgm, name, NULL);
223                 }
224                 /*
225                  * otherwise we use the built-in one.
226                  */
227                 if (one && two)
228                         builtin_diff(name, temp);
229                 else
230                         printf("* Unmerged path %s\n", name);
231                 exit(0);
232         }
233         if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
234                 die("diff program failed");
235
236         remove_tempfile();
237 }
238
239 void diff_addremove(int addremove, unsigned mode,
240                     const unsigned char *sha1,
241                     const char *base, const char *path)
242 {
243         char concatpath[PATH_MAX];
244         struct diff_spec spec[2], *one, *two;
245
246         memcpy(spec[0].u.sha1, sha1, 20);
247         spec[0].mode = mode;
248         spec[0].sha1_valid = spec[0].file_valid = 1;
249         spec[1].file_valid = 0;
250
251         if (addremove == '+') {
252                 one = spec + 1; two = spec;
253         } else {
254                 one = spec; two = one + 1;
255         }
256         
257         if (path) {
258                 strcpy(concatpath, base);
259                 strcat(concatpath, "/");
260                 strcat(concatpath, path);
261         }
262         run_external_diff(path ? concatpath : base, one, two);
263 }
264
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];
271
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;
278
279         if (path) {
280                 strcpy(concatpath, base);
281                 strcat(concatpath, "/");
282                 strcat(concatpath, path);
283         }
284         run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
285 }
286
287 void diff_unmerge(const char *path)
288 {
289         run_external_diff(path, NULL, NULL);
290 }