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"
10#include "progress.h"
11#include "list-objects.h"
12
13struct connectivity_progress {
14 struct progress *progress;
15 unsigned long count;
16};
17
18static void update_progress(struct connectivity_progress *cp)
19{
20 cp->count++;
21 if ((cp->count & 1023) == 0)
22 display_progress(cp->progress, cp->count);
23}
24
25static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
26{
27 struct object *object = parse_object_or_die(sha1, path);
28 struct rev_info *revs = (struct rev_info *)cb_data;
29
30 add_pending_object(revs, object, "");
31
32 return 0;
33}
34
35/*
36 * The traversal will have already marked us as SEEN, so we
37 * only need to handle any progress reporting here.
38 */
39static void mark_object(struct object *obj, const char *name, void *data)
40{
41 update_progress(data);
42}
43
44static void mark_commit(struct commit *c, void *data)
45{
46 mark_object(&c->object, NULL, data);
47}
48
49struct recent_data {
50 struct rev_info *revs;
51 unsigned long timestamp;
52};
53
54static void add_recent_object(const unsigned char *sha1,
55 unsigned long mtime,
56 struct recent_data *data)
57{
58 struct object *obj;
59 enum object_type type;
60
61 if (mtime <= data->timestamp)
62 return;
63
64 /*
65 * We do not want to call parse_object here, because
66 * inflating blobs and trees could be very expensive.
67 * However, we do need to know the correct type for
68 * later processing, and the revision machinery expects
69 * commits and tags to have been parsed.
70 */
71 type = sha1_object_info(sha1, NULL);
72 if (type < 0)
73 die("unable to get object info for %s", sha1_to_hex(sha1));
74
75 switch (type) {
76 case OBJ_TAG:
77 case OBJ_COMMIT:
78 obj = parse_object_or_die(sha1, NULL);
79 break;
80 case OBJ_TREE:
81 obj = (struct object *)lookup_tree(sha1);
82 break;
83 case OBJ_BLOB:
84 obj = (struct object *)lookup_blob(sha1);
85 break;
86 default:
87 die("unknown object type for %s: %s",
88 sha1_to_hex(sha1), typename(type));
89 }
90
91 if (!obj)
92 die("unable to lookup %s", sha1_to_hex(sha1));
93
94 add_pending_object(data->revs, obj, "");
95}
96
97static int add_recent_loose(const unsigned char *sha1,
98 const char *path, void *data)
99{
100 struct stat st;
101 struct object *obj = lookup_object(sha1);
102
103 if (obj && obj->flags & SEEN)
104 return 0;
105
106 if (stat(path, &st) < 0) {
107 /*
108 * It's OK if an object went away during our iteration; this
109 * could be due to a simultaneous repack. But anything else
110 * we should abort, since we might then fail to mark objects
111 * which should not be pruned.
112 */
113 if (errno == ENOENT)
114 return 0;
115 return error("unable to stat %s: %s",
116 sha1_to_hex(sha1), strerror(errno));
117 }
118
119 add_recent_object(sha1, st.st_mtime, data);
120 return 0;
121}
122
123static int add_recent_packed(const unsigned char *sha1,
124 struct packed_git *p, uint32_t pos,
125 void *data)
126{
127 struct object *obj = lookup_object(sha1);
128
129 if (obj && obj->flags & SEEN)
130 return 0;
131 add_recent_object(sha1, p->mtime, data);
132 return 0;
133}
134
135int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
136 unsigned long timestamp)
137{
138 struct recent_data data;
139 int r;
140
141 data.revs = revs;
142 data.timestamp = timestamp;
143
144 r = for_each_loose_object(add_recent_loose, &data,
145 FOR_EACH_OBJECT_LOCAL_ONLY);
146 if (r)
147 return r;
148 return for_each_packed_object(add_recent_packed, &data,
149 FOR_EACH_OBJECT_LOCAL_ONLY);
150}
151
152void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
153 unsigned long mark_recent,
154 struct progress *progress)
155{
156 struct connectivity_progress cp;
157
158 /*
159 * Set up revision parsing, and mark us as being interested
160 * in all object types, not just commits.
161 */
162 revs->tag_objects = 1;
163 revs->blob_objects = 1;
164 revs->tree_objects = 1;
165
166 /* Add all refs from the index file */
167 add_index_objects_to_pending(revs, 0);
168
169 /* Add all external refs */
170 for_each_ref(add_one_ref, revs);
171
172 /* detached HEAD is not included in the list above */
173 head_ref(add_one_ref, revs);
174
175 /* Add all reflog info */
176 if (mark_reflog)
177 add_reflogs_to_pending(revs, 0);
178
179 cp.progress = progress;
180 cp.count = 0;
181
182 /*
183 * Set up the revision walk - this will move all commits
184 * from the pending list to the commit walking list.
185 */
186 if (prepare_revision_walk(revs))
187 die("revision walk setup failed");
188 traverse_commit_list(revs, mark_commit, mark_object, &cp);
189
190 if (mark_recent) {
191 revs->ignore_missing_links = 1;
192 if (add_unseen_recent_objects_to_traversal(revs, mark_recent))
193 die("unable to mark recent objects");
194 if (prepare_revision_walk(revs))
195 die("revision walk setup failed");
196 traverse_commit_list(revs, mark_commit, mark_object, &cp);
197 }
198
199 display_progress(cp.progress, cp.count);
200}