]> git.ipfire.org Git - thirdparty/git.git/blame - ls-refs.c
Merge branch 'js/range-diff-one-side-only'
[thirdparty/git.git] / ls-refs.c
CommitLineData
72d0ea00
BW
1#include "cache.h"
2#include "repository.h"
3#include "refs.h"
4#include "remote.h"
dbbcd44f 5#include "strvec.h"
72d0ea00
BW
6#include "ls-refs.h"
7#include "pkt-line.h"
e20b4192 8#include "config.h"
72d0ea00 9
59e1205d
JT
10static int config_read;
11static int advertise_unborn;
12static int allow_unborn;
13
14static void ensure_config_read(void)
15{
16 const char *str = NULL;
17
18 if (config_read)
19 return;
20
21 if (repo_config_get_string_tmp(the_repository, "lsrefs.unborn", &str)) {
22 /*
23 * If there is no such config, advertise and allow it by
24 * default.
25 */
26 advertise_unborn = 1;
27 allow_unborn = 1;
28 } else {
29 if (!strcmp(str, "advertise")) {
30 advertise_unborn = 1;
31 allow_unborn = 1;
32 } else if (!strcmp(str, "allow")) {
33 allow_unborn = 1;
34 } else if (!strcmp(str, "ignore")) {
35 /* do nothing */
36 } else {
37 die(_("invalid value '%s' for lsrefs.unborn"), str);
38 }
39 }
40 config_read = 1;
41}
42
72d0ea00
BW
43/*
44 * Check if one of the prefixes is a prefix of the ref.
45 * If no prefixes were provided, all refs match.
46 */
ef8d7ac4 47static int ref_match(const struct strvec *prefixes, const char *refname)
72d0ea00
BW
48{
49 int i;
50
d70a9eb6 51 if (!prefixes->nr)
72d0ea00
BW
52 return 1; /* no restriction */
53
d70a9eb6
JK
54 for (i = 0; i < prefixes->nr; i++) {
55 const char *prefix = prefixes->v[i];
72d0ea00
BW
56
57 if (starts_with(refname, prefix))
58 return 1;
59 }
60
61 return 0;
62}
63
64struct ls_refs_data {
65 unsigned peel;
66 unsigned symrefs;
ef8d7ac4 67 struct strvec prefixes;
59e1205d 68 unsigned unborn : 1;
72d0ea00
BW
69};
70
71static int send_ref(const char *refname, const struct object_id *oid,
72 int flag, void *cb_data)
73{
74 struct ls_refs_data *data = cb_data;
75 const char *refname_nons = strip_namespace(refname);
76 struct strbuf refline = STRBUF_INIT;
77
e20b4192
JK
78 if (ref_is_hidden(refname_nons, refname))
79 return 0;
80
e2f41a0a 81 if (!ref_match(&data->prefixes, refname_nons))
72d0ea00
BW
82 return 0;
83
59e1205d
JT
84 if (oid)
85 strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
86 else
87 strbuf_addf(&refline, "unborn %s", refname_nons);
72d0ea00
BW
88 if (data->symrefs && flag & REF_ISSYMREF) {
89 struct object_id unused;
90 const char *symref_target = resolve_ref_unsafe(refname, 0,
91 &unused,
92 &flag);
93
94 if (!symref_target)
95 die("'%s' is a symref but it is not?", refname);
96
533e0882
JK
97 strbuf_addf(&refline, " symref-target:%s",
98 strip_namespace(symref_target));
72d0ea00
BW
99 }
100
59e1205d 101 if (data->peel && oid) {
72d0ea00 102 struct object_id peeled;
36a31792 103 if (!peel_iterated_oid(oid, &peeled))
72d0ea00
BW
104 strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled));
105 }
106
107 strbuf_addch(&refline, '\n');
108 packet_write(1, refline.buf, refline.len);
109
110 strbuf_release(&refline);
111 return 0;
112}
113
59e1205d
JT
114static void send_possibly_unborn_head(struct ls_refs_data *data)
115{
116 struct strbuf namespaced = STRBUF_INIT;
117 struct object_id oid;
118 int flag;
119 int oid_is_null;
120
121 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
122 if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag))
123 return; /* bad ref */
124 oid_is_null = is_null_oid(&oid);
125 if (!oid_is_null ||
126 (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
127 send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
128 strbuf_release(&namespaced);
129}
130
e20b4192
JK
131static int ls_refs_config(const char *var, const char *value, void *data)
132{
133 /*
134 * We only serve fetches over v2 for now, so respect only "uploadpack"
135 * config. This may need to eventually be expanded to "receive", but we
136 * don't yet know how that information will be passed to ls-refs.
137 */
138 return parse_hide_refs_config(var, value, "uploadpack");
139}
140
ef8d7ac4 141int ls_refs(struct repository *r, struct strvec *keys,
72d0ea00
BW
142 struct packet_reader *request)
143{
144 struct ls_refs_data data;
145
146 memset(&data, 0, sizeof(data));
83befd37 147 strvec_init(&data.prefixes);
72d0ea00 148
59e1205d 149 ensure_config_read();
e20b4192
JK
150 git_config(ls_refs_config, NULL);
151
4845b772 152 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
72d0ea00
BW
153 const char *arg = request->line;
154 const char *out;
155
156 if (!strcmp("peel", arg))
157 data.peel = 1;
158 else if (!strcmp("symrefs", arg))
159 data.symrefs = 1;
160 else if (skip_prefix(arg, "ref-prefix ", &out))
ef8d7ac4 161 strvec_push(&data.prefixes, out);
59e1205d
JT
162 else if (!strcmp("unborn", arg))
163 data.unborn = allow_unborn;
72d0ea00
BW
164 }
165
4845b772
JK
166 if (request->status != PACKET_READ_FLUSH)
167 die(_("expected flush after ls-refs arguments"));
168
59e1205d 169 send_possibly_unborn_head(&data);
b3970c70
TB
170 if (!data.prefixes.nr)
171 strvec_push(&data.prefixes, "");
172 for_each_fullref_in_prefixes(get_git_namespace(), data.prefixes.v,
173 send_ref, &data, 0);
72d0ea00 174 packet_flush(1);
ef8d7ac4 175 strvec_clear(&data.prefixes);
72d0ea00
BW
176 return 0;
177}
59e1205d
JT
178
179int ls_refs_advertise(struct repository *r, struct strbuf *value)
180{
181 if (value) {
182 ensure_config_read();
183 if (advertise_unborn)
184 strbuf_addstr(value, "unborn");
185 }
186
187 return 1;
188}