Coverage Report

Created: 2024-09-08 06:23

/src/git/ls-refs.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "environment.h"
5
#include "gettext.h"
6
#include "hash.h"
7
#include "hex.h"
8
#include "repository.h"
9
#include "refs.h"
10
#include "strvec.h"
11
#include "ls-refs.h"
12
#include "pkt-line.h"
13
#include "config.h"
14
#include "string-list.h"
15
16
static enum {
17
  UNBORN_IGNORE = 0,
18
  UNBORN_ALLOW,
19
  UNBORN_ADVERTISE /* implies ALLOW */
20
} unborn_config(struct repository *r)
21
0
{
22
0
  const char *str = NULL;
23
24
0
  if (repo_config_get_string_tmp(r, "lsrefs.unborn", &str)) {
25
    /*
26
     * If there is no such config, advertise and allow it by
27
     * default.
28
     */
29
0
    return UNBORN_ADVERTISE;
30
0
  } else {
31
0
    if (!strcmp(str, "advertise")) {
32
0
      return UNBORN_ADVERTISE;
33
0
    } else if (!strcmp(str, "allow")) {
34
0
      return UNBORN_ALLOW;
35
0
    } else if (!strcmp(str, "ignore")) {
36
0
      return UNBORN_IGNORE;
37
0
    } else {
38
0
      die(_("invalid value for '%s': '%s'"),
39
0
          "lsrefs.unborn", str);
40
0
    }
41
0
  }
42
0
}
43
44
/*
45
 * If we see this many or more "ref-prefix" lines from the client, we consider
46
 * it "too many" and will avoid using the prefix feature entirely.
47
 */
48
0
#define TOO_MANY_PREFIXES 65536
49
50
/*
51
 * Check if one of the prefixes is a prefix of the ref.
52
 * If no prefixes were provided, all refs match.
53
 */
54
static int ref_match(const struct strvec *prefixes, const char *refname)
55
0
{
56
0
  int i;
57
58
0
  if (!prefixes->nr)
59
0
    return 1; /* no restriction */
60
61
0
  for (i = 0; i < prefixes->nr; i++) {
62
0
    const char *prefix = prefixes->v[i];
63
64
0
    if (starts_with(refname, prefix))
65
0
      return 1;
66
0
  }
67
68
0
  return 0;
69
0
}
70
71
struct ls_refs_data {
72
  unsigned peel;
73
  unsigned symrefs;
74
  struct strvec prefixes;
75
  struct strbuf buf;
76
  struct strvec hidden_refs;
77
  unsigned unborn : 1;
78
};
79
80
static int send_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
81
        int flag, void *cb_data)
82
0
{
83
0
  struct ls_refs_data *data = cb_data;
84
0
  const char *refname_nons = strip_namespace(refname);
85
86
0
  strbuf_reset(&data->buf);
87
88
0
  if (ref_is_hidden(refname_nons, refname, &data->hidden_refs))
89
0
    return 0;
90
91
0
  if (!ref_match(&data->prefixes, refname_nons))
92
0
    return 0;
93
94
0
  if (oid)
95
0
    strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
96
0
  else
97
0
    strbuf_addf(&data->buf, "unborn %s", refname_nons);
98
0
  if (data->symrefs && flag & REF_ISSYMREF) {
99
0
    struct object_id unused;
100
0
    const char *symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
101
0
                    refname,
102
0
                    0,
103
0
                    &unused,
104
0
                    &flag);
105
106
0
    if (!symref_target)
107
0
      die("'%s' is a symref but it is not?", refname);
108
109
0
    strbuf_addf(&data->buf, " symref-target:%s",
110
0
          strip_namespace(symref_target));
111
0
  }
112
113
0
  if (data->peel && oid) {
114
0
    struct object_id peeled;
115
0
    if (!peel_iterated_oid(the_repository, oid, &peeled))
116
0
      strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
117
0
  }
118
119
0
  strbuf_addch(&data->buf, '\n');
120
0
  packet_fwrite(stdout, data->buf.buf, data->buf.len);
121
122
0
  return 0;
123
0
}
124
125
static void send_possibly_unborn_head(struct ls_refs_data *data)
126
0
{
127
0
  struct strbuf namespaced = STRBUF_INIT;
128
0
  struct object_id oid;
129
0
  int flag;
130
0
  int oid_is_null;
131
132
0
  strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
133
0
  if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), namespaced.buf, 0, &oid, &flag))
134
0
    return; /* bad ref */
135
0
  oid_is_null = is_null_oid(&oid);
136
0
  if (!oid_is_null ||
137
0
      (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
138
0
    send_ref(namespaced.buf, NULL, oid_is_null ? NULL : &oid, flag, data);
139
0
  strbuf_release(&namespaced);
140
0
}
141
142
static int ls_refs_config(const char *var, const char *value,
143
        const struct config_context *ctx UNUSED,
144
        void *cb_data)
145
0
{
146
0
  struct ls_refs_data *data = cb_data;
147
  /*
148
   * We only serve fetches over v2 for now, so respect only "uploadpack"
149
   * config. This may need to eventually be expanded to "receive", but we
150
   * don't yet know how that information will be passed to ls-refs.
151
   */
152
0
  return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
153
0
}
154
155
int ls_refs(struct repository *r, struct packet_reader *request)
156
0
{
157
0
  struct ls_refs_data data;
158
159
0
  memset(&data, 0, sizeof(data));
160
0
  strvec_init(&data.prefixes);
161
0
  strbuf_init(&data.buf, 0);
162
0
  strvec_init(&data.hidden_refs);
163
164
0
  git_config(ls_refs_config, &data);
165
166
0
  while (packet_reader_read(request) == PACKET_READ_NORMAL) {
167
0
    const char *arg = request->line;
168
0
    const char *out;
169
170
0
    if (!strcmp("peel", arg))
171
0
      data.peel = 1;
172
0
    else if (!strcmp("symrefs", arg))
173
0
      data.symrefs = 1;
174
0
    else if (skip_prefix(arg, "ref-prefix ", &out)) {
175
0
      if (data.prefixes.nr < TOO_MANY_PREFIXES)
176
0
        strvec_push(&data.prefixes, out);
177
0
    }
178
0
    else if (!strcmp("unborn", arg))
179
0
      data.unborn = !!unborn_config(r);
180
0
    else
181
0
      die(_("unexpected line: '%s'"), arg);
182
0
  }
183
184
0
  if (request->status != PACKET_READ_FLUSH)
185
0
    die(_("expected flush after ls-refs arguments"));
186
187
  /*
188
   * If we saw too many prefixes, we must avoid using them at all; as
189
   * soon as we have any prefix, they are meant to form a comprehensive
190
   * list.
191
   */
192
0
  if (data.prefixes.nr >= TOO_MANY_PREFIXES)
193
0
    strvec_clear(&data.prefixes);
194
195
0
  send_possibly_unborn_head(&data);
196
0
  if (!data.prefixes.nr)
197
0
    strvec_push(&data.prefixes, "");
198
0
  refs_for_each_fullref_in_prefixes(get_main_ref_store(r),
199
0
            get_git_namespace(), data.prefixes.v,
200
0
            hidden_refs_to_excludes(&data.hidden_refs),
201
0
            send_ref, &data);
202
0
  packet_fflush(stdout);
203
0
  strvec_clear(&data.prefixes);
204
0
  strbuf_release(&data.buf);
205
0
  strvec_clear(&data.hidden_refs);
206
0
  return 0;
207
0
}
208
209
int ls_refs_advertise(struct repository *r, struct strbuf *value)
210
0
{
211
0
  if (value && unborn_config(r) == UNBORN_ADVERTISE)
212
0
    strbuf_addstr(value, "unborn");
213
214
0
  return 1;
215
0
}