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