| Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2 | #include "config.h" |
| Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 | [diff] [blame] | 3 | #include "environment.h" |
| Elijah Newren | f394e09 | 2023-03-21 06:25:54 | [diff] [blame] | 4 | #include "gettext.h" |
| Elijah Newren | 41771fa | 2023-02-24 00:09:27 | [diff] [blame] | 5 | #include "hex.h" |
| Elijah Newren | dabab1d | 2023-04-11 07:41:49 | [diff] [blame] | 6 | #include "object-name.h" |
| Elijah Newren | 87bed17 | 2023-04-11 07:41:53 | [diff] [blame] | 7 | #include "object-file.h" |
| Elijah Newren | a034e91 | 2023-05-16 06:34:06 | [diff] [blame] | 8 | #include "object-store-ll.h" |
| Karthik Nayak | 9830926 | 2023-10-27 07:59:29 | [diff] [blame] | 9 | #include "oidset.h" |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 10 | #include "tag.h" |
| 11 | #include "blob.h" |
| 12 | #include "tree.h" |
| 13 | #include "commit.h" |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 14 | #include "diff.h" |
| Sergey Organov | a37eec6 | 2020-12-21 15:19:33 | [diff] [blame] | 15 | #include "diff-merges.h" |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 16 | #include "refs.h" |
| 17 | #include "revision.h" |
| Stefan Beller | 23a3f0c | 2018-04-12 00:21:09 | [diff] [blame] | 18 | #include "repository.h" |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 19 | #include "graph.h" |
| Junio C Hamano | 8ecae9b | 2006-09-17 22:43:40 | [diff] [blame] | 20 | #include "grep.h" |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 21 | #include "reflog-walk.h" |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 22 | #include "patch-ids.h" |
| Junio C Hamano | f35f560 | 2008-04-03 09:12:06 | [diff] [blame] | 23 | #include "decorate.h" |
| Thomas Rast | 894a9d3 | 2010-03-12 17:04:26 | [diff] [blame] | 24 | #include "string-list.h" |
| Thomas Rast | 12da1d1 | 2013-03-28 16:47:32 | [diff] [blame] | 25 | #include "line-log.h" |
| Antoine Pelisse | d72fbe8 | 2013-01-05 21:26:45 | [diff] [blame] | 26 | #include "mailmap.h" |
| Thomas Rast | 53d00b3 | 2013-07-31 20:13:20 | [diff] [blame] | 27 | #include "commit-slab.h" |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 28 | #include "cache-tree.h" |
| Antoine Delaite | cb46d63 | 2015-06-29 15:40:30 | [diff] [blame] | 29 | #include "bisect.h" |
| Jonathan Tan | 150e300 | 2017-08-18 22:20:36 | [diff] [blame] | 30 | #include "packfile.h" |
| Nguyễn Thái Ngọc Duy | be489d0 | 2017-08-23 12:36:52 | [diff] [blame] | 31 | #include "worktree.h" |
| Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 | [diff] [blame] | 32 | #include "read-cache.h" |
| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 33 | #include "setup.h" |
| Elijah Newren | baf889c | 2023-05-16 06:33:51 | [diff] [blame] | 34 | #include "sparse-index.h" |
| Jeff King | dbbcd44 | 2020-07-28 20:23:39 | [diff] [blame] | 35 | #include "strvec.h" |
| Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 | [diff] [blame] | 36 | #include "trace2.h" |
| Derrick Stolee | 6404355 | 2018-07-20 16:33:04 | [diff] [blame] | 37 | #include "commit-reach.h" |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 38 | #include "commit-graph.h" |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 39 | #include "prio-queue.h" |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 40 | #include "hashmap.h" |
| Ævar Arnfjörð Bjarmason | 4457018 | 2019-06-27 23:39:05 | [diff] [blame] | 41 | #include "utf8.h" |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 42 | #include "bloom.h" |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 43 | #include "json-writer.h" |
| Derrick Stolee | c4ea513 | 2022-03-09 16:01:40 | [diff] [blame] | 44 | #include "list-objects-filter-options.h" |
| Junio C Hamano | 5a5ea14 | 2022-06-09 23:44:20 | [diff] [blame] | 45 | #include "resolve-undo.h" |
| SZEDER Gábor | 49fd551 | 2023-03-19 16:27:11 | [diff] [blame] | 46 | #include "parse-options.h" |
| Elijah Newren | dd77d58 | 2023-05-16 06:34:03 | [diff] [blame] | 47 | #include "wildmatch.h" |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 48 | |
| Linus Torvalds | cdcefbc | 2007-11-03 18:11:10 | [diff] [blame] | 49 | volatile show_early_output_fn_t show_early_output; |
| 50 | |
| Antoine Delaite | cb46d63 | 2015-06-29 15:40:30 | [diff] [blame] | 51 | static const char *term_bad; |
| 52 | static const char *term_good; |
| 53 | |
| Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 05:28:24 | [diff] [blame] | 54 | implement_shared_commit_slab(revision_sources, char *); |
| 55 | |
| SZEDER Gábor | 3cb9d2b | 2020-05-11 11:56:17 | [diff] [blame] | 56 | static inline int want_ancestry(const struct rev_info *revs); |
| 57 | |
| Jeff King | de1e67d | 2016-02-11 22:28:36 | [diff] [blame] | 58 | void show_object_with_name(FILE *out, struct object *obj, const char *name) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 59 | { |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 60 | fprintf(out, "%s ", oid_to_hex(&obj->oid)); |
| Junio C Hamano | 44ba10d | 2021-11-15 06:27:45 | [diff] [blame] | 61 | for (const char *p = name; *p && *p != '\n'; p++) |
| Jeff King | f9fb9d0 | 2016-02-11 22:24:18 | [diff] [blame] | 62 | fputc(*p, out); |
| Junio C Hamano | beba25a | 2011-08-17 21:30:35 | [diff] [blame] | 63 | fputc('\n', out); |
| Junio C Hamano | 91f1751 | 2011-08-17 21:30:34 | [diff] [blame] | 64 | } |
| 65 | |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 66 | static void mark_blob_uninteresting(struct blob *blob) |
| 67 | { |
| Martin Koegler | c1ee901 | 2008-02-18 20:47:54 | [diff] [blame] | 68 | if (!blob) |
| 69 | return; |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 70 | if (blob->object.flags & UNINTERESTING) |
| 71 | return; |
| 72 | blob->object.flags |= UNINTERESTING; |
| 73 | } |
| 74 | |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 75 | static void mark_tree_contents_uninteresting(struct repository *r, |
| 76 | struct tree *tree) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 77 | { |
| Linus Torvalds | f75e53e | 2006-05-29 19:20:14 | [diff] [blame] | 78 | struct tree_desc desc; |
| Linus Torvalds | 4c068a9 | 2006-05-30 16:45:45 | [diff] [blame] | 79 | struct name_entry entry; |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 80 | |
| Jeff King | a5411df | 2018-05-11 18:00:55 | [diff] [blame] | 81 | if (parse_tree_gently(tree, 1) < 0) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 82 | return; |
| Linus Torvalds | f75e53e | 2006-05-29 19:20:14 | [diff] [blame] | 83 | |
| Eric W. Biederman | efed687 | 2023-10-02 02:40:28 | [diff] [blame] | 84 | init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size); |
| Linus Torvalds | 4c068a9 | 2006-05-30 16:45:45 | [diff] [blame] | 85 | while (tree_entry(&desc, &entry)) { |
| Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 | [diff] [blame] | 86 | switch (object_type(entry.mode)) { |
| 87 | case OBJ_TREE: |
| brian m. carlson | ea82b2a | 2019-01-15 00:39:44 | [diff] [blame] | 88 | mark_tree_uninteresting(r, lookup_tree(r, &entry.oid)); |
| Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 | [diff] [blame] | 89 | break; |
| 90 | case OBJ_BLOB: |
| brian m. carlson | ea82b2a | 2019-01-15 00:39:44 | [diff] [blame] | 91 | mark_blob_uninteresting(lookup_blob(r, &entry.oid)); |
| Linus Torvalds | 4d1012c | 2007-11-11 23:35:23 | [diff] [blame] | 92 | break; |
| 93 | default: |
| 94 | /* Subproject commit - not in this repository */ |
| 95 | break; |
| 96 | } |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 97 | } |
| Linus Torvalds | f75e53e | 2006-05-29 19:20:14 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * We don't care about the tree any more |
| 101 | * after it has been marked uninteresting. |
| 102 | */ |
| Jeff King | 6e454b9 | 2013-06-05 22:37:39 | [diff] [blame] | 103 | free_tree_buffer(tree); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 104 | } |
| 105 | |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 106 | void mark_tree_uninteresting(struct repository *r, struct tree *tree) |
| Junio C Hamano | 2ac5e44 | 2014-01-15 23:38:01 | [diff] [blame] | 107 | { |
| Stefan Naewe | a2678df | 2015-12-05 15:27:24 | [diff] [blame] | 108 | struct object *obj; |
| Junio C Hamano | 2ac5e44 | 2014-01-15 23:38:01 | [diff] [blame] | 109 | |
| 110 | if (!tree) |
| 111 | return; |
| Stefan Naewe | a2678df | 2015-12-05 15:27:24 | [diff] [blame] | 112 | |
| 113 | obj = &tree->object; |
| Junio C Hamano | 2ac5e44 | 2014-01-15 23:38:01 | [diff] [blame] | 114 | if (obj->flags & UNINTERESTING) |
| 115 | return; |
| 116 | obj->flags |= UNINTERESTING; |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 117 | mark_tree_contents_uninteresting(r, tree); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 118 | } |
| 119 | |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 120 | struct path_and_oids_entry { |
| 121 | struct hashmap_entry ent; |
| 122 | char *path; |
| 123 | struct oidset trees; |
| 124 | }; |
| 125 | |
| Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 17:09:48 | [diff] [blame] | 126 | static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED, |
| Eric Wong | 939af16 | 2019-10-06 23:30:37 | [diff] [blame] | 127 | const struct hashmap_entry *eptr, |
| 128 | const struct hashmap_entry *entry_or_key, |
| Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 17:09:48 | [diff] [blame] | 129 | const void *keydata UNUSED) |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 130 | { |
| Eric Wong | 939af16 | 2019-10-06 23:30:37 | [diff] [blame] | 131 | const struct path_and_oids_entry *e1, *e2; |
| 132 | |
| 133 | e1 = container_of(eptr, const struct path_and_oids_entry, ent); |
| 134 | e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent); |
| 135 | |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 136 | return strcmp(e1->path, e2->path); |
| 137 | } |
| 138 | |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 139 | static void paths_and_oids_clear(struct hashmap *map) |
| 140 | { |
| 141 | struct hashmap_iter iter; |
| 142 | struct path_and_oids_entry *entry; |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 143 | |
| Eric Wong | 23dee69 | 2019-10-06 23:30:41 | [diff] [blame] | 144 | hashmap_for_each_entry(map, &iter, entry, ent /* member name */) { |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 145 | oidset_clear(&entry->trees); |
| 146 | free(entry->path); |
| 147 | } |
| 148 | |
| Elijah Newren | 6da1a25 | 2020-11-02 18:55:05 | [diff] [blame] | 149 | hashmap_clear_and_free(map, struct path_and_oids_entry, ent); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void paths_and_oids_insert(struct hashmap *map, |
| 153 | const char *path, |
| 154 | const struct object_id *oid) |
| 155 | { |
| 156 | int hash = strhash(path); |
| 157 | struct path_and_oids_entry key; |
| 158 | struct path_and_oids_entry *entry; |
| 159 | |
| Eric Wong | d22245a | 2019-10-06 23:30:27 | [diff] [blame] | 160 | hashmap_entry_init(&key.ent, hash); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 161 | |
| 162 | /* use a shallow copy for the lookup */ |
| 163 | key.path = (char *)path; |
| 164 | oidset_init(&key.trees, 0); |
| 165 | |
| Eric Wong | 404ab78 | 2019-10-06 23:30:42 | [diff] [blame] | 166 | entry = hashmap_get_entry(map, &key, ent, NULL); |
| Eric Wong | b6c5241 | 2019-10-06 23:30:30 | [diff] [blame] | 167 | if (!entry) { |
| René Scharfe | ca56dad | 2021-03-13 16:17:22 | [diff] [blame] | 168 | CALLOC_ARRAY(entry, 1); |
| Eric Wong | d22245a | 2019-10-06 23:30:27 | [diff] [blame] | 169 | hashmap_entry_init(&entry->ent, hash); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 170 | entry->path = xstrdup(key.path); |
| 171 | oidset_init(&entry->trees, 16); |
| Eric Wong | 26b455f | 2019-10-06 23:30:32 | [diff] [blame] | 172 | hashmap_put(map, &entry->ent); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | oidset_insert(&entry->trees, oid); |
| 176 | } |
| 177 | |
| 178 | static void add_children_by_path(struct repository *r, |
| 179 | struct tree *tree, |
| 180 | struct hashmap *map) |
| 181 | { |
| 182 | struct tree_desc desc; |
| 183 | struct name_entry entry; |
| 184 | |
| 185 | if (!tree) |
| 186 | return; |
| 187 | |
| 188 | if (parse_tree_gently(tree, 1) < 0) |
| 189 | return; |
| 190 | |
| Eric W. Biederman | efed687 | 2023-10-02 02:40:28 | [diff] [blame] | 191 | init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 192 | while (tree_entry(&desc, &entry)) { |
| 193 | switch (object_type(entry.mode)) { |
| 194 | case OBJ_TREE: |
| Junio C Hamano | 5fda343 | 2019-02-07 06:05:24 | [diff] [blame] | 195 | paths_and_oids_insert(map, entry.path, &entry.oid); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 196 | |
| 197 | if (tree->object.flags & UNINTERESTING) { |
| Junio C Hamano | 5fda343 | 2019-02-07 06:05:24 | [diff] [blame] | 198 | struct tree *child = lookup_tree(r, &entry.oid); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 199 | if (child) |
| 200 | child->object.flags |= UNINTERESTING; |
| 201 | } |
| 202 | break; |
| 203 | case OBJ_BLOB: |
| 204 | if (tree->object.flags & UNINTERESTING) { |
| Junio C Hamano | 5fda343 | 2019-02-07 06:05:24 | [diff] [blame] | 205 | struct blob *child = lookup_blob(r, &entry.oid); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 206 | if (child) |
| 207 | child->object.flags |= UNINTERESTING; |
| 208 | } |
| 209 | break; |
| 210 | default: |
| 211 | /* Subproject commit - not in this repository */ |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | free_tree_buffer(tree); |
| 217 | } |
| 218 | |
| Derrick Stolee | f1f5de4 | 2019-01-16 18:25:58 | [diff] [blame] | 219 | void mark_trees_uninteresting_sparse(struct repository *r, |
| 220 | struct oidset *trees) |
| 221 | { |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 222 | unsigned has_interesting = 0, has_uninteresting = 0; |
| Elijah Newren | b19315d | 2020-11-11 20:02:20 | [diff] [blame] | 223 | struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 224 | struct hashmap_iter map_iter; |
| 225 | struct path_and_oids_entry *entry; |
| Derrick Stolee | f1f5de4 | 2019-01-16 18:25:58 | [diff] [blame] | 226 | struct object_id *oid; |
| 227 | struct oidset_iter iter; |
| 228 | |
| 229 | oidset_iter_init(trees, &iter); |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 230 | while ((!has_interesting || !has_uninteresting) && |
| 231 | (oid = oidset_iter_next(&iter))) { |
| Derrick Stolee | f1f5de4 | 2019-01-16 18:25:58 | [diff] [blame] | 232 | struct tree *tree = lookup_tree(r, oid); |
| 233 | |
| 234 | if (!tree) |
| 235 | continue; |
| 236 | |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 237 | if (tree->object.flags & UNINTERESTING) |
| 238 | has_uninteresting = 1; |
| 239 | else |
| 240 | has_interesting = 1; |
| Derrick Stolee | f1f5de4 | 2019-01-16 18:25:58 | [diff] [blame] | 241 | } |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 242 | |
| 243 | /* Do not walk unless we have both types of trees. */ |
| 244 | if (!has_uninteresting || !has_interesting) |
| 245 | return; |
| 246 | |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 247 | oidset_iter_init(trees, &iter); |
| 248 | while ((oid = oidset_iter_next(&iter))) { |
| 249 | struct tree *tree = lookup_tree(r, oid); |
| 250 | add_children_by_path(r, tree, &map); |
| 251 | } |
| 252 | |
| Eric Wong | 23dee69 | 2019-10-06 23:30:41 | [diff] [blame] | 253 | hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */) |
| Derrick Stolee | d5d2e93 | 2019-01-16 18:25:59 | [diff] [blame] | 254 | mark_trees_uninteresting_sparse(r, &entry->trees); |
| 255 | |
| 256 | paths_and_oids_clear(&map); |
| Derrick Stolee | f1f5de4 | 2019-01-16 18:25:58 | [diff] [blame] | 257 | } |
| 258 | |
| Jeff King | 43fc643 | 2018-05-11 18:02:27 | [diff] [blame] | 259 | struct commit_stack { |
| 260 | struct commit **items; |
| 261 | size_t nr, alloc; |
| 262 | }; |
| Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 12:54:25 | [diff] [blame] | 263 | #define COMMIT_STACK_INIT { 0 } |
| Jeff King | 43fc643 | 2018-05-11 18:02:27 | [diff] [blame] | 264 | |
| 265 | static void commit_stack_push(struct commit_stack *stack, struct commit *commit) |
| 266 | { |
| 267 | ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc); |
| 268 | stack->items[stack->nr++] = commit; |
| 269 | } |
| 270 | |
| 271 | static struct commit *commit_stack_pop(struct commit_stack *stack) |
| 272 | { |
| 273 | return stack->nr ? stack->items[--stack->nr] : NULL; |
| 274 | } |
| 275 | |
| 276 | static void commit_stack_clear(struct commit_stack *stack) |
| 277 | { |
| 278 | FREE_AND_NULL(stack->items); |
| 279 | stack->nr = stack->alloc = 0; |
| 280 | } |
| 281 | |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 282 | static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit, |
| Jeff King | 8702b30 | 2018-05-11 18:03:15 | [diff] [blame] | 283 | struct commit_stack *pending) |
| 284 | { |
| 285 | struct commit_list *l; |
| 286 | |
| 287 | if (commit->object.flags & UNINTERESTING) |
| 288 | return; |
| 289 | commit->object.flags |= UNINTERESTING; |
| 290 | |
| 291 | /* |
| 292 | * Normally we haven't parsed the parent |
| 293 | * yet, so we won't have a parent of a parent |
| 294 | * here. However, it may turn out that we've |
| 295 | * reached this commit some other way (where it |
| 296 | * wasn't uninteresting), in which case we need |
| 297 | * to mark its parents recursively too.. |
| 298 | */ |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 299 | for (l = commit->parents; l; l = l->next) { |
| Jeff King | 8702b30 | 2018-05-11 18:03:15 | [diff] [blame] | 300 | commit_stack_push(pending, l->item); |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 301 | if (revs && revs->exclude_first_parent_only) |
| 302 | break; |
| 303 | } |
| Jeff King | 8702b30 | 2018-05-11 18:03:15 | [diff] [blame] | 304 | } |
| 305 | |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 306 | void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 307 | { |
| Jeff King | 43fc643 | 2018-05-11 18:02:27 | [diff] [blame] | 308 | struct commit_stack pending = COMMIT_STACK_INIT; |
| 309 | struct commit_list *l; |
| Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 12:19:53 | [diff] [blame] | 310 | |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 311 | for (l = commit->parents; l; l = l->next) { |
| 312 | mark_one_parent_uninteresting(revs, l->item, &pending); |
| 313 | if (revs && revs->exclude_first_parent_only) |
| 314 | break; |
| 315 | } |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 316 | |
| Jeff King | 8702b30 | 2018-05-11 18:03:15 | [diff] [blame] | 317 | while (pending.nr > 0) |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 318 | mark_one_parent_uninteresting(revs, commit_stack_pop(&pending), |
| Jeff King | 8702b30 | 2018-05-11 18:03:15 | [diff] [blame] | 319 | &pending); |
| Nguyễn Thái Ngọc Duy | 941ba8d | 2012-01-14 12:19:53 | [diff] [blame] | 320 | |
| Jeff King | 43fc643 | 2018-05-11 18:02:27 | [diff] [blame] | 321 | commit_stack_clear(&pending); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 322 | } |
| 323 | |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 324 | static void add_pending_object_with_path(struct rev_info *revs, |
| Michael Haggerty | ff5f5f2 | 2013-05-25 09:08:07 | [diff] [blame] | 325 | struct object *obj, |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 326 | const char *name, unsigned mode, |
| 327 | const char *path) |
| Martin Koegler | bb6c2fb | 2007-04-22 16:43:59 | [diff] [blame] | 328 | { |
| Jonathan Tan | a4f66a7 | 2020-09-01 22:28:07 | [diff] [blame] | 329 | struct interpret_branch_name_options options = { 0 }; |
| Junio C Hamano | cc243c3 | 2011-05-19 01:08:09 | [diff] [blame] | 330 | if (!obj) |
| 331 | return; |
| Linus Torvalds | aa27e46 | 2007-02-28 00:22:52 | [diff] [blame] | 332 | if (revs->no_walk && (obj->flags & UNINTERESTING)) |
| Linus Torvalds | f222abd | 2009-07-13 21:41:12 | [diff] [blame] | 333 | revs->no_walk = 0; |
| Junio C Hamano | 105e473 | 2010-01-26 21:48:28 | [diff] [blame] | 334 | if (revs->reflog_info && obj->type == OBJ_COMMIT) { |
| 335 | struct strbuf buf = STRBUF_INIT; |
| Jeff King | 1d72b60 | 2021-06-10 13:06:43 | [diff] [blame] | 336 | size_t namelen = strlen(name); |
| Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 13:58:46 | [diff] [blame] | 337 | int len = repo_interpret_branch_name(the_repository, name, |
| 338 | namelen, &buf, &options); |
| Junio C Hamano | 105e473 | 2010-01-26 21:48:28 | [diff] [blame] | 339 | |
| Jeff King | 1d72b60 | 2021-06-10 13:06:43 | [diff] [blame] | 340 | if (0 < len && len < namelen && buf.len) |
| Junio C Hamano | 105e473 | 2010-01-26 21:48:28 | [diff] [blame] | 341 | strbuf_addstr(&buf, name + len); |
| Jeff King | d08565b | 2017-07-07 09:14:07 | [diff] [blame] | 342 | add_reflog_for_walk(revs->reflog_info, |
| 343 | (struct commit *)obj, |
| 344 | buf.buf[0] ? buf.buf: name); |
| Junio C Hamano | 105e473 | 2010-01-26 21:48:28 | [diff] [blame] | 345 | strbuf_release(&buf); |
| Jeff King | d08565b | 2017-07-07 09:14:07 | [diff] [blame] | 346 | return; /* do not add the commit itself */ |
| Junio C Hamano | 105e473 | 2010-01-26 21:48:28 | [diff] [blame] | 347 | } |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 348 | add_object_array_with_path(obj, name, &revs->pending, mode, path); |
| 349 | } |
| 350 | |
| 351 | static void add_pending_object_with_mode(struct rev_info *revs, |
| 352 | struct object *obj, |
| 353 | const char *name, unsigned mode) |
| 354 | { |
| 355 | add_pending_object_with_path(revs, obj, name, mode, NULL); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 356 | } |
| 357 | |
| Michael Haggerty | ff5f5f2 | 2013-05-25 09:08:07 | [diff] [blame] | 358 | void add_pending_object(struct rev_info *revs, |
| 359 | struct object *obj, const char *name) |
| Junio C Hamano | 2d93b9f | 2007-06-08 09:24:58 | [diff] [blame] | 360 | { |
| 361 | add_pending_object_with_mode(revs, obj, name, S_IFINVALID); |
| 362 | } |
| 363 | |
| Junio C Hamano | 3384a2d | 2007-12-11 18:09:04 | [diff] [blame] | 364 | void add_head_to_pending(struct rev_info *revs) |
| 365 | { |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 366 | struct object_id oid; |
| Junio C Hamano | 3384a2d | 2007-12-11 18:09:04 | [diff] [blame] | 367 | struct object *obj; |
| Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 13:58:46 | [diff] [blame] | 368 | if (repo_get_oid(the_repository, "HEAD", &oid)) |
| Junio C Hamano | 3384a2d | 2007-12-11 18:09:04 | [diff] [blame] | 369 | return; |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 370 | obj = parse_object(revs->repo, &oid); |
| Junio C Hamano | 3384a2d | 2007-12-11 18:09:04 | [diff] [blame] | 371 | if (!obj) |
| 372 | return; |
| 373 | add_pending_object(revs, obj, "HEAD"); |
| 374 | } |
| 375 | |
| Michael Haggerty | ff5f5f2 | 2013-05-25 09:08:07 | [diff] [blame] | 376 | static struct object *get_reference(struct rev_info *revs, const char *name, |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 377 | const struct object_id *oid, |
| Michael Haggerty | ff5f5f2 | 2013-05-25 09:08:07 | [diff] [blame] | 378 | unsigned int flags) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 379 | { |
| 380 | struct object *object; |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 381 | |
| Jeff King | 9a8c3c4 | 2022-09-06 23:06:25 | [diff] [blame] | 382 | object = parse_object_with_flags(revs->repo, oid, |
| 383 | revs->verify_objects ? 0 : |
| Jeff King | 6cd05e7 | 2024-02-28 22:39:07 | [diff] [blame] | 384 | PARSE_OBJECT_SKIP_HASH_CHECK | |
| 385 | PARSE_OBJECT_DISCARD_TREE); |
| Jonathan Tan | ec0c579 | 2018-12-04 22:42:38 | [diff] [blame] | 386 | |
| Junio C Hamano | cc243c3 | 2011-05-19 01:08:09 | [diff] [blame] | 387 | if (!object) { |
| 388 | if (revs->ignore_missing) |
| Christian Couder | 3ff56af | 2024-02-14 14:25:10 | [diff] [blame] | 389 | return NULL; |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 390 | if (revs->exclude_promisor_objects && is_promisor_object(oid)) |
| 391 | return NULL; |
| Christian Couder | 7b644c8 | 2024-02-14 14:25:13 | [diff] [blame] | 392 | if (revs->do_not_die_on_missing_objects) { |
| 393 | oidset_insert(&revs->missing_commits, oid); |
| 394 | return NULL; |
| 395 | } |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 396 | die("bad object %s", name); |
| Junio C Hamano | cc243c3 | 2011-05-19 01:08:09 | [diff] [blame] | 397 | } |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 398 | object->flags |= flags; |
| 399 | return object; |
| 400 | } |
| 401 | |
| brian m. carlson | a58a1b0 | 2017-05-06 22:10:26 | [diff] [blame] | 402 | void add_pending_oid(struct rev_info *revs, const char *name, |
| 403 | const struct object_id *oid, unsigned int flags) |
| René Scharfe | 26c3177 | 2011-10-01 15:43:52 | [diff] [blame] | 404 | { |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 405 | struct object *object = get_reference(revs, name, oid, flags); |
| René Scharfe | 26c3177 | 2011-10-01 15:43:52 | [diff] [blame] | 406 | add_pending_object(revs, object, name); |
| 407 | } |
| 408 | |
| Michael Haggerty | ff5f5f2 | 2013-05-25 09:08:07 | [diff] [blame] | 409 | static struct commit *handle_commit(struct rev_info *revs, |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 410 | struct object_array_entry *entry) |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 411 | { |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 412 | struct object *object = entry->item; |
| 413 | const char *name = entry->name; |
| 414 | const char *path = entry->path; |
| 415 | unsigned int mode = entry->mode; |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 416 | unsigned long flags = object->flags; |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * Tag object? Look what it points to.. |
| 420 | */ |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 421 | while (object->type == OBJ_TAG) { |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 422 | struct tag *tag = (struct tag *) object; |
| Christian Couder | a4324ba | 2024-02-28 09:10:11 | [diff] [blame] | 423 | struct object_id *oid; |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 424 | if (revs->tag_objects && !(flags & UNINTERESTING)) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 425 | add_pending_object(revs, object, tag->tag); |
| Christian Couder | a4324ba | 2024-02-28 09:10:11 | [diff] [blame] | 426 | oid = get_tagged_oid(tag); |
| 427 | object = parse_object(revs->repo, oid); |
| Junio C Hamano | aeeae1b | 2009-01-28 07:19:30 | [diff] [blame] | 428 | if (!object) { |
| Jeff King | a3ba6bf | 2017-05-20 08:30:25 | [diff] [blame] | 429 | if (revs->ignore_missing_links || (flags & UNINTERESTING)) |
| Junio C Hamano | aeeae1b | 2009-01-28 07:19:30 | [diff] [blame] | 430 | return NULL; |
| Jonathan Tan | dc0a13f | 2018-07-13 00:03:06 | [diff] [blame] | 431 | if (revs->exclude_promisor_objects && |
| 432 | is_promisor_object(&tag->tagged->oid)) |
| 433 | return NULL; |
| Christian Couder | a4324ba | 2024-02-28 09:10:11 | [diff] [blame] | 434 | if (revs->do_not_die_on_missing_objects && oid) { |
| 435 | oidset_insert(&revs->missing_commits, oid); |
| 436 | return NULL; |
| 437 | } |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 438 | die("bad object %s", oid_to_hex(&tag->tagged->oid)); |
| Junio C Hamano | aeeae1b | 2009-01-28 07:19:30 | [diff] [blame] | 439 | } |
| Junio C Hamano | a743528 | 2014-01-15 20:26:13 | [diff] [blame] | 440 | object->flags |= flags; |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 441 | /* |
| 442 | * We'll handle the tagged object by looping or dropping |
| 443 | * through to the non-tag handlers below. Do not |
| Jeff King | 728350b | 2015-12-17 06:47:07 | [diff] [blame] | 444 | * propagate path data from the tag's pending entry. |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 445 | */ |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 446 | path = NULL; |
| 447 | mode = 0; |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Commit object? Just return it, we'll do all the complex |
| 452 | * reachability crud. |
| 453 | */ |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 454 | if (object->type == OBJ_COMMIT) { |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 455 | struct commit *commit = (struct commit *)object; |
| Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 05:28:24 | [diff] [blame] | 456 | |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 457 | if (repo_parse_commit(revs->repo, commit) < 0) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 458 | die("unable to parse commit %s", name); |
| Linus Torvalds | d9a8368 | 2006-02-27 16:54:36 | [diff] [blame] | 459 | if (flags & UNINTERESTING) { |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 460 | mark_parents_uninteresting(revs, commit); |
| Derrick Stolee | 1b4d882 | 2019-05-21 13:14:38 | [diff] [blame] | 461 | |
| 462 | if (!revs->topo_order || !generation_numbers_enabled(the_repository)) |
| 463 | revs->limited = 1; |
| Linus Torvalds | d9a8368 | 2006-02-27 16:54:36 | [diff] [blame] | 464 | } |
| Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 05:28:24 | [diff] [blame] | 465 | if (revs->sources) { |
| 466 | char **slot = revision_sources_at(revs->sources, commit); |
| 467 | |
| 468 | if (!*slot) |
| 469 | *slot = xstrdup(name); |
| 470 | } |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 471 | return commit; |
| 472 | } |
| 473 | |
| 474 | /* |
| Mike Ralphson | 3ea3c21 | 2009-04-17 18:13:30 | [diff] [blame] | 475 | * Tree object? Either mark it uninteresting, or add it |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 476 | * to the list of objects to look at later.. |
| 477 | */ |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 478 | if (object->type == OBJ_TREE) { |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 479 | struct tree *tree = (struct tree *)object; |
| 480 | if (!revs->tree_objects) |
| 481 | return NULL; |
| 482 | if (flags & UNINTERESTING) { |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 483 | mark_tree_contents_uninteresting(revs->repo, tree); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 486 | add_pending_object_with_path(revs, object, name, mode, path); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Blob object? You know the drill by now.. |
| 492 | */ |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 493 | if (object->type == OBJ_BLOB) { |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 494 | if (!revs->blob_objects) |
| 495 | return NULL; |
| Junio C Hamano | a743528 | 2014-01-15 20:26:13 | [diff] [blame] | 496 | if (flags & UNINTERESTING) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 497 | return NULL; |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 498 | add_pending_object_with_path(revs, object, name, mode, path); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 499 | return NULL; |
| 500 | } |
| 501 | die("%s is unknown object", name); |
| 502 | } |
| 503 | |
| Jeff King | b6e8a3b | 2015-04-17 22:11:04 | [diff] [blame] | 504 | static int everybody_uninteresting(struct commit_list *orig, |
| 505 | struct commit **interesting_cache) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 506 | { |
| 507 | struct commit_list *list = orig; |
| Jeff King | b6e8a3b | 2015-04-17 22:11:04 | [diff] [blame] | 508 | |
| 509 | if (*interesting_cache) { |
| 510 | struct commit *commit = *interesting_cache; |
| 511 | if (!(commit->object.flags & UNINTERESTING)) |
| 512 | return 0; |
| 513 | } |
| 514 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 515 | while (list) { |
| 516 | struct commit *commit = list->item; |
| 517 | list = list->next; |
| 518 | if (commit->object.flags & UNINTERESTING) |
| 519 | continue; |
| Stefan Beller | ae40ebd | 2015-06-26 19:40:19 | [diff] [blame] | 520 | |
| 521 | *interesting_cache = commit; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | return 1; |
| 525 | } |
| 526 | |
| Junio C Hamano | 0a4ba7f | 2007-03-14 20:12:18 | [diff] [blame] | 527 | /* |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 528 | * A definition of "relevant" commit that we can use to simplify limited graphs |
| 529 | * by eliminating side branches. |
| 530 | * |
| 531 | * A "relevant" commit is one that is !UNINTERESTING (ie we are including it |
| 532 | * in our list), or that is a specified BOTTOM commit. Then after computing |
| 533 | * a limited list, during processing we can generally ignore boundary merges |
| 534 | * coming from outside the graph, (ie from irrelevant parents), and treat |
| 535 | * those merges as if they were single-parent. TREESAME is defined to consider |
| 536 | * only relevant parents, if any. If we are TREESAME to our on-graph parents, |
| 537 | * we don't care if we were !TREESAME to non-graph parents. |
| 538 | * |
| 539 | * Treating bottom commits as relevant ensures that a limited graph's |
| 540 | * connection to the actual bottom commit is not viewed as a side branch, but |
| 541 | * treated as part of the graph. For example: |
| 542 | * |
| 543 | * ....Z...A---X---o---o---B |
| 544 | * . / |
| 545 | * W---Y |
| 546 | * |
| 547 | * When computing "A..B", the A-X connection is at least as important as |
| 548 | * Y-X, despite A being flagged UNINTERESTING. |
| 549 | * |
| 550 | * And when computing --ancestry-path "A..B", the A-X connection is more |
| 551 | * important than Y-X, despite both A and Y being flagged UNINTERESTING. |
| 552 | */ |
| 553 | static inline int relevant_commit(struct commit *commit) |
| 554 | { |
| 555 | return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Return a single relevant commit from a parent list. If we are a TREESAME |
| 560 | * commit, and this selects one of our parents, then we can safely simplify to |
| 561 | * that parent. |
| 562 | */ |
| 563 | static struct commit *one_relevant_parent(const struct rev_info *revs, |
| 564 | struct commit_list *orig) |
| 565 | { |
| 566 | struct commit_list *list = orig; |
| 567 | struct commit *relevant = NULL; |
| 568 | |
| 569 | if (!orig) |
| 570 | return NULL; |
| 571 | |
| 572 | /* |
| 573 | * For 1-parent commits, or if first-parent-only, then return that |
| 574 | * first parent (even if not "relevant" by the above definition). |
| 575 | * TREESAME will have been set purely on that parent. |
| 576 | */ |
| 577 | if (revs->first_parent_only || !orig->next) |
| 578 | return orig->item; |
| 579 | |
| 580 | /* |
| 581 | * For multi-parent commits, identify a sole relevant parent, if any. |
| 582 | * If we have only one relevant parent, then TREESAME will be set purely |
| 583 | * with regard to that parent, and we can simplify accordingly. |
| 584 | * |
| 585 | * If we have more than one relevant parent, or no relevant parents |
| 586 | * (and multiple irrelevant ones), then we can't select a parent here |
| 587 | * and return NULL. |
| 588 | */ |
| 589 | while (list) { |
| 590 | struct commit *commit = list->item; |
| 591 | list = list->next; |
| 592 | if (relevant_commit(commit)) { |
| 593 | if (relevant) |
| 594 | return NULL; |
| 595 | relevant = commit; |
| 596 | } |
| 597 | } |
| 598 | return relevant; |
| 599 | } |
| 600 | |
| 601 | /* |
| Junio C Hamano | 0a4ba7f | 2007-03-14 20:12:18 | [diff] [blame] | 602 | * The goal is to get REV_TREE_NEW as the result only if the |
| Linus Torvalds | ceff8e7 | 2009-06-03 01:34:01 | [diff] [blame] | 603 | * diff consists of all '+' (and no other changes), REV_TREE_OLD |
| 604 | * if the whole diff is removal of old data, and otherwise |
| 605 | * REV_TREE_DIFFERENT (of course if the trees are the same we |
| 606 | * want REV_TREE_SAME). |
| Jeff King | a937b37 | 2017-10-13 15:27:45 | [diff] [blame] | 607 | * |
| 608 | * The only time we care about the distinction is when |
| 609 | * remove_empty_trees is in effect, in which case we care only about |
| 610 | * whether the whole change is REV_TREE_NEW, or if there's another type |
| 611 | * of change. Which means we can stop the diff early in either of these |
| 612 | * cases: |
| 613 | * |
| 614 | * 1. We're not using remove_empty_trees at all. |
| 615 | * |
| 616 | * 2. We saw anything except REV_TREE_NEW. |
| Junio C Hamano | 0a4ba7f | 2007-03-14 20:12:18 | [diff] [blame] | 617 | */ |
| Ævar Arnfjörð Bjarmason | 296a143 | 2022-04-13 20:01:37 | [diff] [blame] | 618 | #define REV_TREE_SAME 0 |
| 619 | #define REV_TREE_NEW 1 /* Only new files */ |
| 620 | #define REV_TREE_OLD 2 /* Only files removed */ |
| 621 | #define REV_TREE_DIFFERENT 3 /* Mixed changes */ |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 622 | static int tree_difference = REV_TREE_SAME; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 623 | |
| 624 | static void file_add_remove(struct diff_options *options, |
| Jeff King | 61bdc7c | 2022-12-13 11:13:48 | [diff] [blame] | 625 | int addremove, |
| 626 | unsigned mode UNUSED, |
| 627 | const struct object_id *oid UNUSED, |
| 628 | int oid_valid UNUSED, |
| 629 | const char *fullpath UNUSED, |
| 630 | unsigned dirty_submodule UNUSED) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 631 | { |
| Linus Torvalds | ceff8e7 | 2009-06-03 01:34:01 | [diff] [blame] | 632 | int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD; |
| Jeff King | a937b37 | 2017-10-13 15:27:45 | [diff] [blame] | 633 | struct rev_info *revs = options->change_fn_data; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 634 | |
| Linus Torvalds | ceff8e7 | 2009-06-03 01:34:01 | [diff] [blame] | 635 | tree_difference |= diff; |
| Jeff King | a937b37 | 2017-10-13 15:27:45 | [diff] [blame] | 636 | if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW) |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 637 | options->flags.has_changes = 1; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void file_change(struct diff_options *options, |
| Jeff King | 61bdc7c | 2022-12-13 11:13:48 | [diff] [blame] | 641 | unsigned old_mode UNUSED, |
| 642 | unsigned new_mode UNUSED, |
| 643 | const struct object_id *old_oid UNUSED, |
| 644 | const struct object_id *new_oid UNUSED, |
| 645 | int old_oid_valid UNUSED, |
| 646 | int new_oid_valid UNUSED, |
| 647 | const char *fullpath UNUSED, |
| 648 | unsigned old_dirty_submodule UNUSED, |
| 649 | unsigned new_dirty_submodule UNUSED) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 650 | { |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 651 | tree_difference = REV_TREE_DIFFERENT; |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 652 | options->flags.has_changes = 1; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 653 | } |
| 654 | |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 655 | static int bloom_filter_atexit_registered; |
| 656 | static unsigned int count_bloom_filter_maybe; |
| 657 | static unsigned int count_bloom_filter_definitely_not; |
| 658 | static unsigned int count_bloom_filter_false_positive; |
| 659 | static unsigned int count_bloom_filter_not_present; |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 660 | |
| 661 | static void trace2_bloom_filter_statistics_atexit(void) |
| 662 | { |
| 663 | struct json_writer jw = JSON_WRITER_INIT; |
| 664 | |
| 665 | jw_object_begin(&jw, 0); |
| 666 | jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present); |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 667 | jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe); |
| 668 | jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not); |
| 669 | jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive); |
| 670 | jw_end(&jw); |
| 671 | |
| 672 | trace2_data_json("bloom", the_repository, "statistics", &jw); |
| 673 | |
| 674 | jw_release(&jw); |
| 675 | } |
| 676 | |
| Derrick Stolee | 8918e37 | 2020-04-16 20:14:02 | [diff] [blame] | 677 | static int forbid_bloom_filters(struct pathspec *spec) |
| 678 | { |
| 679 | if (spec->has_wildcard) |
| 680 | return 1; |
| 681 | if (spec->nr > 1) |
| 682 | return 1; |
| 683 | if (spec->magic & ~PATHSPEC_LITERAL) |
| 684 | return 1; |
| 685 | if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL)) |
| 686 | return 1; |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 691 | static void prepare_to_use_bloom_filter(struct rev_info *revs) |
| 692 | { |
| 693 | struct pathspec_item *pi; |
| 694 | char *path_alloc = NULL; |
| SZEDER Gábor | c525ce9 | 2020-07-01 13:27:30 | [diff] [blame] | 695 | const char *path, *p; |
| SZEDER Gábor | c525ce9 | 2020-07-01 13:27:30 | [diff] [blame] | 696 | size_t len; |
| 697 | int path_component_nr = 1; |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 698 | |
| 699 | if (!revs->commits) |
| Derrick Stolee | 8918e37 | 2020-04-16 20:14:02 | [diff] [blame] | 700 | return; |
| 701 | |
| 702 | if (forbid_bloom_filters(&revs->prune_data)) |
| 703 | return; |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 704 | |
| 705 | repo_parse_commit(revs->repo, revs->commits->item); |
| 706 | |
| Taylor Blau | 4f36440 | 2020-09-09 15:22:44 | [diff] [blame] | 707 | revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo); |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 708 | if (!revs->bloom_filter_settings) |
| 709 | return; |
| 710 | |
| Derrick Stolee | f32dde8 | 2020-05-11 11:56:19 | [diff] [blame] | 711 | if (!revs->pruning.pathspec.nr) |
| 712 | return; |
| 713 | |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 714 | pi = &revs->pruning.pathspec.items[0]; |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 715 | |
| 716 | /* remove single trailing slash from path, if needed */ |
| Jeff King | fd9a631 | 2020-08-04 07:46:52 | [diff] [blame] | 717 | if (pi->len > 0 && pi->match[pi->len - 1] == '/') { |
| 718 | path_alloc = xmemdupz(pi->match, pi->len - 1); |
| Derrick Stolee | dc8e95b | 2020-07-01 13:27:28 | [diff] [blame] | 719 | path = path_alloc; |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 720 | } else |
| Derrick Stolee | dc8e95b | 2020-07-01 13:27:28 | [diff] [blame] | 721 | path = pi->match; |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 722 | |
| 723 | len = strlen(path); |
| Taylor Blau | f3c2a36 | 2020-07-01 13:27:29 | [diff] [blame] | 724 | if (!len) { |
| 725 | revs->bloom_filter_settings = NULL; |
| Jeff King | 398e659 | 2020-08-04 07:50:17 | [diff] [blame] | 726 | free(path_alloc); |
| Taylor Blau | f3c2a36 | 2020-07-01 13:27:29 | [diff] [blame] | 727 | return; |
| 728 | } |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 729 | |
| SZEDER Gábor | c525ce9 | 2020-07-01 13:27:30 | [diff] [blame] | 730 | p = path; |
| 731 | while (*p) { |
| 732 | /* |
| 733 | * At this point, the path is normalized to use Unix-style |
| 734 | * path separators. This is required due to how the |
| 735 | * changed-path Bloom filters store the paths. |
| 736 | */ |
| 737 | if (*p == '/') |
| 738 | path_component_nr++; |
| 739 | p++; |
| 740 | } |
| 741 | |
| 742 | revs->bloom_keys_nr = path_component_nr; |
| 743 | ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr); |
| 744 | |
| 745 | fill_bloom_key(path, len, &revs->bloom_keys[0], |
| 746 | revs->bloom_filter_settings); |
| 747 | path_component_nr = 1; |
| 748 | |
| 749 | p = path + len - 1; |
| 750 | while (p > path) { |
| 751 | if (*p == '/') |
| 752 | fill_bloom_key(path, p - path, |
| 753 | &revs->bloom_keys[path_component_nr++], |
| 754 | revs->bloom_filter_settings); |
| 755 | p--; |
| 756 | } |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 757 | |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 758 | if (trace2_is_enabled() && !bloom_filter_atexit_registered) { |
| 759 | atexit(trace2_bloom_filter_statistics_atexit); |
| 760 | bloom_filter_atexit_registered = 1; |
| 761 | } |
| 762 | |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 763 | free(path_alloc); |
| 764 | } |
| 765 | |
| 766 | static int check_maybe_different_in_bloom_filter(struct rev_info *revs, |
| 767 | struct commit *commit) |
| 768 | { |
| 769 | struct bloom_filter *filter; |
| SZEDER Gábor | c525ce9 | 2020-07-01 13:27:30 | [diff] [blame] | 770 | int result = 1, j; |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 771 | |
| 772 | if (!revs->repo->objects->commit_graph) |
| 773 | return -1; |
| 774 | |
| Abhishek Kumar | c49c82a | 2020-06-17 09:14:10 | [diff] [blame] | 775 | if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY) |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 776 | return -1; |
| 777 | |
| Taylor Blau | 312cff5 | 2020-09-16 18:07:32 | [diff] [blame] | 778 | filter = get_bloom_filter(revs->repo, commit); |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 779 | |
| 780 | if (!filter) { |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 781 | count_bloom_filter_not_present++; |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 782 | return -1; |
| 783 | } |
| 784 | |
| SZEDER Gábor | c525ce9 | 2020-07-01 13:27:30 | [diff] [blame] | 785 | for (j = 0; result && j < revs->bloom_keys_nr; j++) { |
| 786 | result = bloom_filter_contains(filter, |
| 787 | &revs->bloom_keys[j], |
| 788 | revs->bloom_filter_settings); |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 789 | } |
| 790 | |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 791 | if (result) |
| 792 | count_bloom_filter_maybe++; |
| 793 | else |
| 794 | count_bloom_filter_definitely_not++; |
| 795 | |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 796 | return result; |
| 797 | } |
| 798 | |
| Michael Haggerty | ff5f5f2 | 2013-05-25 09:08:07 | [diff] [blame] | 799 | static int rev_compare_tree(struct rev_info *revs, |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 800 | struct commit *parent, struct commit *commit, int nth_parent) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 801 | { |
| Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 13:58:48 | [diff] [blame] | 802 | struct tree *t1 = repo_get_commit_tree(the_repository, parent); |
| 803 | struct tree *t2 = repo_get_commit_tree(the_repository, commit); |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 804 | int bloom_ret = 1; |
| Linus Torvalds | 3a5e860 | 2008-11-03 18:45:41 | [diff] [blame] | 805 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 806 | if (!t1) |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 807 | return REV_TREE_NEW; |
| Linus Torvalds | ceff8e7 | 2009-06-03 01:34:01 | [diff] [blame] | 808 | if (!t2) |
| 809 | return REV_TREE_OLD; |
| Linus Torvalds | 78892e3 | 2008-11-03 19:25:46 | [diff] [blame] | 810 | |
| 811 | if (revs->simplify_by_decoration) { |
| 812 | /* |
| 813 | * If we are simplifying by decoration, then the commit |
| 814 | * is worth showing if it has a tag pointing at it. |
| 815 | */ |
| Jeff King | 2608c24 | 2014-08-26 10:23:54 | [diff] [blame] | 816 | if (get_name_decoration(&commit->object)) |
| Linus Torvalds | 78892e3 | 2008-11-03 19:25:46 | [diff] [blame] | 817 | return REV_TREE_DIFFERENT; |
| 818 | /* |
| 819 | * A commit that is not pointed by a tag is uninteresting |
| 820 | * if we are not limited by path. This means that you will |
| 821 | * see the usual "commits that touch the paths" plus any |
| 822 | * tagged commit by specifying both --simplify-by-decoration |
| 823 | * and pathspec. |
| 824 | */ |
| Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 12:43:06 | [diff] [blame] | 825 | if (!revs->prune_data.nr) |
| Linus Torvalds | 78892e3 | 2008-11-03 19:25:46 | [diff] [blame] | 826 | return REV_TREE_SAME; |
| 827 | } |
| Linus Torvalds | ceff8e7 | 2009-06-03 01:34:01 | [diff] [blame] | 828 | |
| SZEDER Gábor | c525ce9 | 2020-07-01 13:27:30 | [diff] [blame] | 829 | if (revs->bloom_keys_nr && !nth_parent) { |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 830 | bloom_ret = check_maybe_different_in_bloom_filter(revs, commit); |
| 831 | |
| 832 | if (bloom_ret == 0) |
| 833 | return REV_TREE_SAME; |
| 834 | } |
| 835 | |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 836 | tree_difference = REV_TREE_SAME; |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 837 | revs->pruning.flags.has_changes = 0; |
| SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 | [diff] [blame] | 838 | diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning); |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 839 | |
| Garima Singh | 42e50e7 | 2020-04-06 16:59:53 | [diff] [blame] | 840 | if (!nth_parent) |
| 841 | if (bloom_ret == 1 && tree_difference == REV_TREE_SAME) |
| 842 | count_bloom_filter_false_positive++; |
| 843 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 844 | return tree_difference; |
| 845 | } |
| 846 | |
| Linus Torvalds | 3a5e860 | 2008-11-03 18:45:41 | [diff] [blame] | 847 | static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 848 | { |
| Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 13:58:48 | [diff] [blame] | 849 | struct tree *t1 = repo_get_commit_tree(the_repository, commit); |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 850 | |
| 851 | if (!t1) |
| 852 | return 0; |
| 853 | |
| Junio C Hamano | 0a4ba7f | 2007-03-14 20:12:18 | [diff] [blame] | 854 | tree_difference = REV_TREE_SAME; |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 855 | revs->pruning.flags.has_changes = 0; |
| SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 | [diff] [blame] | 856 | diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning); |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 857 | |
| SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 | [diff] [blame] | 858 | return tree_difference == REV_TREE_SAME; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 859 | } |
| 860 | |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 861 | struct treesame_state { |
| 862 | unsigned int nparents; |
| 863 | unsigned char treesame[FLEX_ARRAY]; |
| 864 | }; |
| 865 | |
| 866 | static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit) |
| 867 | { |
| 868 | unsigned n = commit_list_count(commit->parents); |
| Jeff King | 50a6c8e | 2016-02-22 22:44:35 | [diff] [blame] | 869 | struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n)); |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 870 | st->nparents = n; |
| 871 | add_decoration(&revs->treesame, &commit->object, st); |
| 872 | return st; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Must be called immediately after removing the nth_parent from a commit's |
| 877 | * parent list, if we are maintaining the per-parent treesame[] decoration. |
| 878 | * This does not recalculate the master TREESAME flag - update_treesame() |
| 879 | * should be called to update it after a sequence of treesame[] modifications |
| 880 | * that may have affected it. |
| 881 | */ |
| 882 | static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent) |
| 883 | { |
| 884 | struct treesame_state *st; |
| 885 | int old_same; |
| 886 | |
| 887 | if (!commit->parents) { |
| 888 | /* |
| 889 | * Have just removed the only parent from a non-merge. |
| 890 | * Different handling, as we lack decoration. |
| 891 | */ |
| 892 | if (nth_parent != 0) |
| 893 | die("compact_treesame %u", nth_parent); |
| 894 | old_same = !!(commit->object.flags & TREESAME); |
| 895 | if (rev_same_tree_as_empty(revs, commit)) |
| 896 | commit->object.flags |= TREESAME; |
| 897 | else |
| 898 | commit->object.flags &= ~TREESAME; |
| 899 | return old_same; |
| 900 | } |
| 901 | |
| 902 | st = lookup_decoration(&revs->treesame, &commit->object); |
| 903 | if (!st || nth_parent >= st->nparents) |
| 904 | die("compact_treesame %u", nth_parent); |
| 905 | |
| 906 | old_same = st->treesame[nth_parent]; |
| 907 | memmove(st->treesame + nth_parent, |
| 908 | st->treesame + nth_parent + 1, |
| 909 | st->nparents - nth_parent - 1); |
| 910 | |
| 911 | /* |
| 912 | * If we've just become a non-merge commit, update TREESAME |
| 913 | * immediately, and remove the no-longer-needed decoration. |
| 914 | * If still a merge, defer update until update_treesame(). |
| 915 | */ |
| 916 | if (--st->nparents == 1) { |
| 917 | if (commit->parents->next) |
| 918 | die("compact_treesame parents mismatch"); |
| 919 | if (st->treesame[0] && revs->dense) |
| 920 | commit->object.flags |= TREESAME; |
| 921 | else |
| 922 | commit->object.flags &= ~TREESAME; |
| 923 | free(add_decoration(&revs->treesame, &commit->object, NULL)); |
| 924 | } |
| 925 | |
| 926 | return old_same; |
| 927 | } |
| 928 | |
| 929 | static unsigned update_treesame(struct rev_info *revs, struct commit *commit) |
| 930 | { |
| 931 | if (commit->parents && commit->parents->next) { |
| 932 | unsigned n; |
| 933 | struct treesame_state *st; |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 934 | struct commit_list *p; |
| 935 | unsigned relevant_parents; |
| 936 | unsigned relevant_change, irrelevant_change; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 937 | |
| 938 | st = lookup_decoration(&revs->treesame, &commit->object); |
| 939 | if (!st) |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 940 | die("update_treesame %s", oid_to_hex(&commit->object.oid)); |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 941 | relevant_parents = 0; |
| 942 | relevant_change = irrelevant_change = 0; |
| 943 | for (p = commit->parents, n = 0; p; n++, p = p->next) { |
| 944 | if (relevant_commit(p->item)) { |
| 945 | relevant_change |= !st->treesame[n]; |
| 946 | relevant_parents++; |
| 947 | } else |
| 948 | irrelevant_change |= !st->treesame[n]; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 949 | } |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 950 | if (relevant_parents ? relevant_change : irrelevant_change) |
| 951 | commit->object.flags &= ~TREESAME; |
| 952 | else |
| 953 | commit->object.flags |= TREESAME; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | return commit->object.flags & TREESAME; |
| 957 | } |
| 958 | |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 959 | static inline int limiting_can_increase_treesame(const struct rev_info *revs) |
| 960 | { |
| 961 | /* |
| 962 | * TREESAME is irrelevant unless prune && dense; |
| 963 | * if simplify_history is set, we can't have a mixture of TREESAME and |
| 964 | * !TREESAME INTERESTING parents (and we don't have treesame[] |
| 965 | * decoration anyway); |
| 966 | * if first_parent_only is set, then the TREESAME flag is locked |
| 967 | * against the first parent (and again we lack treesame[] decoration). |
| 968 | */ |
| 969 | return revs->prune && revs->dense && |
| 970 | !revs->simplify_history && |
| 971 | !revs->first_parent_only; |
| 972 | } |
| 973 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 974 | static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit) |
| 975 | { |
| 976 | struct commit_list **pp, *parent; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 977 | struct treesame_state *ts = NULL; |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 978 | int relevant_change = 0, irrelevant_change = 0; |
| 979 | int relevant_parents, nth_parent; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 980 | |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 981 | /* |
| 982 | * If we don't do pruning, everything is interesting |
| 983 | */ |
| Linus Torvalds | 7dc0fe3 | 2007-11-13 07:16:08 | [diff] [blame] | 984 | if (!revs->prune) |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 985 | return; |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 986 | |
| Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 13:58:48 | [diff] [blame] | 987 | if (!repo_get_commit_tree(the_repository, commit)) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 988 | return; |
| 989 | |
| 990 | if (!commit->parents) { |
| Linus Torvalds | 3a5e860 | 2008-11-03 18:45:41 | [diff] [blame] | 991 | if (rev_same_tree_as_empty(revs, commit)) |
| Linus Torvalds | 7dc0fe3 | 2007-11-13 07:16:08 | [diff] [blame] | 992 | commit->object.flags |= TREESAME; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 993 | return; |
| 994 | } |
| 995 | |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 996 | /* |
| 997 | * Normal non-merge commit? If we don't want to make the |
| 998 | * history dense, we consider it always to be a change.. |
| 999 | */ |
| Linus Torvalds | 7dc0fe3 | 2007-11-13 07:16:08 | [diff] [blame] | 1000 | if (!revs->dense && !commit->parents->next) |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 1001 | return; |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 1002 | |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1003 | for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 1004 | (parent = *pp) != NULL; |
| 1005 | pp = &parent->next, nth_parent++) { |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1006 | struct commit *p = parent->item; |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1007 | if (relevant_commit(p)) |
| 1008 | relevant_parents++; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1009 | |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 1010 | if (nth_parent == 1) { |
| 1011 | /* |
| 1012 | * This our second loop iteration - so we now know |
| 1013 | * we're dealing with a merge. |
| 1014 | * |
| 1015 | * Do not compare with later parents when we care only about |
| 1016 | * the first parent chain, in order to avoid derailing the |
| 1017 | * traversal to follow a side branch that brought everything |
| 1018 | * in the path we are limited to by the pathspec. |
| 1019 | */ |
| 1020 | if (revs->first_parent_only) |
| 1021 | break; |
| 1022 | /* |
| 1023 | * If this will remain a potentially-simplifiable |
| 1024 | * merge, remember per-parent treesame if needed. |
| 1025 | * Initialise the array with the comparison from our |
| 1026 | * first iteration. |
| 1027 | */ |
| 1028 | if (revs->treesame.name && |
| 1029 | !revs->simplify_history && |
| 1030 | !(commit->object.flags & UNINTERESTING)) { |
| 1031 | ts = initialise_treesame(revs, commit); |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1032 | if (!(irrelevant_change || relevant_change)) |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 1033 | ts->treesame[0] = 1; |
| 1034 | } |
| 1035 | } |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 1036 | if (repo_parse_commit(revs->repo, p) < 0) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1037 | die("cannot simplify commit %s (because of %s)", |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 1038 | oid_to_hex(&commit->object.oid), |
| 1039 | oid_to_hex(&p->object.oid)); |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 1040 | switch (rev_compare_tree(revs, p, commit, nth_parent)) { |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 1041 | case REV_TREE_SAME: |
| Kevin Bracey | 141efdb | 2013-05-16 15:32:41 | [diff] [blame] | 1042 | if (!revs->simplify_history || !relevant_commit(p)) { |
| Junio C Hamano | f3219fb | 2006-03-11 05:59:37 | [diff] [blame] | 1043 | /* Even if a merge with an uninteresting |
| 1044 | * side branch brought the entire change |
| 1045 | * we are interested in, we do not want |
| 1046 | * to lose the other branches of this |
| 1047 | * merge, so we just keep going. |
| 1048 | */ |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 1049 | if (ts) |
| 1050 | ts->treesame[nth_parent] = 1; |
| Junio C Hamano | f3219fb | 2006-03-11 05:59:37 | [diff] [blame] | 1051 | continue; |
| 1052 | } |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1053 | parent->next = NULL; |
| 1054 | commit->parents = parent; |
| Derrick Stolee | 8d049e1 | 2020-04-10 12:19:43 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * A merge commit is a "diversion" if it is not |
| 1058 | * TREESAME to its first parent but is TREESAME |
| 1059 | * to a later parent. In the simplified history, |
| 1060 | * we "divert" the history walk to the later |
| 1061 | * parent. These commits are shown when "show_pulls" |
| 1062 | * is enabled, so do not mark the object as |
| 1063 | * TREESAME here. |
| 1064 | */ |
| 1065 | if (!revs->show_pulls || !nth_parent) |
| 1066 | commit->object.flags |= TREESAME; |
| 1067 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1068 | return; |
| 1069 | |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 1070 | case REV_TREE_NEW: |
| 1071 | if (revs->remove_empty_trees && |
| Linus Torvalds | 3a5e860 | 2008-11-03 18:45:41 | [diff] [blame] | 1072 | rev_same_tree_as_empty(revs, p)) { |
| Junio C Hamano | c348f31 | 2006-03-12 21:39:31 | [diff] [blame] | 1073 | /* We are adding all the specified |
| 1074 | * paths from this parent, so the |
| 1075 | * history beyond this parent is not |
| 1076 | * interesting. Remove its parents |
| 1077 | * (they are grandparents for us). |
| 1078 | * IOW, we pretend this parent is a |
| 1079 | * "root" commit. |
| Junio C Hamano | a41e109 | 2006-03-12 21:39:31 | [diff] [blame] | 1080 | */ |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 1081 | if (repo_parse_commit(revs->repo, p) < 0) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1082 | die("cannot simplify commit %s (invalid %s)", |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 1083 | oid_to_hex(&commit->object.oid), |
| 1084 | oid_to_hex(&p->object.oid)); |
| Junio C Hamano | c348f31 | 2006-03-12 21:39:31 | [diff] [blame] | 1085 | p->parents = NULL; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1086 | } |
| 1087 | /* fallthrough */ |
| Linus Torvalds | ceff8e7 | 2009-06-03 01:34:01 | [diff] [blame] | 1088 | case REV_TREE_OLD: |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 1089 | case REV_TREE_DIFFERENT: |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1090 | if (relevant_commit(p)) |
| 1091 | relevant_change = 1; |
| 1092 | else |
| 1093 | irrelevant_change = 1; |
| Derrick Stolee | 8d049e1 | 2020-04-10 12:19:43 | [diff] [blame] | 1094 | |
| 1095 | if (!nth_parent) |
| 1096 | commit->object.flags |= PULL_MERGE; |
| 1097 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1098 | continue; |
| 1099 | } |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 1100 | die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid)); |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1101 | } |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * TREESAME is straightforward for single-parent commits. For merge |
| 1105 | * commits, it is most useful to define it so that "irrelevant" |
| 1106 | * parents cannot make us !TREESAME - if we have any relevant |
| 1107 | * parents, then we only consider TREESAMEness with respect to them, |
| 1108 | * allowing irrelevant merges from uninteresting branches to be |
| 1109 | * simplified away. Only if we have only irrelevant parents do we |
| 1110 | * base TREESAME on them. Note that this logic is replicated in |
| 1111 | * update_treesame, which should be kept in sync. |
| 1112 | */ |
| 1113 | if (relevant_parents ? !relevant_change : !irrelevant_change) |
| 1114 | commit->object.flags |= TREESAME; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1115 | } |
| 1116 | |
| Derrick Stolee | 5284fc5 | 2018-11-01 13:46:21 | [diff] [blame] | 1117 | static int process_parents(struct rev_info *revs, struct commit *commit, |
| Jeff King | 8320b1d | 2019-04-04 01:41:09 | [diff] [blame] | 1118 | struct commit_list **list, struct prio_queue *queue) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1119 | { |
| 1120 | struct commit_list *parent = commit->parents; |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1121 | unsigned pass_flags; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1122 | |
| Linus Torvalds | 3381c79 | 2006-04-09 00:05:58 | [diff] [blame] | 1123 | if (commit->object.flags & ADDED) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1124 | return 0; |
| Karthik Nayak | 9830926 | 2023-10-27 07:59:29 | [diff] [blame] | 1125 | if (revs->do_not_die_on_missing_objects && |
| 1126 | oidset_contains(&revs->missing_commits, &commit->object.oid)) |
| 1127 | return 0; |
| Linus Torvalds | 3381c79 | 2006-04-09 00:05:58 | [diff] [blame] | 1128 | commit->object.flags |= ADDED; |
| 1129 | |
| Vicent Marti | a330de3 | 2013-10-24 18:01:41 | [diff] [blame] | 1130 | if (revs->include_check && |
| 1131 | !revs->include_check(commit, revs->include_check_data)) |
| 1132 | return 0; |
| 1133 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1134 | /* |
| 1135 | * If the commit is uninteresting, don't try to |
| 1136 | * prune parents - we want the maximal uninteresting |
| 1137 | * set. |
| 1138 | * |
| 1139 | * Normally we haven't parsed the parent |
| 1140 | * yet, so we won't have a parent of a parent |
| 1141 | * here. However, it may turn out that we've |
| 1142 | * reached this commit some other way (where it |
| 1143 | * wasn't uninteresting), in which case we need |
| 1144 | * to mark its parents recursively too.. |
| 1145 | */ |
| 1146 | if (commit->object.flags & UNINTERESTING) { |
| 1147 | while (parent) { |
| 1148 | struct commit *p = parent->item; |
| 1149 | parent = parent->next; |
| Junio C Hamano | aeeae1b | 2009-01-28 07:19:30 | [diff] [blame] | 1150 | if (p) |
| 1151 | p->object.flags |= UNINTERESTING; |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 1152 | if (repo_parse_commit_gently(revs->repo, p, 1) < 0) |
| Junio C Hamano | aeeae1b | 2009-01-28 07:19:30 | [diff] [blame] | 1153 | continue; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1154 | if (p->parents) |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 1155 | mark_parents_uninteresting(revs, p); |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1156 | if (p->object.flags & SEEN) |
| 1157 | continue; |
| Patrick Steinhardt | b2025da | 2021-04-09 11:27:57 | [diff] [blame] | 1158 | p->object.flags |= (SEEN | NOT_USER_GIVEN); |
| Derrick Stolee | 5284fc5 | 2018-11-01 13:46:21 | [diff] [blame] | 1159 | if (list) |
| Jeff King | 8320b1d | 2019-04-04 01:41:09 | [diff] [blame] | 1160 | commit_list_insert_by_date(p, list); |
| 1161 | if (queue) |
| 1162 | prio_queue_put(queue, p); |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 1163 | if (revs->exclude_first_parent_only) |
| 1164 | break; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1165 | } |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1166 | return 0; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * Ok, the commit wasn't uninteresting. Try to |
| 1171 | * simplify the commit history and find the parent |
| 1172 | * that has no differences in the path set if one exists. |
| 1173 | */ |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 1174 | try_to_simplify_commit(revs, commit); |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1175 | |
| Linus Torvalds | ba1d450 | 2006-04-15 19:09:56 | [diff] [blame] | 1176 | if (revs->no_walk) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1177 | return 0; |
| Linus Torvalds | ba1d450 | 2006-04-15 19:09:56 | [diff] [blame] | 1178 | |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1179 | pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH)); |
| Junio C Hamano | 0053e90 | 2007-03-13 08:57:22 | [diff] [blame] | 1180 | |
| Stephen R. van den Berg | d9c292e | 2008-04-27 17:32:46 | [diff] [blame] | 1181 | for (parent = commit->parents; parent; parent = parent->next) { |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1182 | struct commit *p = parent->item; |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 1183 | int gently = revs->ignore_missing_links || |
| Karthik Nayak | 9830926 | 2023-10-27 07:59:29 | [diff] [blame] | 1184 | revs->exclude_promisor_objects || |
| 1185 | revs->do_not_die_on_missing_objects; |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 1186 | if (repo_parse_commit_gently(revs->repo, p, gently) < 0) { |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 1187 | if (revs->exclude_promisor_objects && |
| 1188 | is_promisor_object(&p->object.oid)) { |
| 1189 | if (revs->first_parent_only) |
| 1190 | break; |
| 1191 | continue; |
| 1192 | } |
| Karthik Nayak | 9830926 | 2023-10-27 07:59:29 | [diff] [blame] | 1193 | |
| 1194 | if (revs->do_not_die_on_missing_objects) |
| 1195 | oidset_insert(&revs->missing_commits, &p->object.oid); |
| 1196 | else |
| 1197 | return -1; /* corrupt repository */ |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 1198 | } |
| Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 05:28:24 | [diff] [blame] | 1199 | if (revs->sources) { |
| 1200 | char **slot = revision_sources_at(revs->sources, p); |
| 1201 | |
| 1202 | if (!*slot) |
| 1203 | *slot = *revision_sources_at(revs->sources, commit); |
| 1204 | } |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1205 | p->object.flags |= pass_flags; |
| Lars Hjemli | ad1012e | 2008-05-12 15:12:36 | [diff] [blame] | 1206 | if (!(p->object.flags & SEEN)) { |
| Patrick Steinhardt | b2025da | 2021-04-09 11:27:57 | [diff] [blame] | 1207 | p->object.flags |= (SEEN | NOT_USER_GIVEN); |
| Derrick Stolee | 5284fc5 | 2018-11-01 13:46:21 | [diff] [blame] | 1208 | if (list) |
| Jeff King | 8320b1d | 2019-04-04 01:41:09 | [diff] [blame] | 1209 | commit_list_insert_by_date(p, list); |
| 1210 | if (queue) |
| 1211 | prio_queue_put(queue, p); |
| Lars Hjemli | ad1012e | 2008-05-12 15:12:36 | [diff] [blame] | 1212 | } |
| Junio C Hamano | 60d30b0 | 2008-08-01 05:17:13 | [diff] [blame] | 1213 | if (revs->first_parent_only) |
| Stephen R. van den Berg | d9c292e | 2008-04-27 17:32:46 | [diff] [blame] | 1214 | break; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1215 | } |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1216 | return 0; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1217 | } |
| 1218 | |
| Johannes Schindelin | 36d56de | 2007-07-10 13:50:49 | [diff] [blame] | 1219 | static void cherry_pick_list(struct commit_list *list, struct rev_info *revs) |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1220 | { |
| 1221 | struct commit_list *p; |
| 1222 | int left_count = 0, right_count = 0; |
| 1223 | int left_first; |
| 1224 | struct patch_ids ids; |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 1225 | unsigned cherry_flag; |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1226 | |
| 1227 | /* First count the commits on the left and on the right */ |
| 1228 | for (p = list; p; p = p->next) { |
| 1229 | struct commit *commit = p->item; |
| 1230 | unsigned flags = commit->object.flags; |
| 1231 | if (flags & BOUNDARY) |
| 1232 | ; |
| 1233 | else if (flags & SYMMETRIC_LEFT) |
| 1234 | left_count++; |
| 1235 | else |
| 1236 | right_count++; |
| 1237 | } |
| 1238 | |
| Thomas Rast | 36c0797 | 2010-02-20 11:42:04 | [diff] [blame] | 1239 | if (!left_count || !right_count) |
| 1240 | return; |
| 1241 | |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1242 | left_first = left_count < right_count; |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 1243 | init_patch_ids(revs->repo, &ids); |
| Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 15:02:38 | [diff] [blame] | 1244 | ids.diffopts.pathspec = revs->diffopt.pathspec; |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1245 | |
| 1246 | /* Compute patch-ids for one side */ |
| 1247 | for (p = list; p; p = p->next) { |
| 1248 | struct commit *commit = p->item; |
| 1249 | unsigned flags = commit->object.flags; |
| 1250 | |
| 1251 | if (flags & BOUNDARY) |
| 1252 | continue; |
| 1253 | /* |
| 1254 | * If we have fewer left, left_first is set and we omit |
| 1255 | * commits on the right branch in this loop. If we have |
| 1256 | * fewer right, we skip the left ones. |
| 1257 | */ |
| 1258 | if (left_first != !!(flags & SYMMETRIC_LEFT)) |
| 1259 | continue; |
| Kevin Willford | 683f17e | 2016-07-29 16:19:18 | [diff] [blame] | 1260 | add_commit_patch_id(commit, &ids); |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1261 | } |
| 1262 | |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 1263 | /* either cherry_mark or cherry_pick are true */ |
| 1264 | cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN; |
| 1265 | |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1266 | /* Check the other side */ |
| 1267 | for (p = list; p; p = p->next) { |
| 1268 | struct commit *commit = p->item; |
| 1269 | struct patch_id *id; |
| 1270 | unsigned flags = commit->object.flags; |
| 1271 | |
| 1272 | if (flags & BOUNDARY) |
| 1273 | continue; |
| 1274 | /* |
| 1275 | * If we have fewer left, left_first is set and we omit |
| 1276 | * commits on the left branch in this loop. |
| 1277 | */ |
| 1278 | if (left_first == !!(flags & SYMMETRIC_LEFT)) |
| 1279 | continue; |
| 1280 | |
| 1281 | /* |
| 1282 | * Have we seen the same patch id? |
| 1283 | */ |
| Jeff King | c9e3a4e | 2021-01-12 15:52:32 | [diff] [blame] | 1284 | id = patch_id_iter_first(commit, &ids); |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1285 | if (!id) |
| 1286 | continue; |
| Kevin Willford | 683f17e | 2016-07-29 16:19:18 | [diff] [blame] | 1287 | |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 1288 | commit->object.flags |= cherry_flag; |
| Jeff King | c9e3a4e | 2021-01-12 15:52:32 | [diff] [blame] | 1289 | do { |
| 1290 | id->commit->object.flags |= cherry_flag; |
| 1291 | } while ((id = patch_id_iter_next(id, &ids))); |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | free_patch_ids(&ids); |
| 1295 | } |
| 1296 | |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1297 | /* How many extra uninteresting commits we want to see.. */ |
| 1298 | #define SLOP 5 |
| 1299 | |
| Johannes Schindelin | dddbad7 | 2017-04-26 19:29:31 | [diff] [blame] | 1300 | static int still_interesting(struct commit_list *src, timestamp_t date, int slop, |
| Jeff King | b6e8a3b | 2015-04-17 22:11:04 | [diff] [blame] | 1301 | struct commit **interesting_cache) |
| Linus Torvalds | 3131b71 | 2008-02-09 22:02:07 | [diff] [blame] | 1302 | { |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1303 | /* |
| 1304 | * No source list at all? We're definitely done.. |
| 1305 | */ |
| 1306 | if (!src) |
| 1307 | return 0; |
| 1308 | |
| 1309 | /* |
| 1310 | * Does the destination list contain entries with a date |
| 1311 | * before the source list? Definitely _not_ done. |
| 1312 | */ |
| Kacper Kornet | c19d1b4 | 2013-03-22 18:38:19 | [diff] [blame] | 1313 | if (date <= src->item->date) |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1314 | return SLOP; |
| 1315 | |
| 1316 | /* |
| 1317 | * Does the source list still have interesting commits in |
| 1318 | * it? Definitely not done.. |
| 1319 | */ |
| Jeff King | b6e8a3b | 2015-04-17 22:11:04 | [diff] [blame] | 1320 | if (!everybody_uninteresting(src, interesting_cache)) |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1321 | return SLOP; |
| 1322 | |
| 1323 | /* Ok, we're closing in.. */ |
| 1324 | return slop-1; |
| Linus Torvalds | 3131b71 | 2008-02-09 22:02:07 | [diff] [blame] | 1325 | } |
| 1326 | |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1327 | /* |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1328 | * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B" |
| 1329 | * computes commits that are ancestors of B but not ancestors of A but |
| 1330 | * further limits the result to those that have any of C in their |
| 1331 | * ancestry path (i.e. are either ancestors of any of C, descendants |
| 1332 | * of any of C, or are any of C). If --ancestry-path is specified with |
| 1333 | * no commit, we use all bottom commits for C. |
| 1334 | * |
| 1335 | * Before this function is called, ancestors of C will have already |
| 1336 | * been marked with ANCESTRY_PATH previously. |
| 1337 | * |
| 1338 | * This takes the list of bottom commits and the result of "A..B" |
| 1339 | * without --ancestry-path, and limits the latter further to the ones |
| 1340 | * that have any of C in their ancestry path. Since the ancestors of C |
| 1341 | * have already been marked (a prerequisite of this function), we just |
| 1342 | * need to mark the descendants, then exclude any commit that does not |
| 1343 | * have any of these marks. |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1344 | */ |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1345 | static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list) |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1346 | { |
| 1347 | struct commit_list *p; |
| 1348 | struct commit_list *rlist = NULL; |
| 1349 | int made_progress; |
| 1350 | |
| 1351 | /* |
| 1352 | * Reverse the list so that it will be likely that we would |
| 1353 | * process parents before children. |
| 1354 | */ |
| 1355 | for (p = list; p; p = p->next) |
| 1356 | commit_list_insert(p->item, &rlist); |
| 1357 | |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1358 | for (p = bottoms; p; p = p->next) |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1359 | p->item->object.flags |= TMP_MARK; |
| 1360 | |
| 1361 | /* |
| 1362 | * Mark the ones that can reach bottom commits in "list", |
| 1363 | * in a bottom-up fashion. |
| 1364 | */ |
| 1365 | do { |
| 1366 | made_progress = 0; |
| 1367 | for (p = rlist; p; p = p->next) { |
| 1368 | struct commit *c = p->item; |
| 1369 | struct commit_list *parents; |
| 1370 | if (c->object.flags & (TMP_MARK | UNINTERESTING)) |
| 1371 | continue; |
| 1372 | for (parents = c->parents; |
| 1373 | parents; |
| 1374 | parents = parents->next) { |
| 1375 | if (!(parents->item->object.flags & TMP_MARK)) |
| 1376 | continue; |
| 1377 | c->object.flags |= TMP_MARK; |
| 1378 | made_progress = 1; |
| 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | } while (made_progress); |
| 1383 | |
| 1384 | /* |
| 1385 | * NEEDSWORK: decide if we want to remove parents that are |
| 1386 | * not marked with TMP_MARK from commit->parents for commits |
| 1387 | * in the resulting list. We may not want to do that, though. |
| 1388 | */ |
| 1389 | |
| 1390 | /* |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1391 | * The ones that are not marked with either TMP_MARK or |
| 1392 | * ANCESTRY_PATH are uninteresting |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1393 | */ |
| 1394 | for (p = list; p; p = p->next) { |
| 1395 | struct commit *c = p->item; |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1396 | if (c->object.flags & (TMP_MARK | ANCESTRY_PATH)) |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1397 | continue; |
| 1398 | c->object.flags |= UNINTERESTING; |
| 1399 | } |
| 1400 | |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1401 | /* We are done with TMP_MARK and ANCESTRY_PATH */ |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1402 | for (p = list; p; p = p->next) |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1403 | p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH); |
| 1404 | for (p = bottoms; p; p = p->next) |
| 1405 | p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH); |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1406 | free_commit_list(rlist); |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1410 | * Before walking the history, add the set of "negative" refs the |
| 1411 | * caller has asked to exclude to the bottom list. |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1412 | * |
| 1413 | * This is used to compute "rev-list --ancestry-path A..B", as we need |
| 1414 | * to filter the result of "A..B" further to the ones that can actually |
| 1415 | * reach A. |
| 1416 | */ |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1417 | static void collect_bottom_commits(struct commit_list *list, |
| 1418 | struct commit_list **bottom) |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1419 | { |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1420 | struct commit_list *elem; |
| Kevin Bracey | 7f34a46 | 2013-05-16 15:32:38 | [diff] [blame] | 1421 | for (elem = list; elem; elem = elem->next) |
| 1422 | if (elem->item->object.flags & BOTTOM) |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1423 | commit_list_insert(elem->item, bottom); |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1424 | } |
| 1425 | |
| Michael J Gruber | 60adf7d | 2011-02-21 16:09:11 | [diff] [blame] | 1426 | /* Assumes either left_only or right_only is set */ |
| 1427 | static void limit_left_right(struct commit_list *list, struct rev_info *revs) |
| 1428 | { |
| 1429 | struct commit_list *p; |
| 1430 | |
| 1431 | for (p = list; p; p = p->next) { |
| 1432 | struct commit *commit = p->item; |
| 1433 | |
| 1434 | if (revs->right_only) { |
| 1435 | if (commit->object.flags & SYMMETRIC_LEFT) |
| 1436 | commit->object.flags |= SHOWN; |
| 1437 | } else /* revs->left_only is set */ |
| 1438 | if (!(commit->object.flags & SYMMETRIC_LEFT)) |
| 1439 | commit->object.flags |= SHOWN; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1443 | static int limit_list(struct rev_info *revs) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1444 | { |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1445 | int slop = SLOP; |
| Johannes Schindelin | dddbad7 | 2017-04-26 19:29:31 | [diff] [blame] | 1446 | timestamp_t date = TIME_MAX; |
| Andrzej Hunt | db69bf6 | 2021-04-25 14:16:08 | [diff] [blame] | 1447 | struct commit_list *original_list = revs->commits; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1448 | struct commit_list *newlist = NULL; |
| 1449 | struct commit_list **p = &newlist; |
| Jeff King | b6e8a3b | 2015-04-17 22:11:04 | [diff] [blame] | 1450 | struct commit *interesting_cache = NULL; |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1451 | |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1452 | if (revs->ancestry_path_implicit_bottoms) { |
| 1453 | collect_bottom_commits(original_list, |
| 1454 | &revs->ancestry_path_bottoms); |
| 1455 | if (!revs->ancestry_path_bottoms) |
| Johan Herland | 97b03c3 | 2010-06-03 23:17:36 | [diff] [blame] | 1456 | die("--ancestry-path given but there are no bottom commits"); |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1457 | } |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1458 | |
| Andrzej Hunt | db69bf6 | 2021-04-25 14:16:08 | [diff] [blame] | 1459 | while (original_list) { |
| 1460 | struct commit *commit = pop_commit(&original_list); |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1461 | struct object *obj = &commit->object; |
| Linus Torvalds | cdcefbc | 2007-11-03 18:11:10 | [diff] [blame] | 1462 | show_early_output_fn_t show; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1463 | |
| Jeff King | b6e8a3b | 2015-04-17 22:11:04 | [diff] [blame] | 1464 | if (commit == interesting_cache) |
| 1465 | interesting_cache = NULL; |
| 1466 | |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1467 | if (revs->max_age != -1 && (commit->date < revs->max_age)) |
| 1468 | obj->flags |= UNINTERESTING; |
| Andrzej Hunt | db69bf6 | 2021-04-25 14:16:08 | [diff] [blame] | 1469 | if (process_parents(revs, commit, &original_list, NULL) < 0) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1470 | return -1; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1471 | if (obj->flags & UNINTERESTING) { |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 1472 | mark_parents_uninteresting(revs, commit); |
| Andrzej Hunt | db69bf6 | 2021-04-25 14:16:08 | [diff] [blame] | 1473 | slop = still_interesting(original_list, date, slop, &interesting_cache); |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1474 | if (slop) |
| Linus Torvalds | 3131b71 | 2008-02-09 22:02:07 | [diff] [blame] | 1475 | continue; |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1476 | break; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1477 | } |
| René Scharfe | 01faa91 | 2020-07-04 12:56:32 | [diff] [blame] | 1478 | if (revs->min_age != -1 && (commit->date > revs->min_age) && |
| 1479 | !revs->line_level_traverse) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1480 | continue; |
| Miklos Vajna | 9669778 | 2022-04-23 12:59:57 | [diff] [blame] | 1481 | if (revs->max_age_as_filter != -1 && |
| 1482 | (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse) |
| 1483 | continue; |
| Linus Torvalds | 7d00419 | 2008-03-18 01:56:33 | [diff] [blame] | 1484 | date = commit->date; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1485 | p = &commit_list_insert(commit, p)->next; |
| Linus Torvalds | cdcefbc | 2007-11-03 18:11:10 | [diff] [blame] | 1486 | |
| 1487 | show = show_early_output; |
| 1488 | if (!show) |
| 1489 | continue; |
| 1490 | |
| 1491 | show(revs, newlist); |
| 1492 | show_early_output = NULL; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1493 | } |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 1494 | if (revs->cherry_pick || revs->cherry_mark) |
| Johannes Schindelin | 36d56de | 2007-07-10 13:50:49 | [diff] [blame] | 1495 | cherry_pick_list(newlist, revs); |
| Junio C Hamano | d7a17ca | 2007-04-09 10:40:38 | [diff] [blame] | 1496 | |
| Michael J Gruber | 60adf7d | 2011-02-21 16:09:11 | [diff] [blame] | 1497 | if (revs->left_only || revs->right_only) |
| 1498 | limit_left_right(newlist, revs); |
| 1499 | |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 1500 | if (revs->ancestry_path) |
| 1501 | limit_to_ancestry(revs->ancestry_path_bottoms, newlist); |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 1502 | |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1503 | /* |
| 1504 | * Check if any commits have become TREESAME by some of their parents |
| 1505 | * becoming UNINTERESTING. |
| 1506 | */ |
| Andrzej Hunt | db69bf6 | 2021-04-25 14:16:08 | [diff] [blame] | 1507 | if (limiting_can_increase_treesame(revs)) { |
| 1508 | struct commit_list *list = NULL; |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1509 | for (list = newlist; list; list = list->next) { |
| 1510 | struct commit *c = list->item; |
| 1511 | if (c->object.flags & (UNINTERESTING | TREESAME)) |
| 1512 | continue; |
| 1513 | update_treesame(revs, c); |
| 1514 | } |
| Andrzej Hunt | db69bf6 | 2021-04-25 14:16:08 | [diff] [blame] | 1515 | } |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 1516 | |
| Andrzej Hunt | db69bf6 | 2021-04-25 14:16:08 | [diff] [blame] | 1517 | free_commit_list(original_list); |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1518 | revs->commits = newlist; |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 1519 | return 0; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 1520 | } |
| 1521 | |
| Michael Haggerty | df835d3 | 2013-05-25 09:08:02 | [diff] [blame] | 1522 | /* |
| 1523 | * Add an entry to refs->cmdline with the specified information. |
| 1524 | * *name is copied. |
| 1525 | */ |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 1526 | static void add_rev_cmdline(struct rev_info *revs, |
| 1527 | struct object *item, |
| 1528 | const char *name, |
| 1529 | int whence, |
| 1530 | unsigned flags) |
| 1531 | { |
| 1532 | struct rev_cmdline_info *info = &revs->cmdline; |
| Ramsay Jones | 071bcaa | 2017-09-21 16:49:38 | [diff] [blame] | 1533 | unsigned int nr = info->nr; |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 1534 | |
| 1535 | ALLOC_GROW(info->rev, nr + 1, info->alloc); |
| 1536 | info->rev[nr].item = item; |
| Michael Haggerty | df835d3 | 2013-05-25 09:08:02 | [diff] [blame] | 1537 | info->rev[nr].name = xstrdup(name); |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 1538 | info->rev[nr].whence = whence; |
| 1539 | info->rev[nr].flags = flags; |
| 1540 | info->nr++; |
| 1541 | } |
| 1542 | |
| Kevin Bracey | a765499 | 2013-05-13 15:00:47 | [diff] [blame] | 1543 | static void add_rev_cmdline_list(struct rev_info *revs, |
| 1544 | struct commit_list *commit_list, |
| 1545 | int whence, |
| 1546 | unsigned flags) |
| 1547 | { |
| 1548 | while (commit_list) { |
| 1549 | struct object *object = &commit_list->item->object; |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 1550 | add_rev_cmdline(revs, object, oid_to_hex(&object->oid), |
| Kevin Bracey | a765499 | 2013-05-13 15:00:47 | [diff] [blame] | 1551 | whence, flags); |
| 1552 | commit_list = commit_list->next; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1556 | int ref_excluded(const struct ref_exclusions *exclusions, const char *path) |
| Junio C Hamano | e7b432c | 2013-08-30 23:37:55 | [diff] [blame] | 1557 | { |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 1558 | const char *stripped_path = strip_namespace(path); |
| Junio C Hamano | e7b432c | 2013-08-30 23:37:55 | [diff] [blame] | 1559 | struct string_list_item *item; |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 1560 | |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1561 | for_each_string_list_item(item, &exclusions->excluded_refs) { |
| Ævar Arnfjörð Bjarmason | 55d3426 | 2017-06-22 21:38:08 | [diff] [blame] | 1562 | if (!wildmatch(item->string, path, 0)) |
| Junio C Hamano | e7b432c | 2013-08-30 23:37:55 | [diff] [blame] | 1563 | return 1; |
| 1564 | } |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 1565 | |
| 1566 | if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs)) |
| 1567 | return 1; |
| 1568 | |
| Junio C Hamano | e7b432c | 2013-08-30 23:37:55 | [diff] [blame] | 1569 | return 0; |
| 1570 | } |
| 1571 | |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1572 | void init_ref_exclusions(struct ref_exclusions *exclusions) |
| Patrick Steinhardt | 05b9425 | 2022-11-17 05:46:47 | [diff] [blame] | 1573 | { |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1574 | struct ref_exclusions blank = REF_EXCLUSIONS_INIT; |
| 1575 | memcpy(exclusions, &blank, sizeof(*exclusions)); |
| Patrick Steinhardt | 05b9425 | 2022-11-17 05:46:47 | [diff] [blame] | 1576 | } |
| 1577 | |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1578 | void clear_ref_exclusions(struct ref_exclusions *exclusions) |
| Patrick Steinhardt | 05b9425 | 2022-11-17 05:46:47 | [diff] [blame] | 1579 | { |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1580 | string_list_clear(&exclusions->excluded_refs, 0); |
| Taylor Blau | c45841f | 2023-07-10 21:12:33 | [diff] [blame] | 1581 | strvec_clear(&exclusions->hidden_refs); |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 1582 | exclusions->hidden_refs_configured = 0; |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude) |
| 1586 | { |
| 1587 | string_list_append(&exclusions->excluded_refs, exclude); |
| Patrick Steinhardt | 05b9425 | 2022-11-17 05:46:47 | [diff] [blame] | 1588 | } |
| 1589 | |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 1590 | struct exclude_hidden_refs_cb { |
| 1591 | struct ref_exclusions *exclusions; |
| 1592 | const char *section; |
| 1593 | }; |
| 1594 | |
| Glen Choo | a4e7e31 | 2023-06-28 19:26:22 | [diff] [blame] | 1595 | static int hide_refs_config(const char *var, const char *value, |
| 1596 | const struct config_context *ctx UNUSED, |
| 1597 | void *cb_data) |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 1598 | { |
| 1599 | struct exclude_hidden_refs_cb *cb = cb_data; |
| 1600 | cb->exclusions->hidden_refs_configured = 1; |
| 1601 | return parse_hide_refs_config(var, value, cb->section, |
| 1602 | &cb->exclusions->hidden_refs); |
| 1603 | } |
| 1604 | |
| 1605 | void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section) |
| 1606 | { |
| 1607 | struct exclude_hidden_refs_cb cb; |
| 1608 | |
| Eric Wong | c6ce27a | 2023-02-12 09:04:26 | [diff] [blame] | 1609 | if (strcmp(section, "fetch") && strcmp(section, "receive") && |
| 1610 | strcmp(section, "uploadpack")) |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 1611 | die(_("unsupported section for hidden refs: %s"), section); |
| 1612 | |
| 1613 | if (exclusions->hidden_refs_configured) |
| 1614 | die(_("--exclude-hidden= passed more than once")); |
| 1615 | |
| 1616 | cb.exclusions = exclusions; |
| 1617 | cb.section = section; |
| 1618 | |
| 1619 | git_config(hide_refs_config, &cb); |
| 1620 | } |
| 1621 | |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1622 | struct all_refs_cb { |
| 1623 | int all_flags; |
| Shawn O. Pearce | 71b03b4 | 2006-12-22 00:49:06 | [diff] [blame] | 1624 | int warned_bad_reflog; |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1625 | struct rev_info *all_revs; |
| 1626 | const char *name_for_errormsg; |
| Nguyễn Thái Ngọc Duy | ab3e1f7 | 2018-10-21 08:08:56 | [diff] [blame] | 1627 | struct worktree *wt; |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1628 | }; |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 1629 | |
| Michael Haggerty | a217dcb | 2015-05-25 18:38:30 | [diff] [blame] | 1630 | static int handle_one_ref(const char *path, const struct object_id *oid, |
| Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 17:09:48 | [diff] [blame] | 1631 | int flag UNUSED, |
| Jeff King | 63e14ee | 2022-08-19 10:08:32 | [diff] [blame] | 1632 | void *cb_data) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 1633 | { |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1634 | struct all_refs_cb *cb = cb_data; |
| Junio C Hamano | e7b432c | 2013-08-30 23:37:55 | [diff] [blame] | 1635 | struct object *object; |
| 1636 | |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1637 | if (ref_excluded(&cb->all_revs->ref_excludes, path)) |
| Junio C Hamano | e7b432c | 2013-08-30 23:37:55 | [diff] [blame] | 1638 | return 0; |
| 1639 | |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 1640 | object = get_reference(cb->all_revs, path, oid, cb->all_flags); |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 1641 | add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags); |
| Patrick Steinhardt | bf9c0cb | 2021-08-09 08:11:54 | [diff] [blame] | 1642 | add_pending_object(cb->all_revs, object, path); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 1643 | return 0; |
| 1644 | } |
| 1645 | |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 1646 | static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs, |
| 1647 | unsigned flags) |
| 1648 | { |
| 1649 | cb->all_revs = revs; |
| 1650 | cb->all_flags = flags; |
| Jeff King | 7ba8262 | 2017-08-02 22:25:27 | [diff] [blame] | 1651 | revs->rev_input_given = 1; |
| Nguyễn Thái Ngọc Duy | ab3e1f7 | 2018-10-21 08:08:56 | [diff] [blame] | 1652 | cb->wt = NULL; |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 1653 | } |
| 1654 | |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 1655 | static void handle_refs(struct ref_store *refs, |
| 1656 | struct rev_info *revs, unsigned flags, |
| 1657 | int (*for_each)(struct ref_store *, each_ref_fn, void *)) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 1658 | { |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1659 | struct all_refs_cb cb; |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 1660 | |
| 1661 | if (!refs) { |
| 1662 | /* this could happen with uninitialized submodules */ |
| 1663 | return; |
| 1664 | } |
| 1665 | |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 1666 | init_all_refs_cb(&cb, revs, flags); |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 1667 | for_each(refs, handle_one_ref, &cb); |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1668 | } |
| 1669 | |
| brian m. carlson | 9461d27 | 2017-02-21 23:47:32 | [diff] [blame] | 1670 | static void handle_one_reflog_commit(struct object_id *oid, void *cb_data) |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1671 | { |
| 1672 | struct all_refs_cb *cb = cb_data; |
| brian m. carlson | 9461d27 | 2017-02-21 23:47:32 | [diff] [blame] | 1673 | if (!is_null_oid(oid)) { |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 1674 | struct object *o = parse_object(cb->all_revs->repo, oid); |
| Shawn O. Pearce | 71b03b4 | 2006-12-22 00:49:06 | [diff] [blame] | 1675 | if (o) { |
| 1676 | o->flags |= cb->all_flags; |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 1677 | /* ??? CMDLINEFLAGS ??? */ |
| Shawn O. Pearce | 71b03b4 | 2006-12-22 00:49:06 | [diff] [blame] | 1678 | add_pending_object(cb->all_revs, o, ""); |
| 1679 | } |
| 1680 | else if (!cb->warned_bad_reflog) { |
| Theodore Ts'o | 46efd2d | 2007-03-30 23:07:05 | [diff] [blame] | 1681 | warning("reflog of '%s' references pruned commits", |
| Shawn O. Pearce | 71b03b4 | 2006-12-22 00:49:06 | [diff] [blame] | 1682 | cb->name_for_errormsg); |
| 1683 | cb->warned_bad_reflog = 1; |
| 1684 | } |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1685 | } |
| Shawn O. Pearce | 71b03b4 | 2006-12-22 00:49:06 | [diff] [blame] | 1686 | } |
| 1687 | |
| brian m. carlson | 9461d27 | 2017-02-21 23:47:32 | [diff] [blame] | 1688 | static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid, |
| Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 17:09:48 | [diff] [blame] | 1689 | const char *email UNUSED, |
| 1690 | timestamp_t timestamp UNUSED, |
| 1691 | int tz UNUSED, |
| 1692 | const char *message UNUSED, |
| Jeff King | c006e9f | 2022-08-19 10:08:35 | [diff] [blame] | 1693 | void *cb_data) |
| Shawn O. Pearce | 71b03b4 | 2006-12-22 00:49:06 | [diff] [blame] | 1694 | { |
| brian m. carlson | 9461d27 | 2017-02-21 23:47:32 | [diff] [blame] | 1695 | handle_one_reflog_commit(ooid, cb_data); |
| 1696 | handle_one_reflog_commit(noid, cb_data); |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1697 | return 0; |
| 1698 | } |
| 1699 | |
| Patrick Steinhardt | 31f8983 | 2024-02-21 12:37:39 | [diff] [blame] | 1700 | static int handle_one_reflog(const char *refname_in_wt, void *cb_data) |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1701 | { |
| 1702 | struct all_refs_cb *cb = cb_data; |
| Nguyễn Thái Ngọc Duy | ab3e1f7 | 2018-10-21 08:08:56 | [diff] [blame] | 1703 | struct strbuf refname = STRBUF_INIT; |
| 1704 | |
| Shawn O. Pearce | 71b03b4 | 2006-12-22 00:49:06 | [diff] [blame] | 1705 | cb->warned_bad_reflog = 0; |
| Nguyễn Thái Ngọc Duy | ab3e1f7 | 2018-10-21 08:08:56 | [diff] [blame] | 1706 | strbuf_worktree_ref(cb->wt, &refname, refname_in_wt); |
| 1707 | cb->name_for_errormsg = refname.buf; |
| 1708 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), |
| 1709 | refname.buf, |
| Nguyễn Thái Ngọc Duy | acd9544 | 2017-08-23 12:37:01 | [diff] [blame] | 1710 | handle_one_reflog_ent, cb_data); |
| Nguyễn Thái Ngọc Duy | ab3e1f7 | 2018-10-21 08:08:56 | [diff] [blame] | 1711 | strbuf_release(&refname); |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
| 1714 | |
| Nguyễn Thái Ngọc Duy | acd9544 | 2017-08-23 12:37:01 | [diff] [blame] | 1715 | static void add_other_reflogs_to_pending(struct all_refs_cb *cb) |
| 1716 | { |
| 1717 | struct worktree **worktrees, **p; |
| 1718 | |
| Eric Sunshine | 03f2465 | 2020-06-19 23:35:44 | [diff] [blame] | 1719 | worktrees = get_worktrees(); |
| Nguyễn Thái Ngọc Duy | acd9544 | 2017-08-23 12:37:01 | [diff] [blame] | 1720 | for (p = worktrees; *p; p++) { |
| 1721 | struct worktree *wt = *p; |
| 1722 | |
| 1723 | if (wt->is_current) |
| 1724 | continue; |
| 1725 | |
| Nguyễn Thái Ngọc Duy | ab3e1f7 | 2018-10-21 08:08:56 | [diff] [blame] | 1726 | cb->wt = wt; |
| 1727 | refs_for_each_reflog(get_worktree_ref_store(wt), |
| Nguyễn Thái Ngọc Duy | acd9544 | 2017-08-23 12:37:01 | [diff] [blame] | 1728 | handle_one_reflog, |
| 1729 | cb); |
| 1730 | } |
| 1731 | free_worktrees(worktrees); |
| 1732 | } |
| 1733 | |
| Jeff King | 718ccc9 | 2014-10-15 22:38:31 | [diff] [blame] | 1734 | void add_reflogs_to_pending(struct rev_info *revs, unsigned flags) |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1735 | { |
| 1736 | struct all_refs_cb cb; |
| Michael Haggerty | 2b2a5be | 2015-05-25 18:38:28 | [diff] [blame] | 1737 | |
| Junio C Hamano | 6304929 | 2006-12-19 01:25:28 | [diff] [blame] | 1738 | cb.all_revs = revs; |
| 1739 | cb.all_flags = flags; |
| Nguyễn Thái Ngọc Duy | ab3e1f7 | 2018-10-21 08:08:56 | [diff] [blame] | 1740 | cb.wt = NULL; |
| Michael Haggerty | a89caf4 | 2015-05-25 18:39:03 | [diff] [blame] | 1741 | for_each_reflog(handle_one_reflog, &cb); |
| Nguyễn Thái Ngọc Duy | acd9544 | 2017-08-23 12:37:01 | [diff] [blame] | 1742 | |
| 1743 | if (!revs->single_worktree) |
| 1744 | add_other_reflogs_to_pending(&cb); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 1745 | } |
| 1746 | |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1747 | static void add_cache_tree(struct cache_tree *it, struct rev_info *revs, |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1748 | struct strbuf *path, unsigned int flags) |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1749 | { |
| 1750 | size_t baselen = path->len; |
| 1751 | int i; |
| 1752 | |
| 1753 | if (it->entry_count >= 0) { |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 1754 | struct tree *tree = lookup_tree(revs->repo, &it->oid); |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1755 | tree->object.flags |= flags; |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1756 | add_pending_object_with_path(revs, &tree->object, "", |
| 1757 | 040000, path->buf); |
| 1758 | } |
| 1759 | |
| 1760 | for (i = 0; i < it->subtree_nr; i++) { |
| 1761 | struct cache_tree_sub *sub = it->down[i]; |
| 1762 | strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name); |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1763 | add_cache_tree(sub->cache_tree, revs, path, flags); |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1764 | strbuf_setlen(path, baselen); |
| 1765 | } |
| 1766 | |
| 1767 | } |
| 1768 | |
| Junio C Hamano | 5a5ea14 | 2022-06-09 23:44:20 | [diff] [blame] | 1769 | static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs) |
| 1770 | { |
| 1771 | struct string_list_item *item; |
| 1772 | struct string_list *resolve_undo = istate->resolve_undo; |
| 1773 | |
| 1774 | if (!resolve_undo) |
| 1775 | return; |
| 1776 | |
| 1777 | for_each_string_list_item(item, resolve_undo) { |
| 1778 | const char *path = item->string; |
| 1779 | struct resolve_undo_info *ru = item->util; |
| 1780 | int i; |
| 1781 | |
| 1782 | if (!ru) |
| 1783 | continue; |
| 1784 | for (i = 0; i < 3; i++) { |
| 1785 | struct blob *blob; |
| 1786 | |
| 1787 | if (!ru->mode[i] || !S_ISREG(ru->mode[i])) |
| 1788 | continue; |
| 1789 | |
| 1790 | blob = lookup_blob(revs->repo, &ru->oid[i]); |
| 1791 | if (!blob) { |
| 1792 | warning(_("resolve-undo records `%s` which is missing"), |
| 1793 | oid_to_hex(&ru->oid[i])); |
| 1794 | continue; |
| 1795 | } |
| 1796 | add_pending_object_with_path(revs, &blob->object, "", |
| 1797 | ru->mode[i], path); |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | |
| Nguyễn Thái Ngọc Duy | 6c3d818 | 2017-08-23 12:36:51 | [diff] [blame] | 1802 | static void do_add_index_objects_to_pending(struct rev_info *revs, |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1803 | struct index_state *istate, |
| 1804 | unsigned int flags) |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1805 | { |
| 1806 | int i; |
| 1807 | |
| Derrick Stolee | f5fed74 | 2021-04-01 01:50:00 | [diff] [blame] | 1808 | /* TODO: audit for interaction with sparse-index. */ |
| 1809 | ensure_full_index(istate); |
| Nguyễn Thái Ngọc Duy | 6c3d818 | 2017-08-23 12:36:51 | [diff] [blame] | 1810 | for (i = 0; i < istate->cache_nr; i++) { |
| 1811 | struct cache_entry *ce = istate->cache[i]; |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1812 | struct blob *blob; |
| 1813 | |
| 1814 | if (S_ISGITLINK(ce->ce_mode)) |
| 1815 | continue; |
| 1816 | |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 1817 | blob = lookup_blob(revs->repo, &ce->oid); |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1818 | if (!blob) |
| 1819 | die("unable to add index blob to traversal"); |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1820 | blob->object.flags |= flags; |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1821 | add_pending_object_with_path(revs, &blob->object, "", |
| 1822 | ce->ce_mode, ce->name); |
| 1823 | } |
| 1824 | |
| Nguyễn Thái Ngọc Duy | 6c3d818 | 2017-08-23 12:36:51 | [diff] [blame] | 1825 | if (istate->cache_tree) { |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1826 | struct strbuf path = STRBUF_INIT; |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1827 | add_cache_tree(istate->cache_tree, revs, &path, flags); |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1828 | strbuf_release(&path); |
| 1829 | } |
| Junio C Hamano | 5a5ea14 | 2022-06-09 23:44:20 | [diff] [blame] | 1830 | |
| 1831 | add_resolve_undo_to_pending(istate, revs); |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 1832 | } |
| 1833 | |
| Nguyễn Thái Ngọc Duy | 6c3d818 | 2017-08-23 12:36:51 | [diff] [blame] | 1834 | void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags) |
| 1835 | { |
| Nguyễn Thái Ngọc Duy | be489d0 | 2017-08-23 12:36:52 | [diff] [blame] | 1836 | struct worktree **worktrees, **p; |
| 1837 | |
| Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 02:13:26 | [diff] [blame] | 1838 | repo_read_index(revs->repo); |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1839 | do_add_index_objects_to_pending(revs, revs->repo->index, flags); |
| Nguyễn Thái Ngọc Duy | be489d0 | 2017-08-23 12:36:52 | [diff] [blame] | 1840 | |
| 1841 | if (revs->single_worktree) |
| 1842 | return; |
| 1843 | |
| Eric Sunshine | 03f2465 | 2020-06-19 23:35:44 | [diff] [blame] | 1844 | worktrees = get_worktrees(); |
| Nguyễn Thái Ngọc Duy | be489d0 | 2017-08-23 12:36:52 | [diff] [blame] | 1845 | for (p = worktrees; *p; p++) { |
| 1846 | struct worktree *wt = *p; |
| Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 13:57:00 | [diff] [blame] | 1847 | struct index_state istate = INDEX_STATE_INIT(revs->repo); |
| Nguyễn Thái Ngọc Duy | be489d0 | 2017-08-23 12:36:52 | [diff] [blame] | 1848 | |
| 1849 | if (wt->is_current) |
| 1850 | continue; /* current index already taken care of */ |
| 1851 | |
| 1852 | if (read_index_from(&istate, |
| Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 | [diff] [blame] | 1853 | worktree_git_path(wt, "index"), |
| 1854 | get_worktree_git_dir(wt)) > 0) |
| Jeff King | b4cfcde | 2018-11-02 05:22:59 | [diff] [blame] | 1855 | do_add_index_objects_to_pending(revs, &istate, flags); |
| Nguyễn Thái Ngọc Duy | be489d0 | 2017-08-23 12:36:52 | [diff] [blame] | 1856 | discard_index(&istate); |
| 1857 | } |
| 1858 | free_worktrees(worktrees); |
| Nguyễn Thái Ngọc Duy | 6c3d818 | 2017-08-23 12:36:51 | [diff] [blame] | 1859 | } |
| 1860 | |
| Jeff King | 39b44ba | 2019-07-01 13:18:15 | [diff] [blame] | 1861 | struct add_alternate_refs_data { |
| 1862 | struct rev_info *revs; |
| 1863 | unsigned int flags; |
| 1864 | }; |
| 1865 | |
| 1866 | static void add_one_alternate_ref(const struct object_id *oid, |
| 1867 | void *vdata) |
| 1868 | { |
| 1869 | const char *name = ".alternate"; |
| 1870 | struct add_alternate_refs_data *data = vdata; |
| 1871 | struct object *obj; |
| 1872 | |
| 1873 | obj = get_reference(data->revs, name, oid, data->flags); |
| 1874 | add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags); |
| 1875 | add_pending_object(data->revs, obj, name); |
| 1876 | } |
| 1877 | |
| 1878 | static void add_alternate_refs_to_pending(struct rev_info *revs, |
| 1879 | unsigned int flags) |
| 1880 | { |
| 1881 | struct add_alternate_refs_data data; |
| 1882 | data.revs = revs; |
| 1883 | data.flags = flags; |
| 1884 | for_each_alternate_ref(add_one_alternate_ref, &data); |
| 1885 | } |
| 1886 | |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 1887 | static int add_parents_only(struct rev_info *revs, const char *arg_, int flags, |
| 1888 | int exclude_parent) |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1889 | { |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 1890 | struct object_id oid; |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1891 | struct object *it; |
| 1892 | struct commit *commit; |
| 1893 | struct commit_list *parents; |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 1894 | int parent_number; |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 1895 | const char *arg = arg_; |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1896 | |
| 1897 | if (*arg == '^') { |
| Kevin Bracey | 7f34a46 | 2013-05-16 15:32:38 | [diff] [blame] | 1898 | flags ^= UNINTERESTING | BOTTOM; |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1899 | arg++; |
| 1900 | } |
| Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 13:58:46 | [diff] [blame] | 1901 | if (repo_get_oid_committish(the_repository, arg, &oid)) |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1902 | return 0; |
| 1903 | while (1) { |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 1904 | it = get_reference(revs, arg, &oid, 0); |
| Junio C Hamano | cc243c3 | 2011-05-19 01:08:09 | [diff] [blame] | 1905 | if (!it && revs->ignore_missing) |
| 1906 | return 0; |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 1907 | if (it->type != OBJ_TAG) |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1908 | break; |
| Martin Koegler | 9684afd | 2008-02-18 20:48:01 | [diff] [blame] | 1909 | if (!((struct tag*)it)->tagged) |
| 1910 | return 0; |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 1911 | oidcpy(&oid, &((struct tag*)it)->tagged->oid); |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1912 | } |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 1913 | if (it->type != OBJ_COMMIT) |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1914 | return 0; |
| 1915 | commit = (struct commit *)it; |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 1916 | if (exclude_parent && |
| 1917 | exclude_parent > commit_list_count(commit->parents)) |
| 1918 | return 0; |
| 1919 | for (parents = commit->parents, parent_number = 1; |
| 1920 | parents; |
| 1921 | parents = parents->next, parent_number++) { |
| 1922 | if (exclude_parent && parent_number != exclude_parent) |
| 1923 | continue; |
| 1924 | |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1925 | it = &parents->item->object; |
| 1926 | it->flags |= flags; |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 1927 | add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags); |
| Junio C Hamano | ea4a19e | 2006-04-30 07:54:29 | [diff] [blame] | 1928 | add_pending_object(revs, it, arg); |
| 1929 | } |
| 1930 | return 1; |
| 1931 | } |
| 1932 | |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 1933 | void repo_init_revisions(struct repository *r, |
| 1934 | struct rev_info *revs, |
| 1935 | const char *prefix) |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 1936 | { |
| Ævar Arnfjörð Bjarmason | 916ebb3 | 2022-11-08 14:02:57 | [diff] [blame] | 1937 | struct rev_info blank = REV_INFO_INIT; |
| 1938 | memcpy(revs, &blank, sizeof(*revs)); |
| Junio C Hamano | 8e8f998 | 2006-04-15 05:19:38 | [diff] [blame] | 1939 | |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 1940 | revs->repo = r; |
| Nguyễn Thái Ngọc Duy | 67022e0 | 2018-11-18 16:47:57 | [diff] [blame] | 1941 | revs->pruning.repo = r; |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 1942 | revs->pruning.add_remove = file_add_remove; |
| 1943 | revs->pruning.change = file_change; |
| Jeff King | a937b37 | 2017-10-13 15:27:45 | [diff] [blame] | 1944 | revs->pruning.change_fn_data = revs; |
| Linus Torvalds | db6296a | 2006-07-29 04:21:48 | [diff] [blame] | 1945 | revs->prefix = prefix; |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 1946 | |
| Ævar Arnfjörð Bjarmason | 9725c8d | 2022-02-16 00:00:34 | [diff] [blame] | 1947 | grep_init(&revs->grep_filter, revs->repo); |
| Jeff King | 0843acf | 2008-08-25 06:15:05 | [diff] [blame] | 1948 | revs->grep_filter.status_only = 1; |
| Jeff King | 0843acf | 2008-08-25 06:15:05 | [diff] [blame] | 1949 | |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 1950 | repo_diff_setup(revs->repo, &revs->diffopt); |
| Junio C Hamano | c0cb4a0 | 2008-02-13 08:34:39 | [diff] [blame] | 1951 | if (prefix && !revs->diffopt.prefix) { |
| Junio C Hamano | cd676a5 | 2008-02-12 22:26:02 | [diff] [blame] | 1952 | revs->diffopt.prefix = prefix; |
| 1953 | revs->diffopt.prefix_length = strlen(prefix); |
| 1954 | } |
| Jeff King | 3a03cf6 | 2011-03-29 20:57:27 | [diff] [blame] | 1955 | |
| Denton Liu | e6e230e | 2019-12-09 13:10:41 | [diff] [blame] | 1956 | init_display_notes(&revs->notes_opt); |
| Jeff King | 2a01bde | 2022-09-11 05:03:07 | [diff] [blame] | 1957 | list_objects_filter_init(&revs->filter); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 1958 | init_ref_exclusions(&revs->ref_excludes); |
| Christian Couder | 7b644c8 | 2024-02-14 14:25:13 | [diff] [blame] | 1959 | oidset_init(&revs->missing_commits, 0); |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 1960 | } |
| 1961 | |
| Rene Scharfe | 0d2c9d6 | 2006-07-01 23:29:37 | [diff] [blame] | 1962 | static void add_pending_commit_list(struct rev_info *revs, |
| Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 15:42:06 | [diff] [blame] | 1963 | struct commit_list *commit_list, |
| 1964 | unsigned int flags) |
| Rene Scharfe | 0d2c9d6 | 2006-07-01 23:29:37 | [diff] [blame] | 1965 | { |
| 1966 | while (commit_list) { |
| 1967 | struct object *object = &commit_list->item->object; |
| 1968 | object->flags |= flags; |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 1969 | add_pending_object(revs, object, oid_to_hex(&object->oid)); |
| Rene Scharfe | 0d2c9d6 | 2006-07-01 23:29:37 | [diff] [blame] | 1970 | commit_list = commit_list->next; |
| 1971 | } |
| 1972 | } |
| 1973 | |
| Michael Lohmann | f3fc5d9 | 2024-02-28 13:54:54 | [diff] [blame] | 1974 | static const char *lookup_other_head(struct object_id *oid) |
| 1975 | { |
| 1976 | int i; |
| 1977 | static const char *const other_head[] = { |
| 1978 | "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD" |
| 1979 | }; |
| 1980 | |
| 1981 | for (i = 0; i < ARRAY_SIZE(other_head); i++) |
| 1982 | if (!read_ref_full(other_head[i], |
| 1983 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, |
| 1984 | oid, NULL)) { |
| 1985 | if (is_null_oid(oid)) |
| 1986 | die(_("%s exists but is a symbolic ref"), other_head[i]); |
| 1987 | return other_head[i]; |
| 1988 | } |
| 1989 | |
| 1990 | die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD")); |
| 1991 | } |
| 1992 | |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 1993 | static void prepare_show_merge(struct rev_info *revs) |
| 1994 | { |
| Johannes Schindelin | 76e2a09 | 2024-02-28 09:44:14 | [diff] [blame] | 1995 | struct commit_list *bases = NULL; |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 1996 | struct commit *head, *other; |
| brian m. carlson | 68ab61d | 2017-05-06 22:10:05 | [diff] [blame] | 1997 | struct object_id oid; |
| Michael Lohmann | f3fc5d9 | 2024-02-28 13:54:54 | [diff] [blame] | 1998 | const char *other_name; |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 1999 | const char **prune = NULL; |
| 2000 | int i, prune_num = 1; /* counting terminating NULL */ |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 2001 | struct index_state *istate = revs->repo->index; |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2002 | |
| Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 13:58:46 | [diff] [blame] | 2003 | if (repo_get_oid(the_repository, "HEAD", &oid)) |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2004 | die("--merge without HEAD?"); |
| brian m. carlson | bc83266 | 2017-05-06 22:10:10 | [diff] [blame] | 2005 | head = lookup_commit_or_die(&oid, "HEAD"); |
| Michael Lohmann | f3fc5d9 | 2024-02-28 13:54:54 | [diff] [blame] | 2006 | other_name = lookup_other_head(&oid); |
| 2007 | other = lookup_commit_or_die(&oid, other_name); |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2008 | add_pending_object(revs, &head->object, "HEAD"); |
| Michael Lohmann | f3fc5d9 | 2024-02-28 13:54:54 | [diff] [blame] | 2009 | add_pending_object(revs, &other->object, other_name); |
| Johannes Schindelin | 76e2a09 | 2024-02-28 09:44:14 | [diff] [blame] | 2010 | if (repo_get_merge_bases(the_repository, head, other, &bases) < 0) |
| 2011 | exit(128); |
| Kevin Bracey | 7f34a46 | 2013-05-16 15:32:38 | [diff] [blame] | 2012 | add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM); |
| 2013 | add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM); |
| Junio C Hamano | e82447b | 2008-02-27 07:18:38 | [diff] [blame] | 2014 | free_commit_list(bases); |
| 2015 | head->object.flags |= SYMMETRIC_LEFT; |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2016 | |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 2017 | if (!istate->cache_nr) |
| Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 02:13:26 | [diff] [blame] | 2018 | repo_read_index(revs->repo); |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 2019 | for (i = 0; i < istate->cache_nr; i++) { |
| 2020 | const struct cache_entry *ce = istate->cache[i]; |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2021 | if (!ce_stage(ce)) |
| 2022 | continue; |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 2023 | if (ce_path_match(istate, ce, &revs->prune_data, NULL)) { |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2024 | prune_num++; |
| René Scharfe | 2756ca4 | 2014-09-16 18:56:57 | [diff] [blame] | 2025 | REALLOC_ARRAY(prune, prune_num); |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2026 | prune[prune_num-2] = ce->name; |
| 2027 | prune[prune_num-1] = NULL; |
| 2028 | } |
| Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 15:57:38 | [diff] [blame] | 2029 | while ((i+1 < istate->cache_nr) && |
| 2030 | ce_same_name(ce, istate->cache[i+1])) |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2031 | i++; |
| 2032 | } |
| Junio C Hamano | ed6e803 | 2016-06-02 21:09:22 | [diff] [blame] | 2033 | clear_pathspec(&revs->prune_data); |
| Nguyễn Thái Ngọc Duy | 4a2d5ae | 2013-10-26 02:09:20 | [diff] [blame] | 2034 | parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, |
| Junio C Hamano | c6f1b92 | 2013-11-18 22:31:29 | [diff] [blame] | 2035 | PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune); |
| Junio C Hamano | e82447b | 2008-02-27 07:18:38 | [diff] [blame] | 2036 | revs->limited = 1; |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 2037 | } |
| 2038 | |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2039 | static int dotdot_missing(const char *arg, char *dotdot, |
| 2040 | struct rev_info *revs, int symmetric) |
| 2041 | { |
| 2042 | if (revs->ignore_missing) |
| 2043 | return 0; |
| 2044 | /* de-munge so we report the full argument */ |
| 2045 | *dotdot = '.'; |
| 2046 | die(symmetric |
| 2047 | ? "Invalid symmetric difference expression %s" |
| 2048 | : "Invalid revision range %s", arg); |
| 2049 | } |
| 2050 | |
| 2051 | static int handle_dotdot_1(const char *arg, char *dotdot, |
| 2052 | struct rev_info *revs, int flags, |
| Jeff King | 18f1ad7 | 2017-05-19 12:55:26 | [diff] [blame] | 2053 | int cant_be_filename, |
| 2054 | struct object_context *a_oc, |
| 2055 | struct object_context *b_oc) |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2056 | { |
| 2057 | const char *a_name, *b_name; |
| 2058 | struct object_id a_oid, b_oid; |
| 2059 | struct object *a_obj, *b_obj; |
| 2060 | unsigned int a_flags, b_flags; |
| 2061 | int symmetric = 0; |
| 2062 | unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM); |
| brian m. carlson | 321c89b | 2017-07-13 23:49:29 | [diff] [blame] | 2063 | unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH; |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2064 | |
| 2065 | a_name = arg; |
| 2066 | if (!*a_name) |
| 2067 | a_name = "HEAD"; |
| 2068 | |
| 2069 | b_name = dotdot + 2; |
| 2070 | if (*b_name == '.') { |
| 2071 | symmetric = 1; |
| 2072 | b_name++; |
| 2073 | } |
| 2074 | if (!*b_name) |
| 2075 | b_name = "HEAD"; |
| 2076 | |
| Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 02:13:28 | [diff] [blame] | 2077 | if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) || |
| 2078 | get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc)) |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2079 | return -1; |
| 2080 | |
| 2081 | if (!cant_be_filename) { |
| 2082 | *dotdot = '.'; |
| 2083 | verify_non_filename(revs->prefix, arg); |
| 2084 | *dotdot = '\0'; |
| 2085 | } |
| 2086 | |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 2087 | a_obj = parse_object(revs->repo, &a_oid); |
| 2088 | b_obj = parse_object(revs->repo, &b_oid); |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2089 | if (!a_obj || !b_obj) |
| 2090 | return dotdot_missing(arg, dotdot, revs, symmetric); |
| 2091 | |
| 2092 | if (!symmetric) { |
| 2093 | /* just A..B */ |
| 2094 | b_flags = flags; |
| 2095 | a_flags = flags_exclude; |
| 2096 | } else { |
| 2097 | /* A...B -- find merge bases between the two */ |
| 2098 | struct commit *a, *b; |
| Johannes Schindelin | 76e2a09 | 2024-02-28 09:44:14 | [diff] [blame] | 2099 | struct commit_list *exclude = NULL; |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2100 | |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 2101 | a = lookup_commit_reference(revs->repo, &a_obj->oid); |
| 2102 | b = lookup_commit_reference(revs->repo, &b_obj->oid); |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2103 | if (!a || !b) |
| 2104 | return dotdot_missing(arg, dotdot, revs, symmetric); |
| 2105 | |
| Johannes Schindelin | 76e2a09 | 2024-02-28 09:44:14 | [diff] [blame] | 2106 | if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) { |
| 2107 | free_commit_list(exclude); |
| 2108 | return -1; |
| 2109 | } |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2110 | add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE, |
| 2111 | flags_exclude); |
| 2112 | add_pending_commit_list(revs, exclude, flags_exclude); |
| 2113 | free_commit_list(exclude); |
| 2114 | |
| 2115 | b_flags = flags; |
| 2116 | a_flags = flags | SYMMETRIC_LEFT; |
| 2117 | } |
| 2118 | |
| 2119 | a_obj->flags |= a_flags; |
| 2120 | b_obj->flags |= b_flags; |
| 2121 | add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags); |
| 2122 | add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags); |
| Jeff King | 18f1ad7 | 2017-05-19 12:55:26 | [diff] [blame] | 2123 | add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path); |
| 2124 | add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path); |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2125 | return 0; |
| 2126 | } |
| 2127 | |
| 2128 | static int handle_dotdot(const char *arg, |
| 2129 | struct rev_info *revs, int flags, |
| 2130 | int cant_be_filename) |
| 2131 | { |
| Jeff King | 18f1ad7 | 2017-05-19 12:55:26 | [diff] [blame] | 2132 | struct object_context a_oc, b_oc; |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2133 | char *dotdot = strstr(arg, ".."); |
| 2134 | int ret; |
| 2135 | |
| 2136 | if (!dotdot) |
| 2137 | return -1; |
| 2138 | |
| Jeff King | 18f1ad7 | 2017-05-19 12:55:26 | [diff] [blame] | 2139 | memset(&a_oc, 0, sizeof(a_oc)); |
| 2140 | memset(&b_oc, 0, sizeof(b_oc)); |
| 2141 | |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2142 | *dotdot = '\0'; |
| Jeff King | 18f1ad7 | 2017-05-19 12:55:26 | [diff] [blame] | 2143 | ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename, |
| 2144 | &a_oc, &b_oc); |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2145 | *dotdot = '.'; |
| 2146 | |
| Jeff King | 18f1ad7 | 2017-05-19 12:55:26 | [diff] [blame] | 2147 | free(a_oc.path); |
| 2148 | free(b_oc.path); |
| 2149 | |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2150 | return ret; |
| 2151 | } |
| 2152 | |
| Jeff King | 04a0e98 | 2020-08-26 20:13:05 | [diff] [blame] | 2153 | static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt) |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2154 | { |
| Junio C Hamano | 249c8f4 | 2012-07-02 19:56:44 | [diff] [blame] | 2155 | struct object_context oc; |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2156 | char *mark; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2157 | struct object *object; |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 2158 | struct object_id oid; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2159 | int local_flags; |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 2160 | const char *arg = arg_; |
| Junio C Hamano | 8e676e8 | 2012-07-02 19:33:52 | [diff] [blame] | 2161 | int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME; |
| brian m. carlson | 321c89b | 2017-07-13 23:49:29 | [diff] [blame] | 2162 | unsigned get_sha1_flags = GET_OID_RECORD_PATH; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2163 | |
| Kevin Bracey | 7f34a46 | 2013-05-16 15:32:38 | [diff] [blame] | 2164 | flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM; |
| 2165 | |
| Jeff King | d89797f | 2017-05-19 12:51:40 | [diff] [blame] | 2166 | if (!cant_be_filename && !strcmp(arg, "..")) { |
| 2167 | /* |
| 2168 | * Just ".."? That is not a range but the |
| 2169 | * pathspec for the parent directory. |
| 2170 | */ |
| 2171 | return -1; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2172 | } |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 2173 | |
| Jeff King | 62faad5 | 2017-05-19 12:52:00 | [diff] [blame] | 2174 | if (!handle_dotdot(arg, revs, flags, revarg_opt)) |
| 2175 | return 0; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2176 | |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2177 | mark = strstr(arg, "^@"); |
| 2178 | if (mark && !mark[2]) { |
| 2179 | *mark = 0; |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 2180 | if (add_parents_only(revs, arg, flags, 0)) |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2181 | return 0; |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2182 | *mark = '^'; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2183 | } |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2184 | mark = strstr(arg, "^!"); |
| 2185 | if (mark && !mark[2]) { |
| 2186 | *mark = 0; |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 2187 | if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0)) |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2188 | *mark = '^'; |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 2189 | } |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2190 | mark = strstr(arg, "^-"); |
| 2191 | if (mark) { |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 2192 | int exclude_parent = 1; |
| 2193 | |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2194 | if (mark[2]) { |
| René Scharfe | 793c211 | 2022-10-01 10:25:36 | [diff] [blame] | 2195 | if (strtol_i(mark + 2, 10, &exclude_parent) || |
| 2196 | exclude_parent < 1) |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 2197 | return -1; |
| 2198 | } |
| 2199 | |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2200 | *mark = 0; |
| Vegard Nossum | 8779351 | 2016-09-27 08:32:49 | [diff] [blame] | 2201 | if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent)) |
| Jeff King | f632ded | 2017-05-19 12:50:07 | [diff] [blame] | 2202 | *mark = '^'; |
| Junio C Hamano | 62476c8 | 2006-10-31 22:22:34 | [diff] [blame] | 2203 | } |
| 2204 | |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2205 | local_flags = 0; |
| 2206 | if (*arg == '^') { |
| Kevin Bracey | 7f34a46 | 2013-05-16 15:32:38 | [diff] [blame] | 2207 | local_flags = UNINTERESTING | BOTTOM; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2208 | arg++; |
| 2209 | } |
| Junio C Hamano | d5f6b1d | 2012-07-02 19:43:05 | [diff] [blame] | 2210 | |
| 2211 | if (revarg_opt & REVARG_COMMITTISH) |
| brian m. carlson | 321c89b | 2017-07-13 23:49:29 | [diff] [blame] | 2212 | get_sha1_flags |= GET_OID_COMMITTISH; |
| Junio C Hamano | d5f6b1d | 2012-07-02 19:43:05 | [diff] [blame] | 2213 | |
| Christian Couder | 7b644c8 | 2024-02-14 14:25:13 | [diff] [blame] | 2214 | /* |
| 2215 | * Even if revs->do_not_die_on_missing_objects is set, we |
| 2216 | * should error out if we can't even get an oid, as |
| 2217 | * `--missing=print` should be able to report missing oids. |
| 2218 | */ |
| Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 02:13:28 | [diff] [blame] | 2219 | if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) |
| Junio C Hamano | cc243c3 | 2011-05-19 01:08:09 | [diff] [blame] | 2220 | return revs->ignore_missing ? 0 : -1; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2221 | if (!cant_be_filename) |
| 2222 | verify_non_filename(revs->prefix, arg); |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 2223 | object = get_reference(revs, arg, &oid, flags ^ local_flags); |
| Matthew DeVore | 4cf6786 | 2018-12-05 21:43:46 | [diff] [blame] | 2224 | if (!object) |
| Christian Couder | 7b644c8 | 2024-02-14 14:25:13 | [diff] [blame] | 2225 | return (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1; |
| Junio C Hamano | 281eee4 | 2011-08-26 00:35:39 | [diff] [blame] | 2226 | add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags); |
| Jeff King | 18f1ad7 | 2017-05-19 12:55:26 | [diff] [blame] | 2227 | add_pending_object_with_path(revs, object, arg, oc.mode, oc.path); |
| 2228 | free(oc.path); |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 2229 | return 0; |
| 2230 | } |
| 2231 | |
| Jeff King | 04a0e98 | 2020-08-26 20:13:05 | [diff] [blame] | 2232 | int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt) |
| 2233 | { |
| 2234 | int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt); |
| 2235 | if (!ret) |
| 2236 | revs->rev_input_given = 1; |
| 2237 | return ret; |
| 2238 | } |
| 2239 | |
| Jeff King | 9163399 | 2019-03-20 08:13:36 | [diff] [blame] | 2240 | static void read_pathspec_from_stdin(struct strbuf *sb, |
| Jeff King | c972bf4 | 2020-07-28 20:25:12 | [diff] [blame] | 2241 | struct strvec *prune) |
| Junio C Hamano | 4da5af3 | 2011-05-11 21:01:19 | [diff] [blame] | 2242 | { |
| Jeff King | 7fa3c2a | 2017-09-20 20:36:59 | [diff] [blame] | 2243 | while (strbuf_getline(sb, stdin) != EOF) |
| Jeff King | c972bf4 | 2020-07-28 20:25:12 | [diff] [blame] | 2244 | strvec_push(prune, sb->buf); |
| Junio C Hamano | 60da8b1 | 2009-11-20 10:50:21 | [diff] [blame] | 2245 | } |
| 2246 | |
| Junio C Hamano | 2d10c55 | 2006-09-20 20:21:56 | [diff] [blame] | 2247 | static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) |
| 2248 | { |
| Jeff King | 0843acf | 2008-08-25 06:15:05 | [diff] [blame] | 2249 | append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what); |
| Junio C Hamano | 2d10c55 | 2006-09-20 20:21:56 | [diff] [blame] | 2250 | } |
| 2251 | |
| Junio C Hamano | a4d7d2c | 2008-09-05 05:15:02 | [diff] [blame] | 2252 | static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern) |
| Junio C Hamano | bd95fcd | 2006-09-18 00:23:20 | [diff] [blame] | 2253 | { |
| Junio C Hamano | a4d7d2c | 2008-09-05 05:15:02 | [diff] [blame] | 2254 | append_header_grep_pattern(&revs->grep_filter, field, pattern); |
| Junio C Hamano | bd95fcd | 2006-09-18 00:23:20 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | static void add_message_grep(struct rev_info *revs, const char *pattern) |
| 2258 | { |
| Junio C Hamano | 2d10c55 | 2006-09-20 20:21:56 | [diff] [blame] | 2259 | add_grep(revs, pattern, GREP_PATTERN_BODY); |
| Junio C Hamano | bd95fcd | 2006-09-18 00:23:20 | [diff] [blame] | 2260 | } |
| 2261 | |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2262 | static int parse_count(const char *arg) |
| 2263 | { |
| 2264 | int count; |
| 2265 | |
| 2266 | if (strtol_i(arg, 10, &count) < 0) |
| 2267 | die("'%s': not an integer", arg); |
| 2268 | return count; |
| 2269 | } |
| 2270 | |
| 2271 | static timestamp_t parse_age(const char *arg) |
| 2272 | { |
| 2273 | timestamp_t num; |
| 2274 | char *p; |
| 2275 | |
| 2276 | errno = 0; |
| 2277 | num = parse_timestamp(arg, &p, 10); |
| 2278 | if (errno || *p || p == arg) |
| 2279 | die("'%s': not a number of seconds since epoch", arg); |
| 2280 | return num; |
| 2281 | } |
| 2282 | |
| Pierre Habouzit | 6b61ec0 | 2008-07-09 21:38:34 | [diff] [blame] | 2283 | static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv, |
| Matthew DeVore | bbcde41 | 2018-12-03 22:10:19 | [diff] [blame] | 2284 | int *unkc, const char **unkv, |
| 2285 | const struct setup_revision_opt* opt) |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2286 | { |
| 2287 | const char *arg = argv[0]; |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 2288 | const char *optarg = NULL; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2289 | int argcount; |
| brian m. carlson | fd52124 | 2018-05-02 00:25:50 | [diff] [blame] | 2290 | const unsigned hexsz = the_hash_algo->hexsz; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2291 | |
| 2292 | /* pseudo revision arguments */ |
| 2293 | if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") || |
| 2294 | !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") || |
| 2295 | !strcmp(arg, "--reflog") || !strcmp(arg, "--not") || |
| Linus Torvalds | ad3f9a7 | 2009-10-27 18:28:07 | [diff] [blame] | 2296 | !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") || |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 2297 | !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") || |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 2298 | !strcmp(arg, "--indexed-objects") || |
| Jeff King | 39b44ba | 2019-07-01 13:18:15 | [diff] [blame] | 2299 | !strcmp(arg, "--alternate-refs") || |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2300 | starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") || |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 2301 | starts_with(arg, "--branches=") || starts_with(arg, "--tags=") || |
| 2302 | starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk=")) |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2303 | { |
| 2304 | unkv[(*unkc)++] = arg; |
| Pierre Habouzit | 0fe8c13 | 2008-07-31 10:22:23 | [diff] [blame] | 2305 | return 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2306 | } |
| 2307 | |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2308 | if ((argcount = parse_long_opt("max-count", argv, &optarg))) { |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2309 | revs->max_count = parse_count(optarg); |
| Jonathan Nieder | 5853cae | 2010-06-01 08:35:49 | [diff] [blame] | 2310 | revs->no_walk = 0; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2311 | return argcount; |
| 2312 | } else if ((argcount = parse_long_opt("skip", argv, &optarg))) { |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2313 | revs->skip_count = parse_count(optarg); |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2314 | return argcount; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2315 | } else if ((*arg == '-') && isdigit(arg[1])) { |
| Junio C Hamano | e3fa568 | 2014-06-06 22:33:25 | [diff] [blame] | 2316 | /* accept -<digit>, like traditional "head" */ |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2317 | revs->max_count = parse_count(arg + 1); |
| Jonathan Nieder | 5853cae | 2010-06-01 08:35:49 | [diff] [blame] | 2318 | revs->no_walk = 0; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2319 | } else if (!strcmp(arg, "-n")) { |
| 2320 | if (argc <= 1) |
| 2321 | return error("-n requires an argument"); |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2322 | revs->max_count = parse_count(argv[1]); |
| Jonathan Nieder | 5853cae | 2010-06-01 08:35:49 | [diff] [blame] | 2323 | revs->no_walk = 0; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2324 | return 2; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2325 | } else if (skip_prefix(arg, "-n", &optarg)) { |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2326 | revs->max_count = parse_count(optarg); |
| Jonathan Nieder | 5853cae | 2010-06-01 08:35:49 | [diff] [blame] | 2327 | revs->no_walk = 0; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2328 | } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) { |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2329 | revs->max_age = parse_age(optarg); |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2330 | return argcount; |
| 2331 | } else if ((argcount = parse_long_opt("since", argv, &optarg))) { |
| 2332 | revs->max_age = approxidate(optarg); |
| 2333 | return argcount; |
| Miklos Vajna | 9669778 | 2022-04-23 12:59:57 | [diff] [blame] | 2334 | } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) { |
| 2335 | revs->max_age_as_filter = approxidate(optarg); |
| 2336 | return argcount; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2337 | } else if ((argcount = parse_long_opt("after", argv, &optarg))) { |
| 2338 | revs->max_age = approxidate(optarg); |
| 2339 | return argcount; |
| 2340 | } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) { |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2341 | revs->min_age = parse_age(optarg); |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2342 | return argcount; |
| 2343 | } else if ((argcount = parse_long_opt("before", argv, &optarg))) { |
| 2344 | revs->min_age = approxidate(optarg); |
| 2345 | return argcount; |
| 2346 | } else if ((argcount = parse_long_opt("until", argv, &optarg))) { |
| 2347 | revs->min_age = approxidate(optarg); |
| 2348 | return argcount; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2349 | } else if (!strcmp(arg, "--first-parent")) { |
| 2350 | revs->first_parent_only = 1; |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 2351 | } else if (!strcmp(arg, "--exclude-first-parent-only")) { |
| 2352 | revs->exclude_first_parent_only = 1; |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 2353 | } else if (!strcmp(arg, "--ancestry-path")) { |
| 2354 | revs->ancestry_path = 1; |
| Johan Herland | cb7529e | 2010-06-03 23:17:37 | [diff] [blame] | 2355 | revs->simplify_history = 0; |
| Junio C Hamano | ebdc94f | 2010-04-20 20:48:39 | [diff] [blame] | 2356 | revs->limited = 1; |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 2357 | revs->ancestry_path_implicit_bottoms = 1; |
| 2358 | } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) { |
| 2359 | struct commit *c; |
| 2360 | struct object_id oid; |
| Alexander Shopov | 781fb7b | 2024-02-16 10:15:37 | [diff] [blame] | 2361 | const char *msg = _("could not get commit for --ancestry-path argument %s"); |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 2362 | |
| 2363 | revs->ancestry_path = 1; |
| 2364 | revs->simplify_history = 0; |
| 2365 | revs->limited = 1; |
| 2366 | |
| 2367 | if (repo_get_oid_committish(revs->repo, optarg, &oid)) |
| 2368 | return error(msg, optarg); |
| 2369 | get_reference(revs, optarg, &oid, ANCESTRY_PATH); |
| 2370 | c = lookup_commit_reference(revs->repo, &oid); |
| 2371 | if (!c) |
| 2372 | return error(msg, optarg); |
| 2373 | commit_list_insert(c, &revs->ancestry_path_bottoms); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2374 | } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) { |
| 2375 | init_reflog_walk(&revs->reflog_info); |
| 2376 | } else if (!strcmp(arg, "--default")) { |
| 2377 | if (argc <= 1) |
| 2378 | return error("bad --default argument"); |
| 2379 | revs->def = argv[1]; |
| 2380 | return 2; |
| 2381 | } else if (!strcmp(arg, "--merge")) { |
| 2382 | revs->show_merge = 1; |
| 2383 | } else if (!strcmp(arg, "--topo-order")) { |
| Junio C Hamano | 08f704f | 2013-06-06 23:07:14 | [diff] [blame] | 2384 | revs->sort_order = REV_SORT_IN_GRAPH_ORDER; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2385 | revs->topo_order = 1; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 2386 | } else if (!strcmp(arg, "--simplify-merges")) { |
| 2387 | revs->simplify_merges = 1; |
| Junio C Hamano | a52f007 | 2012-06-08 21:47:08 | [diff] [blame] | 2388 | revs->topo_order = 1; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 2389 | revs->rewrite_parents = 1; |
| 2390 | revs->simplify_history = 0; |
| 2391 | revs->limited = 1; |
| Linus Torvalds | 78892e3 | 2008-11-03 19:25:46 | [diff] [blame] | 2392 | } else if (!strcmp(arg, "--simplify-by-decoration")) { |
| 2393 | revs->simplify_merges = 1; |
| Junio C Hamano | a52f007 | 2012-06-08 21:47:08 | [diff] [blame] | 2394 | revs->topo_order = 1; |
| Linus Torvalds | 78892e3 | 2008-11-03 19:25:46 | [diff] [blame] | 2395 | revs->rewrite_parents = 1; |
| 2396 | revs->simplify_history = 0; |
| 2397 | revs->simplify_by_decoration = 1; |
| 2398 | revs->limited = 1; |
| 2399 | revs->prune = 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2400 | } else if (!strcmp(arg, "--date-order")) { |
| Junio C Hamano | 08f704f | 2013-06-06 23:07:14 | [diff] [blame] | 2401 | revs->sort_order = REV_SORT_BY_COMMIT_DATE; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2402 | revs->topo_order = 1; |
| Junio C Hamano | 81c6b38 | 2013-06-07 17:35:54 | [diff] [blame] | 2403 | } else if (!strcmp(arg, "--author-date-order")) { |
| 2404 | revs->sort_order = REV_SORT_BY_AUTHOR_DATE; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2405 | revs->topo_order = 1; |
| SZEDER Gábor | dffc651 | 2017-06-09 18:17:31 | [diff] [blame] | 2406 | } else if (!strcmp(arg, "--early-output")) { |
| 2407 | revs->early_output = 100; |
| 2408 | revs->topo_order = 1; |
| 2409 | } else if (skip_prefix(arg, "--early-output=", &optarg)) { |
| 2410 | if (strtoul_ui(optarg, 10, &revs->early_output) < 0) |
| 2411 | die("'%s': not a non-negative integer", optarg); |
| 2412 | revs->topo_order = 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2413 | } else if (!strcmp(arg, "--parents")) { |
| 2414 | revs->rewrite_parents = 1; |
| 2415 | revs->print_parents = 1; |
| 2416 | } else if (!strcmp(arg, "--dense")) { |
| 2417 | revs->dense = 1; |
| 2418 | } else if (!strcmp(arg, "--sparse")) { |
| 2419 | revs->dense = 0; |
| Stefan Beller | ce5b6f9 | 2017-11-16 02:00:35 | [diff] [blame] | 2420 | } else if (!strcmp(arg, "--in-commit-order")) { |
| 2421 | revs->tree_blobs_in_commit_order = 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2422 | } else if (!strcmp(arg, "--remove-empty")) { |
| 2423 | revs->remove_empty_trees = 1; |
| Linus Torvalds | b8e8db2 | 2009-06-29 17:28:25 | [diff] [blame] | 2424 | } else if (!strcmp(arg, "--merges")) { |
| Michael J Gruber | ad5aeed | 2011-03-21 10:14:06 | [diff] [blame] | 2425 | revs->min_parents = 2; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2426 | } else if (!strcmp(arg, "--no-merges")) { |
| Michael J Gruber | ad5aeed | 2011-03-21 10:14:06 | [diff] [blame] | 2427 | revs->max_parents = 1; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2428 | } else if (skip_prefix(arg, "--min-parents=", &optarg)) { |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2429 | revs->min_parents = parse_count(optarg); |
| SZEDER Gábor | 9ada7ae | 2017-06-09 18:17:30 | [diff] [blame] | 2430 | } else if (!strcmp(arg, "--no-min-parents")) { |
| Michael J Gruber | ad5aeed | 2011-03-21 10:14:06 | [diff] [blame] | 2431 | revs->min_parents = 0; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2432 | } else if (skip_prefix(arg, "--max-parents=", &optarg)) { |
| Junio C Hamano | 71a1e94 | 2023-12-08 22:35:23 | [diff] [blame] | 2433 | revs->max_parents = parse_count(optarg); |
| SZEDER Gábor | 9ada7ae | 2017-06-09 18:17:30 | [diff] [blame] | 2434 | } else if (!strcmp(arg, "--no-max-parents")) { |
| Michael J Gruber | ad5aeed | 2011-03-21 10:14:06 | [diff] [blame] | 2435 | revs->max_parents = -1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2436 | } else if (!strcmp(arg, "--boundary")) { |
| 2437 | revs->boundary = 1; |
| 2438 | } else if (!strcmp(arg, "--left-right")) { |
| 2439 | revs->left_right = 1; |
| Michael J Gruber | 60adf7d | 2011-02-21 16:09:11 | [diff] [blame] | 2440 | } else if (!strcmp(arg, "--left-only")) { |
| Junio C Hamano | 24852d9 | 2011-02-22 00:58:37 | [diff] [blame] | 2441 | if (revs->right_only) |
| René Scharfe | 7854bf4 | 2023-11-26 11:57:43 | [diff] [blame] | 2442 | die(_("options '%s' and '%s' cannot be used together"), |
| 2443 | "--left-only", "--right-only/--cherry"); |
| Michael J Gruber | 60adf7d | 2011-02-21 16:09:11 | [diff] [blame] | 2444 | revs->left_only = 1; |
| 2445 | } else if (!strcmp(arg, "--right-only")) { |
| Junio C Hamano | 24852d9 | 2011-02-22 00:58:37 | [diff] [blame] | 2446 | if (revs->left_only) |
| Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 | [diff] [blame] | 2447 | die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only"); |
| Michael J Gruber | 60adf7d | 2011-02-21 16:09:11 | [diff] [blame] | 2448 | revs->right_only = 1; |
| Michael J Gruber | 94f605e | 2011-03-07 12:31:42 | [diff] [blame] | 2449 | } else if (!strcmp(arg, "--cherry")) { |
| 2450 | if (revs->left_only) |
| Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 | [diff] [blame] | 2451 | die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only"); |
| Michael J Gruber | 94f605e | 2011-03-07 12:31:42 | [diff] [blame] | 2452 | revs->cherry_mark = 1; |
| 2453 | revs->right_only = 1; |
| Michael J Gruber | ad5aeed | 2011-03-21 10:14:06 | [diff] [blame] | 2454 | revs->max_parents = 1; |
| Michael J Gruber | 94f605e | 2011-03-07 12:31:42 | [diff] [blame] | 2455 | revs->limited = 1; |
| Thomas Rast | f69c501 | 2010-06-10 11:47:23 | [diff] [blame] | 2456 | } else if (!strcmp(arg, "--count")) { |
| 2457 | revs->count = 1; |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 2458 | } else if (!strcmp(arg, "--cherry-mark")) { |
| 2459 | if (revs->cherry_pick) |
| Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 | [diff] [blame] | 2460 | die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick"); |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 2461 | revs->cherry_mark = 1; |
| 2462 | revs->limited = 1; /* needs limit_list() */ |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2463 | } else if (!strcmp(arg, "--cherry-pick")) { |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 2464 | if (revs->cherry_mark) |
| Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 | [diff] [blame] | 2465 | die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark"); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2466 | revs->cherry_pick = 1; |
| 2467 | revs->limited = 1; |
| 2468 | } else if (!strcmp(arg, "--objects")) { |
| 2469 | revs->tag_objects = 1; |
| 2470 | revs->tree_objects = 1; |
| 2471 | revs->blob_objects = 1; |
| 2472 | } else if (!strcmp(arg, "--objects-edge")) { |
| 2473 | revs->tag_objects = 1; |
| 2474 | revs->tree_objects = 1; |
| 2475 | revs->blob_objects = 1; |
| 2476 | revs->edge_hint = 1; |
| brian m. carlson | 1684c1b | 2014-12-24 23:05:39 | [diff] [blame] | 2477 | } else if (!strcmp(arg, "--objects-edge-aggressive")) { |
| 2478 | revs->tag_objects = 1; |
| 2479 | revs->tree_objects = 1; |
| 2480 | revs->blob_objects = 1; |
| 2481 | revs->edge_hint = 1; |
| 2482 | revs->edge_hint_aggressive = 1; |
| Junio C Hamano | 5a48d24 | 2011-09-01 22:43:34 | [diff] [blame] | 2483 | } else if (!strcmp(arg, "--verify-objects")) { |
| 2484 | revs->tag_objects = 1; |
| 2485 | revs->tree_objects = 1; |
| 2486 | revs->blob_objects = 1; |
| 2487 | revs->verify_objects = 1; |
| Jeff King | b27ccae | 2022-09-06 21:04:35 | [diff] [blame] | 2488 | disable_commit_graph(revs->repo); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2489 | } else if (!strcmp(arg, "--unpacked")) { |
| 2490 | revs->unpacked = 1; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 2491 | } else if (starts_with(arg, "--unpacked=")) { |
| Sergey Organov | f649aaa | 2020-08-04 22:26:30 | [diff] [blame] | 2492 | die(_("--unpacked=<packfile> no longer supported")); |
| Taylor Blau | c9fff00 | 2021-02-23 02:25:07 | [diff] [blame] | 2493 | } else if (!strcmp(arg, "--no-kept-objects")) { |
| 2494 | revs->no_kept_objects = 1; |
| 2495 | revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS; |
| 2496 | revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS; |
| 2497 | } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) { |
| 2498 | revs->no_kept_objects = 1; |
| 2499 | if (!strcmp(optarg, "in-core")) |
| 2500 | revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS; |
| 2501 | if (!strcmp(optarg, "on-disk")) |
| 2502 | revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2503 | } else if (!strcmp(arg, "-r")) { |
| 2504 | revs->diff = 1; |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 2505 | revs->diffopt.flags.recursive = 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2506 | } else if (!strcmp(arg, "-t")) { |
| 2507 | revs->diff = 1; |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 2508 | revs->diffopt.flags.recursive = 1; |
| 2509 | revs->diffopt.flags.tree_in_recursive = 1; |
| Sergey Organov | 18f0947 | 2020-12-21 15:19:34 | [diff] [blame] | 2510 | } else if ((argcount = diff_merges_parse_opts(revs, argv))) { |
| Sergey Organov | 6501580 | 2020-08-05 22:08:30 | [diff] [blame] | 2511 | return argcount; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2512 | } else if (!strcmp(arg, "-v")) { |
| 2513 | revs->verbose_header = 1; |
| 2514 | } else if (!strcmp(arg, "--pretty")) { |
| 2515 | revs->verbose_header = 1; |
| Junio C Hamano | 66b2ed0 | 2010-01-20 21:59:36 | [diff] [blame] | 2516 | revs->pretty_given = 1; |
| Jeff King | ae18165 | 2014-07-29 17:53:40 | [diff] [blame] | 2517 | get_commit_format(NULL, revs); |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2518 | } else if (skip_prefix(arg, "--pretty=", &optarg) || |
| 2519 | skip_prefix(arg, "--format=", &optarg)) { |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2520 | /* |
| 2521 | * Detached form ("--pretty X" as opposed to "--pretty=X") |
| 2522 | * not allowed, since the argument is optional. |
| 2523 | */ |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2524 | revs->verbose_header = 1; |
| Junio C Hamano | 66b2ed0 | 2010-01-20 21:59:36 | [diff] [blame] | 2525 | revs->pretty_given = 1; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2526 | get_commit_format(optarg, revs); |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 2527 | } else if (!strcmp(arg, "--expand-tabs")) { |
| Junio C Hamano | fe37a9c | 2016-03-29 23:05:39 | [diff] [blame] | 2528 | revs->expand_tabs_in_log = 8; |
| Junio C Hamano | 0893eec | 2016-03-29 22:49:24 | [diff] [blame] | 2529 | } else if (!strcmp(arg, "--no-expand-tabs")) { |
| 2530 | revs->expand_tabs_in_log = 0; |
| Junio C Hamano | fe37a9c | 2016-03-29 23:05:39 | [diff] [blame] | 2531 | } else if (skip_prefix(arg, "--expand-tabs=", &arg)) { |
| 2532 | int val; |
| 2533 | if (strtol_i(arg, 10, &val) < 0 || val < 0) |
| 2534 | die("'%s': not a non-negative integer", arg); |
| 2535 | revs->expand_tabs_in_log = val; |
| Jeff King | 7249e91 | 2011-03-29 20:57:47 | [diff] [blame] | 2536 | } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) { |
| Denton Liu | 1d72975 | 2019-12-12 00:49:50 | [diff] [blame] | 2537 | enable_default_display_notes(&revs->notes_opt, &revs->show_notes); |
| Junio C Hamano | 66b2ed0 | 2010-01-20 21:59:36 | [diff] [blame] | 2538 | revs->show_notes_given = 1; |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 2539 | } else if (!strcmp(arg, "--show-signature")) { |
| 2540 | revs->show_signature = 1; |
| Mehul Jain | aa37999 | 2016-06-22 16:51:25 | [diff] [blame] | 2541 | } else if (!strcmp(arg, "--no-show-signature")) { |
| 2542 | revs->show_signature = 0; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2543 | } else if (!strcmp(arg, "--show-linear-break")) { |
| 2544 | revs->break_bar = " .........."; |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 2545 | revs->track_linear = 1; |
| 2546 | revs->track_first_time = 1; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2547 | } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) { |
| 2548 | revs->break_bar = xstrdup(optarg); |
| 2549 | revs->track_linear = 1; |
| 2550 | revs->track_first_time = 1; |
| Kristoffer Haugsbakk | 2e0d30d | 2023-09-19 20:26:50 | [diff] [blame] | 2551 | } else if (!strcmp(arg, "--show-notes-by-default")) { |
| 2552 | revs->show_notes_by_default = 1; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2553 | } else if (skip_prefix(arg, "--show-notes=", &optarg) || |
| 2554 | skip_prefix(arg, "--notes=", &optarg)) { |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2555 | if (starts_with(arg, "--show-notes=") && |
| 2556 | revs->notes_opt.use_default_notes < 0) |
| 2557 | revs->notes_opt.use_default_notes = 1; |
| Denton Liu | 1d72975 | 2019-12-12 00:49:50 | [diff] [blame] | 2558 | enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg); |
| Junio C Hamano | 66b2ed0 | 2010-01-20 21:59:36 | [diff] [blame] | 2559 | revs->show_notes_given = 1; |
| Denton Liu | 452538c | 2019-12-09 13:10:44 | [diff] [blame] | 2560 | } else if (!strcmp(arg, "--no-notes")) { |
| Denton Liu | 1d72975 | 2019-12-12 00:49:50 | [diff] [blame] | 2561 | disable_display_notes(&revs->notes_opt, &revs->show_notes); |
| Denton Liu | 452538c | 2019-12-09 13:10:44 | [diff] [blame] | 2562 | revs->show_notes_given = 1; |
| Thomas Rast | 894a9d3 | 2010-03-12 17:04:26 | [diff] [blame] | 2563 | } else if (!strcmp(arg, "--standard-notes")) { |
| 2564 | revs->show_notes_given = 1; |
| Jeff King | 3a03cf6 | 2011-03-29 20:57:27 | [diff] [blame] | 2565 | revs->notes_opt.use_default_notes = 1; |
| Thomas Rast | 894a9d3 | 2010-03-12 17:04:26 | [diff] [blame] | 2566 | } else if (!strcmp(arg, "--no-standard-notes")) { |
| Jeff King | 3a03cf6 | 2011-03-29 20:57:27 | [diff] [blame] | 2567 | revs->notes_opt.use_default_notes = 0; |
| Nanako Shiraishi | de84acc | 2009-02-24 09:59:16 | [diff] [blame] | 2568 | } else if (!strcmp(arg, "--oneline")) { |
| 2569 | revs->verbose_header = 1; |
| 2570 | get_commit_format("oneline", revs); |
| Junio C Hamano | 7dccadf | 2010-01-21 22:57:41 | [diff] [blame] | 2571 | revs->pretty_given = 1; |
| Nanako Shiraishi | de84acc | 2009-02-24 09:59:16 | [diff] [blame] | 2572 | revs->abbrev_commit = 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2573 | } else if (!strcmp(arg, "--graph")) { |
| Alex Henrie | dccf6c1 | 2022-02-11 16:36:24 | [diff] [blame] | 2574 | graph_clear(revs->graph); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2575 | revs->graph = graph_init(revs); |
| Alex Henrie | 087c745 | 2022-02-11 16:36:25 | [diff] [blame] | 2576 | } else if (!strcmp(arg, "--no-graph")) { |
| 2577 | graph_clear(revs->graph); |
| 2578 | revs->graph = NULL; |
| Emma Brooks | 19d097e | 2020-04-08 04:31:38 | [diff] [blame] | 2579 | } else if (!strcmp(arg, "--encode-email-headers")) { |
| 2580 | revs->encode_email_headers = 1; |
| 2581 | } else if (!strcmp(arg, "--no-encode-email-headers")) { |
| 2582 | revs->encode_email_headers = 0; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2583 | } else if (!strcmp(arg, "--root")) { |
| 2584 | revs->show_root_diff = 1; |
| 2585 | } else if (!strcmp(arg, "--no-commit-id")) { |
| 2586 | revs->no_commit_id = 1; |
| 2587 | } else if (!strcmp(arg, "--always")) { |
| 2588 | revs->always_show_header = 1; |
| 2589 | } else if (!strcmp(arg, "--no-abbrev")) { |
| 2590 | revs->abbrev = 0; |
| 2591 | } else if (!strcmp(arg, "--abbrev")) { |
| 2592 | revs->abbrev = DEFAULT_ABBREV; |
| SZEDER Gábor | 479b3d9 | 2017-06-09 18:17:32 | [diff] [blame] | 2593 | } else if (skip_prefix(arg, "--abbrev=", &optarg)) { |
| 2594 | revs->abbrev = strtoul(optarg, NULL, 10); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2595 | if (revs->abbrev < MINIMUM_ABBREV) |
| 2596 | revs->abbrev = MINIMUM_ABBREV; |
| brian m. carlson | fd52124 | 2018-05-02 00:25:50 | [diff] [blame] | 2597 | else if (revs->abbrev > hexsz) |
| 2598 | revs->abbrev = hexsz; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2599 | } else if (!strcmp(arg, "--abbrev-commit")) { |
| 2600 | revs->abbrev_commit = 1; |
| Jay Soffian | 0c47695 | 2011-05-18 17:56:04 | [diff] [blame] | 2601 | revs->abbrev_commit_given = 1; |
| 2602 | } else if (!strcmp(arg, "--no-abbrev-commit")) { |
| 2603 | revs->abbrev_commit = 0; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2604 | } else if (!strcmp(arg, "--full-diff")) { |
| 2605 | revs->diff = 1; |
| 2606 | revs->full_diff = 1; |
| Derrick Stolee | 8d049e1 | 2020-04-10 12:19:43 | [diff] [blame] | 2607 | } else if (!strcmp(arg, "--show-pulls")) { |
| 2608 | revs->show_pulls = 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2609 | } else if (!strcmp(arg, "--full-history")) { |
| 2610 | revs->simplify_history = 0; |
| 2611 | } else if (!strcmp(arg, "--relative-date")) { |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 2612 | revs->date_mode.type = DATE_RELATIVE; |
| Jeff King | f4ea32f | 2009-09-24 08:28:15 | [diff] [blame] | 2613 | revs->date_mode_explicit = 1; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2614 | } else if ((argcount = parse_long_opt("date", argv, &optarg))) { |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 2615 | parse_date_format(optarg, &revs->date_mode); |
| Jeff King | f4ea32f | 2009-09-24 08:28:15 | [diff] [blame] | 2616 | revs->date_mode_explicit = 1; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2617 | return argcount; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2618 | } else if (!strcmp(arg, "--log-size")) { |
| 2619 | revs->show_log_size = 1; |
| 2620 | } |
| 2621 | /* |
| 2622 | * Grepping the commit log |
| 2623 | */ |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2624 | else if ((argcount = parse_long_opt("author", argv, &optarg))) { |
| 2625 | add_header_grep(revs, GREP_HEADER_AUTHOR, optarg); |
| 2626 | return argcount; |
| 2627 | } else if ((argcount = parse_long_opt("committer", argv, &optarg))) { |
| 2628 | add_header_grep(revs, GREP_HEADER_COMMITTER, optarg); |
| 2629 | return argcount; |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 2630 | } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) { |
| 2631 | add_header_grep(revs, GREP_HEADER_REFLOG, optarg); |
| 2632 | return argcount; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2633 | } else if ((argcount = parse_long_opt("grep", argv, &optarg))) { |
| 2634 | add_message_grep(revs, optarg); |
| 2635 | return argcount; |
| Junio C Hamano | 727b6fc | 2012-10-03 22:01:34 | [diff] [blame] | 2636 | } else if (!strcmp(arg, "--basic-regexp")) { |
| Junio C Hamano | 8465541 | 2016-07-22 18:43:14 | [diff] [blame] | 2637 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2638 | } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) { |
| Junio C Hamano | 8465541 | 2016-07-22 18:43:14 | [diff] [blame] | 2639 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2640 | } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) { |
| Ævar Arnfjörð Bjarmason | 9e3cbc5 | 2017-05-20 21:42:08 | [diff] [blame] | 2641 | revs->grep_filter.ignore_case = 1; |
| Stefan Beller | c1ddc46 | 2018-01-04 22:50:40 | [diff] [blame] | 2642 | revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2643 | } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) { |
| Junio C Hamano | 8465541 | 2016-07-22 18:43:14 | [diff] [blame] | 2644 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED; |
| Ævar Arnfjörð Bjarmason | 7531a2d | 2017-05-25 20:05:24 | [diff] [blame] | 2645 | } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) { |
| Junio C Hamano | 8465541 | 2016-07-22 18:43:14 | [diff] [blame] | 2646 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2647 | } else if (!strcmp(arg, "--all-match")) { |
| Jeff King | 0843acf | 2008-08-25 06:15:05 | [diff] [blame] | 2648 | revs->grep_filter.all_match = 1; |
| Christoph Junghans | 22dfa8a | 2015-01-13 01:33:32 | [diff] [blame] | 2649 | } else if (!strcmp(arg, "--invert-grep")) { |
| René Scharfe | 794c000 | 2021-12-17 16:48:49 | [diff] [blame] | 2650 | revs->grep_filter.no_body_match = 1; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2651 | } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) { |
| 2652 | if (strcmp(optarg, "none")) |
| 2653 | git_log_output_encoding = xstrdup(optarg); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2654 | else |
| 2655 | git_log_output_encoding = ""; |
| Matthieu Moy | 7d7b86f | 2010-08-05 08:22:55 | [diff] [blame] | 2656 | return argcount; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2657 | } else if (!strcmp(arg, "--reverse")) { |
| 2658 | revs->reverse ^= 1; |
| 2659 | } else if (!strcmp(arg, "--children")) { |
| 2660 | revs->children.name = "children"; |
| 2661 | revs->limited = 1; |
| Junio C Hamano | cc243c3 | 2011-05-19 01:08:09 | [diff] [blame] | 2662 | } else if (!strcmp(arg, "--ignore-missing")) { |
| 2663 | revs->ignore_missing = 1; |
| Matthew DeVore | bbcde41 | 2018-12-03 22:10:19 | [diff] [blame] | 2664 | } else if (opt && opt->allow_exclude_promisor_objects && |
| Matthew DeVore | 669b1d2 | 2018-10-23 01:13:42 | [diff] [blame] | 2665 | !strcmp(arg, "--exclude-promisor-objects")) { |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 2666 | if (fetch_if_missing) |
| Johannes Schindelin | 033abf9 | 2018-05-02 09:38:39 | [diff] [blame] | 2667 | BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0"); |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 2668 | revs->exclude_promisor_objects = 1; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2669 | } else { |
| Duy Nguyen | a97262c | 2016-01-21 11:48:44 | [diff] [blame] | 2670 | int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2671 | if (!opts) |
| 2672 | unkv[(*unkc)++] = arg; |
| 2673 | return opts; |
| 2674 | } |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2675 | |
| 2676 | return 1; |
| 2677 | } |
| 2678 | |
| Pierre Habouzit | 6b61ec0 | 2008-07-09 21:38:34 | [diff] [blame] | 2679 | void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx, |
| 2680 | const struct option *options, |
| 2681 | const char * const usagestr[]) |
| 2682 | { |
| 2683 | int n = handle_revision_opt(revs, ctx->argc, ctx->argv, |
| Matthew DeVore | bbcde41 | 2018-12-03 22:10:19 | [diff] [blame] | 2684 | &ctx->cpidx, ctx->out, NULL); |
| Pierre Habouzit | 6b61ec0 | 2008-07-09 21:38:34 | [diff] [blame] | 2685 | if (n <= 0) { |
| 2686 | error("unknown option `%s'", ctx->argv[0]); |
| 2687 | usage_with_options(usagestr, options); |
| 2688 | } |
| 2689 | ctx->argv += n; |
| 2690 | ctx->argc -= n; |
| 2691 | } |
| 2692 | |
| Alex Henrie | 087c745 | 2022-02-11 16:36:25 | [diff] [blame] | 2693 | void revision_opts_finish(struct rev_info *revs) |
| 2694 | { |
| 2695 | if (revs->graph && revs->track_linear) |
| 2696 | die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph"); |
| 2697 | |
| 2698 | if (revs->graph) { |
| 2699 | revs->topo_order = 1; |
| 2700 | revs->rewrite_parents = 1; |
| 2701 | } |
| 2702 | } |
| 2703 | |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2704 | static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn, |
| 2705 | void *cb_data, const char *term) |
| 2706 | { |
| Antoine Delaite | cb46d63 | 2015-06-29 15:40:30 | [diff] [blame] | 2707 | struct strbuf bisect_refs = STRBUF_INIT; |
| 2708 | int status; |
| 2709 | strbuf_addf(&bisect_refs, "refs/bisect/%s", term); |
| Taylor Blau | b269ac5 | 2023-07-10 21:12:22 | [diff] [blame] | 2710 | status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data); |
| Antoine Delaite | cb46d63 | 2015-06-29 15:40:30 | [diff] [blame] | 2711 | strbuf_release(&bisect_refs); |
| 2712 | return status; |
| 2713 | } |
| 2714 | |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2715 | static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
| Linus Torvalds | ad3f9a7 | 2009-10-27 18:28:07 | [diff] [blame] | 2716 | { |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2717 | return for_each_bisect_ref(refs, fn, cb_data, term_bad); |
| Linus Torvalds | ad3f9a7 | 2009-10-27 18:28:07 | [diff] [blame] | 2718 | } |
| 2719 | |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2720 | static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
| Linus Torvalds | ad3f9a7 | 2009-10-27 18:28:07 | [diff] [blame] | 2721 | { |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2722 | return for_each_bisect_ref(refs, fn, cb_data, term_good); |
| Linus Torvalds | ad3f9a7 | 2009-10-27 18:28:07 | [diff] [blame] | 2723 | } |
| 2724 | |
| Jonathan Tan | 10a0d6a | 2021-09-09 18:47:29 | [diff] [blame] | 2725 | static int handle_revision_pseudo_opt(struct rev_info *revs, |
| Jeff King | e885a84 | 2020-09-30 12:28:18 | [diff] [blame] | 2726 | const char **argv, int *flags) |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2727 | { |
| 2728 | const char *arg = argv[0]; |
| 2729 | const char *optarg; |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2730 | struct ref_store *refs; |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2731 | int argcount; |
| 2732 | |
| Jonathan Tan | 10a0d6a | 2021-09-09 18:47:29 | [diff] [blame] | 2733 | if (revs->repo != the_repository) { |
| Nguyễn Thái Ngọc Duy | d0c39a4 | 2017-08-23 12:36:59 | [diff] [blame] | 2734 | /* |
| 2735 | * We need some something like get_submodule_worktrees() |
| 2736 | * before we can go through all worktrees of a submodule, |
| 2737 | * .e.g with adding all HEADs from --all, which is not |
| 2738 | * supported right now, so stick to single worktree. |
| 2739 | */ |
| 2740 | if (!revs->single_worktree) |
| Johannes Schindelin | 033abf9 | 2018-05-02 09:38:39 | [diff] [blame] | 2741 | BUG("--single-worktree cannot be used together with submodule"); |
| Jonathan Tan | 10a0d6a | 2021-09-09 18:47:29 | [diff] [blame] | 2742 | } |
| 2743 | refs = get_main_ref_store(revs->repo); |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2744 | |
| Jonathan Nieder | 0fc63ec | 2011-04-21 10:48:24 | [diff] [blame] | 2745 | /* |
| 2746 | * NOTE! |
| 2747 | * |
| 2748 | * Commands like "git shortlog" will not accept the options below |
| 2749 | * unless parse_revision_opt queues them (as opposed to erroring |
| 2750 | * out). |
| 2751 | * |
| 2752 | * When implementing your new pseudo-option, remember to |
| 2753 | * register it in the list at the top of handle_revision_opt. |
| 2754 | */ |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2755 | if (!strcmp(arg, "--all")) { |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2756 | handle_refs(refs, revs, *flags, refs_for_each_ref); |
| 2757 | handle_refs(refs, revs, *flags, refs_head_ref); |
| Nguyễn Thái Ngọc Duy | d0c39a4 | 2017-08-23 12:36:59 | [diff] [blame] | 2758 | if (!revs->single_worktree) { |
| 2759 | struct all_refs_cb cb; |
| 2760 | |
| 2761 | init_all_refs_cb(&cb, revs, *flags); |
| 2762 | other_head_refs(handle_one_ref, &cb); |
| 2763 | } |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2764 | clear_ref_exclusions(&revs->ref_excludes); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2765 | } else if (!strcmp(arg, "--branches")) { |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2766 | if (revs->ref_excludes.hidden_refs_configured) |
| René Scharfe | 81fb70f | 2023-12-06 11:51:58 | [diff] [blame] | 2767 | return error(_("options '%s' and '%s' cannot be used together"), |
| 2768 | "--exclude-hidden", "--branches"); |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2769 | handle_refs(refs, revs, *flags, refs_for_each_branch_ref); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2770 | clear_ref_exclusions(&revs->ref_excludes); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2771 | } else if (!strcmp(arg, "--bisect")) { |
| Antoine Delaite | cb46d63 | 2015-06-29 15:40:30 | [diff] [blame] | 2772 | read_bisect_terms(&term_bad, &term_good); |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2773 | handle_refs(refs, revs, *flags, for_each_bad_bisect_ref); |
| 2774 | handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM), |
| 2775 | for_each_good_bisect_ref); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2776 | revs->bisect = 1; |
| 2777 | } else if (!strcmp(arg, "--tags")) { |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2778 | if (revs->ref_excludes.hidden_refs_configured) |
| René Scharfe | 81fb70f | 2023-12-06 11:51:58 | [diff] [blame] | 2779 | return error(_("options '%s' and '%s' cannot be used together"), |
| 2780 | "--exclude-hidden", "--tags"); |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2781 | handle_refs(refs, revs, *flags, refs_for_each_tag_ref); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2782 | clear_ref_exclusions(&revs->ref_excludes); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2783 | } else if (!strcmp(arg, "--remotes")) { |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2784 | if (revs->ref_excludes.hidden_refs_configured) |
| René Scharfe | 81fb70f | 2023-12-06 11:51:58 | [diff] [blame] | 2785 | return error(_("options '%s' and '%s' cannot be used together"), |
| 2786 | "--exclude-hidden", "--remotes"); |
| Nguyễn Thái Ngọc Duy | 073cf63 | 2017-08-23 12:36:56 | [diff] [blame] | 2787 | handle_refs(refs, revs, *flags, refs_for_each_remote_ref); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2788 | clear_ref_exclusions(&revs->ref_excludes); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2789 | } else if ((argcount = parse_long_opt("glob", argv, &optarg))) { |
| 2790 | struct all_refs_cb cb; |
| 2791 | init_all_refs_cb(&cb, revs, *flags); |
| Michael Haggerty | a217dcb | 2015-05-25 18:38:30 | [diff] [blame] | 2792 | for_each_glob_ref(handle_one_ref, optarg, &cb); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2793 | clear_ref_exclusions(&revs->ref_excludes); |
| Junio C Hamano | e7b432c | 2013-08-30 23:37:55 | [diff] [blame] | 2794 | return argcount; |
| 2795 | } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) { |
| Junio C Hamano | ff32d34 | 2013-11-01 19:02:45 | [diff] [blame] | 2796 | add_ref_exclusion(&revs->ref_excludes, optarg); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2797 | return argcount; |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2798 | } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) { |
| 2799 | exclude_hidden_refs(&revs->ref_excludes, optarg); |
| 2800 | return argcount; |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2801 | } else if (skip_prefix(arg, "--branches=", &optarg)) { |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2802 | struct all_refs_cb cb; |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2803 | if (revs->ref_excludes.hidden_refs_configured) |
| René Scharfe | 81fb70f | 2023-12-06 11:51:58 | [diff] [blame] | 2804 | return error(_("options '%s' and '%s' cannot be used together"), |
| 2805 | "--exclude-hidden", "--branches"); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2806 | init_all_refs_cb(&cb, revs, *flags); |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2807 | for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2808 | clear_ref_exclusions(&revs->ref_excludes); |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2809 | } else if (skip_prefix(arg, "--tags=", &optarg)) { |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2810 | struct all_refs_cb cb; |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2811 | if (revs->ref_excludes.hidden_refs_configured) |
| René Scharfe | 81fb70f | 2023-12-06 11:51:58 | [diff] [blame] | 2812 | return error(_("options '%s' and '%s' cannot be used together"), |
| 2813 | "--exclude-hidden", "--tags"); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2814 | init_all_refs_cb(&cb, revs, *flags); |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2815 | for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2816 | clear_ref_exclusions(&revs->ref_excludes); |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2817 | } else if (skip_prefix(arg, "--remotes=", &optarg)) { |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2818 | struct all_refs_cb cb; |
| Patrick Steinhardt | 8c1bc2a | 2022-11-17 05:46:56 | [diff] [blame] | 2819 | if (revs->ref_excludes.hidden_refs_configured) |
| René Scharfe | 81fb70f | 2023-12-06 11:51:58 | [diff] [blame] | 2820 | return error(_("options '%s' and '%s' cannot be used together"), |
| 2821 | "--exclude-hidden", "--remotes"); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2822 | init_all_refs_cb(&cb, revs, *flags); |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2823 | for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb); |
| Patrick Steinhardt | 1e9f273 | 2022-11-17 05:46:51 | [diff] [blame] | 2824 | clear_ref_exclusions(&revs->ref_excludes); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2825 | } else if (!strcmp(arg, "--reflog")) { |
| Jeff King | 718ccc9 | 2014-10-15 22:38:31 | [diff] [blame] | 2826 | add_reflogs_to_pending(revs, *flags); |
| Jeff King | 4fe1021 | 2014-10-17 00:44:23 | [diff] [blame] | 2827 | } else if (!strcmp(arg, "--indexed-objects")) { |
| 2828 | add_index_objects_to_pending(revs, *flags); |
| Jeff King | 39b44ba | 2019-07-01 13:18:15 | [diff] [blame] | 2829 | } else if (!strcmp(arg, "--alternate-refs")) { |
| 2830 | add_alternate_refs_to_pending(revs, *flags); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2831 | } else if (!strcmp(arg, "--not")) { |
| Kevin Bracey | 7f34a46 | 2013-05-16 15:32:38 | [diff] [blame] | 2832 | *flags ^= UNINTERESTING | BOTTOM; |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2833 | } else if (!strcmp(arg, "--no-walk")) { |
| Patrick Steinhardt | 29ef1f2 | 2021-08-05 11:25:24 | [diff] [blame] | 2834 | revs->no_walk = 1; |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2835 | } else if (skip_prefix(arg, "--no-walk=", &optarg)) { |
| Martin von Zweigbergk | ca92e59 | 2012-08-29 06:15:54 | [diff] [blame] | 2836 | /* |
| 2837 | * Detached form ("--no-walk X" as opposed to "--no-walk=X") |
| 2838 | * not allowed, since the argument is optional. |
| 2839 | */ |
| Patrick Steinhardt | 29ef1f2 | 2021-08-05 11:25:24 | [diff] [blame] | 2840 | revs->no_walk = 1; |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2841 | if (!strcmp(optarg, "sorted")) |
| Patrick Steinhardt | 29ef1f2 | 2021-08-05 11:25:24 | [diff] [blame] | 2842 | revs->unsorted_input = 0; |
| SZEDER Gábor | 8b1d913 | 2017-06-09 18:17:33 | [diff] [blame] | 2843 | else if (!strcmp(optarg, "unsorted")) |
| Patrick Steinhardt | 29ef1f2 | 2021-08-05 11:25:24 | [diff] [blame] | 2844 | revs->unsorted_input = 1; |
| Martin von Zweigbergk | ca92e59 | 2012-08-29 06:15:54 | [diff] [blame] | 2845 | else |
| 2846 | return error("invalid argument to --no-walk"); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2847 | } else if (!strcmp(arg, "--do-walk")) { |
| 2848 | revs->no_walk = 0; |
| Nguyễn Thái Ngọc Duy | 32619f9 | 2017-08-23 12:37:02 | [diff] [blame] | 2849 | } else if (!strcmp(arg, "--single-worktree")) { |
| 2850 | revs->single_worktree = 1; |
| Derrick Stolee | cc91044 | 2022-03-22 17:28:35 | [diff] [blame] | 2851 | } else if (skip_prefix(arg, ("--filter="), &arg)) { |
| Derrick Stolee | c4ea513 | 2022-03-09 16:01:40 | [diff] [blame] | 2852 | parse_list_objects_filter(&revs->filter, arg); |
| Derrick Stolee | cc91044 | 2022-03-22 17:28:35 | [diff] [blame] | 2853 | } else if (!strcmp(arg, ("--no-filter"))) { |
| Derrick Stolee | c4ea513 | 2022-03-09 16:01:40 | [diff] [blame] | 2854 | list_objects_filter_set_no_filter(&revs->filter); |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2855 | } else { |
| 2856 | return 0; |
| 2857 | } |
| 2858 | |
| 2859 | return 1; |
| 2860 | } |
| 2861 | |
| Patrick Steinhardt | cc80450 | 2023-06-15 14:39:51 | [diff] [blame] | 2862 | static void read_revisions_from_stdin(struct rev_info *revs, |
| Patrick Steinhardt | f97c8b1 | 2023-09-25 13:02:00 | [diff] [blame] | 2863 | struct strvec *prune) |
| Patrick Steinhardt | cc80450 | 2023-06-15 14:39:51 | [diff] [blame] | 2864 | { |
| 2865 | struct strbuf sb; |
| 2866 | int seen_dashdash = 0; |
| Patrick Steinhardt | c40f0b7 | 2023-06-15 14:39:59 | [diff] [blame] | 2867 | int seen_end_of_options = 0; |
| Patrick Steinhardt | cc80450 | 2023-06-15 14:39:51 | [diff] [blame] | 2868 | int save_warning; |
| Patrick Steinhardt | f97c8b1 | 2023-09-25 13:02:00 | [diff] [blame] | 2869 | int flags = 0; |
| Patrick Steinhardt | cc80450 | 2023-06-15 14:39:51 | [diff] [blame] | 2870 | |
| 2871 | save_warning = warn_on_object_refname_ambiguity; |
| 2872 | warn_on_object_refname_ambiguity = 0; |
| 2873 | |
| 2874 | strbuf_init(&sb, 1000); |
| 2875 | while (strbuf_getline(&sb, stdin) != EOF) { |
| Patrick Steinhardt | af37a20 | 2023-06-15 14:39:55 | [diff] [blame] | 2876 | if (!sb.len) |
| Patrick Steinhardt | cc80450 | 2023-06-15 14:39:51 | [diff] [blame] | 2877 | break; |
| Patrick Steinhardt | af37a20 | 2023-06-15 14:39:55 | [diff] [blame] | 2878 | |
| 2879 | if (!strcmp(sb.buf, "--")) { |
| 2880 | seen_dashdash = 1; |
| 2881 | break; |
| Patrick Steinhardt | cc80450 | 2023-06-15 14:39:51 | [diff] [blame] | 2882 | } |
| Patrick Steinhardt | af37a20 | 2023-06-15 14:39:55 | [diff] [blame] | 2883 | |
| Patrick Steinhardt | c40f0b7 | 2023-06-15 14:39:59 | [diff] [blame] | 2884 | if (!seen_end_of_options && sb.buf[0] == '-') { |
| 2885 | const char *argv[] = { sb.buf, NULL }; |
| 2886 | |
| 2887 | if (!strcmp(sb.buf, "--end-of-options")) { |
| 2888 | seen_end_of_options = 1; |
| 2889 | continue; |
| 2890 | } |
| 2891 | |
| Patrick Steinhardt | f97c8b1 | 2023-09-25 13:02:00 | [diff] [blame] | 2892 | if (handle_revision_pseudo_opt(revs, argv, &flags) > 0) |
| Patrick Steinhardt | c40f0b7 | 2023-06-15 14:39:59 | [diff] [blame] | 2893 | continue; |
| 2894 | |
| 2895 | die(_("invalid option '%s' in --stdin mode"), sb.buf); |
| 2896 | } |
| Patrick Steinhardt | af37a20 | 2023-06-15 14:39:55 | [diff] [blame] | 2897 | |
| Patrick Steinhardt | f97c8b1 | 2023-09-25 13:02:00 | [diff] [blame] | 2898 | if (handle_revision_arg(sb.buf, revs, flags, |
| Patrick Steinhardt | cc80450 | 2023-06-15 14:39:51 | [diff] [blame] | 2899 | REVARG_CANNOT_BE_FILENAME)) |
| 2900 | die("bad revision '%s'", sb.buf); |
| 2901 | } |
| 2902 | if (seen_dashdash) |
| 2903 | read_pathspec_from_stdin(&sb, prune); |
| 2904 | |
| 2905 | strbuf_release(&sb); |
| 2906 | warn_on_object_refname_ambiguity = save_warning; |
| 2907 | } |
| 2908 | |
| Jeff King | ce11360 | 2015-08-29 05:04:18 | [diff] [blame] | 2909 | static void NORETURN diagnose_missing_default(const char *def) |
| 2910 | { |
| Jeff King | ce11360 | 2015-08-29 05:04:18 | [diff] [blame] | 2911 | int flags; |
| 2912 | const char *refname; |
| 2913 | |
| René Scharfe | 744c040 | 2017-09-23 09:45:04 | [diff] [blame] | 2914 | refname = resolve_ref_unsafe(def, 0, NULL, &flags); |
| Jeff King | ce11360 | 2015-08-29 05:04:18 | [diff] [blame] | 2915 | if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN)) |
| 2916 | die(_("your current branch appears to be broken")); |
| 2917 | |
| 2918 | skip_prefix(refname, "refs/heads/", &refname); |
| 2919 | die(_("your current branch '%s' does not have any commits yet"), |
| 2920 | refname); |
| 2921 | } |
| 2922 | |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2923 | /* |
| 2924 | * Parse revision information, filling in the "rev_info" structure, |
| 2925 | * and removing the used arguments from the argument list. |
| 2926 | * |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 2927 | * Returns the number of arguments left that weren't recognized |
| 2928 | * (which are also moved to the head of the argument list) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2929 | */ |
| Junio C Hamano | 32962c9 | 2010-03-09 06:58:09 | [diff] [blame] | 2930 | int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt) |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2931 | { |
| Jeff King | 04a0e98 | 2020-08-26 20:13:05 | [diff] [blame] | 2932 | int i, flags, left, seen_dashdash, revarg_opt; |
| Jeff King | c972bf4 | 2020-07-28 20:25:12 | [diff] [blame] | 2933 | struct strvec prune_data = STRVEC_INIT; |
| Jeff King | 19e8789 | 2019-08-06 14:39:58 | [diff] [blame] | 2934 | int seen_end_of_options = 0; |
| Heiko Voigt | 9ef6aeb | 2010-07-07 13:39:12 | [diff] [blame] | 2935 | |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2936 | /* First, search for "--" */ |
| Clemens Buchacher | 6d5b93f | 2012-04-14 19:04:48 | [diff] [blame] | 2937 | if (opt && opt->assume_dashdash) { |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2938 | seen_dashdash = 1; |
| Clemens Buchacher | 6d5b93f | 2012-04-14 19:04:48 | [diff] [blame] | 2939 | } else { |
| 2940 | seen_dashdash = 0; |
| 2941 | for (i = 1; i < argc; i++) { |
| 2942 | const char *arg = argv[i]; |
| 2943 | if (strcmp(arg, "--")) |
| 2944 | continue; |
| Ævar Arnfjörð Bjarmason | f92dbdb | 2022-08-02 15:33:16 | [diff] [blame] | 2945 | if (opt && opt->free_removed_argv_elements) |
| 2946 | free((char *)argv[i]); |
| Clemens Buchacher | 6d5b93f | 2012-04-14 19:04:48 | [diff] [blame] | 2947 | argv[i] = NULL; |
| 2948 | argc = i; |
| 2949 | if (argv[i + 1]) |
| Jeff King | c972bf4 | 2020-07-28 20:25:12 | [diff] [blame] | 2950 | strvec_pushv(&prune_data, argv + i + 1); |
| Clemens Buchacher | 6d5b93f | 2012-04-14 19:04:48 | [diff] [blame] | 2951 | seen_dashdash = 1; |
| 2952 | break; |
| 2953 | } |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2954 | } |
| 2955 | |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2956 | /* Second, deal with arguments and options */ |
| 2957 | flags = 0; |
| Junio C Hamano | d5f6b1d | 2012-07-02 19:43:05 | [diff] [blame] | 2958 | revarg_opt = opt ? opt->revarg_opt : 0; |
| 2959 | if (seen_dashdash) |
| 2960 | revarg_opt |= REVARG_CANNOT_BE_FILENAME; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2961 | for (left = i = 1; i < argc; i++) { |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2962 | const char *arg = argv[i]; |
| Jeff King | 19e8789 | 2019-08-06 14:39:58 | [diff] [blame] | 2963 | if (!seen_end_of_options && *arg == '-') { |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 2964 | int opts; |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2965 | |
| Jonathan Tan | 10a0d6a | 2021-09-09 18:47:29 | [diff] [blame] | 2966 | opts = handle_revision_pseudo_opt( |
| Jeff King | e885a84 | 2020-09-30 12:28:18 | [diff] [blame] | 2967 | revs, argv + i, |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2968 | &flags); |
| 2969 | if (opts > 0) { |
| 2970 | i += opts - 1; |
| Uwe Kleine-König | a5aa930 | 2008-02-28 07:24:25 | [diff] [blame] | 2971 | continue; |
| 2972 | } |
| Jonathan Nieder | f6aca0d | 2011-04-21 10:45:07 | [diff] [blame] | 2973 | |
| Junio C Hamano | 8b3dce5 | 2009-11-03 14:59:18 | [diff] [blame] | 2974 | if (!strcmp(arg, "--stdin")) { |
| 2975 | if (revs->disable_stdin) { |
| 2976 | argv[left++] = arg; |
| 2977 | continue; |
| 2978 | } |
| Jeff King | a12cbe2 | 2018-08-22 21:37:23 | [diff] [blame] | 2979 | if (revs->read_from_stdin++) |
| Junio C Hamano | 8b3dce5 | 2009-11-03 14:59:18 | [diff] [blame] | 2980 | die("--stdin given twice?"); |
| Patrick Steinhardt | f97c8b1 | 2023-09-25 13:02:00 | [diff] [blame] | 2981 | read_revisions_from_stdin(revs, &prune_data); |
| Junio C Hamano | 8b3dce5 | 2009-11-03 14:59:18 | [diff] [blame] | 2982 | continue; |
| 2983 | } |
| Junio C Hamano | 2d10c55 | 2006-09-20 20:21:56 | [diff] [blame] | 2984 | |
| Jeff King | 19e8789 | 2019-08-06 14:39:58 | [diff] [blame] | 2985 | if (!strcmp(arg, "--end-of-options")) { |
| 2986 | seen_end_of_options = 1; |
| 2987 | continue; |
| 2988 | } |
| 2989 | |
| Matthew DeVore | bbcde41 | 2018-12-03 22:10:19 | [diff] [blame] | 2990 | opts = handle_revision_opt(revs, argc - i, argv + i, |
| 2991 | &left, argv, opt); |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 2992 | if (opts > 0) { |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 2993 | i += opts - 1; |
| 2994 | continue; |
| 2995 | } |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 2996 | if (opts < 0) |
| 2997 | exit(128); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 2998 | continue; |
| 2999 | } |
| Rene Scharfe | 0d2c9d6 | 2006-07-01 23:29:37 | [diff] [blame] | 3000 | |
| Junio C Hamano | 8e676e8 | 2012-07-02 19:33:52 | [diff] [blame] | 3001 | |
| 3002 | if (handle_revision_arg(arg, revs, flags, revarg_opt)) { |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 3003 | int j; |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 3004 | if (seen_dashdash || *arg == '^') |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 3005 | die("bad revision '%s'", arg); |
| 3006 | |
| Junio C Hamano | ea92f41 | 2006-04-26 22:09:27 | [diff] [blame] | 3007 | /* If we didn't have a "--": |
| 3008 | * (1) all filenames must exist; |
| 3009 | * (2) all rev-args must not be interpretable |
| 3010 | * as a valid filename. |
| 3011 | * but the latter we have checked in the main loop. |
| 3012 | */ |
| Linus Torvalds | e23d0b4 | 2006-04-26 17:15:54 | [diff] [blame] | 3013 | for (j = i; j < argc; j++) |
| Matthieu Moy | 023e37c | 2012-06-18 18:18:21 | [diff] [blame] | 3014 | verify_filename(revs->prefix, argv[j], j == i); |
| Linus Torvalds | e23d0b4 | 2006-04-26 17:15:54 | [diff] [blame] | 3015 | |
| Jeff King | c972bf4 | 2020-07-28 20:25:12 | [diff] [blame] | 3016 | strvec_pushv(&prune_data, argv + i); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 3017 | break; |
| 3018 | } |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 3019 | } |
| Alex Henrie | 087c745 | 2022-02-11 16:36:25 | [diff] [blame] | 3020 | revision_opts_finish(revs); |
| Junio C Hamano | 5d6f093 | 2006-09-06 04:28:36 | [diff] [blame] | 3021 | |
| Jeff King | d70a9eb | 2020-07-29 00:37:20 | [diff] [blame] | 3022 | if (prune_data.nr) { |
| Junio C Hamano | 93e7d67 | 2011-05-11 22:23:25 | [diff] [blame] | 3023 | /* |
| 3024 | * If we need to introduce the magic "a lone ':' means no |
| 3025 | * pathspec whatsoever", here is the place to do so. |
| 3026 | * |
| 3027 | * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) { |
| 3028 | * prune_data.nr = 0; |
| 3029 | * prune_data.alloc = 0; |
| 3030 | * free(prune_data.path); |
| 3031 | * prune_data.path = NULL; |
| 3032 | * } else { |
| 3033 | * terminate prune_data.alloc with NULL and |
| 3034 | * call init_pathspec() to set revs->prune_data here. |
| 3035 | * } |
| 3036 | */ |
| Nguyễn Thái Ngọc Duy | 0fdc2ae | 2013-07-14 08:35:31 | [diff] [blame] | 3037 | parse_pathspec(&revs->prune_data, 0, 0, |
| Jeff King | d70a9eb | 2020-07-29 00:37:20 | [diff] [blame] | 3038 | revs->prefix, prune_data.v); |
| Junio C Hamano | 4da5af3 | 2011-05-11 21:01:19 | [diff] [blame] | 3039 | } |
| Jeff King | c972bf4 | 2020-07-28 20:25:12 | [diff] [blame] | 3040 | strvec_clear(&prune_data); |
| Junio C Hamano | 5486ef0 | 2009-11-20 10:33:28 | [diff] [blame] | 3041 | |
| Junio C Hamano | afe8a90 | 2022-05-02 16:50:37 | [diff] [blame] | 3042 | if (!revs->def) |
| Junio C Hamano | 32962c9 | 2010-03-09 06:58:09 | [diff] [blame] | 3043 | revs->def = opt ? opt->def : NULL; |
| Junio C Hamano | b449005 | 2010-03-09 07:27:25 | [diff] [blame] | 3044 | if (opt && opt->tweak) |
| Jeff King | cc88afa | 2023-07-03 06:44:16 | [diff] [blame] | 3045 | opt->tweak(revs); |
| Pierre Habouzit | 02e5422 | 2008-07-08 13:19:33 | [diff] [blame] | 3046 | if (revs->show_merge) |
| Junio C Hamano | ae3e5e1 | 2006-07-03 09:59:32 | [diff] [blame] | 3047 | prepare_show_merge(revs); |
| Jeff King | 04a0e98 | 2020-08-26 20:13:05 | [diff] [blame] | 3048 | if (revs->def && !revs->pending.nr && !revs->rev_input_given) { |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 3049 | struct object_id oid; |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3050 | struct object *object; |
| Junio C Hamano | 249c8f4 | 2012-07-02 19:56:44 | [diff] [blame] | 3051 | struct object_context oc; |
| Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 02:13:28 | [diff] [blame] | 3052 | if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc)) |
| Jeff King | ce11360 | 2015-08-29 05:04:18 | [diff] [blame] | 3053 | diagnose_missing_default(revs->def); |
| brian m. carlson | 654b9a9 | 2017-05-06 22:10:27 | [diff] [blame] | 3054 | object = get_reference(revs, revs->def, &oid, 0); |
| Junio C Hamano | 249c8f4 | 2012-07-02 19:56:44 | [diff] [blame] | 3055 | add_pending_object_with_mode(revs, object, revs->def, oc.mode); |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 3056 | } |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 3057 | |
| Linus Torvalds | b7bb760 | 2007-09-29 16:50:39 | [diff] [blame] | 3058 | /* Did the user ask for any diff output? Run the diff! */ |
| 3059 | if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) |
| 3060 | revs->diff = 1; |
| 3061 | |
| Arjen Laarhoven | 0faf2da | 2007-12-25 11:06:47 | [diff] [blame] | 3062 | /* Pickaxe, diff-filter and rename following need diffs */ |
| Stefan Beller | cf63051 | 2018-01-04 22:50:41 | [diff] [blame] | 3063 | if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) || |
| Arjen Laarhoven | 0faf2da | 2007-12-25 11:06:47 | [diff] [blame] | 3064 | revs->diffopt.filter || |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 3065 | revs->diffopt.flags.follow_renames) |
| Linus Torvalds | b7bb760 | 2007-09-29 16:50:39 | [diff] [blame] | 3066 | revs->diff = 1; |
| 3067 | |
| Stefan Beller | 15af58c | 2018-01-04 22:50:42 | [diff] [blame] | 3068 | if (revs->diffopt.objfind) |
| 3069 | revs->simplify_history = 0; |
| 3070 | |
| SZEDER Gábor | 002933f | 2020-05-11 11:56:18 | [diff] [blame] | 3071 | if (revs->line_level_traverse) { |
| 3072 | if (want_ancestry(revs)) |
| 3073 | revs->limited = 1; |
| 3074 | revs->topo_order = 1; |
| 3075 | } |
| 3076 | |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3077 | if (revs->topo_order && !generation_numbers_enabled(the_repository)) |
| Junio C Hamano | 5306968 | 2006-04-02 02:38:25 | [diff] [blame] | 3078 | revs->limited = 1; |
| 3079 | |
| Nguyễn Thái Ngọc Duy | afe069d | 2010-12-17 12:43:06 | [diff] [blame] | 3080 | if (revs->prune_data.nr) { |
| Nguyễn Thái Ngọc Duy | bd1928d | 2013-07-14 08:35:58 | [diff] [blame] | 3081 | copy_pathspec(&revs->pruning.pathspec, &revs->prune_data); |
| Linus Torvalds | 750f7b6 | 2007-06-19 21:22:46 | [diff] [blame] | 3082 | /* Can't prune commits with rename following: the paths change.. */ |
| Brandon Williams | 0d1e0e7 | 2017-10-31 18:19:11 | [diff] [blame] | 3083 | if (!revs->diffopt.flags.follow_renames) |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 3084 | revs->prune = 1; |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3085 | if (!revs->full_diff) |
| Nguyễn Thái Ngọc Duy | bd1928d | 2013-07-14 08:35:58 | [diff] [blame] | 3086 | copy_pathspec(&revs->diffopt.pathspec, |
| 3087 | &revs->prune_data); |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 3088 | } |
| Sergey Organov | 299a663 | 2020-12-21 15:19:30 | [diff] [blame] | 3089 | |
| Sergey Organov | 18f0947 | 2020-12-21 15:19:34 | [diff] [blame] | 3090 | diff_merges_setup_revs(revs); |
| Elijah Newren | d76ce4f | 2019-02-08 01:12:46 | [diff] [blame] | 3091 | |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3092 | revs->diffopt.abbrev = revs->abbrev; |
| Thomas Rast | 12da1d1 | 2013-03-28 16:47:32 | [diff] [blame] | 3093 | |
| Thomas Rast | 2845265 | 2012-08-03 12:16:24 | [diff] [blame] | 3094 | diff_setup_done(&revs->diffopt); |
| Fredrik Kuivinen | 8efdc32 | 2006-03-10 09:21:39 | [diff] [blame] | 3095 | |
| Ævar Arnfjörð Bjarmason | 4457018 | 2019-06-27 23:39:05 | [diff] [blame] | 3096 | if (!is_encoding_utf8(get_log_output_encoding())) |
| 3097 | revs->grep_filter.ignore_locale = 1; |
| Jeff King | 0843acf | 2008-08-25 06:15:05 | [diff] [blame] | 3098 | compile_grep_patterns(&revs->grep_filter); |
| Junio C Hamano | bd95fcd | 2006-09-18 00:23:20 | [diff] [blame] | 3099 | |
| Jeff King | 82fd0f4 | 2017-07-07 09:07:16 | [diff] [blame] | 3100 | if (revs->reflog_info && revs->limited) |
| 3101 | die("cannot combine --walk-reflogs with history-limiting options"); |
| Junio C Hamano | 8bb6588 | 2008-07-08 22:25:44 | [diff] [blame] | 3102 | if (revs->rewrite_parents && revs->children.name) |
| Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 | [diff] [blame] | 3103 | die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children"); |
| Derrick Stolee | c4ea513 | 2022-03-09 16:01:40 | [diff] [blame] | 3104 | if (revs->filter.choice && !revs->blob_objects) |
| 3105 | die(_("object filtering requires --objects")); |
| Shawn O. Pearce | d56651c | 2007-08-20 02:33:43 | [diff] [blame] | 3106 | |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 3107 | /* |
| 3108 | * Limitations on the graph functionality |
| 3109 | */ |
| René Scharfe | fa518ae | 2023-12-06 11:51:57 | [diff] [blame] | 3110 | die_for_incompatible_opt3(!!revs->graph, "--graph", |
| 3111 | !!revs->reverse, "--reverse", |
| 3112 | !!revs->reflog_info, "--walk-reflogs"); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 3113 | |
| Dongcan Jiang | 695985f | 2015-03-11 02:13:02 | [diff] [blame] | 3114 | if (revs->no_walk && revs->graph) |
| Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 | [diff] [blame] | 3115 | die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph"); |
| Junio C Hamano | baa6378 | 2012-09-29 18:59:52 | [diff] [blame] | 3116 | if (!revs->reflog_info && revs->grep_filter.use_reflog_filter) |
| Jean-Noël Avila | 6fa00ee | 2022-01-05 20:02:19 | [diff] [blame] | 3117 | die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs"); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 3118 | |
| Jeff King | 05314ef | 2019-03-11 03:54:33 | [diff] [blame] | 3119 | if (revs->line_level_traverse && |
| 3120 | (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT))) |
| 3121 | die(_("-L does not yet support diff formats besides -p and -s")); |
| 3122 | |
| Junio C Hamano | 0893eec | 2016-03-29 22:49:24 | [diff] [blame] | 3123 | if (revs->expand_tabs_in_log < 0) |
| 3124 | revs->expand_tabs_in_log = revs->expand_tabs_in_log_default; |
| 3125 | |
| Kristoffer Haugsbakk | 2e0d30d | 2023-09-19 20:26:50 | [diff] [blame] | 3126 | if (!revs->show_notes_given && revs->show_notes_by_default) { |
| 3127 | enable_default_display_notes(&revs->notes_opt, &revs->show_notes); |
| 3128 | revs->show_notes_given = 1; |
| 3129 | } |
| 3130 | |
| Linus Torvalds | ae56354 | 2006-02-26 00:19:46 | [diff] [blame] | 3131 | return left; |
| 3132 | } |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 3133 | |
| Ævar Arnfjörð Bjarmason | 7a98d9a | 2022-04-13 20:01:47 | [diff] [blame] | 3134 | static void release_revisions_cmdline(struct rev_cmdline_info *cmdline) |
| 3135 | { |
| 3136 | unsigned int i; |
| 3137 | |
| 3138 | for (i = 0; i < cmdline->nr; i++) |
| 3139 | free((char *)cmdline->rev[i].name); |
| 3140 | free(cmdline->rev); |
| 3141 | } |
| 3142 | |
| Ævar Arnfjörð Bjarmason | a52f07a | 2022-04-13 20:01:46 | [diff] [blame] | 3143 | static void release_revisions_mailmap(struct string_list *mailmap) |
| 3144 | { |
| 3145 | if (!mailmap) |
| 3146 | return; |
| 3147 | clear_mailmap(mailmap); |
| 3148 | free(mailmap); |
| 3149 | } |
| 3150 | |
| Ævar Arnfjörð Bjarmason | ae1b383 | 2022-04-14 05:56:39 | [diff] [blame] | 3151 | static void release_revisions_topo_walk_info(struct topo_walk_info *info); |
| 3152 | |
| Jeff King | 8ef8da4 | 2023-10-05 21:30:14 | [diff] [blame] | 3153 | static void free_void_commit_list(void *list) |
| 3154 | { |
| 3155 | free_commit_list(list); |
| 3156 | } |
| 3157 | |
| Ævar Arnfjörð Bjarmason | 1878b5e | 2022-04-13 20:01:35 | [diff] [blame] | 3158 | void release_revisions(struct rev_info *revs) |
| 3159 | { |
| Ævar Arnfjörð Bjarmason | e966fc5 | 2022-04-13 20:01:45 | [diff] [blame] | 3160 | free_commit_list(revs->commits); |
| Elijah Newren | 257418c | 2022-08-19 04:28:10 | [diff] [blame] | 3161 | free_commit_list(revs->ancestry_path_bottoms); |
| Ævar Arnfjörð Bjarmason | 1878b5e | 2022-04-13 20:01:35 | [diff] [blame] | 3162 | object_array_clear(&revs->pending); |
| Ævar Arnfjörð Bjarmason | ab1f692 | 2022-04-13 20:01:51 | [diff] [blame] | 3163 | object_array_clear(&revs->boundary_commits); |
| Ævar Arnfjörð Bjarmason | 7a98d9a | 2022-04-13 20:01:47 | [diff] [blame] | 3164 | release_revisions_cmdline(&revs->cmdline); |
| Ævar Arnfjörð Bjarmason | e75d2f7 | 2022-04-13 20:01:48 | [diff] [blame] | 3165 | list_objects_filter_release(&revs->filter); |
| Ævar Arnfjörð Bjarmason | 689a8e8 | 2022-04-13 20:01:50 | [diff] [blame] | 3166 | clear_pathspec(&revs->prune_data); |
| Ævar Arnfjörð Bjarmason | 9d5a7df | 2022-04-14 05:56:38 | [diff] [blame] | 3167 | date_mode_release(&revs->date_mode); |
| Ævar Arnfjörð Bjarmason | a52f07a | 2022-04-13 20:01:46 | [diff] [blame] | 3168 | release_revisions_mailmap(revs->mailmap); |
| Ævar Arnfjörð Bjarmason | f41fb66 | 2022-04-13 20:01:49 | [diff] [blame] | 3169 | free_grep_patterns(&revs->grep_filter); |
| Ævar Arnfjörð Bjarmason | fc47252 | 2022-11-08 18:17:44 | [diff] [blame] | 3170 | graph_clear(revs->graph); |
| Ævar Arnfjörð Bjarmason | 54c8a7c | 2022-04-14 05:56:40 | [diff] [blame] | 3171 | /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */ |
| Ævar Arnfjörð Bjarmason | 6ab75ac | 2022-04-13 20:01:53 | [diff] [blame] | 3172 | diff_free(&revs->pruning); |
| Ævar Arnfjörð Bjarmason | 81ffbf8 | 2022-04-13 20:01:52 | [diff] [blame] | 3173 | reflog_walk_info_release(revs->reflog_info); |
| Ævar Arnfjörð Bjarmason | ae1b383 | 2022-04-14 05:56:39 | [diff] [blame] | 3174 | release_revisions_topo_walk_info(revs->topo_walk_info); |
| Jeff King | 8ef8da4 | 2023-10-05 21:30:14 | [diff] [blame] | 3175 | clear_decoration(&revs->children, free_void_commit_list); |
| 3176 | clear_decoration(&revs->merge_simplification, free); |
| 3177 | clear_decoration(&revs->treesame, free); |
| 3178 | line_log_free(revs); |
| Karthik Nayak | 9830926 | 2023-10-27 07:59:29 | [diff] [blame] | 3179 | oidset_clear(&revs->missing_commits); |
| Ævar Arnfjörð Bjarmason | 1878b5e | 2022-04-13 20:01:35 | [diff] [blame] | 3180 | } |
| 3181 | |
| Junio C Hamano | f35f560 | 2008-04-03 09:12:06 | [diff] [blame] | 3182 | static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child) |
| 3183 | { |
| 3184 | struct commit_list *l = xcalloc(1, sizeof(*l)); |
| 3185 | |
| 3186 | l->item = child; |
| 3187 | l->next = add_decoration(&revs->children, &parent->object, l); |
| 3188 | } |
| 3189 | |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3190 | static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit) |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3191 | { |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3192 | struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object); |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3193 | struct commit_list **pp, *p; |
| 3194 | int surviving_parents; |
| 3195 | |
| 3196 | /* Examine existing parents while marking ones we have seen... */ |
| 3197 | pp = &commit->parents; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3198 | surviving_parents = 0; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3199 | while ((p = *pp) != NULL) { |
| 3200 | struct commit *parent = p->item; |
| 3201 | if (parent->object.flags & TMP_MARK) { |
| 3202 | *pp = p->next; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3203 | if (ts) |
| 3204 | compact_treesame(revs, commit, surviving_parents); |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3205 | continue; |
| 3206 | } |
| 3207 | parent->object.flags |= TMP_MARK; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3208 | surviving_parents++; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3209 | pp = &p->next; |
| 3210 | } |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3211 | /* clear the temporary mark */ |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3212 | for (p = commit->parents; p; p = p->next) { |
| 3213 | p->item->object.flags &= ~TMP_MARK; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3214 | } |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3215 | /* no update_treesame() - removing duplicates can't affect TREESAME */ |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3216 | return surviving_parents; |
| 3217 | } |
| 3218 | |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3219 | struct merge_simplify_state { |
| 3220 | struct commit *simplified; |
| 3221 | }; |
| 3222 | |
| 3223 | static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit) |
| 3224 | { |
| 3225 | struct merge_simplify_state *st; |
| 3226 | |
| 3227 | st = lookup_decoration(&revs->merge_simplification, &commit->object); |
| 3228 | if (!st) { |
| René Scharfe | ca56dad | 2021-03-13 16:17:22 | [diff] [blame] | 3229 | CALLOC_ARRAY(st, 1); |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3230 | add_decoration(&revs->merge_simplification, &commit->object, st); |
| 3231 | } |
| 3232 | return st; |
| 3233 | } |
| 3234 | |
| Jeff King | 9163399 | 2019-03-20 08:13:36 | [diff] [blame] | 3235 | static int mark_redundant_parents(struct commit *commit) |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3236 | { |
| 3237 | struct commit_list *h = reduce_heads(commit->parents); |
| 3238 | int i = 0, marked = 0; |
| 3239 | struct commit_list *po, *pn; |
| 3240 | |
| 3241 | /* Want these for sanity-checking only */ |
| 3242 | int orig_cnt = commit_list_count(commit->parents); |
| 3243 | int cnt = commit_list_count(h); |
| 3244 | |
| 3245 | /* |
| 3246 | * Not ready to remove items yet, just mark them for now, based |
| 3247 | * on the output of reduce_heads(). reduce_heads outputs the reduced |
| 3248 | * set in its original order, so this isn't too hard. |
| 3249 | */ |
| 3250 | po = commit->parents; |
| 3251 | pn = h; |
| 3252 | while (po) { |
| 3253 | if (pn && po->item == pn->item) { |
| 3254 | pn = pn->next; |
| 3255 | i++; |
| 3256 | } else { |
| 3257 | po->item->object.flags |= TMP_MARK; |
| 3258 | marked++; |
| 3259 | } |
| 3260 | po=po->next; |
| 3261 | } |
| 3262 | |
| 3263 | if (i != cnt || cnt+marked != orig_cnt) |
| 3264 | die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked); |
| 3265 | |
| 3266 | free_commit_list(h); |
| 3267 | |
| 3268 | return marked; |
| 3269 | } |
| 3270 | |
| Jeff King | 9163399 | 2019-03-20 08:13:36 | [diff] [blame] | 3271 | static int mark_treesame_root_parents(struct commit *commit) |
| Kevin Bracey | 143f1ea | 2013-05-16 15:32:37 | [diff] [blame] | 3272 | { |
| 3273 | struct commit_list *p; |
| 3274 | int marked = 0; |
| 3275 | |
| 3276 | for (p = commit->parents; p; p = p->next) { |
| 3277 | struct commit *parent = p->item; |
| 3278 | if (!parent->parents && (parent->object.flags & TREESAME)) { |
| 3279 | parent->object.flags |= TMP_MARK; |
| 3280 | marked++; |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | return marked; |
| 3285 | } |
| 3286 | |
| Kevin Bracey | 9c129ea | 2013-05-16 15:32:36 | [diff] [blame] | 3287 | /* |
| 3288 | * Awkward naming - this means one parent we are TREESAME to. |
| 3289 | * cf mark_treesame_root_parents: root parents that are TREESAME (to an |
| 3290 | * empty tree). Better name suggestions? |
| 3291 | */ |
| 3292 | static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit) |
| 3293 | { |
| 3294 | struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object); |
| 3295 | struct commit *unmarked = NULL, *marked = NULL; |
| 3296 | struct commit_list *p; |
| 3297 | unsigned n; |
| 3298 | |
| 3299 | for (p = commit->parents, n = 0; p; p = p->next, n++) { |
| 3300 | if (ts->treesame[n]) { |
| 3301 | if (p->item->object.flags & TMP_MARK) { |
| 3302 | if (!marked) |
| 3303 | marked = p->item; |
| 3304 | } else { |
| 3305 | if (!unmarked) { |
| 3306 | unmarked = p->item; |
| 3307 | break; |
| 3308 | } |
| 3309 | } |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | /* |
| 3314 | * If we are TREESAME to a marked-for-deletion parent, but not to any |
| 3315 | * unmarked parents, unmark the first TREESAME parent. This is the |
| 3316 | * parent that the default simplify_history==1 scan would have followed, |
| 3317 | * and it doesn't make sense to omit that path when asking for a |
| 3318 | * simplified full history. Retaining it improves the chances of |
| 3319 | * understanding odd missed merges that took an old version of a file. |
| 3320 | * |
| 3321 | * Example: |
| 3322 | * |
| 3323 | * I--------*X A modified the file, but mainline merge X used |
| 3324 | * \ / "-s ours", so took the version from I. X is |
| 3325 | * `-*A--' TREESAME to I and !TREESAME to A. |
| 3326 | * |
| 3327 | * Default log from X would produce "I". Without this check, |
| 3328 | * --full-history --simplify-merges would produce "I-A-X", showing |
| 3329 | * the merge commit X and that it changed A, but not making clear that |
| 3330 | * it had just taken the I version. With this check, the topology above |
| 3331 | * is retained. |
| 3332 | * |
| 3333 | * Note that it is possible that the simplification chooses a different |
| 3334 | * TREESAME parent from the default, in which case this test doesn't |
| 3335 | * activate, and we _do_ drop the default parent. Example: |
| 3336 | * |
| 3337 | * I------X A modified the file, but it was reverted in B, |
| 3338 | * \ / meaning mainline merge X is TREESAME to both |
| 3339 | * *A-*B parents. |
| 3340 | * |
| 3341 | * Default log would produce "I" by following the first parent; |
| 3342 | * --full-history --simplify-merges will produce "I-A-B". But this is a |
| 3343 | * reasonable result - it presents a logical full history leading from |
| 3344 | * I to X, and X is not an important merge. |
| 3345 | */ |
| 3346 | if (!unmarked && marked) { |
| 3347 | marked->object.flags &= ~TMP_MARK; |
| 3348 | return 1; |
| 3349 | } |
| 3350 | |
| 3351 | return 0; |
| 3352 | } |
| 3353 | |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3354 | static int remove_marked_parents(struct rev_info *revs, struct commit *commit) |
| 3355 | { |
| 3356 | struct commit_list **pp, *p; |
| 3357 | int nth_parent, removed = 0; |
| 3358 | |
| 3359 | pp = &commit->parents; |
| 3360 | nth_parent = 0; |
| 3361 | while ((p = *pp) != NULL) { |
| 3362 | struct commit *parent = p->item; |
| 3363 | if (parent->object.flags & TMP_MARK) { |
| 3364 | parent->object.flags &= ~TMP_MARK; |
| 3365 | *pp = p->next; |
| 3366 | free(p); |
| 3367 | removed++; |
| 3368 | compact_treesame(revs, commit, nth_parent); |
| 3369 | continue; |
| 3370 | } |
| 3371 | pp = &p->next; |
| 3372 | nth_parent++; |
| 3373 | } |
| 3374 | |
| 3375 | /* Removing parents can only increase TREESAMEness */ |
| 3376 | if (removed && !(commit->object.flags & TREESAME)) |
| 3377 | update_treesame(revs, commit); |
| 3378 | |
| 3379 | return nth_parent; |
| 3380 | } |
| 3381 | |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3382 | static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail) |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3383 | { |
| 3384 | struct commit_list *p; |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 3385 | struct commit *parent; |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3386 | struct merge_simplify_state *st, *pst; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3387 | int cnt; |
| 3388 | |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3389 | st = locate_simplify_state(revs, commit); |
| 3390 | |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3391 | /* |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3392 | * Have we handled this one? |
| 3393 | */ |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3394 | if (st->simplified) |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3395 | return tail; |
| 3396 | |
| 3397 | /* |
| 3398 | * An UNINTERESTING commit simplifies to itself, so does a |
| 3399 | * root commit. We do not rewrite parents of such commit |
| 3400 | * anyway. |
| 3401 | */ |
| 3402 | if ((commit->object.flags & UNINTERESTING) || !commit->parents) { |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3403 | st->simplified = commit; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3404 | return tail; |
| 3405 | } |
| 3406 | |
| 3407 | /* |
| Junio C Hamano | 6e513ba | 2012-06-08 21:56:03 | [diff] [blame] | 3408 | * Do we know what commit all of our parents that matter |
| 3409 | * should be rewritten to? Otherwise we are not ready to |
| 3410 | * rewrite this one yet. |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3411 | */ |
| 3412 | for (cnt = 0, p = commit->parents; p; p = p->next) { |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3413 | pst = locate_simplify_state(revs, p->item); |
| 3414 | if (!pst->simplified) { |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3415 | tail = &commit_list_insert(p->item, tail)->next; |
| 3416 | cnt++; |
| 3417 | } |
| Junio C Hamano | 6e513ba | 2012-06-08 21:56:03 | [diff] [blame] | 3418 | if (revs->first_parent_only) |
| 3419 | break; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3420 | } |
| Junio C Hamano | 53030f8 | 2008-08-18 07:37:34 | [diff] [blame] | 3421 | if (cnt) { |
| 3422 | tail = &commit_list_insert(commit, tail)->next; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3423 | return tail; |
| Junio C Hamano | 53030f8 | 2008-08-18 07:37:34 | [diff] [blame] | 3424 | } |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3425 | |
| 3426 | /* |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3427 | * Rewrite our list of parents. Note that this cannot |
| 3428 | * affect our TREESAME flags in any way - a commit is |
| 3429 | * always TREESAME to its simplification. |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3430 | */ |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3431 | for (p = commit->parents; p; p = p->next) { |
| 3432 | pst = locate_simplify_state(revs, p->item); |
| 3433 | p->item = pst->simplified; |
| Junio C Hamano | 6e513ba | 2012-06-08 21:56:03 | [diff] [blame] | 3434 | if (revs->first_parent_only) |
| 3435 | break; |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3436 | } |
| Junio C Hamano | 4b7f53d | 2013-01-17 22:23:03 | [diff] [blame] | 3437 | |
| Junio C Hamano | 0290bf1 | 2013-04-08 20:10:27 | [diff] [blame] | 3438 | if (revs->first_parent_only) |
| Junio C Hamano | 6e513ba | 2012-06-08 21:56:03 | [diff] [blame] | 3439 | cnt = 1; |
| Junio C Hamano | 0290bf1 | 2013-04-08 20:10:27 | [diff] [blame] | 3440 | else |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3441 | cnt = remove_duplicate_parents(revs, commit); |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3442 | |
| 3443 | /* |
| 3444 | * It is possible that we are a merge and one side branch |
| 3445 | * does not have any commit that touches the given paths; |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3446 | * in such a case, the immediate parent from that branch |
| 3447 | * will be rewritten to be the merge base. |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3448 | * |
| 3449 | * o----X X: the commit we are looking at; |
| 3450 | * / / o: a commit that touches the paths; |
| 3451 | * ---o----' |
| 3452 | * |
| Kevin Bracey | 143f1ea | 2013-05-16 15:32:37 | [diff] [blame] | 3453 | * Further, a merge of an independent branch that doesn't |
| 3454 | * touch the path will reduce to a treesame root parent: |
| 3455 | * |
| 3456 | * ----o----X X: the commit we are looking at; |
| 3457 | * / o: a commit that touches the paths; |
| 3458 | * r r: a root commit not touching the paths |
| 3459 | * |
| 3460 | * Detect and simplify both cases. |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3461 | */ |
| 3462 | if (1 < cnt) { |
| Jeff King | 9163399 | 2019-03-20 08:13:36 | [diff] [blame] | 3463 | int marked = mark_redundant_parents(commit); |
| 3464 | marked += mark_treesame_root_parents(commit); |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3465 | if (marked) |
| Kevin Bracey | 9c129ea | 2013-05-16 15:32:36 | [diff] [blame] | 3466 | marked -= leave_one_treesame_to_parent(revs, commit); |
| 3467 | if (marked) |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3468 | cnt = remove_marked_parents(revs, commit); |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | * A commit simplifies to itself if it is a root, if it is |
| 3473 | * UNINTERESTING, if it touches the given paths, or if it is a |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 3474 | * merge and its parents don't simplify to one relevant commit |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3475 | * (the first two cases are already handled at the beginning of |
| 3476 | * this function). |
| 3477 | * |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 3478 | * Otherwise, it simplifies to what its sole relevant parent |
| 3479 | * simplifies to. |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3480 | */ |
| 3481 | if (!cnt || |
| 3482 | (commit->object.flags & UNINTERESTING) || |
| 3483 | !(commit->object.flags & TREESAME) || |
| Derrick Stolee | 8d049e1 | 2020-04-10 12:19:43 | [diff] [blame] | 3484 | (parent = one_relevant_parent(revs, commit->parents)) == NULL || |
| 3485 | (revs->show_pulls && (commit->object.flags & PULL_MERGE))) |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3486 | st->simplified = commit; |
| 3487 | else { |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 3488 | pst = locate_simplify_state(revs, parent); |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3489 | st->simplified = pst->simplified; |
| 3490 | } |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3491 | return tail; |
| 3492 | } |
| 3493 | |
| 3494 | static void simplify_merges(struct rev_info *revs) |
| 3495 | { |
| Junio C Hamano | ab9d75a | 2012-06-08 21:50:22 | [diff] [blame] | 3496 | struct commit_list *list, *next; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3497 | struct commit_list *yet_to_do, **tail; |
| Junio C Hamano | ab9d75a | 2012-06-08 21:50:22 | [diff] [blame] | 3498 | struct commit *commit; |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3499 | |
| Junio C Hamano | 5eac739 | 2008-08-14 20:52:36 | [diff] [blame] | 3500 | if (!revs->prune) |
| 3501 | return; |
| Junio C Hamano | 6534703 | 2008-08-04 00:47:16 | [diff] [blame] | 3502 | |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3503 | /* feed the list reversed */ |
| 3504 | yet_to_do = NULL; |
| Junio C Hamano | ab9d75a | 2012-06-08 21:50:22 | [diff] [blame] | 3505 | for (list = revs->commits; list; list = next) { |
| 3506 | commit = list->item; |
| 3507 | next = list->next; |
| 3508 | /* |
| 3509 | * Do not free(list) here yet; the original list |
| 3510 | * is used later in this function. |
| 3511 | */ |
| 3512 | commit_list_insert(commit, &yet_to_do); |
| 3513 | } |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3514 | while (yet_to_do) { |
| 3515 | list = yet_to_do; |
| 3516 | yet_to_do = NULL; |
| 3517 | tail = &yet_to_do; |
| 3518 | while (list) { |
| René Scharfe | e510ab8 | 2015-10-24 16:21:31 | [diff] [blame] | 3519 | commit = pop_commit(&list); |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3520 | tail = simplify_one(revs, commit, tail); |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | /* clean up the result, removing the simplified ones */ |
| 3525 | list = revs->commits; |
| 3526 | revs->commits = NULL; |
| 3527 | tail = &revs->commits; |
| 3528 | while (list) { |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3529 | struct merge_simplify_state *st; |
| Junio C Hamano | ab9d75a | 2012-06-08 21:50:22 | [diff] [blame] | 3530 | |
| René Scharfe | e510ab8 | 2015-10-24 16:21:31 | [diff] [blame] | 3531 | commit = pop_commit(&list); |
| Junio C Hamano | faf0156 | 2008-08-14 17:59:44 | [diff] [blame] | 3532 | st = locate_simplify_state(revs, commit); |
| 3533 | if (st->simplified == commit) |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3534 | tail = &commit_list_insert(commit, tail)->next; |
| 3535 | } |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3536 | } |
| 3537 | |
| Junio C Hamano | f35f560 | 2008-04-03 09:12:06 | [diff] [blame] | 3538 | static void set_children(struct rev_info *revs) |
| 3539 | { |
| 3540 | struct commit_list *l; |
| 3541 | for (l = revs->commits; l; l = l->next) { |
| 3542 | struct commit *commit = l->item; |
| 3543 | struct commit_list *p; |
| 3544 | |
| 3545 | for (p = commit->parents; p; p = p->next) |
| 3546 | add_child(revs, p->item, commit); |
| 3547 | } |
| 3548 | } |
| 3549 | |
| Heiko Voigt | bcc0a3e | 2012-03-29 07:21:21 | [diff] [blame] | 3550 | void reset_revision_walk(void) |
| 3551 | { |
| Mike Hommey | ffa1f28 | 2019-11-22 08:37:03 | [diff] [blame] | 3552 | clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE); |
| Heiko Voigt | bcc0a3e | 2012-03-29 07:21:21 | [diff] [blame] | 3553 | } |
| 3554 | |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 3555 | static int mark_uninteresting(const struct object_id *oid, |
| Jeff King | be252d3 | 2023-02-24 06:39:24 | [diff] [blame] | 3556 | struct packed_git *pack UNUSED, |
| 3557 | uint32_t pos UNUSED, |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 3558 | void *cb) |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 3559 | { |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 3560 | struct rev_info *revs = cb; |
| Jeff King | c1fa951 | 2021-04-13 07:17:48 | [diff] [blame] | 3561 | struct object *o = lookup_unknown_object(revs->repo, oid); |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 3562 | o->flags |= UNINTERESTING | SEEN; |
| 3563 | return 0; |
| 3564 | } |
| 3565 | |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3566 | define_commit_slab(indegree_slab, int); |
| 3567 | define_commit_slab(author_date_slab, timestamp_t); |
| 3568 | |
| 3569 | struct topo_walk_info { |
| Abhishek Kumar | d7f9278 | 2021-01-16 18:11:13 | [diff] [blame] | 3570 | timestamp_t min_generation; |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3571 | struct prio_queue explore_queue; |
| 3572 | struct prio_queue indegree_queue; |
| 3573 | struct prio_queue topo_queue; |
| 3574 | struct indegree_slab indegree; |
| 3575 | struct author_date_slab author_date; |
| 3576 | }; |
| 3577 | |
| Derrick Stolee | 90b666d | 2020-12-30 04:31:53 | [diff] [blame] | 3578 | static int topo_walk_atexit_registered; |
| 3579 | static unsigned int count_explore_walked; |
| 3580 | static unsigned int count_indegree_walked; |
| 3581 | static unsigned int count_topo_walked; |
| 3582 | |
| 3583 | static void trace2_topo_walk_statistics_atexit(void) |
| 3584 | { |
| 3585 | struct json_writer jw = JSON_WRITER_INIT; |
| 3586 | |
| 3587 | jw_object_begin(&jw, 0); |
| 3588 | jw_object_intmax(&jw, "count_explore_walked", count_explore_walked); |
| 3589 | jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked); |
| 3590 | jw_object_intmax(&jw, "count_topo_walked", count_topo_walked); |
| 3591 | jw_end(&jw); |
| 3592 | |
| 3593 | trace2_data_json("topo_walk", the_repository, "statistics", &jw); |
| 3594 | |
| 3595 | jw_release(&jw); |
| 3596 | } |
| 3597 | |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3598 | static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag) |
| 3599 | { |
| 3600 | if (c->object.flags & flag) |
| 3601 | return; |
| 3602 | |
| 3603 | c->object.flags |= flag; |
| 3604 | prio_queue_put(q, c); |
| 3605 | } |
| 3606 | |
| 3607 | static void explore_walk_step(struct rev_info *revs) |
| 3608 | { |
| 3609 | struct topo_walk_info *info = revs->topo_walk_info; |
| 3610 | struct commit_list *p; |
| 3611 | struct commit *c = prio_queue_get(&info->explore_queue); |
| 3612 | |
| 3613 | if (!c) |
| 3614 | return; |
| 3615 | |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 3616 | if (repo_parse_commit_gently(revs->repo, c, 1) < 0) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3617 | return; |
| 3618 | |
| Derrick Stolee | 90b666d | 2020-12-30 04:31:53 | [diff] [blame] | 3619 | count_explore_walked++; |
| 3620 | |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3621 | if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE) |
| 3622 | record_author_date(&info->author_date, c); |
| 3623 | |
| 3624 | if (revs->max_age != -1 && (c->date < revs->max_age)) |
| 3625 | c->object.flags |= UNINTERESTING; |
| 3626 | |
| 3627 | if (process_parents(revs, c, NULL, NULL) < 0) |
| 3628 | return; |
| 3629 | |
| 3630 | if (c->object.flags & UNINTERESTING) |
| Jerry Zhang | 9d505b7 | 2022-01-11 21:39:41 | [diff] [blame] | 3631 | mark_parents_uninteresting(revs, c); |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3632 | |
| 3633 | for (p = c->parents; p; p = p->next) |
| 3634 | test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED); |
| 3635 | } |
| 3636 | |
| 3637 | static void explore_to_depth(struct rev_info *revs, |
| Abhishek Kumar | d7f9278 | 2021-01-16 18:11:13 | [diff] [blame] | 3638 | timestamp_t gen_cutoff) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3639 | { |
| 3640 | struct topo_walk_info *info = revs->topo_walk_info; |
| 3641 | struct commit *c; |
| 3642 | while ((c = prio_queue_peek(&info->explore_queue)) && |
| Abhishek Kumar | c49c82a | 2020-06-17 09:14:10 | [diff] [blame] | 3643 | commit_graph_generation(c) >= gen_cutoff) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3644 | explore_walk_step(revs); |
| 3645 | } |
| 3646 | |
| 3647 | static void indegree_walk_step(struct rev_info *revs) |
| 3648 | { |
| 3649 | struct commit_list *p; |
| 3650 | struct topo_walk_info *info = revs->topo_walk_info; |
| 3651 | struct commit *c = prio_queue_get(&info->indegree_queue); |
| 3652 | |
| 3653 | if (!c) |
| 3654 | return; |
| 3655 | |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 3656 | if (repo_parse_commit_gently(revs->repo, c, 1) < 0) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3657 | return; |
| 3658 | |
| Derrick Stolee | 90b666d | 2020-12-30 04:31:53 | [diff] [blame] | 3659 | count_indegree_walked++; |
| 3660 | |
| Abhishek Kumar | c49c82a | 2020-06-17 09:14:10 | [diff] [blame] | 3661 | explore_to_depth(revs, commit_graph_generation(c)); |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3662 | |
| 3663 | for (p = c->parents; p; p = p->next) { |
| 3664 | struct commit *parent = p->item; |
| 3665 | int *pi = indegree_slab_at(&info->indegree, parent); |
| 3666 | |
| Abhishek Kumar | 2f9bbb6 | 2021-01-16 18:11:09 | [diff] [blame] | 3667 | if (repo_parse_commit_gently(revs->repo, parent, 1) < 0) |
| 3668 | return; |
| 3669 | |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3670 | if (*pi) |
| 3671 | (*pi)++; |
| 3672 | else |
| 3673 | *pi = 2; |
| 3674 | |
| 3675 | test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE); |
| 3676 | |
| 3677 | if (revs->first_parent_only) |
| 3678 | return; |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | static void compute_indegrees_to_depth(struct rev_info *revs, |
| Abhishek Kumar | d7f9278 | 2021-01-16 18:11:13 | [diff] [blame] | 3683 | timestamp_t gen_cutoff) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3684 | { |
| 3685 | struct topo_walk_info *info = revs->topo_walk_info; |
| 3686 | struct commit *c; |
| 3687 | while ((c = prio_queue_peek(&info->indegree_queue)) && |
| Abhishek Kumar | c49c82a | 2020-06-17 09:14:10 | [diff] [blame] | 3688 | commit_graph_generation(c) >= gen_cutoff) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3689 | indegree_walk_step(revs); |
| 3690 | } |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3691 | |
| Ævar Arnfjörð Bjarmason | ae1b383 | 2022-04-14 05:56:39 | [diff] [blame] | 3692 | static void release_revisions_topo_walk_info(struct topo_walk_info *info) |
| Mike Hommey | 0aa0c2b | 2019-11-22 08:37:04 | [diff] [blame] | 3693 | { |
| Ævar Arnfjörð Bjarmason | ae1b383 | 2022-04-14 05:56:39 | [diff] [blame] | 3694 | if (!info) |
| 3695 | return; |
| Mike Hommey | 0aa0c2b | 2019-11-22 08:37:04 | [diff] [blame] | 3696 | clear_prio_queue(&info->explore_queue); |
| 3697 | clear_prio_queue(&info->indegree_queue); |
| 3698 | clear_prio_queue(&info->topo_queue); |
| 3699 | clear_indegree_slab(&info->indegree); |
| 3700 | clear_author_date_slab(&info->author_date); |
| Ævar Arnfjörð Bjarmason | ae1b383 | 2022-04-14 05:56:39 | [diff] [blame] | 3701 | free(info); |
| 3702 | } |
| Mike Hommey | 0aa0c2b | 2019-11-22 08:37:04 | [diff] [blame] | 3703 | |
| Ævar Arnfjörð Bjarmason | ae1b383 | 2022-04-14 05:56:39 | [diff] [blame] | 3704 | static void reset_topo_walk(struct rev_info *revs) |
| 3705 | { |
| 3706 | release_revisions_topo_walk_info(revs->topo_walk_info); |
| 3707 | revs->topo_walk_info = NULL; |
| Mike Hommey | 0aa0c2b | 2019-11-22 08:37:04 | [diff] [blame] | 3708 | } |
| 3709 | |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3710 | static void init_topo_walk(struct rev_info *revs) |
| 3711 | { |
| 3712 | struct topo_walk_info *info; |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3713 | struct commit_list *list; |
| Mike Hommey | 0aa0c2b | 2019-11-22 08:37:04 | [diff] [blame] | 3714 | if (revs->topo_walk_info) |
| 3715 | reset_topo_walk(revs); |
| 3716 | |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3717 | revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info)); |
| 3718 | info = revs->topo_walk_info; |
| 3719 | memset(info, 0, sizeof(struct topo_walk_info)); |
| 3720 | |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3721 | init_indegree_slab(&info->indegree); |
| 3722 | memset(&info->explore_queue, 0, sizeof(info->explore_queue)); |
| 3723 | memset(&info->indegree_queue, 0, sizeof(info->indegree_queue)); |
| 3724 | memset(&info->topo_queue, 0, sizeof(info->topo_queue)); |
| 3725 | |
| 3726 | switch (revs->sort_order) { |
| 3727 | default: /* REV_SORT_IN_GRAPH_ORDER */ |
| 3728 | info->topo_queue.compare = NULL; |
| 3729 | break; |
| 3730 | case REV_SORT_BY_COMMIT_DATE: |
| 3731 | info->topo_queue.compare = compare_commits_by_commit_date; |
| 3732 | break; |
| 3733 | case REV_SORT_BY_AUTHOR_DATE: |
| 3734 | init_author_date_slab(&info->author_date); |
| 3735 | info->topo_queue.compare = compare_commits_by_author_date; |
| 3736 | info->topo_queue.cb_data = &info->author_date; |
| 3737 | break; |
| 3738 | } |
| 3739 | |
| 3740 | info->explore_queue.compare = compare_commits_by_gen_then_commit_date; |
| 3741 | info->indegree_queue.compare = compare_commits_by_gen_then_commit_date; |
| 3742 | |
| 3743 | info->min_generation = GENERATION_NUMBER_INFINITY; |
| 3744 | for (list = revs->commits; list; list = list->next) { |
| 3745 | struct commit *c = list->item; |
| Abhishek Kumar | d7f9278 | 2021-01-16 18:11:13 | [diff] [blame] | 3746 | timestamp_t generation; |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3747 | |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 3748 | if (repo_parse_commit_gently(revs->repo, c, 1)) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3749 | continue; |
| 3750 | |
| 3751 | test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED); |
| 3752 | test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE); |
| 3753 | |
| Abhishek Kumar | c752ad0 | 2020-06-17 09:14:11 | [diff] [blame] | 3754 | generation = commit_graph_generation(c); |
| 3755 | if (generation < info->min_generation) |
| 3756 | info->min_generation = generation; |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3757 | |
| 3758 | *(indegree_slab_at(&info->indegree, c)) = 1; |
| 3759 | |
| 3760 | if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE) |
| 3761 | record_author_date(&info->author_date, c); |
| 3762 | } |
| 3763 | compute_indegrees_to_depth(revs, info->min_generation); |
| 3764 | |
| 3765 | for (list = revs->commits; list; list = list->next) { |
| 3766 | struct commit *c = list->item; |
| 3767 | |
| 3768 | if (*(indegree_slab_at(&info->indegree, c)) == 1) |
| 3769 | prio_queue_put(&info->topo_queue, c); |
| 3770 | } |
| 3771 | |
| 3772 | /* |
| 3773 | * This is unfortunate; the initial tips need to be shown |
| 3774 | * in the order given from the revision traversal machinery. |
| 3775 | */ |
| 3776 | if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER) |
| 3777 | prio_queue_reverse(&info->topo_queue); |
| Derrick Stolee | 90b666d | 2020-12-30 04:31:53 | [diff] [blame] | 3778 | |
| 3779 | if (trace2_is_enabled() && !topo_walk_atexit_registered) { |
| 3780 | atexit(trace2_topo_walk_statistics_atexit); |
| 3781 | topo_walk_atexit_registered = 1; |
| 3782 | } |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | static struct commit *next_topo_commit(struct rev_info *revs) |
| 3786 | { |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3787 | struct commit *c; |
| 3788 | struct topo_walk_info *info = revs->topo_walk_info; |
| 3789 | |
| 3790 | /* pop next off of topo_queue */ |
| 3791 | c = prio_queue_get(&info->topo_queue); |
| 3792 | |
| 3793 | if (c) |
| 3794 | *(indegree_slab_at(&info->indegree, c)) = 0; |
| 3795 | |
| 3796 | return c; |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | static void expand_topo_walk(struct rev_info *revs, struct commit *commit) |
| 3800 | { |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3801 | struct commit_list *p; |
| 3802 | struct topo_walk_info *info = revs->topo_walk_info; |
| 3803 | if (process_parents(revs, commit, NULL, NULL) < 0) { |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3804 | if (!revs->ignore_missing_links) |
| 3805 | die("Failed to traverse parents of commit %s", |
| 3806 | oid_to_hex(&commit->object.oid)); |
| 3807 | } |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3808 | |
| Derrick Stolee | 90b666d | 2020-12-30 04:31:53 | [diff] [blame] | 3809 | count_topo_walked++; |
| 3810 | |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3811 | for (p = commit->parents; p; p = p->next) { |
| 3812 | struct commit *parent = p->item; |
| 3813 | int *pi; |
| Abhishek Kumar | d7f9278 | 2021-01-16 18:11:13 | [diff] [blame] | 3814 | timestamp_t generation; |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3815 | |
| Derrick Stolee | 1d8e31a | 2019-05-21 13:59:53 | [diff] [blame] | 3816 | if (parent->object.flags & UNINTERESTING) |
| 3817 | continue; |
| 3818 | |
| Michael Forney | ea3f7e5 | 2020-06-23 20:56:58 | [diff] [blame] | 3819 | if (repo_parse_commit_gently(revs->repo, parent, 1) < 0) |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3820 | continue; |
| 3821 | |
| Abhishek Kumar | c752ad0 | 2020-06-17 09:14:11 | [diff] [blame] | 3822 | generation = commit_graph_generation(parent); |
| 3823 | if (generation < info->min_generation) { |
| 3824 | info->min_generation = generation; |
| Derrick Stolee | b454241 | 2018-11-01 13:46:22 | [diff] [blame] | 3825 | compute_indegrees_to_depth(revs, info->min_generation); |
| 3826 | } |
| 3827 | |
| 3828 | pi = indegree_slab_at(&info->indegree, parent); |
| 3829 | |
| 3830 | (*pi)--; |
| 3831 | if (*pi == 1) |
| 3832 | prio_queue_put(&info->topo_queue, parent); |
| 3833 | |
| 3834 | if (revs->first_parent_only) |
| 3835 | return; |
| 3836 | } |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3837 | } |
| 3838 | |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3839 | int prepare_revision_walk(struct rev_info *revs) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 3840 | { |
| Jeff King | 1da1e07 | 2014-10-15 22:35:12 | [diff] [blame] | 3841 | int i; |
| 3842 | struct object_array old_pending; |
| René Scharfe | 2e7da8e | 2012-04-25 20:35:41 | [diff] [blame] | 3843 | struct commit_list **next = &revs->commits; |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3844 | |
| Jeff King | 1da1e07 | 2014-10-15 22:35:12 | [diff] [blame] | 3845 | memcpy(&old_pending, &revs->pending, sizeof(old_pending)); |
| Linus Torvalds | 1f1e895 | 2006-06-20 00:42:35 | [diff] [blame] | 3846 | revs->pending.nr = 0; |
| 3847 | revs->pending.alloc = 0; |
| 3848 | revs->pending.objects = NULL; |
| Jeff King | 1da1e07 | 2014-10-15 22:35:12 | [diff] [blame] | 3849 | for (i = 0; i < old_pending.nr; i++) { |
| 3850 | struct object_array_entry *e = old_pending.objects + i; |
| Jeff King | 2073949 | 2014-10-15 22:43:19 | [diff] [blame] | 3851 | struct commit *commit = handle_commit(revs, e); |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3852 | if (commit) { |
| 3853 | if (!(commit->object.flags & SEEN)) { |
| 3854 | commit->object.flags |= SEEN; |
| René Scharfe | 2e7da8e | 2012-04-25 20:35:41 | [diff] [blame] | 3855 | next = commit_list_append(commit, next); |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3856 | } |
| 3857 | } |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3858 | } |
| René Scharfe | f1230fb | 2017-12-25 17:47:51 | [diff] [blame] | 3859 | object_array_clear(&old_pending); |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 3860 | |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3861 | /* Signal whether we need per-parent treesame decoration */ |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 3862 | if (revs->simplify_merges || |
| 3863 | (revs->limited && limiting_can_increase_treesame(revs))) |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3864 | revs->treesame.name = "treesame"; |
| 3865 | |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 3866 | if (revs->exclude_promisor_objects) { |
| Nguyễn Thái Ngọc Duy | b3c7eef | 2018-09-21 15:57:39 | [diff] [blame] | 3867 | for_each_packed_object(mark_uninteresting, revs, |
| Jonathan Tan | df11e19 | 2017-12-08 15:27:15 | [diff] [blame] | 3868 | FOR_EACH_OBJECT_PROMISOR_ONLY); |
| 3869 | } |
| 3870 | |
| Derrick Stolee | f32dde8 | 2020-05-11 11:56:19 | [diff] [blame] | 3871 | if (!revs->reflog_info) |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 3872 | prepare_to_use_bloom_filter(revs); |
| Patrick Steinhardt | 29ef1f2 | 2021-08-05 11:25:24 | [diff] [blame] | 3873 | if (!revs->unsorted_input) |
| Martin von Zweigbergk | ca92e59 | 2012-08-29 06:15:54 | [diff] [blame] | 3874 | commit_list_sort_by_date(&revs->commits); |
| Linus Torvalds | ba1d450 | 2006-04-15 19:09:56 | [diff] [blame] | 3875 | if (revs->no_walk) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3876 | return 0; |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3877 | if (revs->limited) { |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3878 | if (limit_list(revs) < 0) |
| 3879 | return -1; |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 3880 | if (revs->topo_order) |
| 3881 | sort_in_topological_order(&revs->commits, revs->sort_order); |
| 3882 | } else if (revs->topo_order) |
| 3883 | init_topo_walk(revs); |
| SZEDER Gábor | 3cb9d2b | 2020-05-11 11:56:17 | [diff] [blame] | 3884 | if (revs->line_level_traverse && want_ancestry(revs)) |
| 3885 | /* |
| 3886 | * At the moment we can only do line-level log with parent |
| 3887 | * rewriting by performing this expensive pre-filtering step. |
| 3888 | * If parent rewriting is not requested, then we rather |
| 3889 | * perform the line-level log filtering during the regular |
| 3890 | * history traversal. |
| 3891 | */ |
| Thomas Rast | 12da1d1 | 2013-03-28 16:47:32 | [diff] [blame] | 3892 | line_log_filter(revs); |
| Junio C Hamano | 6546b59 | 2008-07-31 08:17:41 | [diff] [blame] | 3893 | if (revs->simplify_merges) |
| 3894 | simplify_merges(revs); |
| Junio C Hamano | f35f560 | 2008-04-03 09:12:06 | [diff] [blame] | 3895 | if (revs->children.name) |
| 3896 | set_children(revs); |
| Garima Singh | a56b946 | 2020-04-06 16:59:52 | [diff] [blame] | 3897 | |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3898 | return 0; |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 3899 | } |
| 3900 | |
| Jeff King | 8320b1d | 2019-04-04 01:41:09 | [diff] [blame] | 3901 | static enum rewrite_result rewrite_one_1(struct rev_info *revs, |
| 3902 | struct commit **pp, |
| 3903 | struct prio_queue *queue) |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 3904 | { |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 3905 | for (;;) { |
| 3906 | struct commit *p = *pp; |
| Linus Torvalds | 3381c79 | 2006-04-09 00:05:58 | [diff] [blame] | 3907 | if (!revs->limited) |
| Jeff King | 8320b1d | 2019-04-04 01:41:09 | [diff] [blame] | 3908 | if (process_parents(revs, p, NULL, queue) < 0) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3909 | return rewrite_one_error; |
| Linus Torvalds | 7dc0fe3 | 2007-11-13 07:16:08 | [diff] [blame] | 3910 | if (p->object.flags & UNINTERESTING) |
| 3911 | return rewrite_one_ok; |
| 3912 | if (!(p->object.flags & TREESAME)) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3913 | return rewrite_one_ok; |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 3914 | if (!p->parents) |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3915 | return rewrite_one_noparents; |
| Junio C Hamano | afe8a90 | 2022-05-02 16:50:37 | [diff] [blame] | 3916 | if (!(p = one_relevant_parent(revs, p->parents))) |
| Kevin Bracey | 4d82660 | 2013-05-16 15:32:39 | [diff] [blame] | 3917 | return rewrite_one_ok; |
| 3918 | *pp = p; |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 3919 | } |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 3920 | } |
| 3921 | |
| Jeff King | 8320b1d | 2019-04-04 01:41:09 | [diff] [blame] | 3922 | static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list) |
| 3923 | { |
| 3924 | while (q->nr) { |
| 3925 | struct commit *item = prio_queue_peek(q); |
| 3926 | struct commit_list *p = *list; |
| 3927 | |
| 3928 | if (p && p->item->date >= item->date) |
| 3929 | list = &p->next; |
| 3930 | else { |
| 3931 | p = commit_list_insert(item, list); |
| 3932 | list = &p->next; /* skip newly added item */ |
| 3933 | prio_queue_get(q); /* pop item */ |
| 3934 | } |
| 3935 | } |
| 3936 | } |
| 3937 | |
| 3938 | static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp) |
| 3939 | { |
| 3940 | struct prio_queue queue = { compare_commits_by_commit_date }; |
| 3941 | enum rewrite_result ret = rewrite_one_1(revs, pp, &queue); |
| 3942 | merge_queue_into_list(&queue, &revs->commits); |
| 3943 | clear_prio_queue(&queue); |
| 3944 | return ret; |
| 3945 | } |
| 3946 | |
| Bo Yang | c7edcae | 2013-03-28 16:47:31 | [diff] [blame] | 3947 | int rewrite_parents(struct rev_info *revs, struct commit *commit, |
| 3948 | rewrite_parent_fn_t rewrite_parent) |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 3949 | { |
| 3950 | struct commit_list **pp = &commit->parents; |
| 3951 | while (*pp) { |
| 3952 | struct commit_list *parent = *pp; |
| Bo Yang | c7edcae | 2013-03-28 16:47:31 | [diff] [blame] | 3953 | switch (rewrite_parent(revs, &parent->item)) { |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3954 | case rewrite_one_ok: |
| 3955 | break; |
| 3956 | case rewrite_one_noparents: |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 3957 | *pp = parent->next; |
| 3958 | continue; |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3959 | case rewrite_one_error: |
| 3960 | return -1; |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 3961 | } |
| 3962 | pp = &parent->next; |
| 3963 | } |
| Kevin Bracey | d0af663 | 2013-05-16 15:32:34 | [diff] [blame] | 3964 | remove_duplicate_parents(revs, commit); |
| Alex Riesen | cc0e6c5 | 2007-05-04 21:54:57 | [diff] [blame] | 3965 | return 0; |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 3966 | } |
| Linus Torvalds | a4a88b2 | 2006-02-28 19:24:00 | [diff] [blame] | 3967 | |
| Junio C Hamano | 8ecae9b | 2006-09-17 22:43:40 | [diff] [blame] | 3968 | static int commit_match(struct commit *commit, struct rev_info *opt) |
| 3969 | { |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 3970 | int retval; |
| Jeff King | 04deccd | 2013-02-11 20:59:58 | [diff] [blame] | 3971 | const char *encoding; |
| Jeff King | b000c59 | 2014-06-10 21:39:30 | [diff] [blame] | 3972 | const char *message; |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 3973 | struct strbuf buf = STRBUF_INIT; |
| Jeff King | 04deccd | 2013-02-11 20:59:58 | [diff] [blame] | 3974 | |
| Junio C Hamano | 80235ba | 2010-01-18 04:09:06 | [diff] [blame] | 3975 | if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list) |
| Junio C Hamano | 8ecae9b | 2006-09-17 22:43:40 | [diff] [blame] | 3976 | return 1; |
| Junio C Hamano | baa6378 | 2012-09-29 18:59:52 | [diff] [blame] | 3977 | |
| 3978 | /* Prepend "fake" headers as needed */ |
| 3979 | if (opt->grep_filter.use_reflog_filter) { |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 3980 | strbuf_addstr(&buf, "reflog "); |
| 3981 | get_reflog_message(&buf, opt->reflog_info); |
| 3982 | strbuf_addch(&buf, '\n'); |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 3983 | } |
| Junio C Hamano | baa6378 | 2012-09-29 18:59:52 | [diff] [blame] | 3984 | |
| Jeff King | 04deccd | 2013-02-11 20:59:58 | [diff] [blame] | 3985 | /* |
| 3986 | * We grep in the user's output encoding, under the assumption that it |
| 3987 | * is the encoding they are most likely to write their grep pattern |
| 3988 | * for. In addition, it means we will match the "notes" encoding below, |
| 3989 | * so we will not end up with a buffer that has two different encodings |
| 3990 | * in it. |
| 3991 | */ |
| 3992 | encoding = get_log_output_encoding(); |
| Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 13:58:48 | [diff] [blame] | 3993 | message = repo_logmsg_reencode(the_repository, commit, NULL, encoding); |
| Jeff King | 04deccd | 2013-02-11 20:59:58 | [diff] [blame] | 3994 | |
| Junio C Hamano | baa6378 | 2012-09-29 18:59:52 | [diff] [blame] | 3995 | /* Copy the commit to temporary if we are using "fake" headers */ |
| 3996 | if (buf.len) |
| Jeff King | 04deccd | 2013-02-11 20:59:58 | [diff] [blame] | 3997 | strbuf_addstr(&buf, message); |
| Junio C Hamano | baa6378 | 2012-09-29 18:59:52 | [diff] [blame] | 3998 | |
| Junio C Hamano | df874fa | 2013-01-08 08:02:49 | [diff] [blame] | 3999 | if (opt->grep_filter.header_list && opt->mailmap) { |
| Siddharth Asthana | e9c1b0e | 2022-07-18 19:50:59 | [diff] [blame] | 4000 | const char *commit_headers[] = { "author ", "committer ", NULL }; |
| 4001 | |
| Antoine Pelisse | d72fbe8 | 2013-01-05 21:26:45 | [diff] [blame] | 4002 | if (!buf.len) |
| Jeff King | 04deccd | 2013-02-11 20:59:58 | [diff] [blame] | 4003 | strbuf_addstr(&buf, message); |
| Antoine Pelisse | d72fbe8 | 2013-01-05 21:26:45 | [diff] [blame] | 4004 | |
| Siddharth Asthana | 66a8a95 | 2022-07-18 19:51:01 | [diff] [blame] | 4005 | apply_mailmap_to_header(&buf, commit_headers, opt->mailmap); |
| Antoine Pelisse | d72fbe8 | 2013-01-05 21:26:45 | [diff] [blame] | 4006 | } |
| 4007 | |
| Nguyễn Thái Ngọc Duy | 38cfe91 | 2012-09-29 04:41:29 | [diff] [blame] | 4008 | /* Append "fake" message parts as needed */ |
| 4009 | if (opt->show_notes) { |
| 4010 | if (!buf.len) |
| Jeff King | 04deccd | 2013-02-11 20:59:58 | [diff] [blame] | 4011 | strbuf_addstr(&buf, message); |
| brian m. carlson | fb61e4d | 2017-05-30 17:30:41 | [diff] [blame] | 4012 | format_display_notes(&commit->object.oid, &buf, encoding, 1); |
| Nguyễn Thái Ngọc Duy | 38cfe91 | 2012-09-29 04:41:29 | [diff] [blame] | 4013 | } |
| 4014 | |
| Jeff King | b000c59 | 2014-06-10 21:39:30 | [diff] [blame] | 4015 | /* |
| 4016 | * Find either in the original commit message, or in the temporary. |
| 4017 | * Note that we cast away the constness of "message" here. It is |
| 4018 | * const because it may come from the cached commit buffer. That's OK, |
| 4019 | * because we know that it is modifiable heap memory, and that while |
| 4020 | * grep_buffer may modify it for speed, it will restore any |
| 4021 | * changes before returning. |
| 4022 | */ |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 4023 | if (buf.len) |
| 4024 | retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len); |
| 4025 | else |
| 4026 | retval = grep_buffer(&opt->grep_filter, |
| Jeff King | b000c59 | 2014-06-10 21:39:30 | [diff] [blame] | 4027 | (char *)message, strlen(message)); |
| Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 04:41:28 | [diff] [blame] | 4028 | strbuf_release(&buf); |
| Ævar Arnfjörð Bjarmason | ecb5091 | 2023-03-28 13:58:48 | [diff] [blame] | 4029 | repo_unuse_commit_buffer(the_repository, commit, message); |
| René Scharfe | 794c000 | 2021-12-17 16:48:49 | [diff] [blame] | 4030 | return retval; |
| Junio C Hamano | 8ecae9b | 2006-09-17 22:43:40 | [diff] [blame] | 4031 | } |
| 4032 | |
| Thomas Rast | 53d00b3 | 2013-07-31 20:13:20 | [diff] [blame] | 4033 | static inline int want_ancestry(const struct rev_info *revs) |
| Junio C Hamano | f35f560 | 2008-04-03 09:12:06 | [diff] [blame] | 4034 | { |
| Junio C Hamano | 8bb6588 | 2008-07-08 22:25:44 | [diff] [blame] | 4035 | return (revs->rewrite_parents || revs->children.name); |
| Junio C Hamano | f35f560 | 2008-04-03 09:12:06 | [diff] [blame] | 4036 | } |
| 4037 | |
| Jeff King | de23944 | 2017-07-07 09:16:21 | [diff] [blame] | 4038 | /* |
| 4039 | * Return a timestamp to be used for --since/--until comparisons for this |
| 4040 | * commit, based on the revision options. |
| 4041 | */ |
| 4042 | static timestamp_t comparison_date(const struct rev_info *revs, |
| 4043 | struct commit *commit) |
| 4044 | { |
| 4045 | return revs->reflog_info ? |
| 4046 | get_reflog_timestamp(revs->reflog_info) : |
| 4047 | commit->date; |
| 4048 | } |
| 4049 | |
| Adam Simpkins | beb5af4 | 2009-08-19 02:34:33 | [diff] [blame] | 4050 | enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit) |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4051 | { |
| 4052 | if (commit->object.flags & SHOWN) |
| 4053 | return commit_ignore; |
| brian m. carlson | 14c3c80 | 2018-05-02 00:25:33 | [diff] [blame] | 4054 | if (revs->unpacked && has_object_pack(&commit->object.oid)) |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4055 | return commit_ignore; |
| Taylor Blau | c9fff00 | 2021-02-23 02:25:07 | [diff] [blame] | 4056 | if (revs->no_kept_objects) { |
| 4057 | if (has_object_kept_pack(&commit->object.oid, |
| 4058 | revs->keep_pack_cache_flags)) |
| 4059 | return commit_ignore; |
| 4060 | } |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4061 | if (commit->object.flags & UNINTERESTING) |
| 4062 | return commit_ignore; |
| SZEDER Gábor | 3cb9d2b | 2020-05-11 11:56:17 | [diff] [blame] | 4063 | if (revs->line_level_traverse && !want_ancestry(revs)) { |
| 4064 | /* |
| 4065 | * In case of line-level log with parent rewriting |
| 4066 | * prepare_revision_walk() already took care of all line-level |
| 4067 | * log filtering, and there is nothing left to do here. |
| 4068 | * |
| 4069 | * If parent rewriting was not requested, then this is the |
| 4070 | * place to perform the line-level log filtering. Notably, |
| 4071 | * this check, though expensive, must come before the other, |
| 4072 | * cheaper filtering conditions, because the tracked line |
| 4073 | * ranges must be adjusted even when the commit will end up |
| 4074 | * being ignored based on other conditions. |
| 4075 | */ |
| 4076 | if (!line_log_process_ranges_arbitrary_commit(revs, commit)) |
| 4077 | return commit_ignore; |
| 4078 | } |
| Jeff King | de23944 | 2017-07-07 09:16:21 | [diff] [blame] | 4079 | if (revs->min_age != -1 && |
| 4080 | comparison_date(revs, commit) > revs->min_age) |
| 4081 | return commit_ignore; |
| Miklos Vajna | 9669778 | 2022-04-23 12:59:57 | [diff] [blame] | 4082 | if (revs->max_age_as_filter != -1 && |
| 4083 | comparison_date(revs, commit) < revs->max_age_as_filter) |
| 4084 | return commit_ignore; |
| Michael J Gruber | ad5aeed | 2011-03-21 10:14:06 | [diff] [blame] | 4085 | if (revs->min_parents || (revs->max_parents >= 0)) { |
| Kevin Bracey | bf3418b | 2013-05-16 15:32:40 | [diff] [blame] | 4086 | int n = commit_list_count(commit->parents); |
| Michael J Gruber | ad5aeed | 2011-03-21 10:14:06 | [diff] [blame] | 4087 | if ((n < revs->min_parents) || |
| 4088 | ((revs->max_parents >= 0) && (n > revs->max_parents))) |
| 4089 | return commit_ignore; |
| 4090 | } |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4091 | if (!commit_match(commit, revs)) |
| 4092 | return commit_ignore; |
| Linus Torvalds | 53b2c82 | 2007-11-05 21:22:34 | [diff] [blame] | 4093 | if (revs->prune && revs->dense) { |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4094 | /* Commit without changes? */ |
| Linus Torvalds | 7dc0fe3 | 2007-11-13 07:16:08 | [diff] [blame] | 4095 | if (commit->object.flags & TREESAME) { |
| Kevin Bracey | bf3418b | 2013-05-16 15:32:40 | [diff] [blame] | 4096 | int n; |
| 4097 | struct commit_list *p; |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4098 | /* drop merges unless we want parenthood */ |
| Junio C Hamano | f35f560 | 2008-04-03 09:12:06 | [diff] [blame] | 4099 | if (!want_ancestry(revs)) |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4100 | return commit_ignore; |
| Derrick Stolee | 8d049e1 | 2020-04-10 12:19:43 | [diff] [blame] | 4101 | |
| 4102 | if (revs->show_pulls && (commit->object.flags & PULL_MERGE)) |
| 4103 | return commit_show; |
| 4104 | |
| Kevin Bracey | bf3418b | 2013-05-16 15:32:40 | [diff] [blame] | 4105 | /* |
| 4106 | * If we want ancestry, then need to keep any merges |
| 4107 | * between relevant commits to tie together topology. |
| 4108 | * For consistency with TREESAME and simplification |
| 4109 | * use "relevant" here rather than just INTERESTING, |
| 4110 | * to treat bottom commit(s) as part of the topology. |
| 4111 | */ |
| 4112 | for (n = 0, p = commit->parents; p; p = p->next) |
| 4113 | if (relevant_commit(p->item)) |
| 4114 | if (++n >= 2) |
| 4115 | return commit_show; |
| 4116 | return commit_ignore; |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4117 | } |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4118 | } |
| 4119 | return commit_show; |
| 4120 | } |
| 4121 | |
| Junio C Hamano | 0131c49 | 2015-01-14 22:49:24 | [diff] [blame] | 4122 | define_commit_slab(saved_parents, struct commit_list *); |
| 4123 | |
| 4124 | #define EMPTY_PARENT_LIST ((struct commit_list *)-1) |
| 4125 | |
| 4126 | /* |
| 4127 | * You may only call save_parents() once per commit (this is checked |
| 4128 | * for non-root commits). |
| 4129 | */ |
| 4130 | static void save_parents(struct rev_info *revs, struct commit *commit) |
| 4131 | { |
| 4132 | struct commit_list **pp; |
| 4133 | |
| 4134 | if (!revs->saved_parents_slab) { |
| 4135 | revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents)); |
| 4136 | init_saved_parents(revs->saved_parents_slab); |
| 4137 | } |
| 4138 | |
| 4139 | pp = saved_parents_at(revs->saved_parents_slab, commit); |
| 4140 | |
| 4141 | /* |
| 4142 | * When walking with reflogs, we may visit the same commit |
| 4143 | * several times: once for each appearance in the reflog. |
| 4144 | * |
| 4145 | * In this case, save_parents() will be called multiple times. |
| 4146 | * We want to keep only the first set of parents. We need to |
| 4147 | * store a sentinel value for an empty (i.e., NULL) parent |
| 4148 | * list to distinguish it from a not-yet-saved list, however. |
| 4149 | */ |
| 4150 | if (*pp) |
| 4151 | return; |
| 4152 | if (commit->parents) |
| 4153 | *pp = copy_commit_list(commit->parents); |
| 4154 | else |
| 4155 | *pp = EMPTY_PARENT_LIST; |
| 4156 | } |
| 4157 | |
| 4158 | static void free_saved_parents(struct rev_info *revs) |
| 4159 | { |
| 4160 | if (revs->saved_parents_slab) |
| 4161 | clear_saved_parents(revs->saved_parents_slab); |
| 4162 | } |
| 4163 | |
| 4164 | struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit) |
| 4165 | { |
| 4166 | struct commit_list *parents; |
| 4167 | |
| 4168 | if (!revs->saved_parents_slab) |
| 4169 | return commit->parents; |
| 4170 | |
| 4171 | parents = *saved_parents_at(revs->saved_parents_slab, commit); |
| 4172 | if (parents == EMPTY_PARENT_LIST) |
| 4173 | return NULL; |
| 4174 | return parents; |
| 4175 | } |
| 4176 | |
| Adam Simpkins | beb5af4 | 2009-08-19 02:34:33 | [diff] [blame] | 4177 | enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit) |
| 4178 | { |
| 4179 | enum commit_action action = get_commit_action(revs, commit); |
| 4180 | |
| 4181 | if (action == commit_show && |
| Adam Simpkins | beb5af4 | 2009-08-19 02:34:33 | [diff] [blame] | 4182 | revs->prune && revs->dense && want_ancestry(revs)) { |
| Thomas Rast | 53d00b3 | 2013-07-31 20:13:20 | [diff] [blame] | 4183 | /* |
| 4184 | * --full-diff on simplified parents is no good: it |
| 4185 | * will show spurious changes from the commits that |
| 4186 | * were elided. So we save the parents on the side |
| 4187 | * when --full-diff is in effect. |
| 4188 | */ |
| 4189 | if (revs->full_diff) |
| 4190 | save_parents(revs, commit); |
| Bo Yang | c7edcae | 2013-03-28 16:47:31 | [diff] [blame] | 4191 | if (rewrite_parents(revs, commit, rewrite_one) < 0) |
| Adam Simpkins | beb5af4 | 2009-08-19 02:34:33 | [diff] [blame] | 4192 | return commit_error; |
| 4193 | } |
| 4194 | return action; |
| 4195 | } |
| 4196 | |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 4197 | static void track_linear(struct rev_info *revs, struct commit *commit) |
| 4198 | { |
| 4199 | if (revs->track_first_time) { |
| 4200 | revs->linear = 1; |
| 4201 | revs->track_first_time = 0; |
| 4202 | } else { |
| 4203 | struct commit_list *p; |
| 4204 | for (p = revs->previous_parents; p; p = p->next) |
| 4205 | if (p->item == NULL || /* first commit */ |
| Jeff King | 4a7e27e | 2018-08-28 21:22:40 | [diff] [blame] | 4206 | oideq(&p->item->object.oid, &commit->object.oid)) |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 4207 | break; |
| 4208 | revs->linear = p != NULL; |
| 4209 | } |
| 4210 | if (revs->reverse) { |
| 4211 | if (revs->linear) |
| 4212 | commit->object.flags |= TRACK_LINEAR; |
| 4213 | } |
| 4214 | free_commit_list(revs->previous_parents); |
| 4215 | revs->previous_parents = copy_commit_list(commit->parents); |
| 4216 | } |
| 4217 | |
| Junio C Hamano | d5db6c9 | 2006-12-20 02:25:32 | [diff] [blame] | 4218 | static struct commit *get_revision_1(struct rev_info *revs) |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 4219 | { |
| Jeff King | 7c2f08a | 2017-07-07 09:07:58 | [diff] [blame] | 4220 | while (1) { |
| Jeff King | d08565b | 2017-07-07 09:14:07 | [diff] [blame] | 4221 | struct commit *commit; |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 4222 | |
| Jeff King | d08565b | 2017-07-07 09:14:07 | [diff] [blame] | 4223 | if (revs->reflog_info) |
| 4224 | commit = next_reflog_entry(revs->reflog_info); |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 4225 | else if (revs->topo_walk_info) |
| 4226 | commit = next_topo_commit(revs); |
| Jeff King | d08565b | 2017-07-07 09:14:07 | [diff] [blame] | 4227 | else |
| 4228 | commit = pop_commit(&revs->commits); |
| Linus Torvalds | 2a0925b | 2006-03-31 01:05:25 | [diff] [blame] | 4229 | |
| Jeff King | 7c2f08a | 2017-07-07 09:07:58 | [diff] [blame] | 4230 | if (!commit) |
| 4231 | return NULL; |
| 4232 | |
| Jeff King | d08565b | 2017-07-07 09:14:07 | [diff] [blame] | 4233 | if (revs->reflog_info) |
| Jeff King | ffa1eea | 2010-11-22 04:42:53 | [diff] [blame] | 4234 | commit->object.flags &= ~(ADDED | SEEN | SHOWN); |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 4235 | |
| Linus Torvalds | 2a0925b | 2006-03-31 01:05:25 | [diff] [blame] | 4236 | /* |
| 4237 | * If we haven't done the list limiting, we need to look at |
| Linus Torvalds | be7db6e | 2006-04-02 00:35:06 | [diff] [blame] | 4238 | * the parents here. We also need to do the date-based limiting |
| 4239 | * that we'd otherwise have done in limit_list(). |
| Linus Torvalds | 2a0925b | 2006-03-31 01:05:25 | [diff] [blame] | 4240 | */ |
| Linus Torvalds | be7db6e | 2006-04-02 00:35:06 | [diff] [blame] | 4241 | if (!revs->limited) { |
| Jan Harkes | 744f498 | 2006-10-31 01:37:49 | [diff] [blame] | 4242 | if (revs->max_age != -1 && |
| Jeff King | de23944 | 2017-07-07 09:16:21 | [diff] [blame] | 4243 | comparison_date(revs, commit) < revs->max_age) |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4244 | continue; |
| Jeff King | d08565b | 2017-07-07 09:14:07 | [diff] [blame] | 4245 | |
| 4246 | if (revs->reflog_info) |
| 4247 | try_to_simplify_commit(revs, commit); |
| Derrick Stolee | f0d9cc4 | 2018-11-01 13:46:20 | [diff] [blame] | 4248 | else if (revs->topo_walk_info) |
| 4249 | expand_topo_walk(revs, commit); |
| Derrick Stolee | 5284fc5 | 2018-11-01 13:46:21 | [diff] [blame] | 4250 | else if (process_parents(revs, commit, &revs->commits, NULL) < 0) { |
| Vicent Marti | 2db1a43 | 2014-03-28 10:00:43 | [diff] [blame] | 4251 | if (!revs->ignore_missing_links) |
| 4252 | die("Failed to traverse parents of commit %s", |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 4253 | oid_to_hex(&commit->object.oid)); |
| Vicent Marti | 2db1a43 | 2014-03-28 10:00:43 | [diff] [blame] | 4254 | } |
| Linus Torvalds | be7db6e | 2006-04-02 00:35:06 | [diff] [blame] | 4255 | } |
| Junio C Hamano | 1b65a5a | 2006-04-17 01:12:49 | [diff] [blame] | 4256 | |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4257 | switch (simplify_commit(revs, commit)) { |
| 4258 | case commit_ignore: |
| Linus Torvalds | 2a0925b | 2006-03-31 01:05:25 | [diff] [blame] | 4259 | continue; |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4260 | case commit_error: |
| Junio C Hamano | ed62089 | 2009-02-11 09:27:43 | [diff] [blame] | 4261 | die("Failed to simplify parents of commit %s", |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 4262 | oid_to_hex(&commit->object.oid)); |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4263 | default: |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 4264 | if (revs->track_linear) |
| 4265 | track_linear(revs, commit); |
| Linus Torvalds | 252a7c0 | 2007-11-04 20:12:05 | [diff] [blame] | 4266 | return commit; |
| Junio C Hamano | 384e99a | 2006-03-28 07:58:34 | [diff] [blame] | 4267 | } |
| Jeff King | 7c2f08a | 2017-07-07 09:07:58 | [diff] [blame] | 4268 | } |
| Linus Torvalds | 765ac8e | 2006-02-28 23:07:20 | [diff] [blame] | 4269 | } |
| Junio C Hamano | d5db6c9 | 2006-12-20 02:25:32 | [diff] [blame] | 4270 | |
| Michael Haggerty | be6754c | 2013-05-25 09:08:09 | [diff] [blame] | 4271 | /* |
| 4272 | * Return true for entries that have not yet been shown. (This is an |
| 4273 | * object_array_each_func_t.) |
| 4274 | */ |
| Jeff King | d3dcfa0 | 2023-02-24 06:39:15 | [diff] [blame] | 4275 | static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED) |
| Michael Haggerty | be6754c | 2013-05-25 09:08:09 | [diff] [blame] | 4276 | { |
| 4277 | return !(entry->item->flags & SHOWN); |
| 4278 | } |
| 4279 | |
| 4280 | /* |
| 4281 | * If array is on the verge of a realloc, garbage-collect any entries |
| 4282 | * that have already been shown to try to free up some space. |
| 4283 | */ |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4284 | static void gc_boundary(struct object_array *array) |
| 4285 | { |
| Michael Haggerty | be6754c | 2013-05-25 09:08:09 | [diff] [blame] | 4286 | if (array->nr == array->alloc) |
| 4287 | object_array_filter(array, entry_unshown, NULL); |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4288 | } |
| 4289 | |
| Adam Simpkins | 4603ec0 | 2008-05-24 23:02:05 | [diff] [blame] | 4290 | static void create_boundary_commit_list(struct rev_info *revs) |
| 4291 | { |
| 4292 | unsigned i; |
| 4293 | struct commit *c; |
| 4294 | struct object_array *array = &revs->boundary_commits; |
| 4295 | struct object_array_entry *objects = array->objects; |
| 4296 | |
| 4297 | /* |
| 4298 | * If revs->commits is non-NULL at this point, an error occurred in |
| 4299 | * get_revision_1(). Ignore the error and continue printing the |
| 4300 | * boundary commits anyway. (This is what the code has always |
| 4301 | * done.) |
| 4302 | */ |
| Ævar Arnfj��rð Bjarmason | bf20fe4 | 2022-04-13 20:01:34 | [diff] [blame] | 4303 | free_commit_list(revs->commits); |
| 4304 | revs->commits = NULL; |
| Adam Simpkins | 4603ec0 | 2008-05-24 23:02:05 | [diff] [blame] | 4305 | |
| 4306 | /* |
| 4307 | * Put all of the actual boundary commits from revs->boundary_commits |
| 4308 | * into revs->commits |
| 4309 | */ |
| 4310 | for (i = 0; i < array->nr; i++) { |
| 4311 | c = (struct commit *)(objects[i].item); |
| 4312 | if (!c) |
| 4313 | continue; |
| 4314 | if (!(c->object.flags & CHILD_SHOWN)) |
| 4315 | continue; |
| 4316 | if (c->object.flags & (SHOWN | BOUNDARY)) |
| 4317 | continue; |
| 4318 | c->object.flags |= BOUNDARY; |
| 4319 | commit_list_insert(c, &revs->commits); |
| 4320 | } |
| 4321 | |
| 4322 | /* |
| 4323 | * If revs->topo_order is set, sort the boundary commits |
| 4324 | * in topological order |
| 4325 | */ |
| Junio C Hamano | 08f704f | 2013-06-06 23:07:14 | [diff] [blame] | 4326 | sort_in_topological_order(&revs->commits, revs->sort_order); |
| Adam Simpkins | 4603ec0 | 2008-05-24 23:02:05 | [diff] [blame] | 4327 | } |
| 4328 | |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 4329 | static struct commit *get_revision_internal(struct rev_info *revs) |
| Junio C Hamano | d5db6c9 | 2006-12-20 02:25:32 | [diff] [blame] | 4330 | { |
| 4331 | struct commit *c = NULL; |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4332 | struct commit_list *l; |
| Junio C Hamano | d5db6c9 | 2006-12-20 02:25:32 | [diff] [blame] | 4333 | |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4334 | if (revs->boundary == 2) { |
| Adam Simpkins | 4603ec0 | 2008-05-24 23:02:05 | [diff] [blame] | 4335 | /* |
| 4336 | * All of the normal commits have already been returned, |
| 4337 | * and we are now returning boundary commits. |
| 4338 | * create_boundary_commit_list() has populated |
| 4339 | * revs->commits with the remaining commits to return. |
| 4340 | */ |
| 4341 | c = pop_commit(&revs->commits); |
| 4342 | if (c) |
| 4343 | c->object.flags |= SHOWN; |
| Johannes Schindelin | 9c5e66e | 2007-01-20 22:04:02 | [diff] [blame] | 4344 | return c; |
| 4345 | } |
| 4346 | |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4347 | /* |
| Jeff King | b72a190 | 2012-07-13 07:50:23 | [diff] [blame] | 4348 | * If our max_count counter has reached zero, then we are done. We |
| 4349 | * don't simply return NULL because we still might need to show |
| 4350 | * boundary commits. But we want to avoid calling get_revision_1, which |
| 4351 | * might do a considerable amount of work finding the next commit only |
| 4352 | * for us to throw it away. |
| 4353 | * |
| 4354 | * If it is non-zero, then either we don't have a max_count at all |
| 4355 | * (-1), or it is still counting, in which case we decrement. |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4356 | */ |
| Jeff King | b72a190 | 2012-07-13 07:50:23 | [diff] [blame] | 4357 | if (revs->max_count) { |
| 4358 | c = get_revision_1(revs); |
| 4359 | if (c) { |
| Felipe Contreras | 9e57ac5 | 2013-10-31 09:25:43 | [diff] [blame] | 4360 | while (revs->skip_count > 0) { |
| Jeff King | b72a190 | 2012-07-13 07:50:23 | [diff] [blame] | 4361 | revs->skip_count--; |
| 4362 | c = get_revision_1(revs); |
| 4363 | if (!c) |
| 4364 | break; |
| 4365 | } |
| Junio C Hamano | 8839ac9 | 2007-03-06 11:20:55 | [diff] [blame] | 4366 | } |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4367 | |
| Jeff King | b72a190 | 2012-07-13 07:50:23 | [diff] [blame] | 4368 | if (revs->max_count > 0) |
| 4369 | revs->max_count--; |
| Junio C Hamano | d5db6c9 | 2006-12-20 02:25:32 | [diff] [blame] | 4370 | } |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4371 | |
| Junio C Hamano | c33d859 | 2007-03-06 02:23:57 | [diff] [blame] | 4372 | if (c) |
| 4373 | c->object.flags |= SHOWN; |
| 4374 | |
| Felipe Contreras | 9e57ac5 | 2013-10-31 09:25:43 | [diff] [blame] | 4375 | if (!revs->boundary) |
| Junio C Hamano | d5db6c9 | 2006-12-20 02:25:32 | [diff] [blame] | 4376 | return c; |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4377 | |
| 4378 | if (!c) { |
| 4379 | /* |
| 4380 | * get_revision_1() runs out the commits, and |
| 4381 | * we are done computing the boundaries. |
| 4382 | * switch to boundary commits output mode. |
| 4383 | */ |
| 4384 | revs->boundary = 2; |
| Adam Simpkins | 4603ec0 | 2008-05-24 23:02:05 | [diff] [blame] | 4385 | |
| 4386 | /* |
| 4387 | * Update revs->commits to contain the list of |
| 4388 | * boundary commits. |
| 4389 | */ |
| 4390 | create_boundary_commit_list(revs); |
| 4391 | |
| Adam Simpkins | 3c68d67 | 2008-05-24 23:02:04 | [diff] [blame] | 4392 | return get_revision_internal(revs); |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | /* |
| 4396 | * boundary commits are the commits that are parents of the |
| 4397 | * ones we got from get_revision_1() but they themselves are |
| 4398 | * not returned from get_revision_1(). Before returning |
| 4399 | * 'c', we need to mark its parents that they could be boundaries. |
| 4400 | */ |
| 4401 | |
| 4402 | for (l = c->parents; l; l = l->next) { |
| 4403 | struct object *p; |
| 4404 | p = &(l->item->object); |
| Junio C Hamano | c33d859 | 2007-03-06 02:23:57 | [diff] [blame] | 4405 | if (p->flags & (CHILD_SHOWN | SHOWN)) |
| Junio C Hamano | 86ab490 | 2007-03-05 21:10:06 | [diff] [blame] | 4406 | continue; |
| 4407 | p->flags |= CHILD_SHOWN; |
| 4408 | gc_boundary(&revs->boundary_commits); |
| 4409 | add_object_array(p, NULL, &revs->boundary_commits); |
| 4410 | } |
| 4411 | |
| 4412 | return c; |
| Junio C Hamano | d5db6c9 | 2006-12-20 02:25:32 | [diff] [blame] | 4413 | } |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 4414 | |
| 4415 | struct commit *get_revision(struct rev_info *revs) |
| 4416 | { |
| Thomas Rast | 498bcd3 | 2008-08-29 19:18:38 | [diff] [blame] | 4417 | struct commit *c; |
| 4418 | struct commit_list *reversed; |
| 4419 | |
| 4420 | if (revs->reverse) { |
| 4421 | reversed = NULL; |
| Felipe Contreras | 9e57ac5 | 2013-10-31 09:25:43 | [diff] [blame] | 4422 | while ((c = get_revision_internal(revs))) |
| Thomas Rast | 498bcd3 | 2008-08-29 19:18:38 | [diff] [blame] | 4423 | commit_list_insert(c, &reversed); |
| Thomas Rast | 498bcd3 | 2008-08-29 19:18:38 | [diff] [blame] | 4424 | revs->commits = reversed; |
| 4425 | revs->reverse = 0; |
| 4426 | revs->reverse_output_stage = 1; |
| 4427 | } |
| 4428 | |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 4429 | if (revs->reverse_output_stage) { |
| 4430 | c = pop_commit(&revs->commits); |
| 4431 | if (revs->track_linear) |
| 4432 | revs->linear = !!(c && c->object.flags & TRACK_LINEAR); |
| 4433 | return c; |
| 4434 | } |
| Thomas Rast | 498bcd3 | 2008-08-29 19:18:38 | [diff] [blame] | 4435 | |
| 4436 | c = get_revision_internal(revs); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 4437 | if (c && revs->graph) |
| 4438 | graph_update(revs->graph, c); |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 4439 | if (!c) { |
| Thomas Rast | 53d00b3 | 2013-07-31 20:13:20 | [diff] [blame] | 4440 | free_saved_parents(revs); |
| Ævar Arnfjörð Bjarmason | bf20fe4 | 2022-04-13 20:01:34 | [diff] [blame] | 4441 | free_commit_list(revs->previous_parents); |
| 4442 | revs->previous_parents = NULL; |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 4443 | } |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 4444 | return c; |
| 4445 | } |
| Michael J Gruber | 1df2d65 | 2011-03-07 12:31:39 | [diff] [blame] | 4446 | |
| Denton Liu | 4982516 | 2019-11-20 00:51:13 | [diff] [blame] | 4447 | const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit) |
| Michael J Gruber | 1df2d65 | 2011-03-07 12:31:39 | [diff] [blame] | 4448 | { |
| 4449 | if (commit->object.flags & BOUNDARY) |
| 4450 | return "-"; |
| 4451 | else if (commit->object.flags & UNINTERESTING) |
| 4452 | return "^"; |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 4453 | else if (commit->object.flags & PATCHSAME) |
| 4454 | return "="; |
| Michael J Gruber | 1df2d65 | 2011-03-07 12:31:39 | [diff] [blame] | 4455 | else if (!revs || revs->left_right) { |
| 4456 | if (commit->object.flags & SYMMETRIC_LEFT) |
| 4457 | return "<"; |
| 4458 | else |
| 4459 | return ">"; |
| 4460 | } else if (revs->graph) |
| 4461 | return "*"; |
| Michael J Gruber | adbbb31 | 2011-03-07 12:31:40 | [diff] [blame] | 4462 | else if (revs->cherry_mark) |
| 4463 | return "+"; |
| Michael J Gruber | 1df2d65 | 2011-03-07 12:31:39 | [diff] [blame] | 4464 | return ""; |
| 4465 | } |
| Michael J Gruber | b1b4755 | 2011-03-10 14:45:03 | [diff] [blame] | 4466 | |
| 4467 | void put_revision_mark(const struct rev_info *revs, const struct commit *commit) |
| 4468 | { |
| Denton Liu | 4982516 | 2019-11-20 00:51:13 | [diff] [blame] | 4469 | const char *mark = get_revision_mark(revs, commit); |
| Michael J Gruber | b1b4755 | 2011-03-10 14:45:03 | [diff] [blame] | 4470 | if (!strlen(mark)) |
| 4471 | return; |
| 4472 | fputs(mark, stdout); |
| 4473 | putchar(' '); |
| 4474 | } |