Merge branch 'ma/help-w-check-for-requested-page'
[git.git] / ls-refs.c
blobbe0956891088b6a7181dd43c022b9ecca65c7cd8
1 #include "cache.h"
2 #include "repository.h"
3 #include "refs.h"
4 #include "remote.h"
5 #include "strvec.h"
6 #include "ls-refs.h"
7 #include "pkt-line.h"
8 #include "config.h"
10 static int config_read;
11 static int advertise_unborn;
12 static int allow_unborn;
14 static void ensure_config_read(void)
16 const char *str = NULL;
18 if (config_read)
19 return;
21 if (repo_config_get_string_tmp(the_repository, "lsrefs.unborn", &str)) {
23 * If there is no such config, advertise and allow it by
24 * default.
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);
40 config_read = 1;
44 * Check if one of the prefixes is a prefix of the ref.
45 * If no prefixes were provided, all refs match.
47 static int ref_match(const struct strvec *prefixes, const char *refname)
49 int i;
51 if (!prefixes->nr)
52 return 1; /* no restriction */
54 for (i = 0; i < prefixes->nr; i++) {
55 const char *prefix = prefixes->v[i];
57 if (starts_with(refname, prefix))
58 return 1;
61 return 0;
64 struct ls_refs_data {
65 unsigned peel;
66 unsigned symrefs;
67 struct strvec prefixes;
68 struct strbuf buf;
69 unsigned unborn : 1;
72 static int send_ref(const char *refname, const struct object_id *oid,
73 int flag, void *cb_data)
75 struct ls_refs_data *data = cb_data;
76 const char *refname_nons = strip_namespace(refname);
78 strbuf_reset(&data->buf);
80 if (ref_is_hidden(refname_nons, refname))
81 return 0;
83 if (!ref_match(&data->prefixes, refname_nons))
84 return 0;
86 if (oid)
87 strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
88 else
89 strbuf_addf(&data->buf, "unborn %s", refname_nons);
90 if (data->symrefs && flag & REF_ISSYMREF) {
91 struct object_id unused;
92 const char *symref_target = resolve_ref_unsafe(refname, 0,
93 &unused,
94 &flag);
96 if (!symref_target)
97 die("'%s' is a symref but it is not?", refname);
99 strbuf_addf(&data->buf, " symref-target:%s",
100 strip_namespace(symref_target));
103 if (data->peel && oid) {
104 struct object_id peeled;
105 if (!peel_iterated_oid(oid, &peeled))
106 strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
109 strbuf_addch(&data->buf, '\n');
110 packet_fwrite(stdout, data->buf.buf, data->buf.len);
112 return 0;
115 static void send_possibly_unborn_head(struct ls_refs_data *data)
117 struct strbuf namespaced = STRBUF_INIT;
118 struct object_id oid;
119 int flag;
120 int oid_is_null;
122 strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
123 if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag))
124 return; /* bad ref */
125 oid_is_null = is_null_oid(&oid);
126 if (!oid_is_null ||
127 (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
128 send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
129 strbuf_release(&namespaced);
132 static int ls_refs_config(const char *var, const char *value, void *data)
135 * We only serve fetches over v2 for now, so respect only "uploadpack"
136 * config. This may need to eventually be expanded to "receive", but we
137 * don't yet know how that information will be passed to ls-refs.
139 return parse_hide_refs_config(var, value, "uploadpack");
142 int ls_refs(struct repository *r, struct packet_reader *request)
144 struct ls_refs_data data;
146 memset(&data, 0, sizeof(data));
147 strvec_init(&data.prefixes);
148 strbuf_init(&data.buf, 0);
150 ensure_config_read();
151 git_config(ls_refs_config, NULL);
153 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
154 const char *arg = request->line;
155 const char *out;
157 if (!strcmp("peel", arg))
158 data.peel = 1;
159 else if (!strcmp("symrefs", arg))
160 data.symrefs = 1;
161 else if (skip_prefix(arg, "ref-prefix ", &out))
162 strvec_push(&data.prefixes, out);
163 else if (!strcmp("unborn", arg))
164 data.unborn = allow_unborn;
167 if (request->status != PACKET_READ_FLUSH)
168 die(_("expected flush after ls-refs arguments"));
170 send_possibly_unborn_head(&data);
171 if (!data.prefixes.nr)
172 strvec_push(&data.prefixes, "");
173 for_each_fullref_in_prefixes(get_git_namespace(), data.prefixes.v,
174 send_ref, &data, 0);
175 packet_fflush(stdout);
176 strvec_clear(&data.prefixes);
177 strbuf_release(&data.buf);
178 return 0;
181 int ls_refs_advertise(struct repository *r, struct strbuf *value)
183 if (value) {
184 ensure_config_read();
185 if (advertise_unborn)
186 strbuf_addstr(value, "unborn");
189 return 1;