Documentation: improve "add", "pull" and "format-patch" examples
[git] / diff-lib.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include "cache.h"
5 #include "quote.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "diffcore.h"
9 #include "revision.h"
10 #include "cache-tree.h"
11 #include "path-list.h"
12 #include "unpack-trees.h"
13 #include "refs.h"
14
15 /*
16  * diff-files
17  */
18
19 static int read_directory(const char *path, struct path_list *list)
20 {
21         DIR *dir;
22         struct dirent *e;
23
24         if (!(dir = opendir(path)))
25                 return error("Could not open directory %s", path);
26
27         while ((e = readdir(dir)))
28                 if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
29                         path_list_insert(e->d_name, list);
30
31         closedir(dir);
32         return 0;
33 }
34
35 static int get_mode(const char *path, int *mode)
36 {
37         struct stat st;
38
39         if (!path || !strcmp(path, "/dev/null"))
40                 *mode = 0;
41         else if (!strcmp(path, "-"))
42                 *mode = create_ce_mode(0666);
43         else if (stat(path, &st))
44                 return error("Could not access '%s'", path);
45         else
46                 *mode = st.st_mode;
47         return 0;
48 }
49
50 static int queue_diff(struct diff_options *o,
51                 const char *name1, const char *name2)
52 {
53         int mode1 = 0, mode2 = 0;
54
55         if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
56                 return -1;
57
58         if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
59                 return error("file/directory conflict: %s, %s", name1, name2);
60
61         if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
62                 char buffer1[PATH_MAX], buffer2[PATH_MAX];
63                 struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1};
64                 int len1 = 0, len2 = 0, i1, i2, ret = 0;
65
66                 if (name1 && read_directory(name1, &p1))
67                         return -1;
68                 if (name2 && read_directory(name2, &p2)) {
69                         path_list_clear(&p1, 0);
70                         return -1;
71                 }
72
73                 if (name1) {
74                         len1 = strlen(name1);
75                         if (len1 > 0 && name1[len1 - 1] == '/')
76                                 len1--;
77                         memcpy(buffer1, name1, len1);
78                         buffer1[len1++] = '/';
79                 }
80
81                 if (name2) {
82                         len2 = strlen(name2);
83                         if (len2 > 0 && name2[len2 - 1] == '/')
84                                 len2--;
85                         memcpy(buffer2, name2, len2);
86                         buffer2[len2++] = '/';
87                 }
88
89                 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
90                         const char *n1, *n2;
91                         int comp;
92
93                         if (i1 == p1.nr)
94                                 comp = 1;
95                         else if (i2 == p2.nr)
96                                 comp = -1;
97                         else
98                                 comp = strcmp(p1.items[i1].path,
99                                         p2.items[i2].path);
100
101                         if (comp > 0)
102                                 n1 = NULL;
103                         else {
104                                 n1 = buffer1;
105                                 strncpy(buffer1 + len1, p1.items[i1++].path,
106                                                 PATH_MAX - len1);
107                         }
108
109                         if (comp < 0)
110                                 n2 = NULL;
111                         else {
112                                 n2 = buffer2;
113                                 strncpy(buffer2 + len2, p2.items[i2++].path,
114                                                 PATH_MAX - len2);
115                         }
116
117                         ret = queue_diff(o, n1, n2);
118                 }
119                 path_list_clear(&p1, 0);
120                 path_list_clear(&p2, 0);
121
122                 return ret;
123         } else {
124                 struct diff_filespec *d1, *d2;
125
126                 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
127                         unsigned tmp;
128                         const char *tmp_c;
129                         tmp = mode1; mode1 = mode2; mode2 = tmp;
130                         tmp_c = name1; name1 = name2; name2 = tmp_c;
131                 }
132
133                 if (!name1)
134                         name1 = "/dev/null";
135                 if (!name2)
136                         name2 = "/dev/null";
137                 d1 = alloc_filespec(name1);
138                 d2 = alloc_filespec(name2);
139                 fill_filespec(d1, null_sha1, mode1);
140                 fill_filespec(d2, null_sha1, mode2);
141
142                 diff_queue(&diff_queued_diff, d1, d2);
143                 return 0;
144         }
145 }
146
147 /*
148  * Does the path name a blob in the working tree, or a directory
149  * in the working tree?
150  */
151 static int is_in_index(const char *path)
152 {
153         int len, pos;
154         struct cache_entry *ce;
155
156         len = strlen(path);
157         while (path[len-1] == '/')
158                 len--;
159         if (!len)
160                 return 1; /* "." */
161         pos = cache_name_pos(path, len);
162         if (0 <= pos)
163                 return 1;
164         pos = -1 - pos;
165         while (pos < active_nr) {
166                 ce = active_cache[pos++];
167                 if (ce_namelen(ce) <= len ||
168                     strncmp(ce->name, path, len) ||
169                     (ce->name[len] > '/'))
170                         break; /* path cannot be a prefix */
171                 if (ce->name[len] == '/')
172                         return 1;
173         }
174         return 0;
175 }
176
177 static int handle_diff_files_args(struct rev_info *revs,
178                                   int argc, const char **argv,
179                                   unsigned int *options)
180 {
181         *options = 0;
182
183         /* revs->max_count == -2 means --no-index */
184         while (1 < argc && argv[1][0] == '-') {
185                 if (!strcmp(argv[1], "--base"))
186                         revs->max_count = 1;
187                 else if (!strcmp(argv[1], "--ours"))
188                         revs->max_count = 2;
189                 else if (!strcmp(argv[1], "--theirs"))
190                         revs->max_count = 3;
191                 else if (!strcmp(argv[1], "-n") ||
192                                 !strcmp(argv[1], "--no-index")) {
193                         revs->max_count = -2;
194                         DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
195                         DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
196                 }
197                 else if (!strcmp(argv[1], "-q"))
198                         *options |= DIFF_SILENT_ON_REMOVED;
199                 else
200                         return error("invalid option: %s", argv[1]);
201                 argv++; argc--;
202         }
203
204         if (revs->max_count == -1 && revs->diffopt.nr_paths == 2) {
205                 /*
206                  * If two files are specified, and at least one is untracked,
207                  * default to no-index.
208                  */
209                 read_cache();
210                 if (!is_in_index(revs->diffopt.paths[0]) ||
211                                         !is_in_index(revs->diffopt.paths[1])) {
212                         revs->max_count = -2;
213                         DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
214                 }
215         }
216
217         /*
218          * Make sure there are NO revision (i.e. pending object) parameter,
219          * rev.max_count is reasonable (0 <= n <= 3),
220          * there is no other revision filtering parameters.
221          */
222         if (revs->pending.nr || revs->max_count > 3 ||
223             revs->min_age != -1 || revs->max_age != -1)
224                 return error("no revision allowed with diff-files");
225
226         if (revs->max_count == -1 &&
227             (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
228                 revs->combine_merges = revs->dense_combined_merges = 1;
229
230         return 0;
231 }
232
233 static int is_outside_repo(const char *path, int nongit, const char *prefix)
234 {
235         int i;
236         if (nongit || !strcmp(path, "-") || is_absolute_path(path))
237                 return 1;
238         if (prefixcmp(path, "../"))
239                 return 0;
240         if (!prefix)
241                 return 1;
242         for (i = strlen(prefix); !prefixcmp(path, "../"); ) {
243                 while (i > 0 && prefix[i - 1] != '/')
244                         i--;
245                 if (--i < 0)
246                         return 1;
247                 path += 3;
248         }
249         return 0;
250 }
251
252 int setup_diff_no_index(struct rev_info *revs,
253                 int argc, const char ** argv, int nongit, const char *prefix)
254 {
255         int i;
256         for (i = 1; i < argc; i++)
257                 if (argv[i][0] != '-' || argv[i][1] == '\0')
258                         break;
259                 else if (!strcmp(argv[i], "--")) {
260                         i++;
261                         break;
262                 } else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) {
263                         i = argc - 3;
264                         DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
265                         break;
266                 }
267         if (nongit && argc != i + 2)
268                 die("git diff [--no-index] takes two paths");
269
270         if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) &&
271                                 !is_outside_repo(argv[i], nongit, prefix)))
272                 return -1;
273
274         diff_setup(&revs->diffopt);
275         for (i = 1; i < argc - 2; )
276                 if (!strcmp(argv[i], "--no-index"))
277                         i++;
278                 else {
279                         int j = diff_opt_parse(&revs->diffopt,
280                                         argv + i, argc - i);
281                         if (!j)
282                                 die("invalid diff option/value: %s", argv[i]);
283                         i += j;
284                 }
285
286         if (prefix) {
287                 int len = strlen(prefix);
288
289                 revs->diffopt.paths = xcalloc(2, sizeof(char*));
290                 for (i = 0; i < 2; i++) {
291                         const char *p = argv[argc - 2 + i];
292                         /*
293                          * stdin should be spelled as '-'; if you have
294                          * path that is '-', spell it as ./-.
295                          */
296                         p = (strcmp(p, "-")
297                              ? xstrdup(prefix_filename(prefix, len, p))
298                              : p);
299                         revs->diffopt.paths[i] = p;
300                 }
301         }
302         else
303                 revs->diffopt.paths = argv + argc - 2;
304         revs->diffopt.nr_paths = 2;
305         DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
306         revs->max_count = -2;
307         if (diff_setup_done(&revs->diffopt) < 0)
308                 die("diff_setup_done failed");
309         return 0;
310 }
311
312 int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
313 {
314         unsigned int options;
315
316         if (handle_diff_files_args(revs, argc, argv, &options))
317                 return -1;
318
319         if (DIFF_OPT_TST(&revs->diffopt, NO_INDEX)) {
320                 if (revs->diffopt.nr_paths != 2)
321                         return error("need two files/directories with --no-index");
322                 if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
323                                 revs->diffopt.paths[1]))
324                         return -1;
325                 diffcore_std(&revs->diffopt);
326                 diff_flush(&revs->diffopt);
327                 /*
328                  * The return code for --no-index imitates diff(1):
329                  * 0 = no changes, 1 = changes, else error
330                  */
331                 return revs->diffopt.found_changes;
332         }
333
334         if (read_cache() < 0) {
335                 perror("read_cache");
336                 return -1;
337         }
338         return run_diff_files(revs, options);
339 }
340 /*
341  * See if work tree has an entity that can be staged.  Return 0 if so,
342  * return 1 if not and return -1 if error.
343  */
344 static int check_work_tree_entity(const struct cache_entry *ce, struct stat *st, char *symcache)
345 {
346         if (lstat(ce->name, st) < 0) {
347                 if (errno != ENOENT && errno != ENOTDIR)
348                         return -1;
349                 return 1;
350         }
351         if (has_symlink_leading_path(ce->name, symcache))
352                 return 1;
353         if (S_ISDIR(st->st_mode)) {
354                 unsigned char sub[20];
355                 if (resolve_gitlink_ref(ce->name, "HEAD", sub))
356                         return 1;
357         }
358         return 0;
359 }
360
361 int run_diff_files(struct rev_info *revs, unsigned int option)
362 {
363         int entries, i;
364         int diff_unmerged_stage = revs->max_count;
365         int silent_on_removed = option & DIFF_SILENT_ON_REMOVED;
366         unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
367                               ? CE_MATCH_RACY_IS_DIRTY : 0);
368         char symcache[PATH_MAX];
369
370         if (diff_unmerged_stage < 0)
371                 diff_unmerged_stage = 2;
372         entries = active_nr;
373         symcache[0] = '\0';
374         for (i = 0; i < entries; i++) {
375                 struct stat st;
376                 unsigned int oldmode, newmode;
377                 struct cache_entry *ce = active_cache[i];
378                 int changed;
379
380                 if (DIFF_OPT_TST(&revs->diffopt, QUIET) &&
381                         DIFF_OPT_TST(&revs->diffopt, HAS_CHANGES))
382                         break;
383
384                 if (!ce_path_match(ce, revs->prune_data))
385                         continue;
386
387                 if (ce_stage(ce)) {
388                         struct combine_diff_path *dpath;
389                         int num_compare_stages = 0;
390                         size_t path_len;
391
392                         path_len = ce_namelen(ce);
393
394                         dpath = xmalloc(combine_diff_path_size(5, path_len));
395                         dpath->path = (char *) &(dpath->parent[5]);
396
397                         dpath->next = NULL;
398                         dpath->len = path_len;
399                         memcpy(dpath->path, ce->name, path_len);
400                         dpath->path[path_len] = '\0';
401                         hashclr(dpath->sha1);
402                         memset(&(dpath->parent[0]), 0,
403                                sizeof(struct combine_diff_parent)*5);
404
405                         changed = check_work_tree_entity(ce, &st, symcache);
406                         if (!changed)
407                                 dpath->mode = ce_mode_from_stat(ce, st.st_mode);
408                         else {
409                                 if (changed < 0) {
410                                         perror(ce->name);
411                                         continue;
412                                 }
413                                 if (silent_on_removed)
414                                         continue;
415                         }
416
417                         while (i < entries) {
418                                 struct cache_entry *nce = active_cache[i];
419                                 int stage;
420
421                                 if (strcmp(ce->name, nce->name))
422                                         break;
423
424                                 /* Stage #2 (ours) is the first parent,
425                                  * stage #3 (theirs) is the second.
426                                  */
427                                 stage = ce_stage(nce);
428                                 if (2 <= stage) {
429                                         int mode = nce->ce_mode;
430                                         num_compare_stages++;
431                                         hashcpy(dpath->parent[stage-2].sha1, nce->sha1);
432                                         dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
433                                         dpath->parent[stage-2].status =
434                                                 DIFF_STATUS_MODIFIED;
435                                 }
436
437                                 /* diff against the proper unmerged stage */
438                                 if (stage == diff_unmerged_stage)
439                                         ce = nce;
440                                 i++;
441                         }
442                         /*
443                          * Compensate for loop update
444                          */
445                         i--;
446
447                         if (revs->combine_merges && num_compare_stages == 2) {
448                                 show_combined_diff(dpath, 2,
449                                                    revs->dense_combined_merges,
450                                                    revs);
451                                 free(dpath);
452                                 continue;
453                         }
454                         free(dpath);
455                         dpath = NULL;
456
457                         /*
458                          * Show the diff for the 'ce' if we found the one
459                          * from the desired stage.
460                          */
461                         diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
462                         if (ce_stage(ce) != diff_unmerged_stage)
463                                 continue;
464                 }
465
466                 if (ce_uptodate(ce))
467                         continue;
468
469                 changed = check_work_tree_entity(ce, &st, symcache);
470                 if (changed) {
471                         if (changed < 0) {
472                                 perror(ce->name);
473                                 continue;
474                         }
475                         if (silent_on_removed)
476                                 continue;
477                         diff_addremove(&revs->diffopt, '-', ce->ce_mode,
478                                        ce->sha1, ce->name, NULL);
479                         continue;
480                 }
481                 changed = ce_match_stat(ce, &st, ce_option);
482                 if (!changed) {
483                         ce_mark_uptodate(ce);
484                         if (!DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
485                                 continue;
486                 }
487                 oldmode = ce->ce_mode;
488                 newmode = ce_mode_from_stat(ce, st.st_mode);
489                 diff_change(&revs->diffopt, oldmode, newmode,
490                             ce->sha1, (changed ? null_sha1 : ce->sha1),
491                             ce->name, NULL);
492
493         }
494         diffcore_std(&revs->diffopt);
495         diff_flush(&revs->diffopt);
496         return 0;
497 }
498
499 /*
500  * diff-index
501  */
502
503 struct oneway_unpack_data {
504         struct rev_info *revs;
505         char symcache[PATH_MAX];
506 };
507
508 /* A file entry went away or appeared */
509 static void diff_index_show_file(struct rev_info *revs,
510                                  const char *prefix,
511                                  struct cache_entry *ce,
512                                  const unsigned char *sha1, unsigned int mode)
513 {
514         diff_addremove(&revs->diffopt, prefix[0], mode,
515                        sha1, ce->name, NULL);
516 }
517
518 static int get_stat_data(struct cache_entry *ce,
519                          const unsigned char **sha1p,
520                          unsigned int *modep,
521                          int cached, int match_missing,
522                          struct oneway_unpack_data *cbdata)
523 {
524         const unsigned char *sha1 = ce->sha1;
525         unsigned int mode = ce->ce_mode;
526
527         if (!cached) {
528                 int changed;
529                 struct stat st;
530                 changed = check_work_tree_entity(ce, &st, cbdata->symcache);
531                 if (changed < 0)
532                         return -1;
533                 else if (changed) {
534                         if (match_missing) {
535                                 *sha1p = sha1;
536                                 *modep = mode;
537                                 return 0;
538                         }
539                         return -1;
540                 }
541                 changed = ce_match_stat(ce, &st, 0);
542                 if (changed) {
543                         mode = ce_mode_from_stat(ce, st.st_mode);
544                         sha1 = null_sha1;
545                 }
546         }
547
548         *sha1p = sha1;
549         *modep = mode;
550         return 0;
551 }
552
553 static void show_new_file(struct oneway_unpack_data *cbdata,
554                           struct cache_entry *new,
555                           int cached, int match_missing)
556 {
557         const unsigned char *sha1;
558         unsigned int mode;
559         struct rev_info *revs = cbdata->revs;
560
561         /*
562          * New file in the index: it might actually be different in
563          * the working copy.
564          */
565         if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0)
566                 return;
567
568         diff_index_show_file(revs, "+", new, sha1, mode);
569 }
570
571 static int show_modified(struct oneway_unpack_data *cbdata,
572                          struct cache_entry *old,
573                          struct cache_entry *new,
574                          int report_missing,
575                          int cached, int match_missing)
576 {
577         unsigned int mode, oldmode;
578         const unsigned char *sha1;
579         struct rev_info *revs = cbdata->revs;
580
581         if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0) {
582                 if (report_missing)
583                         diff_index_show_file(revs, "-", old,
584                                              old->sha1, old->ce_mode);
585                 return -1;
586         }
587
588         if (revs->combine_merges && !cached &&
589             (hashcmp(sha1, old->sha1) || hashcmp(old->sha1, new->sha1))) {
590                 struct combine_diff_path *p;
591                 int pathlen = ce_namelen(new);
592
593                 p = xmalloc(combine_diff_path_size(2, pathlen));
594                 p->path = (char *) &p->parent[2];
595                 p->next = NULL;
596                 p->len = pathlen;
597                 memcpy(p->path, new->name, pathlen);
598                 p->path[pathlen] = 0;
599                 p->mode = mode;
600                 hashclr(p->sha1);
601                 memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
602                 p->parent[0].status = DIFF_STATUS_MODIFIED;
603                 p->parent[0].mode = new->ce_mode;
604                 hashcpy(p->parent[0].sha1, new->sha1);
605                 p->parent[1].status = DIFF_STATUS_MODIFIED;
606                 p->parent[1].mode = old->ce_mode;
607                 hashcpy(p->parent[1].sha1, old->sha1);
608                 show_combined_diff(p, 2, revs->dense_combined_merges, revs);
609                 free(p);
610                 return 0;
611         }
612
613         oldmode = old->ce_mode;
614         if (mode == oldmode && !hashcmp(sha1, old->sha1) &&
615             !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
616                 return 0;
617
618         diff_change(&revs->diffopt, oldmode, mode,
619                     old->sha1, sha1, old->name, NULL);
620         return 0;
621 }
622
623 /*
624  * This turns all merge entries into "stage 3". That guarantees that
625  * when we read in the new tree (into "stage 1"), we won't lose sight
626  * of the fact that we had unmerged entries.
627  */
628 static void mark_merge_entries(void)
629 {
630         int i;
631         for (i = 0; i < active_nr; i++) {
632                 struct cache_entry *ce = active_cache[i];
633                 if (!ce_stage(ce))
634                         continue;
635                 ce->ce_flags |= CE_STAGEMASK;
636         }
637 }
638
639 /*
640  * This gets a mix of an existing index and a tree, one pathname entry
641  * at a time. The index entry may be a single stage-0 one, but it could
642  * also be multiple unmerged entries (in which case idx_pos/idx_nr will
643  * give you the position and number of entries in the index).
644  */
645 static void do_oneway_diff(struct unpack_trees_options *o,
646         struct cache_entry *idx,
647         struct cache_entry *tree)
648 {
649         struct oneway_unpack_data *cbdata = o->unpack_data;
650         struct rev_info *revs = cbdata->revs;
651         int match_missing, cached;
652
653         /*
654          * Backward compatibility wart - "diff-index -m" does
655          * not mean "do not ignore merges", but "match_missing".
656          *
657          * But with the revision flag parsing, that's found in
658          * "!revs->ignore_merges".
659          */
660         cached = o->index_only;
661         match_missing = !revs->ignore_merges;
662
663         if (cached && idx && ce_stage(idx)) {
664                 if (tree)
665                         diff_unmerge(&revs->diffopt, idx->name, idx->ce_mode, idx->sha1);
666                 return;
667         }
668
669         /*
670          * Something added to the tree?
671          */
672         if (!tree) {
673                 show_new_file(cbdata, idx, cached, match_missing);
674                 return;
675         }
676
677         /*
678          * Something removed from the tree?
679          */
680         if (!idx) {
681                 diff_index_show_file(revs, "-", tree, tree->sha1, tree->ce_mode);
682                 return;
683         }
684
685         /* Show difference between old and new */
686         show_modified(cbdata, tree, idx, 1, cached, match_missing);
687 }
688
689 static inline void skip_same_name(struct cache_entry *ce, struct unpack_trees_options *o)
690 {
691         int len = ce_namelen(ce);
692         const struct index_state *index = o->src_index;
693
694         while (o->pos < index->cache_nr) {
695                 struct cache_entry *next = index->cache[o->pos];
696                 if (len != ce_namelen(next))
697                         break;
698                 if (memcmp(ce->name, next->name, len))
699                         break;
700                 o->pos++;
701         }
702 }
703
704 /*
705  * The unpack_trees() interface is designed for merging, so
706  * the different source entries are designed primarily for
707  * the source trees, with the old index being really mainly
708  * used for being replaced by the result.
709  *
710  * For diffing, the index is more important, and we only have a
711  * single tree.
712  *
713  * We're supposed to return how many index entries we want to skip.
714  *
715  * This wrapper makes it all more readable, and takes care of all
716  * the fairly complex unpack_trees() semantic requirements, including
717  * the skipping, the path matching, the type conflict cases etc.
718  */
719 static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)
720 {
721         struct cache_entry *idx = src[0];
722         struct cache_entry *tree = src[1];
723         struct oneway_unpack_data *cbdata = o->unpack_data;
724         struct rev_info *revs = cbdata->revs;
725
726         if (idx && ce_stage(idx))
727                 skip_same_name(idx, o);
728
729         /*
730          * Unpack-trees generates a DF/conflict entry if
731          * there was a directory in the index and a tree
732          * in the tree. From a diff standpoint, that's a
733          * delete of the tree and a create of the file.
734          */
735         if (tree == o->df_conflict_entry)
736                 tree = NULL;
737
738         if (ce_path_match(idx ? idx : tree, revs->prune_data))
739                 do_oneway_diff(o, idx, tree);
740
741         return 0;
742 }
743
744 int run_diff_index(struct rev_info *revs, int cached)
745 {
746         struct object *ent;
747         struct tree *tree;
748         const char *tree_name;
749         struct unpack_trees_options opts;
750         struct tree_desc t;
751         struct oneway_unpack_data unpack_cb;
752
753         mark_merge_entries();
754
755         ent = revs->pending.objects[0].item;
756         tree_name = revs->pending.objects[0].name;
757         tree = parse_tree_indirect(ent->sha1);
758         if (!tree)
759                 return error("bad tree object %s", tree_name);
760
761         unpack_cb.revs = revs;
762         unpack_cb.symcache[0] = '\0';
763         memset(&opts, 0, sizeof(opts));
764         opts.head_idx = 1;
765         opts.index_only = cached;
766         opts.merge = 1;
767         opts.fn = oneway_diff;
768         opts.unpack_data = &unpack_cb;
769         opts.src_index = &the_index;
770         opts.dst_index = NULL;
771
772         init_tree_desc(&t, tree->buffer, tree->size);
773         if (unpack_trees(1, &t, &opts))
774                 exit(128);
775
776         diffcore_std(&revs->diffopt);
777         diff_flush(&revs->diffopt);
778         return 0;
779 }
780
781 int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
782 {
783         struct tree *tree;
784         struct rev_info revs;
785         int i;
786         struct cache_entry **dst;
787         struct cache_entry *last = NULL;
788         struct unpack_trees_options opts;
789         struct tree_desc t;
790         struct oneway_unpack_data unpack_cb;
791
792         /*
793          * This is used by git-blame to run diff-cache internally;
794          * it potentially needs to repeatedly run this, so we will
795          * start by removing the higher order entries the last round
796          * left behind.
797          */
798         dst = active_cache;
799         for (i = 0; i < active_nr; i++) {
800                 struct cache_entry *ce = active_cache[i];
801                 if (ce_stage(ce)) {
802                         if (last && !strcmp(ce->name, last->name))
803                                 continue;
804                         cache_tree_invalidate_path(active_cache_tree,
805                                                    ce->name);
806                         last = ce;
807                         ce->ce_flags |= CE_REMOVE;
808                 }
809                 *dst++ = ce;
810         }
811         active_nr = dst - active_cache;
812
813         init_revisions(&revs, NULL);
814         revs.prune_data = opt->paths;
815         tree = parse_tree_indirect(tree_sha1);
816         if (!tree)
817                 die("bad tree object %s", sha1_to_hex(tree_sha1));
818
819         unpack_cb.revs = &revs;
820         unpack_cb.symcache[0] = '\0';
821         memset(&opts, 0, sizeof(opts));
822         opts.head_idx = 1;
823         opts.index_only = 1;
824         opts.merge = 1;
825         opts.fn = oneway_diff;
826         opts.unpack_data = &unpack_cb;
827         opts.src_index = &the_index;
828         opts.dst_index = &the_index;
829
830         init_tree_desc(&t, tree->buffer, tree->size);
831         if (unpack_trees(1, &t, &opts))
832                 exit(128);
833         return 0;
834 }