2 #include "repository.h"
11 * Check if one of the prefixes is a prefix of the ref.
12 * If no prefixes were provided, all refs match.
14 static int ref_match(const struct strvec *prefixes, const char *refname)
19 return 1; /* no restriction */
21 for (i = 0; i < prefixes->nr; i++) {
22 const char *prefix = prefixes->v[i];
24 if (starts_with(refname, prefix))
34 struct strvec prefixes;
37 static int send_ref(const char *refname, const struct object_id *oid,
38 int flag, void *cb_data)
40 struct ls_refs_data *data = cb_data;
41 const char *refname_nons = strip_namespace(refname);
42 struct strbuf refline = STRBUF_INIT;
44 if (ref_is_hidden(refname_nons, refname))
47 if (!ref_match(&data->prefixes, refname_nons))
50 strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
51 if (data->symrefs && flag & REF_ISSYMREF) {
52 struct object_id unused;
53 const char *symref_target = resolve_ref_unsafe(refname, 0,
58 die("'%s' is a symref but it is not?", refname);
60 strbuf_addf(&refline, " symref-target:%s",
61 strip_namespace(symref_target));
65 struct object_id peeled;
66 if (!peel_iterated_oid(oid, &peeled))
67 strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled));
70 strbuf_addch(&refline, '\n');
71 packet_write(1, refline.buf, refline.len);
73 strbuf_release(&refline);
77 static int ls_refs_config(const char *var, const char *value, void *data)
80 * We only serve fetches over v2 for now, so respect only "uploadpack"
81 * config. This may need to eventually be expanded to "receive", but we
82 * don't yet know how that information will be passed to ls-refs.
84 return parse_hide_refs_config(var, value, "uploadpack");
87 int ls_refs(struct repository *r, struct strvec *keys,
88 struct packet_reader *request)
90 struct ls_refs_data data;
92 memset(&data, 0, sizeof(data));
93 strvec_init(&data.prefixes);
95 git_config(ls_refs_config, NULL);
97 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
98 const char *arg = request->line;
101 if (!strcmp("peel", arg))
103 else if (!strcmp("symrefs", arg))
105 else if (skip_prefix(arg, "ref-prefix ", &out))
106 strvec_push(&data.prefixes, out);
109 if (request->status != PACKET_READ_FLUSH)
110 die(_("expected flush after ls-refs arguments"));
112 head_ref_namespaced(send_ref, &data);
113 if (!data.prefixes.nr)
114 strvec_push(&data.prefixes, "");
115 for_each_fullref_in_prefixes(get_git_namespace(), data.prefixes.v,
118 strvec_clear(&data.prefixes);