2 * Copyright (C) 2005 Junio C Hamano
10 #include "cache-tree.h"
11 #include "path-list.h"
12 #include "unpack-trees.h"
19 static int read_directory(const char *path
, struct path_list
*list
)
24 if (!(dir
= opendir(path
)))
25 return error("Could not open directory %s", path
);
27 while ((e
= readdir(dir
)))
28 if (strcmp(".", e
->d_name
) && strcmp("..", e
->d_name
))
29 path_list_insert(e
->d_name
, list
);
35 static int get_mode(const char *path
, int *mode
)
39 if (!path
|| !strcmp(path
, "/dev/null"))
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
);
50 static int queue_diff(struct diff_options
*o
,
51 const char *name1
, const char *name2
)
53 int mode1
= 0, mode2
= 0;
55 if (get_mode(name1
, &mode1
) || get_mode(name2
, &mode2
))
58 if (mode1
&& mode2
&& S_ISDIR(mode1
) != S_ISDIR(mode2
))
59 return error("file/directory conflict: %s, %s", name1
, name2
);
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;
66 if (name1
&& read_directory(name1
, &p1
))
68 if (name2
&& read_directory(name2
, &p2
)) {
69 path_list_clear(&p1
, 0);
75 if (len1
> 0 && name1
[len1
- 1] == '/')
77 memcpy(buffer1
, name1
, len1
);
78 buffer1
[len1
++] = '/';
83 if (len2
> 0 && name2
[len2
- 1] == '/')
85 memcpy(buffer2
, name2
, len2
);
86 buffer2
[len2
++] = '/';
89 for (i1
= i2
= 0; !ret
&& (i1
< p1
.nr
|| i2
< p2
.nr
); ) {
98 comp
= strcmp(p1
.items
[i1
].path
,
105 strncpy(buffer1
+ len1
, p1
.items
[i1
++].path
,
113 strncpy(buffer2
+ len2
, p2
.items
[i2
++].path
,
117 ret
= queue_diff(o
, n1
, n2
);
119 path_list_clear(&p1
, 0);
120 path_list_clear(&p2
, 0);
124 struct diff_filespec
*d1
, *d2
;
126 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
129 tmp
= mode1
; mode1
= mode2
; mode2
= tmp
;
130 tmp_c
= name1
; name1
= name2
; name2
= tmp_c
;
137 d1
= alloc_filespec(name1
);
138 d2
= alloc_filespec(name2
);
139 fill_filespec(d1
, null_sha1
, mode1
);
140 fill_filespec(d2
, null_sha1
, mode2
);
142 diff_queue(&diff_queued_diff
, d1
, d2
);
148 * Does the path name a blob in the working tree, or a directory
149 * in the working tree?
151 static int is_in_index(const char *path
)
154 struct cache_entry
*ce
;
157 while (path
[len
-1] == '/')
161 pos
= cache_name_pos(path
, len
);
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
] == '/')
177 static int handle_diff_files_args(struct rev_info
*revs
,
178 int argc
, const char **argv
,
179 unsigned int *options
)
183 /* revs->max_count == -2 means --no-index */
184 while (1 < argc
&& argv
[1][0] == '-') {
185 if (!strcmp(argv
[1], "--base"))
187 else if (!strcmp(argv
[1], "--ours"))
189 else if (!strcmp(argv
[1], "--theirs"))
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
);
197 else if (!strcmp(argv
[1], "-q"))
198 *options
|= DIFF_SILENT_ON_REMOVED
;
200 return error("invalid option: %s", argv
[1]);
204 if (revs
->max_count
== -1 && revs
->diffopt
.nr_paths
== 2) {
206 * If two files are specified, and at least one is untracked,
207 * default to no-index.
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
);
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.
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");
226 if (revs
->max_count
== -1 &&
227 (revs
->diffopt
.output_format
& DIFF_FORMAT_PATCH
))
228 revs
->combine_merges
= revs
->dense_combined_merges
= 1;
233 static int is_outside_repo(const char *path
, int nongit
, const char *prefix
)
236 if (nongit
|| !strcmp(path
, "-") || is_absolute_path(path
))
238 if (prefixcmp(path
, "../"))
242 for (i
= strlen(prefix
); !prefixcmp(path
, "../"); ) {
243 while (i
> 0 && prefix
[i
- 1] != '/')
252 int setup_diff_no_index(struct rev_info
*revs
,
253 int argc
, const char ** argv
, int nongit
, const char *prefix
)
256 for (i
= 1; i
< argc
; i
++)
257 if (argv
[i
][0] != '-' || argv
[i
][1] == '\0')
259 else if (!strcmp(argv
[i
], "--")) {
262 } else if (i
< argc
- 3 && !strcmp(argv
[i
], "--no-index")) {
264 DIFF_OPT_SET(&revs
->diffopt
, EXIT_WITH_STATUS
);
267 if (argc
!= i
+ 2 || (!is_outside_repo(argv
[i
+ 1], nongit
, prefix
) &&
268 !is_outside_repo(argv
[i
], nongit
, prefix
)))
271 diff_setup(&revs
->diffopt
);
272 for (i
= 1; i
< argc
- 2; )
273 if (!strcmp(argv
[i
], "--no-index"))
276 int j
= diff_opt_parse(&revs
->diffopt
,
279 die("invalid diff option/value: %s", argv
[i
]);
284 int len
= strlen(prefix
);
286 revs
->diffopt
.paths
= xcalloc(2, sizeof(char*));
287 for (i
= 0; i
< 2; i
++) {
288 const char *p
= argv
[argc
- 2 + i
];
290 * stdin should be spelled as '-'; if you have
291 * path that is '-', spell it as ./-.
294 ? xstrdup(prefix_filename(prefix
, len
, p
))
296 revs
->diffopt
.paths
[i
] = p
;
300 revs
->diffopt
.paths
= argv
+ argc
- 2;
301 revs
->diffopt
.nr_paths
= 2;
302 DIFF_OPT_SET(&revs
->diffopt
, NO_INDEX
);
303 revs
->max_count
= -2;
304 if (diff_setup_done(&revs
->diffopt
) < 0)
305 die("diff_setup_done failed");
309 int run_diff_files_cmd(struct rev_info
*revs
, int argc
, const char **argv
)
311 unsigned int options
;
313 if (handle_diff_files_args(revs
, argc
, argv
, &options
))
316 if (DIFF_OPT_TST(&revs
->diffopt
, NO_INDEX
)) {
317 if (revs
->diffopt
.nr_paths
!= 2)
318 return error("need two files/directories with --no-index");
319 if (queue_diff(&revs
->diffopt
, revs
->diffopt
.paths
[0],
320 revs
->diffopt
.paths
[1]))
322 diffcore_std(&revs
->diffopt
);
323 diff_flush(&revs
->diffopt
);
325 * The return code for --no-index imitates diff(1):
326 * 0 = no changes, 1 = changes, else error
328 return revs
->diffopt
.found_changes
;
331 if (read_cache() < 0) {
332 perror("read_cache");
335 return run_diff_files(revs
, options
);
338 * See if work tree has an entity that can be staged. Return 0 if so,
339 * return 1 if not and return -1 if error.
341 static int check_work_tree_entity(const struct cache_entry
*ce
, struct stat
*st
, char *symcache
)
343 if (lstat(ce
->name
, st
) < 0) {
344 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
348 if (has_symlink_leading_path(ce
->name
, symcache
))
350 if (S_ISDIR(st
->st_mode
)) {
351 unsigned char sub
[20];
352 if (resolve_gitlink_ref(ce
->name
, "HEAD", sub
))
358 int run_diff_files(struct rev_info
*revs
, unsigned int option
)
361 int diff_unmerged_stage
= revs
->max_count
;
362 int silent_on_removed
= option
& DIFF_SILENT_ON_REMOVED
;
363 unsigned ce_option
= ((option
& DIFF_RACY_IS_MODIFIED
)
364 ? CE_MATCH_RACY_IS_DIRTY
: 0);
365 char symcache
[PATH_MAX
];
367 if (diff_unmerged_stage
< 0)
368 diff_unmerged_stage
= 2;
371 for (i
= 0; i
< entries
; i
++) {
373 unsigned int oldmode
, newmode
;
374 struct cache_entry
*ce
= active_cache
[i
];
377 if (DIFF_OPT_TST(&revs
->diffopt
, QUIET
) &&
378 DIFF_OPT_TST(&revs
->diffopt
, HAS_CHANGES
))
381 if (!ce_path_match(ce
, revs
->prune_data
))
385 struct combine_diff_path
*dpath
;
386 int num_compare_stages
= 0;
389 path_len
= ce_namelen(ce
);
391 dpath
= xmalloc(combine_diff_path_size(5, path_len
));
392 dpath
->path
= (char *) &(dpath
->parent
[5]);
395 dpath
->len
= path_len
;
396 memcpy(dpath
->path
, ce
->name
, path_len
);
397 dpath
->path
[path_len
] = '\0';
398 hashclr(dpath
->sha1
);
399 memset(&(dpath
->parent
[0]), 0,
400 sizeof(struct combine_diff_parent
)*5);
402 changed
= check_work_tree_entity(ce
, &st
, symcache
);
404 dpath
->mode
= ce_mode_from_stat(ce
, st
.st_mode
);
410 if (silent_on_removed
)
414 while (i
< entries
) {
415 struct cache_entry
*nce
= active_cache
[i
];
418 if (strcmp(ce
->name
, nce
->name
))
421 /* Stage #2 (ours) is the first parent,
422 * stage #3 (theirs) is the second.
424 stage
= ce_stage(nce
);
426 int mode
= nce
->ce_mode
;
427 num_compare_stages
++;
428 hashcpy(dpath
->parent
[stage
-2].sha1
, nce
->sha1
);
429 dpath
->parent
[stage
-2].mode
= ce_mode_from_stat(nce
, mode
);
430 dpath
->parent
[stage
-2].status
=
431 DIFF_STATUS_MODIFIED
;
434 /* diff against the proper unmerged stage */
435 if (stage
== diff_unmerged_stage
)
440 * Compensate for loop update
444 if (revs
->combine_merges
&& num_compare_stages
== 2) {
445 show_combined_diff(dpath
, 2,
446 revs
->dense_combined_merges
,
455 * Show the diff for the 'ce' if we found the one
456 * from the desired stage.
458 diff_unmerge(&revs
->diffopt
, ce
->name
, 0, null_sha1
);
459 if (ce_stage(ce
) != diff_unmerged_stage
)
466 changed
= check_work_tree_entity(ce
, &st
, symcache
);
472 if (silent_on_removed
)
474 diff_addremove(&revs
->diffopt
, '-', ce
->ce_mode
,
475 ce
->sha1
, ce
->name
, NULL
);
478 changed
= ce_match_stat(ce
, &st
, ce_option
);
480 ce_mark_uptodate(ce
);
481 if (!DIFF_OPT_TST(&revs
->diffopt
, FIND_COPIES_HARDER
))
484 oldmode
= ce
->ce_mode
;
485 newmode
= ce_mode_from_stat(ce
, st
.st_mode
);
486 diff_change(&revs
->diffopt
, oldmode
, newmode
,
487 ce
->sha1
, (changed
? null_sha1
: ce
->sha1
),
491 diffcore_std(&revs
->diffopt
);
492 diff_flush(&revs
->diffopt
);
500 struct oneway_unpack_data
{
501 struct rev_info
*revs
;
502 char symcache
[PATH_MAX
];
505 /* A file entry went away or appeared */
506 static void diff_index_show_file(struct rev_info
*revs
,
508 struct cache_entry
*ce
,
509 const unsigned char *sha1
, unsigned int mode
)
511 diff_addremove(&revs
->diffopt
, prefix
[0], mode
,
512 sha1
, ce
->name
, NULL
);
515 static int get_stat_data(struct cache_entry
*ce
,
516 const unsigned char **sha1p
,
518 int cached
, int match_missing
,
519 struct oneway_unpack_data
*cbdata
)
521 const unsigned char *sha1
= ce
->sha1
;
522 unsigned int mode
= ce
->ce_mode
;
527 changed
= check_work_tree_entity(ce
, &st
, cbdata
->symcache
);
538 changed
= ce_match_stat(ce
, &st
, 0);
540 mode
= ce_mode_from_stat(ce
, st
.st_mode
);
550 static void show_new_file(struct oneway_unpack_data
*cbdata
,
551 struct cache_entry
*new,
552 int cached
, int match_missing
)
554 const unsigned char *sha1
;
556 struct rev_info
*revs
= cbdata
->revs
;
559 * New file in the index: it might actually be different in
562 if (get_stat_data(new, &sha1
, &mode
, cached
, match_missing
, cbdata
) < 0)
565 diff_index_show_file(revs
, "+", new, sha1
, mode
);
568 static int show_modified(struct oneway_unpack_data
*cbdata
,
569 struct cache_entry
*old
,
570 struct cache_entry
*new,
572 int cached
, int match_missing
)
574 unsigned int mode
, oldmode
;
575 const unsigned char *sha1
;
576 struct rev_info
*revs
= cbdata
->revs
;
578 if (get_stat_data(new, &sha1
, &mode
, cached
, match_missing
, cbdata
) < 0) {
580 diff_index_show_file(revs
, "-", old
,
581 old
->sha1
, old
->ce_mode
);
585 if (revs
->combine_merges
&& !cached
&&
586 (hashcmp(sha1
, old
->sha1
) || hashcmp(old
->sha1
, new->sha1
))) {
587 struct combine_diff_path
*p
;
588 int pathlen
= ce_namelen(new);
590 p
= xmalloc(combine_diff_path_size(2, pathlen
));
591 p
->path
= (char *) &p
->parent
[2];
594 memcpy(p
->path
, new->name
, pathlen
);
595 p
->path
[pathlen
] = 0;
598 memset(p
->parent
, 0, 2 * sizeof(struct combine_diff_parent
));
599 p
->parent
[0].status
= DIFF_STATUS_MODIFIED
;
600 p
->parent
[0].mode
= new->ce_mode
;
601 hashcpy(p
->parent
[0].sha1
, new->sha1
);
602 p
->parent
[1].status
= DIFF_STATUS_MODIFIED
;
603 p
->parent
[1].mode
= old
->ce_mode
;
604 hashcpy(p
->parent
[1].sha1
, old
->sha1
);
605 show_combined_diff(p
, 2, revs
->dense_combined_merges
, revs
);
610 oldmode
= old
->ce_mode
;
611 if (mode
== oldmode
&& !hashcmp(sha1
, old
->sha1
) &&
612 !DIFF_OPT_TST(&revs
->diffopt
, FIND_COPIES_HARDER
))
615 diff_change(&revs
->diffopt
, oldmode
, mode
,
616 old
->sha1
, sha1
, old
->name
, NULL
);
621 * This turns all merge entries into "stage 3". That guarantees that
622 * when we read in the new tree (into "stage 1"), we won't lose sight
623 * of the fact that we had unmerged entries.
625 static void mark_merge_entries(void)
628 for (i
= 0; i
< active_nr
; i
++) {
629 struct cache_entry
*ce
= active_cache
[i
];
632 ce
->ce_flags
|= CE_STAGEMASK
;
637 * This gets a mix of an existing index and a tree, one pathname entry
638 * at a time. The index entry may be a single stage-0 one, but it could
639 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
640 * give you the position and number of entries in the index).
642 static void do_oneway_diff(struct unpack_trees_options
*o
,
643 struct cache_entry
*idx
,
644 struct cache_entry
*tree
)
646 struct oneway_unpack_data
*cbdata
= o
->unpack_data
;
647 struct rev_info
*revs
= cbdata
->revs
;
648 int match_missing
, cached
;
651 * Backward compatibility wart - "diff-index -m" does
652 * not mean "do not ignore merges", but "match_missing".
654 * But with the revision flag parsing, that's found in
655 * "!revs->ignore_merges".
657 cached
= o
->index_only
;
658 match_missing
= !revs
->ignore_merges
;
660 if (cached
&& idx
&& ce_stage(idx
)) {
662 diff_unmerge(&revs
->diffopt
, idx
->name
, idx
->ce_mode
, idx
->sha1
);
667 * Something added to the tree?
670 show_new_file(cbdata
, idx
, cached
, match_missing
);
675 * Something removed from the tree?
678 diff_index_show_file(revs
, "-", tree
, tree
->sha1
, tree
->ce_mode
);
682 /* Show difference between old and new */
683 show_modified(cbdata
, tree
, idx
, 1, cached
, match_missing
);
686 static inline void skip_same_name(struct cache_entry
*ce
, struct unpack_trees_options
*o
)
688 int len
= ce_namelen(ce
);
689 const struct index_state
*index
= o
->src_index
;
691 while (o
->pos
< index
->cache_nr
) {
692 struct cache_entry
*next
= index
->cache
[o
->pos
];
693 if (len
!= ce_namelen(next
))
695 if (memcmp(ce
->name
, next
->name
, len
))
702 * The unpack_trees() interface is designed for merging, so
703 * the different source entries are designed primarily for
704 * the source trees, with the old index being really mainly
705 * used for being replaced by the result.
707 * For diffing, the index is more important, and we only have a
710 * We're supposed to return how many index entries we want to skip.
712 * This wrapper makes it all more readable, and takes care of all
713 * the fairly complex unpack_trees() semantic requirements, including
714 * the skipping, the path matching, the type conflict cases etc.
716 static int oneway_diff(struct cache_entry
**src
, struct unpack_trees_options
*o
)
718 struct cache_entry
*idx
= src
[0];
719 struct cache_entry
*tree
= src
[1];
720 struct oneway_unpack_data
*cbdata
= o
->unpack_data
;
721 struct rev_info
*revs
= cbdata
->revs
;
723 if (idx
&& ce_stage(idx
))
724 skip_same_name(idx
, o
);
727 * Unpack-trees generates a DF/conflict entry if
728 * there was a directory in the index and a tree
729 * in the tree. From a diff standpoint, that's a
730 * delete of the tree and a create of the file.
732 if (tree
== o
->df_conflict_entry
)
735 if (ce_path_match(idx
? idx
: tree
, revs
->prune_data
))
736 do_oneway_diff(o
, idx
, tree
);
741 int run_diff_index(struct rev_info
*revs
, int cached
)
745 const char *tree_name
;
746 struct unpack_trees_options opts
;
748 struct oneway_unpack_data unpack_cb
;
750 mark_merge_entries();
752 ent
= revs
->pending
.objects
[0].item
;
753 tree_name
= revs
->pending
.objects
[0].name
;
754 tree
= parse_tree_indirect(ent
->sha1
);
756 return error("bad tree object %s", tree_name
);
758 unpack_cb
.revs
= revs
;
759 unpack_cb
.symcache
[0] = '\0';
760 memset(&opts
, 0, sizeof(opts
));
762 opts
.index_only
= cached
;
764 opts
.fn
= oneway_diff
;
765 opts
.unpack_data
= &unpack_cb
;
766 opts
.src_index
= &the_index
;
767 opts
.dst_index
= NULL
;
769 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
770 if (unpack_trees(1, &t
, &opts
))
773 diffcore_std(&revs
->diffopt
);
774 diff_flush(&revs
->diffopt
);
778 int do_diff_cache(const unsigned char *tree_sha1
, struct diff_options
*opt
)
781 struct rev_info revs
;
783 struct cache_entry
**dst
;
784 struct cache_entry
*last
= NULL
;
785 struct unpack_trees_options opts
;
787 struct oneway_unpack_data unpack_cb
;
790 * This is used by git-blame to run diff-cache internally;
791 * it potentially needs to repeatedly run this, so we will
792 * start by removing the higher order entries the last round
796 for (i
= 0; i
< active_nr
; i
++) {
797 struct cache_entry
*ce
= active_cache
[i
];
799 if (last
&& !strcmp(ce
->name
, last
->name
))
801 cache_tree_invalidate_path(active_cache_tree
,
804 ce
->ce_flags
|= CE_REMOVE
;
808 active_nr
= dst
- active_cache
;
810 init_revisions(&revs
, NULL
);
811 revs
.prune_data
= opt
->paths
;
812 tree
= parse_tree_indirect(tree_sha1
);
814 die("bad tree object %s", sha1_to_hex(tree_sha1
));
816 unpack_cb
.revs
= &revs
;
817 unpack_cb
.symcache
[0] = '\0';
818 memset(&opts
, 0, sizeof(opts
));
822 opts
.fn
= oneway_diff
;
823 opts
.unpack_data
= &unpack_cb
;
824 opts
.src_index
= &the_index
;
825 opts
.dst_index
= &the_index
;
827 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
828 if (unpack_trees(1, &t
, &opts
))