🌐 AI搜索 & 代理 主页
blob: c8159183dac830bf7cba20edf480712f039d7135 [file] [log] [blame]
Junio C Hamanobe3cfa82005-04-26 16:25:051/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include <sys/types.h>
5#include <sys/wait.h>
Junio C Hamano532149d2005-04-28 17:13:016#include <signal.h>
Junio C Hamano86436c22005-04-26 01:22:477#include "cache.h"
Junio C Hamano6fb737b2005-07-08 06:58:328#include "quote.h"
Junio C Hamano86436c22005-04-26 01:22:479#include "diff.h"
Junio C Hamano427dcb42005-05-21 09:39:0910#include "diffcore.h"
Junio C Hamano86436c22005-04-26 01:22:4711
Junio C Hamanod19938a2005-05-10 00:57:5612static const char *diff_opts = "-pu";
Junio C Hamano427dcb42005-05-21 09:39:0913
Junio C Hamanof0c6b2a2005-05-27 22:56:3814static int use_size_cache;
Junio C Hamano86436c22005-04-26 01:22:4715
Junio C Hamano3299c6f2005-11-15 20:48:0816int diff_rename_limit_default = -1;
17
Junio C Hamano9ce392f2005-11-22 06:52:3718int git_diff_config(const char *var, const char *value)
19{
20 if (!strcmp(var, "diff.renamelimit")) {
21 diff_rename_limit_default = git_config_int(var, value);
22 return 0;
23 }
24
25 return git_default_config(var, value);
26}
27
Junio C Hamanocf9dfc62005-10-15 04:56:4628static char *quote_one(const char *str)
29{
30 int needlen;
31 char *xp;
32
33 if (!str)
34 return NULL;
35 needlen = quote_c_style(str, NULL, NULL, 0);
36 if (!needlen)
37 return strdup(str);
38 xp = xmalloc(needlen + 1);
39 quote_c_style(str, xp, NULL, 0);
40 return xp;
41}
42
43static char *quote_two(const char *one, const char *two)
44{
45 int need_one = quote_c_style(one, NULL, NULL, 1);
46 int need_two = quote_c_style(two, NULL, NULL, 1);
47 char *xp;
48
49 if (need_one + need_two) {
50 if (!need_one) need_one = strlen(one);
51 if (!need_two) need_one = strlen(two);
52
53 xp = xmalloc(need_one + need_two + 3);
54 xp[0] = '"';
55 quote_c_style(one, xp + 1, NULL, 1);
56 quote_c_style(two, xp + need_one + 1, NULL, 1);
57 strcpy(xp + need_one + need_two + 1, "\"");
58 return xp;
59 }
60 need_one = strlen(one);
61 need_two = strlen(two);
62 xp = xmalloc(need_one + need_two + 1);
63 strcpy(xp, one);
64 strcpy(xp + need_one, two);
65 return xp;
66}
67
Junio C Hamanobe3cfa82005-04-26 16:25:0568static const char *external_diff(void)
Junio C Hamano86436c22005-04-26 01:22:4769{
Junio C Hamanod19938a2005-05-10 00:57:5670 static const char *external_diff_cmd = NULL;
Junio C Hamanobe3cfa82005-04-26 16:25:0571 static int done_preparing = 0;
Jason Riedyc7c81b32005-08-23 20:34:0772 const char *env_diff_opts;
Junio C Hamanobe3cfa82005-04-26 16:25:0573
74 if (done_preparing)
75 return external_diff_cmd;
76
Junio C Hamano86436c22005-04-26 01:22:4777 /*
78 * Default values above are meant to match the
79 * Linux kernel development style. Examples of
80 * alternative styles you can specify via environment
81 * variables are:
82 *
Junio C Hamano86436c22005-04-26 01:22:4783 * GIT_DIFF_OPTS="-c";
84 */
Junio C Hamanoa9ab5862005-09-09 21:48:5485 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
Junio C Hamanobe3cfa82005-04-26 16:25:0586
87 /* In case external diff fails... */
Junio C Hamanoa9ab5862005-09-09 21:48:5488 env_diff_opts = getenv("GIT_DIFF_OPTS");
Jason Riedyc7c81b32005-08-23 20:34:0789 if (env_diff_opts) diff_opts = env_diff_opts;
Junio C Hamanobe3cfa82005-04-26 16:25:0590
91 done_preparing = 1;
92 return external_diff_cmd;
Junio C Hamano86436c22005-04-26 01:22:4793}
94
Holger Eitzenberger64f8a632005-08-04 20:49:4995#define TEMPFILE_PATH_LEN 50
96
Junio C Hamanobe3cfa82005-04-26 16:25:0597static struct diff_tempfile {
Junio C Hamano427dcb42005-05-21 09:39:0998 const char *name; /* filename external diff should read from */
Junio C Hamanobe3cfa82005-04-26 16:25:0599 char hex[41];
100 char mode[10];
Holger Eitzenberger64f8a632005-08-04 20:49:49101 char tmp_path[TEMPFILE_PATH_LEN];
Junio C Hamanobe3cfa82005-04-26 16:25:05102} diff_temp[2];
Junio C Hamano86436c22005-04-26 01:22:47103
Junio C Hamano366175e2005-06-19 20:17:50104static int count_lines(const char *filename)
105{
106 FILE *in;
107 int count, ch, completely_empty = 1, nl_just_seen = 0;
108 in = fopen(filename, "r");
109 count = 0;
110 while ((ch = fgetc(in)) != EOF)
111 if (ch == '\n') {
112 count++;
113 nl_just_seen = 1;
114 completely_empty = 0;
115 }
116 else {
117 nl_just_seen = 0;
118 completely_empty = 0;
119 }
120 fclose(in);
121 if (completely_empty)
122 return 0;
123 if (!nl_just_seen)
124 count++; /* no trailing newline */
125 return count;
126}
127
128static void print_line_count(int count)
129{
130 switch (count) {
131 case 0:
132 printf("0,0");
133 break;
134 case 1:
135 printf("1");
136 break;
137 default:
138 printf("1,%d", count);
139 break;
140 }
141}
142
143static void copy_file(int prefix, const char *filename)
144{
145 FILE *in;
146 int ch, nl_just_seen = 1;
147 in = fopen(filename, "r");
148 while ((ch = fgetc(in)) != EOF) {
149 if (nl_just_seen)
150 putchar(prefix);
151 putchar(ch);
152 if (ch == '\n')
153 nl_just_seen = 1;
154 else
155 nl_just_seen = 0;
156 }
157 fclose(in);
158 if (!nl_just_seen)
159 printf("\n\\ No newline at end of file\n");
160}
161
162static void emit_rewrite_diff(const char *name_a,
163 const char *name_b,
164 struct diff_tempfile *temp)
165{
166 /* Use temp[i].name as input, name_a and name_b as labels */
167 int lc_a, lc_b;
168 lc_a = count_lines(temp[0].name);
169 lc_b = count_lines(temp[1].name);
170 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
171 print_line_count(lc_a);
172 printf(" +");
173 print_line_count(lc_b);
174 printf(" @@\n");
175 if (lc_a)
176 copy_file('-', temp[0].name);
177 if (lc_b)
178 copy_file('+', temp[1].name);
179}
180
Junio C Hamano915838c2005-05-18 06:29:49181static void builtin_diff(const char *name_a,
182 const char *name_b,
Junio C Hamano57fe64a2005-05-20 02:00:36183 struct diff_tempfile *temp,
Junio C Hamano366175e2005-06-19 20:17:50184 const char *xfrm_msg,
185 int complete_rewrite)
Junio C Hamanobe3cfa82005-04-26 16:25:05186{
Thomas Glanzmann9669e172005-05-19 13:23:18187 int i, next_at, cmd_size;
Junio C Hamanocf9dfc62005-10-15 04:56:46188 const char *const diff_cmd = "diff -L%s -L%s";
Linus Torvalds694a7642005-10-18 07:16:45189 const char *const diff_arg = "-- %s %s||:"; /* "||:" is to return 0 */
Junio C Hamano2f978132005-04-28 15:04:39190 const char *input_name_sq[2];
Junio C Hamanocf9dfc62005-10-15 04:56:46191 const char *label_path[2];
Junio C Hamano2f978132005-04-28 15:04:39192 char *cmd;
Junio C Hamano915838c2005-05-18 06:29:49193
Junio C Hamanocf9dfc62005-10-15 04:56:46194 /* diff_cmd and diff_arg have 4 %s in total which makes
195 * the sum of these strings 8 bytes larger than required.
Junio C Hamanobe3cfa82005-04-26 16:25:05196 * we use 2 spaces around diff-opts, and we need to count
Junio C Hamanocf9dfc62005-10-15 04:56:46197 * terminating NUL; we used to subtract 5 here, but we do not
198 * care about small leaks in this subprocess that is about
199 * to exec "diff" anymore.
Junio C Hamanobe3cfa82005-04-26 16:25:05200 */
Junio C Hamanocf9dfc62005-10-15 04:56:46201 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg)
202 + 128);
203
Junio C Hamano2f978132005-04-28 15:04:39204 for (i = 0; i < 2; i++) {
Junio C Hamano6fb737b2005-07-08 06:58:32205 input_name_sq[i] = sq_quote(temp[i].name);
Junio C Hamanocf9dfc62005-10-15 04:56:46206 if (!strcmp(temp[i].name, "/dev/null"))
207 label_path[i] = "/dev/null";
208 else if (!i)
209 label_path[i] = sq_quote(quote_two("a/", name_a));
210 else
211 label_path[i] = sq_quote(quote_two("b/", name_b));
212 cmd_size += (strlen(label_path[i]) + strlen(input_name_sq[i]));
Junio C Hamano2f978132005-04-28 15:04:39213 }
Junio C Hamanobe3cfa82005-04-26 16:25:05214
Junio C Hamano2f978132005-04-28 15:04:39215 cmd = xmalloc(cmd_size);
216
217 next_at = 0;
Junio C Hamanobe3cfa82005-04-26 16:25:05218 next_at += snprintf(cmd+next_at, cmd_size-next_at,
Junio C Hamanocf9dfc62005-10-15 04:56:46219 diff_cmd, label_path[0], label_path[1]);
Junio C Hamanobe3cfa82005-04-26 16:25:05220 next_at += snprintf(cmd+next_at, cmd_size-next_at,
221 " %s ", diff_opts);
222 next_at += snprintf(cmd+next_at, cmd_size-next_at,
Junio C Hamano2f978132005-04-28 15:04:39223 diff_arg, input_name_sq[0], input_name_sq[1]);
224
Junio C Hamanocf9dfc62005-10-15 04:56:46225 printf("diff --git %s %s\n",
226 quote_two("a/", name_a), quote_two("b/", name_b));
227 if (label_path[0][0] == '/') {
228 /* dev/null */
Junio C Hamanob58f23b2005-05-18 16:10:47229 printf("new file mode %s\n", temp[1].mode);
Junio C Hamano70aadac2005-05-30 23:40:16230 if (xfrm_msg && xfrm_msg[0])
231 puts(xfrm_msg);
232 }
Junio C Hamanocf9dfc62005-10-15 04:56:46233 else if (label_path[1][0] == '/') {
Junio C Hamanob58f23b2005-05-18 16:10:47234 printf("deleted file mode %s\n", temp[0].mode);
Junio C Hamano70aadac2005-05-30 23:40:16235 if (xfrm_msg && xfrm_msg[0])
236 puts(xfrm_msg);
237 }
Junio C Hamano273b9832005-05-14 01:40:14238 else {
Junio C Hamanob58f23b2005-05-18 16:10:47239 if (strcmp(temp[0].mode, temp[1].mode)) {
240 printf("old mode %s\n", temp[0].mode);
241 printf("new mode %s\n", temp[1].mode);
242 }
Junio C Hamano427dcb42005-05-21 09:39:09243 if (xfrm_msg && xfrm_msg[0])
Junio C Hamano09d9d1a2005-05-27 22:54:06244 puts(xfrm_msg);
Junio C Hamano273b9832005-05-14 01:40:14245 if (strncmp(temp[0].mode, temp[1].mode, 3))
246 /* we do not run diff between different kind
247 * of objects.
248 */
Junio C Hamanob28858b2005-05-05 23:10:21249 exit(0);
Junio C Hamano366175e2005-06-19 20:17:50250 if (complete_rewrite) {
251 fflush(NULL);
252 emit_rewrite_diff(name_a, name_b, temp);
253 exit(0);
254 }
Junio C Hamanob28858b2005-05-05 23:10:21255 }
Junio C Hamanoc9833702005-05-01 16:33:12256 fflush(NULL);
Junio C Hamanobe3cfa82005-04-26 16:25:05257 execlp("/bin/sh","sh", "-c", cmd, NULL);
Junio C Hamano86436c22005-04-26 01:22:47258}
259
Junio C Hamano427dcb42005-05-21 09:39:09260struct diff_filespec *alloc_filespec(const char *path)
261{
262 int namelen = strlen(path);
263 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
Linus Torvalds705a7142005-09-14 20:41:24264
265 memset(spec, 0, sizeof(*spec));
Junio C Hamano427dcb42005-05-21 09:39:09266 spec->path = (char *)(spec + 1);
Linus Torvalds705a7142005-09-14 20:41:24267 memcpy(spec->path, path, namelen+1);
Junio C Hamano427dcb42005-05-21 09:39:09268 return spec;
269}
270
271void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
272 unsigned short mode)
273{
Junio C Hamano4130b992005-05-26 09:24:30274 if (mode) {
275 spec->mode = DIFF_FILE_CANON_MODE(mode);
Junio C Hamano81e50ea2005-05-22 02:42:18276 memcpy(spec->sha1, sha1, 20);
277 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
278 }
Junio C Hamano427dcb42005-05-21 09:39:09279}
280
Junio C Hamanob46f0b62005-05-04 08:45:24281/*
282 * Given a name and sha1 pair, if the dircache tells us the file in
283 * the work tree has that object contents, return true, so that
284 * prepare_temp_file() does not have to inflate and extract.
285 */
286static int work_tree_matches(const char *name, const unsigned char *sha1)
287{
288 struct cache_entry *ce;
289 struct stat st;
290 int pos, len;
Junio C Hamano5c975582005-05-19 10:32:35291
Junio C Hamanob46f0b62005-05-04 08:45:24292 /* We do not read the cache ourselves here, because the
293 * benchmark with my previous version that always reads cache
294 * shows that it makes things worse for diff-tree comparing
295 * two linux-2.6 kernel trees in an already checked out work
Junio C Hamano915838c2005-05-18 06:29:49296 * tree. This is because most diff-tree comparisons deal with
Junio C Hamanob46f0b62005-05-04 08:45:24297 * only a small number of files, while reading the cache is
298 * expensive for a large project, and its cost outweighs the
299 * savings we get by not inflating the object to a temporary
300 * file. Practically, this code only helps when we are used
301 * by diff-cache --cached, which does read the cache before
302 * calling us.
Junio C Hamano57fe64a2005-05-20 02:00:36303 */
Junio C Hamanob46f0b62005-05-04 08:45:24304 if (!active_cache)
305 return 0;
306
307 len = strlen(name);
308 pos = cache_name_pos(name, len);
309 if (pos < 0)
310 return 0;
311 ce = active_cache[pos];
Junio C Hamanob28858b2005-05-05 23:10:21312 if ((lstat(name, &st) < 0) ||
Junio C Hamano427dcb42005-05-21 09:39:09313 !S_ISREG(st.st_mode) || /* careful! */
Brad Roberts5d728c82005-05-15 02:04:25314 ce_match_stat(ce, &st) ||
Junio C Hamanob46f0b62005-05-04 08:45:24315 memcmp(sha1, ce->sha1, 20))
316 return 0;
Junio C Hamano427dcb42005-05-21 09:39:09317 /* we return 1 only when we can stat, it is a regular file,
318 * stat information matches, and sha1 recorded in the cache
319 * matches. I.e. we know the file in the work tree really is
320 * the same as the <name, sha1> pair.
321 */
Junio C Hamanob46f0b62005-05-04 08:45:24322 return 1;
323}
324
Junio C Hamanof0c6b2a2005-05-27 22:56:38325static struct sha1_size_cache {
326 unsigned char sha1[20];
327 unsigned long size;
328} **sha1_size_cache;
329static int sha1_size_cache_nr, sha1_size_cache_alloc;
330
331static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
Junio C Hamano0601e132005-06-04 06:02:23332 int find_only,
Junio C Hamanof0c6b2a2005-05-27 22:56:38333 unsigned long size)
334{
335 int first, last;
336 struct sha1_size_cache *e;
337
338 first = 0;
339 last = sha1_size_cache_nr;
340 while (last > first) {
Junio C Hamano0601e132005-06-04 06:02:23341 int cmp, next = (last + first) >> 1;
Junio C Hamanof0c6b2a2005-05-27 22:56:38342 e = sha1_size_cache[next];
Junio C Hamano0601e132005-06-04 06:02:23343 cmp = memcmp(e->sha1, sha1, 20);
Junio C Hamanof0c6b2a2005-05-27 22:56:38344 if (!cmp)
345 return e;
346 if (cmp < 0) {
347 last = next;
348 continue;
349 }
350 first = next+1;
351 }
352 /* not found */
Junio C Hamano0601e132005-06-04 06:02:23353 if (find_only)
Junio C Hamanof0c6b2a2005-05-27 22:56:38354 return NULL;
355 /* insert to make it at "first" */
356 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
357 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
358 sha1_size_cache = xrealloc(sha1_size_cache,
359 sha1_size_cache_alloc *
360 sizeof(*sha1_size_cache));
361 }
362 sha1_size_cache_nr++;
363 if (first < sha1_size_cache_nr)
364 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
365 (sha1_size_cache_nr - first - 1) *
366 sizeof(*sha1_size_cache));
367 e = xmalloc(sizeof(struct sha1_size_cache));
368 sha1_size_cache[first] = e;
369 memcpy(e->sha1, sha1, 20);
370 e->size = size;
371 return e;
372}
373
Junio C Hamano427dcb42005-05-21 09:39:09374/*
375 * While doing rename detection and pickaxe operation, we may need to
376 * grab the data for the blob (or file) for our own in-core comparison.
377 * diff_filespec has data and size fields for this purpose.
378 */
Junio C Hamanof0c6b2a2005-05-27 22:56:38379int diff_populate_filespec(struct diff_filespec *s, int size_only)
Junio C Hamano427dcb42005-05-21 09:39:09380{
381 int err = 0;
Junio C Hamano81e50ea2005-05-22 02:42:18382 if (!DIFF_FILE_VALID(s))
Junio C Hamano427dcb42005-05-21 09:39:09383 die("internal error: asking to populate invalid file.");
384 if (S_ISDIR(s->mode))
385 return -1;
386
Junio C Hamanof0c6b2a2005-05-27 22:56:38387 if (!use_size_cache)
388 size_only = 0;
389
Junio C Hamano427dcb42005-05-21 09:39:09390 if (s->data)
391 return err;
392 if (!s->sha1_valid ||
393 work_tree_matches(s->path, s->sha1)) {
394 struct stat st;
395 int fd;
396 if (lstat(s->path, &st) < 0) {
397 if (errno == ENOENT) {
398 err_empty:
399 err = -1;
400 empty:
401 s->data = "";
402 s->size = 0;
403 return err;
404 }
405 }
406 s->size = st.st_size;
407 if (!s->size)
408 goto empty;
Junio C Hamanof0c6b2a2005-05-27 22:56:38409 if (size_only)
410 return 0;
Junio C Hamano427dcb42005-05-21 09:39:09411 if (S_ISLNK(st.st_mode)) {
412 int ret;
413 s->data = xmalloc(s->size);
414 s->should_free = 1;
415 ret = readlink(s->path, s->data, s->size);
416 if (ret < 0) {
417 free(s->data);
418 goto err_empty;
419 }
420 return 0;
421 }
422 fd = open(s->path, O_RDONLY);
423 if (fd < 0)
424 goto err_empty;
425 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano427dcb42005-05-21 09:39:09426 close(fd);
Pavel Roskine35f9822005-07-29 14:49:14427 if (s->data == MAP_FAILED)
428 goto err_empty;
429 s->should_munmap = 1;
Junio C Hamano427dcb42005-05-21 09:39:09430 }
431 else {
432 char type[20];
Junio C Hamanof0c6b2a2005-05-27 22:56:38433 struct sha1_size_cache *e;
434
435 if (size_only) {
Junio C Hamano0601e132005-06-04 06:02:23436 e = locate_size_cache(s->sha1, 1, 0);
Junio C Hamanof0c6b2a2005-05-27 22:56:38437 if (e) {
438 s->size = e->size;
439 return 0;
440 }
Junio C Hamano36e4d742005-06-27 10:34:06441 if (!sha1_object_info(s->sha1, type, &s->size))
Junio C Hamano0601e132005-06-04 06:02:23442 locate_size_cache(s->sha1, 0, s->size);
Junio C Hamanof0c6b2a2005-05-27 22:56:38443 }
Junio C Hamano65c2e0c2005-06-02 22:20:54444 else {
445 s->data = read_sha1_file(s->sha1, type, &s->size);
446 s->should_free = 1;
447 }
Junio C Hamano427dcb42005-05-21 09:39:09448 }
449 return 0;
450}
451
Junio C Hamano19397b42005-09-14 21:06:50452void diff_free_filespec_data(struct diff_filespec *s)
Junio C Hamano427dcb42005-05-21 09:39:09453{
454 if (s->should_free)
455 free(s->data);
456 else if (s->should_munmap)
457 munmap(s->data, s->size);
Junio C Hamano19397b42005-09-14 21:06:50458 s->should_free = s->should_munmap = 0;
459 s->data = NULL;
Junio C Hamano427dcb42005-05-21 09:39:09460}
461
Junio C Hamanob28858b2005-05-05 23:10:21462static void prep_temp_blob(struct diff_tempfile *temp,
463 void *blob,
464 unsigned long size,
Junio C Hamano88cd6212005-09-30 21:02:47465 const unsigned char *sha1,
Junio C Hamanob28858b2005-05-05 23:10:21466 int mode)
467{
468 int fd;
469
Holger Eitzenberger64f8a632005-08-04 20:49:49470 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
Junio C Hamanob28858b2005-05-05 23:10:21471 if (fd < 0)
472 die("unable to create temp-file");
473 if (write(fd, blob, size) != size)
474 die("unable to write temp-file");
475 close(fd);
476 temp->name = temp->tmp_path;
477 strcpy(temp->hex, sha1_to_hex(sha1));
478 temp->hex[40] = 0;
479 sprintf(temp->mode, "%06o", mode);
480}
481
Junio C Hamanobe3cfa82005-04-26 16:25:05482static void prepare_temp_file(const char *name,
483 struct diff_tempfile *temp,
Junio C Hamano427dcb42005-05-21 09:39:09484 struct diff_filespec *one)
Junio C Hamano86436c22005-04-26 01:22:47485{
Junio C Hamano81e50ea2005-05-22 02:42:18486 if (!DIFF_FILE_VALID(one)) {
Junio C Hamanobe3cfa82005-04-26 16:25:05487 not_a_valid_file:
Junio C Hamano532149d2005-04-28 17:13:01488 /* A '-' entry produces this for file-2, and
489 * a '+' entry produces this for file-1.
490 */
Junio C Hamanobe3cfa82005-04-26 16:25:05491 temp->name = "/dev/null";
492 strcpy(temp->hex, ".");
493 strcpy(temp->mode, ".");
Junio C Hamano86436c22005-04-26 01:22:47494 return;
495 }
Junio C Hamanobe3cfa82005-04-26 16:25:05496
Junio C Hamano5c975582005-05-19 10:32:35497 if (!one->sha1_valid ||
Junio C Hamano427dcb42005-05-21 09:39:09498 work_tree_matches(name, one->sha1)) {
Junio C Hamanobe3cfa82005-04-26 16:25:05499 struct stat st;
Junio C Hamano427dcb42005-05-21 09:39:09500 if (lstat(name, &st) < 0) {
Junio C Hamanobe3cfa82005-04-26 16:25:05501 if (errno == ENOENT)
502 goto not_a_valid_file;
Junio C Hamano427dcb42005-05-21 09:39:09503 die("stat(%s): %s", name, strerror(errno));
Junio C Hamanobe3cfa82005-04-26 16:25:05504 }
Junio C Hamanob28858b2005-05-05 23:10:21505 if (S_ISLNK(st.st_mode)) {
506 int ret;
507 char *buf, buf_[1024];
508 buf = ((sizeof(buf_) < st.st_size) ?
509 xmalloc(st.st_size) : buf_);
510 ret = readlink(name, buf, st.st_size);
511 if (ret < 0)
512 die("readlink(%s)", name);
513 prep_temp_blob(temp, buf, st.st_size,
514 (one->sha1_valid ?
Junio C Hamano427dcb42005-05-21 09:39:09515 one->sha1 : null_sha1),
Junio C Hamanob28858b2005-05-05 23:10:21516 (one->sha1_valid ?
517 one->mode : S_IFLNK));
518 }
519 else {
Junio C Hamano427dcb42005-05-21 09:39:09520 /* we can borrow from the file in the work tree */
521 temp->name = name;
Junio C Hamanob28858b2005-05-05 23:10:21522 if (!one->sha1_valid)
523 strcpy(temp->hex, sha1_to_hex(null_sha1));
524 else
Junio C Hamano427dcb42005-05-21 09:39:09525 strcpy(temp->hex, sha1_to_hex(one->sha1));
Junio C Hamano9d429ff2005-05-30 07:07:39526 /* Even though we may sometimes borrow the
527 * contents from the work tree, we always want
528 * one->mode. mode is trustworthy even when
529 * !(one->sha1_valid), as long as
530 * DIFF_FILE_VALID(one).
531 */
532 sprintf(temp->mode, "%06o", one->mode);
Junio C Hamanob28858b2005-05-05 23:10:21533 }
534 return;
Junio C Hamanobe3cfa82005-04-26 16:25:05535 }
536 else {
Junio C Hamanof0c6b2a2005-05-27 22:56:38537 if (diff_populate_filespec(one, 0))
Junio C Hamano427dcb42005-05-21 09:39:09538 die("cannot read data blob for %s", one->path);
539 prep_temp_blob(temp, one->data, one->size,
540 one->sha1, one->mode);
Junio C Hamanobe3cfa82005-04-26 16:25:05541 }
542}
543
544static void remove_tempfile(void)
545{
546 int i;
547
548 for (i = 0; i < 2; i++)
549 if (diff_temp[i].name == diff_temp[i].tmp_path) {
550 unlink(diff_temp[i].name);
551 diff_temp[i].name = NULL;
552 }
553}
554
Junio C Hamano532149d2005-04-28 17:13:01555static void remove_tempfile_on_signal(int signo)
556{
557 remove_tempfile();
558}
559
Junio C Hamanobe3cfa82005-04-26 16:25:05560/* An external diff command takes:
561 *
562 * diff-cmd name infile1 infile1-sha1 infile1-mode \
Junio C Hamano5c975582005-05-19 10:32:35563 * infile2 infile2-sha1 infile2-mode [ rename-to ]
Junio C Hamanobe3cfa82005-04-26 16:25:05564 *
565 */
Junio C Hamano4130b992005-05-26 09:24:30566static void run_external_diff(const char *pgm,
567 const char *name,
Junio C Hamano5c975582005-05-19 10:32:35568 const char *other,
Junio C Hamano427dcb42005-05-21 09:39:09569 struct diff_filespec *one,
570 struct diff_filespec *two,
Junio C Hamano366175e2005-06-19 20:17:50571 const char *xfrm_msg,
572 int complete_rewrite)
Junio C Hamanobe3cfa82005-04-26 16:25:05573{
574 struct diff_tempfile *temp = diff_temp;
Junio C Hamano532149d2005-04-28 17:13:01575 pid_t pid;
576 int status;
Junio C Hamanobe3cfa82005-04-26 16:25:05577 static int atexit_asked = 0;
Jason Riedyc7c81b32005-08-23 20:34:07578 const char *othername;
Junio C Hamanobe3cfa82005-04-26 16:25:05579
Jason Riedyc7c81b32005-08-23 20:34:07580 othername = (other? other : name);
Junio C Hamano77eb2722005-04-27 16:21:00581 if (one && two) {
582 prepare_temp_file(name, &temp[0], one);
Jason Riedyc7c81b32005-08-23 20:34:07583 prepare_temp_file(othername, &temp[1], two);
Junio C Hamano77eb2722005-04-27 16:21:00584 if (! atexit_asked &&
585 (temp[0].name == temp[0].tmp_path ||
586 temp[1].name == temp[1].tmp_path)) {
587 atexit_asked = 1;
588 atexit(remove_tempfile);
589 }
Junio C Hamano532149d2005-04-28 17:13:01590 signal(SIGINT, remove_tempfile_on_signal);
Junio C Hamanobe3cfa82005-04-26 16:25:05591 }
592
593 fflush(NULL);
594 pid = fork();
595 if (pid < 0)
596 die("unable to fork");
597 if (!pid) {
Junio C Hamano5c975582005-05-19 10:32:35598 if (pgm) {
599 if (one && two) {
Junio C Hamano427dcb42005-05-21 09:39:09600 const char *exec_arg[10];
Junio C Hamano5c975582005-05-19 10:32:35601 const char **arg = &exec_arg[0];
602 *arg++ = pgm;
603 *arg++ = name;
604 *arg++ = temp[0].name;
605 *arg++ = temp[0].hex;
606 *arg++ = temp[0].mode;
607 *arg++ = temp[1].name;
608 *arg++ = temp[1].hex;
609 *arg++ = temp[1].mode;
Junio C Hamano427dcb42005-05-21 09:39:09610 if (other) {
Junio C Hamano5c975582005-05-19 10:32:35611 *arg++ = other;
Junio C Hamano427dcb42005-05-21 09:39:09612 *arg++ = xfrm_msg;
613 }
Linus Torvalds09d74b32005-05-22 21:33:43614 *arg = NULL;
Junio C Hamano5c975582005-05-19 10:32:35615 execvp(pgm, (char *const*) exec_arg);
616 }
Junio C Hamano77eb2722005-04-27 16:21:00617 else
618 execlp(pgm, pgm, name, NULL);
619 }
Junio C Hamanobe3cfa82005-04-26 16:25:05620 /*
621 * otherwise we use the built-in one.
622 */
Junio C Hamano77eb2722005-04-27 16:21:00623 if (one && two)
Jason Riedyc7c81b32005-08-23 20:34:07624 builtin_diff(name, othername, temp, xfrm_msg,
Junio C Hamano366175e2005-06-19 20:17:50625 complete_rewrite);
Junio C Hamano77eb2722005-04-27 16:21:00626 else
627 printf("* Unmerged path %s\n", name);
Junio C Hamanobe3cfa82005-04-26 16:25:05628 exit(0);
629 }
Junio C Hamano6fa28062005-05-04 08:38:06630 if (waitpid(pid, &status, 0) < 0 ||
631 !WIFEXITED(status) || WEXITSTATUS(status)) {
632 /* Earlier we did not check the exit status because
Junio C Hamano532149d2005-04-28 17:13:01633 * diff exits non-zero if files are different, and
Junio C Hamano6fa28062005-05-04 08:38:06634 * we are not interested in knowing that. It was a
635 * mistake which made it harder to quit a diff-*
636 * session that uses the git-apply-patch-script as
637 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
638 * should also exit non-zero only when it wants to
639 * abort the entire diff-* session.
Junio C Hamano532149d2005-04-28 17:13:01640 */
641 remove_tempfile();
Junio C Hamano6fa28062005-05-04 08:38:06642 fprintf(stderr, "external diff died, stopping at %s.\n", name);
643 exit(1);
Junio C Hamano532149d2005-04-28 17:13:01644 }
Junio C Hamanobe3cfa82005-04-26 16:25:05645 remove_tempfile();
646}
647
Junio C Hamanoec1fcc12005-10-07 10:42:00648static void diff_fill_sha1_info(struct diff_filespec *one)
649{
650 if (DIFF_FILE_VALID(one)) {
651 if (!one->sha1_valid) {
652 struct stat st;
653 if (stat(one->path, &st) < 0)
654 die("stat %s", one->path);
655 if (index_path(one->sha1, one->path, &st, 0))
656 die("cannot hash %s\n", one->path);
657 }
658 }
659 else
660 memset(one->sha1, 0, 20);
661}
662
Junio C Hamano80b1e512005-11-15 01:53:22663static void run_diff(struct diff_filepair *p, struct diff_options *o)
Junio C Hamano4130b992005-05-26 09:24:30664{
665 const char *pgm = external_diff();
Junio C Hamanoec1fcc12005-10-07 10:42:00666 char msg[PATH_MAX*2+300], *xfrm_msg;
Junio C Hamano366175e2005-06-19 20:17:50667 struct diff_filespec *one;
668 struct diff_filespec *two;
669 const char *name;
670 const char *other;
Junio C Hamanocf9dfc62005-10-15 04:56:46671 char *name_munged, *other_munged;
Junio C Hamano366175e2005-06-19 20:17:50672 int complete_rewrite = 0;
Junio C Hamanoec1fcc12005-10-07 10:42:00673 int len;
Junio C Hamano366175e2005-06-19 20:17:50674
675 if (DIFF_PAIR_UNMERGED(p)) {
676 /* unmerged */
677 run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL,
678 0);
679 return;
680 }
681
682 name = p->one->path;
683 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocf9dfc62005-10-15 04:56:46684 name_munged = quote_one(name);
685 other_munged = quote_one(other);
Junio C Hamano366175e2005-06-19 20:17:50686 one = p->one; two = p->two;
Junio C Hamanoec1fcc12005-10-07 10:42:00687
688 diff_fill_sha1_info(one);
689 diff_fill_sha1_info(two);
690
691 len = 0;
Junio C Hamano366175e2005-06-19 20:17:50692 switch (p->status) {
Junio C Hamanoe7baa4f2005-07-25 20:05:44693 case DIFF_STATUS_COPIED:
Junio C Hamanoec1fcc12005-10-07 10:42:00694 len += snprintf(msg + len, sizeof(msg) - len,
695 "similarity index %d%%\n"
696 "copy from %s\n"
697 "copy to %s\n",
698 (int)(0.5 + p->score * 100.0/MAX_SCORE),
Junio C Hamanocf9dfc62005-10-15 04:56:46699 name_munged, other_munged);
Junio C Hamano366175e2005-06-19 20:17:50700 break;
Junio C Hamanoe7baa4f2005-07-25 20:05:44701 case DIFF_STATUS_RENAMED:
Junio C Hamanoec1fcc12005-10-07 10:42:00702 len += snprintf(msg + len, sizeof(msg) - len,
703 "similarity index %d%%\n"
704 "rename from %s\n"
705 "rename to %s\n",
706 (int)(0.5 + p->score * 100.0/MAX_SCORE),
Junio C Hamanocf9dfc62005-10-15 04:56:46707 name_munged, other_munged);
Junio C Hamano366175e2005-06-19 20:17:50708 break;
Junio C Hamanoe7baa4f2005-07-25 20:05:44709 case DIFF_STATUS_MODIFIED:
Junio C Hamano366175e2005-06-19 20:17:50710 if (p->score) {
Junio C Hamanoec1fcc12005-10-07 10:42:00711 len += snprintf(msg + len, sizeof(msg) - len,
712 "dissimilarity index %d%%\n",
713 (int)(0.5 + p->score *
714 100.0/MAX_SCORE));
Junio C Hamano366175e2005-06-19 20:17:50715 complete_rewrite = 1;
716 break;
717 }
718 /* fallthru */
719 default:
Junio C Hamanoec1fcc12005-10-07 10:42:00720 /* nothing */
721 ;
Junio C Hamano366175e2005-06-19 20:17:50722 }
723
Junio C Hamanoec1fcc12005-10-07 10:42:00724 if (memcmp(one->sha1, two->sha1, 20)) {
725 char one_sha1[41];
Junio C Hamano47dd0d52005-12-14 01:21:41726 int abbrev = o->full_index ? 40 : DIFF_DEFAULT_INDEX_ABBREV;
Junio C Hamanoec1fcc12005-10-07 10:42:00727 memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
728
729 len += snprintf(msg + len, sizeof(msg) - len,
Junio C Hamano47dd0d52005-12-14 01:21:41730 "index %.*s..%.*s",
731 abbrev, one_sha1, abbrev,
732 sha1_to_hex(two->sha1));
Junio C Hamanoec1fcc12005-10-07 10:42:00733 if (one->mode == two->mode)
734 len += snprintf(msg + len, sizeof(msg) - len,
735 " %06o", one->mode);
736 len += snprintf(msg + len, sizeof(msg) - len, "\n");
737 }
738
739 if (len)
740 msg[--len] = 0;
741 xfrm_msg = len ? msg : NULL;
742
Junio C Hamano4130b992005-05-26 09:24:30743 if (!pgm &&
744 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
745 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
746 /* a filepair that changes between file and symlink
747 * needs to be split into deletion and creation.
748 */
749 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamano366175e2005-06-19 20:17:50750 run_external_diff(NULL, name, other, one, null, xfrm_msg, 0);
Junio C Hamano4130b992005-05-26 09:24:30751 free(null);
752 null = alloc_filespec(one->path);
Junio C Hamano366175e2005-06-19 20:17:50753 run_external_diff(NULL, name, other, null, two, xfrm_msg, 0);
Junio C Hamano4130b992005-05-26 09:24:30754 free(null);
755 }
756 else
Junio C Hamano366175e2005-06-19 20:17:50757 run_external_diff(pgm, name, other, one, two, xfrm_msg,
758 complete_rewrite);
Junio C Hamanocf9dfc62005-10-15 04:56:46759
760 free(name_munged);
761 free(other_munged);
Junio C Hamano4130b992005-05-26 09:24:30762}
763
Junio C Hamano6b5ee132005-09-21 07:00:47764void diff_setup(struct diff_options *options)
Junio C Hamano5c975582005-05-19 10:32:35765{
Junio C Hamano6b5ee132005-09-21 07:00:47766 memset(options, 0, sizeof(*options));
767 options->output_format = DIFF_FORMAT_RAW;
768 options->line_termination = '\n';
769 options->break_opt = -1;
Junio C Hamano8082d8d2005-09-21 07:18:27770 options->rename_limit = -1;
Linus Torvaldsac1b3d12005-10-21 04:05:05771
772 options->change = diff_change;
773 options->add_remove = diff_addremove;
Junio C Hamano6b5ee132005-09-21 07:00:47774}
775
776int diff_setup_done(struct diff_options *options)
777{
Junio C Hamano3299c6f2005-11-15 20:48:08778 if ((options->find_copies_harder &&
779 options->detect_rename != DIFF_DETECT_COPY) ||
780 (0 <= options->rename_limit && !options->detect_rename))
Junio C Hamano6b5ee132005-09-21 07:00:47781 return -1;
Junio C Hamano3299c6f2005-11-15 20:48:08782 if (options->detect_rename && options->rename_limit < 0)
783 options->rename_limit = diff_rename_limit_default;
Junio C Hamano6b5ee132005-09-21 07:00:47784 if (options->setup & DIFF_SETUP_USE_CACHE) {
Junio C Hamanof0c6b2a2005-05-27 22:56:38785 if (!active_cache)
786 /* read-cache does not die even when it fails
787 * so it is safe for us to do this here. Also
788 * it does not smudge active_cache or active_nr
789 * when it fails, so we do not have to worry about
790 * cleaning it up oufselves either.
791 */
792 read_cache();
793 }
Junio C Hamano6b5ee132005-09-21 07:00:47794 if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
Junio C Hamanof0c6b2a2005-05-27 22:56:38795 use_size_cache = 1;
Junio C Hamano47dd0d52005-12-14 01:21:41796 if (options->abbrev <= 0 || 40 < options->abbrev)
797 options->abbrev = 40; /* full */
Junio C Hamano6b5ee132005-09-21 07:00:47798
799 return 0;
800}
801
802int diff_opt_parse(struct diff_options *options, const char **av, int ac)
803{
804 const char *arg = av[0];
805 if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
806 options->output_format = DIFF_FORMAT_PATCH;
807 else if (!strcmp(arg, "-z"))
808 options->line_termination = 0;
Junio C Hamano8082d8d2005-09-21 07:18:27809 else if (!strncmp(arg, "-l", 2))
810 options->rename_limit = strtoul(arg+2, NULL, 10);
Junio C Hamano80b1e512005-11-15 01:53:22811 else if (!strcmp(arg, "--full-index"))
812 options->full_index = 1;
Junio C Hamano6b5ee132005-09-21 07:00:47813 else if (!strcmp(arg, "--name-only"))
814 options->output_format = DIFF_FORMAT_NAME;
Junio C Hamano946f5f72005-09-21 07:20:06815 else if (!strcmp(arg, "--name-status"))
816 options->output_format = DIFF_FORMAT_NAME_STATUS;
Junio C Hamano6b5ee132005-09-21 07:00:47817 else if (!strcmp(arg, "-R"))
818 options->reverse_diff = 1;
819 else if (!strncmp(arg, "-S", 2))
820 options->pickaxe = arg + 2;
821 else if (!strcmp(arg, "-s"))
822 options->output_format = DIFF_FORMAT_NO_OUTPUT;
823 else if (!strncmp(arg, "-O", 2))
824 options->orderfile = arg + 2;
825 else if (!strncmp(arg, "--diff-filter=", 14))
826 options->filter = arg + 14;
827 else if (!strcmp(arg, "--pickaxe-all"))
828 options->pickaxe_opts = DIFF_PICKAXE_ALL;
829 else if (!strncmp(arg, "-B", 2)) {
830 if ((options->break_opt =
831 diff_scoreopt_parse(arg)) == -1)
832 return -1;
833 }
834 else if (!strncmp(arg, "-M", 2)) {
835 if ((options->rename_score =
836 diff_scoreopt_parse(arg)) == -1)
837 return -1;
838 options->detect_rename = DIFF_DETECT_RENAME;
839 }
840 else if (!strncmp(arg, "-C", 2)) {
841 if ((options->rename_score =
842 diff_scoreopt_parse(arg)) == -1)
843 return -1;
844 options->detect_rename = DIFF_DETECT_COPY;
845 }
846 else if (!strcmp(arg, "--find-copies-harder"))
847 options->find_copies_harder = 1;
Junio C Hamano47dd0d52005-12-14 01:21:41848 else if (!strcmp(arg, "--abbrev"))
849 options->abbrev = DIFF_DEFAULT_ABBREV;
850 else if (!strncmp(arg, "--abbrev=", 9))
851 options->abbrev = strtoul(arg + 9, NULL, 10);
Junio C Hamano6b5ee132005-09-21 07:00:47852 else
853 return 0;
854 return 1;
Junio C Hamano57fe64a2005-05-20 02:00:36855}
856
Junio C Hamano0e3994f2005-06-03 08:37:54857static int parse_num(const char **cp_p)
858{
H. Peter Anvin1b1480f2005-11-21 22:17:12859 unsigned long num, scale;
860 int ch, dot;
Junio C Hamano0e3994f2005-06-03 08:37:54861 const char *cp = *cp_p;
862
H. Peter Anvin1b1480f2005-11-21 22:17:12863 num = 0;
Junio C Hamano0e3994f2005-06-03 08:37:54864 scale = 1;
H. Peter Anvin1b1480f2005-11-21 22:17:12865 dot = 0;
866 for(;;) {
867 ch = *cp;
868 if ( !dot && ch == '.' ) {
869 scale = 1;
870 dot = 1;
871 } else if ( ch == '%' ) {
872 scale = dot ? scale*100 : 100;
873 cp++; /* % is always at the end */
874 break;
875 } else if ( ch >= '0' && ch <= '9' ) {
876 if ( scale < 100000 ) {
877 scale *= 10;
878 num = (num*10) + (ch-'0');
879 }
880 } else {
881 break;
Junio C Hamano0e3994f2005-06-03 08:37:54882 }
Junio C Hamano5de36bf2005-08-30 04:17:21883 cp++;
Junio C Hamano0e3994f2005-06-03 08:37:54884 }
885 *cp_p = cp;
886
887 /* user says num divided by scale and we say internally that
888 * is MAX_SCORE * num / scale.
889 */
H. Peter Anvin1b1480f2005-11-21 22:17:12890 return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
Junio C Hamano0e3994f2005-06-03 08:37:54891}
892
893int diff_scoreopt_parse(const char *opt)
894{
Junio C Hamanoeeaa4602005-06-03 08:40:28895 int opt1, opt2, cmd;
Junio C Hamano0e3994f2005-06-03 08:37:54896
897 if (*opt++ != '-')
898 return -1;
899 cmd = *opt++;
900 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
901 return -1; /* that is not a -M, -C nor -B option */
902
903 opt1 = parse_num(&opt);
Junio C Hamanoeeaa4602005-06-03 08:40:28904 if (cmd != 'B')
905 opt2 = 0;
906 else {
907 if (*opt == 0)
908 opt2 = 0;
909 else if (*opt != '/')
910 return -1; /* we expect -B80/99 or -B80 */
911 else {
912 opt++;
913 opt2 = parse_num(&opt);
914 }
915 }
Junio C Hamano0e3994f2005-06-03 08:37:54916 if (*opt != 0)
917 return -1;
Junio C Hamanoeeaa4602005-06-03 08:40:28918 return opt1 | (opt2 << 16);
Junio C Hamano0e3994f2005-06-03 08:37:54919}
920
Junio C Hamano38c6f782005-05-22 02:40:36921struct diff_queue_struct diff_queued_diff;
Junio C Hamano427dcb42005-05-21 09:39:09922
Junio C Hamano6b14d7f2005-05-22 17:04:37923void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
924{
925 if (queue->alloc <= queue->nr) {
926 queue->alloc = alloc_nr(queue->alloc);
927 queue->queue = xrealloc(queue->queue,
928 sizeof(dp) * queue->alloc);
929 }
930 queue->queue[queue->nr++] = dp;
931}
932
Junio C Hamano52e95782005-05-21 09:40:01933struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
Junio C Hamano6b14d7f2005-05-22 17:04:37934 struct diff_filespec *one,
935 struct diff_filespec *two)
Junio C Hamano427dcb42005-05-21 09:39:09936{
Junio C Hamano52e95782005-05-21 09:40:01937 struct diff_filepair *dp = xmalloc(sizeof(*dp));
Junio C Hamano427dcb42005-05-21 09:39:09938 dp->one = one;
939 dp->two = two;
Junio C Hamanof7c15122005-05-23 04:26:09940 dp->score = 0;
Junio C Hamano22101002005-06-12 03:55:20941 dp->status = 0;
Junio C Hamano15d061b2005-05-27 22:55:55942 dp->source_stays = 0;
Junio C Hamanof345b0a2005-05-30 07:08:37943 dp->broken_pair = 0;
Junio C Hamano5098baf2005-09-15 23:13:43944 if (queue)
945 diff_q(queue, dp);
Junio C Hamano427dcb42005-05-21 09:39:09946 return dp;
947}
948
Junio C Hamano226406f2005-05-27 22:50:30949void diff_free_filepair(struct diff_filepair *p)
950{
Junio C Hamano19397b42005-09-14 21:06:50951 diff_free_filespec_data(p->one);
952 diff_free_filespec_data(p->two);
Junio C Hamano5098baf2005-09-15 23:13:43953 free(p->one);
954 free(p->two);
Junio C Hamano226406f2005-05-27 22:50:30955 free(p);
956}
957
Junio C Hamano47dd0d52005-12-14 01:21:41958/* This is different from find_unique_abbrev() in that
959 * it needs to deal with 0{40} SHA1.
960 */
961const char *diff_unique_abbrev(const unsigned char *sha1, int len)
962{
963 int abblen;
964 const char *abbrev;
965 if (len == 40)
966 return sha1_to_hex(sha1);
967
968 abbrev = find_unique_abbrev(sha1, len);
969 if (!abbrev) {
970 if (!memcmp(sha1, null_sha1, 20)) {
971 char *buf = sha1_to_hex(null_sha1);
972 if (len < 37)
973 strcpy(buf + len, "...");
974 return buf;
975 }
976 else
977 return sha1_to_hex(sha1);
978 }
979 abblen = strlen(abbrev);
980 if (abblen < 37) {
981 static char hex[41];
982 if (len < abblen && abblen <= len + 2)
983 sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
984 else
985 sprintf(hex, "%s...", abbrev);
986 return hex;
987 }
988 return sha1_to_hex(sha1);
989}
990
Junio C Hamanobceafe72005-05-24 01:14:03991static void diff_flush_raw(struct diff_filepair *p,
992 int line_termination,
Junio C Hamano946f5f72005-09-21 07:20:06993 int inter_name_termination,
Junio C Hamano47dd0d52005-12-14 01:21:41994 struct diff_options *options)
Junio C Hamano427dcb42005-05-21 09:39:09995{
Junio C Hamanob6d8f302005-05-23 21:55:33996 int two_paths;
997 char status[10];
Junio C Hamano47dd0d52005-12-14 01:21:41998 int abbrev = options->abbrev;
Junio C Hamanocf9dfc62005-10-15 04:56:46999 const char *path_one, *path_two;
Junio C Hamano47dd0d52005-12-14 01:21:411000 int output_format = options->output_format;
Junio C Hamano25d5ea42005-05-24 08:10:481001
Junio C Hamanocf9dfc62005-10-15 04:56:461002 path_one = p->one->path;
1003 path_two = p->two->path;
Junio C Hamano25d5ea42005-05-24 08:10:481004 if (line_termination) {
Junio C Hamanocf9dfc62005-10-15 04:56:461005 path_one = quote_one(path_one);
1006 path_two = quote_one(path_two);
Junio C Hamano25d5ea42005-05-24 08:10:481007 }
1008
Junio C Hamano366175e2005-06-19 20:17:501009 if (p->score)
1010 sprintf(status, "%c%03d", p->status,
1011 (int)(0.5 + p->score * 100.0/MAX_SCORE));
1012 else {
1013 status[0] = p->status;
1014 status[1] = 0;
1015 }
Junio C Hamanob6d8f302005-05-23 21:55:331016 switch (p->status) {
Junio C Hamanoe7baa4f2005-07-25 20:05:441017 case DIFF_STATUS_COPIED:
1018 case DIFF_STATUS_RENAMED:
Junio C Hamanob6d8f302005-05-23 21:55:331019 two_paths = 1;
Junio C Hamanob6d8f302005-05-23 21:55:331020 break;
Junio C Hamanoe7baa4f2005-07-25 20:05:441021 case DIFF_STATUS_ADDED:
1022 case DIFF_STATUS_DELETED:
Junio C Hamanof345b0a2005-05-30 07:08:371023 two_paths = 0;
Junio C Hamanof345b0a2005-05-30 07:08:371024 break;
Junio C Hamanob6d8f302005-05-23 21:55:331025 default:
1026 two_paths = 0;
Junio C Hamanob6d8f302005-05-23 21:55:331027 break;
Junio C Hamano6b14d7f2005-05-22 17:04:371028 }
Junio C Hamano946f5f72005-09-21 07:20:061029 if (output_format != DIFF_FORMAT_NAME_STATUS) {
1030 printf(":%06o %06o %s ",
Junio C Hamano47dd0d52005-12-14 01:21:411031 p->one->mode, p->two->mode,
1032 diff_unique_abbrev(p->one->sha1, abbrev));
1033 printf("%s ",
1034 diff_unique_abbrev(p->two->sha1, abbrev));
Junio C Hamano946f5f72005-09-21 07:20:061035 }
Junio C Hamanocf9dfc62005-10-15 04:56:461036 printf("%s%c%s", status, inter_name_termination, path_one);
Junio C Hamanob6d8f302005-05-23 21:55:331037 if (two_paths)
Junio C Hamanocf9dfc62005-10-15 04:56:461038 printf("%c%s", inter_name_termination, path_two);
Junio C Hamanob6d8f302005-05-23 21:55:331039 putchar(line_termination);
Junio C Hamanocf9dfc62005-10-15 04:56:461040 if (path_one != p->one->path)
1041 free((void*)path_one);
1042 if (path_two != p->two->path)
1043 free((void*)path_two);
Junio C Hamano427dcb42005-05-21 09:39:091044}
1045
Junio C Hamano52f28522005-07-13 19:45:511046static void diff_flush_name(struct diff_filepair *p,
Junio C Hamanocf9dfc62005-10-15 04:56:461047 int inter_name_termination,
Junio C Hamano52f28522005-07-13 19:45:511048 int line_termination)
1049{
Junio C Hamanocf9dfc62005-10-15 04:56:461050 char *path = p->two->path;
1051
1052 if (line_termination)
1053 path = quote_one(p->two->path);
1054 else
1055 path = p->two->path;
1056 printf("%s%c", path, line_termination);
1057 if (p->two->path != path)
1058 free(path);
Junio C Hamano52f28522005-07-13 19:45:511059}
1060
Junio C Hamanof7c15122005-05-23 04:26:091061int diff_unmodified_pair(struct diff_filepair *p)
Junio C Hamano427dcb42005-05-21 09:39:091062{
1063 /* This function is written stricter than necessary to support
1064 * the currently implemented transformers, but the idea is to
Junio C Hamano52e95782005-05-21 09:40:011065 * let transformers to produce diff_filepairs any way they want,
Junio C Hamano427dcb42005-05-21 09:39:091066 * and filter and clean them up here before producing the output.
1067 */
Junio C Hamano6b14d7f2005-05-22 17:04:371068 struct diff_filespec *one, *two;
Junio C Hamano427dcb42005-05-21 09:39:091069
Junio C Hamano6b14d7f2005-05-22 17:04:371070 if (DIFF_PAIR_UNMERGED(p))
1071 return 0; /* unmerged is interesting */
1072
1073 one = p->one;
1074 two = p->two;
Junio C Hamano427dcb42005-05-21 09:39:091075
Junio C Hamano4130b992005-05-26 09:24:301076 /* deletion, addition, mode or type change
1077 * and rename are all interesting.
1078 */
Junio C Hamano81e50ea2005-05-22 02:42:181079 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
Junio C Hamano4130b992005-05-26 09:24:301080 DIFF_PAIR_MODE_CHANGED(p) ||
Junio C Hamano427dcb42005-05-21 09:39:091081 strcmp(one->path, two->path))
1082 return 0;
1083
1084 /* both are valid and point at the same path. that is, we are
1085 * dealing with a change.
1086 */
1087 if (one->sha1_valid && two->sha1_valid &&
1088 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1089 return 1; /* no change */
1090 if (!one->sha1_valid && !two->sha1_valid)
1091 return 1; /* both look at the same file on the filesystem. */
1092 return 0;
1093}
1094
Junio C Hamano80b1e512005-11-15 01:53:221095static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
Junio C Hamanof7c15122005-05-23 04:26:091096{
Junio C Hamanof7c15122005-05-23 04:26:091097 if (diff_unmodified_pair(p))
1098 return;
1099
Junio C Hamanof7c15122005-05-23 04:26:091100 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1101 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1102 return; /* no tree diffs in patch format */
1103
Junio C Hamano80b1e512005-11-15 01:53:221104 run_diff(p, o);
Junio C Hamanof7c15122005-05-23 04:26:091105}
1106
Junio C Hamano38c6f782005-05-22 02:40:361107int diff_queue_is_empty(void)
Junio C Hamano427dcb42005-05-21 09:39:091108{
Junio C Hamano38c6f782005-05-22 02:40:361109 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano25d5ea42005-05-24 08:10:481110 int i;
1111 for (i = 0; i < q->nr; i++)
1112 if (!diff_unmodified_pair(q->queue[i]))
1113 return 0;
1114 return 1;
Junio C Hamano38c6f782005-05-22 02:40:361115}
1116
Junio C Hamano25d5ea42005-05-24 08:10:481117#if DIFF_DEBUG
1118void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1119{
1120 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
Jason Riedyc7c81b32005-08-23 20:34:071121 x, one ? one : "",
Junio C Hamano25d5ea42005-05-24 08:10:481122 s->path,
1123 DIFF_FILE_VALID(s) ? "valid" : "invalid",
1124 s->mode,
1125 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1126 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
Jason Riedyc7c81b32005-08-23 20:34:071127 x, one ? one : "",
Junio C Hamano25d5ea42005-05-24 08:10:481128 s->size, s->xfrm_flags);
1129}
1130
1131void diff_debug_filepair(const struct diff_filepair *p, int i)
1132{
1133 diff_debug_filespec(p->one, i, "one");
1134 diff_debug_filespec(p->two, i, "two");
Junio C Hamanof345b0a2005-05-30 07:08:371135 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
Jason Riedyc7c81b32005-08-23 20:34:071136 p->score, p->status ? p->status : '?',
Junio C Hamanof345b0a2005-05-30 07:08:371137 p->source_stays, p->broken_pair);
Junio C Hamano25d5ea42005-05-24 08:10:481138}
1139
1140void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1141{
1142 int i;
1143 if (msg)
1144 fprintf(stderr, "%s\n", msg);
1145 fprintf(stderr, "q->nr = %d\n", q->nr);
1146 for (i = 0; i < q->nr; i++) {
1147 struct diff_filepair *p = q->queue[i];
1148 diff_debug_filepair(p, i);
1149 }
1150}
1151#endif
1152
Junio C Hamanobceafe72005-05-24 01:14:031153static void diff_resolve_rename_copy(void)
1154{
Junio C Hamano25d5ea42005-05-24 08:10:481155 int i, j;
1156 struct diff_filepair *p, *pp;
Junio C Hamanobceafe72005-05-24 01:14:031157 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano25d5ea42005-05-24 08:10:481158
Junio C Hamano25d5ea42005-05-24 08:10:481159 diff_debug_queue("resolve-rename-copy", q);
1160
Junio C Hamanobceafe72005-05-24 01:14:031161 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 08:10:481162 p = q->queue[i];
Junio C Hamano96716a12005-05-25 22:07:081163 p->status = 0; /* undecided */
Junio C Hamanobceafe72005-05-24 01:14:031164 if (DIFF_PAIR_UNMERGED(p))
Junio C Hamanoe7baa4f2005-07-25 20:05:441165 p->status = DIFF_STATUS_UNMERGED;
Junio C Hamano15d061b2005-05-27 22:55:551166 else if (!DIFF_FILE_VALID(p->one))
Junio C Hamanoe7baa4f2005-07-25 20:05:441167 p->status = DIFF_STATUS_ADDED;
Junio C Hamano2cd68882005-05-30 07:08:071168 else if (!DIFF_FILE_VALID(p->two))
Junio C Hamanoe7baa4f2005-07-25 20:05:441169 p->status = DIFF_STATUS_DELETED;
Junio C Hamano96716a12005-05-25 22:07:081170 else if (DIFF_PAIR_TYPE_CHANGED(p))
Junio C Hamanoe7baa4f2005-07-25 20:05:441171 p->status = DIFF_STATUS_TYPE_CHANGED;
Junio C Hamano96716a12005-05-25 22:07:081172
1173 /* from this point on, we are dealing with a pair
1174 * whose both sides are valid and of the same type, i.e.
1175 * either in-place edit or rename/copy edit.
1176 */
Junio C Hamano01c4e702005-05-29 23:56:481177 else if (DIFF_PAIR_RENAME(p)) {
Junio C Hamano15d061b2005-05-27 22:55:551178 if (p->source_stays) {
Junio C Hamanoe7baa4f2005-07-25 20:05:441179 p->status = DIFF_STATUS_COPIED;
Junio C Hamano15d061b2005-05-27 22:55:551180 continue;
1181 }
1182 /* See if there is some other filepair that
1183 * copies from the same source as us. If so
Junio C Hamano6bac10d2005-09-10 19:42:321184 * we are a copy. Otherwise we are either a
1185 * copy if the path stays, or a rename if it
1186 * does not, but we already handled "stays" case.
Junio C Hamano25d5ea42005-05-24 08:10:481187 */
Junio C Hamano15d061b2005-05-27 22:55:551188 for (j = i + 1; j < q->nr; j++) {
Junio C Hamano25d5ea42005-05-24 08:10:481189 pp = q->queue[j];
1190 if (strcmp(pp->one->path, p->one->path))
Junio C Hamano15d061b2005-05-27 22:55:551191 continue; /* not us */
Junio C Hamano01c4e702005-05-29 23:56:481192 if (!DIFF_PAIR_RENAME(pp))
Junio C Hamano15d061b2005-05-27 22:55:551193 continue; /* not a rename/copy */
1194 /* pp is a rename/copy from the same source */
Junio C Hamanoe7baa4f2005-07-25 20:05:441195 p->status = DIFF_STATUS_COPIED;
Junio C Hamano15d061b2005-05-27 22:55:551196 break;
Junio C Hamano25d5ea42005-05-24 08:10:481197 }
1198 if (!p->status)
Junio C Hamanoe7baa4f2005-07-25 20:05:441199 p->status = DIFF_STATUS_RENAMED;
Junio C Hamanobceafe72005-05-24 01:14:031200 }
Junio C Hamano9fdade02005-05-25 23:00:041201 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1202 p->one->mode != p->two->mode)
Junio C Hamanoe7baa4f2005-07-25 20:05:441203 p->status = DIFF_STATUS_MODIFIED;
Junio C Hamano67574c42005-06-01 18:38:071204 else {
1205 /* This is a "no-change" entry and should not
1206 * happen anymore, but prepare for broken callers.
Junio C Hamano15d061b2005-05-27 22:55:551207 */
Junio C Hamano67574c42005-06-01 18:38:071208 error("feeding unmodified %s to diffcore",
1209 p->one->path);
Junio C Hamanoe7baa4f2005-07-25 20:05:441210 p->status = DIFF_STATUS_UNKNOWN;
Junio C Hamano67574c42005-06-01 18:38:071211 }
Junio C Hamanobceafe72005-05-24 01:14:031212 }
Junio C Hamano25d5ea42005-05-24 08:10:481213 diff_debug_queue("resolve-rename-copy done", q);
Junio C Hamanobceafe72005-05-24 01:14:031214}
1215
Junio C Hamano6b5ee132005-09-21 07:00:471216void diff_flush(struct diff_options *options)
Junio C Hamano38c6f782005-05-22 02:40:361217{
1218 struct diff_queue_struct *q = &diff_queued_diff;
1219 int i;
Junio C Hamanobceafe72005-05-24 01:14:031220 int inter_name_termination = '\t';
Junio C Hamano6b5ee132005-09-21 07:00:471221 int diff_output_format = options->output_format;
1222 int line_termination = options->line_termination;
Junio C Hamano38c6f782005-05-22 02:40:361223
Linus Torvaldse68b6f12005-07-15 00:59:171224 if (!line_termination)
1225 inter_name_termination = 0;
Junio C Hamanobceafe72005-05-24 01:14:031226
Junio C Hamanof7c15122005-05-23 04:26:091227 for (i = 0; i < q->nr; i++) {
Junio C Hamanof7c15122005-05-23 04:26:091228 struct diff_filepair *p = q->queue[i];
Junio C Hamano6b5ee132005-09-21 07:00:471229 if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
Junio C Hamanoe7baa4f2005-07-25 20:05:441230 (p->status == DIFF_STATUS_UNKNOWN))
Junio C Hamano96716a12005-05-25 22:07:081231 continue;
Junio C Hamanobceafe72005-05-24 01:14:031232 if (p->status == 0)
Junio C Hamano96716a12005-05-25 22:07:081233 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6b5ee132005-09-21 07:00:471234 switch (diff_output_format) {
Junio C Hamanobceafe72005-05-24 01:14:031235 case DIFF_FORMAT_PATCH:
Junio C Hamano80b1e512005-11-15 01:53:221236 diff_flush_patch(p, options);
Junio C Hamanobceafe72005-05-24 01:14:031237 break;
Linus Torvaldse68b6f12005-07-15 00:59:171238 case DIFF_FORMAT_RAW:
Junio C Hamano946f5f72005-09-21 07:20:061239 case DIFF_FORMAT_NAME_STATUS:
Junio C Hamanobceafe72005-05-24 01:14:031240 diff_flush_raw(p, line_termination,
Junio C Hamano946f5f72005-09-21 07:20:061241 inter_name_termination,
Junio C Hamano47dd0d52005-12-14 01:21:411242 options);
Junio C Hamanobceafe72005-05-24 01:14:031243 break;
Junio C Hamano52f28522005-07-13 19:45:511244 case DIFF_FORMAT_NAME:
Junio C Hamanocf9dfc62005-10-15 04:56:461245 diff_flush_name(p,
1246 inter_name_termination,
1247 line_termination);
Junio C Hamano52f28522005-07-13 19:45:511248 break;
Junio C Hamanobceafe72005-05-24 01:14:031249 }
Junio C Hamano226406f2005-05-27 22:50:301250 diff_free_filepair(q->queue[i]);
Yasushi SHOJI90a734d2005-08-21 07:14:161251 }
Junio C Hamano427dcb42005-05-21 09:39:091252 free(q->queue);
1253 q->queue = NULL;
1254 q->nr = q->alloc = 0;
1255}
1256
Junio C Hamanof2ce9fd2005-06-12 03:57:131257static void diffcore_apply_filter(const char *filter)
1258{
1259 int i;
1260 struct diff_queue_struct *q = &diff_queued_diff;
1261 struct diff_queue_struct outq;
1262 outq.queue = NULL;
1263 outq.nr = outq.alloc = 0;
1264
1265 if (!filter)
1266 return;
1267
Junio C Hamanoe7baa4f2005-07-25 20:05:441268 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
Junio C Hamanof2ce9fd2005-06-12 03:57:131269 int found;
1270 for (i = found = 0; !found && i < q->nr; i++) {
1271 struct diff_filepair *p = q->queue[i];
Junio C Hamanoe7baa4f2005-07-25 20:05:441272 if (((p->status == DIFF_STATUS_MODIFIED) &&
1273 ((p->score &&
1274 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1275 (!p->score &&
1276 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1277 ((p->status != DIFF_STATUS_MODIFIED) &&
1278 strchr(filter, p->status)))
Junio C Hamanof2ce9fd2005-06-12 03:57:131279 found++;
1280 }
1281 if (found)
1282 return;
1283
1284 /* otherwise we will clear the whole queue
1285 * by copying the empty outq at the end of this
1286 * function, but first clear the current entries
1287 * in the queue.
1288 */
1289 for (i = 0; i < q->nr; i++)
1290 diff_free_filepair(q->queue[i]);
1291 }
1292 else {
1293 /* Only the matching ones */
1294 for (i = 0; i < q->nr; i++) {
1295 struct diff_filepair *p = q->queue[i];
Junio C Hamanoe7baa4f2005-07-25 20:05:441296
1297 if (((p->status == DIFF_STATUS_MODIFIED) &&
1298 ((p->score &&
1299 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1300 (!p->score &&
1301 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1302 ((p->status != DIFF_STATUS_MODIFIED) &&
1303 strchr(filter, p->status)))
Junio C Hamanof2ce9fd2005-06-12 03:57:131304 diff_q(&outq, p);
1305 else
1306 diff_free_filepair(p);
1307 }
1308 }
1309 free(q->queue);
1310 *q = outq;
1311}
1312
Junio C Hamano6b5ee132005-09-21 07:00:471313void diffcore_std(struct diff_options *options)
Junio C Hamanobefe8632005-05-29 23:56:131314{
Junio C Hamano6b5ee132005-09-21 07:00:471315 if (options->paths && options->paths[0])
1316 diffcore_pathspec(options->paths);
1317 if (options->break_opt != -1)
1318 diffcore_break(options->break_opt);
1319 if (options->detect_rename)
Junio C Hamano8082d8d2005-09-21 07:18:271320 diffcore_rename(options);
Junio C Hamano6b5ee132005-09-21 07:00:471321 if (options->break_opt != -1)
Junio C Hamanoeeaa4602005-06-03 08:40:281322 diffcore_merge_broken();
Junio C Hamano6b5ee132005-09-21 07:00:471323 if (options->pickaxe)
1324 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1325 if (options->orderfile)
1326 diffcore_order(options->orderfile);
Junio C Hamanof2ce9fd2005-06-12 03:57:131327 diff_resolve_rename_copy();
Junio C Hamano6b5ee132005-09-21 07:00:471328 diffcore_apply_filter(options->filter);
Junio C Hamanof2ce9fd2005-06-12 03:57:131329}
1330
1331
Junio C Hamano6b5ee132005-09-21 07:00:471332void diffcore_std_no_resolve(struct diff_options *options)
Junio C Hamanof2ce9fd2005-06-12 03:57:131333{
Junio C Hamano6b5ee132005-09-21 07:00:471334 if (options->pickaxe)
1335 diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1336 if (options->orderfile)
1337 diffcore_order(options->orderfile);
1338 diffcore_apply_filter(options->filter);
Junio C Hamanobefe8632005-05-29 23:56:131339}
1340
Junio C Hamano6b5ee132005-09-21 07:00:471341void diff_addremove(struct diff_options *options,
1342 int addremove, unsigned mode,
Junio C Hamano77eb2722005-04-27 16:21:001343 const unsigned char *sha1,
1344 const char *base, const char *path)
Junio C Hamanobe3cfa82005-04-26 16:25:051345{
Junio C Hamano77eb2722005-04-27 16:21:001346 char concatpath[PATH_MAX];
Junio C Hamano427dcb42005-05-21 09:39:091347 struct diff_filespec *one, *two;
Junio C Hamanobe3cfa82005-04-26 16:25:051348
Junio C Hamano427dcb42005-05-21 09:39:091349 /* This may look odd, but it is a preparation for
1350 * feeding "there are unchanged files which should
1351 * not produce diffs, but when you are doing copy
1352 * detection you would need them, so here they are"
1353 * entries to the diff-core. They will be prefixed
1354 * with something like '=' or '*' (I haven't decided
1355 * which but should not make any difference).
Junio C Hamanof7c15122005-05-23 04:26:091356 * Feeding the same new and old to diff_change()
Junio C Hamanobceafe72005-05-24 01:14:031357 * also has the same effect.
1358 * Before the final output happens, they are pruned after
1359 * merged into rename/copy pairs as appropriate.
Junio C Hamano427dcb42005-05-21 09:39:091360 */
Junio C Hamano6b5ee132005-09-21 07:00:471361 if (options->reverse_diff)
Junio C Hamano427dcb42005-05-21 09:39:091362 addremove = (addremove == '+' ? '-' :
1363 addremove == '-' ? '+' : addremove);
Junio C Hamano7ca45252005-05-20 16:54:071364
Junio C Hamano427dcb42005-05-21 09:39:091365 if (!path) path = "";
1366 sprintf(concatpath, "%s%s", base, path);
1367 one = alloc_filespec(concatpath);
1368 two = alloc_filespec(concatpath);
Junio C Hamano57fe64a2005-05-20 02:00:361369
Junio C Hamano427dcb42005-05-21 09:39:091370 if (addremove != '+')
1371 fill_filespec(one, sha1, mode);
1372 if (addremove != '-')
1373 fill_filespec(two, sha1, mode);
Junio C Hamanobe3cfa82005-04-26 16:25:051374
Junio C Hamano38c6f782005-05-22 02:40:361375 diff_queue(&diff_queued_diff, one, two);
Junio C Hamanobe3cfa82005-04-26 16:25:051376}
1377
Junio C Hamano6b5ee132005-09-21 07:00:471378void diff_change(struct diff_options *options,
1379 unsigned old_mode, unsigned new_mode,
Junio C Hamano77eb2722005-04-27 16:21:001380 const unsigned char *old_sha1,
1381 const unsigned char *new_sha1,
Junio C Hamano81e50ea2005-05-22 02:42:181382 const char *base, const char *path)
1383{
Junio C Hamano77eb2722005-04-27 16:21:001384 char concatpath[PATH_MAX];
Junio C Hamano427dcb42005-05-21 09:39:091385 struct diff_filespec *one, *two;
Junio C Hamanobe3cfa82005-04-26 16:25:051386
Junio C Hamano6b5ee132005-09-21 07:00:471387 if (options->reverse_diff) {
Junio C Hamano7ca45252005-05-20 16:54:071388 unsigned tmp;
1389 const unsigned char *tmp_c;
1390 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1391 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1392 }
Junio C Hamano427dcb42005-05-21 09:39:091393 if (!path) path = "";
1394 sprintf(concatpath, "%s%s", base, path);
1395 one = alloc_filespec(concatpath);
1396 two = alloc_filespec(concatpath);
1397 fill_filespec(one, old_sha1, old_mode);
1398 fill_filespec(two, new_sha1, new_mode);
Junio C Hamano7ca45252005-05-20 16:54:071399
Junio C Hamano38c6f782005-05-22 02:40:361400 diff_queue(&diff_queued_diff, one, two);
Junio C Hamano77eb2722005-04-27 16:21:001401}
Junio C Hamanobe3cfa82005-04-26 16:25:051402
Junio C Hamano6b5ee132005-09-21 07:00:471403void diff_unmerge(struct diff_options *options,
1404 const char *path)
Junio C Hamano77eb2722005-04-27 16:21:001405{
Junio C Hamano6b14d7f2005-05-22 17:04:371406 struct diff_filespec *one, *two;
1407 one = alloc_filespec(path);
1408 two = alloc_filespec(path);
1409 diff_queue(&diff_queued_diff, one, two);
Junio C Hamano86436c22005-04-26 01:22:471410}