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