blob: 6e9b810d2a5e03dc613fe1e58938086f1ed9b684 [file] [log] [blame]
Junio C Hamano94421472007-01-06 02:16:17 -08001#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 Duydc347192011-11-05 19:00:08 +070010#include "progress.h"
Jeff King5f78a432014-10-15 18:37:28 -040011#include "list-objects.h"
Jonathan Tan7709f462017-08-18 15:20:38 -070012#include "packfile.h"
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +070013#include "worktree.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +010014#include "object-store.h"
Junio C Hamano94421472007-01-06 02:16:17 -080015
Jeff King0b26abc2011-11-08 00:37:00 -050016struct connectivity_progress {
17 struct progress *progress;
18 unsigned long count;
19};
20
21static 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 Haggerty635170f2015-05-25 18:39:00 +000028static int add_one_ref(const char *path, const struct object_id *oid,
29 int flag, void *cb_data)
Junio C Hamano94421472007-01-06 02:16:17 -080030{
Junio C Hamano94421472007-01-06 02:16:17 -080031 struct rev_info *revs = (struct rev_info *)cb_data;
Johannes Schindelin14886b42015-09-28 16:01:25 +020032 struct object *object;
Junio C Hamano94421472007-01-06 02:16:17 -080033
Johannes Schindelin14886b42015-09-28 16:01:25 +020034 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
35 warning("symbolic ref is dangling: %s", path);
36 return 0;
37 }
38
brian m. carlsonc251c832017-05-06 22:10:38 +000039 object = parse_object_or_die(oid, path);
Junio C Hamano94421472007-01-06 02:16:17 -080040 add_pending_object(revs, object, "");
41
42 return 0;
43}
44
Jeff King5f78a432014-10-15 18:37:28 -040045/*
46 * The traversal will have already marked us as SEEN, so we
47 * only need to handle any progress reporting here.
48 */
Jeff Kingde1e67d2016-02-11 17:28:36 -050049static void mark_object(struct object *obj, const char *name, void *data)
Jeff King5f78a432014-10-15 18:37:28 -040050{
51 update_progress(data);
52}
53
54static void mark_commit(struct commit *c, void *data)
55{
Jeff Kingde1e67d2016-02-11 17:28:36 -050056 mark_object(&c->object, NULL, data);
Jeff King5f78a432014-10-15 18:37:28 -040057}
58
Jeff Kingd3038d22014-10-15 18:41:35 -040059struct recent_data {
60 struct rev_info *revs;
Johannes Schindelindddbad72017-04-26 21:29:31 +020061 timestamp_t timestamp;
Jeff Kingd3038d22014-10-15 18:41:35 -040062};
63
brian m. carlson76c1d9a2017-02-21 23:47:35 +000064static void add_recent_object(const struct object_id *oid,
Johannes Schindelindddbad72017-04-26 21:29:31 +020065 timestamp_t mtime,
Jeff Kingd3038d22014-10-15 18:41:35 -040066 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 Beller0df8e962018-04-25 11:20:59 -070081 type = oid_object_info(the_repository, oid, NULL);
Jeff Kingd3038d22014-10-15 18:41:35 -040082 if (type < 0)
brian m. carlson76c1d9a2017-02-21 23:47:35 +000083 die("unable to get object info for %s", oid_to_hex(oid));
Jeff Kingd3038d22014-10-15 18:41:35 -040084
85 switch (type) {
86 case OBJ_TAG:
87 case OBJ_COMMIT:
brian m. carlsonc251c832017-05-06 22:10:38 +000088 obj = parse_object_or_die(oid, NULL);
Jeff Kingd3038d22014-10-15 18:41:35 -040089 break;
90 case OBJ_TREE:
Stefan Bellerf86bcc72018-06-28 18:21:56 -070091 obj = (struct object *)lookup_tree(the_repository, oid);
Jeff Kingd3038d22014-10-15 18:41:35 -040092 break;
93 case OBJ_BLOB:
Stefan Bellerda14a7f2018-06-28 18:21:55 -070094 obj = (struct object *)lookup_blob(the_repository, oid);
Jeff Kingd3038d22014-10-15 18:41:35 -040095 break;
96 default:
97 die("unknown object type for %s: %s",
Brandon Williamsdebca9d2018-02-14 10:59:24 -080098 oid_to_hex(oid), type_name(type));
Jeff Kingd3038d22014-10-15 18:41:35 -040099 }
100
101 if (!obj)
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000102 die("unable to lookup %s", oid_to_hex(oid));
Jeff Kingd3038d22014-10-15 18:41:35 -0400103
104 add_pending_object(data->revs, obj, "");
105}
106
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000107static int add_recent_loose(const struct object_id *oid,
Jeff Kingd3038d22014-10-15 18:41:35 -0400108 const char *path, void *data)
109{
110 struct stat st;
Stefan Beller5abddd12018-06-28 18:21:52 -0700111 struct object *obj = lookup_object(the_repository, oid->hash);
Jeff Kingd3038d22014-10-15 18:41:35 -0400112
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. carlson76c1d9a2017-02-21 23:47:35 +0000125 return error_errno("unable to stat %s", oid_to_hex(oid));
Jeff Kingd3038d22014-10-15 18:41:35 -0400126 }
127
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000128 add_recent_object(oid, st.st_mtime, data);
Jeff Kingd3038d22014-10-15 18:41:35 -0400129 return 0;
130}
131
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000132static int add_recent_packed(const struct object_id *oid,
Jeff Kingd3038d22014-10-15 18:41:35 -0400133 struct packed_git *p, uint32_t pos,
134 void *data)
135{
Stefan Beller5abddd12018-06-28 18:21:52 -0700136 struct object *obj = lookup_object(the_repository, oid->hash);
Jeff Kingd3038d22014-10-15 18:41:35 -0400137
138 if (obj && obj->flags & SEEN)
139 return 0;
brian m. carlson76c1d9a2017-02-21 23:47:35 +0000140 add_recent_object(oid, p->mtime, data);
Jeff Kingd3038d22014-10-15 18:41:35 -0400141 return 0;
142}
143
Jeff Kingabcb8652014-10-15 18:42:09 -0400144int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200145 timestamp_t timestamp)
Jeff Kingd3038d22014-10-15 18:41:35 -0400146{
147 struct recent_data data;
148 int r;
149
150 data.revs = revs;
151 data.timestamp = timestamp;
152
Jeff King1385bb72015-03-27 07:32:41 -0400153 r = for_each_loose_object(add_recent_loose, &data,
154 FOR_EACH_OBJECT_LOCAL_ONLY);
Jeff Kingd3038d22014-10-15 18:41:35 -0400155 if (r)
156 return r;
Jeff King1385bb72015-03-27 07:32:41 -0400157 return for_each_packed_object(add_recent_packed, &data,
158 FOR_EACH_OBJECT_LOCAL_ONLY);
Jeff Kingd3038d22014-10-15 18:41:35 -0400159}
160
Nguyễn Thái Ngọc Duydc347192011-11-05 19:00:08 +0700161void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200162 timestamp_t mark_recent, struct progress *progress)
Junio C Hamano94421472007-01-06 02:16:17 -0800163{
Jeff King0b26abc2011-11-08 00:37:00 -0500164 struct connectivity_progress cp;
165
Junio C Hamano94421472007-01-06 02:16:17 -0800166 /*
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 King1be111d2014-10-16 20:44:30 -0400175 add_index_objects_to_pending(revs, 0);
Junio C Hamano94421472007-01-06 02:16:17 -0800176
177 /* Add all external refs */
Michael Haggerty635170f2015-05-25 18:39:00 +0000178 for_each_ref(add_one_ref, revs);
Junio C Hamano94421472007-01-06 02:16:17 -0800179
Max Kirillovc40fdd02014-09-03 19:14:10 +0300180 /* detached HEAD is not included in the list above */
Michael Haggerty635170f2015-05-25 18:39:00 +0000181 head_ref(add_one_ref, revs);
Nguyễn Thái Ngọc Duyd0c39a42017-08-23 19:36:59 +0700182 other_head_refs(add_one_ref, revs);
Max Kirillovc40fdd02014-09-03 19:14:10 +0300183
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500184 /* Add all reflog info */
Junio C Hamano94421472007-01-06 02:16:17 -0800185 if (mark_reflog)
Jeff King718ccc92014-10-15 18:38:31 -0400186 add_reflogs_to_pending(revs, 0);
Junio C Hamano94421472007-01-06 02:16:17 -0800187
Jeff King0b26abc2011-11-08 00:37:00 -0500188 cp.progress = progress;
189 cp.count = 0;
190
Junio C Hamano94421472007-01-06 02:16:17 -0800191 /*
192 * Set up the revision walk - this will move all commits
193 * from the pending list to the commit walking list.
194 */
Martin Koegler3d51e1b2008-02-18 08:31:56 +0100195 if (prepare_revision_walk(revs))
196 die("revision walk setup failed");
Jeff King5f78a432014-10-15 18:37:28 -0400197 traverse_commit_list(revs, mark_commit, mark_object, &cp);
Jeff Kingd3038d22014-10-15 18:41:35 -0400198
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 King0b26abc2011-11-08 00:37:00 -0500208 display_progress(cp.progress, cp.count);
Junio C Hamano94421472007-01-06 02:16:17 -0800209}