Steal -t option to git-ls-files from Cogito fork.
[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 <signal.h>
7 #include "cache.h"
8 #include "diff.h"
9
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_OPTS="-c";
27          */
28         if (getenv("GIT_EXTERNAL_DIFF"))
29                 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
30
31         /* In case external diff fails... */
32         diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
33
34         done_preparing = 1;
35         return external_diff_cmd;
36 }
37
38 /* Help to copy the thing properly quoted for the shell safety.
39  * any single quote is replaced with '\'', and the caller is
40  * expected to enclose the result within a single quote pair.
41  *
42  * E.g.
43  *  original     sq_expand     result
44  *  name     ==> name      ==> 'name'
45  *  a b      ==> a b       ==> 'a b'
46  *  a'b      ==> a'\''b    ==> 'a'\''b'
47  */
48 static char *sq_expand(const char *src)
49 {
50         static char *buf = NULL;
51         int cnt, c;
52         const char *cp;
53         char *bp;
54
55         /* count bytes needed to store the quoted string. */ 
56         for (cnt = 1, cp = src; *cp; cnt++, cp++)
57                 if (*cp == '\'')
58                         cnt += 3;
59
60         buf = xmalloc(cnt);
61         bp = buf;
62         while ((c = *src++)) {
63                 if (c != '\'')
64                         *bp++ = c;
65                 else {
66                         bp = strcpy(bp, "'\\''");
67                         bp += 4;
68                 }
69         }
70         *bp = 0;
71         return buf;
72 }
73
74 static struct diff_tempfile {
75         const char *name;
76         char hex[41];
77         char mode[10];
78         char tmp_path[50];
79 } diff_temp[2];
80
81 static void builtin_diff(const char *name,
82                          struct diff_tempfile *temp)
83 {
84         int i, next_at;
85         const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
86         const char *diff_arg  = "'%s' '%s'||:"; /* "||:" is to return 0 */
87         const char *input_name_sq[2];
88         const char *path0[2];
89         const char *path1[2];
90         const char *name_sq = sq_expand(name);
91         char *cmd;
92         
93         /* diff_cmd and diff_arg have 6 %s in total which makes
94          * the sum of these strings 12 bytes larger than required.
95          * we use 2 spaces around diff-opts, and we need to count
96          * terminating NUL, so we subtract 9 here.
97          */
98         int cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
99                         strlen(diff_arg) - 9);
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";
104                         path1[i] = "";
105                 } else {
106                         path0[i] = i ? "b/" : "a/";
107                         path1[i] = name_sq;
108                 }
109                 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
110                              strlen(input_name_sq[i]));
111         }
112
113         cmd = xmalloc(cmd_size);
114
115         next_at = 0;
116         next_at += snprintf(cmd+next_at, cmd_size-next_at,
117                             diff_cmd,
118                             path0[0], path1[0], path0[1], path1[1]);
119         next_at += snprintf(cmd+next_at, cmd_size-next_at,
120                             " %s ", diff_opts);
121         next_at += snprintf(cmd+next_at, cmd_size-next_at,
122                             diff_arg, input_name_sq[0], input_name_sq[1]);
123
124         if (!path1[0][0])
125                 printf("Created: %s (mode:%s)\n", name, temp[1].mode);
126         else if (!path1[1][0])
127                 printf("Deleted: %s\n", name);
128         else if (strcmp(temp[0].mode, temp[1].mode)) {
129                 printf("Mode changed: %s (%s->%s)\n", name,
130                        temp[0].mode, temp[1].mode);
131                 /* Be careful.  We do not want to diff between
132                  * symlink and a file.
133                  */
134                 if (strncmp(temp[0].mode, "120", 3) !=
135                     strncmp(temp[1].mode, "120", 3))
136                         exit(0);
137         }
138         fflush(NULL);
139         execlp("/bin/sh","sh", "-c", cmd, NULL);
140 }
141
142 /*
143  * Given a name and sha1 pair, if the dircache tells us the file in
144  * the work tree has that object contents, return true, so that
145  * prepare_temp_file() does not have to inflate and extract.
146  */
147 static int work_tree_matches(const char *name, const unsigned char *sha1)
148 {
149         struct cache_entry *ce;
150         struct stat st;
151         int pos, len;
152         
153         /* We do not read the cache ourselves here, because the
154          * benchmark with my previous version that always reads cache
155          * shows that it makes things worse for diff-tree comparing
156          * two linux-2.6 kernel trees in an already checked out work
157          * tree.  This is because most diff-tree comparison deals with
158          * only a small number of files, while reading the cache is
159          * expensive for a large project, and its cost outweighs the
160          * savings we get by not inflating the object to a temporary
161          * file.  Practically, this code only helps when we are used
162          * by diff-cache --cached, which does read the cache before
163          * calling us.
164          */ 
165         if (!active_cache)
166                 return 0;
167
168         len = strlen(name);
169         pos = cache_name_pos(name, len);
170         if (pos < 0)
171                 return 0;
172         ce = active_cache[pos];
173         if ((lstat(name, &st) < 0) ||
174             !S_ISREG(st.st_mode) ||
175             cache_match_stat(ce, &st) ||
176             memcmp(sha1, ce->sha1, 20))
177                 return 0;
178         return 1;
179 }
180
181 static void prep_temp_blob(struct diff_tempfile *temp,
182                            void *blob,
183                            unsigned long size,
184                            unsigned char *sha1,
185                            int mode)
186 {
187         int fd;
188
189         strcpy(temp->tmp_path, ".diff_XXXXXX");
190         fd = mkstemp(temp->tmp_path);
191         if (fd < 0)
192                 die("unable to create temp-file");
193         if (write(fd, blob, size) != size)
194                 die("unable to write temp-file");
195         close(fd);
196         temp->name = temp->tmp_path;
197         strcpy(temp->hex, sha1_to_hex(sha1));
198         temp->hex[40] = 0;
199         sprintf(temp->mode, "%06o", mode);
200 }
201
202 static void prepare_temp_file(const char *name,
203                               struct diff_tempfile *temp,
204                               struct diff_spec *one)
205 {
206         static unsigned char null_sha1[20] = { 0, };
207         int use_work_tree = 0;
208
209         if (!one->file_valid) {
210         not_a_valid_file:
211                 /* A '-' entry produces this for file-2, and
212                  * a '+' entry produces this for file-1.
213                  */
214                 temp->name = "/dev/null";
215                 strcpy(temp->hex, ".");
216                 strcpy(temp->mode, ".");
217                 return;
218         }
219
220         if (one->sha1_valid &&
221             (!memcmp(one->blob_sha1, null_sha1, sizeof(null_sha1)) ||
222              work_tree_matches(name, one->blob_sha1)))
223                 use_work_tree = 1;
224
225         if (!one->sha1_valid || use_work_tree) {
226                 struct stat st;
227                 temp->name = name;
228                 if (lstat(temp->name, &st) < 0) {
229                         if (errno == ENOENT)
230                                 goto not_a_valid_file;
231                         die("stat(%s): %s", temp->name, strerror(errno));
232                 }
233                 if (S_ISLNK(st.st_mode)) {
234                         int ret;
235                         char *buf, buf_[1024];
236                         buf = ((sizeof(buf_) < st.st_size) ?
237                                xmalloc(st.st_size) : buf_);
238                         ret = readlink(name, buf, st.st_size);
239                         if (ret < 0)
240                                 die("readlink(%s)", name);
241                         prep_temp_blob(temp, buf, st.st_size,
242                                        (one->sha1_valid ?
243                                         one->blob_sha1 : null_sha1),
244                                        (one->sha1_valid ?
245                                         one->mode : S_IFLNK));
246                 }
247                 else {
248                         if (!one->sha1_valid)
249                                 strcpy(temp->hex, sha1_to_hex(null_sha1));
250                         else
251                                 strcpy(temp->hex, sha1_to_hex(one->blob_sha1));
252                         sprintf(temp->mode, "%06o",
253                                 S_IFREG |ce_permissions(st.st_mode));
254                 }
255                 return;
256         }
257         else {
258                 void *blob;
259                 char type[20];
260                 unsigned long size;
261
262                 blob = read_sha1_file(one->blob_sha1, type, &size);
263                 if (!blob || strcmp(type, "blob"))
264                         die("unable to read blob object for %s (%s)",
265                             name, sha1_to_hex(one->blob_sha1));
266                 prep_temp_blob(temp, blob, size, one->blob_sha1, one->mode);
267                 free(blob);
268         }
269 }
270
271 static void remove_tempfile(void)
272 {
273         int i;
274
275         for (i = 0; i < 2; i++)
276                 if (diff_temp[i].name == diff_temp[i].tmp_path) {
277                         unlink(diff_temp[i].name);
278                         diff_temp[i].name = NULL;
279                 }
280 }
281
282 static void remove_tempfile_on_signal(int signo)
283 {
284         remove_tempfile();
285 }
286
287 /* An external diff command takes:
288  *
289  * diff-cmd name infile1 infile1-sha1 infile1-mode \
290  *               infile2 infile2-sha1 infile2-mode.
291  *
292  */
293 void run_external_diff(const char *name,
294                        struct diff_spec *one,
295                        struct diff_spec *two)
296 {
297         struct diff_tempfile *temp = diff_temp;
298         pid_t pid;
299         int status;
300         static int atexit_asked = 0;
301
302         if (one && two) {
303                 prepare_temp_file(name, &temp[0], one);
304                 prepare_temp_file(name, &temp[1], two);
305                 if (! atexit_asked &&
306                     (temp[0].name == temp[0].tmp_path ||
307                      temp[1].name == temp[1].tmp_path)) {
308                         atexit_asked = 1;
309                         atexit(remove_tempfile);
310                 }
311                 signal(SIGINT, remove_tempfile_on_signal);
312         }
313
314         fflush(NULL);
315         pid = fork();
316         if (pid < 0)
317                 die("unable to fork");
318         if (!pid) {
319                 const char *pgm = external_diff();
320                 if (pgm) {
321                         if (one && two)
322                                 execlp(pgm, pgm,
323                                        name,
324                                        temp[0].name, temp[0].hex, temp[0].mode,
325                                        temp[1].name, temp[1].hex, temp[1].mode,
326                                        NULL);
327                         else
328                                 execlp(pgm, pgm, name, NULL);
329                 }
330                 /*
331                  * otherwise we use the built-in one.
332                  */
333                 if (one && two)
334                         builtin_diff(name, temp);
335                 else
336                         printf("* Unmerged path %s\n", name);
337                 exit(0);
338         }
339         if (waitpid(pid, &status, 0) < 0 ||
340             !WIFEXITED(status) || WEXITSTATUS(status)) {
341                 /* Earlier we did not check the exit status because
342                  * diff exits non-zero if files are different, and
343                  * we are not interested in knowing that.  It was a
344                  * mistake which made it harder to quit a diff-*
345                  * session that uses the git-apply-patch-script as
346                  * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
347                  * should also exit non-zero only when it wants to
348                  * abort the entire diff-* session.
349                  */
350                 remove_tempfile();
351                 fprintf(stderr, "external diff died, stopping at %s.\n", name);
352                 exit(1);
353         }
354         remove_tempfile();
355 }
356
357 void diff_addremove(int addremove, unsigned mode,
358                     const unsigned char *sha1,
359                     const char *base, const char *path)
360 {
361         char concatpath[PATH_MAX];
362         struct diff_spec spec[2], *one, *two;
363
364         memcpy(spec[0].blob_sha1, sha1, 20);
365         spec[0].mode = mode;
366         spec[0].sha1_valid = spec[0].file_valid = 1;
367         spec[1].file_valid = 0;
368
369         if (addremove == '+') {
370                 one = spec + 1; two = spec;
371         } else {
372                 one = spec; two = one + 1;
373         }
374         
375         if (path) {
376                 strcpy(concatpath, base);
377                 strcat(concatpath, path);
378         }
379         run_external_diff(path ? concatpath : base, one, two);
380 }
381
382 void diff_change(unsigned old_mode, unsigned new_mode,
383                  const unsigned char *old_sha1,
384                  const unsigned char *new_sha1,
385                  const char *base, const char *path) {
386         char concatpath[PATH_MAX];
387         struct diff_spec spec[2];
388
389         memcpy(spec[0].blob_sha1, old_sha1, 20);
390         spec[0].mode = old_mode;
391         memcpy(spec[1].blob_sha1, new_sha1, 20);
392         spec[1].mode = new_mode;
393         spec[0].sha1_valid = spec[0].file_valid = 1;
394         spec[1].sha1_valid = spec[1].file_valid = 1;
395
396         if (path) {
397                 strcpy(concatpath, base);
398                 strcat(concatpath, path);
399         }
400         run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
401 }
402
403 void diff_unmerge(const char *path)
404 {
405         run_external_diff(path, NULL, NULL);
406 }