Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 4 | #include "cache.h" |
Junio C Hamano | 6fb737b | 2005-07-07 23:58:32 -0700 | [diff] [blame] | 5 | #include "quote.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6 | #include "commit.h" |
Junio C Hamano | 86436c2 | 2005-04-25 18:22:47 -0700 | [diff] [blame] | 7 | #include "diff.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 8 | #include "diffcore.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 9 | #include "revision.h" |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 10 | #include "cache-tree.h" |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 11 | #include "unpack-trees.h" |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 12 | #include "refs.h" |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 13 | #include "submodule.h" |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 14 | #include "dir.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 15 | #include "fsmonitor.h" |
Denton Liu | 177a830 | 2020-09-20 04:22:23 -0700 | [diff] [blame] | 16 | #include "commit-reach.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 17 | |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 18 | /* |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 19 | * diff-files |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 20 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 21 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 22 | /* |
Junio C Hamano | 1392a37 | 2008-05-03 17:04:42 -0700 | [diff] [blame] | 23 | * Has the work tree entity been removed? |
| 24 | * |
| 25 | * Return 1 if it was removed from the work tree, 0 if an entity to be |
| 26 | * compared with the cache entry ce still exists (the latter includes |
| 27 | * the case where a directory that is not a submodule repository |
| 28 | * exists for ce that is a submodule -- it is a submodule that is not |
| 29 | * checked out). Return negative for an error. |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 30 | */ |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 31 | static int check_removed(const struct index_state *istate, const struct cache_entry *ce, struct stat *st) |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 32 | { |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 33 | assert(is_fsmonitor_refreshed(istate)); |
Nipunn Koorapati | 4f3d6d0 | 2021-03-17 21:22:21 +0000 | [diff] [blame] | 34 | if (!(ce->ce_flags & CE_FSMONITOR_VALID) && lstat(ce->name, st) < 0) { |
Junio C Hamano | c705420 | 2017-05-30 09:23:33 +0900 | [diff] [blame] | 35 | if (!is_missing_file_error(errno)) |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 36 | return -1; |
| 37 | return 1; |
| 38 | } |
Kjetil Barvik | 5719989 | 2009-02-09 21:54:06 +0100 | [diff] [blame] | 39 | if (has_symlink_leading_path(ce->name, ce_namelen(ce))) |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 40 | return 1; |
| 41 | if (S_ISDIR(st->st_mode)) { |
brian m. carlson | 1053fe8 | 2017-10-15 22:07:06 +0000 | [diff] [blame] | 42 | struct object_id sub; |
Junio C Hamano | 1392a37 | 2008-05-03 17:04:42 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * If ce is already a gitlink, we can have a plain |
| 46 | * directory (i.e. the submodule is not checked out), |
| 47 | * or a checked out submodule. Either case this is not |
| 48 | * a case where something was removed from the work tree, |
| 49 | * so we will return 0. |
| 50 | * |
| 51 | * Otherwise, if the directory is not a submodule |
| 52 | * repository, that means ce which was a blob turned into |
| 53 | * a directory --- the blob was removed! |
| 54 | */ |
| 55 | if (!S_ISGITLINK(ce->ce_mode) && |
brian m. carlson | a98e610 | 2017-10-15 22:07:07 +0000 | [diff] [blame] | 56 | resolve_gitlink_ref(ce->name, "HEAD", &sub)) |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 57 | return 1; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 61 | |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 62 | /* |
| 63 | * Has a file changed or has a submodule new commits or a dirty work tree? |
| 64 | * |
| 65 | * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES |
| 66 | * option is set, the caller does not only want to know if a submodule is |
| 67 | * modified at all but wants to know all the conditions that are met (new |
| 68 | * commits, untracked content and/or modified content). |
| 69 | */ |
| 70 | static int match_stat_with_submodule(struct diff_options *diffopt, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 71 | const struct cache_entry *ce, |
| 72 | struct stat *st, unsigned ce_option, |
| 73 | unsigned *dirty_submodule) |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 74 | { |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 75 | int changed = ie_match_stat(diffopt->repo->index, ce, st, ce_option); |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 76 | if (S_ISGITLINK(ce->ce_mode)) { |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 77 | struct diff_flags orig_flags = diffopt->flags; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 78 | if (!diffopt->flags.override_submodule_config) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 79 | set_diffopt_flags_from_submodule_config(diffopt, ce->name); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 80 | if (diffopt->flags.ignore_submodules) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 81 | changed = 0; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 82 | else if (!diffopt->flags.ignore_dirty_submodules && |
| 83 | (!changed || diffopt->flags.dirty_submodules)) |
Brandon Williams | 3b69dae | 2017-10-31 11:19:08 -0700 | [diff] [blame] | 84 | *dirty_submodule = is_submodule_modified(ce->name, |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 85 | diffopt->flags.ignore_untracked_in_submodules); |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 86 | diffopt->flags = orig_flags; |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 87 | } |
| 88 | return changed; |
| 89 | } |
| 90 | |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 91 | int run_diff_files(struct rev_info *revs, unsigned int option) |
Junio C Hamano | b46f0b6 | 2005-05-04 01:45:24 -0700 | [diff] [blame] | 92 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 93 | int entries, i; |
| 94 | int diff_unmerged_stage = revs->max_count; |
Junio C Hamano | fb63d7f | 2007-11-09 18:22:52 -0800 | [diff] [blame] | 95 | unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED) |
| 96 | ? CE_MATCH_RACY_IS_DIRTY : 0); |
Nguyễn Thái Ngọc Duy | ca54d9b | 2018-01-27 19:27:56 +0700 | [diff] [blame] | 97 | uint64_t start = getnanotime(); |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 98 | struct index_state *istate = revs->diffopt.repo->index; |
Junio C Hamano | 5c97558 | 2005-05-19 03:32:35 -0700 | [diff] [blame] | 99 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 100 | diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/"); |
| 101 | |
Alex Vandiver | c9052a8 | 2020-10-20 13:40:58 +0000 | [diff] [blame] | 102 | refresh_fsmonitor(istate); |
| 103 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 104 | if (diff_unmerged_stage < 0) |
| 105 | diff_unmerged_stage = 2; |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 106 | entries = istate->cache_nr; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 107 | for (i = 0; i < entries; i++) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 108 | unsigned int oldmode, newmode; |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 109 | struct cache_entry *ce = istate->cache[i]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 110 | int changed; |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 111 | unsigned dirty_submodule = 0; |
Brandon Williams | 55497b8 | 2017-05-30 10:30:48 -0700 | [diff] [blame] | 112 | const struct object_id *old_oid, *new_oid; |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 113 | |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 114 | if (diff_can_quit_early(&revs->diffopt)) |
Junio C Hamano | 822cac0 | 2007-03-14 11:12:51 -0700 | [diff] [blame] | 115 | break; |
| 116 | |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 117 | if (!ce_path_match(istate, ce, &revs->prune_data, NULL)) |
Linus Torvalds | 8676eb4 | 2006-02-26 15:51:24 -0800 | [diff] [blame] | 118 | continue; |
Linus Torvalds | 8676eb4 | 2006-02-26 15:51:24 -0800 | [diff] [blame] | 119 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 120 | if (ce_stage(ce)) { |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 121 | struct combine_diff_path *dpath; |
Junio C Hamano | 095ce95 | 2011-04-22 16:19:27 -0700 | [diff] [blame] | 122 | struct diff_filepair *pair; |
| 123 | unsigned int wt_mode = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 124 | int num_compare_stages = 0; |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 125 | size_t path_len; |
Jeff King | 5304810 | 2014-05-14 18:13:06 -0400 | [diff] [blame] | 126 | struct stat st; |
Linus Torvalds | 8676eb4 | 2006-02-26 15:51:24 -0800 | [diff] [blame] | 127 | |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 128 | path_len = ce_namelen(ce); |
| 129 | |
Junio C Hamano | 4fc970c | 2007-02-25 22:24:47 -0800 | [diff] [blame] | 130 | dpath = xmalloc(combine_diff_path_size(5, path_len)); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 131 | dpath->path = (char *) &(dpath->parent[5]); |
| 132 | |
| 133 | dpath->next = NULL; |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 134 | memcpy(dpath->path, ce->name, path_len); |
| 135 | dpath->path[path_len] = '\0'; |
brian m. carlson | 1ff57c1 | 2015-03-13 23:39:33 +0000 | [diff] [blame] | 136 | oidclr(&dpath->oid); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 137 | memset(&(dpath->parent[0]), 0, |
Junio C Hamano | 4fc970c | 2007-02-25 22:24:47 -0800 | [diff] [blame] | 138 | sizeof(struct combine_diff_parent)*5); |
| 139 | |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 140 | changed = check_removed(istate, ce, &st); |
Junio C Hamano | f58dbf2 | 2008-03-30 17:30:08 -0700 | [diff] [blame] | 141 | if (!changed) |
Junio C Hamano | 095ce95 | 2011-04-22 16:19:27 -0700 | [diff] [blame] | 142 | wt_mode = ce_mode_from_stat(ce, st.st_mode); |
Junio C Hamano | f58dbf2 | 2008-03-30 17:30:08 -0700 | [diff] [blame] | 143 | else { |
| 144 | if (changed < 0) { |
Junio C Hamano | 4fc970c | 2007-02-25 22:24:47 -0800 | [diff] [blame] | 145 | perror(ce->name); |
| 146 | continue; |
| 147 | } |
Junio C Hamano | 095ce95 | 2011-04-22 16:19:27 -0700 | [diff] [blame] | 148 | wt_mode = 0; |
Junio C Hamano | 4fc970c | 2007-02-25 22:24:47 -0800 | [diff] [blame] | 149 | } |
Junio C Hamano | 095ce95 | 2011-04-22 16:19:27 -0700 | [diff] [blame] | 150 | dpath->mode = wt_mode; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 151 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 152 | while (i < entries) { |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 153 | struct cache_entry *nce = istate->cache[i]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 154 | int stage; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 155 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 156 | if (strcmp(ce->name, nce->name)) |
| 157 | break; |
Junio C Hamano | be3cfa8 | 2005-04-26 09:25:05 -0700 | [diff] [blame] | 158 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 159 | /* Stage #2 (ours) is the first parent, |
| 160 | * stage #3 (theirs) is the second. |
| 161 | */ |
| 162 | stage = ce_stage(nce); |
| 163 | if (2 <= stage) { |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 164 | int mode = nce->ce_mode; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 165 | num_compare_stages++; |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 166 | oidcpy(&dpath->parent[stage - 2].oid, |
| 167 | &nce->oid); |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 168 | dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 169 | dpath->parent[stage-2].status = |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 170 | DIFF_STATUS_MODIFIED; |
| 171 | } |
Junio C Hamano | cebff98 | 2006-03-25 23:12:17 -0800 | [diff] [blame] | 172 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 173 | /* diff against the proper unmerged stage */ |
| 174 | if (stage == diff_unmerged_stage) |
| 175 | ce = nce; |
| 176 | i++; |
H. Peter Anvin | 1b1480f | 2005-11-21 14:17:12 -0800 | [diff] [blame] | 177 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 178 | /* |
| 179 | * Compensate for loop update |
| 180 | */ |
| 181 | i--; |
Junio C Hamano | 0e3994f | 2005-06-03 01:37:54 -0700 | [diff] [blame] | 182 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 183 | if (revs->combine_merges && num_compare_stages == 2) { |
Sergey Organov | d01141d | 2020-09-29 14:31:22 +0300 | [diff] [blame] | 184 | show_combined_diff(dpath, 2, revs); |
Florian Forster | b4b1550 | 2006-06-18 17:18:05 +0200 | [diff] [blame] | 185 | free(dpath); |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 186 | continue; |
| 187 | } |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 188 | FREE_AND_NULL(dpath); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * Show the diff for the 'ce' if we found the one |
| 192 | * from the desired stage. |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 193 | */ |
Junio C Hamano | 095ce95 | 2011-04-22 16:19:27 -0700 | [diff] [blame] | 194 | pair = diff_unmerge(&revs->diffopt, ce->name); |
| 195 | if (wt_mode) |
| 196 | pair->two->mode = wt_mode; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 197 | if (ce_stage(ce) != diff_unmerged_stage) |
| 198 | continue; |
| 199 | } |
| 200 | |
Junio C Hamano | 125fd98 | 2010-01-24 00:10:20 -0800 | [diff] [blame] | 201 | if (ce_uptodate(ce) || ce_skip_worktree(ce)) |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 202 | continue; |
Junio C Hamano | f58dbf2 | 2008-03-30 17:30:08 -0700 | [diff] [blame] | 203 | |
Alex Vandiver | c9052a8 | 2020-10-20 13:40:58 +0000 | [diff] [blame] | 204 | /* |
| 205 | * When CE_VALID is set (via "update-index --assume-unchanged" |
| 206 | * or via adding paths while core.ignorestat is set to true), |
| 207 | * the user has promised that the working tree file for that |
| 208 | * path will not be modified. When CE_FSMONITOR_VALID is true, |
| 209 | * the fsmonitor knows that the path hasn't been modified since |
| 210 | * we refreshed the cached stat information. In either case, |
| 211 | * we do not have to stat to see if the path has been removed |
| 212 | * or modified. |
| 213 | */ |
| 214 | if (ce->ce_flags & (CE_VALID | CE_FSMONITOR_VALID)) { |
Jeff King | 5304810 | 2014-05-14 18:13:06 -0400 | [diff] [blame] | 215 | changed = 0; |
| 216 | newmode = ce->ce_mode; |
| 217 | } else { |
| 218 | struct stat st; |
| 219 | |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 220 | changed = check_removed(istate, ce, &st); |
Jeff King | 5304810 | 2014-05-14 18:13:06 -0400 | [diff] [blame] | 221 | if (changed) { |
| 222 | if (changed < 0) { |
| 223 | perror(ce->name); |
| 224 | continue; |
| 225 | } |
| 226 | diff_addremove(&revs->diffopt, '-', ce->ce_mode, |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 227 | &ce->oid, |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 228 | !is_null_oid(&ce->oid), |
Jeff King | 5304810 | 2014-05-14 18:13:06 -0400 | [diff] [blame] | 229 | ce->name, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 230 | continue; |
Nguyễn Thái Ngọc Duy | 425a28e | 2016-10-24 17:42:19 +0700 | [diff] [blame] | 231 | } else if (revs->diffopt.ita_invisible_in_index && |
| 232 | ce_intent_to_add(ce)) { |
Raymond E. Pasco | cb0dd22 | 2020-08-08 03:53:23 -0400 | [diff] [blame] | 233 | newmode = ce_mode_from_stat(ce, st.st_mode); |
| 234 | diff_addremove(&revs->diffopt, '+', newmode, |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 235 | null_oid(), 0, ce->name, 0); |
Nguyễn Thái Ngọc Duy | 425a28e | 2016-10-24 17:42:19 +0700 | [diff] [blame] | 236 | continue; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 237 | } |
Jeff King | 5304810 | 2014-05-14 18:13:06 -0400 | [diff] [blame] | 238 | |
| 239 | changed = match_stat_with_submodule(&revs->diffopt, ce, &st, |
| 240 | ce_option, &dirty_submodule); |
| 241 | newmode = ce_mode_from_stat(ce, st.st_mode); |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 242 | } |
Jeff King | 5304810 | 2014-05-14 18:13:06 -0400 | [diff] [blame] | 243 | |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 244 | if (!changed && !dirty_submodule) { |
Junio C Hamano | 8fa2960 | 2008-03-30 12:39:25 -0700 | [diff] [blame] | 245 | ce_mark_uptodate(ce); |
Johannes Schindelin | b5a8169 | 2019-05-24 05:23:48 -0700 | [diff] [blame] | 246 | mark_fsmonitor_valid(istate, ce); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 247 | if (!revs->diffopt.flags.find_copies_harder) |
Junio C Hamano | 8fa2960 | 2008-03-30 12:39:25 -0700 | [diff] [blame] | 248 | continue; |
| 249 | } |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 250 | oldmode = ce->ce_mode; |
Brandon Williams | 55497b8 | 2017-05-30 10:30:48 -0700 | [diff] [blame] | 251 | old_oid = &ce->oid; |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 252 | new_oid = changed ? null_oid() : &ce->oid; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 253 | diff_change(&revs->diffopt, oldmode, newmode, |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 254 | old_oid, new_oid, |
Brandon Williams | 55497b8 | 2017-05-30 10:30:48 -0700 | [diff] [blame] | 255 | !is_null_oid(old_oid), |
| 256 | !is_null_oid(new_oid), |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 257 | ce->name, 0, dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 258 | |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 259 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 260 | diffcore_std(&revs->diffopt); |
| 261 | diff_flush(&revs->diffopt); |
Nguyễn Thái Ngọc Duy | ca54d9b | 2018-01-27 19:27:56 +0700 | [diff] [blame] | 262 | trace_performance_since(start, "diff-files"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 263 | return 0; |
Junio C Hamano | bceafe7 | 2005-05-23 18:14:03 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 266 | /* |
| 267 | * diff-index |
| 268 | */ |
| 269 | |
| 270 | /* A file entry went away or appeared */ |
| 271 | static void diff_index_show_file(struct rev_info *revs, |
| 272 | const char *prefix, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 273 | const struct cache_entry *ce, |
Brandon Williams | fcf2cfb | 2017-05-30 10:30:46 -0700 | [diff] [blame] | 274 | const struct object_id *oid, int oid_valid, |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 275 | unsigned int mode, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 276 | unsigned dirty_submodule) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 277 | { |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 278 | diff_addremove(&revs->diffopt, prefix[0], mode, |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 279 | oid, oid_valid, ce->name, dirty_submodule); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 282 | static int get_stat_data(const struct index_state *istate, |
| 283 | const struct cache_entry *ce, |
Brandon Williams | 362d765 | 2017-05-30 10:30:45 -0700 | [diff] [blame] | 284 | const struct object_id **oidp, |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 285 | unsigned int *modep, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 286 | int cached, int match_missing, |
Jens Lehmann | 4d34477 | 2010-01-23 17:37:26 +0100 | [diff] [blame] | 287 | unsigned *dirty_submodule, struct diff_options *diffopt) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 288 | { |
Brandon Williams | 362d765 | 2017-05-30 10:30:45 -0700 | [diff] [blame] | 289 | const struct object_id *oid = &ce->oid; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 290 | unsigned int mode = ce->ce_mode; |
| 291 | |
Linus Torvalds | 658dd48 | 2009-05-09 14:57:30 -0700 | [diff] [blame] | 292 | if (!cached && !ce_uptodate(ce)) { |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 293 | int changed; |
| 294 | struct stat st; |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 295 | changed = check_removed(istate, ce, &st); |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 296 | if (changed < 0) |
| 297 | return -1; |
| 298 | else if (changed) { |
| 299 | if (match_missing) { |
Brandon Williams | 362d765 | 2017-05-30 10:30:45 -0700 | [diff] [blame] | 300 | *oidp = oid; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 301 | *modep = mode; |
| 302 | return 0; |
| 303 | } |
| 304 | return -1; |
| 305 | } |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 306 | changed = match_stat_with_submodule(diffopt, ce, &st, |
| 307 | 0, dirty_submodule); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 308 | if (changed) { |
Junio C Hamano | 185c975 | 2007-02-16 22:43:48 -0800 | [diff] [blame] | 309 | mode = ce_mode_from_stat(ce, st.st_mode); |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 310 | oid = null_oid(); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Brandon Williams | 362d765 | 2017-05-30 10:30:45 -0700 | [diff] [blame] | 314 | *oidp = oid; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 315 | *modep = mode; |
| 316 | return 0; |
| 317 | } |
| 318 | |
Kjetil Barvik | ff7e6aa | 2009-01-11 19:36:42 +0100 | [diff] [blame] | 319 | static void show_new_file(struct rev_info *revs, |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 320 | const struct cache_entry *new_file, |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 321 | int cached, int match_missing) |
| 322 | { |
Brandon Williams | 362d765 | 2017-05-30 10:30:45 -0700 | [diff] [blame] | 323 | const struct object_id *oid; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 324 | unsigned int mode; |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 325 | unsigned dirty_submodule = 0; |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 326 | struct index_state *istate = revs->diffopt.repo->index; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 327 | |
Junio C Hamano | 948dd34 | 2008-03-30 17:29:48 -0700 | [diff] [blame] | 328 | /* |
| 329 | * New file in the index: it might actually be different in |
Carlos Martín Nieto | f7d650c | 2011-09-20 22:25:57 +0200 | [diff] [blame] | 330 | * the working tree. |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 331 | */ |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 332 | if (get_stat_data(istate, new_file, &oid, &mode, cached, match_missing, |
Jens Lehmann | 4d34477 | 2010-01-23 17:37:26 +0100 | [diff] [blame] | 333 | &dirty_submodule, &revs->diffopt) < 0) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 334 | return; |
| 335 | |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 336 | diff_index_show_file(revs, "+", new_file, oid, !is_null_oid(oid), mode, dirty_submodule); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Kjetil Barvik | ff7e6aa | 2009-01-11 19:36:42 +0100 | [diff] [blame] | 339 | static int show_modified(struct rev_info *revs, |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 340 | const struct cache_entry *old_entry, |
| 341 | const struct cache_entry *new_entry, |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 342 | int report_missing, |
| 343 | int cached, int match_missing) |
| 344 | { |
| 345 | unsigned int mode, oldmode; |
Brandon Williams | 362d765 | 2017-05-30 10:30:45 -0700 | [diff] [blame] | 346 | const struct object_id *oid; |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 347 | unsigned dirty_submodule = 0; |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 348 | struct index_state *istate = revs->diffopt.repo->index; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 349 | |
Nipunn Koorapati | 0ec9949 | 2021-03-17 21:22:22 +0000 | [diff] [blame] | 350 | if (get_stat_data(istate, new_entry, &oid, &mode, cached, match_missing, |
Jens Lehmann | 4d34477 | 2010-01-23 17:37:26 +0100 | [diff] [blame] | 351 | &dirty_submodule, &revs->diffopt) < 0) { |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 352 | if (report_missing) |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 353 | diff_index_show_file(revs, "-", old_entry, |
| 354 | &old_entry->oid, 1, old_entry->ce_mode, |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 355 | 0); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 356 | return -1; |
| 357 | } |
| 358 | |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 359 | if (revs->combine_merges && !cached && |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 360 | (!oideq(oid, &old_entry->oid) || !oideq(&old_entry->oid, &new_entry->oid))) { |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 361 | struct combine_diff_path *p; |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 362 | int pathlen = ce_namelen(new_entry); |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 363 | |
| 364 | p = xmalloc(combine_diff_path_size(2, pathlen)); |
| 365 | p->path = (char *) &p->parent[2]; |
| 366 | p->next = NULL; |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 367 | memcpy(p->path, new_entry->name, pathlen); |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 368 | p->path[pathlen] = 0; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 369 | p->mode = mode; |
brian m. carlson | 1ff57c1 | 2015-03-13 23:39:33 +0000 | [diff] [blame] | 370 | oidclr(&p->oid); |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 371 | memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent)); |
| 372 | p->parent[0].status = DIFF_STATUS_MODIFIED; |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 373 | p->parent[0].mode = new_entry->ce_mode; |
| 374 | oidcpy(&p->parent[0].oid, &new_entry->oid); |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 375 | p->parent[1].status = DIFF_STATUS_MODIFIED; |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 376 | p->parent[1].mode = old_entry->ce_mode; |
| 377 | oidcpy(&p->parent[1].oid, &old_entry->oid); |
Sergey Organov | d01141d | 2020-09-29 14:31:22 +0300 | [diff] [blame] | 378 | show_combined_diff(p, 2, revs); |
Paul Mackerras | cb2b9f5 | 2006-09-04 21:38:40 +1000 | [diff] [blame] | 379 | free(p); |
| 380 | return 0; |
| 381 | } |
| 382 | |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 383 | oldmode = old_entry->ce_mode; |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 384 | if (mode == oldmode && oideq(oid, &old_entry->oid) && !dirty_submodule && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 385 | !revs->diffopt.flags.find_copies_harder) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 388 | diff_change(&revs->diffopt, oldmode, mode, |
Brandon Williams | e36ede2 | 2018-02-14 10:59:38 -0800 | [diff] [blame] | 389 | &old_entry->oid, oid, 1, !is_null_oid(oid), |
| 390 | old_entry->name, 0, dirty_submodule); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 394 | /* |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 395 | * This gets a mix of an existing index and a tree, one pathname entry |
| 396 | * at a time. The index entry may be a single stage-0 one, but it could |
| 397 | * also be multiple unmerged entries (in which case idx_pos/idx_nr will |
| 398 | * give you the position and number of entries in the index). |
| 399 | */ |
| 400 | static void do_oneway_diff(struct unpack_trees_options *o, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 401 | const struct cache_entry *idx, |
| 402 | const struct cache_entry *tree) |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 403 | { |
Kjetil Barvik | ff7e6aa | 2009-01-11 19:36:42 +0100 | [diff] [blame] | 404 | struct rev_info *revs = o->unpack_data; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 405 | int match_missing, cached; |
Junio C Hamano | 5c21ac0 | 2006-04-22 03:58:04 -0700 | [diff] [blame] | 406 | |
Nguyễn Thái Ngọc Duy | ba4e356 | 2018-05-26 14:08:43 +0200 | [diff] [blame] | 407 | /* |
| 408 | * i-t-a entries do not actually exist in the index (if we're |
| 409 | * looking at its content) |
| 410 | */ |
| 411 | if (o->index_only && |
| 412 | revs->diffopt.ita_invisible_in_index && |
Nguyễn Thái Ngọc Duy | 425a28e | 2016-10-24 17:42:19 +0700 | [diff] [blame] | 413 | idx && ce_intent_to_add(idx)) { |
| 414 | idx = NULL; |
| 415 | if (!tree) |
| 416 | return; /* nothing to diff.. */ |
| 417 | } |
| 418 | |
Nguyễn Thái Ngọc Duy | 540e694 | 2009-08-11 22:43:59 +0700 | [diff] [blame] | 419 | /* if the entry is not checked out, don't examine work tree */ |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 420 | cached = o->index_only || |
| 421 | (idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx))); |
Sergey Organov | 572fc9a | 2020-08-31 23:14:22 +0300 | [diff] [blame] | 422 | |
| 423 | match_missing = revs->match_missing; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 424 | |
| 425 | if (cached && idx && ce_stage(idx)) { |
Junio C Hamano | fa7b290 | 2011-04-22 16:05:58 -0700 | [diff] [blame] | 426 | struct diff_filepair *pair; |
| 427 | pair = diff_unmerge(&revs->diffopt, idx->name); |
Junio C Hamano | ff00b68 | 2011-07-13 21:36:29 -0700 | [diff] [blame] | 428 | if (tree) |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 429 | fill_filespec(pair->one, &tree->oid, 1, |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 430 | tree->ce_mode); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Something added to the tree? |
| 436 | */ |
| 437 | if (!tree) { |
Kjetil Barvik | ff7e6aa | 2009-01-11 19:36:42 +0100 | [diff] [blame] | 438 | show_new_file(revs, idx, cached, match_missing); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Something removed from the tree? |
| 444 | */ |
| 445 | if (!idx) { |
Brandon Williams | fcf2cfb | 2017-05-30 10:30:46 -0700 | [diff] [blame] | 446 | diff_index_show_file(revs, "-", tree, &tree->oid, 1, |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 447 | tree->ce_mode, 0); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
| 451 | /* Show difference between old and new */ |
Kjetil Barvik | ff7e6aa | 2009-01-11 19:36:42 +0100 | [diff] [blame] | 452 | show_modified(revs, tree, idx, 1, cached, match_missing); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 456 | * The unpack_trees() interface is designed for merging, so |
| 457 | * the different source entries are designed primarily for |
| 458 | * the source trees, with the old index being really mainly |
| 459 | * used for being replaced by the result. |
| 460 | * |
| 461 | * For diffing, the index is more important, and we only have a |
| 462 | * single tree. |
| 463 | * |
Junio C Hamano | da8ba5e | 2009-09-17 22:12:17 -0700 | [diff] [blame] | 464 | * We're supposed to advance o->pos to skip what we have already processed. |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 465 | * |
| 466 | * This wrapper makes it all more readable, and takes care of all |
| 467 | * the fairly complex unpack_trees() semantic requirements, including |
| 468 | * the skipping, the path matching, the type conflict cases etc. |
| 469 | */ |
René Scharfe | 5828e83 | 2013-06-02 17:46:56 +0200 | [diff] [blame] | 470 | static int oneway_diff(const struct cache_entry * const *src, |
| 471 | struct unpack_trees_options *o) |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 472 | { |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 473 | const struct cache_entry *idx = src[0]; |
| 474 | const struct cache_entry *tree = src[1]; |
Kjetil Barvik | ff7e6aa | 2009-01-11 19:36:42 +0100 | [diff] [blame] | 475 | struct rev_info *revs = o->unpack_data; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 476 | |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 477 | /* |
| 478 | * Unpack-trees generates a DF/conflict entry if |
| 479 | * there was a directory in the index and a tree |
| 480 | * in the tree. From a diff standpoint, that's a |
| 481 | * delete of the tree and a create of the file. |
| 482 | */ |
| 483 | if (tree == o->df_conflict_entry) |
| 484 | tree = NULL; |
| 485 | |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 486 | if (ce_path_match(revs->diffopt.repo->index, |
| 487 | idx ? idx : tree, |
| 488 | &revs->prune_data, NULL)) { |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 489 | do_oneway_diff(o, idx, tree); |
Junio C Hamano | b419482 | 2011-05-31 10:06:44 -0700 | [diff] [blame] | 490 | if (diff_can_quit_early(&revs->diffopt)) { |
| 491 | o->exiting_early = 1; |
| 492 | return -1; |
| 493 | } |
| 494 | } |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 495 | |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 496 | return 0; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 499 | static int diff_cache(struct rev_info *revs, |
brian m. carlson | 944cffb | 2017-05-06 22:10:35 +0000 | [diff] [blame] | 500 | const struct object_id *tree_oid, |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 501 | const char *tree_name, |
| 502 | int cached) |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 503 | { |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 504 | struct tree *tree; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 505 | struct tree_desc t; |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 506 | struct unpack_trees_options opts; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 507 | |
brian m. carlson | a9dbc17 | 2017-05-06 22:10:37 +0000 | [diff] [blame] | 508 | tree = parse_tree_indirect(tree_oid); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 509 | if (!tree) |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 510 | return error("bad tree object %s", |
brian m. carlson | 944cffb | 2017-05-06 22:10:35 +0000 | [diff] [blame] | 511 | tree_name ? tree_name : oid_to_hex(tree_oid)); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 512 | memset(&opts, 0, sizeof(opts)); |
| 513 | opts.head_idx = 1; |
| 514 | opts.index_only = cached; |
Junio C Hamano | a0919ce | 2009-05-22 23:14:25 -0700 | [diff] [blame] | 515 | opts.diff_index_cached = (cached && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 516 | !revs->diffopt.flags.find_copies_harder); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 517 | opts.merge = 1; |
| 518 | opts.fn = oneway_diff; |
Kjetil Barvik | ff7e6aa | 2009-01-11 19:36:42 +0100 | [diff] [blame] | 519 | opts.unpack_data = revs; |
Nguyễn Thái Ngọc Duy | 5adbb40 | 2018-09-21 17:57:26 +0200 | [diff] [blame] | 520 | opts.src_index = revs->diffopt.repo->index; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 521 | opts.dst_index = NULL; |
Junio C Hamano | 2f88c19 | 2011-08-29 13:34:08 -0700 | [diff] [blame] | 522 | opts.pathspec = &revs->diffopt.pathspec; |
Nguyen Thai Ngoc Duy | 4838237 | 2012-01-15 17:03:27 +0700 | [diff] [blame] | 523 | opts.pathspec->recursive = 1; |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 524 | |
| 525 | init_tree_desc(&t, tree->buffer, tree->size); |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 526 | return unpack_trees(1, &t, &opts); |
| 527 | } |
| 528 | |
Denton Liu | 177a830 | 2020-09-20 04:22:23 -0700 | [diff] [blame] | 529 | void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb) |
| 530 | { |
| 531 | int i; |
| 532 | struct commit *mb_child[2] = {0}; |
| 533 | struct commit_list *merge_bases; |
| 534 | |
| 535 | for (i = 0; i < revs->pending.nr; i++) { |
| 536 | struct object *obj = revs->pending.objects[i].item; |
| 537 | if (obj->flags) |
| 538 | die(_("--merge-base does not work with ranges")); |
| 539 | if (obj->type != OBJ_COMMIT) |
| 540 | die(_("--merge-base only works with commits")); |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * This check must go after the for loop above because A...B |
| 545 | * ranges produce three pending commits, resulting in a |
| 546 | * misleading error message. |
| 547 | */ |
| 548 | if (revs->pending.nr < 1 || revs->pending.nr > 2) |
| 549 | BUG("unexpected revs->pending.nr: %d", revs->pending.nr); |
| 550 | |
| 551 | for (i = 0; i < revs->pending.nr; i++) |
| 552 | mb_child[i] = lookup_commit_reference(the_repository, &revs->pending.objects[i].item->oid); |
| 553 | if (revs->pending.nr == 1) { |
| 554 | struct object_id oid; |
| 555 | |
| 556 | if (get_oid("HEAD", &oid)) |
| 557 | die(_("unable to get HEAD")); |
| 558 | |
| 559 | mb_child[1] = lookup_commit_reference(the_repository, &oid); |
| 560 | } |
| 561 | |
| 562 | merge_bases = repo_get_merge_bases(the_repository, mb_child[0], mb_child[1]); |
| 563 | if (!merge_bases) |
| 564 | die(_("no merge base found")); |
| 565 | if (merge_bases->next) |
| 566 | die(_("multiple merge bases found")); |
| 567 | |
| 568 | oidcpy(mb, &merge_bases->item->object.oid); |
| 569 | |
| 570 | free_commit_list(merge_bases); |
| 571 | } |
| 572 | |
Denton Liu | 4c3fe82 | 2020-09-20 04:22:22 -0700 | [diff] [blame] | 573 | int run_diff_index(struct rev_info *revs, unsigned int option) |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 574 | { |
| 575 | struct object_array_entry *ent; |
Denton Liu | 4c3fe82 | 2020-09-20 04:22:22 -0700 | [diff] [blame] | 576 | int cached = !!(option & DIFF_INDEX_CACHED); |
Denton Liu | 0f5a1d4 | 2020-09-20 04:22:25 -0700 | [diff] [blame] | 577 | int merge_base = !!(option & DIFF_INDEX_MERGE_BASE); |
| 578 | struct object_id oid; |
| 579 | const char *name; |
| 580 | char merge_base_hex[GIT_MAX_HEXSZ + 1]; |
Nipunn Koorapati | 4f3d6d0 | 2021-03-17 21:22:21 +0000 | [diff] [blame] | 581 | struct index_state *istate = revs->diffopt.repo->index; |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 582 | |
Jeff King | 3506dc9 | 2018-07-11 10:14:06 -0400 | [diff] [blame] | 583 | if (revs->pending.nr != 1) |
| 584 | BUG("run_diff_index must be passed exactly one tree"); |
| 585 | |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 586 | trace_performance_enter(); |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 587 | ent = revs->pending.objects; |
Denton Liu | 0f5a1d4 | 2020-09-20 04:22:25 -0700 | [diff] [blame] | 588 | |
Nipunn Koorapati | 4f3d6d0 | 2021-03-17 21:22:21 +0000 | [diff] [blame] | 589 | refresh_fsmonitor(istate); |
| 590 | |
Denton Liu | 0f5a1d4 | 2020-09-20 04:22:25 -0700 | [diff] [blame] | 591 | if (merge_base) { |
| 592 | diff_get_merge_base(revs, &oid); |
| 593 | name = oid_to_hex_r(merge_base_hex, &oid); |
| 594 | } else { |
| 595 | oidcpy(&oid, &ent->item->oid); |
| 596 | name = ent->name; |
| 597 | } |
| 598 | |
| 599 | if (diff_cache(revs, &oid, name, cached)) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 600 | exit(128); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 601 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 602 | diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/"); |
Jeff King | 784c0da | 2019-02-14 00:48:03 -0500 | [diff] [blame] | 603 | diffcore_fix_diff_index(); |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 604 | diffcore_std(&revs->diffopt); |
| 605 | diff_flush(&revs->diffopt); |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 606 | trace_performance_leave("diff-index"); |
Linus Torvalds | d1f2d7e | 2008-01-19 17:27:12 -0800 | [diff] [blame] | 607 | return 0; |
Junio C Hamano | e09ad6e | 2006-04-22 02:43:00 -0700 | [diff] [blame] | 608 | } |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 609 | |
brian m. carlson | 944cffb | 2017-05-06 22:10:35 +0000 | [diff] [blame] | 610 | int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt) |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 611 | { |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 612 | struct rev_info revs; |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 613 | |
Nguyễn Thái Ngọc Duy | ffc00a4 | 2018-11-10 06:49:04 +0100 | [diff] [blame] | 614 | repo_init_revisions(opt->repo, &revs, NULL); |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 615 | copy_pathspec(&revs.prune_data, &opt->pathspec); |
René Scharfe | d44e526 | 2020-11-14 19:37:03 +0100 | [diff] [blame] | 616 | diff_setup_done(&revs.diffopt); |
Junio C Hamano | bf979c0 | 2011-07-13 18:57:42 -0700 | [diff] [blame] | 617 | revs.diffopt = *opt; |
Johannes Schindelin | 204ce97 | 2008-01-20 15:19:56 +0000 | [diff] [blame] | 618 | |
brian m. carlson | 944cffb | 2017-05-06 22:10:35 +0000 | [diff] [blame] | 619 | if (diff_cache(&revs, tree_oid, NULL, 1)) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 620 | exit(128); |
René Scharfe | d44e526 | 2020-11-14 19:37:03 +0100 | [diff] [blame] | 621 | clear_pathspec(&revs.prune_data); |
Johannes Schindelin | 204ce97 | 2008-01-20 15:19:56 +0000 | [diff] [blame] | 622 | return 0; |
Junio C Hamano | 1cfe773 | 2007-01-30 01:11:08 -0800 | [diff] [blame] | 623 | } |
Stephan Beyer | 75f3ff2 | 2009-02-10 15:30:35 +0100 | [diff] [blame] | 624 | |
Nguyễn Thái Ngọc Duy | ffc00a4 | 2018-11-10 06:49:04 +0100 | [diff] [blame] | 625 | int index_differs_from(struct repository *r, |
| 626 | const char *def, const struct diff_flags *flags, |
Nguyễn Thái Ngọc Duy | 018ec3c | 2016-10-24 17:42:21 +0700 | [diff] [blame] | 627 | int ita_invisible_in_index) |
Stephan Beyer | 75f3ff2 | 2009-02-10 15:30:35 +0100 | [diff] [blame] | 628 | { |
| 629 | struct rev_info rev; |
Junio C Hamano | 32962c9 | 2010-03-08 22:58:09 -0800 | [diff] [blame] | 630 | struct setup_revision_opt opt; |
Stephan Beyer | 75f3ff2 | 2009-02-10 15:30:35 +0100 | [diff] [blame] | 631 | |
Nguyễn Thái Ngọc Duy | ffc00a4 | 2018-11-10 06:49:04 +0100 | [diff] [blame] | 632 | repo_init_revisions(r, &rev, NULL); |
Junio C Hamano | 32962c9 | 2010-03-08 22:58:09 -0800 | [diff] [blame] | 633 | memset(&opt, 0, sizeof(opt)); |
| 634 | opt.def = def; |
| 635 | setup_revisions(0, NULL, &rev, &opt); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 636 | rev.diffopt.flags.quick = 1; |
| 637 | rev.diffopt.flags.exit_with_status = 1; |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 638 | if (flags) |
| 639 | diff_flags_or(&rev.diffopt.flags, flags); |
Nguyễn Thái Ngọc Duy | 018ec3c | 2016-10-24 17:42:21 +0700 | [diff] [blame] | 640 | rev.diffopt.ita_invisible_in_index = ita_invisible_in_index; |
Stephan Beyer | 75f3ff2 | 2009-02-10 15:30:35 +0100 | [diff] [blame] | 641 | run_diff_index(&rev, 1); |
Martin Ågren | dcb572a | 2017-09-23 01:34:52 +0200 | [diff] [blame] | 642 | object_array_clear(&rev.pending); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 643 | return (rev.diffopt.flags.has_changes != 0); |
Stephan Beyer | 75f3ff2 | 2009-02-10 15:30:35 +0100 | [diff] [blame] | 644 | } |
Eric Sunshine | cdffbdc | 2020-09-08 03:16:08 -0400 | [diff] [blame] | 645 | |
| 646 | static struct strbuf *idiff_prefix_cb(struct diff_options *opt, void *data) |
| 647 | { |
| 648 | return data; |
| 649 | } |
| 650 | |
Eric Sunshine | 72a7239 | 2020-09-08 03:16:09 -0400 | [diff] [blame] | 651 | void show_interdiff(const struct object_id *oid1, const struct object_id *oid2, |
| 652 | int indent, struct diff_options *diffopt) |
Eric Sunshine | cdffbdc | 2020-09-08 03:16:08 -0400 | [diff] [blame] | 653 | { |
| 654 | struct diff_options opts; |
| 655 | struct strbuf prefix = STRBUF_INIT; |
| 656 | |
Eric Sunshine | 72a7239 | 2020-09-08 03:16:09 -0400 | [diff] [blame] | 657 | memcpy(&opts, diffopt, sizeof(opts)); |
Eric Sunshine | cdffbdc | 2020-09-08 03:16:08 -0400 | [diff] [blame] | 658 | opts.output_format = DIFF_FORMAT_PATCH; |
| 659 | opts.output_prefix = idiff_prefix_cb; |
| 660 | strbuf_addchars(&prefix, ' ', indent); |
| 661 | opts.output_prefix_data = &prefix; |
| 662 | diff_setup_done(&opts); |
| 663 | |
Eric Sunshine | 72a7239 | 2020-09-08 03:16:09 -0400 | [diff] [blame] | 664 | diff_tree_oid(oid1, oid2, "", &opts); |
Eric Sunshine | cdffbdc | 2020-09-08 03:16:08 -0400 | [diff] [blame] | 665 | diffcore_std(&opts); |
| 666 | diff_flush(&opts); |
| 667 | |
| 668 | strbuf_release(&prefix); |
| 669 | } |