1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
8 #include "repository.h"
14 #include "string-list.h"
19 UNBORN_ADVERTISE
/* implies ALLOW */
20 } unborn_config(struct repository
*r
)
22 const char *str
= NULL
;
24 if (repo_config_get_string_tmp(r
, "lsrefs.unborn", &str
)) {
26 * If there is no such config, advertise and allow it by
29 return UNBORN_ADVERTISE
;
31 if (!strcmp(str
, "advertise")) {
32 return UNBORN_ADVERTISE
;
33 } else if (!strcmp(str
, "allow")) {
35 } else if (!strcmp(str
, "ignore")) {
38 die(_("invalid value for '%s': '%s'"),
39 "lsrefs.unborn", str
);
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.
48 #define TOO_MANY_PREFIXES 65536
51 * Check if one of the prefixes is a prefix of the ref.
52 * If no prefixes were provided, all refs match.
54 static int ref_match(const struct strvec
*prefixes
, const char *refname
)
59 return 1; /* no restriction */
61 for (i
= 0; i
< prefixes
->nr
; i
++) {
62 const char *prefix
= prefixes
->v
[i
];
64 if (starts_with(refname
, prefix
))
74 struct strvec prefixes
;
76 struct strvec hidden_refs
;
80 static int send_ref(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
81 int flag
, void *cb_data
)
83 struct ls_refs_data
*data
= cb_data
;
84 const char *refname_nons
= strip_namespace(refname
);
86 strbuf_reset(&data
->buf
);
88 if (ref_is_hidden(refname_nons
, refname
, &data
->hidden_refs
))
91 if (!ref_match(&data
->prefixes
, refname_nons
))
95 strbuf_addf(&data
->buf
, "%s %s", oid_to_hex(oid
), refname_nons
);
97 strbuf_addf(&data
->buf
, "unborn %s", refname_nons
);
98 if (data
->symrefs
&& flag
& REF_ISSYMREF
) {
99 struct object_id unused
;
100 const char *symref_target
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
107 die("'%s' is a symref but it is not?", refname
);
109 strbuf_addf(&data
->buf
, " symref-target:%s",
110 strip_namespace(symref_target
));
113 if (data
->peel
&& oid
) {
114 struct object_id peeled
;
115 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
116 strbuf_addf(&data
->buf
, " peeled:%s", oid_to_hex(&peeled
));
119 strbuf_addch(&data
->buf
, '\n');
120 packet_fwrite(stdout
, data
->buf
.buf
, data
->buf
.len
);
125 static void send_possibly_unborn_head(struct ls_refs_data
*data
)
127 struct strbuf namespaced
= STRBUF_INIT
;
128 struct object_id oid
;
132 strbuf_addf(&namespaced
, "%sHEAD", get_git_namespace());
133 if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository
), namespaced
.buf
, 0, &oid
, &flag
))
134 return; /* bad ref */
135 oid_is_null
= is_null_oid(&oid
);
137 (data
->unborn
&& data
->symrefs
&& (flag
& REF_ISSYMREF
)))
138 send_ref(namespaced
.buf
, NULL
, oid_is_null
? NULL
: &oid
, flag
, data
);
139 strbuf_release(&namespaced
);
142 static int ls_refs_config(const char *var
, const char *value
,
143 const struct config_context
*ctx UNUSED
,
146 struct ls_refs_data
*data
= cb_data
;
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.
152 return parse_hide_refs_config(var
, value
, "uploadpack", &data
->hidden_refs
);
155 int ls_refs(struct repository
*r
, struct packet_reader
*request
)
157 struct ls_refs_data data
;
159 memset(&data
, 0, sizeof(data
));
160 strvec_init(&data
.prefixes
);
161 strbuf_init(&data
.buf
, 0);
162 strvec_init(&data
.hidden_refs
);
164 git_config(ls_refs_config
, &data
);
166 while (packet_reader_read(request
) == PACKET_READ_NORMAL
) {
167 const char *arg
= request
->line
;
170 if (!strcmp("peel", arg
))
172 else if (!strcmp("symrefs", arg
))
174 else if (skip_prefix(arg
, "ref-prefix ", &out
)) {
175 if (data
.prefixes
.nr
< TOO_MANY_PREFIXES
)
176 strvec_push(&data
.prefixes
, out
);
178 else if (!strcmp("unborn", arg
))
179 data
.unborn
= !!unborn_config(r
);
181 die(_("unexpected line: '%s'"), arg
);
184 if (request
->status
!= PACKET_READ_FLUSH
)
185 die(_("expected flush after ls-refs arguments"));
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
192 if (data
.prefixes
.nr
>= TOO_MANY_PREFIXES
)
193 strvec_clear(&data
.prefixes
);
195 send_possibly_unborn_head(&data
);
196 if (!data
.prefixes
.nr
)
197 strvec_push(&data
.prefixes
, "");
198 refs_for_each_fullref_in_prefixes(get_main_ref_store(r
),
199 get_git_namespace(), data
.prefixes
.v
,
200 hidden_refs_to_excludes(&data
.hidden_refs
),
202 packet_fflush(stdout
);
203 strvec_clear(&data
.prefixes
);
204 strbuf_release(&data
.buf
);
205 strvec_clear(&data
.hidden_refs
);
209 int ls_refs_advertise(struct repository
*r
, struct strbuf
*value
)
211 if (value
&& unborn_config(r
) == UNBORN_ADVERTISE
)
212 strbuf_addstr(value
, "unborn");