[PATCH] Make diff-cache and friends output more cg-patch friendly.
[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_opts = "-pu";
10
11 static const char *external_diff(void)
12 {
13         static char *external_diff_cmd = NULL;
14         static int done_preparing = 0;
15
16         if (done_preparing)
17                 return external_diff_cmd;
18
19         /*
20          * Default values above are meant to match the
21          * Linux kernel development style.  Examples of
22          * alternative styles you can specify via environment
23          * variables are:
24          *
25          * GIT_DIFF_OPTS="-c";
26          */
27         if (getenv("GIT_EXTERNAL_DIFF"))
28                 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
29
30         /* In case external diff fails... */
31         diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
32
33         done_preparing = 1;
34         return external_diff_cmd;
35 }
36
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.
40  *
41  * E.g.
42  *  original     sq_expand     result
43  *  name     ==> name      ==> 'name'
44  *  a b      ==> a b       ==> 'a b'
45  *  a'b      ==> a'\''b    ==> 'a'\''b'
46  */
47 static char *sq_expand(const char *src)
48 {
49         static char *buf = NULL;
50         int cnt, c;
51         const char *cp;
52         char *bp;
53
54         /* count bytes needed to store the quoted string. */ 
55         for (cnt = 1, cp = src; *cp; cnt++, cp++)
56                 if (*cp == '\'')
57                         cnt += 3;
58
59         buf = xmalloc(cnt);
60         bp = buf;
61         while ((c = *src++)) {
62                 if (c != '\'')
63                         *bp++ = c;
64                 else {
65                         bp = strcpy(bp, "'\\''");
66                         bp += 4;
67                 }
68         }
69         *bp = 0;
70         return buf;
71 }
72
73 static struct diff_tempfile {
74         const char *name;
75         char hex[41];
76         char mode[10];
77         char tmp_path[50];
78 } diff_temp[2];
79
80 static void builtin_diff(const char *name,
81                          struct diff_tempfile *temp)
82 {
83         int i, next_at;
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];
87         const char *path0[2];
88         const char *path1[2];
89         char mode[2][20];
90         const char *name_sq = sq_expand(name);
91         char *cmd;
92         
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.
97          */
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";
104                         path1[i] = "";
105                         mode[i][0] = 0;
106                 } else {
107                         path0[i] = i ? "l/" : "k/";
108                         path1[i] = name_sq;
109                         sprintf(mode[i], "  (mode:%s)", temp[i].mode);
110                 }
111                 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
112                              strlen(mode[i]) + strlen(input_name_sq[i]));
113         }
114
115         cmd = xmalloc(cmd_size);
116
117         next_at = 0;
118         next_at += snprintf(cmd+next_at, cmd_size-next_at,
119                             diff_cmd,
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,
123                             " %s ", diff_opts);
124         next_at += snprintf(cmd+next_at, cmd_size-next_at,
125                             diff_arg, input_name_sq[0], input_name_sq[1]);
126
127         execlp("/bin/sh","sh", "-c", cmd, NULL);
128 }
129
130 static void prepare_temp_file(const char *name,
131                               struct diff_tempfile *temp,
132                               struct diff_spec *one)
133 {
134         static unsigned char null_sha1[20] = { 0, };
135
136         if (!one->file_valid) {
137         not_a_valid_file:
138                 temp->name = "/dev/null";
139                 strcpy(temp->hex, ".");
140                 strcpy(temp->mode, ".");
141                 return;
142         }
143
144         if (one->sha1_valid &&
145             !memcmp(one->u.sha1, null_sha1, sizeof(null_sha1))) {
146                 one->sha1_valid = 0;
147                 one->u.name = name;
148         }
149
150         if (!one->sha1_valid) {
151                 struct stat st;
152                 temp->name = one->u.name;
153                 if (stat(temp->name, &st) < 0) {
154                         if (errno == ENOENT)
155                                 goto not_a_valid_file;
156                         die("stat(%s): %s", temp->name, strerror(errno));
157                 }
158                 strcpy(temp->hex, ".");
159                 sprintf(temp->mode, "%06o",
160                         S_IFREG |ce_permissions(st.st_mode));
161         }
162         else {
163                 int fd;
164                 void *blob;
165                 char type[20];
166                 unsigned long size;
167
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));
172
173                 strcpy(temp->tmp_path, ".diff_XXXXXX");
174                 fd = mkstemp(temp->tmp_path);
175                 if (fd < 0)
176                         die("unable to create temp-file");
177                 if (write(fd, blob, size) != size)
178                         die("unable to write temp-file");
179                 close(fd);
180                 free(blob);
181                 temp->name = temp->tmp_path;
182                 strcpy(temp->hex, sha1_to_hex(one->u.sha1));
183                 temp->hex[40] = 0;
184                 sprintf(temp->mode, "%06o", one->mode);
185         }
186 }
187
188 static void remove_tempfile(void)
189 {
190         int i;
191
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;
196                 }
197 }
198
199 /* An external diff command takes:
200  *
201  * diff-cmd name infile1 infile1-sha1 infile1-mode \
202  *               infile2 infile2-sha1 infile2-mode.
203  *
204  */
205 void run_external_diff(const char *name,
206                        struct diff_spec *one,
207                        struct diff_spec *two)
208 {
209         struct diff_tempfile *temp = diff_temp;
210         int pid, status;
211         static int atexit_asked = 0;
212
213         if (one && two) {
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)) {
219                         atexit_asked = 1;
220                         atexit(remove_tempfile);
221                 }
222         }
223
224         fflush(NULL);
225         pid = fork();
226         if (pid < 0)
227                 die("unable to fork");
228         if (!pid) {
229                 const char *pgm = external_diff();
230                 if (pgm) {
231                         if (one && two)
232                                 execlp(pgm, pgm,
233                                        name,
234                                        temp[0].name, temp[0].hex, temp[0].mode,
235                                        temp[1].name, temp[1].hex, temp[1].mode,
236                                        NULL);
237                         else
238                                 execlp(pgm, pgm, name, NULL);
239                 }
240                 /*
241                  * otherwise we use the built-in one.
242                  */
243                 if (one && two)
244                         builtin_diff(name, temp);
245                 else
246                         printf("* Unmerged path %s\n", name);
247                 exit(0);
248         }
249         if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
250                 die("diff program failed");
251
252         remove_tempfile();
253 }
254
255 void diff_addremove(int addremove, unsigned mode,
256                     const unsigned char *sha1,
257                     const char *base, const char *path)
258 {
259         char concatpath[PATH_MAX];
260         struct diff_spec spec[2], *one, *two;
261
262         memcpy(spec[0].u.sha1, sha1, 20);
263         spec[0].mode = mode;
264         spec[0].sha1_valid = spec[0].file_valid = 1;
265         spec[1].file_valid = 0;
266
267         if (addremove == '+') {
268                 one = spec + 1; two = spec;
269         } else {
270                 one = spec; two = one + 1;
271         }
272         
273         if (path) {
274                 strcpy(concatpath, base);
275                 strcat(concatpath, path);
276         }
277         run_external_diff(path ? concatpath : base, one, two);
278 }
279
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];
286
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;
293
294         if (path) {
295                 strcpy(concatpath, base);
296                 strcat(concatpath, path);
297         }
298         run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
299 }
300
301 void diff_unmerge(const char *path)
302 {
303         run_external_diff(path, NULL, NULL);
304 }