]>
Commit | Line | Data |
---|---|---|
7bd9bcf3 MH |
1 | /* |
2 | * The backend-independent part of the reference module. | |
3 | */ | |
4 | ||
95fc7512 | 5 | #include "cache.h" |
7d4558c4 | 6 | #include "hashmap.h" |
697cc8ef | 7 | #include "lockfile.h" |
85023577 | 8 | #include "refs.h" |
4cb77009 | 9 | #include "refs/refs-internal.h" |
cf0adba7 JH |
10 | #include "object.h" |
11 | #include "tag.h" | |
5d0bc90e | 12 | #include "submodule.h" |
3581d793 | 13 | |
3dce444f RS |
14 | /* |
15 | * List of all available backends | |
16 | */ | |
17 | static struct ref_storage_be *refs_backends = &refs_be_files; | |
18 | ||
19 | static struct ref_storage_be *find_ref_storage_backend(const char *name) | |
20 | { | |
21 | struct ref_storage_be *be; | |
22 | for (be = refs_backends; be; be = be->next) | |
23 | if (!strcmp(be->name, name)) | |
24 | return be; | |
25 | return NULL; | |
26 | } | |
27 | ||
28 | int ref_storage_backend_exists(const char *name) | |
29 | { | |
30 | return find_ref_storage_backend(name) != NULL; | |
31 | } | |
32 | ||
bc5fd6d3 | 33 | /* |
dde8a902 DT |
34 | * How to handle various characters in refnames: |
35 | * 0: An acceptable character for refs | |
5e650228 JH |
36 | * 1: End-of-component |
37 | * 2: ., look for a preceding . to reject .. in refs | |
38 | * 3: {, look for a preceding @ to reject @{ in refs | |
53a8555e | 39 | * 4: A bad character: ASCII control characters, and |
cd377f45 JK |
40 | * ":", "?", "[", "\", "^", "~", SP, or TAB |
41 | * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set | |
dde8a902 DT |
42 | */ |
43 | static unsigned char refname_disposition[256] = { | |
5e650228 JH |
44 | 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
45 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
cd377f45 | 46 | 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1, |
5e650228 | 47 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, |
dde8a902 | 48 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
5e650228 JH |
49 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0, |
50 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
51 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4 | |
dde8a902 DT |
52 | }; |
53 | ||
54 | /* | |
55 | * Try to read one refname component from the front of refname. | |
56 | * Return the length of the component found, or -1 if the component is | |
57 | * not legal. It is legal if it is something reasonable to have under | |
58 | * ".git/refs/"; We do not like it if: | |
bc5fd6d3 MH |
59 | * |
60 | * - any path component of it begins with ".", or | |
61 | * - it has double dots "..", or | |
53a8555e | 62 | * - it has ASCII control characters, or |
cd377f45 JK |
63 | * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or |
64 | * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or | |
53a8555e JK |
65 | * - it ends with a "/", or |
66 | * - it ends with ".lock", or | |
67 | * - it contains a "@{" portion | |
bc5fd6d3 | 68 | */ |
cd377f45 | 69 | static int check_refname_component(const char *refname, int *flags) |
bc5fd6d3 MH |
70 | { |
71 | const char *cp; | |
72 | char last = '\0'; | |
73 | ||
74 | for (cp = refname; ; cp++) { | |
dde8a902 DT |
75 | int ch = *cp & 255; |
76 | unsigned char disp = refname_disposition[ch]; | |
77 | switch (disp) { | |
5e650228 | 78 | case 1: |
dde8a902 | 79 | goto out; |
5e650228 | 80 | case 2: |
dde8a902 DT |
81 | if (last == '.') |
82 | return -1; /* Refname contains "..". */ | |
83 | break; | |
5e650228 | 84 | case 3: |
dde8a902 DT |
85 | if (last == '@') |
86 | return -1; /* Refname contains "@{". */ | |
bc5fd6d3 | 87 | break; |
5e650228 | 88 | case 4: |
dde8a902 | 89 | return -1; |
cd377f45 JK |
90 | case 5: |
91 | if (!(*flags & REFNAME_REFSPEC_PATTERN)) | |
92 | return -1; /* refspec can't be a pattern */ | |
93 | ||
94 | /* | |
95 | * Unset the pattern flag so that we only accept | |
96 | * a single asterisk for one side of refspec. | |
97 | */ | |
98 | *flags &= ~ REFNAME_REFSPEC_PATTERN; | |
99 | break; | |
dde8a902 | 100 | } |
bc5fd6d3 MH |
101 | last = ch; |
102 | } | |
dde8a902 | 103 | out: |
bc5fd6d3 | 104 | if (cp == refname) |
dac529e4 | 105 | return 0; /* Component has zero length. */ |
f3cc52d8 JN |
106 | if (refname[0] == '.') |
107 | return -1; /* Component starts with '.'. */ | |
7108ad23 MH |
108 | if (cp - refname >= LOCK_SUFFIX_LEN && |
109 | !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) | |
bc5fd6d3 MH |
110 | return -1; /* Refname ends with ".lock". */ |
111 | return cp - refname; | |
112 | } | |
113 | ||
5e650228 | 114 | int check_refname_format(const char *refname, int flags) |
bc5fd6d3 MH |
115 | { |
116 | int component_len, component_count = 0; | |
117 | ||
9ba89f48 FC |
118 | if (!strcmp(refname, "@")) |
119 | /* Refname is a single character '@'. */ | |
120 | return -1; | |
121 | ||
bc5fd6d3 MH |
122 | while (1) { |
123 | /* We are at the start of a path component. */ | |
cd377f45 JK |
124 | component_len = check_refname_component(refname, &flags); |
125 | if (component_len <= 0) | |
126 | return -1; | |
127 | ||
bc5fd6d3 MH |
128 | component_count++; |
129 | if (refname[component_len] == '\0') | |
130 | break; | |
131 | /* Skip to next component. */ | |
132 | refname += component_len + 1; | |
133 | } | |
134 | ||
135 | if (refname[component_len - 1] == '.') | |
136 | return -1; /* Refname ends with '.'. */ | |
137 | if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2) | |
138 | return -1; /* Refname has only one component. */ | |
139 | return 0; | |
140 | } | |
141 | ||
4cb77009 | 142 | int refname_is_safe(const char *refname) |
d0f810f0 | 143 | { |
39950fef MH |
144 | const char *rest; |
145 | ||
146 | if (skip_prefix(refname, "refs/", &rest)) { | |
d0f810f0 RS |
147 | char *buf; |
148 | int result; | |
e40f3557 MH |
149 | size_t restlen = strlen(rest); |
150 | ||
151 | /* rest must not be empty, or start or end with "/" */ | |
152 | if (!restlen || *rest == '/' || rest[restlen - 1] == '/') | |
153 | return 0; | |
d0f810f0 | 154 | |
d0f810f0 RS |
155 | /* |
156 | * Does the refname try to escape refs/? | |
157 | * For example: refs/foo/../bar is safe but refs/foo/../../bar | |
158 | * is not. | |
159 | */ | |
e40f3557 MH |
160 | buf = xmallocz(restlen); |
161 | result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest); | |
d0f810f0 RS |
162 | free(buf); |
163 | return result; | |
164 | } | |
35db25c6 MH |
165 | |
166 | do { | |
d0f810f0 RS |
167 | if (!isupper(*refname) && *refname != '_') |
168 | return 0; | |
169 | refname++; | |
35db25c6 | 170 | } while (*refname); |
d0f810f0 RS |
171 | return 1; |
172 | } | |
173 | ||
7d2df051 NTND |
174 | char *refs_resolve_refdup(struct ref_store *refs, |
175 | const char *refname, int resolve_flags, | |
176 | unsigned char *sha1, int *flags) | |
177 | { | |
178 | const char *result; | |
179 | ||
180 | result = refs_resolve_ref_unsafe(refs, refname, resolve_flags, | |
181 | sha1, flags); | |
182 | return xstrdup_or_null(result); | |
183 | } | |
184 | ||
7bd9bcf3 MH |
185 | char *resolve_refdup(const char *refname, int resolve_flags, |
186 | unsigned char *sha1, int *flags) | |
e1e22e37 | 187 | { |
7d2df051 NTND |
188 | return refs_resolve_refdup(get_main_ref_store(), |
189 | refname, resolve_flags, | |
190 | sha1, flags); | |
cddc4258 MH |
191 | } |
192 | ||
7bd9bcf3 MH |
193 | /* The argument to filter_refs */ |
194 | struct ref_filter { | |
195 | const char *pattern; | |
196 | each_ref_fn *fn; | |
197 | void *cb_data; | |
198 | }; | |
432ad41e | 199 | |
7d2df051 NTND |
200 | int refs_read_ref_full(struct ref_store *refs, const char *refname, |
201 | int resolve_flags, unsigned char *sha1, int *flags) | |
732134ed | 202 | { |
7d2df051 | 203 | if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, sha1, flags)) |
7bd9bcf3 MH |
204 | return 0; |
205 | return -1; | |
732134ed MH |
206 | } |
207 | ||
7d2df051 NTND |
208 | int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags) |
209 | { | |
210 | return refs_read_ref_full(get_main_ref_store(), refname, | |
211 | resolve_flags, sha1, flags); | |
212 | } | |
213 | ||
7bd9bcf3 | 214 | int read_ref(const char *refname, unsigned char *sha1) |
cddc4258 | 215 | { |
7bd9bcf3 | 216 | return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL); |
c774aab9 JP |
217 | } |
218 | ||
7bd9bcf3 | 219 | int ref_exists(const char *refname) |
bc5fd6d3 | 220 | { |
7bd9bcf3 MH |
221 | unsigned char sha1[20]; |
222 | return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL); | |
bc5fd6d3 MH |
223 | } |
224 | ||
7bd9bcf3 MH |
225 | static int filter_refs(const char *refname, const struct object_id *oid, |
226 | int flags, void *data) | |
432ad41e | 227 | { |
7bd9bcf3 MH |
228 | struct ref_filter *filter = (struct ref_filter *)data; |
229 | ||
230 | if (wildmatch(filter->pattern, refname, 0, NULL)) | |
231 | return 0; | |
232 | return filter->fn(refname, oid, flags, filter->cb_data); | |
432ad41e MH |
233 | } |
234 | ||
7bd9bcf3 | 235 | enum peel_status peel_object(const unsigned char *name, unsigned char *sha1) |
c774aab9 | 236 | { |
7bd9bcf3 | 237 | struct object *o = lookup_unknown_object(name); |
c774aab9 | 238 | |
7bd9bcf3 MH |
239 | if (o->type == OBJ_NONE) { |
240 | int type = sha1_object_info(name, NULL); | |
241 | if (type < 0 || !object_as_type(o, type, 0)) | |
242 | return PEEL_INVALID; | |
243 | } | |
bc5fd6d3 | 244 | |
7bd9bcf3 MH |
245 | if (o->type != OBJ_TAG) |
246 | return PEEL_NON_TAG; | |
e1980c9d | 247 | |
7bd9bcf3 MH |
248 | o = deref_tag_noverify(o); |
249 | if (!o) | |
250 | return PEEL_INVALID; | |
251 | ||
844a9ce4 | 252 | hashcpy(sha1, o->oid.hash); |
7bd9bcf3 | 253 | return PEEL_PEELED; |
e1980c9d JH |
254 | } |
255 | ||
7bd9bcf3 MH |
256 | struct warn_if_dangling_data { |
257 | FILE *fp; | |
258 | const char *refname; | |
259 | const struct string_list *refnames; | |
260 | const char *msg_fmt; | |
261 | }; | |
bc5fd6d3 | 262 | |
7bd9bcf3 MH |
263 | static int warn_if_dangling_symref(const char *refname, const struct object_id *oid, |
264 | int flags, void *cb_data) | |
265 | { | |
266 | struct warn_if_dangling_data *d = cb_data; | |
267 | const char *resolves_to; | |
268 | struct object_id junk; | |
bc5fd6d3 | 269 | |
7bd9bcf3 MH |
270 | if (!(flags & REF_ISSYMREF)) |
271 | return 0; | |
bc5fd6d3 | 272 | |
7bd9bcf3 MH |
273 | resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL); |
274 | if (!resolves_to | |
275 | || (d->refname | |
276 | ? strcmp(resolves_to, d->refname) | |
277 | : !string_list_has_string(d->refnames, resolves_to))) { | |
278 | return 0; | |
279 | } | |
bc5fd6d3 | 280 | |
7bd9bcf3 MH |
281 | fprintf(d->fp, d->msg_fmt, refname); |
282 | fputc('\n', d->fp); | |
283 | return 0; | |
bc5fd6d3 MH |
284 | } |
285 | ||
7bd9bcf3 | 286 | void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) |
f348ac92 | 287 | { |
7bd9bcf3 MH |
288 | struct warn_if_dangling_data data; |
289 | ||
290 | data.fp = fp; | |
291 | data.refname = refname; | |
292 | data.refnames = NULL; | |
293 | data.msg_fmt = msg_fmt; | |
294 | for_each_rawref(warn_if_dangling_symref, &data); | |
f348ac92 MH |
295 | } |
296 | ||
7bd9bcf3 | 297 | void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames) |
432ad41e | 298 | { |
7bd9bcf3 | 299 | struct warn_if_dangling_data data; |
432ad41e | 300 | |
7bd9bcf3 MH |
301 | data.fp = fp; |
302 | data.refname = NULL; | |
303 | data.refnames = refnames; | |
304 | data.msg_fmt = msg_fmt; | |
305 | for_each_rawref(warn_if_dangling_symref, &data); | |
432ad41e MH |
306 | } |
307 | ||
7d2df051 NTND |
308 | int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
309 | { | |
310 | return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data); | |
311 | } | |
312 | ||
7bd9bcf3 | 313 | int for_each_tag_ref(each_ref_fn fn, void *cb_data) |
432ad41e | 314 | { |
7d2df051 | 315 | return refs_for_each_tag_ref(get_main_ref_store(), fn, cb_data); |
432ad41e MH |
316 | } |
317 | ||
7bd9bcf3 | 318 | int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
506a760d | 319 | { |
7d2df051 NTND |
320 | return refs_for_each_tag_ref(get_submodule_ref_store(submodule), |
321 | fn, cb_data); | |
322 | } | |
323 | ||
324 | int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) | |
325 | { | |
326 | return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data); | |
432ad41e MH |
327 | } |
328 | ||
7bd9bcf3 | 329 | int for_each_branch_ref(each_ref_fn fn, void *cb_data) |
432ad41e | 330 | { |
7d2df051 | 331 | return refs_for_each_branch_ref(get_main_ref_store(), fn, cb_data); |
432ad41e MH |
332 | } |
333 | ||
7bd9bcf3 | 334 | int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
202a56a9 | 335 | { |
7d2df051 NTND |
336 | return refs_for_each_branch_ref(get_submodule_ref_store(submodule), |
337 | fn, cb_data); | |
338 | } | |
339 | ||
340 | int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) | |
341 | { | |
342 | return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data); | |
202a56a9 | 343 | } |
432ad41e | 344 | |
7bd9bcf3 | 345 | int for_each_remote_ref(each_ref_fn fn, void *cb_data) |
e9c4c111 | 346 | { |
7d2df051 | 347 | return refs_for_each_remote_ref(get_main_ref_store(), fn, cb_data); |
202a56a9 MH |
348 | } |
349 | ||
7bd9bcf3 | 350 | int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
e9c4c111 | 351 | { |
7d2df051 NTND |
352 | return refs_for_each_remote_ref(get_submodule_ref_store(submodule), |
353 | fn, cb_data); | |
662428f4 | 354 | } |
c774aab9 | 355 | |
7bd9bcf3 MH |
356 | int head_ref_namespaced(each_ref_fn fn, void *cb_data) |
357 | { | |
358 | struct strbuf buf = STRBUF_INIT; | |
359 | int ret = 0; | |
360 | struct object_id oid; | |
361 | int flag; | |
c774aab9 | 362 | |
7bd9bcf3 MH |
363 | strbuf_addf(&buf, "%sHEAD", get_git_namespace()); |
364 | if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag)) | |
365 | ret = fn(buf.buf, &oid, flag, cb_data); | |
366 | strbuf_release(&buf); | |
c774aab9 | 367 | |
7bd9bcf3 | 368 | return ret; |
e9c4c111 | 369 | } |
c774aab9 | 370 | |
7bd9bcf3 MH |
371 | int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, |
372 | const char *prefix, void *cb_data) | |
662428f4 | 373 | { |
7bd9bcf3 MH |
374 | struct strbuf real_pattern = STRBUF_INIT; |
375 | struct ref_filter filter; | |
376 | int ret; | |
d08bae7e | 377 | |
59556548 | 378 | if (!prefix && !starts_with(pattern, "refs/")) |
d08bae7e | 379 | strbuf_addstr(&real_pattern, "refs/"); |
b09fe971 IL |
380 | else if (prefix) |
381 | strbuf_addstr(&real_pattern, prefix); | |
d08bae7e IL |
382 | strbuf_addstr(&real_pattern, pattern); |
383 | ||
894a9d33 | 384 | if (!has_glob_specials(pattern)) { |
9517e6b8 | 385 | /* Append implied '/' '*' if not present. */ |
00b6c178 | 386 | strbuf_complete(&real_pattern, '/'); |
d08bae7e IL |
387 | /* No need to check for '*', there is none. */ |
388 | strbuf_addch(&real_pattern, '*'); | |
389 | } | |
390 | ||
391 | filter.pattern = real_pattern.buf; | |
392 | filter.fn = fn; | |
393 | filter.cb_data = cb_data; | |
394 | ret = for_each_ref(filter_refs, &filter); | |
395 | ||
396 | strbuf_release(&real_pattern); | |
397 | return ret; | |
398 | } | |
399 | ||
b09fe971 IL |
400 | int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) |
401 | { | |
402 | return for_each_glob_ref_in(fn, pattern, NULL, cb_data); | |
403 | } | |
404 | ||
4577e483 | 405 | const char *prettify_refname(const char *name) |
a9c37a72 | 406 | { |
a9c37a72 | 407 | return name + ( |
59556548 CC |
408 | starts_with(name, "refs/heads/") ? 11 : |
409 | starts_with(name, "refs/tags/") ? 10 : | |
410 | starts_with(name, "refs/remotes/") ? 13 : | |
a9c37a72 DB |
411 | 0); |
412 | } | |
413 | ||
54457fe5 | 414 | static const char *ref_rev_parse_rules[] = { |
79803322 SP |
415 | "%.*s", |
416 | "refs/%.*s", | |
417 | "refs/tags/%.*s", | |
418 | "refs/heads/%.*s", | |
419 | "refs/remotes/%.*s", | |
420 | "refs/remotes/%.*s/HEAD", | |
421 | NULL | |
422 | }; | |
423 | ||
54457fe5 | 424 | int refname_match(const char *abbrev_name, const char *full_name) |
79803322 SP |
425 | { |
426 | const char **p; | |
427 | const int abbrev_name_len = strlen(abbrev_name); | |
428 | ||
54457fe5 | 429 | for (p = ref_rev_parse_rules; *p; p++) { |
79803322 SP |
430 | if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) { |
431 | return 1; | |
432 | } | |
433 | } | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
ff74f7f1 JH |
438 | /* |
439 | * *string and *len will only be substituted, and *string returned (for | |
440 | * later free()ing) if the string passed in is a magic short-hand form | |
441 | * to name a branch. | |
442 | */ | |
443 | static char *substitute_branch_name(const char **string, int *len) | |
444 | { | |
445 | struct strbuf buf = STRBUF_INIT; | |
0e9f62da | 446 | int ret = interpret_branch_name(*string, *len, &buf, 0); |
ff74f7f1 JH |
447 | |
448 | if (ret == *len) { | |
449 | size_t size; | |
450 | *string = strbuf_detach(&buf, &size); | |
451 | *len = size; | |
452 | return (char *)*string; | |
453 | } | |
454 | ||
455 | return NULL; | |
456 | } | |
457 | ||
458 | int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref) | |
459 | { | |
460 | char *last_branch = substitute_branch_name(&str, &len); | |
41da7111 NTND |
461 | int refs_found = expand_ref(str, len, sha1, ref); |
462 | free(last_branch); | |
463 | return refs_found; | |
464 | } | |
465 | ||
466 | int expand_ref(const char *str, int len, unsigned char *sha1, char **ref) | |
467 | { | |
ff74f7f1 JH |
468 | const char **p, *r; |
469 | int refs_found = 0; | |
470 | ||
471 | *ref = NULL; | |
472 | for (p = ref_rev_parse_rules; *p; p++) { | |
473 | char fullref[PATH_MAX]; | |
474 | unsigned char sha1_from_ref[20]; | |
475 | unsigned char *this_result; | |
476 | int flag; | |
477 | ||
478 | this_result = refs_found ? sha1_from_ref : sha1; | |
479 | mksnpath(fullref, sizeof(fullref), *p, len, str); | |
7695d118 RS |
480 | r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING, |
481 | this_result, &flag); | |
ff74f7f1 JH |
482 | if (r) { |
483 | if (!refs_found++) | |
484 | *ref = xstrdup(r); | |
485 | if (!warn_ambiguous_refs) | |
486 | break; | |
55956350 | 487 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) { |
ff74f7f1 | 488 | warning("ignoring dangling symref %s.", fullref); |
55956350 JH |
489 | } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) { |
490 | warning("ignoring broken ref %s.", fullref); | |
491 | } | |
ff74f7f1 | 492 | } |
ff74f7f1 JH |
493 | return refs_found; |
494 | } | |
495 | ||
496 | int dwim_log(const char *str, int len, unsigned char *sha1, char **log) | |
497 | { | |
498 | char *last_branch = substitute_branch_name(&str, &len); | |
499 | const char **p; | |
500 | int logs_found = 0; | |
501 | ||
502 | *log = NULL; | |
503 | for (p = ref_rev_parse_rules; *p; p++) { | |
ff74f7f1 JH |
504 | unsigned char hash[20]; |
505 | char path[PATH_MAX]; | |
506 | const char *ref, *it; | |
507 | ||
508 | mksnpath(path, sizeof(path), *p, len, str); | |
7695d118 RS |
509 | ref = resolve_ref_unsafe(path, RESOLVE_REF_READING, |
510 | hash, NULL); | |
ff74f7f1 JH |
511 | if (!ref) |
512 | continue; | |
4da58835 | 513 | if (reflog_exists(path)) |
ff74f7f1 | 514 | it = path; |
4da58835 | 515 | else if (strcmp(ref, path) && reflog_exists(ref)) |
ff74f7f1 JH |
516 | it = ref; |
517 | else | |
518 | continue; | |
519 | if (!logs_found++) { | |
520 | *log = xstrdup(it); | |
521 | hashcpy(sha1, hash); | |
522 | } | |
7bd9bcf3 MH |
523 | if (!warn_ambiguous_refs) |
524 | break; | |
c0277d15 | 525 | } |
7bd9bcf3 MH |
526 | free(last_branch); |
527 | return logs_found; | |
2ddb5d17 BK |
528 | } |
529 | ||
266b1827 DT |
530 | static int is_per_worktree_ref(const char *refname) |
531 | { | |
ce414b33 DT |
532 | return !strcmp(refname, "HEAD") || |
533 | starts_with(refname, "refs/bisect/"); | |
266b1827 DT |
534 | } |
535 | ||
536 | static int is_pseudoref_syntax(const char *refname) | |
537 | { | |
538 | const char *c; | |
539 | ||
540 | for (c = refname; *c; c++) { | |
541 | if (!isupper(*c) && *c != '-' && *c != '_') | |
542 | return 0; | |
543 | } | |
544 | ||
545 | return 1; | |
546 | } | |
547 | ||
548 | enum ref_type ref_type(const char *refname) | |
549 | { | |
550 | if (is_per_worktree_ref(refname)) | |
551 | return REF_TYPE_PER_WORKTREE; | |
552 | if (is_pseudoref_syntax(refname)) | |
553 | return REF_TYPE_PSEUDOREF; | |
554 | return REF_TYPE_NORMAL; | |
555 | } | |
556 | ||
74ec19d4 DT |
557 | static int write_pseudoref(const char *pseudoref, const unsigned char *sha1, |
558 | const unsigned char *old_sha1, struct strbuf *err) | |
559 | { | |
560 | const char *filename; | |
561 | int fd; | |
562 | static struct lock_file lock; | |
563 | struct strbuf buf = STRBUF_INIT; | |
564 | int ret = -1; | |
565 | ||
566 | strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1)); | |
567 | ||
568 | filename = git_path("%s", pseudoref); | |
569 | fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR); | |
570 | if (fd < 0) { | |
0568c8e9 | 571 | strbuf_addf(err, "could not open '%s' for writing: %s", |
74ec19d4 DT |
572 | filename, strerror(errno)); |
573 | return -1; | |
574 | } | |
575 | ||
576 | if (old_sha1) { | |
577 | unsigned char actual_old_sha1[20]; | |
2c3aed13 DT |
578 | |
579 | if (read_ref(pseudoref, actual_old_sha1)) | |
580 | die("could not read ref '%s'", pseudoref); | |
74ec19d4 | 581 | if (hashcmp(actual_old_sha1, old_sha1)) { |
0568c8e9 | 582 | strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref); |
74ec19d4 DT |
583 | rollback_lock_file(&lock); |
584 | goto done; | |
585 | } | |
586 | } | |
587 | ||
588 | if (write_in_full(fd, buf.buf, buf.len) != buf.len) { | |
0568c8e9 | 589 | strbuf_addf(err, "could not write to '%s'", filename); |
74ec19d4 DT |
590 | rollback_lock_file(&lock); |
591 | goto done; | |
592 | } | |
593 | ||
594 | commit_lock_file(&lock); | |
595 | ret = 0; | |
596 | done: | |
597 | strbuf_release(&buf); | |
598 | return ret; | |
599 | } | |
600 | ||
601 | static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1) | |
602 | { | |
603 | static struct lock_file lock; | |
604 | const char *filename; | |
605 | ||
606 | filename = git_path("%s", pseudoref); | |
607 | ||
608 | if (old_sha1 && !is_null_sha1(old_sha1)) { | |
609 | int fd; | |
610 | unsigned char actual_old_sha1[20]; | |
611 | ||
612 | fd = hold_lock_file_for_update(&lock, filename, | |
613 | LOCK_DIE_ON_ERROR); | |
614 | if (fd < 0) | |
615 | die_errno(_("Could not open '%s' for writing"), filename); | |
2c3aed13 DT |
616 | if (read_ref(pseudoref, actual_old_sha1)) |
617 | die("could not read ref '%s'", pseudoref); | |
74ec19d4 DT |
618 | if (hashcmp(actual_old_sha1, old_sha1)) { |
619 | warning("Unexpected sha1 when deleting %s", pseudoref); | |
620 | rollback_lock_file(&lock); | |
621 | return -1; | |
a4c653df | 622 | } |
74ec19d4 DT |
623 | |
624 | unlink(filename); | |
625 | rollback_lock_file(&lock); | |
626 | } else { | |
627 | unlink(filename); | |
4bd18c43 | 628 | } |
a4c653df | 629 | |
4bd18c43 | 630 | return 0; |
95fc7512 | 631 | } |
d556fae2 | 632 | |
755b49ae KM |
633 | int delete_ref(const char *msg, const char *refname, |
634 | const unsigned char *old_sha1, unsigned int flags) | |
41b625b0 | 635 | { |
7521cc46 | 636 | struct ref_transaction *transaction; |
a4c653df | 637 | struct strbuf err = STRBUF_INIT; |
8b5157e4 | 638 | |
74ec19d4 | 639 | if (ref_type(refname) == REF_TYPE_PSEUDOREF) |
080cc646 | 640 | return delete_pseudoref(refname, old_sha1); |
d48744d1 | 641 | |
7521cc46 RS |
642 | transaction = ref_transaction_begin(&err); |
643 | if (!transaction || | |
fc67a082 | 644 | ref_transaction_delete(transaction, refname, old_sha1, |
755b49ae | 645 | flags, msg, &err) || |
db7516ab | 646 | ref_transaction_commit(transaction, &err)) { |
7521cc46 RS |
647 | error("%s", err.buf); |
648 | ref_transaction_free(transaction); | |
649 | strbuf_release(&err); | |
c0277d15 | 650 | return 1; |
41b625b0 | 651 | } |
7bd9bcf3 MH |
652 | ref_transaction_free(transaction); |
653 | strbuf_release(&err); | |
b531394d BC |
654 | return 0; |
655 | } | |
41b625b0 | 656 | |
4cb77009 | 657 | int copy_reflog_msg(char *buf, const char *msg) |
0ec29a47 JH |
658 | { |
659 | char *cp = buf; | |
660 | char c; | |
661 | int wasspace = 1; | |
8b5157e4 | 662 | |
0ec29a47 JH |
663 | *cp++ = '\t'; |
664 | while ((c = *msg++)) { | |
665 | if (wasspace && isspace(c)) | |
666 | continue; | |
667 | wasspace = isspace(c); | |
668 | if (wasspace) | |
669 | c = ' '; | |
670 | *cp++ = c; | |
a4c653df | 671 | } |
0ec29a47 JH |
672 | while (buf < cp && isspace(cp[-1])) |
673 | cp--; | |
674 | *cp++ = '\n'; | |
675 | return cp - buf; | |
676 | } | |
8b5157e4 | 677 | |
4cb77009 | 678 | int should_autocreate_reflog(const char *refname) |
4e2bef57 | 679 | { |
341fb286 CW |
680 | switch (log_all_ref_updates) { |
681 | case LOG_REFS_ALWAYS: | |
682 | return 1; | |
683 | case LOG_REFS_NORMAL: | |
684 | return starts_with(refname, "refs/heads/") || | |
685 | starts_with(refname, "refs/remotes/") || | |
686 | starts_with(refname, "refs/notes/") || | |
687 | !strcmp(refname, "HEAD"); | |
688 | default: | |
4e2bef57 | 689 | return 0; |
341fb286 | 690 | } |
4e2bef57 DT |
691 | } |
692 | ||
e7e0f26e | 693 | int is_branch(const char *refname) |
c3b0dec5 | 694 | { |
59556548 | 695 | return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/"); |
41b625b0 NP |
696 | } |
697 | ||
4207ed28 RS |
698 | struct read_ref_at_cb { |
699 | const char *refname; | |
700 | unsigned long at_time; | |
701 | int cnt; | |
702 | int reccnt; | |
703 | unsigned char *sha1; | |
704 | int found_it; | |
705 | ||
706 | unsigned char osha1[20]; | |
707 | unsigned char nsha1[20]; | |
708 | int tz; | |
709 | unsigned long date; | |
710 | char **msg; | |
711 | unsigned long *cutoff_time; | |
712 | int *cutoff_tz; | |
713 | int *cutoff_cnt; | |
714 | }; | |
715 | ||
9461d272 | 716 | static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid, |
4207ed28 RS |
717 | const char *email, unsigned long timestamp, int tz, |
718 | const char *message, void *cb_data) | |
719 | { | |
720 | struct read_ref_at_cb *cb = cb_data; | |
721 | ||
722 | cb->reccnt++; | |
723 | cb->tz = tz; | |
724 | cb->date = timestamp; | |
725 | ||
726 | if (timestamp <= cb->at_time || cb->cnt == 0) { | |
727 | if (cb->msg) | |
728 | *cb->msg = xstrdup(message); | |
729 | if (cb->cutoff_time) | |
730 | *cb->cutoff_time = timestamp; | |
731 | if (cb->cutoff_tz) | |
732 | *cb->cutoff_tz = tz; | |
733 | if (cb->cutoff_cnt) | |
734 | *cb->cutoff_cnt = cb->reccnt - 1; | |
735 | /* | |
736 | * we have not yet updated cb->[n|o]sha1 so they still | |
737 | * hold the values for the previous record. | |
738 | */ | |
739 | if (!is_null_sha1(cb->osha1)) { | |
9461d272 | 740 | hashcpy(cb->sha1, noid->hash); |
741 | if (hashcmp(cb->osha1, noid->hash)) | |
4207ed28 | 742 | warning("Log for ref %s has gap after %s.", |
a5481a6c | 743 | cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); |
4207ed28 RS |
744 | } |
745 | else if (cb->date == cb->at_time) | |
9461d272 | 746 | hashcpy(cb->sha1, noid->hash); |
747 | else if (hashcmp(noid->hash, cb->sha1)) | |
4207ed28 RS |
748 | warning("Log for ref %s unexpectedly ended on %s.", |
749 | cb->refname, show_date(cb->date, cb->tz, | |
a5481a6c | 750 | DATE_MODE(RFC2822))); |
9461d272 | 751 | hashcpy(cb->osha1, ooid->hash); |
752 | hashcpy(cb->nsha1, noid->hash); | |
4207ed28 RS |
753 | cb->found_it = 1; |
754 | return 1; | |
755 | } | |
9461d272 | 756 | hashcpy(cb->osha1, ooid->hash); |
757 | hashcpy(cb->nsha1, noid->hash); | |
4207ed28 RS |
758 | if (cb->cnt > 0) |
759 | cb->cnt--; | |
760 | return 0; | |
761 | } | |
762 | ||
9461d272 | 763 | static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid, |
4207ed28 RS |
764 | const char *email, unsigned long timestamp, |
765 | int tz, const char *message, void *cb_data) | |
766 | { | |
767 | struct read_ref_at_cb *cb = cb_data; | |
768 | ||
769 | if (cb->msg) | |
770 | *cb->msg = xstrdup(message); | |
771 | if (cb->cutoff_time) | |
772 | *cb->cutoff_time = timestamp; | |
773 | if (cb->cutoff_tz) | |
774 | *cb->cutoff_tz = tz; | |
775 | if (cb->cutoff_cnt) | |
776 | *cb->cutoff_cnt = cb->reccnt; | |
9461d272 | 777 | hashcpy(cb->sha1, ooid->hash); |
4207ed28 | 778 | if (is_null_sha1(cb->sha1)) |
9461d272 | 779 | hashcpy(cb->sha1, noid->hash); |
4207ed28 RS |
780 | /* We just want the first entry */ |
781 | return 1; | |
16d7cc90 JH |
782 | } |
783 | ||
c41a87dd | 784 | int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt, |
dfefa935 MH |
785 | unsigned char *sha1, char **msg, |
786 | unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt) | |
d556fae2 | 787 | { |
4207ed28 | 788 | struct read_ref_at_cb cb; |
d556fae2 | 789 | |
4207ed28 RS |
790 | memset(&cb, 0, sizeof(cb)); |
791 | cb.refname = refname; | |
792 | cb.at_time = at_time; | |
793 | cb.cnt = cnt; | |
794 | cb.msg = msg; | |
795 | cb.cutoff_time = cutoff_time; | |
796 | cb.cutoff_tz = cutoff_tz; | |
797 | cb.cutoff_cnt = cutoff_cnt; | |
798 | cb.sha1 = sha1; | |
799 | ||
800 | for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb); | |
801 | ||
c41a87dd DA |
802 | if (!cb.reccnt) { |
803 | if (flags & GET_SHA1_QUIETLY) | |
804 | exit(128); | |
805 | else | |
806 | die("Log for %s is empty.", refname); | |
807 | } | |
4207ed28 RS |
808 | if (cb.found_it) |
809 | return 0; | |
810 | ||
811 | for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb); | |
d556fae2 | 812 | |
16d7cc90 | 813 | return 1; |
d556fae2 | 814 | } |
2ff81662 | 815 | |
93a644ea | 816 | struct ref_transaction *ref_transaction_begin(struct strbuf *err) |
caa4046c | 817 | { |
5a603b04 JN |
818 | assert(err); |
819 | ||
caa4046c MH |
820 | return xcalloc(1, sizeof(struct ref_transaction)); |
821 | } | |
822 | ||
026bd1d3 | 823 | void ref_transaction_free(struct ref_transaction *transaction) |
caa4046c MH |
824 | { |
825 | int i; | |
826 | ||
1b07255c RS |
827 | if (!transaction) |
828 | return; | |
829 | ||
db7516ab RS |
830 | for (i = 0; i < transaction->nr; i++) { |
831 | free(transaction->updates[i]->msg); | |
88615910 | 832 | free(transaction->updates[i]); |
db7516ab | 833 | } |
caa4046c MH |
834 | free(transaction->updates); |
835 | free(transaction); | |
836 | } | |
837 | ||
71564516 MH |
838 | struct ref_update *ref_transaction_add_update( |
839 | struct ref_transaction *transaction, | |
840 | const char *refname, unsigned int flags, | |
841 | const unsigned char *new_sha1, | |
842 | const unsigned char *old_sha1, | |
843 | const char *msg) | |
caa4046c | 844 | { |
96ffc06f | 845 | struct ref_update *update; |
71564516 MH |
846 | |
847 | if (transaction->state != REF_TRANSACTION_OPEN) | |
848 | die("BUG: update called for transaction that is not open"); | |
849 | ||
850 | if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF)) | |
851 | die("BUG: REF_ISPRUNING set without REF_NODEREF"); | |
852 | ||
96ffc06f | 853 | FLEX_ALLOC_STR(update, refname, refname); |
caa4046c MH |
854 | ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); |
855 | transaction->updates[transaction->nr++] = update; | |
71564516 MH |
856 | |
857 | update->flags = flags; | |
858 | ||
859 | if (flags & REF_HAVE_NEW) | |
860 | hashcpy(update->new_sha1, new_sha1); | |
861 | if (flags & REF_HAVE_OLD) | |
862 | hashcpy(update->old_sha1, old_sha1); | |
13092a91 | 863 | update->msg = xstrdup_or_null(msg); |
caa4046c MH |
864 | return update; |
865 | } | |
866 | ||
8e34800e RS |
867 | int ref_transaction_update(struct ref_transaction *transaction, |
868 | const char *refname, | |
869 | const unsigned char *new_sha1, | |
870 | const unsigned char *old_sha1, | |
1d147bdf | 871 | unsigned int flags, const char *msg, |
8e34800e | 872 | struct strbuf *err) |
caa4046c | 873 | { |
5a603b04 JN |
874 | assert(err); |
875 | ||
8a679de6 MH |
876 | if ((new_sha1 && !is_null_sha1(new_sha1)) ? |
877 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) : | |
878 | !refname_is_safe(refname)) { | |
0568c8e9 | 879 | strbuf_addf(err, "refusing to update ref with bad name '%s'", |
d0f810f0 RS |
880 | refname); |
881 | return -1; | |
882 | } | |
883 | ||
71564516 MH |
884 | flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0); |
885 | ||
886 | ref_transaction_add_update(transaction, refname, flags, | |
887 | new_sha1, old_sha1, msg); | |
8e34800e | 888 | return 0; |
caa4046c MH |
889 | } |
890 | ||
b416af5b RS |
891 | int ref_transaction_create(struct ref_transaction *transaction, |
892 | const char *refname, | |
893 | const unsigned char *new_sha1, | |
fec14ec3 | 894 | unsigned int flags, const char *msg, |
b416af5b | 895 | struct strbuf *err) |
caa4046c | 896 | { |
f04c5b55 MH |
897 | if (!new_sha1 || is_null_sha1(new_sha1)) |
898 | die("BUG: create called without valid new_sha1"); | |
bc9f2925 | 899 | return ref_transaction_update(transaction, refname, new_sha1, |
1d147bdf | 900 | null_sha1, flags, msg, err); |
caa4046c MH |
901 | } |
902 | ||
8c8bdc0d RS |
903 | int ref_transaction_delete(struct ref_transaction *transaction, |
904 | const char *refname, | |
905 | const unsigned char *old_sha1, | |
fb5a6bb6 | 906 | unsigned int flags, const char *msg, |
8c8bdc0d | 907 | struct strbuf *err) |
caa4046c | 908 | { |
60294596 MH |
909 | if (old_sha1 && is_null_sha1(old_sha1)) |
910 | die("BUG: delete called with old_sha1 set to zeros"); | |
1d147bdf | 911 | return ref_transaction_update(transaction, refname, |
fb5a6bb6 | 912 | null_sha1, old_sha1, |
1d147bdf | 913 | flags, msg, err); |
caa4046c MH |
914 | } |
915 | ||
16180334 MH |
916 | int ref_transaction_verify(struct ref_transaction *transaction, |
917 | const char *refname, | |
918 | const unsigned char *old_sha1, | |
919 | unsigned int flags, | |
920 | struct strbuf *err) | |
921 | { | |
922 | if (!old_sha1) | |
923 | die("BUG: verify called with old_sha1 set to NULL"); | |
924 | return ref_transaction_update(transaction, refname, | |
925 | NULL, old_sha1, | |
926 | flags, NULL, err); | |
927 | } | |
928 | ||
8f6dc7e3 | 929 | int update_ref_oid(const char *msg, const char *refname, |
930 | const struct object_id *new_oid, const struct object_id *old_oid, | |
931 | unsigned int flags, enum action_on_err onerr) | |
932 | { | |
933 | return update_ref(msg, refname, new_oid ? new_oid->hash : NULL, | |
934 | old_oid ? old_oid->hash : NULL, flags, onerr); | |
935 | } | |
936 | ||
4b7b520b MH |
937 | int update_ref(const char *msg, const char *refname, |
938 | const unsigned char *new_sha1, const unsigned char *old_sha1, | |
fec14ec3 | 939 | unsigned int flags, enum action_on_err onerr) |
4738a333 | 940 | { |
74ec19d4 | 941 | struct ref_transaction *t = NULL; |
b4d75ac1 | 942 | struct strbuf err = STRBUF_INIT; |
74ec19d4 | 943 | int ret = 0; |
b4d75ac1 | 944 | |
74ec19d4 DT |
945 | if (ref_type(refname) == REF_TYPE_PSEUDOREF) { |
946 | ret = write_pseudoref(refname, new_sha1, old_sha1, &err); | |
947 | } else { | |
948 | t = ref_transaction_begin(&err); | |
949 | if (!t || | |
950 | ref_transaction_update(t, refname, new_sha1, old_sha1, | |
951 | flags, msg, &err) || | |
952 | ref_transaction_commit(t, &err)) { | |
953 | ret = 1; | |
954 | ref_transaction_free(t); | |
955 | } | |
956 | } | |
957 | if (ret) { | |
b4d75ac1 RS |
958 | const char *str = "update_ref failed for ref '%s': %s"; |
959 | ||
b4d75ac1 RS |
960 | switch (onerr) { |
961 | case UPDATE_REFS_MSG_ON_ERR: | |
962 | error(str, refname, err.buf); | |
963 | break; | |
964 | case UPDATE_REFS_DIE_ON_ERR: | |
965 | die(str, refname, err.buf); | |
966 | break; | |
967 | case UPDATE_REFS_QUIET_ON_ERR: | |
968 | break; | |
969 | } | |
970 | strbuf_release(&err); | |
4738a333 | 971 | return 1; |
b4d75ac1 RS |
972 | } |
973 | strbuf_release(&err); | |
74ec19d4 DT |
974 | if (t) |
975 | ref_transaction_free(t); | |
b4d75ac1 | 976 | return 0; |
4738a333 BK |
977 | } |
978 | ||
dfefa935 | 979 | char *shorten_unambiguous_ref(const char *refname, int strict) |
7c2b3029 JK |
980 | { |
981 | int i; | |
982 | static char **scanf_fmts; | |
983 | static int nr_rules; | |
984 | char *short_name; | |
985 | ||
7c2b3029 | 986 | if (!nr_rules) { |
4346663a MH |
987 | /* |
988 | * Pre-generate scanf formats from ref_rev_parse_rules[]. | |
989 | * Generate a format suitable for scanf from a | |
990 | * ref_rev_parse_rules rule by interpolating "%s" at the | |
991 | * location of the "%.*s". | |
992 | */ | |
7c2b3029 | 993 | size_t total_len = 0; |
84d5633f | 994 | size_t offset = 0; |
7c2b3029 JK |
995 | |
996 | /* the rule list is NULL terminated, count them first */ | |
a4165851 | 997 | for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++) |
7902fe03 MH |
998 | /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */ |
999 | total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1; | |
7c2b3029 | 1000 | |
50492f7b | 1001 | scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len)); |
7c2b3029 | 1002 | |
84d5633f | 1003 | offset = 0; |
7c2b3029 | 1004 | for (i = 0; i < nr_rules; i++) { |
4346663a | 1005 | assert(offset < total_len); |
84d5633f | 1006 | scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset; |
4346663a MH |
1007 | offset += snprintf(scanf_fmts[i], total_len - offset, |
1008 | ref_rev_parse_rules[i], 2, "%s") + 1; | |
7c2b3029 JK |
1009 | } |
1010 | } | |
1011 | ||
1012 | /* bail out if there are no rules */ | |
1013 | if (!nr_rules) | |
dfefa935 | 1014 | return xstrdup(refname); |
7c2b3029 | 1015 | |
dfefa935 MH |
1016 | /* buffer for scanf result, at most refname must fit */ |
1017 | short_name = xstrdup(refname); | |
7c2b3029 JK |
1018 | |
1019 | /* skip first rule, it will always match */ | |
1020 | for (i = nr_rules - 1; i > 0 ; --i) { | |
1021 | int j; | |
6e7b3309 | 1022 | int rules_to_fail = i; |
7c2b3029 JK |
1023 | int short_name_len; |
1024 | ||
dfefa935 | 1025 | if (1 != sscanf(refname, scanf_fmts[i], short_name)) |
7c2b3029 JK |
1026 | continue; |
1027 | ||
1028 | short_name_len = strlen(short_name); | |
1029 | ||
6e7b3309 BW |
1030 | /* |
1031 | * in strict mode, all (except the matched one) rules | |
1032 | * must fail to resolve to a valid non-ambiguous ref | |
1033 | */ | |
1034 | if (strict) | |
1035 | rules_to_fail = nr_rules; | |
1036 | ||
7c2b3029 JK |
1037 | /* |
1038 | * check if the short name resolves to a valid ref, | |
1039 | * but use only rules prior to the matched one | |
1040 | */ | |
6e7b3309 | 1041 | for (j = 0; j < rules_to_fail; j++) { |
7c2b3029 | 1042 | const char *rule = ref_rev_parse_rules[j]; |
7c2b3029 JK |
1043 | char refname[PATH_MAX]; |
1044 | ||
6e7b3309 BW |
1045 | /* skip matched rule */ |
1046 | if (i == j) | |
1047 | continue; | |
1048 | ||
7c2b3029 JK |
1049 | /* |
1050 | * the short name is ambiguous, if it resolves | |
1051 | * (with this previous rule) to a valid ref | |
1052 | * read_ref() returns 0 on success | |
1053 | */ | |
1054 | mksnpath(refname, sizeof(refname), | |
1055 | rule, short_name_len, short_name); | |
c6893323 | 1056 | if (ref_exists(refname)) |
7c2b3029 JK |
1057 | break; |
1058 | } | |
1059 | ||
1060 | /* | |
1061 | * short name is non-ambiguous if all previous rules | |
1062 | * haven't resolved to a valid ref | |
1063 | */ | |
6e7b3309 | 1064 | if (j == rules_to_fail) |
7c2b3029 JK |
1065 | return short_name; |
1066 | } | |
1067 | ||
1068 | free(short_name); | |
dfefa935 | 1069 | return xstrdup(refname); |
7c2b3029 | 1070 | } |
daebaa78 JH |
1071 | |
1072 | static struct string_list *hide_refs; | |
1073 | ||
1074 | int parse_hide_refs_config(const char *var, const char *value, const char *section) | |
1075 | { | |
ad8c7cda | 1076 | const char *key; |
daebaa78 | 1077 | if (!strcmp("transfer.hiderefs", var) || |
ad8c7cda JK |
1078 | (!parse_config_key(var, section, NULL, NULL, &key) && |
1079 | !strcmp(key, "hiderefs"))) { | |
daebaa78 JH |
1080 | char *ref; |
1081 | int len; | |
1082 | ||
1083 | if (!value) | |
1084 | return config_error_nonbool(var); | |
1085 | ref = xstrdup(value); | |
1086 | len = strlen(ref); | |
1087 | while (len && ref[len - 1] == '/') | |
1088 | ref[--len] = '\0'; | |
1089 | if (!hide_refs) { | |
1090 | hide_refs = xcalloc(1, sizeof(*hide_refs)); | |
1091 | hide_refs->strdup_strings = 1; | |
1092 | } | |
1093 | string_list_append(hide_refs, ref); | |
1094 | } | |
1095 | return 0; | |
1096 | } | |
1097 | ||
78a766ab | 1098 | int ref_is_hidden(const char *refname, const char *refname_full) |
daebaa78 | 1099 | { |
2bc31d16 | 1100 | int i; |
daebaa78 JH |
1101 | |
1102 | if (!hide_refs) | |
1103 | return 0; | |
2bc31d16 JK |
1104 | for (i = hide_refs->nr - 1; i >= 0; i--) { |
1105 | const char *match = hide_refs->items[i].string; | |
78a766ab | 1106 | const char *subject; |
2bc31d16 | 1107 | int neg = 0; |
daebaa78 | 1108 | int len; |
2bc31d16 JK |
1109 | |
1110 | if (*match == '!') { | |
1111 | neg = 1; | |
1112 | match++; | |
1113 | } | |
1114 | ||
78a766ab LF |
1115 | if (*match == '^') { |
1116 | subject = refname_full; | |
1117 | match++; | |
1118 | } else { | |
1119 | subject = refname; | |
1120 | } | |
1121 | ||
1122 | /* refname can be NULL when namespaces are used. */ | |
1123 | if (!subject || !starts_with(subject, match)) | |
daebaa78 | 1124 | continue; |
2bc31d16 | 1125 | len = strlen(match); |
78a766ab | 1126 | if (!subject[len] || subject[len] == '/') |
2bc31d16 | 1127 | return !neg; |
daebaa78 JH |
1128 | } |
1129 | return 0; | |
1130 | } | |
fa5b1830 | 1131 | |
0845122c DT |
1132 | const char *find_descendant_ref(const char *dirname, |
1133 | const struct string_list *extras, | |
1134 | const struct string_list *skip) | |
fa5b1830 | 1135 | { |
0845122c | 1136 | int pos; |
fa5b1830 | 1137 | |
0845122c DT |
1138 | if (!extras) |
1139 | return NULL; | |
fa5b1830 MH |
1140 | |
1141 | /* | |
0845122c DT |
1142 | * Look at the place where dirname would be inserted into |
1143 | * extras. If there is an entry at that position that starts | |
1144 | * with dirname (remember, dirname includes the trailing | |
1145 | * slash) and is not in skip, then we have a conflict. | |
fa5b1830 | 1146 | */ |
0845122c DT |
1147 | for (pos = string_list_find_insert_index(extras, dirname, 0); |
1148 | pos < extras->nr; pos++) { | |
1149 | const char *extra_refname = extras->items[pos].string; | |
fa5b1830 | 1150 | |
0845122c DT |
1151 | if (!starts_with(extra_refname, dirname)) |
1152 | break; | |
1153 | ||
1154 | if (!skip || !string_list_has_string(skip, extra_refname)) | |
1155 | return extra_refname; | |
fa5b1830 | 1156 | } |
0845122c DT |
1157 | return NULL; |
1158 | } | |
fa5b1830 | 1159 | |
7d2df051 NTND |
1160 | int refs_rename_ref_available(struct ref_store *refs, |
1161 | const char *old_refname, | |
1162 | const char *new_refname) | |
0845122c DT |
1163 | { |
1164 | struct string_list skip = STRING_LIST_INIT_NODUP; | |
1165 | struct strbuf err = STRBUF_INIT; | |
ff3a299c | 1166 | int ok; |
fa5b1830 | 1167 | |
ff3a299c | 1168 | string_list_insert(&skip, old_refname); |
7d2df051 NTND |
1169 | ok = !refs_verify_refname_available(refs, new_refname, |
1170 | NULL, &skip, &err); | |
ff3a299c | 1171 | if (!ok) |
0845122c DT |
1172 | error("%s", err.buf); |
1173 | ||
1174 | string_list_clear(&skip, 0); | |
1175 | strbuf_release(&err); | |
ff3a299c | 1176 | return ok; |
fa5b1830 | 1177 | } |
2bf68ed5 DT |
1178 | |
1179 | int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) | |
1180 | { | |
1181 | struct object_id oid; | |
1182 | int flag; | |
1183 | ||
1184 | if (submodule) { | |
1185 | if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0) | |
1186 | return fn("HEAD", &oid, 0, cb_data); | |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
1191 | if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag)) | |
1192 | return fn("HEAD", &oid, flag, cb_data); | |
1193 | ||
1194 | return 0; | |
1195 | } | |
1196 | ||
1197 | int head_ref(each_ref_fn fn, void *cb_data) | |
1198 | { | |
1199 | return head_ref_submodule(NULL, fn, cb_data); | |
1200 | } | |
93770590 | 1201 | |
4c4de895 MH |
1202 | /* |
1203 | * Call fn for each reference in the specified submodule for which the | |
1204 | * refname begins with prefix. If trim is non-zero, then trim that | |
1205 | * many characters off the beginning of each refname before passing | |
1206 | * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to | |
1207 | * include broken references in the iteration. If fn ever returns a | |
1208 | * non-zero value, stop the iteration and return that value; | |
1209 | * otherwise, return 0. | |
1210 | */ | |
7d2df051 | 1211 | static int do_for_each_ref(struct ref_store *refs, const char *prefix, |
4c4de895 MH |
1212 | each_ref_fn fn, int trim, int flags, void *cb_data) |
1213 | { | |
1214 | struct ref_iterator *iter; | |
1215 | ||
00eebe35 MH |
1216 | if (!refs) |
1217 | return 0; | |
1218 | ||
1a769003 | 1219 | iter = refs->be->iterator_begin(refs, prefix, flags); |
4c4de895 MH |
1220 | iter = prefix_ref_iterator_begin(iter, prefix, trim); |
1221 | ||
1222 | return do_for_each_ref_iterator(iter, fn, cb_data); | |
1223 | } | |
1224 | ||
7d2df051 NTND |
1225 | int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
1226 | { | |
1227 | return do_for_each_ref(refs, "", fn, 0, 0, cb_data); | |
1228 | } | |
1229 | ||
93770590 DT |
1230 | int for_each_ref(each_ref_fn fn, void *cb_data) |
1231 | { | |
7d2df051 | 1232 | return refs_for_each_ref(get_main_ref_store(), fn, cb_data); |
93770590 DT |
1233 | } |
1234 | ||
1235 | int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) | |
1236 | { | |
7d2df051 NTND |
1237 | return refs_for_each_ref(get_submodule_ref_store(submodule), fn, cb_data); |
1238 | } | |
1239 | ||
1240 | int refs_for_each_ref_in(struct ref_store *refs, const char *prefix, | |
1241 | each_ref_fn fn, void *cb_data) | |
1242 | { | |
1243 | return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data); | |
93770590 DT |
1244 | } |
1245 | ||
1246 | int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) | |
1247 | { | |
7d2df051 | 1248 | return refs_for_each_ref_in(get_main_ref_store(), prefix, fn, cb_data); |
93770590 DT |
1249 | } |
1250 | ||
1251 | int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken) | |
1252 | { | |
1253 | unsigned int flag = 0; | |
1254 | ||
1255 | if (broken) | |
1256 | flag = DO_FOR_EACH_INCLUDE_BROKEN; | |
7d2df051 NTND |
1257 | return do_for_each_ref(get_main_ref_store(), |
1258 | prefix, fn, 0, flag, cb_data); | |
93770590 DT |
1259 | } |
1260 | ||
1261 | int for_each_ref_in_submodule(const char *submodule, const char *prefix, | |
7d2df051 | 1262 | each_ref_fn fn, void *cb_data) |
93770590 | 1263 | { |
7d2df051 NTND |
1264 | return refs_for_each_ref_in(get_submodule_ref_store(submodule), |
1265 | prefix, fn, cb_data); | |
93770590 DT |
1266 | } |
1267 | ||
1268 | int for_each_replace_ref(each_ref_fn fn, void *cb_data) | |
1269 | { | |
7d2df051 NTND |
1270 | return do_for_each_ref(get_main_ref_store(), |
1271 | git_replace_ref_base, fn, | |
1272 | strlen(git_replace_ref_base), | |
1273 | 0, cb_data); | |
93770590 DT |
1274 | } |
1275 | ||
1276 | int for_each_namespaced_ref(each_ref_fn fn, void *cb_data) | |
1277 | { | |
1278 | struct strbuf buf = STRBUF_INIT; | |
1279 | int ret; | |
1280 | strbuf_addf(&buf, "%srefs/", get_git_namespace()); | |
7d2df051 NTND |
1281 | ret = do_for_each_ref(get_main_ref_store(), |
1282 | buf.buf, fn, 0, 0, cb_data); | |
93770590 DT |
1283 | strbuf_release(&buf); |
1284 | return ret; | |
1285 | } | |
1286 | ||
7d2df051 | 1287 | int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
93770590 | 1288 | { |
7d2df051 | 1289 | return do_for_each_ref(refs, "", fn, 0, |
93770590 DT |
1290 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); |
1291 | } | |
2d0663b2 | 1292 | |
7d2df051 NTND |
1293 | int for_each_rawref(each_ref_fn fn, void *cb_data) |
1294 | { | |
1295 | return refs_for_each_rawref(get_main_ref_store(), fn, cb_data); | |
1296 | } | |
1297 | ||
2d0663b2 | 1298 | /* This function needs to return a meaningful errno on failure */ |
7d2df051 | 1299 | const char *refs_resolve_ref_unsafe(struct ref_store *refs, |
3c0cb0cb MH |
1300 | const char *refname, |
1301 | int resolve_flags, | |
1302 | unsigned char *sha1, int *flags) | |
2d0663b2 DT |
1303 | { |
1304 | static struct strbuf sb_refname = STRBUF_INIT; | |
1305 | int unused_flags; | |
1306 | int symref_count; | |
1307 | ||
1308 | if (!flags) | |
1309 | flags = &unused_flags; | |
1310 | ||
1311 | *flags = 0; | |
1312 | ||
1313 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
1314 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || | |
1315 | !refname_is_safe(refname)) { | |
1316 | errno = EINVAL; | |
1317 | return NULL; | |
1318 | } | |
1319 | ||
1320 | /* | |
1321 | * dwim_ref() uses REF_ISBROKEN to distinguish between | |
1322 | * missing refs and refs that were present but invalid, | |
1323 | * to complain about the latter to stderr. | |
1324 | * | |
1325 | * We don't know whether the ref exists, so don't set | |
1326 | * REF_ISBROKEN yet. | |
1327 | */ | |
1328 | *flags |= REF_BAD_NAME; | |
1329 | } | |
1330 | ||
1331 | for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) { | |
1332 | unsigned int read_flags = 0; | |
1333 | ||
e1e33b72 MH |
1334 | if (refs->be->read_raw_ref(refs, refname, |
1335 | sha1, &sb_refname, &read_flags)) { | |
2d0663b2 DT |
1336 | *flags |= read_flags; |
1337 | if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING)) | |
1338 | return NULL; | |
1339 | hashclr(sha1); | |
1340 | if (*flags & REF_BAD_NAME) | |
1341 | *flags |= REF_ISBROKEN; | |
1342 | return refname; | |
1343 | } | |
1344 | ||
1345 | *flags |= read_flags; | |
1346 | ||
1347 | if (!(read_flags & REF_ISSYMREF)) { | |
1348 | if (*flags & REF_BAD_NAME) { | |
1349 | hashclr(sha1); | |
1350 | *flags |= REF_ISBROKEN; | |
1351 | } | |
1352 | return refname; | |
1353 | } | |
1354 | ||
1355 | refname = sb_refname.buf; | |
1356 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { | |
1357 | hashclr(sha1); | |
1358 | return refname; | |
1359 | } | |
1360 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
1361 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || | |
1362 | !refname_is_safe(refname)) { | |
1363 | errno = EINVAL; | |
1364 | return NULL; | |
1365 | } | |
1366 | ||
1367 | *flags |= REF_ISBROKEN | REF_BAD_NAME; | |
1368 | } | |
1369 | } | |
1370 | ||
1371 | errno = ELOOP; | |
1372 | return NULL; | |
1373 | } | |
00eebe35 | 1374 | |
6fb5acfd DT |
1375 | /* backend functions */ |
1376 | int refs_init_db(struct strbuf *err) | |
1377 | { | |
077be78d | 1378 | struct ref_store *refs = get_main_ref_store(); |
6fb5acfd DT |
1379 | |
1380 | return refs->be->init_db(refs, err); | |
1381 | } | |
1382 | ||
bd40dcda MH |
1383 | const char *resolve_ref_unsafe(const char *refname, int resolve_flags, |
1384 | unsigned char *sha1, int *flags) | |
1385 | { | |
7d2df051 | 1386 | return refs_resolve_ref_unsafe(get_main_ref_store(), refname, |
bd40dcda MH |
1387 | resolve_flags, sha1, flags); |
1388 | } | |
1389 | ||
a8355bb7 MH |
1390 | int resolve_gitlink_ref(const char *submodule, const char *refname, |
1391 | unsigned char *sha1) | |
424dcc76 | 1392 | { |
a8355bb7 | 1393 | size_t len = strlen(submodule); |
424dcc76 MH |
1394 | struct ref_store *refs; |
1395 | int flags; | |
1396 | ||
a8355bb7 | 1397 | while (len && submodule[len - 1] == '/') |
424dcc76 | 1398 | len--; |
48a8475f | 1399 | |
424dcc76 MH |
1400 | if (!len) |
1401 | return -1; | |
1402 | ||
a8355bb7 | 1403 | if (submodule[len]) { |
48a8475f | 1404 | /* We need to strip off one or more trailing slashes */ |
a8355bb7 | 1405 | char *stripped = xmemdupz(submodule, len); |
48a8475f | 1406 | |
18d0002d | 1407 | refs = get_submodule_ref_store(stripped); |
48a8475f MH |
1408 | free(stripped); |
1409 | } else { | |
18d0002d | 1410 | refs = get_submodule_ref_store(submodule); |
48a8475f MH |
1411 | } |
1412 | ||
424dcc76 MH |
1413 | if (!refs) |
1414 | return -1; | |
1415 | ||
7d2df051 | 1416 | if (!refs_resolve_ref_unsafe(refs, refname, 0, sha1, &flags) || |
424dcc76 MH |
1417 | is_null_sha1(sha1)) |
1418 | return -1; | |
1419 | return 0; | |
1420 | } | |
1421 | ||
7d4558c4 MH |
1422 | struct submodule_hash_entry |
1423 | { | |
1424 | struct hashmap_entry ent; /* must be the first member! */ | |
1425 | ||
1426 | struct ref_store *refs; | |
1427 | ||
1428 | /* NUL-terminated name of submodule: */ | |
1429 | char submodule[FLEX_ARRAY]; | |
1430 | }; | |
1431 | ||
1432 | static int submodule_hash_cmp(const void *entry, const void *entry_or_key, | |
1433 | const void *keydata) | |
1434 | { | |
1435 | const struct submodule_hash_entry *e1 = entry, *e2 = entry_or_key; | |
1436 | const char *submodule = keydata ? keydata : e2->submodule; | |
1437 | ||
1438 | return strcmp(e1->submodule, submodule); | |
1439 | } | |
1440 | ||
1441 | static struct submodule_hash_entry *alloc_submodule_hash_entry( | |
1442 | const char *submodule, struct ref_store *refs) | |
1443 | { | |
1444 | struct submodule_hash_entry *entry; | |
1445 | ||
1446 | FLEX_ALLOC_STR(entry, submodule, submodule); | |
1447 | hashmap_entry_init(entry, strhash(submodule)); | |
1448 | entry->refs = refs; | |
1449 | return entry; | |
1450 | } | |
1451 | ||
00eebe35 MH |
1452 | /* A pointer to the ref_store for the main repository: */ |
1453 | static struct ref_store *main_ref_store; | |
1454 | ||
7d4558c4 MH |
1455 | /* A hashmap of ref_stores, stored by submodule name: */ |
1456 | static struct hashmap submodule_ref_stores; | |
00eebe35 | 1457 | |
c468da4e | 1458 | /* |
9476c6ed NTND |
1459 | * Return the ref_store instance for the specified submodule. If that |
1460 | * ref_store hasn't been initialized yet, return NULL. | |
c468da4e | 1461 | */ |
9476c6ed | 1462 | static struct ref_store *lookup_submodule_ref_store(const char *submodule) |
00eebe35 | 1463 | { |
7d4558c4 | 1464 | struct submodule_hash_entry *entry; |
00eebe35 | 1465 | |
7d4558c4 MH |
1466 | if (!submodule_ref_stores.tablesize) |
1467 | /* It's initialized on demand in register_ref_store(). */ | |
1468 | return NULL; | |
620a66b9 | 1469 | |
7d4558c4 MH |
1470 | entry = hashmap_get_from_hash(&submodule_ref_stores, |
1471 | strhash(submodule), submodule); | |
1472 | return entry ? entry->refs : NULL; | |
00eebe35 MH |
1473 | } |
1474 | ||
c468da4e MH |
1475 | /* |
1476 | * Create, record, and return a ref_store instance for the specified | |
5d0bc90e | 1477 | * gitdir. |
c468da4e | 1478 | */ |
9e7ec634 NTND |
1479 | static struct ref_store *ref_store_init(const char *gitdir, |
1480 | unsigned int flags) | |
00eebe35 MH |
1481 | { |
1482 | const char *be_name = "files"; | |
1483 | struct ref_storage_be *be = find_ref_storage_backend(be_name); | |
ba88add5 | 1484 | struct ref_store *refs; |
00eebe35 MH |
1485 | |
1486 | if (!be) | |
1487 | die("BUG: reference backend %s is unknown", be_name); | |
1488 | ||
9e7ec634 | 1489 | refs = be->init(gitdir, flags); |
ba88add5 | 1490 | return refs; |
00eebe35 MH |
1491 | } |
1492 | ||
077be78d | 1493 | struct ref_store *get_main_ref_store(void) |
24c8407e NTND |
1494 | { |
1495 | if (main_ref_store) | |
1496 | return main_ref_store; | |
1497 | ||
9e7ec634 NTND |
1498 | main_ref_store = ref_store_init(get_git_dir(), |
1499 | (REF_STORE_READ | | |
1500 | REF_STORE_WRITE | | |
1501 | REF_STORE_ODB | | |
1502 | REF_STORE_MAIN)); | |
378dc910 NTND |
1503 | return main_ref_store; |
1504 | } | |
1505 | ||
1506 | /* | |
1507 | * Register the specified ref_store to be the one that should be used | |
1508 | * for submodule. It is a fatal error to call this function twice for | |
1509 | * the same submodule. | |
1510 | */ | |
1511 | static void register_submodule_ref_store(struct ref_store *refs, | |
1512 | const char *submodule) | |
1513 | { | |
1514 | if (!submodule_ref_stores.tablesize) | |
1515 | hashmap_init(&submodule_ref_stores, submodule_hash_cmp, 0); | |
1516 | ||
1517 | if (hashmap_put(&submodule_ref_stores, | |
1518 | alloc_submodule_hash_entry(submodule, refs))) | |
1519 | die("BUG: ref_store for submodule '%s' initialized twice", | |
1520 | submodule); | |
24c8407e NTND |
1521 | } |
1522 | ||
18d0002d | 1523 | struct ref_store *get_submodule_ref_store(const char *submodule) |
00eebe35 | 1524 | { |
126c9e05 | 1525 | struct strbuf submodule_sb = STRBUF_INIT; |
00eebe35 | 1526 | struct ref_store *refs; |
126c9e05 | 1527 | int ret; |
00eebe35 MH |
1528 | |
1529 | if (!submodule || !*submodule) { | |
18d0002d NTND |
1530 | /* |
1531 | * FIXME: This case is ideally not allowed. But that | |
1532 | * can't happen until we clean up all the callers. | |
1533 | */ | |
24c8407e | 1534 | return get_main_ref_store(); |
126c9e05 | 1535 | } |
00eebe35 | 1536 | |
126c9e05 NTND |
1537 | refs = lookup_submodule_ref_store(submodule); |
1538 | if (refs) | |
1539 | return refs; | |
00eebe35 | 1540 | |
126c9e05 NTND |
1541 | strbuf_addstr(&submodule_sb, submodule); |
1542 | ret = is_nonbare_repository_dir(&submodule_sb); | |
1543 | strbuf_release(&submodule_sb); | |
1544 | if (!ret) | |
1545 | return NULL; | |
1546 | ||
5d0bc90e NTND |
1547 | ret = submodule_to_gitdir(&submodule_sb, submodule); |
1548 | if (ret) { | |
1549 | strbuf_release(&submodule_sb); | |
1550 | return NULL; | |
1551 | } | |
1552 | ||
9e7ec634 NTND |
1553 | /* assume that add_submodule_odb() has been called */ |
1554 | refs = ref_store_init(submodule_sb.buf, | |
1555 | REF_STORE_READ | REF_STORE_ODB); | |
378dc910 | 1556 | register_submodule_ref_store(refs, submodule); |
5d0bc90e NTND |
1557 | |
1558 | strbuf_release(&submodule_sb); | |
00eebe35 MH |
1559 | return refs; |
1560 | } | |
1561 | ||
620a66b9 | 1562 | void base_ref_store_init(struct ref_store *refs, |
fbfd0a29 | 1563 | const struct ref_storage_be *be) |
00eebe35 | 1564 | { |
620a66b9 | 1565 | refs->be = be; |
00eebe35 | 1566 | } |
127b42a1 RS |
1567 | |
1568 | /* backend functions */ | |
7d2df051 NTND |
1569 | int refs_pack_refs(struct ref_store *refs, unsigned int flags) |
1570 | { | |
1571 | return refs->be->pack_refs(refs, flags); | |
1572 | } | |
1573 | ||
8231527e MH |
1574 | int pack_refs(unsigned int flags) |
1575 | { | |
7d2df051 NTND |
1576 | return refs_pack_refs(get_main_ref_store(), flags); |
1577 | } | |
8231527e | 1578 | |
7d2df051 NTND |
1579 | int refs_peel_ref(struct ref_store *refs, const char *refname, |
1580 | unsigned char *sha1) | |
1581 | { | |
1582 | return refs->be->peel_ref(refs, refname, sha1); | |
8231527e MH |
1583 | } |
1584 | ||
bd427cf2 MH |
1585 | int peel_ref(const char *refname, unsigned char *sha1) |
1586 | { | |
7d2df051 NTND |
1587 | return refs_peel_ref(get_main_ref_store(), refname, sha1); |
1588 | } | |
bd427cf2 | 1589 | |
7d2df051 NTND |
1590 | int refs_create_symref(struct ref_store *refs, |
1591 | const char *ref_target, | |
1592 | const char *refs_heads_master, | |
1593 | const char *logmsg) | |
1594 | { | |
1595 | return refs->be->create_symref(refs, ref_target, | |
1596 | refs_heads_master, | |
1597 | logmsg); | |
bd427cf2 MH |
1598 | } |
1599 | ||
284689ba MH |
1600 | int create_symref(const char *ref_target, const char *refs_heads_master, |
1601 | const char *logmsg) | |
1602 | { | |
7d2df051 NTND |
1603 | return refs_create_symref(get_main_ref_store(), ref_target, |
1604 | refs_heads_master, logmsg); | |
284689ba MH |
1605 | } |
1606 | ||
127b42a1 RS |
1607 | int ref_transaction_commit(struct ref_transaction *transaction, |
1608 | struct strbuf *err) | |
1609 | { | |
077be78d | 1610 | struct ref_store *refs = get_main_ref_store(); |
127b42a1 RS |
1611 | |
1612 | return refs->be->transaction_commit(refs, transaction, err); | |
1613 | } | |
62665823 | 1614 | |
7d2df051 NTND |
1615 | int refs_verify_refname_available(struct ref_store *refs, |
1616 | const char *refname, | |
1617 | const struct string_list *extra, | |
1618 | const struct string_list *skip, | |
1619 | struct strbuf *err) | |
62665823 | 1620 | { |
62665823 MH |
1621 | return refs->be->verify_refname_available(refs, refname, extra, skip, err); |
1622 | } | |
e3688bd6 | 1623 | |
7d2df051 | 1624 | int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
e3688bd6 | 1625 | { |
e3688bd6 DT |
1626 | struct ref_iterator *iter; |
1627 | ||
1628 | iter = refs->be->reflog_iterator_begin(refs); | |
1629 | ||
1630 | return do_for_each_ref_iterator(iter, fn, cb_data); | |
1631 | } | |
1632 | ||
7d2df051 | 1633 | int for_each_reflog(each_ref_fn fn, void *cb_data) |
e3688bd6 | 1634 | { |
7d2df051 NTND |
1635 | return refs_for_each_reflog(get_main_ref_store(), fn, cb_data); |
1636 | } | |
e3688bd6 | 1637 | |
7d2df051 NTND |
1638 | int refs_for_each_reflog_ent_reverse(struct ref_store *refs, |
1639 | const char *refname, | |
1640 | each_reflog_ent_fn fn, | |
1641 | void *cb_data) | |
1642 | { | |
e3688bd6 DT |
1643 | return refs->be->for_each_reflog_ent_reverse(refs, refname, |
1644 | fn, cb_data); | |
1645 | } | |
1646 | ||
7d2df051 NTND |
1647 | int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, |
1648 | void *cb_data) | |
1649 | { | |
1650 | return refs_for_each_reflog_ent_reverse(get_main_ref_store(), | |
1651 | refname, fn, cb_data); | |
1652 | } | |
1653 | ||
1654 | int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, | |
1655 | each_reflog_ent_fn fn, void *cb_data) | |
1656 | { | |
1657 | return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data); | |
1658 | } | |
1659 | ||
e3688bd6 DT |
1660 | int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, |
1661 | void *cb_data) | |
1662 | { | |
7d2df051 NTND |
1663 | return refs_for_each_reflog_ent(get_main_ref_store(), refname, |
1664 | fn, cb_data); | |
1665 | } | |
e3688bd6 | 1666 | |
7d2df051 NTND |
1667 | int refs_reflog_exists(struct ref_store *refs, const char *refname) |
1668 | { | |
1669 | return refs->be->reflog_exists(refs, refname); | |
e3688bd6 DT |
1670 | } |
1671 | ||
1672 | int reflog_exists(const char *refname) | |
1673 | { | |
7d2df051 NTND |
1674 | return refs_reflog_exists(get_main_ref_store(), refname); |
1675 | } | |
e3688bd6 | 1676 | |
7d2df051 NTND |
1677 | int refs_create_reflog(struct ref_store *refs, const char *refname, |
1678 | int force_create, struct strbuf *err) | |
1679 | { | |
1680 | return refs->be->create_reflog(refs, refname, force_create, err); | |
e3688bd6 DT |
1681 | } |
1682 | ||
1683 | int safe_create_reflog(const char *refname, int force_create, | |
1684 | struct strbuf *err) | |
1685 | { | |
7d2df051 NTND |
1686 | return refs_create_reflog(get_main_ref_store(), refname, |
1687 | force_create, err); | |
1688 | } | |
e3688bd6 | 1689 | |
7d2df051 NTND |
1690 | int refs_delete_reflog(struct ref_store *refs, const char *refname) |
1691 | { | |
1692 | return refs->be->delete_reflog(refs, refname); | |
e3688bd6 DT |
1693 | } |
1694 | ||
1695 | int delete_reflog(const char *refname) | |
1696 | { | |
7d2df051 NTND |
1697 | return refs_delete_reflog(get_main_ref_store(), refname); |
1698 | } | |
e3688bd6 | 1699 | |
7d2df051 NTND |
1700 | int refs_reflog_expire(struct ref_store *refs, |
1701 | const char *refname, const unsigned char *sha1, | |
1702 | unsigned int flags, | |
1703 | reflog_expiry_prepare_fn prepare_fn, | |
1704 | reflog_expiry_should_prune_fn should_prune_fn, | |
1705 | reflog_expiry_cleanup_fn cleanup_fn, | |
1706 | void *policy_cb_data) | |
1707 | { | |
1708 | return refs->be->reflog_expire(refs, refname, sha1, flags, | |
1709 | prepare_fn, should_prune_fn, | |
1710 | cleanup_fn, policy_cb_data); | |
e3688bd6 DT |
1711 | } |
1712 | ||
1713 | int reflog_expire(const char *refname, const unsigned char *sha1, | |
1714 | unsigned int flags, | |
1715 | reflog_expiry_prepare_fn prepare_fn, | |
1716 | reflog_expiry_should_prune_fn should_prune_fn, | |
1717 | reflog_expiry_cleanup_fn cleanup_fn, | |
1718 | void *policy_cb_data) | |
1719 | { | |
7d2df051 NTND |
1720 | return refs_reflog_expire(get_main_ref_store(), |
1721 | refname, sha1, flags, | |
1722 | prepare_fn, should_prune_fn, | |
1723 | cleanup_fn, policy_cb_data); | |
e3688bd6 | 1724 | } |
fc681463 DT |
1725 | |
1726 | int initial_ref_transaction_commit(struct ref_transaction *transaction, | |
1727 | struct strbuf *err) | |
1728 | { | |
077be78d | 1729 | struct ref_store *refs = get_main_ref_store(); |
fc681463 DT |
1730 | |
1731 | return refs->be->initial_transaction_commit(refs, transaction, err); | |
1732 | } | |
a27dcf89 | 1733 | |
7d2df051 NTND |
1734 | int refs_delete_refs(struct ref_store *refs, struct string_list *refnames, |
1735 | unsigned int flags) | |
a27dcf89 | 1736 | { |
a27dcf89 DT |
1737 | return refs->be->delete_refs(refs, refnames, flags); |
1738 | } | |
9b6b40d9 | 1739 | |
7d2df051 | 1740 | int delete_refs(struct string_list *refnames, unsigned int flags) |
9b6b40d9 | 1741 | { |
7d2df051 NTND |
1742 | return refs_delete_refs(get_main_ref_store(), refnames, flags); |
1743 | } | |
9b6b40d9 | 1744 | |
7d2df051 NTND |
1745 | int refs_rename_ref(struct ref_store *refs, const char *oldref, |
1746 | const char *newref, const char *logmsg) | |
1747 | { | |
9b6b40d9 DT |
1748 | return refs->be->rename_ref(refs, oldref, newref, logmsg); |
1749 | } | |
7d2df051 NTND |
1750 | |
1751 | int rename_ref(const char *oldref, const char *newref, const char *logmsg) | |
1752 | { | |
1753 | return refs_rename_ref(get_main_ref_store(), oldref, newref, logmsg); | |
1754 | } |