Coverage Report

Created: 2026-01-09 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/negotiator/default.c
Line
Count
Source
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 struct reference *ref, void *cb_data UNUSED)
42
0
{
43
0
  struct object *o = deref_tag(the_repository, parse_object(the_repository, ref->oid),
44
0
             ref->name, 0);
45
46
0
  if (o && o->type == OBJ_COMMIT)
47
0
    clear_commit_marks((struct commit *)o,
48
0
           COMMON | COMMON_REF | SEEN | POPPED);
49
0
  return 0;
50
0
}
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
0
{
60
0
  struct prio_queue queue = { NULL };
61
62
0
  if (!commit || (commit->object.flags & COMMON))
63
0
    return;
64
65
0
  prio_queue_put(&queue, commit);
66
0
  if (!ancestors_only) {
67
0
    commit->object.flags |= COMMON;
68
69
0
    if ((commit->object.flags & SEEN) && !(commit->object.flags & POPPED))
70
0
      ns->non_common_revs--;
71
0
  }
72
0
  while ((commit = prio_queue_get(&queue))) {
73
0
    struct object *o = (struct object *)commit;
74
75
0
    if (!(o->flags & SEEN))
76
0
      rev_list_push(ns, commit, SEEN);
77
0
    else {
78
0
      struct commit_list *parents;
79
80
0
      if (!o->parsed && !dont_parse)
81
0
        if (repo_parse_commit(the_repository, commit))
82
0
          continue;
83
84
0
      for (parents = commit->parents;
85
0
          parents;
86
0
          parents = parents->next) {
87
0
        struct commit *p = parents->item;
88
89
0
        if (p->object.flags & COMMON)
90
0
          continue;
91
92
0
        p->object.flags |= COMMON;
93
94
0
        if ((p->object.flags & SEEN) && !(p->object.flags & POPPED))
95
0
          ns->non_common_revs--;
96
97
0
        prio_queue_put(&queue, parents->item);
98
0
      }
99
0
    }
100
0
  }
101
102
0
  clear_prio_queue(&queue);
103
0
}
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
0
{
110
0
  struct commit *commit = NULL;
111
112
0
  while (commit == NULL) {
113
0
    unsigned int mark;
114
0
    struct commit_list *parents;
115
116
0
    if (ns->rev_list.nr == 0 || ns->non_common_revs == 0)
117
0
      return NULL;
118
119
0
    commit = prio_queue_get(&ns->rev_list);
120
0
    repo_parse_commit(the_repository, commit);
121
0
    parents = commit->parents;
122
123
0
    commit->object.flags |= POPPED;
124
0
    if (!(commit->object.flags & COMMON))
125
0
      ns->non_common_revs--;
126
127
0
    if (commit->object.flags & COMMON) {
128
      /* do not send "have", and ignore ancestors */
129
0
      commit = NULL;
130
0
      mark = COMMON | SEEN;
131
0
    } else if (commit->object.flags & COMMON_REF)
132
      /* send "have", and ignore ancestors */
133
0
      mark = COMMON | SEEN;
134
0
    else
135
      /* send "have", also for its ancestors */
136
0
      mark = SEEN;
137
138
0
    while (parents) {
139
0
      if (!(parents->item->object.flags & SEEN))
140
0
        rev_list_push(ns, parents->item, mark);
141
0
      if (mark & COMMON)
142
0
        mark_common(ns, parents->item, 1, 0);
143
0
      parents = parents->next;
144
0
    }
145
0
  }
146
147
0
  return &commit->object.oid;
148
0
}
149
150
static void known_common(struct fetch_negotiator *n, struct commit *c)
151
0
{
152
0
  if (!(c->object.flags & SEEN)) {
153
0
    rev_list_push(n->data, c, COMMON_REF | SEEN);
154
0
    mark_common(n->data, c, 1, 1);
155
0
  }
156
0
}
157
158
static void add_tip(struct fetch_negotiator *n, struct commit *c)
159
0
{
160
0
  n->known_common = NULL;
161
0
  rev_list_push(n->data, c, SEEN);
162
0
}
163
164
static const struct object_id *next(struct fetch_negotiator *n)
165
0
{
166
0
  n->known_common = NULL;
167
0
  n->add_tip = NULL;
168
0
  return get_rev(n->data);
169
0
}
170
171
static int ack(struct fetch_negotiator *n, struct commit *c)
172
0
{
173
0
  int known_to_be_common = !!(c->object.flags & COMMON);
174
0
  mark_common(n->data, c, 0, 1);
175
0
  return known_to_be_common;
176
0
}
177
178
static void release(struct fetch_negotiator *n)
179
0
{
180
0
  clear_prio_queue(&((struct negotiation_state *)n->data)->rev_list);
181
0
  FREE_AND_NULL(n->data);
182
0
}
183
184
void default_negotiator_init(struct fetch_negotiator *negotiator)
185
0
{
186
0
  struct negotiation_state *ns;
187
0
  negotiator->known_common = known_common;
188
0
  negotiator->add_tip = add_tip;
189
0
  negotiator->next = next;
190
0
  negotiator->ack = ack;
191
0
  negotiator->release = release;
192
0
  negotiator->data = CALLOC_ARRAY(ns, 1);
193
0
  ns->rev_list.compare = compare_commits_by_commit_date;
194
195
0
  if (marked)
196
0
    refs_for_each_ref(get_main_ref_store(the_repository),
197
          clear_marks, NULL);
198
0
  marked = 1;
199
0
}