2 * Copyright (C) 2005 Junio C Hamano
10 #include "cache-tree.h"
11 #include "path-list.h"
12 #include "unpack-trees.h"
18 static int read_directory(const char *path
, struct path_list
*list
)
23 if (!(dir
= opendir(path
)))
24 return error("Could not open directory %s", path
);
26 while ((e
= readdir(dir
)))
27 if (strcmp(".", e
->d_name
) && strcmp("..", e
->d_name
))
28 path_list_insert(e
->d_name
, list
);
34 static int get_mode(const char *path
, int *mode
)
38 if (!path
|| !strcmp(path
, "/dev/null"))
40 else if (!strcmp(path
, "-"))
41 *mode
= create_ce_mode(0666);
42 else if (stat(path
, &st
))
43 return error("Could not access '%s'", path
);
49 static int queue_diff(struct diff_options
*o
,
50 const char *name1
, const char *name2
)
52 int mode1
= 0, mode2
= 0;
54 if (get_mode(name1
, &mode1
) || get_mode(name2
, &mode2
))
57 if (mode1
&& mode2
&& S_ISDIR(mode1
) != S_ISDIR(mode2
))
58 return error("file/directory conflict: %s, %s", name1
, name2
);
60 if (S_ISDIR(mode1
) || S_ISDIR(mode2
)) {
61 char buffer1
[PATH_MAX
], buffer2
[PATH_MAX
];
62 struct path_list p1
= {NULL
, 0, 0, 1}, p2
= {NULL
, 0, 0, 1};
63 int len1
= 0, len2
= 0, i1
, i2
, ret
= 0;
65 if (name1
&& read_directory(name1
, &p1
))
67 if (name2
&& read_directory(name2
, &p2
)) {
68 path_list_clear(&p1
, 0);
74 if (len1
> 0 && name1
[len1
- 1] == '/')
76 memcpy(buffer1
, name1
, len1
);
77 buffer1
[len1
++] = '/';
82 if (len2
> 0 && name2
[len2
- 1] == '/')
84 memcpy(buffer2
, name2
, len2
);
85 buffer2
[len2
++] = '/';
88 for (i1
= i2
= 0; !ret
&& (i1
< p1
.nr
|| i2
< p2
.nr
); ) {
97 comp
= strcmp(p1
.items
[i1
].path
,
104 strncpy(buffer1
+ len1
, p1
.items
[i1
++].path
,
112 strncpy(buffer2
+ len2
, p2
.items
[i2
++].path
,
116 ret
= queue_diff(o
, n1
, n2
);
118 path_list_clear(&p1
, 0);
119 path_list_clear(&p2
, 0);
123 struct diff_filespec
*d1
, *d2
;
125 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
128 tmp
= mode1
; mode1
= mode2
; mode2
= tmp
;
129 tmp_c
= name1
; name1
= name2
; name2
= tmp_c
;
136 d1
= alloc_filespec(name1
);
137 d2
= alloc_filespec(name2
);
138 fill_filespec(d1
, null_sha1
, mode1
);
139 fill_filespec(d2
, null_sha1
, mode2
);
141 diff_queue(&diff_queued_diff
, d1
, d2
);
147 * Does the path name a blob in the working tree, or a directory
148 * in the working tree?
150 static int is_in_index(const char *path
)
153 struct cache_entry
*ce
;
156 while (path
[len
-1] == '/')
160 pos
= cache_name_pos(path
, len
);
164 while (pos
< active_nr
) {
165 ce
= active_cache
[pos
++];
166 if (ce_namelen(ce
) <= len
||
167 strncmp(ce
->name
, path
, len
) ||
168 (ce
->name
[len
] > '/'))
169 break; /* path cannot be a prefix */
170 if (ce
->name
[len
] == '/')
176 static int handle_diff_files_args(struct rev_info
*revs
,
177 int argc
, const char **argv
,
178 unsigned int *options
)
182 /* revs->max_count == -2 means --no-index */
183 while (1 < argc
&& argv
[1][0] == '-') {
184 if (!strcmp(argv
[1], "--base"))
186 else if (!strcmp(argv
[1], "--ours"))
188 else if (!strcmp(argv
[1], "--theirs"))
190 else if (!strcmp(argv
[1], "-n") ||
191 !strcmp(argv
[1], "--no-index")) {
192 revs
->max_count
= -2;
193 DIFF_OPT_SET(&revs
->diffopt
, EXIT_WITH_STATUS
);
194 DIFF_OPT_SET(&revs
->diffopt
, NO_INDEX
);
196 else if (!strcmp(argv
[1], "-q"))
197 *options
|= DIFF_SILENT_ON_REMOVED
;
199 return error("invalid option: %s", argv
[1]);
203 if (revs
->max_count
== -1 && revs
->diffopt
.nr_paths
== 2) {
205 * If two files are specified, and at least one is untracked,
206 * default to no-index.
209 if (!is_in_index(revs
->diffopt
.paths
[0]) ||
210 !is_in_index(revs
->diffopt
.paths
[1])) {
211 revs
->max_count
= -2;
212 DIFF_OPT_SET(&revs
->diffopt
, NO_INDEX
);
217 * Make sure there are NO revision (i.e. pending object) parameter,
218 * rev.max_count is reasonable (0 <= n <= 3),
219 * there is no other revision filtering parameters.
221 if (revs
->pending
.nr
|| revs
->max_count
> 3 ||
222 revs
->min_age
!= -1 || revs
->max_age
!= -1)
223 return error("no revision allowed with diff-files");
225 if (revs
->max_count
== -1 &&
226 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
227 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
232 static int is_outside_repo(const char *path
, int nongit
, const char *prefix
)
235 if (nongit
|| !strcmp(path
, "-") || is_absolute_path(path
))
237 if (prefixcmp(path
, "../"))
241 for (i
= strlen(prefix
); !prefixcmp(path
, "../"); ) {
242 while (i
> 0 && prefix
[i
- 1] != '/')
251 int setup_diff_no_index(struct rev_info
*revs
,
252 int argc
, const char ** argv
, int nongit
, const char *prefix
)
255 for (i
= 1; i
< argc
; i
++)
256 if (argv
[i
][0] != '-' || argv
[i
][1] == '\0')
258 else if (!strcmp(argv
[i
], "--")) {
261 } else if (i
< argc
- 3 && !strcmp(argv
[i
], "--no-index")) {
263 DIFF_OPT_SET(&revs
->diffopt
, EXIT_WITH_STATUS
);
266 if (argc
!= i
+ 2 || (!is_outside_repo(argv
[i
+ 1], nongit
, prefix
) &&
267 !is_outside_repo(argv
[i
], nongit
, prefix
)))
270 diff_setup(&revs
->diffopt
);
271 for (i
= 1; i
< argc
- 2; )
272 if (!strcmp(argv
[i
], "--no-index"))
275 int j
= diff_opt_parse(&revs
->diffopt
,
278 die("invalid diff option/value: %s", argv
[i
]);
283 int len
= strlen(prefix
);
285 revs
->diffopt
.paths
= xcalloc(2, sizeof(char*));
286 for (i
= 0; i
< 2; i
++) {
287 const char *p
= argv
[argc
- 2 + i
];
289 * stdin should be spelled as '-'; if you have
290 * path that is '-', spell it as ./-.
293 ? xstrdup(prefix_filename(prefix
, len
, p
))
295 revs
->diffopt
.paths
[i
] = p
;
299 revs
->diffopt
.paths
= argv
+ argc
- 2;
300 revs
->diffopt
.nr_paths
= 2;
301 DIFF_OPT_SET(&revs
->diffopt
, NO_INDEX
);
302 revs
->max_count
= -2;
303 if (diff_setup_done(&revs
->diffopt
) < 0)
304 die("diff_setup_done failed");
308 int run_diff_files_cmd(struct rev_info
*revs
, int argc
, const char **argv
)
310 unsigned int options
;
312 if (handle_diff_files_args(revs
, argc
, argv
, &options
))
315 if (DIFF_OPT_TST(&revs
->diffopt
, NO_INDEX
)) {
316 if (revs
->diffopt
.nr_paths
!= 2)
317 return error("need two files/directories with --no-index");
318 if (queue_diff(&revs
->diffopt
, revs
->diffopt
.paths
[0],
319 revs
->diffopt
.paths
[1]))
321 diffcore_std(&revs
->diffopt
);
322 diff_flush(&revs
->diffopt
);
324 * The return code for --no-index imitates diff(1):
325 * 0 = no changes, 1 = changes, else error
327 return revs
->diffopt
.found_changes
;
330 if (read_cache() < 0) {
331 perror("read_cache");
334 return run_diff_files(revs
, options
);
337 int run_diff_files(struct rev_info
*revs
, unsigned int option
)
340 int diff_unmerged_stage
= revs
->max_count
;
341 int silent_on_removed
= option
& DIFF_SILENT_ON_REMOVED
;
342 unsigned ce_option
= ((option
& DIFF_RACY_IS_MODIFIED
)
343 ? CE_MATCH_RACY_IS_DIRTY
: 0);
345 if (diff_unmerged_stage
< 0)
346 diff_unmerged_stage
= 2;
348 for (i
= 0; i
< entries
; i
++) {
350 unsigned int oldmode
, newmode
;
351 struct cache_entry
*ce
= active_cache
[i
];
354 if (DIFF_OPT_TST(&revs
->diffopt
, QUIET
) &&
355 DIFF_OPT_TST(&revs
->diffopt
, HAS_CHANGES
))
358 if (!ce_path_match(ce
, revs
->prune_data
))
362 struct combine_diff_path
*dpath
;
363 int num_compare_stages
= 0;
366 path_len
= ce_namelen(ce
);
368 dpath
= xmalloc(combine_diff_path_size(5, path_len
));
369 dpath
->path
= (char *) &(dpath
->parent
[5]);
372 dpath
->len
= path_len
;
373 memcpy(dpath
->path
, ce
->name
, path_len
);
374 dpath
->path
[path_len
] = '\0';
375 hashclr(dpath
->sha1
);
376 memset(&(dpath
->parent
[0]), 0,
377 sizeof(struct combine_diff_parent
)*5);
379 if (lstat(ce
->name
, &st
) < 0) {
380 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
384 if (silent_on_removed
)
388 dpath
->mode
= ce_mode_from_stat(ce
, st
.st_mode
);
390 while (i
< entries
) {
391 struct cache_entry
*nce
= active_cache
[i
];
394 if (strcmp(ce
->name
, nce
->name
))
397 /* Stage #2 (ours) is the first parent,
398 * stage #3 (theirs) is the second.
400 stage
= ce_stage(nce
);
402 int mode
= nce
->ce_mode
;
403 num_compare_stages
++;
404 hashcpy(dpath
->parent
[stage
-2].sha1
, nce
->sha1
);
405 dpath
->parent
[stage
-2].mode
= ce_mode_from_stat(nce
, mode
);
406 dpath
->parent
[stage
-2].status
=
407 DIFF_STATUS_MODIFIED
;
410 /* diff against the proper unmerged stage */
411 if (stage
== diff_unmerged_stage
)
416 * Compensate for loop update
420 if (revs
->combine_merges
&& num_compare_stages
== 2) {
421 show_combined_diff(dpath
, 2,
422 revs
->dense_combined_merges
,
431 * Show the diff for the 'ce' if we found the one
432 * from the desired stage.
434 diff_unmerge(&revs
->diffopt
, ce
->name
, 0, null_sha1
);
435 if (ce_stage(ce
) != diff_unmerged_stage
)
441 if (lstat(ce
->name
, &st
) < 0) {
442 if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
446 if (silent_on_removed
)
448 diff_addremove(&revs
->diffopt
, '-', ce
->ce_mode
,
449 ce
->sha1
, ce
->name
, NULL
);
452 changed
= ce_match_stat(ce
, &st
, ce_option
);
453 if (!changed
&& !DIFF_OPT_TST(&revs
->diffopt
, FIND_COPIES_HARDER
))
455 oldmode
= ce
->ce_mode
;
456 newmode
= ce_mode_from_stat(ce
, st
.st_mode
);
457 diff_change(&revs
->diffopt
, oldmode
, newmode
,
458 ce
->sha1
, (changed
? null_sha1
: ce
->sha1
),
462 diffcore_std(&revs
->diffopt
);
463 diff_flush(&revs
->diffopt
);
471 /* A file entry went away or appeared */
472 static void diff_index_show_file(struct rev_info
*revs
,
474 struct cache_entry
*ce
,
475 const unsigned char *sha1
, unsigned int mode
)
477 diff_addremove(&revs
->diffopt
, prefix
[0], mode
,
478 sha1
, ce
->name
, NULL
);
481 static int get_stat_data(struct cache_entry
*ce
,
482 const unsigned char **sha1p
,
484 int cached
, int match_missing
)
486 const unsigned char *sha1
= ce
->sha1
;
487 unsigned int mode
= ce
->ce_mode
;
492 if (lstat(ce
->name
, &st
) < 0) {
493 if (errno
== ENOENT
&& match_missing
) {
500 changed
= ce_match_stat(ce
, &st
, 0);
502 mode
= ce_mode_from_stat(ce
, st
.st_mode
);
512 static void show_new_file(struct rev_info
*revs
,
513 struct cache_entry
*new,
514 int cached
, int match_missing
)
516 const unsigned char *sha1
;
519 /* New file in the index: it might actually be different in
522 if (get_stat_data(new, &sha1
, &mode
, cached
, match_missing
) < 0)
525 diff_index_show_file(revs
, "+", new, sha1
, mode
);
528 static int show_modified(struct rev_info
*revs
,
529 struct cache_entry
*old
,
530 struct cache_entry
*new,
532 int cached
, int match_missing
)
534 unsigned int mode
, oldmode
;
535 const unsigned char *sha1
;
537 if (get_stat_data(new, &sha1
, &mode
, cached
, match_missing
) < 0) {
539 diff_index_show_file(revs
, "-", old
,
540 old
->sha1
, old
->ce_mode
);
544 if (revs
->combine_merges
&& !cached
&&
545 (hashcmp(sha1
, old
->sha1
) || hashcmp(old
->sha1
, new->sha1
))) {
546 struct combine_diff_path
*p
;
547 int pathlen
= ce_namelen(new);
549 p
= xmalloc(combine_diff_path_size(2, pathlen
));
550 p
->path
= (char *) &p
->parent
[2];
553 memcpy(p
->path
, new->name
, pathlen
);
554 p
->path
[pathlen
] = 0;
557 memset(p
->parent
, 0, 2 * sizeof(struct combine_diff_parent
));
558 p
->parent
[0].status
= DIFF_STATUS_MODIFIED
;
559 p
->parent
[0].mode
= new->ce_mode
;
560 hashcpy(p
->parent
[0].sha1
, new->sha1
);
561 p
->parent
[1].status
= DIFF_STATUS_MODIFIED
;
562 p
->parent
[1].mode
= old
->ce_mode
;
563 hashcpy(p
->parent
[1].sha1
, old
->sha1
);
564 show_combined_diff(p
, 2, revs
->dense_combined_merges
, revs
);
569 oldmode
= old
->ce_mode
;
570 if (mode
== oldmode
&& !hashcmp(sha1
, old
->sha1
) &&
571 !DIFF_OPT_TST(&revs
->diffopt
, FIND_COPIES_HARDER
))
574 diff_change(&revs
->diffopt
, oldmode
, mode
,
575 old
->sha1
, sha1
, old
->name
, NULL
);
580 * This turns all merge entries into "stage 3". That guarantees that
581 * when we read in the new tree (into "stage 1"), we won't lose sight
582 * of the fact that we had unmerged entries.
584 static void mark_merge_entries(void)
587 for (i
= 0; i
< active_nr
; i
++) {
588 struct cache_entry
*ce
= active_cache
[i
];
591 ce
->ce_flags
|= CE_STAGEMASK
;
596 * This gets a mix of an existing index and a tree, one pathname entry
597 * at a time. The index entry may be a single stage-0 one, but it could
598 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
599 * give you the position and number of entries in the index).
601 static void do_oneway_diff(struct unpack_trees_options
*o
,
602 struct cache_entry
*idx
,
603 struct cache_entry
*tree
)
605 struct rev_info
*revs
= o
->unpack_data
;
606 int match_missing
, cached
;
609 * Backward compatibility wart - "diff-index -m" does
610 * not mean "do not ignore merges", but "match_missing".
612 * But with the revision flag parsing, that's found in
613 * "!revs->ignore_merges".
615 cached
= o
->index_only
;
616 match_missing
= !revs
->ignore_merges
;
618 if (cached
&& idx
&& ce_stage(idx
)) {
620 diff_unmerge(&revs
->diffopt
, idx
->name
, idx
->ce_mode
, idx
->sha1
);
625 * Something added to the tree?
628 show_new_file(revs
, idx
, cached
, match_missing
);
633 * Something removed from the tree?
636 diff_index_show_file(revs
, "-", tree
, tree
->sha1
, tree
->ce_mode
);
640 /* Show difference between old and new */
641 show_modified(revs
, tree
, idx
, 1, cached
, match_missing
);
644 static inline void skip_same_name(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
646 int len
= ce_namelen(ce
);
647 const struct index_state
*index
= o
->src_index
;
649 while (o
->pos
< index
->cache_nr
) {
650 struct cache_entry
*next
= index
->cache
[o
->pos
];
651 if (len
!= ce_namelen(next
))
653 if (memcmp(ce
->name
, next
->name
, len
))
660 * The unpack_trees() interface is designed for merging, so
661 * the different source entries are designed primarily for
662 * the source trees, with the old index being really mainly
663 * used for being replaced by the result.
665 * For diffing, the index is more important, and we only have a
668 * We're supposed to return how many index entries we want to skip.
670 * This wrapper makes it all more readable, and takes care of all
671 * the fairly complex unpack_trees() semantic requirements, including
672 * the skipping, the path matching, the type conflict cases etc.
674 static int oneway_diff(struct cache_entry
**src
, struct unpack_trees_options
*o
)
676 struct cache_entry
*idx
= src
[0];
677 struct cache_entry
*tree
= src
[1];
678 struct rev_info
*revs
= o
->unpack_data
;
680 if (idx
&& ce_stage(idx
))
681 skip_same_name(idx
, o
);
684 * Unpack-trees generates a DF/conflict entry if
685 * there was a directory in the index and a tree
686 * in the tree. From a diff standpoint, that's a
687 * delete of the tree and a create of the file.
689 if (tree
== o
->df_conflict_entry
)
692 if (ce_path_match(idx
? idx
: tree
, revs
->prune_data
))
693 do_oneway_diff(o
, idx
, tree
);
698 int run_diff_index(struct rev_info
*revs
, int cached
)
702 const char *tree_name
;
703 struct unpack_trees_options opts
;
706 mark_merge_entries();
708 ent
= revs
->pending
.objects
[0].item
;
709 tree_name
= revs
->pending
.objects
[0].name
;
710 tree
= parse_tree_indirect(ent
->sha1
);
712 return error("bad tree object %s", tree_name
);
714 memset(&opts
, 0, sizeof(opts
));
716 opts
.index_only
= cached
;
718 opts
.fn
= oneway_diff
;
719 opts
.unpack_data
= revs
;
720 opts
.src_index
= &the_index
;
721 opts
.dst_index
= NULL
;
723 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
724 if (unpack_trees(1, &t
, &opts
))
727 diffcore_std(&revs
->diffopt
);
728 diff_flush(&revs
->diffopt
);
732 int do_diff_cache(const unsigned char *tree_sha1
, struct diff_options
*opt
)
735 struct rev_info revs
;
737 struct cache_entry
**dst
;
738 struct cache_entry
*last
= NULL
;
739 struct unpack_trees_options opts
;
743 * This is used by git-blame to run diff-cache internally;
744 * it potentially needs to repeatedly run this, so we will
745 * start by removing the higher order entries the last round
749 for (i
= 0; i
< active_nr
; i
++) {
750 struct cache_entry
*ce
= active_cache
[i
];
752 if (last
&& !strcmp(ce
->name
, last
->name
))
754 cache_tree_invalidate_path(active_cache_tree
,
757 ce
->ce_flags
|= CE_REMOVE
;
761 active_nr
= dst
- active_cache
;
763 init_revisions(&revs
, NULL
);
764 revs
.prune_data
= opt
->paths
;
765 tree
= parse_tree_indirect(tree_sha1
);
767 die("bad tree object %s", sha1_to_hex(tree_sha1
));
769 memset(&opts
, 0, sizeof(opts
));
773 opts
.fn
= oneway_diff
;
774 opts
.unpack_data
= &revs
;
775 opts
.src_index
= &the_index
;
776 opts
.dst_index
= &the_index
;
778 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
779 if (unpack_trees(1, &t
, &opts
))