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"
9 
10 static int config_read;
11 static int advertise_unborn;
12 static int allow_unborn;
13 
ensure_config_read(void)14 static 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 
43 /*
44  * If we see this many or more "ref-prefix" lines from the client, we consider
45  * it "too many" and will avoid using the prefix feature entirely.
46  */
47 #define TOO_MANY_PREFIXES 65536
48 
49 /*
50  * Check if one of the prefixes is a prefix of the ref.
51  * If no prefixes were provided, all refs match.
52  */
ref_match(const struct strvec * prefixes,const char * refname)53 static int ref_match(const struct strvec *prefixes, const char *refname)
54 {
55 	int i;
56 
57 	if (!prefixes->nr)
58 		return 1; /* no restriction */
59 
60 	for (i = 0; i < prefixes->nr; i++) {
61 		const char *prefix = prefixes->v[i];
62 
63 		if (starts_with(refname, prefix))
64 			return 1;
65 	}
66 
67 	return 0;
68 }
69 
70 struct ls_refs_data {
71 	unsigned peel;
72 	unsigned symrefs;
73 	struct strvec prefixes;
74 	struct strbuf buf;
75 	unsigned unborn : 1;
76 };
77 
send_ref(const char * refname,const struct object_id * oid,int flag,void * cb_data)78 static int send_ref(const char *refname, const struct object_id *oid,
79 		    int flag, void *cb_data)
80 {
81 	struct ls_refs_data *data = cb_data;
82 	const char *refname_nons = strip_namespace(refname);
83 
84 	strbuf_reset(&data->buf);
85 
86 	if (ref_is_hidden(refname_nons, refname))
87 		return 0;
88 
89 	if (!ref_match(&data->prefixes, refname_nons))
90 		return 0;
91 
92 	if (oid)
93 		strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
94 	else
95 		strbuf_addf(&data->buf, "unborn %s", refname_nons);
96 	if (data->symrefs && flag & REF_ISSYMREF) {
97 		struct object_id unused;
98 		const char *symref_target = resolve_ref_unsafe(refname, 0,
99 							       &unused,
100 							       &flag);
101 
102 		if (!symref_target)
103 			die("'%s' is a symref but it is not?", refname);
104 
105 		strbuf_addf(&data->buf, " symref-target:%s",
106 			    strip_namespace(symref_target));
107 	}
108 
109 	if (data->peel && oid) {
110 		struct object_id peeled;
111 		if (!peel_iterated_oid(oid, &peeled))
112 			strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
113 	}
114 
115 	strbuf_addch(&data->buf, '\n');
116 	packet_fwrite(stdout, data->buf.buf, data->buf.len);
117 
118 	return 0;
119 }
120 
send_possibly_unborn_head(struct ls_refs_data * data)121 static void send_possibly_unborn_head(struct ls_refs_data *data)
122 {
123 	struct strbuf namespaced = STRBUF_INIT;
124 	struct object_id oid;
125 	int flag;
126 	int oid_is_null;
127 
128 	strbuf_addf(&namespaced, "%sHEAD", get_git_namespace());
129 	if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag))
130 		return; /* bad ref */
131 	oid_is_null = is_null_oid(&oid);
132 	if (!oid_is_null ||
133 	    (data->unborn && data->symrefs && (flag & REF_ISSYMREF)))
134 		send_ref(namespaced.buf, oid_is_null ? NULL : &oid, flag, data);
135 	strbuf_release(&namespaced);
136 }
137 
ls_refs_config(const char * var,const char * value,void * data)138 static int ls_refs_config(const char *var, const char *value, void *data)
139 {
140 	/*
141 	 * We only serve fetches over v2 for now, so respect only "uploadpack"
142 	 * config. This may need to eventually be expanded to "receive", but we
143 	 * don't yet know how that information will be passed to ls-refs.
144 	 */
145 	return parse_hide_refs_config(var, value, "uploadpack");
146 }
147 
ls_refs(struct repository * r,struct packet_reader * request)148 int ls_refs(struct repository *r, struct packet_reader *request)
149 {
150 	struct ls_refs_data data;
151 
152 	memset(&data, 0, sizeof(data));
153 	strvec_init(&data.prefixes);
154 	strbuf_init(&data.buf, 0);
155 
156 	ensure_config_read();
157 	git_config(ls_refs_config, NULL);
158 
159 	while (packet_reader_read(request) == PACKET_READ_NORMAL) {
160 		const char *arg = request->line;
161 		const char *out;
162 
163 		if (!strcmp("peel", arg))
164 			data.peel = 1;
165 		else if (!strcmp("symrefs", arg))
166 			data.symrefs = 1;
167 		else if (skip_prefix(arg, "ref-prefix ", &out)) {
168 			if (data.prefixes.nr < TOO_MANY_PREFIXES)
169 				strvec_push(&data.prefixes, out);
170 		}
171 		else if (!strcmp("unborn", arg))
172 			data.unborn = allow_unborn;
173 		else
174 			die(_("unexpected line: '%s'"), arg);
175 	}
176 
177 	if (request->status != PACKET_READ_FLUSH)
178 		die(_("expected flush after ls-refs arguments"));
179 
180 	/*
181 	 * If we saw too many prefixes, we must avoid using them at all; as
182 	 * soon as we have any prefix, they are meant to form a comprehensive
183 	 * list.
184 	 */
185 	if (data.prefixes.nr >= TOO_MANY_PREFIXES)
186 		strvec_clear(&data.prefixes);
187 
188 	send_possibly_unborn_head(&data);
189 	if (!data.prefixes.nr)
190 		strvec_push(&data.prefixes, "");
191 	for_each_fullref_in_prefixes(get_git_namespace(), data.prefixes.v,
192 				     send_ref, &data);
193 	packet_fflush(stdout);
194 	strvec_clear(&data.prefixes);
195 	strbuf_release(&data.buf);
196 	return 0;
197 }
198 
ls_refs_advertise(struct repository * r,struct strbuf * value)199 int ls_refs_advertise(struct repository *r, struct strbuf *value)
200 {
201 	if (value) {
202 		ensure_config_read();
203 		if (advertise_unborn)
204 			strbuf_addstr(value, "unborn");
205 	}
206 
207 	return 1;
208 }
209