3 #include "repository.h"
10 #include "string-list.h"
12 static int config_read
;
13 static int advertise_unborn
;
14 static int allow_unborn
;
16 static void ensure_config_read(void)
18 const char *str
= NULL
;
23 if (repo_config_get_string_tmp(the_repository
, "lsrefs.unborn", &str
)) {
25 * If there is no such config, advertise and allow it by
31 if (!strcmp(str
, "advertise")) {
34 } else if (!strcmp(str
, "allow")) {
36 } else if (!strcmp(str
, "ignore")) {
39 die(_("invalid value for '%s': '%s'"),
40 "lsrefs.unborn", str
);
47 * If we see this many or more "ref-prefix" lines from the client, we consider
48 * it "too many" and will avoid using the prefix feature entirely.
50 #define TOO_MANY_PREFIXES 65536
53 * Check if one of the prefixes is a prefix of the ref.
54 * If no prefixes were provided, all refs match.
56 static int ref_match(const struct strvec
*prefixes
, const char *refname
)
61 return 1; /* no restriction */
63 for (i
= 0; i
< prefixes
->nr
; i
++) {
64 const char *prefix
= prefixes
->v
[i
];
66 if (starts_with(refname
, prefix
))
76 struct strvec prefixes
;
78 struct string_list hidden_refs
;
82 static int send_ref(const char *refname
, const struct object_id
*oid
,
83 int flag
, void *cb_data
)
85 struct ls_refs_data
*data
= cb_data
;
86 const char *refname_nons
= strip_namespace(refname
);
88 strbuf_reset(&data
->buf
);
90 if (ref_is_hidden(refname_nons
, refname
, &data
->hidden_refs
))
93 if (!ref_match(&data
->prefixes
, refname_nons
))
97 strbuf_addf(&data
->buf
, "%s %s", oid_to_hex(oid
), refname_nons
);
99 strbuf_addf(&data
->buf
, "unborn %s", refname_nons
);
100 if (data
->symrefs
&& flag
& REF_ISSYMREF
) {
101 struct object_id unused
;
102 const char *symref_target
= resolve_ref_unsafe(refname
, 0,
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(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 (!resolve_ref_unsafe(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
, oid_is_null
? NULL
: &oid
, flag
, data
);
139 strbuf_release(&namespaced
);
142 static int ls_refs_config(const char *var
, const char *value
,
145 struct ls_refs_data
*data
= cb_data
;
147 * We only serve fetches over v2 for now, so respect only "uploadpack"
148 * config. This may need to eventually be expanded to "receive", but we
149 * don't yet know how that information will be passed to ls-refs.
151 return parse_hide_refs_config(var
, value
, "uploadpack", &data
->hidden_refs
);
154 int ls_refs(struct repository
*r
, struct packet_reader
*request
)
156 struct ls_refs_data data
;
158 memset(&data
, 0, sizeof(data
));
159 strvec_init(&data
.prefixes
);
160 strbuf_init(&data
.buf
, 0);
161 string_list_init_dup(&data
.hidden_refs
);
163 ensure_config_read();
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
= allow_unborn
;
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
,
201 packet_fflush(stdout
);
202 strvec_clear(&data
.prefixes
);
203 strbuf_release(&data
.buf
);
204 string_list_clear(&data
.hidden_refs
, 0);
208 int ls_refs_advertise(struct repository
*r
, struct strbuf
*value
)
211 ensure_config_read();
212 if (advertise_unborn
)
213 strbuf_addstr(value
, "unborn");