Fix up recent object model cleanups
[git/git.git] / diff.c
CommitLineData
be3cfa85
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include <sys/types.h>
5#include <sys/wait.h>
86436c28
JH
6#include "cache.h"
7#include "diff.h"
8
be3cfa85
JH
9static char *diff_cmd = "diff -L'k/%s' -L'l/%s'";
10static char *diff_opts = "-pu";
86436c28 11
be3cfa85 12static const char *external_diff(void)
86436c28 13{
be3cfa85
JH
14 static char *external_diff_cmd = NULL;
15 static int done_preparing = 0;
16
17 if (done_preparing)
18 return external_diff_cmd;
19
86436c28
JH
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 */
be3cfa85
JH
29 if (getenv("GIT_EXTERNAL_DIFF"))
30 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
31
32 /* In case external diff fails... */
86436c28
JH
33 diff_cmd = getenv("GIT_DIFF_CMD") ? : diff_cmd;
34 diff_opts = getenv("GIT_DIFF_OPTS") ? : diff_opts;
be3cfa85
JH
35
36 done_preparing = 1;
37 return external_diff_cmd;
86436c28
JH
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 */
50static 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
812666c8 62 buf = xmalloc(cnt);
86436c28
JH
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
be3cfa85
JH
76static 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
83static void builtin_diff(const char *name,
84 struct diff_tempfile *temp)
86436c28 85{
be3cfa85
JH
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);
812666c8 102 char *cmd = xmalloc(cmd_size);
be3cfa85
JH
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);
86436c28
JH
112}
113
be3cfa85
JH
114static void prepare_temp_file(const char *name,
115 struct diff_tempfile *temp,
116 struct diff_spec *one)
86436c28 117{
be3cfa85
JH
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, ".");
86436c28
JH
125 return;
126 }
be3cfa85
JH
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
172static 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 */
189void 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
77eb2720
JH
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 }
be3cfa85
JH
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();
77eb2720
JH
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 }
be3cfa85
JH
224 /*
225 * otherwise we use the built-in one.
226 */
77eb2720
JH
227 if (one && two)
228 builtin_diff(name, temp);
229 else
230 printf("* Unmerged path %s\n", name);
be3cfa85
JH
231 exit(0);
232 }
233 if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
234 die("diff program failed");
235
236 remove_tempfile();
237}
238
77eb2720
JH
239void diff_addremove(int addremove, unsigned mode,
240 const unsigned char *sha1,
241 const char *base, const char *path)
be3cfa85 242{
77eb2720 243 char concatpath[PATH_MAX];
be3cfa85
JH
244 struct diff_spec spec[2], *one, *two;
245
77eb2720
JH
246 memcpy(spec[0].u.sha1, sha1, 20);
247 spec[0].mode = mode;
be3cfa85
JH
248 spec[0].sha1_valid = spec[0].file_valid = 1;
249 spec[1].file_valid = 0;
250
77eb2720 251 if (addremove == '+') {
be3cfa85
JH
252 one = spec + 1; two = spec;
253 } else {
254 one = spec; two = one + 1;
255 }
77eb2720
JH
256
257 if (path) {
258 strcpy(concatpath, base);
77eb2720
JH
259 strcat(concatpath, path);
260 }
261 run_external_diff(path ? concatpath : base, one, two);
be3cfa85
JH
262}
263
77eb2720
JH
264void diff_change(unsigned old_mode, unsigned new_mode,
265 const unsigned char *old_sha1,
266 const unsigned char *new_sha1,
267 const char *base, const char *path) {
268 char concatpath[PATH_MAX];
269 struct diff_spec spec[2];
270
271 memcpy(spec[0].u.sha1, old_sha1, 20);
272 spec[0].mode = old_mode;
273 memcpy(spec[1].u.sha1, new_sha1, 20);
274 spec[1].mode = new_mode;
be3cfa85 275 spec[0].sha1_valid = spec[0].file_valid = 1;
77eb2720 276 spec[1].sha1_valid = spec[1].file_valid = 1;
be3cfa85 277
77eb2720
JH
278 if (path) {
279 strcpy(concatpath, base);
77eb2720 280 strcat(concatpath, path);
be3cfa85 281 }
77eb2720
JH
282 run_external_diff(path ? concatpath : base, &spec[0], &spec[1]);
283}
be3cfa85 284
77eb2720
JH
285void diff_unmerge(const char *path)
286{
287 run_external_diff(path, NULL, NULL);
86436c28 288}