[PATCH] introduce xmalloc and xrealloc
[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         prepare_temp_file(name, &temp[0], one);
198         prepare_temp_file(name, &temp[1], two);
199         if (! atexit_asked &&
200             (temp[0].name == temp[0].tmp_path ||
201              temp[1].name == temp[1].tmp_path)) {
202                 atexit_asked = 1;
203                 atexit(remove_tempfile);
204         }
205
206         fflush(NULL);
207         pid = fork();
208         if (pid < 0)
209                 die("unable to fork");
210         if (!pid) {
211                 const char *pgm = external_diff();
212                 if (pgm)
213                         execlp(pgm, pgm,
214                                name,
215                                temp[0].name, temp[0].hex, temp[0].mode,
216                                temp[1].name, temp[1].hex, temp[1].mode,
217                                NULL);
218                 /*
219                  * otherwise we use the built-in one.
220                  */
221                 builtin_diff(name, temp);
222                 exit(0);
223         }
224         if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
225                 die("diff program failed");
226
227         remove_tempfile();
228 }
229
230 void show_diff_empty(const struct cache_entry *ce, int reverse)
231 {
232         struct diff_spec spec[2], *one, *two;
233
234         memcpy(spec[0].u.sha1, ce->sha1, 20);
235         spec[0].mode = ntohl(ce->ce_mode);
236         spec[0].sha1_valid = spec[0].file_valid = 1;
237         spec[1].file_valid = 0;
238
239         if (reverse) {
240                 one = spec + 1; two = spec;
241         } else {
242                 one = spec; two = one + 1;
243         }
244
245         run_external_diff(ce->name, one, two);
246 }
247
248 void show_differences(const struct cache_entry *ce, int reverse) 
249 {
250         struct diff_spec spec[2], *one, *two;
251
252         memcpy(spec[0].u.sha1, ce->sha1, 20);
253         spec[0].mode = ntohl(ce->ce_mode);
254         spec[0].sha1_valid = spec[0].file_valid = 1;
255
256         spec[1].u.name = ce->name; /* the name we stated */
257         spec[1].sha1_valid = 0;
258         spec[1].file_valid = 1;
259
260         if (reverse) {
261                 one = spec + 1; two = spec;
262         } else {
263                 one = spec; two = one + 1;
264         }
265
266         run_external_diff(ce->name, one, two);
267 }