1 #include "git-compat-util.h"
4 #include "../fetch-negotiator.h"
5 #include "../prio-queue.h"
7 #include "../repository.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)
18 struct negotiation_state
{
19 struct prio_queue rev_list
;
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
))
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
,
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
);
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 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
60 struct object
*o
= (struct object
*)commit
;
65 if (!(o
->flags
& SEEN
))
66 rev_list_push(ns
, commit
, SEEN
);
68 struct commit_list
*parents
;
70 if (!ancestors_only
&& !(o
->flags
& POPPED
))
71 ns
->non_common_revs
--;
72 if (!o
->parsed
&& !dont_parse
)
73 if (repo_parse_commit(the_repository
, commit
))
76 for (parents
= commit
->parents
;
78 parents
= parents
->next
)
79 mark_common(ns
, parents
->item
, 0,
86 * Get the next rev to send, ignoring the common.
88 static const struct object_id
*get_rev(struct negotiation_state
*ns
)
90 struct commit
*commit
= NULL
;
92 while (commit
== NULL
) {
94 struct commit_list
*parents
;
96 if (ns
->rev_list
.nr
== 0 || ns
->non_common_revs
== 0)
99 commit
= prio_queue_get(&ns
->rev_list
);
100 repo_parse_commit(the_repository
, commit
);
101 parents
= commit
->parents
;
103 commit
->object
.flags
|= POPPED
;
104 if (!(commit
->object
.flags
& COMMON
))
105 ns
->non_common_revs
--;
107 if (commit
->object
.flags
& COMMON
) {
108 /* do not send "have", and ignore ancestors */
110 mark
= COMMON
| SEEN
;
111 } else if (commit
->object
.flags
& COMMON_REF
)
112 /* send "have", and ignore ancestors */
113 mark
= COMMON
| SEEN
;
115 /* send "have", also for its ancestors */
119 if (!(parents
->item
->object
.flags
& SEEN
))
120 rev_list_push(ns
, parents
->item
, mark
);
122 mark_common(ns
, parents
->item
, 1, 0);
123 parents
= parents
->next
;
127 return &commit
->object
.oid
;
130 static void known_common(struct fetch_negotiator
*n
, struct commit
*c
)
132 if (!(c
->object
.flags
& SEEN
)) {
133 rev_list_push(n
->data
, c
, COMMON_REF
| SEEN
);
134 mark_common(n
->data
, c
, 1, 1);
138 static void add_tip(struct fetch_negotiator
*n
, struct commit
*c
)
140 n
->known_common
= NULL
;
141 rev_list_push(n
->data
, c
, SEEN
);
144 static const struct object_id
*next(struct fetch_negotiator
*n
)
146 n
->known_common
= NULL
;
148 return get_rev(n
->data
);
151 static int ack(struct fetch_negotiator
*n
, struct commit
*c
)
153 int known_to_be_common
= !!(c
->object
.flags
& COMMON
);
154 mark_common(n
->data
, c
, 0, 1);
155 return known_to_be_common
;
158 static void release(struct fetch_negotiator
*n
)
160 clear_prio_queue(&((struct negotiation_state
*)n
->data
)->rev_list
);
161 FREE_AND_NULL(n
->data
);
164 void default_negotiator_init(struct fetch_negotiator
*negotiator
)
166 struct negotiation_state
*ns
;
167 negotiator
->known_common
= known_common
;
168 negotiator
->add_tip
= add_tip
;
169 negotiator
->next
= next
;
170 negotiator
->ack
= ack
;
171 negotiator
->release
= release
;
172 negotiator
->data
= CALLOC_ARRAY(ns
, 1);
173 ns
->rev_list
.compare
= compare_commits_by_commit_date
;
176 for_each_ref(clear_marks
, NULL
);