Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "skipping.h"
5 #include "../commit.h"
6 #include "../fetch-negotiator.h"
7 #include "../hex.h"
8 #include "../prio-queue.h"
9 #include "../refs.h"
10 #include "../repository.h"
11 #include "../tag.h"
12
13 /* Remember to update object flag allocation in object.h */
14 /*
15 * Both us and the server know that both parties have this object.
16 */
17 #define COMMON (1U << 2)
18 /*
19 * The server has told us that it has this object. We still need to tell the
20 * server that we have this object (or one of its descendants), but since we are
21 * going to do that, we do not need to tell the server about its ancestors.
22 */
23 #define ADVERTISED (1U << 3)
24 /*
25 * This commit has entered the priority queue.
26 */
27 #define SEEN (1U << 4)
28 /*
29 * This commit has left the priority queue.
30 */
31 #define POPPED (1U << 5)
32
33 static int marked;
34
35 /*
36 * An entry in the priority queue.
37 */
38 struct entry {
39 struct commit *commit;
40
41 /*
42 * Used only if commit is not COMMON.
43 */
44 uint16_t original_ttl;
45 uint16_t ttl;
46 };
47
48 struct data {
49 struct prio_queue rev_list;
50
51 /*
52 * The number of non-COMMON commits in rev_list.
53 */
54 int non_common_revs;
55 };
56
57 static int compare(const void *a_, const void *b_, void *data UNUSED)
58 {
59 const struct entry *a = a_;
60 const struct entry *b = b_;
61 return compare_commits_by_commit_date(a->commit, b->commit, NULL);
62 }
63
64 static struct entry *rev_list_push(struct data *data, struct commit *commit, int mark)
65 {
66 struct entry *entry;
67 commit->object.flags |= mark | SEEN;
68
69 CALLOC_ARRAY(entry, 1);
70 entry->commit = commit;
71 prio_queue_put(&data->rev_list, entry);
72
73 if (!(mark & COMMON))
74 data->non_common_revs++;
75 return entry;
76 }
77
78 static int clear_marks(const struct reference *ref, void *cb_data UNUSED)
79 {
80 struct object *o = deref_tag(the_repository, parse_object(the_repository, ref->oid),
81 ref->name, 0);
82
83 if (o && o->type == OBJ_COMMIT)
84 clear_commit_marks((struct commit *)o,
85 COMMON | ADVERTISED | SEEN | POPPED);
86 return 0;
87 }
88
89 /*
90 * Mark this SEEN commit and all its parsed SEEN ancestors as COMMON.
91 */
92 static void mark_common(struct data *data, struct commit *seen_commit)
93 {
94 struct commit_stack stack = COMMIT_STACK_INIT;
95 struct commit *c;
96
97 if (seen_commit->object.flags & COMMON)
98 return;
99
100 commit_stack_push(&stack, seen_commit);
101 seen_commit->object.flags |= COMMON;
102 while ((c = commit_stack_pop(&stack))) {
103 struct commit_list *p;
104
105 if (!(c->object.flags & POPPED))
106 data->non_common_revs--;
107
108 if (!c->object.parsed)
109 continue;
110 for (p = c->parents; p; p = p->next) {
111 if (!(p->item->object.flags & SEEN) ||
112 (p->item->object.flags & COMMON))
113 continue;
114
115 p->item->object.flags |= COMMON;
116 commit_stack_push(&stack, p->item);
117 }
118 }
119
120 commit_stack_clear(&stack);
121 }
122
123 /*
124 * Ensure that the priority queue has an entry for to_push, and ensure that the
125 * entry has the correct flags and ttl.
126 *
127 * This function returns 1 if an entry was found or created, and 0 otherwise
128 * (because the entry for this commit had already been popped).
129 */
130 static int push_parent(struct data *data, struct entry *entry,
131 struct commit *to_push)
132 {
133 struct entry *parent_entry;
134
135 if (to_push->object.flags & SEEN) {
136 if (to_push->object.flags & POPPED)
137 /*
138 * The entry for this commit has already been popped,
139 * due to clock skew. Pretend that this parent does not
140 * exist.
141 */
142 return 0;
143 /*
144 * Find the existing entry and use it.
145 */
146 prio_queue_for_each(&data->rev_list, parent_entry) {
147 if (parent_entry->commit == to_push)
148 goto parent_found;
149 }
150 BUG("missing parent in priority queue");
151 parent_found:
152 ;
153 } else {
154 parent_entry = rev_list_push(data, to_push, 0);
155 }
156
157 if (entry->commit->object.flags & (COMMON | ADVERTISED)) {
158 mark_common(data, to_push);
159 } else {
160 uint16_t new_original_ttl = entry->ttl
161 ? entry->original_ttl : entry->original_ttl * 3 / 2 + 1;
162 uint16_t new_ttl = entry->ttl
163 ? entry->ttl - 1 : new_original_ttl;
164 if (parent_entry->original_ttl < new_original_ttl) {
165 parent_entry->original_ttl = new_original_ttl;
166 parent_entry->ttl = new_ttl;
167 }
168 }
169
170 return 1;
171 }
172
173 static const struct object_id *get_rev(struct data *data)
174 {
175 struct commit *to_send = NULL;
176
177 while (to_send == NULL) {
178 struct entry *entry;
179 struct commit *commit;
180 struct commit_list *p;
181 int parent_pushed = 0;
182
183 if (data->non_common_revs == 0)
184 return NULL;
185
186 entry = prio_queue_get(&data->rev_list);
187 if (!entry)
188 return NULL;
189 commit = entry->commit;
190 commit->object.flags |= POPPED;
191 if (!(commit->object.flags & COMMON))
192 data->non_common_revs--;
193
194 if (!(commit->object.flags & COMMON) && !entry->ttl)
195 to_send = commit;
196
197 repo_parse_commit(the_repository, commit);
198 for (p = commit->parents; p; p = p->next)
199 parent_pushed |= push_parent(data, entry, p->item);
200
201 if (!(commit->object.flags & COMMON) && !parent_pushed)
202 /*
203 * This commit has no parents, or all of its parents
204 * have already been popped (due to clock skew), so send
205 * it anyway.
206 */
207 to_send = commit;
208
209 free(entry);
210 }
211
212 return &to_send->object.oid;
213 }
214
215 static void known_common(struct fetch_negotiator *n, struct commit *c)
216 {
217 if (c->object.flags & SEEN)
218 return;
219 rev_list_push(n->data, c, ADVERTISED);
220 }
221
222 static void add_tip(struct fetch_negotiator *n, struct commit *c)
223 {
224 n->known_common = NULL;
225 if (c->object.flags & SEEN)
226 return;
227 rev_list_push(n->data, c, 0);
228 }
229
230 static const struct object_id *next(struct fetch_negotiator *n)
231 {
232 n->known_common = NULL;
233 n->add_tip = NULL;
234 return get_rev(n->data);
235 }
236
237 static int ack(struct fetch_negotiator *n, struct commit *c)
238 {
239 int known_to_be_common = !!(c->object.flags & COMMON);
240 if (!(c->object.flags & SEEN))
241 die("received ack for commit %s not sent as 'have'",
242 oid_to_hex(&c->object.oid));
243 mark_common(n->data, c);
244 return known_to_be_common;
245 }
246
247 static void have_sent(struct fetch_negotiator *n, struct commit *c)
248 {
249 if (repo_parse_commit(the_repository, c))
250 return;
251 mark_common(n->data, c);
252 }
253
254 static void release(struct fetch_negotiator *n)
255 {
256 struct data *data = n->data;
257 void *entry;
258 prio_queue_for_each(&data->rev_list, entry)
259 free(entry);
260 clear_prio_queue(&data->rev_list);
261 FREE_AND_NULL(data);
262 }
263
264 void skipping_negotiator_init(struct fetch_negotiator *negotiator)
265 {
266 struct data *data;
267 negotiator->known_common = known_common;
268 negotiator->add_tip = add_tip;
269 negotiator->next = next;
270 negotiator->ack = ack;
271 negotiator->have_sent = have_sent;
272 negotiator->release = release;
273 negotiator->data = CALLOC_ARRAY(data, 1);
274 data->rev_list.compare = compare;
275
276 if (marked)
277 refs_for_each_ref(get_main_ref_store(the_repository),
278 clear_marks, NULL);
279 marked = 1;
280 }