t4301: verify that merge-tree fails on missing blob objects
[git/gitster.git] / negotiator / default.c
blob9a5b69632728d1d6877556a330e666ba530a894a
1 #include "git-compat-util.h"
2 #include "default.h"
3 #include "../commit.h"
4 #include "../fetch-negotiator.h"
5 #include "../prio-queue.h"
6 #include "../refs.h"
7 #include "../repository.h"
8 #include "../tag.h"
10 /* Remember to update object flag allocation in object.h */
11 #define COMMON (1U << 2)
12 #define COMMON_REF (1U << 3)
13 #define SEEN (1U << 4)
14 #define POPPED (1U << 5)
16 static int marked;
18 struct negotiation_state {
19 struct prio_queue rev_list;
20 int non_common_revs;
23 static void rev_list_push(struct negotiation_state *ns,
24 struct commit *commit, int mark)
26 if (!(commit->object.flags & mark)) {
27 commit->object.flags |= mark;
29 if (repo_parse_commit(the_repository, commit))
30 return;
32 prio_queue_put(&ns->rev_list, commit);
34 if (!(commit->object.flags & COMMON))
35 ns->non_common_revs++;
39 static int clear_marks(const char *refname, const struct object_id *oid,
40 int flag UNUSED,
41 void *cb_data UNUSED)
43 struct object *o = deref_tag(the_repository, parse_object(the_repository, oid), refname, 0);
45 if (o && o->type == OBJ_COMMIT)
46 clear_commit_marks((struct commit *)o,
47 COMMON | COMMON_REF | SEEN | POPPED);
48 return 0;
52 * This function marks a rev and its ancestors as common.
53 * In some cases, it is desirable to mark only the ancestors (for example
54 * when only the server does not yet know that they are common).
56 static void mark_common(struct negotiation_state *ns, struct commit *commit,
57 int ancestors_only, int dont_parse)
59 struct prio_queue queue = { NULL };
61 if (!commit || (commit->object.flags & COMMON))
62 return;
64 prio_queue_put(&queue, commit);
65 if (!ancestors_only) {
66 commit->object.flags |= COMMON;
68 if ((commit->object.flags & SEEN) && !(commit->object.flags & POPPED))
69 ns->non_common_revs--;
71 while ((commit = prio_queue_get(&queue))) {
72 struct object *o = (struct object *)commit;
74 if (!(o->flags & SEEN))
75 rev_list_push(ns, commit, SEEN);
76 else {
77 struct commit_list *parents;
79 if (!o->parsed && !dont_parse)
80 if (repo_parse_commit(the_repository, commit))
81 continue;
83 for (parents = commit->parents;
84 parents;
85 parents = parents->next) {
86 struct commit *p = parents->item;
88 if (p->object.flags & COMMON)
89 continue;
91 p->object.flags |= COMMON;
93 if ((p->object.flags & SEEN) && !(p->object.flags & POPPED))
94 ns->non_common_revs--;
96 prio_queue_put(&queue, parents->item);
101 clear_prio_queue(&queue);
105 * Get the next rev to send, ignoring the common.
107 static const struct object_id *get_rev(struct negotiation_state *ns)
109 struct commit *commit = NULL;
111 while (commit == NULL) {
112 unsigned int mark;
113 struct commit_list *parents;
115 if (ns->rev_list.nr == 0 || ns->non_common_revs == 0)
116 return NULL;
118 commit = prio_queue_get(&ns->rev_list);
119 repo_parse_commit(the_repository, commit);
120 parents = commit->parents;
122 commit->object.flags |= POPPED;
123 if (!(commit->object.flags & COMMON))
124 ns->non_common_revs--;
126 if (commit->object.flags & COMMON) {
127 /* do not send "have", and ignore ancestors */
128 commit = NULL;
129 mark = COMMON | SEEN;
130 } else if (commit->object.flags & COMMON_REF)
131 /* send "have", and ignore ancestors */
132 mark = COMMON | SEEN;
133 else
134 /* send "have", also for its ancestors */
135 mark = SEEN;
137 while (parents) {
138 if (!(parents->item->object.flags & SEEN))
139 rev_list_push(ns, parents->item, mark);
140 if (mark & COMMON)
141 mark_common(ns, parents->item, 1, 0);
142 parents = parents->next;
146 return &commit->object.oid;
149 static void known_common(struct fetch_negotiator *n, struct commit *c)
151 if (!(c->object.flags & SEEN)) {
152 rev_list_push(n->data, c, COMMON_REF | SEEN);
153 mark_common(n->data, c, 1, 1);
157 static void add_tip(struct fetch_negotiator *n, struct commit *c)
159 n->known_common = NULL;
160 rev_list_push(n->data, c, SEEN);
163 static const struct object_id *next(struct fetch_negotiator *n)
165 n->known_common = NULL;
166 n->add_tip = NULL;
167 return get_rev(n->data);
170 static int ack(struct fetch_negotiator *n, struct commit *c)
172 int known_to_be_common = !!(c->object.flags & COMMON);
173 mark_common(n->data, c, 0, 1);
174 return known_to_be_common;
177 static void release(struct fetch_negotiator *n)
179 clear_prio_queue(&((struct negotiation_state *)n->data)->rev_list);
180 FREE_AND_NULL(n->data);
183 void default_negotiator_init(struct fetch_negotiator *negotiator)
185 struct negotiation_state *ns;
186 negotiator->known_common = known_common;
187 negotiator->add_tip = add_tip;
188 negotiator->next = next;
189 negotiator->ack = ack;
190 negotiator->release = release;
191 negotiator->data = CALLOC_ARRAY(ns, 1);
192 ns->rev_list.compare = compare_commits_by_commit_date;
194 if (marked)
195 for_each_ref(clear_marks, NULL);
196 marked = 1;