4 #include "../fetch-negotiator.h"
5 #include "../prio-queue.h"
9 /* Remember to update object flag allocation in object.h */
10 #define COMMON (1U << 2)
11 #define COMMON_REF (1U << 3)
12 #define SEEN (1U << 4)
13 #define POPPED (1U << 5)
17 struct negotiation_state
{
18 struct prio_queue rev_list
;
22 static void rev_list_push(struct negotiation_state
*ns
,
23 struct commit
*commit
, int mark
)
25 if (!(commit
->object
.flags
& mark
)) {
26 commit
->object
.flags
|= mark
;
28 if (parse_commit(commit
))
31 prio_queue_put(&ns
->rev_list
, commit
);
33 if (!(commit
->object
.flags
& COMMON
))
34 ns
->non_common_revs
++;
38 static int clear_marks(const char *refname
, const struct object_id
*oid
,
42 struct object
*o
= deref_tag(the_repository
, parse_object(the_repository
, oid
), refname
, 0);
44 if (o
&& o
->type
== OBJ_COMMIT
)
45 clear_commit_marks((struct commit
*)o
,
46 COMMON
| COMMON_REF
| SEEN
| POPPED
);
51 * This function marks a rev and its ancestors as common.
52 * In some cases, it is desirable to mark only the ancestors (for example
53 * when only the server does not yet know that they are common).
55 static void mark_common(struct negotiation_state
*ns
, struct commit
*commit
,
56 int ancestors_only
, int dont_parse
)
58 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
59 struct object
*o
= (struct object
*)commit
;
64 if (!(o
->flags
& SEEN
))
65 rev_list_push(ns
, commit
, SEEN
);
67 struct commit_list
*parents
;
69 if (!ancestors_only
&& !(o
->flags
& POPPED
))
70 ns
->non_common_revs
--;
71 if (!o
->parsed
&& !dont_parse
)
72 if (parse_commit(commit
))
75 for (parents
= commit
->parents
;
77 parents
= parents
->next
)
78 mark_common(ns
, parents
->item
, 0,
85 * Get the next rev to send, ignoring the common.
87 static const struct object_id
*get_rev(struct negotiation_state
*ns
)
89 struct commit
*commit
= NULL
;
91 while (commit
== NULL
) {
93 struct commit_list
*parents
;
95 if (ns
->rev_list
.nr
== 0 || ns
->non_common_revs
== 0)
98 commit
= prio_queue_get(&ns
->rev_list
);
100 parents
= commit
->parents
;
102 commit
->object
.flags
|= POPPED
;
103 if (!(commit
->object
.flags
& COMMON
))
104 ns
->non_common_revs
--;
106 if (commit
->object
.flags
& COMMON
) {
107 /* do not send "have", and ignore ancestors */
109 mark
= COMMON
| SEEN
;
110 } else if (commit
->object
.flags
& COMMON_REF
)
111 /* send "have", and ignore ancestors */
112 mark
= COMMON
| SEEN
;
114 /* send "have", also for its ancestors */
118 if (!(parents
->item
->object
.flags
& SEEN
))
119 rev_list_push(ns
, parents
->item
, mark
);
121 mark_common(ns
, parents
->item
, 1, 0);
122 parents
= parents
->next
;
126 return &commit
->object
.oid
;
129 static void known_common(struct fetch_negotiator
*n
, struct commit
*c
)
131 if (!(c
->object
.flags
& SEEN
)) {
132 rev_list_push(n
->data
, c
, COMMON_REF
| SEEN
);
133 mark_common(n
->data
, c
, 1, 1);
137 static void add_tip(struct fetch_negotiator
*n
, struct commit
*c
)
139 n
->known_common
= NULL
;
140 rev_list_push(n
->data
, c
, SEEN
);
143 static const struct object_id
*next(struct fetch_negotiator
*n
)
145 n
->known_common
= NULL
;
147 return get_rev(n
->data
);
150 static int ack(struct fetch_negotiator
*n
, struct commit
*c
)
152 int known_to_be_common
= !!(c
->object
.flags
& COMMON
);
153 mark_common(n
->data
, c
, 0, 1);
154 return known_to_be_common
;
157 static void release(struct fetch_negotiator
*n
)
159 clear_prio_queue(&((struct negotiation_state
*)n
->data
)->rev_list
);
160 FREE_AND_NULL(n
->data
);
163 void default_negotiator_init(struct fetch_negotiator
*negotiator
)
165 struct negotiation_state
*ns
;
166 negotiator
->known_common
= known_common
;
167 negotiator
->add_tip
= add_tip
;
168 negotiator
->next
= next
;
169 negotiator
->ack
= ack
;
170 negotiator
->release
= release
;
171 negotiator
->data
= CALLOC_ARRAY(ns
, 1);
172 ns
->rev_list
.compare
= compare_commits_by_commit_date
;
175 for_each_ref(clear_marks
, NULL
);