Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "default.h"
5 #include "../commit.h"
6 #include "../fetch-negotiator.h"
7 #include "../prio-queue.h"
8 #include "../refs.h"
9 #include "../repository.h"
10 #include "../tag.h"
11
12 /* Remember to update object flag allocation in object.h */
13 #define COMMON (1U << 2)
14 #define COMMON_REF (1U << 3)
15 #define SEEN (1U << 4)
16 #define POPPED (1U << 5)
17
18 static int marked;
19
20 struct negotiation_state {
21 struct prio_queue rev_list;
22 int non_common_revs;
23 };
24
25 static void rev_list_push(struct negotiation_state *ns,
26 struct commit *commit, int mark)
27 {
28 if (!(commit->object.flags & mark)) {
29 commit->object.flags |= mark;
30
31 if (repo_parse_commit(the_repository, commit))
32 return;
33
34 prio_queue_put(&ns->rev_list, commit);
35
36 if (!(commit->object.flags & COMMON))
37 ns->non_common_revs++;
38 }
39 }
40
41 static int clear_marks(const struct reference *ref, void *cb_data UNUSED)
42 {
43 struct object *o = deref_tag(the_repository, parse_object(the_repository, ref->oid),
44 ref->name, 0);
45
46 if (o && o->type == OBJ_COMMIT)
47 clear_commit_marks((struct commit *)o,
48 COMMON | COMMON_REF | SEEN | POPPED);
49 return 0;
50 }
51
52 /*
53 * This function marks a rev and its ancestors as common.
54 * In some cases, it is desirable to mark only the ancestors (for example
55 * when only the server does not yet know that they are common).
56 */
57 static void mark_common(struct negotiation_state *ns, struct commit *commit,
58 int ancestors_only, int dont_parse)
59 {
60 struct commit_stack stack = COMMIT_STACK_INIT;
61
62 if (!commit || (commit->object.flags & COMMON))
63 return;
64
65 commit_stack_push(&stack, commit);
66 if (!ancestors_only) {
67 commit->object.flags |= COMMON;
68
69 if ((commit->object.flags & SEEN) && !(commit->object.flags & POPPED))
70 ns->non_common_revs--;
71 }
72 while ((commit = commit_stack_pop(&stack))) {
73 struct object *o = (struct object *)commit;
74
75 if (!(o->flags & SEEN))
76 rev_list_push(ns, commit, SEEN);
77 else {
78 struct commit_list *parents;
79
80 if (!o->parsed && !dont_parse)
81 if (repo_parse_commit(the_repository, commit))
82 continue;
83
84 for (parents = commit->parents;
85 parents;
86 parents = parents->next) {
87 struct commit *p = parents->item;
88
89 if (p->object.flags & COMMON)
90 continue;
91
92 p->object.flags |= COMMON;
93
94 if ((p->object.flags & SEEN) && !(p->object.flags & POPPED))
95 ns->non_common_revs--;
96
97 commit_stack_push(&stack, parents->item);
98 }
99 }
100 }
101
102 commit_stack_clear(&stack);
103 }
104
105 /*
106 * Get the next rev to send, ignoring the common.
107 */
108 static const struct object_id *get_rev(struct negotiation_state *ns)
109 {
110 struct commit *commit = NULL;
111
112 while (commit == NULL) {
113 unsigned int mark;
114 struct commit_list *parents;
115
116 if (ns->rev_list.nr == 0 || ns->non_common_revs == 0)
117 return NULL;
118
119 commit = prio_queue_get(&ns->rev_list);
120 repo_parse_commit(the_repository, commit);
121 parents = commit->parents;
122
123 commit->object.flags |= POPPED;
124 if (!(commit->object.flags & COMMON))
125 ns->non_common_revs--;
126
127 if (commit->object.flags & COMMON) {
128 /* do not send "have", and ignore ancestors */
129 commit = NULL;
130 mark = COMMON | SEEN;
131 } else if (commit->object.flags & COMMON_REF)
132 /* send "have", and ignore ancestors */
133 mark = COMMON | SEEN;
134 else
135 /* send "have", also for its ancestors */
136 mark = SEEN;
137
138 while (parents) {
139 if (!(parents->item->object.flags & SEEN))
140 rev_list_push(ns, parents->item, mark);
141 if (mark & COMMON)
142 mark_common(ns, parents->item, 1, 0);
143 parents = parents->next;
144 }
145 }
146
147 return &commit->object.oid;
148 }
149
150 static void known_common(struct fetch_negotiator *n, struct commit *c)
151 {
152 if (!(c->object.flags & SEEN)) {
153 rev_list_push(n->data, c, COMMON_REF | SEEN);
154 mark_common(n->data, c, 1, 1);
155 }
156 }
157
158 static void add_tip(struct fetch_negotiator *n, struct commit *c)
159 {
160 n->known_common = NULL;
161 rev_list_push(n->data, c, SEEN);
162 }
163
164 static const struct object_id *next(struct fetch_negotiator *n)
165 {
166 n->known_common = NULL;
167 n->add_tip = NULL;
168 return get_rev(n->data);
169 }
170
171 static int ack(struct fetch_negotiator *n, struct commit *c)
172 {
173 int known_to_be_common = !!(c->object.flags & COMMON);
174 mark_common(n->data, c, 0, 1);
175 return known_to_be_common;
176 }
177
178 static void have_sent(struct fetch_negotiator *n, struct commit *c)
179 {
180 if (repo_parse_commit(the_repository, c))
181 return;
182 mark_common(n->data, c, 0, 0);
183 }
184
185 static void release(struct fetch_negotiator *n)
186 {
187 clear_prio_queue(&((struct negotiation_state *)n->data)->rev_list);
188 FREE_AND_NULL(n->data);
189 }
190
191 void default_negotiator_init(struct fetch_negotiator *negotiator)
192 {
193 struct negotiation_state *ns;
194 negotiator->known_common = known_common;
195 negotiator->add_tip = add_tip;
196 negotiator->next = next;
197 negotiator->ack = ack;
198 negotiator->have_sent = have_sent;
199 negotiator->release = release;
200 negotiator->data = CALLOC_ARRAY(ns, 1);
201 ns->rev_list.compare = compare_commits_by_commit_date;
202
203 if (marked)
204 refs_for_each_ref(get_main_ref_store(the_repository),
205 clear_marks, NULL);
206 marked = 1;
207 }