Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "refs.h" |
| 3 | #include "tag.h" |
| 4 | #include "commit.h" |
| 5 | #include "blob.h" |
| 6 | #include "diff.h" |
| 7 | #include "revision.h" |
| 8 | #include "reachable.h" |
| 9 | #include "cache-tree.h" |
Nguyễn Thái Ngọc Duy | dc34719 | 2011-11-05 19:00:08 +0700 | [diff] [blame] | 10 | #include "progress.h" |
Jeff King | 5f78a43 | 2014-10-15 18:37:28 -0400 | [diff] [blame] | 11 | #include "list-objects.h" |
Jonathan Tan | 7709f46 | 2017-08-18 15:20:38 -0700 | [diff] [blame] | 12 | #include "packfile.h" |
Nguyễn Thái Ngọc Duy | d0c39a4 | 2017-08-23 19:36:59 +0700 | [diff] [blame] | 13 | #include "worktree.h" |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 14 | #include "object-store.h" |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 15 | |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 16 | struct connectivity_progress { |
| 17 | struct progress *progress; |
| 18 | unsigned long count; |
| 19 | }; |
| 20 | |
| 21 | static void update_progress(struct connectivity_progress *cp) |
| 22 | { |
| 23 | cp->count++; |
| 24 | if ((cp->count & 1023) == 0) |
| 25 | display_progress(cp->progress, cp->count); |
| 26 | } |
| 27 | |
Michael Haggerty | 635170f | 2015-05-25 18:39:00 +0000 | [diff] [blame] | 28 | static int add_one_ref(const char *path, const struct object_id *oid, |
| 29 | int flag, void *cb_data) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 30 | { |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 31 | struct rev_info *revs = (struct rev_info *)cb_data; |
Johannes Schindelin | 14886b4 | 2015-09-28 16:01:25 +0200 | [diff] [blame] | 32 | struct object *object; |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 33 | |
Johannes Schindelin | 14886b4 | 2015-09-28 16:01:25 +0200 | [diff] [blame] | 34 | if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) { |
| 35 | warning("symbolic ref is dangling: %s", path); |
| 36 | return 0; |
| 37 | } |
| 38 | |
brian m. carlson | c251c83 | 2017-05-06 22:10:38 +0000 | [diff] [blame] | 39 | object = parse_object_or_die(oid, path); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 40 | add_pending_object(revs, object, ""); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
Jeff King | 5f78a43 | 2014-10-15 18:37:28 -0400 | [diff] [blame] | 45 | /* |
| 46 | * The traversal will have already marked us as SEEN, so we |
| 47 | * only need to handle any progress reporting here. |
| 48 | */ |
Jeff King | de1e67d | 2016-02-11 17:28:36 -0500 | [diff] [blame] | 49 | static void mark_object(struct object *obj, const char *name, void *data) |
Jeff King | 5f78a43 | 2014-10-15 18:37:28 -0400 | [diff] [blame] | 50 | { |
| 51 | update_progress(data); |
| 52 | } |
| 53 | |
| 54 | static void mark_commit(struct commit *c, void *data) |
| 55 | { |
Jeff King | de1e67d | 2016-02-11 17:28:36 -0500 | [diff] [blame] | 56 | mark_object(&c->object, NULL, data); |
Jeff King | 5f78a43 | 2014-10-15 18:37:28 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 59 | struct recent_data { |
| 60 | struct rev_info *revs; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 61 | timestamp_t timestamp; |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 62 | }; |
| 63 | |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 64 | static void add_recent_object(const struct object_id *oid, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 65 | timestamp_t mtime, |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 66 | struct recent_data *data) |
| 67 | { |
| 68 | struct object *obj; |
| 69 | enum object_type type; |
| 70 | |
| 71 | if (mtime <= data->timestamp) |
| 72 | return; |
| 73 | |
| 74 | /* |
| 75 | * We do not want to call parse_object here, because |
| 76 | * inflating blobs and trees could be very expensive. |
| 77 | * However, we do need to know the correct type for |
| 78 | * later processing, and the revision machinery expects |
| 79 | * commits and tags to have been parsed. |
| 80 | */ |
Stefan Beller | 0df8e96 | 2018-04-25 11:20:59 -0700 | [diff] [blame] | 81 | type = oid_object_info(the_repository, oid, NULL); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 82 | if (type < 0) |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 83 | die("unable to get object info for %s", oid_to_hex(oid)); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 84 | |
| 85 | switch (type) { |
| 86 | case OBJ_TAG: |
| 87 | case OBJ_COMMIT: |
brian m. carlson | c251c83 | 2017-05-06 22:10:38 +0000 | [diff] [blame] | 88 | obj = parse_object_or_die(oid, NULL); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 89 | break; |
| 90 | case OBJ_TREE: |
Stefan Beller | f86bcc7 | 2018-06-28 18:21:56 -0700 | [diff] [blame] | 91 | obj = (struct object *)lookup_tree(the_repository, oid); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 92 | break; |
| 93 | case OBJ_BLOB: |
Stefan Beller | da14a7f | 2018-06-28 18:21:55 -0700 | [diff] [blame] | 94 | obj = (struct object *)lookup_blob(the_repository, oid); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 95 | break; |
| 96 | default: |
| 97 | die("unknown object type for %s: %s", |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 98 | oid_to_hex(oid), type_name(type)); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (!obj) |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 102 | die("unable to lookup %s", oid_to_hex(oid)); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 103 | |
| 104 | add_pending_object(data->revs, obj, ""); |
| 105 | } |
| 106 | |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 107 | static int add_recent_loose(const struct object_id *oid, |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 108 | const char *path, void *data) |
| 109 | { |
| 110 | struct stat st; |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 111 | struct object *obj = lookup_object(the_repository, oid->hash); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 112 | |
| 113 | if (obj && obj->flags & SEEN) |
| 114 | return 0; |
| 115 | |
| 116 | if (stat(path, &st) < 0) { |
| 117 | /* |
| 118 | * It's OK if an object went away during our iteration; this |
| 119 | * could be due to a simultaneous repack. But anything else |
| 120 | * we should abort, since we might then fail to mark objects |
| 121 | * which should not be pruned. |
| 122 | */ |
| 123 | if (errno == ENOENT) |
| 124 | return 0; |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 125 | return error_errno("unable to stat %s", oid_to_hex(oid)); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 126 | } |
| 127 | |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 128 | add_recent_object(oid, st.st_mtime, data); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 132 | static int add_recent_packed(const struct object_id *oid, |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 133 | struct packed_git *p, uint32_t pos, |
| 134 | void *data) |
| 135 | { |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 136 | struct object *obj = lookup_object(the_repository, oid->hash); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 137 | |
| 138 | if (obj && obj->flags & SEEN) |
| 139 | return 0; |
brian m. carlson | 76c1d9a | 2017-02-21 23:47:35 +0000 | [diff] [blame] | 140 | add_recent_object(oid, p->mtime, data); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Jeff King | abcb865 | 2014-10-15 18:42:09 -0400 | [diff] [blame] | 144 | int add_unseen_recent_objects_to_traversal(struct rev_info *revs, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 145 | timestamp_t timestamp) |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 146 | { |
| 147 | struct recent_data data; |
| 148 | int r; |
| 149 | |
| 150 | data.revs = revs; |
| 151 | data.timestamp = timestamp; |
| 152 | |
Jeff King | 1385bb7 | 2015-03-27 07:32:41 -0400 | [diff] [blame] | 153 | r = for_each_loose_object(add_recent_loose, &data, |
| 154 | FOR_EACH_OBJECT_LOCAL_ONLY); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 155 | if (r) |
| 156 | return r; |
Jeff King | 1385bb7 | 2015-03-27 07:32:41 -0400 | [diff] [blame] | 157 | return for_each_packed_object(add_recent_packed, &data, |
| 158 | FOR_EACH_OBJECT_LOCAL_ONLY); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Nguyễn Thái Ngọc Duy | dc34719 | 2011-11-05 19:00:08 +0700 | [diff] [blame] | 161 | void mark_reachable_objects(struct rev_info *revs, int mark_reflog, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 162 | timestamp_t mark_recent, struct progress *progress) |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 163 | { |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 164 | struct connectivity_progress cp; |
| 165 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 166 | /* |
| 167 | * Set up revision parsing, and mark us as being interested |
| 168 | * in all object types, not just commits. |
| 169 | */ |
| 170 | revs->tag_objects = 1; |
| 171 | revs->blob_objects = 1; |
| 172 | revs->tree_objects = 1; |
| 173 | |
| 174 | /* Add all refs from the index file */ |
Jeff King | 1be111d | 2014-10-16 20:44:30 -0400 | [diff] [blame] | 175 | add_index_objects_to_pending(revs, 0); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 176 | |
| 177 | /* Add all external refs */ |
Michael Haggerty | 635170f | 2015-05-25 18:39:00 +0000 | [diff] [blame] | 178 | for_each_ref(add_one_ref, revs); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 179 | |
Max Kirillov | c40fdd0 | 2014-09-03 19:14:10 +0300 | [diff] [blame] | 180 | /* detached HEAD is not included in the list above */ |
Michael Haggerty | 635170f | 2015-05-25 18:39:00 +0000 | [diff] [blame] | 181 | head_ref(add_one_ref, revs); |
Nguyễn Thái Ngọc Duy | d0c39a4 | 2017-08-23 19:36:59 +0700 | [diff] [blame] | 182 | other_head_refs(add_one_ref, revs); |
Max Kirillov | c40fdd0 | 2014-09-03 19:14:10 +0300 | [diff] [blame] | 183 | |
Nicolas Pitre | eb8381c | 2007-02-03 13:25:43 -0500 | [diff] [blame] | 184 | /* Add all reflog info */ |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 185 | if (mark_reflog) |
Jeff King | 718ccc9 | 2014-10-15 18:38:31 -0400 | [diff] [blame] | 186 | add_reflogs_to_pending(revs, 0); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 187 | |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 188 | cp.progress = progress; |
| 189 | cp.count = 0; |
| 190 | |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 191 | /* |
| 192 | * Set up the revision walk - this will move all commits |
| 193 | * from the pending list to the commit walking list. |
| 194 | */ |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 195 | if (prepare_revision_walk(revs)) |
| 196 | die("revision walk setup failed"); |
Jeff King | 5f78a43 | 2014-10-15 18:37:28 -0400 | [diff] [blame] | 197 | traverse_commit_list(revs, mark_commit, mark_object, &cp); |
Jeff King | d3038d2 | 2014-10-15 18:41:35 -0400 | [diff] [blame] | 198 | |
| 199 | if (mark_recent) { |
| 200 | revs->ignore_missing_links = 1; |
| 201 | if (add_unseen_recent_objects_to_traversal(revs, mark_recent)) |
| 202 | die("unable to mark recent objects"); |
| 203 | if (prepare_revision_walk(revs)) |
| 204 | die("revision walk setup failed"); |
| 205 | traverse_commit_list(revs, mark_commit, mark_object, &cp); |
| 206 | } |
| 207 | |
Jeff King | 0b26abc | 2011-11-08 00:37:00 -0500 | [diff] [blame] | 208 | display_progress(cp.progress, cp.count); |
Junio C Hamano | 9442147 | 2007-01-06 02:16:17 -0800 | [diff] [blame] | 209 | } |