+#define COMMON (1U << 1)
+#define COMMON_REF (1U << 2 | COMMON)
+#define SEEN (1U << 3)
+#define POPPED (1U << 4)
+
+static struct commit_list *rev_list = NULL;
+static struct commit_list *rev_list_end = NULL;
+static unsigned long non_common_revs = 0;
+
+static void rev_list_append(struct commit *commit, int mark)
+{
+ if (!(commit->object.flags & mark)) {
+ commit->object.flags |= mark;
+
+ if (rev_list == NULL) {
+ commit_list_insert(commit, &rev_list);
+ rev_list_end = rev_list;
+ } else {
+ commit_list_insert(commit, &(rev_list_end->next));
+ rev_list_end = rev_list_end->next;
+ }
+
+ if (!(commit->object.flags & COMMON))
+ non_common_revs++;
+ }
+}
+
+static int rev_list_append_sha1(const char *path, const unsigned char *sha1)
+{
+ struct object *o = deref_tag(parse_object(sha1));
+
+ if (o->type == commit_type)
+ rev_list_append((struct commit *)o, SEEN);
+
+ return 0;
+}
+
+static void mark_common(struct commit *commit)
+{
+ if (commit != NULL && !(commit->object.flags & COMMON)) {
+ struct object *o = (struct object *)commit;
+ o->flags |= COMMON;
+ if (!(o->flags & SEEN))
+ rev_list_append(commit, SEEN);
+ else {
+ struct commit_list *parents;
+
+ if (!(o->flags & POPPED))
+ non_common_revs--;
+ if (!o->parsed)
+ parse_commit(commit);
+ for (parents = commit->parents;
+ parents;
+ parents = parents->next)
+ mark_common(parents->item);
+ }
+ }
+}
+
+/*
+ Get the next rev to send, ignoring the common.
+*/
+
+static const unsigned char* get_rev()
+{
+ struct commit *commit = NULL;
+
+ while (commit == NULL) {
+ unsigned int mark;
+ struct commit_list* parents;
+
+ if (rev_list == NULL || non_common_revs == 0)
+ return NULL;
+
+ commit = rev_list->item;
+ if (!(commit->object.parsed))
+ parse_commit(commit);
+ commit->object.flags |= POPPED;
+ if (!(commit->object.flags & COMMON))
+ non_common_revs--;
+
+ parents = commit->parents;
+
+ if (commit->object.flags & COMMON) {
+ /* do not send "have", and ignore ancestors */
+ commit = NULL;
+ mark = COMMON | SEEN;
+ } else if (commit->object.flags & COMMON_REF)
+ /* send "have", and ignore ancestors */
+ mark = COMMON | SEEN;
+ else
+ /* send "have", also for its ancestors */
+ mark = SEEN;
+
+ while (parents) {
+ if (mark & COMMON)
+ mark_common(parents->item);
+ else
+ rev_list_append(parents->item, mark);
+ parents = parents->next;
+ }
+
+ rev_list = rev_list->next;
+ }
+
+ return commit->object.sha1;
+}