blob: d8d20c2fcb141124bf4c798eb054e5c1be451f68 [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 Hamano5c975582005-05-19 10:32:3513static unsigned char null_sha1[20] = { 0, };
Junio C Hamano427dcb42005-05-21 09:39:0914
Junio C Hamano427dcb42005-05-21 09:39:0915static int reverse_diff;
Junio C Hamanof0c6b2a2005-05-27 22:56:3816static int use_size_cache;
Junio C Hamano86436c22005-04-26 01:22:4717
Junio C Hamanobe3cfa82005-04-26 16:25:0518static const char *external_diff(void)
Junio C Hamano86436c22005-04-26 01:22:4719{
Junio C Hamanod19938a2005-05-10 00:57:5620 static const char *external_diff_cmd = NULL;
Junio C Hamanobe3cfa82005-04-26 16:25:0521 static int done_preparing = 0;
Jason Riedyc7c81b32005-08-23 20:34:0722 const char *env_diff_opts;
Junio C Hamanobe3cfa82005-04-26 16:25:0523
24 if (done_preparing)
25 return external_diff_cmd;
26
Junio C Hamano86436c22005-04-26 01:22:4727 /*
28 * Default values above are meant to match the
29 * Linux kernel development style. Examples of
30 * alternative styles you can specify via environment
31 * variables are:
32 *
Junio C Hamano86436c22005-04-26 01:22:4733 * GIT_DIFF_OPTS="-c";
34 */
Junio C Hamanoa9ab5862005-09-09 21:48:5435 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
Junio C Hamanobe3cfa82005-04-26 16:25:0536
37 /* In case external diff fails... */
Junio C Hamanoa9ab5862005-09-09 21:48:5438 env_diff_opts = getenv("GIT_DIFF_OPTS");
Jason Riedyc7c81b32005-08-23 20:34:0739 if (env_diff_opts) diff_opts = env_diff_opts;
Junio C Hamanobe3cfa82005-04-26 16:25:0540
41 done_preparing = 1;
42 return external_diff_cmd;
Junio C Hamano86436c22005-04-26 01:22:4743}
44
Holger Eitzenberger64f8a632005-08-04 20:49:4945#define TEMPFILE_PATH_LEN 50
46
Junio C Hamanobe3cfa82005-04-26 16:25:0547static struct diff_tempfile {
Junio C Hamano427dcb42005-05-21 09:39:0948 const char *name; /* filename external diff should read from */
Junio C Hamanobe3cfa82005-04-26 16:25:0549 char hex[41];
50 char mode[10];
Holger Eitzenberger64f8a632005-08-04 20:49:4951 char tmp_path[TEMPFILE_PATH_LEN];
Junio C Hamanobe3cfa82005-04-26 16:25:0552} diff_temp[2];
Junio C Hamano86436c22005-04-26 01:22:4753
Junio C Hamano366175e2005-06-19 20:17:5054static int count_lines(const char *filename)
55{
56 FILE *in;
57 int count, ch, completely_empty = 1, nl_just_seen = 0;
58 in = fopen(filename, "r");
59 count = 0;
60 while ((ch = fgetc(in)) != EOF)
61 if (ch == '\n') {
62 count++;
63 nl_just_seen = 1;
64 completely_empty = 0;
65 }
66 else {
67 nl_just_seen = 0;
68 completely_empty = 0;
69 }
70 fclose(in);
71 if (completely_empty)
72 return 0;
73 if (!nl_just_seen)
74 count++; /* no trailing newline */
75 return count;
76}
77
78static void print_line_count(int count)
79{
80 switch (count) {
81 case 0:
82 printf("0,0");
83 break;
84 case 1:
85 printf("1");
86 break;
87 default:
88 printf("1,%d", count);
89 break;
90 }
91}
92
93static void copy_file(int prefix, const char *filename)
94{
95 FILE *in;
96 int ch, nl_just_seen = 1;
97 in = fopen(filename, "r");
98 while ((ch = fgetc(in)) != EOF) {
99 if (nl_just_seen)
100 putchar(prefix);
101 putchar(ch);
102 if (ch == '\n')
103 nl_just_seen = 1;
104 else
105 nl_just_seen = 0;
106 }
107 fclose(in);
108 if (!nl_just_seen)
109 printf("\n\\ No newline at end of file\n");
110}
111
112static void emit_rewrite_diff(const char *name_a,
113 const char *name_b,
114 struct diff_tempfile *temp)
115{
116 /* Use temp[i].name as input, name_a and name_b as labels */
117 int lc_a, lc_b;
118 lc_a = count_lines(temp[0].name);
119 lc_b = count_lines(temp[1].name);
120 printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
121 print_line_count(lc_a);
122 printf(" +");
123 print_line_count(lc_b);
124 printf(" @@\n");
125 if (lc_a)
126 copy_file('-', temp[0].name);
127 if (lc_b)
128 copy_file('+', temp[1].name);
129}
130
Junio C Hamano915838c2005-05-18 06:29:49131static void builtin_diff(const char *name_a,
132 const char *name_b,
Junio C Hamano57fe64a2005-05-20 02:00:36133 struct diff_tempfile *temp,
Junio C Hamano366175e2005-06-19 20:17:50134 const char *xfrm_msg,
135 int complete_rewrite)
Junio C Hamanobe3cfa82005-04-26 16:25:05136{
Thomas Glanzmann9669e172005-05-19 13:23:18137 int i, next_at, cmd_size;
Junio C Hamano79db12e2005-08-10 04:25:46138 const char *const diff_cmd = "diff -L%s%s -L%s%s";
139 const char *const diff_arg = "%s %s||:"; /* "||:" is to return 0 */
Junio C Hamano2f978132005-04-28 15:04:39140 const char *input_name_sq[2];
141 const char *path0[2];
142 const char *path1[2];
Junio C Hamano915838c2005-05-18 06:29:49143 const char *name_sq[2];
Junio C Hamano2f978132005-04-28 15:04:39144 char *cmd;
Junio C Hamano915838c2005-05-18 06:29:49145
Junio C Hamano6fb737b2005-07-08 06:58:32146 name_sq[0] = sq_quote(name_a);
147 name_sq[1] = sq_quote(name_b);
Junio C Hamano915838c2005-05-18 06:29:49148
Junio C Hamanoc9833702005-05-01 16:33:12149 /* diff_cmd and diff_arg have 6 %s in total which makes
150 * the sum of these strings 12 bytes larger than required.
Junio C Hamanobe3cfa82005-04-26 16:25:05151 * we use 2 spaces around diff-opts, and we need to count
Junio C Hamanoc9833702005-05-01 16:33:12152 * terminating NUL, so we subtract 9 here.
Junio C Hamanobe3cfa82005-04-26 16:25:05153 */
Thomas Glanzmann9669e172005-05-19 13:23:18154 cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
Junio C Hamanoc9833702005-05-01 16:33:12155 strlen(diff_arg) - 9);
Junio C Hamano2f978132005-04-28 15:04:39156 for (i = 0; i < 2; i++) {
Junio C Hamano6fb737b2005-07-08 06:58:32157 input_name_sq[i] = sq_quote(temp[i].name);
Junio C Hamano2f978132005-04-28 15:04:39158 if (!strcmp(temp[i].name, "/dev/null")) {
159 path0[i] = "/dev/null";
160 path1[i] = "";
Junio C Hamano2f978132005-04-28 15:04:39161 } else {
Linus Torvalds0980d9b2005-05-02 04:53:36162 path0[i] = i ? "b/" : "a/";
Junio C Hamano915838c2005-05-18 06:29:49163 path1[i] = name_sq[i];
Junio C Hamano2f978132005-04-28 15:04:39164 }
165 cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
Junio C Hamanoc9833702005-05-01 16:33:12166 strlen(input_name_sq[i]));
Junio C Hamano2f978132005-04-28 15:04:39167 }
Junio C Hamanobe3cfa82005-04-26 16:25:05168
Junio C Hamano2f978132005-04-28 15:04:39169 cmd = xmalloc(cmd_size);
170
171 next_at = 0;
Junio C Hamanobe3cfa82005-04-26 16:25:05172 next_at += snprintf(cmd+next_at, cmd_size-next_at,
Junio C Hamano2f978132005-04-28 15:04:39173 diff_cmd,
Junio C Hamanoc9833702005-05-01 16:33:12174 path0[0], path1[0], path0[1], path1[1]);
Junio C Hamanobe3cfa82005-04-26 16:25:05175 next_at += snprintf(cmd+next_at, cmd_size-next_at,
176 " %s ", diff_opts);
177 next_at += snprintf(cmd+next_at, cmd_size-next_at,
Junio C Hamano2f978132005-04-28 15:04:39178 diff_arg, input_name_sq[0], input_name_sq[1]);
179
Junio C Hamano915838c2005-05-18 06:29:49180 printf("diff --git a/%s b/%s\n", name_a, name_b);
Junio C Hamano70aadac2005-05-30 23:40:16181 if (!path1[0][0]) {
Junio C Hamanob58f23b2005-05-18 16:10:47182 printf("new file mode %s\n", temp[1].mode);
Junio C Hamano70aadac2005-05-30 23:40:16183 if (xfrm_msg && xfrm_msg[0])
184 puts(xfrm_msg);
185 }
186 else if (!path1[1][0]) {
Junio C Hamanob58f23b2005-05-18 16:10:47187 printf("deleted file mode %s\n", temp[0].mode);
Junio C Hamano70aadac2005-05-30 23:40:16188 if (xfrm_msg && xfrm_msg[0])
189 puts(xfrm_msg);
190 }
Junio C Hamano273b9832005-05-14 01:40:14191 else {
Junio C Hamanob58f23b2005-05-18 16:10:47192 if (strcmp(temp[0].mode, temp[1].mode)) {
193 printf("old mode %s\n", temp[0].mode);
194 printf("new mode %s\n", temp[1].mode);
195 }
Junio C Hamano427dcb42005-05-21 09:39:09196 if (xfrm_msg && xfrm_msg[0])
Junio C Hamano09d9d1a2005-05-27 22:54:06197 puts(xfrm_msg);
Junio C Hamano273b9832005-05-14 01:40:14198 if (strncmp(temp[0].mode, temp[1].mode, 3))
199 /* we do not run diff between different kind
200 * of objects.
201 */
Junio C Hamanob28858b2005-05-05 23:10:21202 exit(0);
Junio C Hamano366175e2005-06-19 20:17:50203 if (complete_rewrite) {
204 fflush(NULL);
205 emit_rewrite_diff(name_a, name_b, temp);
206 exit(0);
207 }
Junio C Hamanob28858b2005-05-05 23:10:21208 }
Junio C Hamanoc9833702005-05-01 16:33:12209 fflush(NULL);
Junio C Hamanobe3cfa82005-04-26 16:25:05210 execlp("/bin/sh","sh", "-c", cmd, NULL);
Junio C Hamano86436c22005-04-26 01:22:47211}
212
Junio C Hamano427dcb42005-05-21 09:39:09213struct diff_filespec *alloc_filespec(const char *path)
214{
215 int namelen = strlen(path);
216 struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
Linus Torvalds705a7142005-09-14 20:41:24217
218 memset(spec, 0, sizeof(*spec));
Junio C Hamano427dcb42005-05-21 09:39:09219 spec->path = (char *)(spec + 1);
Linus Torvalds705a7142005-09-14 20:41:24220 memcpy(spec->path, path, namelen+1);
Junio C Hamano427dcb42005-05-21 09:39:09221 return spec;
222}
223
224void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
225 unsigned short mode)
226{
Junio C Hamano4130b992005-05-26 09:24:30227 if (mode) {
228 spec->mode = DIFF_FILE_CANON_MODE(mode);
Junio C Hamano81e50ea2005-05-22 02:42:18229 memcpy(spec->sha1, sha1, 20);
230 spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
231 }
Junio C Hamano427dcb42005-05-21 09:39:09232}
233
Junio C Hamanob46f0b62005-05-04 08:45:24234/*
235 * Given a name and sha1 pair, if the dircache tells us the file in
236 * the work tree has that object contents, return true, so that
237 * prepare_temp_file() does not have to inflate and extract.
238 */
239static int work_tree_matches(const char *name, const unsigned char *sha1)
240{
241 struct cache_entry *ce;
242 struct stat st;
243 int pos, len;
Junio C Hamano5c975582005-05-19 10:32:35244
Junio C Hamanob46f0b62005-05-04 08:45:24245 /* We do not read the cache ourselves here, because the
246 * benchmark with my previous version that always reads cache
247 * shows that it makes things worse for diff-tree comparing
248 * two linux-2.6 kernel trees in an already checked out work
Junio C Hamano915838c2005-05-18 06:29:49249 * tree. This is because most diff-tree comparisons deal with
Junio C Hamanob46f0b62005-05-04 08:45:24250 * only a small number of files, while reading the cache is
251 * expensive for a large project, and its cost outweighs the
252 * savings we get by not inflating the object to a temporary
253 * file. Practically, this code only helps when we are used
254 * by diff-cache --cached, which does read the cache before
255 * calling us.
Junio C Hamano57fe64a2005-05-20 02:00:36256 */
Junio C Hamanob46f0b62005-05-04 08:45:24257 if (!active_cache)
258 return 0;
259
260 len = strlen(name);
261 pos = cache_name_pos(name, len);
262 if (pos < 0)
263 return 0;
264 ce = active_cache[pos];
Junio C Hamanob28858b2005-05-05 23:10:21265 if ((lstat(name, &st) < 0) ||
Junio C Hamano427dcb42005-05-21 09:39:09266 !S_ISREG(st.st_mode) || /* careful! */
Brad Roberts5d728c82005-05-15 02:04:25267 ce_match_stat(ce, &st) ||
Junio C Hamanob46f0b62005-05-04 08:45:24268 memcmp(sha1, ce->sha1, 20))
269 return 0;
Junio C Hamano427dcb42005-05-21 09:39:09270 /* we return 1 only when we can stat, it is a regular file,
271 * stat information matches, and sha1 recorded in the cache
272 * matches. I.e. we know the file in the work tree really is
273 * the same as the <name, sha1> pair.
274 */
Junio C Hamanob46f0b62005-05-04 08:45:24275 return 1;
276}
277
Junio C Hamanof0c6b2a2005-05-27 22:56:38278static struct sha1_size_cache {
279 unsigned char sha1[20];
280 unsigned long size;
281} **sha1_size_cache;
282static int sha1_size_cache_nr, sha1_size_cache_alloc;
283
284static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
Junio C Hamano0601e132005-06-04 06:02:23285 int find_only,
Junio C Hamanof0c6b2a2005-05-27 22:56:38286 unsigned long size)
287{
288 int first, last;
289 struct sha1_size_cache *e;
290
291 first = 0;
292 last = sha1_size_cache_nr;
293 while (last > first) {
Junio C Hamano0601e132005-06-04 06:02:23294 int cmp, next = (last + first) >> 1;
Junio C Hamanof0c6b2a2005-05-27 22:56:38295 e = sha1_size_cache[next];
Junio C Hamano0601e132005-06-04 06:02:23296 cmp = memcmp(e->sha1, sha1, 20);
Junio C Hamanof0c6b2a2005-05-27 22:56:38297 if (!cmp)
298 return e;
299 if (cmp < 0) {
300 last = next;
301 continue;
302 }
303 first = next+1;
304 }
305 /* not found */
Junio C Hamano0601e132005-06-04 06:02:23306 if (find_only)
Junio C Hamanof0c6b2a2005-05-27 22:56:38307 return NULL;
308 /* insert to make it at "first" */
309 if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
310 sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
311 sha1_size_cache = xrealloc(sha1_size_cache,
312 sha1_size_cache_alloc *
313 sizeof(*sha1_size_cache));
314 }
315 sha1_size_cache_nr++;
316 if (first < sha1_size_cache_nr)
317 memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
318 (sha1_size_cache_nr - first - 1) *
319 sizeof(*sha1_size_cache));
320 e = xmalloc(sizeof(struct sha1_size_cache));
321 sha1_size_cache[first] = e;
322 memcpy(e->sha1, sha1, 20);
323 e->size = size;
324 return e;
325}
326
Junio C Hamano427dcb42005-05-21 09:39:09327/*
328 * While doing rename detection and pickaxe operation, we may need to
329 * grab the data for the blob (or file) for our own in-core comparison.
330 * diff_filespec has data and size fields for this purpose.
331 */
Junio C Hamanof0c6b2a2005-05-27 22:56:38332int diff_populate_filespec(struct diff_filespec *s, int size_only)
Junio C Hamano427dcb42005-05-21 09:39:09333{
334 int err = 0;
Junio C Hamano81e50ea2005-05-22 02:42:18335 if (!DIFF_FILE_VALID(s))
Junio C Hamano427dcb42005-05-21 09:39:09336 die("internal error: asking to populate invalid file.");
337 if (S_ISDIR(s->mode))
338 return -1;
339
Junio C Hamanof0c6b2a2005-05-27 22:56:38340 if (!use_size_cache)
341 size_only = 0;
342
Junio C Hamano427dcb42005-05-21 09:39:09343 if (s->data)
344 return err;
345 if (!s->sha1_valid ||
346 work_tree_matches(s->path, s->sha1)) {
347 struct stat st;
348 int fd;
349 if (lstat(s->path, &st) < 0) {
350 if (errno == ENOENT) {
351 err_empty:
352 err = -1;
353 empty:
354 s->data = "";
355 s->size = 0;
356 return err;
357 }
358 }
359 s->size = st.st_size;
360 if (!s->size)
361 goto empty;
Junio C Hamanof0c6b2a2005-05-27 22:56:38362 if (size_only)
363 return 0;
Junio C Hamano427dcb42005-05-21 09:39:09364 if (S_ISLNK(st.st_mode)) {
365 int ret;
366 s->data = xmalloc(s->size);
367 s->should_free = 1;
368 ret = readlink(s->path, s->data, s->size);
369 if (ret < 0) {
370 free(s->data);
371 goto err_empty;
372 }
373 return 0;
374 }
375 fd = open(s->path, O_RDONLY);
376 if (fd < 0)
377 goto err_empty;
378 s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano427dcb42005-05-21 09:39:09379 close(fd);
Pavel Roskine35f9822005-07-29 14:49:14380 if (s->data == MAP_FAILED)
381 goto err_empty;
382 s->should_munmap = 1;
Junio C Hamano427dcb42005-05-21 09:39:09383 }
384 else {
385 char type[20];
Junio C Hamanof0c6b2a2005-05-27 22:56:38386 struct sha1_size_cache *e;
387
388 if (size_only) {
Junio C Hamano0601e132005-06-04 06:02:23389 e = locate_size_cache(s->sha1, 1, 0);
Junio C Hamanof0c6b2a2005-05-27 22:56:38390 if (e) {
391 s->size = e->size;
392 return 0;
393 }
Junio C Hamano36e4d742005-06-27 10:34:06394 if (!sha1_object_info(s->sha1, type, &s->size))
Junio C Hamano0601e132005-06-04 06:02:23395 locate_size_cache(s->sha1, 0, s->size);
Junio C Hamanof0c6b2a2005-05-27 22:56:38396 }
Junio C Hamano65c2e0c2005-06-02 22:20:54397 else {
398 s->data = read_sha1_file(s->sha1, type, &s->size);
399 s->should_free = 1;
400 }
Junio C Hamano427dcb42005-05-21 09:39:09401 }
402 return 0;
403}
404
Junio C Hamano19397b42005-09-14 21:06:50405void diff_free_filespec_data(struct diff_filespec *s)
Junio C Hamano427dcb42005-05-21 09:39:09406{
407 if (s->should_free)
408 free(s->data);
409 else if (s->should_munmap)
410 munmap(s->data, s->size);
Junio C Hamano19397b42005-09-14 21:06:50411 s->should_free = s->should_munmap = 0;
412 s->data = NULL;
Junio C Hamano427dcb42005-05-21 09:39:09413}
414
Junio C Hamanob28858b2005-05-05 23:10:21415static void prep_temp_blob(struct diff_tempfile *temp,
416 void *blob,
417 unsigned long size,
418 unsigned char *sha1,
419 int mode)
420{
421 int fd;
422
Holger Eitzenberger64f8a632005-08-04 20:49:49423 fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
Junio C Hamanob28858b2005-05-05 23:10:21424 if (fd < 0)
425 die("unable to create temp-file");
426 if (write(fd, blob, size) != size)
427 die("unable to write temp-file");
428 close(fd);
429 temp->name = temp->tmp_path;
430 strcpy(temp->hex, sha1_to_hex(sha1));
431 temp->hex[40] = 0;
432 sprintf(temp->mode, "%06o", mode);
433}
434
Junio C Hamanobe3cfa82005-04-26 16:25:05435static void prepare_temp_file(const char *name,
436 struct diff_tempfile *temp,
Junio C Hamano427dcb42005-05-21 09:39:09437 struct diff_filespec *one)
Junio C Hamano86436c22005-04-26 01:22:47438{
Junio C Hamano81e50ea2005-05-22 02:42:18439 if (!DIFF_FILE_VALID(one)) {
Junio C Hamanobe3cfa82005-04-26 16:25:05440 not_a_valid_file:
Junio C Hamano532149d2005-04-28 17:13:01441 /* A '-' entry produces this for file-2, and
442 * a '+' entry produces this for file-1.
443 */
Junio C Hamanobe3cfa82005-04-26 16:25:05444 temp->name = "/dev/null";
445 strcpy(temp->hex, ".");
446 strcpy(temp->mode, ".");
Junio C Hamano86436c22005-04-26 01:22:47447 return;
448 }
Junio C Hamanobe3cfa82005-04-26 16:25:05449
Junio C Hamano5c975582005-05-19 10:32:35450 if (!one->sha1_valid ||
Junio C Hamano427dcb42005-05-21 09:39:09451 work_tree_matches(name, one->sha1)) {
Junio C Hamanobe3cfa82005-04-26 16:25:05452 struct stat st;
Junio C Hamano427dcb42005-05-21 09:39:09453 if (lstat(name, &st) < 0) {
Junio C Hamanobe3cfa82005-04-26 16:25:05454 if (errno == ENOENT)
455 goto not_a_valid_file;
Junio C Hamano427dcb42005-05-21 09:39:09456 die("stat(%s): %s", name, strerror(errno));
Junio C Hamanobe3cfa82005-04-26 16:25:05457 }
Junio C Hamanob28858b2005-05-05 23:10:21458 if (S_ISLNK(st.st_mode)) {
459 int ret;
460 char *buf, buf_[1024];
461 buf = ((sizeof(buf_) < st.st_size) ?
462 xmalloc(st.st_size) : buf_);
463 ret = readlink(name, buf, st.st_size);
464 if (ret < 0)
465 die("readlink(%s)", name);
466 prep_temp_blob(temp, buf, st.st_size,
467 (one->sha1_valid ?
Junio C Hamano427dcb42005-05-21 09:39:09468 one->sha1 : null_sha1),
Junio C Hamanob28858b2005-05-05 23:10:21469 (one->sha1_valid ?
470 one->mode : S_IFLNK));
471 }
472 else {
Junio C Hamano427dcb42005-05-21 09:39:09473 /* we can borrow from the file in the work tree */
474 temp->name = name;
Junio C Hamanob28858b2005-05-05 23:10:21475 if (!one->sha1_valid)
476 strcpy(temp->hex, sha1_to_hex(null_sha1));
477 else
Junio C Hamano427dcb42005-05-21 09:39:09478 strcpy(temp->hex, sha1_to_hex(one->sha1));
Junio C Hamano9d429ff2005-05-30 07:07:39479 /* Even though we may sometimes borrow the
480 * contents from the work tree, we always want
481 * one->mode. mode is trustworthy even when
482 * !(one->sha1_valid), as long as
483 * DIFF_FILE_VALID(one).
484 */
485 sprintf(temp->mode, "%06o", one->mode);
Junio C Hamanob28858b2005-05-05 23:10:21486 }
487 return;
Junio C Hamanobe3cfa82005-04-26 16:25:05488 }
489 else {
Junio C Hamanof0c6b2a2005-05-27 22:56:38490 if (diff_populate_filespec(one, 0))
Junio C Hamano427dcb42005-05-21 09:39:09491 die("cannot read data blob for %s", one->path);
492 prep_temp_blob(temp, one->data, one->size,
493 one->sha1, one->mode);
Junio C Hamanobe3cfa82005-04-26 16:25:05494 }
495}
496
497static void remove_tempfile(void)
498{
499 int i;
500
501 for (i = 0; i < 2; i++)
502 if (diff_temp[i].name == diff_temp[i].tmp_path) {
503 unlink(diff_temp[i].name);
504 diff_temp[i].name = NULL;
505 }
506}
507
Junio C Hamano532149d2005-04-28 17:13:01508static void remove_tempfile_on_signal(int signo)
509{
510 remove_tempfile();
511}
512
Junio C Hamanobe3cfa82005-04-26 16:25:05513/* An external diff command takes:
514 *
515 * diff-cmd name infile1 infile1-sha1 infile1-mode \
Junio C Hamano5c975582005-05-19 10:32:35516 * infile2 infile2-sha1 infile2-mode [ rename-to ]
Junio C Hamanobe3cfa82005-04-26 16:25:05517 *
518 */
Junio C Hamano4130b992005-05-26 09:24:30519static void run_external_diff(const char *pgm,
520 const char *name,
Junio C Hamano5c975582005-05-19 10:32:35521 const char *other,
Junio C Hamano427dcb42005-05-21 09:39:09522 struct diff_filespec *one,
523 struct diff_filespec *two,
Junio C Hamano366175e2005-06-19 20:17:50524 const char *xfrm_msg,
525 int complete_rewrite)
Junio C Hamanobe3cfa82005-04-26 16:25:05526{
527 struct diff_tempfile *temp = diff_temp;
Junio C Hamano532149d2005-04-28 17:13:01528 pid_t pid;
529 int status;
Junio C Hamanobe3cfa82005-04-26 16:25:05530 static int atexit_asked = 0;
Jason Riedyc7c81b32005-08-23 20:34:07531 const char *othername;
Junio C Hamanobe3cfa82005-04-26 16:25:05532
Jason Riedyc7c81b32005-08-23 20:34:07533 othername = (other? other : name);
Junio C Hamano77eb2722005-04-27 16:21:00534 if (one && two) {
535 prepare_temp_file(name, &temp[0], one);
Jason Riedyc7c81b32005-08-23 20:34:07536 prepare_temp_file(othername, &temp[1], two);
Junio C Hamano77eb2722005-04-27 16:21:00537 if (! atexit_asked &&
538 (temp[0].name == temp[0].tmp_path ||
539 temp[1].name == temp[1].tmp_path)) {
540 atexit_asked = 1;
541 atexit(remove_tempfile);
542 }
Junio C Hamano532149d2005-04-28 17:13:01543 signal(SIGINT, remove_tempfile_on_signal);
Junio C Hamanobe3cfa82005-04-26 16:25:05544 }
545
546 fflush(NULL);
547 pid = fork();
548 if (pid < 0)
549 die("unable to fork");
550 if (!pid) {
Junio C Hamano5c975582005-05-19 10:32:35551 if (pgm) {
552 if (one && two) {
Junio C Hamano427dcb42005-05-21 09:39:09553 const char *exec_arg[10];
Junio C Hamano5c975582005-05-19 10:32:35554 const char **arg = &exec_arg[0];
555 *arg++ = pgm;
556 *arg++ = name;
557 *arg++ = temp[0].name;
558 *arg++ = temp[0].hex;
559 *arg++ = temp[0].mode;
560 *arg++ = temp[1].name;
561 *arg++ = temp[1].hex;
562 *arg++ = temp[1].mode;
Junio C Hamano427dcb42005-05-21 09:39:09563 if (other) {
Junio C Hamano5c975582005-05-19 10:32:35564 *arg++ = other;
Junio C Hamano427dcb42005-05-21 09:39:09565 *arg++ = xfrm_msg;
566 }
Linus Torvalds09d74b32005-05-22 21:33:43567 *arg = NULL;
Junio C Hamano5c975582005-05-19 10:32:35568 execvp(pgm, (char *const*) exec_arg);
569 }
Junio C Hamano77eb2722005-04-27 16:21:00570 else
571 execlp(pgm, pgm, name, NULL);
572 }
Junio C Hamanobe3cfa82005-04-26 16:25:05573 /*
574 * otherwise we use the built-in one.
575 */
Junio C Hamano77eb2722005-04-27 16:21:00576 if (one && two)
Jason Riedyc7c81b32005-08-23 20:34:07577 builtin_diff(name, othername, temp, xfrm_msg,
Junio C Hamano366175e2005-06-19 20:17:50578 complete_rewrite);
Junio C Hamano77eb2722005-04-27 16:21:00579 else
580 printf("* Unmerged path %s\n", name);
Junio C Hamanobe3cfa82005-04-26 16:25:05581 exit(0);
582 }
Junio C Hamano6fa28062005-05-04 08:38:06583 if (waitpid(pid, &status, 0) < 0 ||
584 !WIFEXITED(status) || WEXITSTATUS(status)) {
585 /* Earlier we did not check the exit status because
Junio C Hamano532149d2005-04-28 17:13:01586 * diff exits non-zero if files are different, and
Junio C Hamano6fa28062005-05-04 08:38:06587 * we are not interested in knowing that. It was a
588 * mistake which made it harder to quit a diff-*
589 * session that uses the git-apply-patch-script as
590 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
591 * should also exit non-zero only when it wants to
592 * abort the entire diff-* session.
Junio C Hamano532149d2005-04-28 17:13:01593 */
594 remove_tempfile();
Junio C Hamano6fa28062005-05-04 08:38:06595 fprintf(stderr, "external diff died, stopping at %s.\n", name);
596 exit(1);
Junio C Hamano532149d2005-04-28 17:13:01597 }
Junio C Hamanobe3cfa82005-04-26 16:25:05598 remove_tempfile();
599}
600
Junio C Hamano366175e2005-06-19 20:17:50601static void run_diff(struct diff_filepair *p)
Junio C Hamano4130b992005-05-26 09:24:30602{
603 const char *pgm = external_diff();
Junio C Hamano366175e2005-06-19 20:17:50604 char msg_[PATH_MAX*2+200], *xfrm_msg;
605 struct diff_filespec *one;
606 struct diff_filespec *two;
607 const char *name;
608 const char *other;
609 int complete_rewrite = 0;
610
611 if (DIFF_PAIR_UNMERGED(p)) {
612 /* unmerged */
613 run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL,
614 0);
615 return;
616 }
617
618 name = p->one->path;
619 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
620 one = p->one; two = p->two;
621 switch (p->status) {
Junio C Hamanoe7baa4f2005-07-25 20:05:44622 case DIFF_STATUS_COPIED:
Junio C Hamano366175e2005-06-19 20:17:50623 sprintf(msg_,
624 "similarity index %d%%\n"
625 "copy from %s\n"
626 "copy to %s",
627 (int)(0.5 + p->score * 100.0/MAX_SCORE),
628 name, other);
629 xfrm_msg = msg_;
630 break;
Junio C Hamanoe7baa4f2005-07-25 20:05:44631 case DIFF_STATUS_RENAMED:
Junio C Hamano366175e2005-06-19 20:17:50632 sprintf(msg_,
633 "similarity index %d%%\n"
634 "rename from %s\n"
635 "rename to %s",
636 (int)(0.5 + p->score * 100.0/MAX_SCORE),
637 name, other);
638 xfrm_msg = msg_;
639 break;
Junio C Hamanoe7baa4f2005-07-25 20:05:44640 case DIFF_STATUS_MODIFIED:
Junio C Hamano366175e2005-06-19 20:17:50641 if (p->score) {
642 sprintf(msg_,
643 "dissimilarity index %d%%",
644 (int)(0.5 + p->score * 100.0/MAX_SCORE));
645 xfrm_msg = msg_;
646 complete_rewrite = 1;
647 break;
648 }
649 /* fallthru */
650 default:
651 xfrm_msg = NULL;
652 }
653
Junio C Hamano4130b992005-05-26 09:24:30654 if (!pgm &&
655 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
656 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
657 /* a filepair that changes between file and symlink
658 * needs to be split into deletion and creation.
659 */
660 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamano366175e2005-06-19 20:17:50661 run_external_diff(NULL, name, other, one, null, xfrm_msg, 0);
Junio C Hamano4130b992005-05-26 09:24:30662 free(null);
663 null = alloc_filespec(one->path);
Junio C Hamano366175e2005-06-19 20:17:50664 run_external_diff(NULL, name, other, null, two, xfrm_msg, 0);
Junio C Hamano4130b992005-05-26 09:24:30665 free(null);
666 }
667 else
Junio C Hamano366175e2005-06-19 20:17:50668 run_external_diff(pgm, name, other, one, two, xfrm_msg,
669 complete_rewrite);
Junio C Hamano4130b992005-05-26 09:24:30670}
671
Junio C Hamano19feebc2005-05-27 22:54:37672void diff_setup(int flags)
Junio C Hamano5c975582005-05-19 10:32:35673{
Junio C Hamano19feebc2005-05-27 22:54:37674 if (flags & DIFF_SETUP_REVERSE)
675 reverse_diff = 1;
Junio C Hamanof0c6b2a2005-05-27 22:56:38676 if (flags & DIFF_SETUP_USE_CACHE) {
677 if (!active_cache)
678 /* read-cache does not die even when it fails
679 * so it is safe for us to do this here. Also
680 * it does not smudge active_cache or active_nr
681 * when it fails, so we do not have to worry about
682 * cleaning it up oufselves either.
683 */
684 read_cache();
685 }
686 if (flags & DIFF_SETUP_USE_SIZE_CACHE)
687 use_size_cache = 1;
688
Junio C Hamano57fe64a2005-05-20 02:00:36689}
690
Junio C Hamano0e3994f2005-06-03 08:37:54691static int parse_num(const char **cp_p)
692{
693 int num, scale, ch, cnt;
694 const char *cp = *cp_p;
695
696 cnt = num = 0;
697 scale = 1;
698 while ('0' <= (ch = *cp) && ch <= '9') {
699 if (cnt++ < 5) {
700 /* We simply ignore more than 5 digits precision. */
701 scale *= 10;
702 num = num * 10 + ch - '0';
703 }
Junio C Hamano5de36bf2005-08-30 04:17:21704 cp++;
Junio C Hamano0e3994f2005-06-03 08:37:54705 }
706 *cp_p = cp;
707
708 /* user says num divided by scale and we say internally that
709 * is MAX_SCORE * num / scale.
710 */
711 return (MAX_SCORE * num / scale);
712}
713
714int diff_scoreopt_parse(const char *opt)
715{
Junio C Hamanoeeaa4602005-06-03 08:40:28716 int opt1, opt2, cmd;
Junio C Hamano0e3994f2005-06-03 08:37:54717
718 if (*opt++ != '-')
719 return -1;
720 cmd = *opt++;
721 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
722 return -1; /* that is not a -M, -C nor -B option */
723
724 opt1 = parse_num(&opt);
Junio C Hamanoeeaa4602005-06-03 08:40:28725 if (cmd != 'B')
726 opt2 = 0;
727 else {
728 if (*opt == 0)
729 opt2 = 0;
730 else if (*opt != '/')
731 return -1; /* we expect -B80/99 or -B80 */
732 else {
733 opt++;
734 opt2 = parse_num(&opt);
735 }
736 }
Junio C Hamano0e3994f2005-06-03 08:37:54737 if (*opt != 0)
738 return -1;
Junio C Hamanoeeaa4602005-06-03 08:40:28739 return opt1 | (opt2 << 16);
Junio C Hamano0e3994f2005-06-03 08:37:54740}
741
Junio C Hamano38c6f782005-05-22 02:40:36742struct diff_queue_struct diff_queued_diff;
Junio C Hamano427dcb42005-05-21 09:39:09743
Junio C Hamano6b14d7f2005-05-22 17:04:37744void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
745{
746 if (queue->alloc <= queue->nr) {
747 queue->alloc = alloc_nr(queue->alloc);
748 queue->queue = xrealloc(queue->queue,
749 sizeof(dp) * queue->alloc);
750 }
751 queue->queue[queue->nr++] = dp;
752}
753
Junio C Hamano52e95782005-05-21 09:40:01754struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
Junio C Hamano6b14d7f2005-05-22 17:04:37755 struct diff_filespec *one,
756 struct diff_filespec *two)
Junio C Hamano427dcb42005-05-21 09:39:09757{
Junio C Hamano52e95782005-05-21 09:40:01758 struct diff_filepair *dp = xmalloc(sizeof(*dp));
Junio C Hamano427dcb42005-05-21 09:39:09759 dp->one = one;
760 dp->two = two;
Junio C Hamanof7c15122005-05-23 04:26:09761 dp->score = 0;
Junio C Hamano22101002005-06-12 03:55:20762 dp->status = 0;
Junio C Hamano15d061b2005-05-27 22:55:55763 dp->source_stays = 0;
Junio C Hamanof345b0a2005-05-30 07:08:37764 dp->broken_pair = 0;
Junio C Hamano5098baf2005-09-15 23:13:43765 if (queue)
766 diff_q(queue, dp);
Junio C Hamano427dcb42005-05-21 09:39:09767 return dp;
768}
769
Junio C Hamano226406f2005-05-27 22:50:30770void diff_free_filepair(struct diff_filepair *p)
771{
Junio C Hamano19397b42005-09-14 21:06:50772 diff_free_filespec_data(p->one);
773 diff_free_filespec_data(p->two);
Junio C Hamano5098baf2005-09-15 23:13:43774 free(p->one);
775 free(p->two);
Junio C Hamano226406f2005-05-27 22:50:30776 free(p);
777}
778
Junio C Hamanobceafe72005-05-24 01:14:03779static void diff_flush_raw(struct diff_filepair *p,
780 int line_termination,
781 int inter_name_termination)
Junio C Hamano427dcb42005-05-21 09:39:09782{
Junio C Hamanob6d8f302005-05-23 21:55:33783 int two_paths;
784 char status[10];
Junio C Hamano25d5ea42005-05-24 08:10:48785
786 if (line_termination) {
Junio C Hamano79db12e2005-08-10 04:25:46787 const char *const err =
788 "path %s cannot be expressed without -z";
Junio C Hamano25d5ea42005-05-24 08:10:48789 if (strchr(p->one->path, line_termination) ||
790 strchr(p->one->path, inter_name_termination))
791 die(err, p->one->path);
792 if (strchr(p->two->path, line_termination) ||
793 strchr(p->two->path, inter_name_termination))
794 die(err, p->two->path);
795 }
796
Junio C Hamano366175e2005-06-19 20:17:50797 if (p->score)
798 sprintf(status, "%c%03d", p->status,
799 (int)(0.5 + p->score * 100.0/MAX_SCORE));
800 else {
801 status[0] = p->status;
802 status[1] = 0;
803 }
Junio C Hamanob6d8f302005-05-23 21:55:33804 switch (p->status) {
Junio C Hamanoe7baa4f2005-07-25 20:05:44805 case DIFF_STATUS_COPIED:
806 case DIFF_STATUS_RENAMED:
Junio C Hamanob6d8f302005-05-23 21:55:33807 two_paths = 1;
Junio C Hamanob6d8f302005-05-23 21:55:33808 break;
Junio C Hamanoe7baa4f2005-07-25 20:05:44809 case DIFF_STATUS_ADDED:
810 case DIFF_STATUS_DELETED:
Junio C Hamanof345b0a2005-05-30 07:08:37811 two_paths = 0;
Junio C Hamanof345b0a2005-05-30 07:08:37812 break;
Junio C Hamanob6d8f302005-05-23 21:55:33813 default:
814 two_paths = 0;
Junio C Hamanob6d8f302005-05-23 21:55:33815 break;
Junio C Hamano6b14d7f2005-05-22 17:04:37816 }
Junio C Hamano81e50ea2005-05-22 02:42:18817 printf(":%06o %06o %s ",
818 p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
Junio C Hamanob6d8f302005-05-23 21:55:33819 printf("%s %s%c%s",
820 sha1_to_hex(p->two->sha1),
821 status,
822 inter_name_termination,
823 p->one->path);
824 if (two_paths)
825 printf("%c%s", inter_name_termination, p->two->path);
826 putchar(line_termination);
Junio C Hamano427dcb42005-05-21 09:39:09827}
828
Junio C Hamano52f28522005-07-13 19:45:51829static void diff_flush_name(struct diff_filepair *p,
830 int line_termination)
831{
832 printf("%s%c", p->two->path, line_termination);
833}
834
Junio C Hamanof7c15122005-05-23 04:26:09835int diff_unmodified_pair(struct diff_filepair *p)
Junio C Hamano427dcb42005-05-21 09:39:09836{
837 /* This function is written stricter than necessary to support
838 * the currently implemented transformers, but the idea is to
Junio C Hamano52e95782005-05-21 09:40:01839 * let transformers to produce diff_filepairs any way they want,
Junio C Hamano427dcb42005-05-21 09:39:09840 * and filter and clean them up here before producing the output.
841 */
Junio C Hamano6b14d7f2005-05-22 17:04:37842 struct diff_filespec *one, *two;
Junio C Hamano427dcb42005-05-21 09:39:09843
Junio C Hamano6b14d7f2005-05-22 17:04:37844 if (DIFF_PAIR_UNMERGED(p))
845 return 0; /* unmerged is interesting */
846
847 one = p->one;
848 two = p->two;
Junio C Hamano427dcb42005-05-21 09:39:09849
Junio C Hamano4130b992005-05-26 09:24:30850 /* deletion, addition, mode or type change
851 * and rename are all interesting.
852 */
Junio C Hamano81e50ea2005-05-22 02:42:18853 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
Junio C Hamano4130b992005-05-26 09:24:30854 DIFF_PAIR_MODE_CHANGED(p) ||
Junio C Hamano427dcb42005-05-21 09:39:09855 strcmp(one->path, two->path))
856 return 0;
857
858 /* both are valid and point at the same path. that is, we are
859 * dealing with a change.
860 */
861 if (one->sha1_valid && two->sha1_valid &&
862 !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
863 return 1; /* no change */
864 if (!one->sha1_valid && !two->sha1_valid)
865 return 1; /* both look at the same file on the filesystem. */
866 return 0;
867}
868
Junio C Hamanob6d8f302005-05-23 21:55:33869static void diff_flush_patch(struct diff_filepair *p)
Junio C Hamanof7c15122005-05-23 04:26:09870{
Junio C Hamanof7c15122005-05-23 04:26:09871 if (diff_unmodified_pair(p))
872 return;
873
Junio C Hamanof7c15122005-05-23 04:26:09874 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
875 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
876 return; /* no tree diffs in patch format */
877
Junio C Hamano366175e2005-06-19 20:17:50878 run_diff(p);
Junio C Hamanof7c15122005-05-23 04:26:09879}
880
Junio C Hamano38c6f782005-05-22 02:40:36881int diff_queue_is_empty(void)
Junio C Hamano427dcb42005-05-21 09:39:09882{
Junio C Hamano38c6f782005-05-22 02:40:36883 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano25d5ea42005-05-24 08:10:48884 int i;
885 for (i = 0; i < q->nr; i++)
886 if (!diff_unmodified_pair(q->queue[i]))
887 return 0;
888 return 1;
Junio C Hamano38c6f782005-05-22 02:40:36889}
890
Junio C Hamano25d5ea42005-05-24 08:10:48891#if DIFF_DEBUG
892void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
893{
894 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
Jason Riedyc7c81b32005-08-23 20:34:07895 x, one ? one : "",
Junio C Hamano25d5ea42005-05-24 08:10:48896 s->path,
897 DIFF_FILE_VALID(s) ? "valid" : "invalid",
898 s->mode,
899 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
900 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
Jason Riedyc7c81b32005-08-23 20:34:07901 x, one ? one : "",
Junio C Hamano25d5ea42005-05-24 08:10:48902 s->size, s->xfrm_flags);
903}
904
905void diff_debug_filepair(const struct diff_filepair *p, int i)
906{
907 diff_debug_filespec(p->one, i, "one");
908 diff_debug_filespec(p->two, i, "two");
Junio C Hamanof345b0a2005-05-30 07:08:37909 fprintf(stderr, "score %d, status %c stays %d broken %d\n",
Jason Riedyc7c81b32005-08-23 20:34:07910 p->score, p->status ? p->status : '?',
Junio C Hamanof345b0a2005-05-30 07:08:37911 p->source_stays, p->broken_pair);
Junio C Hamano25d5ea42005-05-24 08:10:48912}
913
914void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
915{
916 int i;
917 if (msg)
918 fprintf(stderr, "%s\n", msg);
919 fprintf(stderr, "q->nr = %d\n", q->nr);
920 for (i = 0; i < q->nr; i++) {
921 struct diff_filepair *p = q->queue[i];
922 diff_debug_filepair(p, i);
923 }
924}
925#endif
926
Junio C Hamanobceafe72005-05-24 01:14:03927static void diff_resolve_rename_copy(void)
928{
Junio C Hamano25d5ea42005-05-24 08:10:48929 int i, j;
930 struct diff_filepair *p, *pp;
Junio C Hamanobceafe72005-05-24 01:14:03931 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano25d5ea42005-05-24 08:10:48932
Junio C Hamano25d5ea42005-05-24 08:10:48933 diff_debug_queue("resolve-rename-copy", q);
934
Junio C Hamanobceafe72005-05-24 01:14:03935 for (i = 0; i < q->nr; i++) {
Junio C Hamano25d5ea42005-05-24 08:10:48936 p = q->queue[i];
Junio C Hamano96716a12005-05-25 22:07:08937 p->status = 0; /* undecided */
Junio C Hamanobceafe72005-05-24 01:14:03938 if (DIFF_PAIR_UNMERGED(p))
Junio C Hamanoe7baa4f2005-07-25 20:05:44939 p->status = DIFF_STATUS_UNMERGED;
Junio C Hamano15d061b2005-05-27 22:55:55940 else if (!DIFF_FILE_VALID(p->one))
Junio C Hamanoe7baa4f2005-07-25 20:05:44941 p->status = DIFF_STATUS_ADDED;
Junio C Hamano2cd68882005-05-30 07:08:07942 else if (!DIFF_FILE_VALID(p->two))
Junio C Hamanoe7baa4f2005-07-25 20:05:44943 p->status = DIFF_STATUS_DELETED;
Junio C Hamano96716a12005-05-25 22:07:08944 else if (DIFF_PAIR_TYPE_CHANGED(p))
Junio C Hamanoe7baa4f2005-07-25 20:05:44945 p->status = DIFF_STATUS_TYPE_CHANGED;
Junio C Hamano96716a12005-05-25 22:07:08946
947 /* from this point on, we are dealing with a pair
948 * whose both sides are valid and of the same type, i.e.
949 * either in-place edit or rename/copy edit.
950 */
Junio C Hamano01c4e702005-05-29 23:56:48951 else if (DIFF_PAIR_RENAME(p)) {
Junio C Hamano15d061b2005-05-27 22:55:55952 if (p->source_stays) {
Junio C Hamanoe7baa4f2005-07-25 20:05:44953 p->status = DIFF_STATUS_COPIED;
Junio C Hamano15d061b2005-05-27 22:55:55954 continue;
955 }
956 /* See if there is some other filepair that
957 * copies from the same source as us. If so
Junio C Hamano6bac10d2005-09-10 19:42:32958 * we are a copy. Otherwise we are either a
959 * copy if the path stays, or a rename if it
960 * does not, but we already handled "stays" case.
Junio C Hamano25d5ea42005-05-24 08:10:48961 */
Junio C Hamano15d061b2005-05-27 22:55:55962 for (j = i + 1; j < q->nr; j++) {
Junio C Hamano25d5ea42005-05-24 08:10:48963 pp = q->queue[j];
964 if (strcmp(pp->one->path, p->one->path))
Junio C Hamano15d061b2005-05-27 22:55:55965 continue; /* not us */
Junio C Hamano01c4e702005-05-29 23:56:48966 if (!DIFF_PAIR_RENAME(pp))
Junio C Hamano15d061b2005-05-27 22:55:55967 continue; /* not a rename/copy */
968 /* pp is a rename/copy from the same source */
Junio C Hamanoe7baa4f2005-07-25 20:05:44969 p->status = DIFF_STATUS_COPIED;
Junio C Hamano15d061b2005-05-27 22:55:55970 break;
Junio C Hamano25d5ea42005-05-24 08:10:48971 }
972 if (!p->status)
Junio C Hamanoe7baa4f2005-07-25 20:05:44973 p->status = DIFF_STATUS_RENAMED;
Junio C Hamanobceafe72005-05-24 01:14:03974 }
Junio C Hamano9fdade02005-05-25 23:00:04975 else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
976 p->one->mode != p->two->mode)
Junio C Hamanoe7baa4f2005-07-25 20:05:44977 p->status = DIFF_STATUS_MODIFIED;
Junio C Hamano67574c42005-06-01 18:38:07978 else {
979 /* This is a "no-change" entry and should not
980 * happen anymore, but prepare for broken callers.
Junio C Hamano15d061b2005-05-27 22:55:55981 */
Junio C Hamano67574c42005-06-01 18:38:07982 error("feeding unmodified %s to diffcore",
983 p->one->path);
Junio C Hamanoe7baa4f2005-07-25 20:05:44984 p->status = DIFF_STATUS_UNKNOWN;
Junio C Hamano67574c42005-06-01 18:38:07985 }
Junio C Hamanobceafe72005-05-24 01:14:03986 }
Junio C Hamano25d5ea42005-05-24 08:10:48987 diff_debug_queue("resolve-rename-copy done", q);
Junio C Hamanobceafe72005-05-24 01:14:03988}
989
Linus Torvaldse68b6f12005-07-15 00:59:17990void diff_flush(int diff_output_style, int line_termination)
Junio C Hamano38c6f782005-05-22 02:40:36991{
992 struct diff_queue_struct *q = &diff_queued_diff;
993 int i;
Junio C Hamanobceafe72005-05-24 01:14:03994 int inter_name_termination = '\t';
Junio C Hamano38c6f782005-05-22 02:40:36995
Linus Torvaldse68b6f12005-07-15 00:59:17996 if (!line_termination)
997 inter_name_termination = 0;
Junio C Hamanobceafe72005-05-24 01:14:03998
Junio C Hamanof7c15122005-05-23 04:26:09999 for (i = 0; i < q->nr; i++) {
Junio C Hamanof7c15122005-05-23 04:26:091000 struct diff_filepair *p = q->queue[i];
Junio C Hamano4130b992005-05-26 09:24:301001 if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
Junio C Hamanoe7baa4f2005-07-25 20:05:441002 (p->status == DIFF_STATUS_UNKNOWN))
Junio C Hamano96716a12005-05-25 22:07:081003 continue;
Junio C Hamanobceafe72005-05-24 01:14:031004 if (p->status == 0)
Junio C Hamano96716a12005-05-25 22:07:081005 die("internal error in diff-resolve-rename-copy");
Junio C Hamanobceafe72005-05-24 01:14:031006 switch (diff_output_style) {
1007 case DIFF_FORMAT_PATCH:
Junio C Hamanob6d8f302005-05-23 21:55:331008 diff_flush_patch(p);
Junio C Hamanobceafe72005-05-24 01:14:031009 break;
Linus Torvaldse68b6f12005-07-15 00:59:171010 case DIFF_FORMAT_RAW:
Junio C Hamanobceafe72005-05-24 01:14:031011 diff_flush_raw(p, line_termination,
1012 inter_name_termination);
1013 break;
Junio C Hamano52f28522005-07-13 19:45:511014 case DIFF_FORMAT_NAME:
Junio C Hamano52f28522005-07-13 19:45:511015 diff_flush_name(p, line_termination);
1016 break;
Junio C Hamanobceafe72005-05-24 01:14:031017 }
Junio C Hamano226406f2005-05-27 22:50:301018 diff_free_filepair(q->queue[i]);
Yasushi SHOJI90a734d2005-08-21 07:14:161019 }
Junio C Hamano427dcb42005-05-21 09:39:091020 free(q->queue);
1021 q->queue = NULL;
1022 q->nr = q->alloc = 0;
1023}
1024
Junio C Hamanof2ce9fd2005-06-12 03:57:131025static void diffcore_apply_filter(const char *filter)
1026{
1027 int i;
1028 struct diff_queue_struct *q = &diff_queued_diff;
1029 struct diff_queue_struct outq;
1030 outq.queue = NULL;
1031 outq.nr = outq.alloc = 0;
1032
1033 if (!filter)
1034 return;
1035
Junio C Hamanoe7baa4f2005-07-25 20:05:441036 if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
Junio C Hamanof2ce9fd2005-06-12 03:57:131037 int found;
1038 for (i = found = 0; !found && i < q->nr; i++) {
1039 struct diff_filepair *p = q->queue[i];
Junio C Hamanoe7baa4f2005-07-25 20:05:441040 if (((p->status == DIFF_STATUS_MODIFIED) &&
1041 ((p->score &&
1042 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1043 (!p->score &&
1044 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1045 ((p->status != DIFF_STATUS_MODIFIED) &&
1046 strchr(filter, p->status)))
Junio C Hamanof2ce9fd2005-06-12 03:57:131047 found++;
1048 }
1049 if (found)
1050 return;
1051
1052 /* otherwise we will clear the whole queue
1053 * by copying the empty outq at the end of this
1054 * function, but first clear the current entries
1055 * in the queue.
1056 */
1057 for (i = 0; i < q->nr; i++)
1058 diff_free_filepair(q->queue[i]);
1059 }
1060 else {
1061 /* Only the matching ones */
1062 for (i = 0; i < q->nr; i++) {
1063 struct diff_filepair *p = q->queue[i];
Junio C Hamanoe7baa4f2005-07-25 20:05:441064
1065 if (((p->status == DIFF_STATUS_MODIFIED) &&
1066 ((p->score &&
1067 strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1068 (!p->score &&
1069 strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1070 ((p->status != DIFF_STATUS_MODIFIED) &&
1071 strchr(filter, p->status)))
Junio C Hamanof2ce9fd2005-06-12 03:57:131072 diff_q(&outq, p);
1073 else
1074 diff_free_filepair(p);
1075 }
1076 }
1077 free(q->queue);
1078 *q = outq;
1079}
1080
Junio C Hamanobefe8632005-05-29 23:56:131081void diffcore_std(const char **paths,
1082 int detect_rename, int rename_score,
Junio C Hamanof345b0a2005-05-30 07:08:371083 const char *pickaxe, int pickaxe_opts,
Junio C Hamanoaf5323e2005-05-30 07:09:071084 int break_opt,
Junio C Hamanof2ce9fd2005-06-12 03:57:131085 const char *orderfile,
1086 const char *filter)
Junio C Hamanobefe8632005-05-29 23:56:131087{
1088 if (paths && paths[0])
1089 diffcore_pathspec(paths);
Junio C Hamano49d9e852005-06-04 06:04:071090 if (break_opt != -1)
Junio C Hamanof345b0a2005-05-30 07:08:371091 diffcore_break(break_opt);
Junio C Hamanobefe8632005-05-29 23:56:131092 if (detect_rename)
1093 diffcore_rename(detect_rename, rename_score);
Junio C Hamano49d9e852005-06-04 06:04:071094 if (break_opt != -1)
Junio C Hamanoeeaa4602005-06-03 08:40:281095 diffcore_merge_broken();
Junio C Hamanobefe8632005-05-29 23:56:131096 if (pickaxe)
1097 diffcore_pickaxe(pickaxe, pickaxe_opts);
Junio C Hamanoaf5323e2005-05-30 07:09:071098 if (orderfile)
1099 diffcore_order(orderfile);
Junio C Hamanof2ce9fd2005-06-12 03:57:131100 diff_resolve_rename_copy();
1101 diffcore_apply_filter(filter);
1102}
1103
1104
1105void diffcore_std_no_resolve(const char **paths,
1106 const char *pickaxe, int pickaxe_opts,
1107 const char *orderfile,
1108 const char *filter)
1109{
1110 if (paths && paths[0])
1111 diffcore_pathspec(paths);
1112 if (pickaxe)
1113 diffcore_pickaxe(pickaxe, pickaxe_opts);
1114 if (orderfile)
1115 diffcore_order(orderfile);
1116 diffcore_apply_filter(filter);
Junio C Hamanobefe8632005-05-29 23:56:131117}
1118
Junio C Hamano77eb2722005-04-27 16:21:001119void diff_addremove(int addremove, unsigned mode,
1120 const unsigned char *sha1,
1121 const char *base, const char *path)
Junio C Hamanobe3cfa82005-04-26 16:25:051122{
Junio C Hamano77eb2722005-04-27 16:21:001123 char concatpath[PATH_MAX];
Junio C Hamano427dcb42005-05-21 09:39:091124 struct diff_filespec *one, *two;
Junio C Hamanobe3cfa82005-04-26 16:25:051125
Junio C Hamano427dcb42005-05-21 09:39:091126 /* This may look odd, but it is a preparation for
1127 * feeding "there are unchanged files which should
1128 * not produce diffs, but when you are doing copy
1129 * detection you would need them, so here they are"
1130 * entries to the diff-core. They will be prefixed
1131 * with something like '=' or '*' (I haven't decided
1132 * which but should not make any difference).
Junio C Hamanof7c15122005-05-23 04:26:091133 * Feeding the same new and old to diff_change()
Junio C Hamanobceafe72005-05-24 01:14:031134 * also has the same effect.
1135 * Before the final output happens, they are pruned after
1136 * merged into rename/copy pairs as appropriate.
Junio C Hamano427dcb42005-05-21 09:39:091137 */
Junio C Hamano7ca45252005-05-20 16:54:071138 if (reverse_diff)
Junio C Hamano427dcb42005-05-21 09:39:091139 addremove = (addremove == '+' ? '-' :
1140 addremove == '-' ? '+' : addremove);
Junio C Hamano7ca45252005-05-20 16:54:071141
Junio C Hamano427dcb42005-05-21 09:39:091142 if (!path) path = "";
1143 sprintf(concatpath, "%s%s", base, path);
1144 one = alloc_filespec(concatpath);
1145 two = alloc_filespec(concatpath);
Junio C Hamano57fe64a2005-05-20 02:00:361146
Junio C Hamano427dcb42005-05-21 09:39:091147 if (addremove != '+')
1148 fill_filespec(one, sha1, mode);
1149 if (addremove != '-')
1150 fill_filespec(two, sha1, mode);
Junio C Hamanobe3cfa82005-04-26 16:25:051151
Junio C Hamano38c6f782005-05-22 02:40:361152 diff_queue(&diff_queued_diff, one, two);
Junio C Hamanobe3cfa82005-04-26 16:25:051153}
1154
Junio C Hamanob6d8f302005-05-23 21:55:331155void diff_helper_input(unsigned old_mode,
1156 unsigned new_mode,
1157 const unsigned char *old_sha1,
1158 const unsigned char *new_sha1,
1159 const char *old_path,
1160 int status,
1161 int score,
1162 const char *new_path)
Junio C Hamano81e50ea2005-05-22 02:42:181163{
1164 struct diff_filespec *one, *two;
Junio C Hamanob6d8f302005-05-23 21:55:331165 struct diff_filepair *dp;
Junio C Hamano81e50ea2005-05-22 02:42:181166
Junio C Hamano81e50ea2005-05-22 02:42:181167 one = alloc_filespec(old_path);
1168 two = alloc_filespec(new_path);
1169 if (old_mode)
1170 fill_filespec(one, old_sha1, old_mode);
1171 if (new_mode)
1172 fill_filespec(two, new_sha1, new_mode);
Junio C Hamanob6d8f302005-05-23 21:55:331173 dp = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano903d4752005-05-27 22:53:311174 dp->score = score * MAX_SCORE / 100;
Junio C Hamanob6d8f302005-05-23 21:55:331175 dp->status = status;
Junio C Hamano81e50ea2005-05-22 02:42:181176}
1177
Junio C Hamano77eb2722005-04-27 16:21:001178void diff_change(unsigned old_mode, unsigned new_mode,
1179 const unsigned char *old_sha1,
1180 const unsigned char *new_sha1,
Junio C Hamano81e50ea2005-05-22 02:42:181181 const char *base, const char *path)
1182{
Junio C Hamano77eb2722005-04-27 16:21:001183 char concatpath[PATH_MAX];
Junio C Hamano427dcb42005-05-21 09:39:091184 struct diff_filespec *one, *two;
Junio C Hamanobe3cfa82005-04-26 16:25:051185
Junio C Hamano7ca45252005-05-20 16:54:071186 if (reverse_diff) {
1187 unsigned tmp;
1188 const unsigned char *tmp_c;
1189 tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1190 tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1191 }
Junio C Hamano427dcb42005-05-21 09:39:091192 if (!path) path = "";
1193 sprintf(concatpath, "%s%s", base, path);
1194 one = alloc_filespec(concatpath);
1195 two = alloc_filespec(concatpath);
1196 fill_filespec(one, old_sha1, old_mode);
1197 fill_filespec(two, new_sha1, new_mode);
Junio C Hamano7ca45252005-05-20 16:54:071198
Junio C Hamano38c6f782005-05-22 02:40:361199 diff_queue(&diff_queued_diff, one, two);
Junio C Hamano77eb2722005-04-27 16:21:001200}
Junio C Hamanobe3cfa82005-04-26 16:25:051201
Junio C Hamano77eb2722005-04-27 16:21:001202void diff_unmerge(const char *path)
1203{
Junio C Hamano6b14d7f2005-05-22 17:04:371204 struct diff_filespec *one, *two;
1205 one = alloc_filespec(path);
1206 two = alloc_filespec(path);
1207 diff_queue(&diff_queued_diff, one, two);
Junio C Hamano86436c22005-04-26 01:22:471208}