]>
Commit | Line | Data |
---|---|---|
7bd9bcf3 MH |
1 | /* |
2 | * The backend-independent part of the reference module. | |
3 | */ | |
4 | ||
a6dc3d36 | 5 | #include "cache.h" |
36bf1958 | 6 | #include "alloc.h" |
b2141fc1 | 7 | #include "config.h" |
32a8f510 | 8 | #include "environment.h" |
7d4558c4 | 9 | #include "hashmap.h" |
f394e093 | 10 | #include "gettext.h" |
41771fa4 | 11 | #include "hex.h" |
697cc8ef | 12 | #include "lockfile.h" |
b05855b5 | 13 | #include "iterator.h" |
85023577 | 14 | #include "refs.h" |
4cb77009 | 15 | #include "refs/refs-internal.h" |
67541597 | 16 | #include "run-command.h" |
5e3aba33 | 17 | #include "hook.h" |
cbd53a21 | 18 | #include "object-store.h" |
cf0adba7 JH |
19 | #include "object.h" |
20 | #include "tag.h" | |
5d0bc90e | 21 | #include "submodule.h" |
17eff96b | 22 | #include "worktree.h" |
dbbcd44f | 23 | #include "strvec.h" |
23a3f0cb | 24 | #include "repository.h" |
67541597 | 25 | #include "sigchain.h" |
88c7b4c3 | 26 | #include "date.h" |
b9342b3f | 27 | #include "commit.h" |
d5ebb50d | 28 | #include "wrapper.h" |
3581d793 | 29 | |
3dce444f RS |
30 | /* |
31 | * List of all available backends | |
32 | */ | |
33 | static struct ref_storage_be *refs_backends = &refs_be_files; | |
34 | ||
35 | static struct ref_storage_be *find_ref_storage_backend(const char *name) | |
36 | { | |
37 | struct ref_storage_be *be; | |
38 | for (be = refs_backends; be; be = be->next) | |
39 | if (!strcmp(be->name, name)) | |
40 | return be; | |
41 | return NULL; | |
42 | } | |
43 | ||
bc5fd6d3 | 44 | /* |
dde8a902 DT |
45 | * How to handle various characters in refnames: |
46 | * 0: An acceptable character for refs | |
5e650228 JH |
47 | * 1: End-of-component |
48 | * 2: ., look for a preceding . to reject .. in refs | |
49 | * 3: {, look for a preceding @ to reject @{ in refs | |
53a8555e | 50 | * 4: A bad character: ASCII control characters, and |
cd377f45 JK |
51 | * ":", "?", "[", "\", "^", "~", SP, or TAB |
52 | * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set | |
dde8a902 DT |
53 | */ |
54 | static unsigned char refname_disposition[256] = { | |
5e650228 JH |
55 | 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
56 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
cd377f45 | 57 | 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1, |
5e650228 | 58 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, |
dde8a902 | 59 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
5e650228 JH |
60 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0, |
61 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
62 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4 | |
dde8a902 DT |
63 | }; |
64 | ||
b9342b3f DS |
65 | struct ref_namespace_info ref_namespace[] = { |
66 | [NAMESPACE_HEAD] = { | |
67 | .ref = "HEAD", | |
68 | .decoration = DECORATION_REF_HEAD, | |
69 | .exact = 1, | |
70 | }, | |
71 | [NAMESPACE_BRANCHES] = { | |
72 | .ref = "refs/heads/", | |
73 | .decoration = DECORATION_REF_LOCAL, | |
74 | }, | |
75 | [NAMESPACE_TAGS] = { | |
76 | .ref = "refs/tags/", | |
77 | .decoration = DECORATION_REF_TAG, | |
78 | }, | |
79 | [NAMESPACE_REMOTE_REFS] = { | |
80 | /* | |
81 | * The default refspec for new remotes copies refs from | |
82 | * refs/heads/ on the remote into refs/remotes/<remote>/. | |
83 | * As such, "refs/remotes/" has special handling. | |
84 | */ | |
85 | .ref = "refs/remotes/", | |
86 | .decoration = DECORATION_REF_REMOTE, | |
87 | }, | |
88 | [NAMESPACE_STASH] = { | |
89 | /* | |
90 | * The single ref "refs/stash" stores the latest stash. | |
91 | * Older stashes can be found in the reflog. | |
92 | */ | |
93 | .ref = "refs/stash", | |
94 | .exact = 1, | |
95 | .decoration = DECORATION_REF_STASH, | |
96 | }, | |
97 | [NAMESPACE_REPLACE] = { | |
98 | /* | |
99 | * This namespace allows Git to act as if one object ID | |
100 | * points to the content of another. Unlike the other | |
101 | * ref namespaces, this one can be changed by the | |
102 | * GIT_REPLACE_REF_BASE environment variable. This | |
103 | * .namespace value will be overwritten in setup_git_env(). | |
104 | */ | |
105 | .ref = "refs/replace/", | |
106 | .decoration = DECORATION_GRAFTED, | |
107 | }, | |
108 | [NAMESPACE_NOTES] = { | |
109 | /* | |
110 | * The refs/notes/commit ref points to the tip of a | |
111 | * parallel commit history that adds metadata to commits | |
112 | * in the normal history. This ref can be overwritten | |
113 | * by the core.notesRef config variable or the | |
114 | * GIT_NOTES_REFS environment variable. | |
115 | */ | |
116 | .ref = "refs/notes/commit", | |
117 | .exact = 1, | |
118 | }, | |
119 | [NAMESPACE_PREFETCH] = { | |
120 | /* | |
121 | * Prefetch refs are written by the background 'fetch' | |
122 | * maintenance task. It allows faster foreground fetches | |
123 | * by advertising these previously-downloaded tips without | |
124 | * updating refs/remotes/ without user intervention. | |
125 | */ | |
126 | .ref = "refs/prefetch/", | |
127 | }, | |
128 | [NAMESPACE_REWRITTEN] = { | |
129 | /* | |
130 | * Rewritten refs are used by the 'label' command in the | |
131 | * sequencer. These are particularly useful during an | |
132 | * interactive rebase that uses the 'merge' command. | |
133 | */ | |
134 | .ref = "refs/rewritten/", | |
135 | }, | |
136 | }; | |
137 | ||
138 | void update_ref_namespace(enum ref_namespace namespace, char *ref) | |
139 | { | |
140 | struct ref_namespace_info *info = &ref_namespace[namespace]; | |
141 | if (info->ref_updated) | |
142 | free(info->ref); | |
143 | info->ref = ref; | |
144 | info->ref_updated = 1; | |
145 | } | |
146 | ||
dde8a902 DT |
147 | /* |
148 | * Try to read one refname component from the front of refname. | |
149 | * Return the length of the component found, or -1 if the component is | |
150 | * not legal. It is legal if it is something reasonable to have under | |
151 | * ".git/refs/"; We do not like it if: | |
bc5fd6d3 | 152 | * |
1de16aec | 153 | * - it begins with ".", or |
bc5fd6d3 | 154 | * - it has double dots "..", or |
53a8555e | 155 | * - it has ASCII control characters, or |
cd377f45 JK |
156 | * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or |
157 | * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or | |
53a8555e JK |
158 | * - it ends with a "/", or |
159 | * - it ends with ".lock", or | |
160 | * - it contains a "@{" portion | |
1de16aec NTND |
161 | * |
162 | * When sanitized is not NULL, instead of rejecting the input refname | |
163 | * as an error, try to come up with a usable replacement for the input | |
164 | * refname in it. | |
bc5fd6d3 | 165 | */ |
1de16aec NTND |
166 | static int check_refname_component(const char *refname, int *flags, |
167 | struct strbuf *sanitized) | |
bc5fd6d3 MH |
168 | { |
169 | const char *cp; | |
170 | char last = '\0'; | |
1de16aec NTND |
171 | size_t component_start = 0; /* garbage - not a reasonable initial value */ |
172 | ||
173 | if (sanitized) | |
174 | component_start = sanitized->len; | |
bc5fd6d3 MH |
175 | |
176 | for (cp = refname; ; cp++) { | |
dde8a902 DT |
177 | int ch = *cp & 255; |
178 | unsigned char disp = refname_disposition[ch]; | |
1de16aec NTND |
179 | |
180 | if (sanitized && disp != 1) | |
181 | strbuf_addch(sanitized, ch); | |
182 | ||
dde8a902 | 183 | switch (disp) { |
5e650228 | 184 | case 1: |
dde8a902 | 185 | goto out; |
5e650228 | 186 | case 2: |
1de16aec NTND |
187 | if (last == '.') { /* Refname contains "..". */ |
188 | if (sanitized) | |
189 | /* collapse ".." to single "." */ | |
190 | strbuf_setlen(sanitized, sanitized->len - 1); | |
191 | else | |
192 | return -1; | |
193 | } | |
dde8a902 | 194 | break; |
5e650228 | 195 | case 3: |
1de16aec NTND |
196 | if (last == '@') { /* Refname contains "@{". */ |
197 | if (sanitized) | |
198 | sanitized->buf[sanitized->len-1] = '-'; | |
199 | else | |
200 | return -1; | |
201 | } | |
bc5fd6d3 | 202 | break; |
5e650228 | 203 | case 4: |
1de16aec NTND |
204 | /* forbidden char */ |
205 | if (sanitized) | |
206 | sanitized->buf[sanitized->len-1] = '-'; | |
207 | else | |
208 | return -1; | |
209 | break; | |
cd377f45 | 210 | case 5: |
1de16aec NTND |
211 | if (!(*flags & REFNAME_REFSPEC_PATTERN)) { |
212 | /* refspec can't be a pattern */ | |
213 | if (sanitized) | |
214 | sanitized->buf[sanitized->len-1] = '-'; | |
215 | else | |
216 | return -1; | |
217 | } | |
cd377f45 JK |
218 | |
219 | /* | |
220 | * Unset the pattern flag so that we only accept | |
221 | * a single asterisk for one side of refspec. | |
222 | */ | |
223 | *flags &= ~ REFNAME_REFSPEC_PATTERN; | |
224 | break; | |
dde8a902 | 225 | } |
bc5fd6d3 MH |
226 | last = ch; |
227 | } | |
dde8a902 | 228 | out: |
bc5fd6d3 | 229 | if (cp == refname) |
dac529e4 | 230 | return 0; /* Component has zero length. */ |
1de16aec NTND |
231 | |
232 | if (refname[0] == '.') { /* Component starts with '.'. */ | |
233 | if (sanitized) | |
234 | sanitized->buf[component_start] = '-'; | |
235 | else | |
236 | return -1; | |
237 | } | |
7108ad23 | 238 | if (cp - refname >= LOCK_SUFFIX_LEN && |
1de16aec NTND |
239 | !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) { |
240 | if (!sanitized) | |
241 | return -1; | |
242 | /* Refname ends with ".lock". */ | |
243 | while (strbuf_strip_suffix(sanitized, LOCK_SUFFIX)) { | |
244 | /* try again in case we have .lock.lock */ | |
245 | } | |
246 | } | |
bc5fd6d3 MH |
247 | return cp - refname; |
248 | } | |
249 | ||
1de16aec NTND |
250 | static int check_or_sanitize_refname(const char *refname, int flags, |
251 | struct strbuf *sanitized) | |
bc5fd6d3 MH |
252 | { |
253 | int component_len, component_count = 0; | |
254 | ||
1de16aec | 255 | if (!strcmp(refname, "@")) { |
9ba89f48 | 256 | /* Refname is a single character '@'. */ |
1de16aec NTND |
257 | if (sanitized) |
258 | strbuf_addch(sanitized, '-'); | |
259 | else | |
260 | return -1; | |
261 | } | |
9ba89f48 | 262 | |
bc5fd6d3 | 263 | while (1) { |
1de16aec NTND |
264 | if (sanitized && sanitized->len) |
265 | strbuf_complete(sanitized, '/'); | |
266 | ||
bc5fd6d3 | 267 | /* We are at the start of a path component. */ |
1de16aec NTND |
268 | component_len = check_refname_component(refname, &flags, |
269 | sanitized); | |
270 | if (sanitized && component_len == 0) | |
271 | ; /* OK, omit empty component */ | |
272 | else if (component_len <= 0) | |
cd377f45 JK |
273 | return -1; |
274 | ||
bc5fd6d3 MH |
275 | component_count++; |
276 | if (refname[component_len] == '\0') | |
277 | break; | |
278 | /* Skip to next component. */ | |
279 | refname += component_len + 1; | |
280 | } | |
281 | ||
1de16aec NTND |
282 | if (refname[component_len - 1] == '.') { |
283 | /* Refname ends with '.'. */ | |
284 | if (sanitized) | |
285 | ; /* omit ending dot */ | |
286 | else | |
287 | return -1; | |
288 | } | |
bc5fd6d3 MH |
289 | if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2) |
290 | return -1; /* Refname has only one component. */ | |
291 | return 0; | |
292 | } | |
293 | ||
1de16aec NTND |
294 | int check_refname_format(const char *refname, int flags) |
295 | { | |
296 | return check_or_sanitize_refname(refname, flags, NULL); | |
297 | } | |
298 | ||
299 | void sanitize_refname_component(const char *refname, struct strbuf *out) | |
300 | { | |
301 | if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out)) | |
302 | BUG("sanitizing refname '%s' check returned error", refname); | |
303 | } | |
304 | ||
4cb77009 | 305 | int refname_is_safe(const char *refname) |
d0f810f0 | 306 | { |
39950fef MH |
307 | const char *rest; |
308 | ||
309 | if (skip_prefix(refname, "refs/", &rest)) { | |
d0f810f0 RS |
310 | char *buf; |
311 | int result; | |
e40f3557 MH |
312 | size_t restlen = strlen(rest); |
313 | ||
314 | /* rest must not be empty, or start or end with "/" */ | |
315 | if (!restlen || *rest == '/' || rest[restlen - 1] == '/') | |
316 | return 0; | |
d0f810f0 | 317 | |
d0f810f0 RS |
318 | /* |
319 | * Does the refname try to escape refs/? | |
320 | * For example: refs/foo/../bar is safe but refs/foo/../../bar | |
321 | * is not. | |
322 | */ | |
e40f3557 MH |
323 | buf = xmallocz(restlen); |
324 | result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest); | |
d0f810f0 RS |
325 | free(buf); |
326 | return result; | |
327 | } | |
35db25c6 MH |
328 | |
329 | do { | |
d0f810f0 RS |
330 | if (!isupper(*refname) && *refname != '_') |
331 | return 0; | |
332 | refname++; | |
35db25c6 | 333 | } while (*refname); |
d0f810f0 RS |
334 | return 1; |
335 | } | |
336 | ||
67be7c5a MH |
337 | /* |
338 | * Return true if refname, which has the specified oid and flags, can | |
339 | * be resolved to an object in the database. If the referred-to object | |
340 | * does not exist, emit a warning and return false. | |
341 | */ | |
342 | int ref_resolves_to_object(const char *refname, | |
9bc45a28 | 343 | struct repository *repo, |
67be7c5a MH |
344 | const struct object_id *oid, |
345 | unsigned int flags) | |
346 | { | |
347 | if (flags & REF_ISBROKEN) | |
348 | return 0; | |
9bc45a28 | 349 | if (!repo_has_object_file(repo, oid)) { |
661558f0 | 350 | error(_("%s does not point to a valid object!"), refname); |
67be7c5a MH |
351 | return 0; |
352 | } | |
353 | return 1; | |
354 | } | |
355 | ||
7d2df051 NTND |
356 | char *refs_resolve_refdup(struct ref_store *refs, |
357 | const char *refname, int resolve_flags, | |
0f2dc722 | 358 | struct object_id *oid, int *flags) |
7d2df051 NTND |
359 | { |
360 | const char *result; | |
361 | ||
362 | result = refs_resolve_ref_unsafe(refs, refname, resolve_flags, | |
ce14de03 | 363 | oid, flags); |
7d2df051 NTND |
364 | return xstrdup_or_null(result); |
365 | } | |
366 | ||
7bd9bcf3 | 367 | char *resolve_refdup(const char *refname, int resolve_flags, |
0f2dc722 | 368 | struct object_id *oid, int *flags) |
e1e22e37 | 369 | { |
23a3f0cb | 370 | return refs_resolve_refdup(get_main_ref_store(the_repository), |
7d2df051 | 371 | refname, resolve_flags, |
0f2dc722 | 372 | oid, flags); |
cddc4258 MH |
373 | } |
374 | ||
7bd9bcf3 MH |
375 | /* The argument to filter_refs */ |
376 | struct ref_filter { | |
377 | const char *pattern; | |
9ab9b5df | 378 | const char *prefix; |
7bd9bcf3 MH |
379 | each_ref_fn *fn; |
380 | void *cb_data; | |
381 | }; | |
432ad41e | 382 | |
76887df0 | 383 | int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags) |
732134ed | 384 | { |
76887df0 ÆAB |
385 | struct ref_store *refs = get_main_ref_store(the_repository); |
386 | ||
f1da24ca | 387 | if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, |
ce14de03 | 388 | oid, flags)) |
7bd9bcf3 MH |
389 | return 0; |
390 | return -1; | |
732134ed MH |
391 | } |
392 | ||
34c290a6 | 393 | int read_ref(const char *refname, struct object_id *oid) |
cddc4258 | 394 | { |
34c290a6 | 395 | return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL); |
c774aab9 JP |
396 | } |
397 | ||
3f9f1acc | 398 | int refs_ref_exists(struct ref_store *refs, const char *refname) |
b3cd33d0 | 399 | { |
f1da24ca | 400 | return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING, |
ce14de03 | 401 | NULL, NULL); |
b3cd33d0 NTND |
402 | } |
403 | ||
7bd9bcf3 | 404 | int ref_exists(const char *refname) |
bc5fd6d3 | 405 | { |
b3cd33d0 | 406 | return refs_ref_exists(get_main_ref_store(the_repository), refname); |
bc5fd6d3 MH |
407 | } |
408 | ||
7bd9bcf3 MH |
409 | static int filter_refs(const char *refname, const struct object_id *oid, |
410 | int flags, void *data) | |
432ad41e | 411 | { |
7bd9bcf3 MH |
412 | struct ref_filter *filter = (struct ref_filter *)data; |
413 | ||
55d34269 | 414 | if (wildmatch(filter->pattern, refname, 0)) |
7bd9bcf3 | 415 | return 0; |
9ab9b5df RA |
416 | if (filter->prefix) |
417 | skip_prefix(refname, filter->prefix, &refname); | |
7bd9bcf3 | 418 | return filter->fn(refname, oid, flags, filter->cb_data); |
432ad41e MH |
419 | } |
420 | ||
ac2ed0d7 | 421 | enum peel_status peel_object(const struct object_id *name, struct object_id *oid) |
c774aab9 | 422 | { |
45a187cc | 423 | struct object *o = lookup_unknown_object(the_repository, name); |
c774aab9 | 424 | |
7bd9bcf3 | 425 | if (o->type == OBJ_NONE) { |
0df8e965 | 426 | int type = oid_object_info(the_repository, name, NULL); |
6da43d93 | 427 | if (type < 0 || !object_as_type(o, type, 0)) |
7bd9bcf3 MH |
428 | return PEEL_INVALID; |
429 | } | |
bc5fd6d3 | 430 | |
7bd9bcf3 MH |
431 | if (o->type != OBJ_TAG) |
432 | return PEEL_NON_TAG; | |
e1980c9d | 433 | |
7bd9bcf3 MH |
434 | o = deref_tag_noverify(o); |
435 | if (!o) | |
436 | return PEEL_INVALID; | |
437 | ||
ac2ed0d7 | 438 | oidcpy(oid, &o->oid); |
7bd9bcf3 | 439 | return PEEL_PEELED; |
e1980c9d JH |
440 | } |
441 | ||
7bd9bcf3 MH |
442 | struct warn_if_dangling_data { |
443 | FILE *fp; | |
444 | const char *refname; | |
445 | const struct string_list *refnames; | |
446 | const char *msg_fmt; | |
447 | }; | |
bc5fd6d3 | 448 | |
63e14ee2 | 449 | static int warn_if_dangling_symref(const char *refname, |
5cf88fd8 | 450 | const struct object_id *oid UNUSED, |
7bd9bcf3 MH |
451 | int flags, void *cb_data) |
452 | { | |
453 | struct warn_if_dangling_data *d = cb_data; | |
454 | const char *resolves_to; | |
bc5fd6d3 | 455 | |
7bd9bcf3 MH |
456 | if (!(flags & REF_ISSYMREF)) |
457 | return 0; | |
bc5fd6d3 | 458 | |
744c040b | 459 | resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL); |
7bd9bcf3 MH |
460 | if (!resolves_to |
461 | || (d->refname | |
462 | ? strcmp(resolves_to, d->refname) | |
463 | : !string_list_has_string(d->refnames, resolves_to))) { | |
464 | return 0; | |
465 | } | |
bc5fd6d3 | 466 | |
7bd9bcf3 MH |
467 | fprintf(d->fp, d->msg_fmt, refname); |
468 | fputc('\n', d->fp); | |
469 | return 0; | |
bc5fd6d3 MH |
470 | } |
471 | ||
7bd9bcf3 | 472 | void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) |
f348ac92 | 473 | { |
7bd9bcf3 MH |
474 | struct warn_if_dangling_data data; |
475 | ||
476 | data.fp = fp; | |
477 | data.refname = refname; | |
478 | data.refnames = NULL; | |
479 | data.msg_fmt = msg_fmt; | |
480 | for_each_rawref(warn_if_dangling_symref, &data); | |
f348ac92 MH |
481 | } |
482 | ||
7bd9bcf3 | 483 | void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames) |
432ad41e | 484 | { |
7bd9bcf3 | 485 | struct warn_if_dangling_data data; |
432ad41e | 486 | |
7bd9bcf3 MH |
487 | data.fp = fp; |
488 | data.refname = NULL; | |
489 | data.refnames = refnames; | |
490 | data.msg_fmt = msg_fmt; | |
491 | for_each_rawref(warn_if_dangling_symref, &data); | |
432ad41e MH |
492 | } |
493 | ||
7d2df051 NTND |
494 | int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
495 | { | |
496 | return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data); | |
497 | } | |
498 | ||
7bd9bcf3 | 499 | int for_each_tag_ref(each_ref_fn fn, void *cb_data) |
432ad41e | 500 | { |
23a3f0cb | 501 | return refs_for_each_tag_ref(get_main_ref_store(the_repository), fn, cb_data); |
432ad41e MH |
502 | } |
503 | ||
7d2df051 NTND |
504 | int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
505 | { | |
506 | return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data); | |
432ad41e MH |
507 | } |
508 | ||
7bd9bcf3 | 509 | int for_each_branch_ref(each_ref_fn fn, void *cb_data) |
432ad41e | 510 | { |
23a3f0cb | 511 | return refs_for_each_branch_ref(get_main_ref_store(the_repository), fn, cb_data); |
432ad41e MH |
512 | } |
513 | ||
7d2df051 NTND |
514 | int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
515 | { | |
516 | return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data); | |
202a56a9 | 517 | } |
432ad41e | 518 | |
7bd9bcf3 | 519 | int for_each_remote_ref(each_ref_fn fn, void *cb_data) |
e9c4c111 | 520 | { |
23a3f0cb | 521 | return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data); |
202a56a9 MH |
522 | } |
523 | ||
7bd9bcf3 MH |
524 | int head_ref_namespaced(each_ref_fn fn, void *cb_data) |
525 | { | |
526 | struct strbuf buf = STRBUF_INIT; | |
527 | int ret = 0; | |
528 | struct object_id oid; | |
529 | int flag; | |
c774aab9 | 530 | |
7bd9bcf3 | 531 | strbuf_addf(&buf, "%sHEAD", get_git_namespace()); |
34c290a6 | 532 | if (!read_ref_full(buf.buf, RESOLVE_REF_READING, &oid, &flag)) |
7bd9bcf3 MH |
533 | ret = fn(buf.buf, &oid, flag, cb_data); |
534 | strbuf_release(&buf); | |
c774aab9 | 535 | |
7bd9bcf3 | 536 | return ret; |
e9c4c111 | 537 | } |
c774aab9 | 538 | |
65516f58 RA |
539 | void normalize_glob_ref(struct string_list_item *item, const char *prefix, |
540 | const char *pattern) | |
541 | { | |
542 | struct strbuf normalized_pattern = STRBUF_INIT; | |
543 | ||
544 | if (*pattern == '/') | |
545 | BUG("pattern must not start with '/'"); | |
546 | ||
b877e617 | 547 | if (prefix) |
65516f58 | 548 | strbuf_addstr(&normalized_pattern, prefix); |
b877e617 DS |
549 | else if (!starts_with(pattern, "refs/") && |
550 | strcmp(pattern, "HEAD")) | |
65516f58 | 551 | strbuf_addstr(&normalized_pattern, "refs/"); |
b877e617 DS |
552 | /* |
553 | * NEEDSWORK: Special case other symrefs such as REBASE_HEAD, | |
554 | * MERGE_HEAD, etc. | |
555 | */ | |
556 | ||
65516f58 RA |
557 | strbuf_addstr(&normalized_pattern, pattern); |
558 | strbuf_strip_suffix(&normalized_pattern, "/"); | |
559 | ||
560 | item->string = strbuf_detach(&normalized_pattern, NULL); | |
561 | item->util = has_glob_specials(pattern) ? NULL : item->string; | |
562 | strbuf_release(&normalized_pattern); | |
563 | } | |
564 | ||
7bd9bcf3 MH |
565 | int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, |
566 | const char *prefix, void *cb_data) | |
662428f4 | 567 | { |
7bd9bcf3 MH |
568 | struct strbuf real_pattern = STRBUF_INIT; |
569 | struct ref_filter filter; | |
570 | int ret; | |
d08bae7e | 571 | |
59556548 | 572 | if (!prefix && !starts_with(pattern, "refs/")) |
d08bae7e | 573 | strbuf_addstr(&real_pattern, "refs/"); |
b09fe971 IL |
574 | else if (prefix) |
575 | strbuf_addstr(&real_pattern, prefix); | |
d08bae7e IL |
576 | strbuf_addstr(&real_pattern, pattern); |
577 | ||
894a9d33 | 578 | if (!has_glob_specials(pattern)) { |
9517e6b8 | 579 | /* Append implied '/' '*' if not present. */ |
00b6c178 | 580 | strbuf_complete(&real_pattern, '/'); |
d08bae7e IL |
581 | /* No need to check for '*', there is none. */ |
582 | strbuf_addch(&real_pattern, '*'); | |
583 | } | |
584 | ||
585 | filter.pattern = real_pattern.buf; | |
9ab9b5df | 586 | filter.prefix = prefix; |
d08bae7e IL |
587 | filter.fn = fn; |
588 | filter.cb_data = cb_data; | |
589 | ret = for_each_ref(filter_refs, &filter); | |
590 | ||
591 | strbuf_release(&real_pattern); | |
592 | return ret; | |
593 | } | |
594 | ||
b09fe971 IL |
595 | int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) |
596 | { | |
597 | return for_each_glob_ref_in(fn, pattern, NULL, cb_data); | |
598 | } | |
599 | ||
4577e483 | 600 | const char *prettify_refname(const char *name) |
a9c37a72 | 601 | { |
3e5b36c6 SG |
602 | if (skip_prefix(name, "refs/heads/", &name) || |
603 | skip_prefix(name, "refs/tags/", &name) || | |
604 | skip_prefix(name, "refs/remotes/", &name)) | |
605 | ; /* nothing */ | |
606 | return name; | |
a9c37a72 DB |
607 | } |
608 | ||
54457fe5 | 609 | static const char *ref_rev_parse_rules[] = { |
79803322 SP |
610 | "%.*s", |
611 | "refs/%.*s", | |
612 | "refs/tags/%.*s", | |
613 | "refs/heads/%.*s", | |
614 | "refs/remotes/%.*s", | |
615 | "refs/remotes/%.*s/HEAD", | |
616 | NULL | |
617 | }; | |
618 | ||
60650a48 JH |
619 | #define NUM_REV_PARSE_RULES (ARRAY_SIZE(ref_rev_parse_rules) - 1) |
620 | ||
621 | /* | |
622 | * Is it possible that the caller meant full_name with abbrev_name? | |
623 | * If so return a non-zero value to signal "yes"; the magnitude of | |
624 | * the returned value gives the precedence used for disambiguation. | |
625 | * | |
626 | * If abbrev_name cannot mean full_name, return 0. | |
627 | */ | |
54457fe5 | 628 | int refname_match(const char *abbrev_name, const char *full_name) |
79803322 SP |
629 | { |
630 | const char **p; | |
631 | const int abbrev_name_len = strlen(abbrev_name); | |
60650a48 | 632 | const int num_rules = NUM_REV_PARSE_RULES; |
79803322 | 633 | |
60650a48 JH |
634 | for (p = ref_rev_parse_rules; *p; p++) |
635 | if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) | |
636 | return &ref_rev_parse_rules[num_rules] - p; | |
79803322 SP |
637 | |
638 | return 0; | |
639 | } | |
640 | ||
b4be7410 BW |
641 | /* |
642 | * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add | |
643 | * the results to 'prefixes' | |
644 | */ | |
c972bf4c | 645 | void expand_ref_prefix(struct strvec *prefixes, const char *prefix) |
b4be7410 BW |
646 | { |
647 | const char **p; | |
648 | int len = strlen(prefix); | |
649 | ||
650 | for (p = ref_rev_parse_rules; *p; p++) | |
c972bf4c | 651 | strvec_pushf(prefixes, *p, len, prefix); |
b4be7410 BW |
652 | } |
653 | ||
675704c7 JS |
654 | static const char default_branch_name_advice[] = N_( |
655 | "Using '%s' as the name for the initial branch. This default branch name\n" | |
656 | "is subject to change. To configure the initial branch name to use in all\n" | |
657 | "of your new repositories, which will suppress this warning, call:\n" | |
658 | "\n" | |
659 | "\tgit config --global init.defaultBranch <name>\n" | |
660 | "\n" | |
661 | "Names commonly chosen instead of 'master' are 'main', 'trunk' and\n" | |
662 | "'development'. The just-created branch can be renamed via this command:\n" | |
663 | "\n" | |
664 | "\tgit branch -m <name>\n" | |
665 | ); | |
666 | ||
cc0f13c5 | 667 | char *repo_default_branch_name(struct repository *r, int quiet) |
8747ebb7 DGW |
668 | { |
669 | const char *config_key = "init.defaultbranch"; | |
670 | const char *config_display_key = "init.defaultBranch"; | |
671 | char *ret = NULL, *full_ref; | |
704fed9e | 672 | const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME"); |
8747ebb7 | 673 | |
704fed9e JS |
674 | if (env && *env) |
675 | ret = xstrdup(env); | |
676 | else if (repo_config_get_string(r, config_key, &ret) < 0) | |
8747ebb7 DGW |
677 | die(_("could not retrieve `%s`"), config_display_key); |
678 | ||
675704c7 | 679 | if (!ret) { |
8747ebb7 | 680 | ret = xstrdup("master"); |
675704c7 JS |
681 | if (!quiet) |
682 | advise(_(default_branch_name_advice), ret); | |
683 | } | |
8747ebb7 DGW |
684 | |
685 | full_ref = xstrfmt("refs/heads/%s", ret); | |
686 | if (check_refname_format(full_ref, 0)) | |
687 | die(_("invalid branch name: %s = %s"), config_display_key, ret); | |
688 | free(full_ref); | |
689 | ||
690 | return ret; | |
691 | } | |
692 | ||
cc0f13c5 | 693 | const char *git_default_branch_name(int quiet) |
8747ebb7 DGW |
694 | { |
695 | static char *ret; | |
696 | ||
697 | if (!ret) | |
cc0f13c5 | 698 | ret = repo_default_branch_name(the_repository, quiet); |
8747ebb7 DGW |
699 | |
700 | return ret; | |
701 | } | |
702 | ||
ff74f7f1 JH |
703 | /* |
704 | * *string and *len will only be substituted, and *string returned (for | |
705 | * later free()ing) if the string passed in is a magic short-hand form | |
706 | * to name a branch. | |
707 | */ | |
8f56e9d4 | 708 | static char *substitute_branch_name(struct repository *r, |
f24c30e0 JT |
709 | const char **string, int *len, |
710 | int nonfatal_dangling_mark) | |
ff74f7f1 JH |
711 | { |
712 | struct strbuf buf = STRBUF_INIT; | |
f24c30e0 JT |
713 | struct interpret_branch_name_options options = { |
714 | .nonfatal_dangling_mark = nonfatal_dangling_mark | |
715 | }; | |
a4f66a78 | 716 | int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options); |
ff74f7f1 JH |
717 | |
718 | if (ret == *len) { | |
719 | size_t size; | |
720 | *string = strbuf_detach(&buf, &size); | |
721 | *len = size; | |
722 | return (char *)*string; | |
723 | } | |
724 | ||
725 | return NULL; | |
726 | } | |
727 | ||
d8984c53 | 728 | int repo_dwim_ref(struct repository *r, const char *str, int len, |
f24c30e0 | 729 | struct object_id *oid, char **ref, int nonfatal_dangling_mark) |
ff74f7f1 | 730 | { |
f24c30e0 JT |
731 | char *last_branch = substitute_branch_name(r, &str, &len, |
732 | nonfatal_dangling_mark); | |
d8984c53 | 733 | int refs_found = expand_ref(r, str, len, oid, ref); |
41da7111 NTND |
734 | free(last_branch); |
735 | return refs_found; | |
736 | } | |
737 | ||
0b1dbf53 NTND |
738 | int expand_ref(struct repository *repo, const char *str, int len, |
739 | struct object_id *oid, char **ref) | |
41da7111 | 740 | { |
ff74f7f1 JH |
741 | const char **p, *r; |
742 | int refs_found = 0; | |
6cd4a898 | 743 | struct strbuf fullref = STRBUF_INIT; |
ff74f7f1 JH |
744 | |
745 | *ref = NULL; | |
746 | for (p = ref_rev_parse_rules; *p; p++) { | |
cca5fa64 | 747 | struct object_id oid_from_ref; |
748 | struct object_id *this_result; | |
ff74f7f1 | 749 | int flag; |
6582bd31 | 750 | struct ref_store *refs = get_main_ref_store(repo); |
ff74f7f1 | 751 | |
cca5fa64 | 752 | this_result = refs_found ? &oid_from_ref : oid; |
6cd4a898 JK |
753 | strbuf_reset(&fullref); |
754 | strbuf_addf(&fullref, *p, len, str); | |
f1da24ca | 755 | r = refs_resolve_ref_unsafe(refs, fullref.buf, |
6582bd31 | 756 | RESOLVE_REF_READING, |
ce14de03 | 757 | this_result, &flag); |
ff74f7f1 JH |
758 | if (r) { |
759 | if (!refs_found++) | |
760 | *ref = xstrdup(r); | |
761 | if (!warn_ambiguous_refs) | |
762 | break; | |
6cd4a898 | 763 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) { |
661558f0 | 764 | warning(_("ignoring dangling symref %s"), fullref.buf); |
6cd4a898 | 765 | } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) { |
661558f0 | 766 | warning(_("ignoring broken ref %s"), fullref.buf); |
55956350 | 767 | } |
ff74f7f1 | 768 | } |
6cd4a898 | 769 | strbuf_release(&fullref); |
ff74f7f1 JH |
770 | return refs_found; |
771 | } | |
772 | ||
56700903 NTND |
773 | int repo_dwim_log(struct repository *r, const char *str, int len, |
774 | struct object_id *oid, char **log) | |
ff74f7f1 | 775 | { |
56700903 | 776 | struct ref_store *refs = get_main_ref_store(r); |
f24c30e0 | 777 | char *last_branch = substitute_branch_name(r, &str, &len, 0); |
ff74f7f1 JH |
778 | const char **p; |
779 | int logs_found = 0; | |
6cd4a898 | 780 | struct strbuf path = STRBUF_INIT; |
ff74f7f1 JH |
781 | |
782 | *log = NULL; | |
783 | for (p = ref_rev_parse_rules; *p; p++) { | |
334dc52f | 784 | struct object_id hash; |
ff74f7f1 JH |
785 | const char *ref, *it; |
786 | ||
6cd4a898 JK |
787 | strbuf_reset(&path); |
788 | strbuf_addf(&path, *p, len, str); | |
56700903 NTND |
789 | ref = refs_resolve_ref_unsafe(refs, path.buf, |
790 | RESOLVE_REF_READING, | |
ce14de03 | 791 | oid ? &hash : NULL, NULL); |
ff74f7f1 JH |
792 | if (!ref) |
793 | continue; | |
56700903 | 794 | if (refs_reflog_exists(refs, path.buf)) |
6cd4a898 | 795 | it = path.buf; |
56700903 NTND |
796 | else if (strcmp(ref, path.buf) && |
797 | refs_reflog_exists(refs, ref)) | |
ff74f7f1 JH |
798 | it = ref; |
799 | else | |
800 | continue; | |
801 | if (!logs_found++) { | |
802 | *log = xstrdup(it); | |
6f45ec88 ÆAB |
803 | if (oid) |
804 | oidcpy(oid, &hash); | |
ff74f7f1 | 805 | } |
7bd9bcf3 MH |
806 | if (!warn_ambiguous_refs) |
807 | break; | |
c0277d15 | 808 | } |
6cd4a898 | 809 | strbuf_release(&path); |
7bd9bcf3 MH |
810 | free(last_branch); |
811 | return logs_found; | |
2ddb5d17 BK |
812 | } |
813 | ||
56700903 NTND |
814 | int dwim_log(const char *str, int len, struct object_id *oid, char **log) |
815 | { | |
816 | return repo_dwim_log(the_repository, str, len, oid, log); | |
817 | } | |
818 | ||
71e54734 | 819 | int is_per_worktree_ref(const char *refname) |
266b1827 | 820 | { |
55dd8b91 HWN |
821 | return starts_with(refname, "refs/worktree/") || |
822 | starts_with(refname, "refs/bisect/") || | |
823 | starts_with(refname, "refs/rewritten/"); | |
266b1827 DT |
824 | } |
825 | ||
826 | static int is_pseudoref_syntax(const char *refname) | |
827 | { | |
828 | const char *c; | |
829 | ||
830 | for (c = refname; *c; c++) { | |
831 | if (!isupper(*c) && *c != '-' && *c != '_') | |
832 | return 0; | |
833 | } | |
834 | ||
71e54734 HWN |
835 | /* |
836 | * HEAD is not a pseudoref, but it certainly uses the | |
837 | * pseudoref syntax. | |
838 | */ | |
266b1827 DT |
839 | return 1; |
840 | } | |
841 | ||
71e54734 HWN |
842 | static int is_current_worktree_ref(const char *ref) { |
843 | return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref); | |
3a3b9d8c NTND |
844 | } |
845 | ||
71e54734 HWN |
846 | enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref, |
847 | const char **worktree_name, int *worktree_name_length, | |
848 | const char **bare_refname) | |
3a3b9d8c | 849 | { |
71e54734 HWN |
850 | const char *name_dummy; |
851 | int name_length_dummy; | |
852 | const char *ref_dummy; | |
3a3b9d8c | 853 | |
71e54734 HWN |
854 | if (!worktree_name) |
855 | worktree_name = &name_dummy; | |
856 | if (!worktree_name_length) | |
857 | worktree_name_length = &name_length_dummy; | |
858 | if (!bare_refname) | |
859 | bare_refname = &ref_dummy; | |
860 | ||
861 | if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) { | |
862 | const char *slash = strchr(*bare_refname, '/'); | |
863 | ||
864 | *worktree_name = *bare_refname; | |
865 | if (!slash) { | |
866 | *worktree_name_length = strlen(*worktree_name); | |
867 | ||
868 | /* This is an error condition, and the caller tell because the bare_refname is "" */ | |
869 | *bare_refname = *worktree_name + *worktree_name_length; | |
870 | return REF_WORKTREE_OTHER; | |
871 | } | |
872 | ||
873 | *worktree_name_length = slash - *bare_refname; | |
874 | *bare_refname = slash + 1; | |
875 | ||
876 | if (is_current_worktree_ref(*bare_refname)) | |
877 | return REF_WORKTREE_OTHER; | |
878 | } | |
879 | ||
880 | *worktree_name = NULL; | |
881 | *worktree_name_length = 0; | |
882 | ||
883 | if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname) | |
884 | && is_current_worktree_ref(*bare_refname)) | |
885 | return REF_WORKTREE_MAIN; | |
886 | ||
887 | *bare_refname = maybe_worktree_ref; | |
888 | if (is_current_worktree_ref(maybe_worktree_ref)) | |
889 | return REF_WORKTREE_CURRENT; | |
890 | ||
891 | return REF_WORKTREE_SHARED; | |
266b1827 DT |
892 | } |
893 | ||
4ff0f01c MH |
894 | long get_files_ref_lock_timeout_ms(void) |
895 | { | |
896 | static int configured = 0; | |
897 | ||
898 | /* The default timeout is 100 ms: */ | |
899 | static int timeout_ms = 100; | |
900 | ||
901 | if (!configured) { | |
902 | git_config_get_int("core.filesreflocktimeout", &timeout_ms); | |
903 | configured = 1; | |
904 | } | |
905 | ||
906 | return timeout_ms; | |
907 | } | |
908 | ||
c0fe4e8b NTND |
909 | int refs_delete_ref(struct ref_store *refs, const char *msg, |
910 | const char *refname, | |
2616a5e5 | 911 | const struct object_id *old_oid, |
c0fe4e8b | 912 | unsigned int flags) |
41b625b0 | 913 | { |
7521cc46 | 914 | struct ref_transaction *transaction; |
a4c653df | 915 | struct strbuf err = STRBUF_INIT; |
8b5157e4 | 916 | |
c6da34a6 | 917 | transaction = ref_store_transaction_begin(refs, &err); |
7521cc46 | 918 | if (!transaction || |
89f3bbdd | 919 | ref_transaction_delete(transaction, refname, old_oid, |
755b49ae | 920 | flags, msg, &err) || |
db7516ab | 921 | ref_transaction_commit(transaction, &err)) { |
7521cc46 RS |
922 | error("%s", err.buf); |
923 | ref_transaction_free(transaction); | |
924 | strbuf_release(&err); | |
c0277d15 | 925 | return 1; |
41b625b0 | 926 | } |
7bd9bcf3 MH |
927 | ref_transaction_free(transaction); |
928 | strbuf_release(&err); | |
b531394d BC |
929 | return 0; |
930 | } | |
41b625b0 | 931 | |
c0fe4e8b | 932 | int delete_ref(const char *msg, const char *refname, |
2616a5e5 | 933 | const struct object_id *old_oid, unsigned int flags) |
c0fe4e8b | 934 | { |
23a3f0cb | 935 | return refs_delete_ref(get_main_ref_store(the_repository), msg, refname, |
2616a5e5 | 936 | old_oid, flags); |
c0fe4e8b NTND |
937 | } |
938 | ||
523fa69c | 939 | static void copy_reflog_msg(struct strbuf *sb, const char *msg) |
0ec29a47 | 940 | { |
0ec29a47 JH |
941 | char c; |
942 | int wasspace = 1; | |
8b5157e4 | 943 | |
0ec29a47 JH |
944 | while ((c = *msg++)) { |
945 | if (wasspace && isspace(c)) | |
946 | continue; | |
947 | wasspace = isspace(c); | |
948 | if (wasspace) | |
949 | c = ' '; | |
80a6c207 | 950 | strbuf_addch(sb, c); |
a4c653df | 951 | } |
80a6c207 | 952 | strbuf_rtrim(sb); |
0ec29a47 | 953 | } |
8b5157e4 | 954 | |
523fa69c JH |
955 | static char *normalize_reflog_message(const char *msg) |
956 | { | |
957 | struct strbuf sb = STRBUF_INIT; | |
958 | ||
959 | if (msg && *msg) | |
960 | copy_reflog_msg(&sb, msg); | |
961 | return strbuf_detach(&sb, NULL); | |
962 | } | |
963 | ||
4cb77009 | 964 | int should_autocreate_reflog(const char *refname) |
4e2bef57 | 965 | { |
341fb286 CW |
966 | switch (log_all_ref_updates) { |
967 | case LOG_REFS_ALWAYS: | |
968 | return 1; | |
969 | case LOG_REFS_NORMAL: | |
970 | return starts_with(refname, "refs/heads/") || | |
971 | starts_with(refname, "refs/remotes/") || | |
972 | starts_with(refname, "refs/notes/") || | |
973 | !strcmp(refname, "HEAD"); | |
974 | default: | |
4e2bef57 | 975 | return 0; |
341fb286 | 976 | } |
4e2bef57 DT |
977 | } |
978 | ||
e7e0f26e | 979 | int is_branch(const char *refname) |
c3b0dec5 | 980 | { |
59556548 | 981 | return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/"); |
41b625b0 NP |
982 | } |
983 | ||
4207ed28 RS |
984 | struct read_ref_at_cb { |
985 | const char *refname; | |
dddbad72 | 986 | timestamp_t at_time; |
4207ed28 RS |
987 | int cnt; |
988 | int reccnt; | |
8eb36d94 | 989 | struct object_id *oid; |
4207ed28 RS |
990 | int found_it; |
991 | ||
8eb36d94 | 992 | struct object_id ooid; |
993 | struct object_id noid; | |
4207ed28 | 994 | int tz; |
dddbad72 | 995 | timestamp_t date; |
4207ed28 | 996 | char **msg; |
dddbad72 | 997 | timestamp_t *cutoff_time; |
4207ed28 RS |
998 | int *cutoff_tz; |
999 | int *cutoff_cnt; | |
1000 | }; | |
1001 | ||
95c2a718 DL |
1002 | static void set_read_ref_cutoffs(struct read_ref_at_cb *cb, |
1003 | timestamp_t timestamp, int tz, const char *message) | |
1004 | { | |
1005 | if (cb->msg) | |
1006 | *cb->msg = xstrdup(message); | |
1007 | if (cb->cutoff_time) | |
1008 | *cb->cutoff_time = timestamp; | |
1009 | if (cb->cutoff_tz) | |
1010 | *cb->cutoff_tz = tz; | |
1011 | if (cb->cutoff_cnt) | |
1012 | *cb->cutoff_cnt = cb->reccnt; | |
1013 | } | |
1014 | ||
9461d272 | 1015 | static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid, |
5cf88fd8 | 1016 | const char *email UNUSED, |
c006e9fa JK |
1017 | timestamp_t timestamp, int tz, |
1018 | const char *message, void *cb_data) | |
4207ed28 RS |
1019 | { |
1020 | struct read_ref_at_cb *cb = cb_data; | |
6436a202 | 1021 | int reached_count; |
4207ed28 | 1022 | |
4207ed28 RS |
1023 | cb->tz = tz; |
1024 | cb->date = timestamp; | |
1025 | ||
6436a202 DL |
1026 | /* |
1027 | * It is not possible for cb->cnt == 0 on the first iteration because | |
1028 | * that special case is handled in read_ref_at(). | |
1029 | */ | |
1030 | if (cb->cnt > 0) | |
1031 | cb->cnt--; | |
1032 | reached_count = cb->cnt == 0 && !is_null_oid(ooid); | |
1033 | if (timestamp <= cb->at_time || reached_count) { | |
95c2a718 | 1034 | set_read_ref_cutoffs(cb, timestamp, tz, message); |
4207ed28 | 1035 | /* |
78fb4579 | 1036 | * we have not yet updated cb->[n|o]oid so they still |
4207ed28 RS |
1037 | * hold the values for the previous record. |
1038 | */ | |
6436a202 DL |
1039 | if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid)) |
1040 | warning(_("log for ref %s has gap after %s"), | |
a5481a6c | 1041 | cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); |
6436a202 DL |
1042 | if (reached_count) |
1043 | oidcpy(cb->oid, ooid); | |
1044 | else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time) | |
8eb36d94 | 1045 | oidcpy(cb->oid, noid); |
9001dc2a | 1046 | else if (!oideq(noid, cb->oid)) |
661558f0 | 1047 | warning(_("log for ref %s unexpectedly ended on %s"), |
4207ed28 | 1048 | cb->refname, show_date(cb->date, cb->tz, |
a5481a6c | 1049 | DATE_MODE(RFC2822))); |
4207ed28 | 1050 | cb->found_it = 1; |
4207ed28 | 1051 | } |
95c2a718 | 1052 | cb->reccnt++; |
8eb36d94 | 1053 | oidcpy(&cb->ooid, ooid); |
1054 | oidcpy(&cb->noid, noid); | |
6436a202 DL |
1055 | return cb->found_it; |
1056 | } | |
1057 | ||
5cf88fd8 | 1058 | static int read_ref_at_ent_newest(struct object_id *ooid UNUSED, |
63e14ee2 | 1059 | struct object_id *noid, |
5cf88fd8 | 1060 | const char *email UNUSED, |
63e14ee2 JK |
1061 | timestamp_t timestamp, int tz, |
1062 | const char *message, void *cb_data) | |
6436a202 DL |
1063 | { |
1064 | struct read_ref_at_cb *cb = cb_data; | |
1065 | ||
1066 | set_read_ref_cutoffs(cb, timestamp, tz, message); | |
1067 | oidcpy(cb->oid, noid); | |
1068 | /* We just want the first entry */ | |
1069 | return 1; | |
4207ed28 RS |
1070 | } |
1071 | ||
9461d272 | 1072 | static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid, |
5cf88fd8 | 1073 | const char *email UNUSED, |
c006e9fa JK |
1074 | timestamp_t timestamp, int tz, |
1075 | const char *message, void *cb_data) | |
4207ed28 RS |
1076 | { |
1077 | struct read_ref_at_cb *cb = cb_data; | |
1078 | ||
95c2a718 | 1079 | set_read_ref_cutoffs(cb, timestamp, tz, message); |
8eb36d94 | 1080 | oidcpy(cb->oid, ooid); |
1081 | if (is_null_oid(cb->oid)) | |
1082 | oidcpy(cb->oid, noid); | |
4207ed28 RS |
1083 | /* We just want the first entry */ |
1084 | return 1; | |
16d7cc90 JH |
1085 | } |
1086 | ||
7fdff474 NTND |
1087 | int read_ref_at(struct ref_store *refs, const char *refname, |
1088 | unsigned int flags, timestamp_t at_time, int cnt, | |
8eb36d94 | 1089 | struct object_id *oid, char **msg, |
dddbad72 | 1090 | timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt) |
d556fae2 | 1091 | { |
4207ed28 | 1092 | struct read_ref_at_cb cb; |
d556fae2 | 1093 | |
4207ed28 RS |
1094 | memset(&cb, 0, sizeof(cb)); |
1095 | cb.refname = refname; | |
1096 | cb.at_time = at_time; | |
1097 | cb.cnt = cnt; | |
1098 | cb.msg = msg; | |
1099 | cb.cutoff_time = cutoff_time; | |
1100 | cb.cutoff_tz = cutoff_tz; | |
1101 | cb.cutoff_cnt = cutoff_cnt; | |
8eb36d94 | 1102 | cb.oid = oid; |
4207ed28 | 1103 | |
6436a202 DL |
1104 | if (cb.cnt == 0) { |
1105 | refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb); | |
1106 | return 0; | |
1107 | } | |
1108 | ||
7fdff474 | 1109 | refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb); |
4207ed28 | 1110 | |
c41a87dd | 1111 | if (!cb.reccnt) { |
321c89bf | 1112 | if (flags & GET_OID_QUIETLY) |
c41a87dd DA |
1113 | exit(128); |
1114 | else | |
661558f0 | 1115 | die(_("log for %s is empty"), refname); |
c41a87dd | 1116 | } |
4207ed28 RS |
1117 | if (cb.found_it) |
1118 | return 0; | |
1119 | ||
7fdff474 | 1120 | refs_for_each_reflog_ent(refs, refname, read_ref_at_ent_oldest, &cb); |
d556fae2 | 1121 | |
16d7cc90 | 1122 | return 1; |
d556fae2 | 1123 | } |
2ff81662 | 1124 | |
c0fe4e8b NTND |
1125 | struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, |
1126 | struct strbuf *err) | |
caa4046c | 1127 | { |
c0fe4e8b | 1128 | struct ref_transaction *tr; |
5a603b04 JN |
1129 | assert(err); |
1130 | ||
ca56dadb | 1131 | CALLOC_ARRAY(tr, 1); |
c0fe4e8b NTND |
1132 | tr->ref_store = refs; |
1133 | return tr; | |
1134 | } | |
1135 | ||
1136 | struct ref_transaction *ref_transaction_begin(struct strbuf *err) | |
1137 | { | |
c6da34a6 | 1138 | return ref_store_transaction_begin(get_main_ref_store(the_repository), err); |
caa4046c MH |
1139 | } |
1140 | ||
026bd1d3 | 1141 | void ref_transaction_free(struct ref_transaction *transaction) |
caa4046c | 1142 | { |
43a2dfde | 1143 | size_t i; |
caa4046c | 1144 | |
1b07255c RS |
1145 | if (!transaction) |
1146 | return; | |
1147 | ||
30173b88 MH |
1148 | switch (transaction->state) { |
1149 | case REF_TRANSACTION_OPEN: | |
1150 | case REF_TRANSACTION_CLOSED: | |
1151 | /* OK */ | |
1152 | break; | |
1153 | case REF_TRANSACTION_PREPARED: | |
033abf97 | 1154 | BUG("free called on a prepared reference transaction"); |
30173b88 MH |
1155 | break; |
1156 | default: | |
033abf97 | 1157 | BUG("unexpected reference transaction state"); |
30173b88 MH |
1158 | break; |
1159 | } | |
1160 | ||
db7516ab RS |
1161 | for (i = 0; i < transaction->nr; i++) { |
1162 | free(transaction->updates[i]->msg); | |
88615910 | 1163 | free(transaction->updates[i]); |
db7516ab | 1164 | } |
caa4046c MH |
1165 | free(transaction->updates); |
1166 | free(transaction); | |
1167 | } | |
1168 | ||
71564516 MH |
1169 | struct ref_update *ref_transaction_add_update( |
1170 | struct ref_transaction *transaction, | |
1171 | const char *refname, unsigned int flags, | |
89f3bbdd | 1172 | const struct object_id *new_oid, |
1173 | const struct object_id *old_oid, | |
71564516 | 1174 | const char *msg) |
caa4046c | 1175 | { |
96ffc06f | 1176 | struct ref_update *update; |
71564516 MH |
1177 | |
1178 | if (transaction->state != REF_TRANSACTION_OPEN) | |
033abf97 | 1179 | BUG("update called for transaction that is not open"); |
71564516 | 1180 | |
96ffc06f | 1181 | FLEX_ALLOC_STR(update, refname, refname); |
caa4046c MH |
1182 | ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); |
1183 | transaction->updates[transaction->nr++] = update; | |
71564516 MH |
1184 | |
1185 | update->flags = flags; | |
1186 | ||
1187 | if (flags & REF_HAVE_NEW) | |
89f3bbdd | 1188 | oidcpy(&update->new_oid, new_oid); |
71564516 | 1189 | if (flags & REF_HAVE_OLD) |
89f3bbdd | 1190 | oidcpy(&update->old_oid, old_oid); |
523fa69c | 1191 | update->msg = normalize_reflog_message(msg); |
caa4046c MH |
1192 | return update; |
1193 | } | |
1194 | ||
8e34800e RS |
1195 | int ref_transaction_update(struct ref_transaction *transaction, |
1196 | const char *refname, | |
89f3bbdd | 1197 | const struct object_id *new_oid, |
1198 | const struct object_id *old_oid, | |
1d147bdf | 1199 | unsigned int flags, const char *msg, |
8e34800e | 1200 | struct strbuf *err) |
caa4046c | 1201 | { |
5a603b04 JN |
1202 | assert(err); |
1203 | ||
3c966c7b HWN |
1204 | if (!(flags & REF_SKIP_REFNAME_VERIFICATION) && |
1205 | ((new_oid && !is_null_oid(new_oid)) ? | |
1206 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) : | |
1207 | !refname_is_safe(refname))) { | |
661558f0 | 1208 | strbuf_addf(err, _("refusing to update ref with bad name '%s'"), |
d0f810f0 RS |
1209 | refname); |
1210 | return -1; | |
1211 | } | |
1212 | ||
a9bbbcec MH |
1213 | if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS) |
1214 | BUG("illegal flags 0x%x passed to ref_transaction_update()", flags); | |
c788c54c | 1215 | |
49f1eb3b JK |
1216 | /* |
1217 | * Clear flags outside the allowed set; this should be a noop because | |
1218 | * of the BUG() check above, but it works around a -Wnonnull warning | |
1219 | * with some versions of "gcc -O3". | |
1220 | */ | |
1221 | flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; | |
1222 | ||
89f3bbdd | 1223 | flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); |
71564516 MH |
1224 | |
1225 | ref_transaction_add_update(transaction, refname, flags, | |
89f3bbdd | 1226 | new_oid, old_oid, msg); |
8e34800e | 1227 | return 0; |
caa4046c MH |
1228 | } |
1229 | ||
b416af5b RS |
1230 | int ref_transaction_create(struct ref_transaction *transaction, |
1231 | const char *refname, | |
89f3bbdd | 1232 | const struct object_id *new_oid, |
fec14ec3 | 1233 | unsigned int flags, const char *msg, |
b416af5b | 1234 | struct strbuf *err) |
caa4046c | 1235 | { |
d097a23b DS |
1236 | if (!new_oid || is_null_oid(new_oid)) { |
1237 | strbuf_addf(err, "'%s' has a null OID", refname); | |
1238 | return 1; | |
1239 | } | |
89f3bbdd | 1240 | return ref_transaction_update(transaction, refname, new_oid, |
14228447 | 1241 | null_oid(), flags, msg, err); |
caa4046c MH |
1242 | } |
1243 | ||
8c8bdc0d RS |
1244 | int ref_transaction_delete(struct ref_transaction *transaction, |
1245 | const char *refname, | |
89f3bbdd | 1246 | const struct object_id *old_oid, |
fb5a6bb6 | 1247 | unsigned int flags, const char *msg, |
8c8bdc0d | 1248 | struct strbuf *err) |
caa4046c | 1249 | { |
89f3bbdd | 1250 | if (old_oid && is_null_oid(old_oid)) |
033abf97 | 1251 | BUG("delete called with old_oid set to zeros"); |
1d147bdf | 1252 | return ref_transaction_update(transaction, refname, |
14228447 | 1253 | null_oid(), old_oid, |
1d147bdf | 1254 | flags, msg, err); |
caa4046c MH |
1255 | } |
1256 | ||
16180334 MH |
1257 | int ref_transaction_verify(struct ref_transaction *transaction, |
1258 | const char *refname, | |
89f3bbdd | 1259 | const struct object_id *old_oid, |
16180334 MH |
1260 | unsigned int flags, |
1261 | struct strbuf *err) | |
1262 | { | |
89f3bbdd | 1263 | if (!old_oid) |
033abf97 | 1264 | BUG("verify called with old_oid set to NULL"); |
16180334 | 1265 | return ref_transaction_update(transaction, refname, |
89f3bbdd | 1266 | NULL, old_oid, |
16180334 MH |
1267 | flags, NULL, err); |
1268 | } | |
1269 | ||
c0fe4e8b | 1270 | int refs_update_ref(struct ref_store *refs, const char *msg, |
ae077771 | 1271 | const char *refname, const struct object_id *new_oid, |
1272 | const struct object_id *old_oid, unsigned int flags, | |
c0fe4e8b | 1273 | enum action_on_err onerr) |
4738a333 | 1274 | { |
74ec19d4 | 1275 | struct ref_transaction *t = NULL; |
b4d75ac1 | 1276 | struct strbuf err = STRBUF_INIT; |
74ec19d4 | 1277 | int ret = 0; |
b4d75ac1 | 1278 | |
c6da34a6 | 1279 | t = ref_store_transaction_begin(refs, &err); |
09743417 HWN |
1280 | if (!t || |
1281 | ref_transaction_update(t, refname, new_oid, old_oid, flags, msg, | |
1282 | &err) || | |
1283 | ref_transaction_commit(t, &err)) { | |
1284 | ret = 1; | |
1285 | ref_transaction_free(t); | |
74ec19d4 DT |
1286 | } |
1287 | if (ret) { | |
661558f0 | 1288 | const char *str = _("update_ref failed for ref '%s': %s"); |
b4d75ac1 | 1289 | |
b4d75ac1 RS |
1290 | switch (onerr) { |
1291 | case UPDATE_REFS_MSG_ON_ERR: | |
1292 | error(str, refname, err.buf); | |
1293 | break; | |
1294 | case UPDATE_REFS_DIE_ON_ERR: | |
1295 | die(str, refname, err.buf); | |
1296 | break; | |
1297 | case UPDATE_REFS_QUIET_ON_ERR: | |
1298 | break; | |
1299 | } | |
1300 | strbuf_release(&err); | |
4738a333 | 1301 | return 1; |
b4d75ac1 RS |
1302 | } |
1303 | strbuf_release(&err); | |
74ec19d4 DT |
1304 | if (t) |
1305 | ref_transaction_free(t); | |
b4d75ac1 | 1306 | return 0; |
4738a333 BK |
1307 | } |
1308 | ||
c0fe4e8b | 1309 | int update_ref(const char *msg, const char *refname, |
ae077771 | 1310 | const struct object_id *new_oid, |
1311 | const struct object_id *old_oid, | |
c0fe4e8b NTND |
1312 | unsigned int flags, enum action_on_err onerr) |
1313 | { | |
23a3f0cb | 1314 | return refs_update_ref(get_main_ref_store(the_repository), msg, refname, new_oid, |
ae077771 | 1315 | old_oid, flags, onerr); |
c0fe4e8b NTND |
1316 | } |
1317 | ||
613bef56 JK |
1318 | /* |
1319 | * Check that the string refname matches a rule of the form | |
1320 | * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule | |
1321 | * "foo/%.*s/baz", and return the string "bar". | |
1322 | */ | |
1323 | static const char *match_parse_rule(const char *refname, const char *rule, | |
1324 | size_t *len) | |
7c2b3029 | 1325 | { |
613bef56 JK |
1326 | /* |
1327 | * Check that rule matches refname up to the first percent in the rule. | |
1328 | * We can bail immediately if not, but otherwise we leave "rule" at the | |
1329 | * %-placeholder, and "refname" at the start of the potential matched | |
1330 | * name. | |
1331 | */ | |
1332 | while (*rule != '%') { | |
1333 | if (!*rule) | |
1334 | BUG("rev-parse rule did not have percent"); | |
1335 | if (*refname++ != *rule++) | |
1336 | return NULL; | |
1337 | } | |
7c2b3029 | 1338 | |
613bef56 JK |
1339 | /* |
1340 | * Check that our "%" is the expected placeholder. This assumes there | |
1341 | * are no other percents (placeholder or quoted) in the string, but | |
1342 | * that is sufficient for our rev-parse rules. | |
1343 | */ | |
1344 | if (!skip_prefix(rule, "%.*s", &rule)) | |
1345 | return NULL; | |
7c2b3029 | 1346 | |
613bef56 JK |
1347 | /* |
1348 | * And now check that our suffix (if any) matches. | |
1349 | */ | |
1350 | if (!strip_suffix(refname, rule, len)) | |
1351 | return NULL; | |
7c2b3029 | 1352 | |
613bef56 JK |
1353 | return refname; /* len set by strip_suffix() */ |
1354 | } | |
7c2b3029 | 1355 | |
546edf37 NTND |
1356 | char *refs_shorten_unambiguous_ref(struct ref_store *refs, |
1357 | const char *refname, int strict) | |
7c2b3029 JK |
1358 | { |
1359 | int i; | |
6cd4a898 | 1360 | struct strbuf resolved_buf = STRBUF_INIT; |
7c2b3029 | 1361 | |
7c2b3029 | 1362 | /* skip first rule, it will always match */ |
8f416f65 | 1363 | for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) { |
7c2b3029 | 1364 | int j; |
6e7b3309 | 1365 | int rules_to_fail = i; |
613bef56 | 1366 | const char *short_name; |
dd5e4d39 | 1367 | size_t short_name_len; |
7c2b3029 | 1368 | |
613bef56 JK |
1369 | short_name = match_parse_rule(refname, ref_rev_parse_rules[i], |
1370 | &short_name_len); | |
1371 | if (!short_name) | |
7c2b3029 JK |
1372 | continue; |
1373 | ||
6e7b3309 BW |
1374 | /* |
1375 | * in strict mode, all (except the matched one) rules | |
1376 | * must fail to resolve to a valid non-ambiguous ref | |
1377 | */ | |
1378 | if (strict) | |
8f416f65 | 1379 | rules_to_fail = NUM_REV_PARSE_RULES; |
6e7b3309 | 1380 | |
7c2b3029 JK |
1381 | /* |
1382 | * check if the short name resolves to a valid ref, | |
1383 | * but use only rules prior to the matched one | |
1384 | */ | |
6e7b3309 | 1385 | for (j = 0; j < rules_to_fail; j++) { |
7c2b3029 | 1386 | const char *rule = ref_rev_parse_rules[j]; |
7c2b3029 | 1387 | |
6e7b3309 BW |
1388 | /* skip matched rule */ |
1389 | if (i == j) | |
1390 | continue; | |
1391 | ||
7c2b3029 JK |
1392 | /* |
1393 | * the short name is ambiguous, if it resolves | |
1394 | * (with this previous rule) to a valid ref | |
1395 | * read_ref() returns 0 on success | |
1396 | */ | |
6cd4a898 JK |
1397 | strbuf_reset(&resolved_buf); |
1398 | strbuf_addf(&resolved_buf, rule, | |
dd5e4d39 JK |
1399 | cast_size_t_to_int(short_name_len), |
1400 | short_name); | |
546edf37 | 1401 | if (refs_ref_exists(refs, resolved_buf.buf)) |
7c2b3029 JK |
1402 | break; |
1403 | } | |
1404 | ||
1405 | /* | |
1406 | * short name is non-ambiguous if all previous rules | |
1407 | * haven't resolved to a valid ref | |
1408 | */ | |
6cd4a898 JK |
1409 | if (j == rules_to_fail) { |
1410 | strbuf_release(&resolved_buf); | |
613bef56 | 1411 | return xmemdupz(short_name, short_name_len); |
6cd4a898 | 1412 | } |
7c2b3029 JK |
1413 | } |
1414 | ||
6cd4a898 | 1415 | strbuf_release(&resolved_buf); |
dfefa935 | 1416 | return xstrdup(refname); |
7c2b3029 | 1417 | } |
daebaa78 | 1418 | |
546edf37 NTND |
1419 | char *shorten_unambiguous_ref(const char *refname, int strict) |
1420 | { | |
1421 | return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), | |
1422 | refname, strict); | |
1423 | } | |
1424 | ||
9b67eb6f PS |
1425 | int parse_hide_refs_config(const char *var, const char *value, const char *section, |
1426 | struct string_list *hide_refs) | |
daebaa78 | 1427 | { |
ad8c7cda | 1428 | const char *key; |
daebaa78 | 1429 | if (!strcmp("transfer.hiderefs", var) || |
ad8c7cda JK |
1430 | (!parse_config_key(var, section, NULL, NULL, &key) && |
1431 | !strcmp(key, "hiderefs"))) { | |
daebaa78 JH |
1432 | char *ref; |
1433 | int len; | |
1434 | ||
1435 | if (!value) | |
1436 | return config_error_nonbool(var); | |
1437 | ref = xstrdup(value); | |
1438 | len = strlen(ref); | |
1439 | while (len && ref[len - 1] == '/') | |
1440 | ref[--len] = '\0'; | |
5eeb9aa2 | 1441 | string_list_append_nodup(hide_refs, ref); |
daebaa78 JH |
1442 | } |
1443 | return 0; | |
1444 | } | |
1445 | ||
9b67eb6f PS |
1446 | int ref_is_hidden(const char *refname, const char *refname_full, |
1447 | const struct string_list *hide_refs) | |
daebaa78 | 1448 | { |
2bc31d16 | 1449 | int i; |
daebaa78 | 1450 | |
2bc31d16 JK |
1451 | for (i = hide_refs->nr - 1; i >= 0; i--) { |
1452 | const char *match = hide_refs->items[i].string; | |
78a766ab | 1453 | const char *subject; |
2bc31d16 | 1454 | int neg = 0; |
7a40a95e | 1455 | const char *p; |
2bc31d16 JK |
1456 | |
1457 | if (*match == '!') { | |
1458 | neg = 1; | |
1459 | match++; | |
1460 | } | |
1461 | ||
78a766ab LF |
1462 | if (*match == '^') { |
1463 | subject = refname_full; | |
1464 | match++; | |
1465 | } else { | |
1466 | subject = refname; | |
1467 | } | |
1468 | ||
1469 | /* refname can be NULL when namespaces are used. */ | |
7a40a95e CC |
1470 | if (subject && |
1471 | skip_prefix(subject, match, &p) && | |
1472 | (!*p || *p == '/')) | |
2bc31d16 | 1473 | return !neg; |
daebaa78 JH |
1474 | } |
1475 | return 0; | |
1476 | } | |
fa5b1830 | 1477 | |
0845122c DT |
1478 | const char *find_descendant_ref(const char *dirname, |
1479 | const struct string_list *extras, | |
1480 | const struct string_list *skip) | |
fa5b1830 | 1481 | { |
0845122c | 1482 | int pos; |
fa5b1830 | 1483 | |
0845122c DT |
1484 | if (!extras) |
1485 | return NULL; | |
fa5b1830 MH |
1486 | |
1487 | /* | |
0845122c DT |
1488 | * Look at the place where dirname would be inserted into |
1489 | * extras. If there is an entry at that position that starts | |
1490 | * with dirname (remember, dirname includes the trailing | |
1491 | * slash) and is not in skip, then we have a conflict. | |
fa5b1830 | 1492 | */ |
0845122c DT |
1493 | for (pos = string_list_find_insert_index(extras, dirname, 0); |
1494 | pos < extras->nr; pos++) { | |
1495 | const char *extra_refname = extras->items[pos].string; | |
fa5b1830 | 1496 | |
0845122c DT |
1497 | if (!starts_with(extra_refname, dirname)) |
1498 | break; | |
1499 | ||
1500 | if (!skip || !string_list_has_string(skip, extra_refname)) | |
1501 | return extra_refname; | |
fa5b1830 | 1502 | } |
0845122c DT |
1503 | return NULL; |
1504 | } | |
fa5b1830 | 1505 | |
62f0b399 | 1506 | int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
2bf68ed5 DT |
1507 | { |
1508 | struct object_id oid; | |
1509 | int flag; | |
1510 | ||
f1da24ca | 1511 | if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING, |
ce14de03 | 1512 | &oid, &flag)) |
2bf68ed5 DT |
1513 | return fn("HEAD", &oid, flag, cb_data); |
1514 | ||
1515 | return 0; | |
1516 | } | |
1517 | ||
1518 | int head_ref(each_ref_fn fn, void *cb_data) | |
1519 | { | |
23a3f0cb | 1520 | return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data); |
2bf68ed5 | 1521 | } |
93770590 | 1522 | |
e121b9cb MH |
1523 | struct ref_iterator *refs_ref_iterator_begin( |
1524 | struct ref_store *refs, | |
9aab952e JK |
1525 | const char *prefix, int trim, |
1526 | enum do_for_each_ref_flags flags) | |
e121b9cb MH |
1527 | { |
1528 | struct ref_iterator *iter; | |
1529 | ||
6d751be4 | 1530 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) { |
5d1f5b8c JK |
1531 | static int ref_paranoia = -1; |
1532 | ||
6d751be4 | 1533 | if (ref_paranoia < 0) |
968f12fd | 1534 | ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 1); |
6d751be4 JK |
1535 | if (ref_paranoia) { |
1536 | flags |= DO_FOR_EACH_INCLUDE_BROKEN; | |
1537 | flags |= DO_FOR_EACH_OMIT_DANGLING_SYMREFS; | |
1538 | } | |
1539 | } | |
0a0865b8 | 1540 | |
e121b9cb | 1541 | iter = refs->be->iterator_begin(refs, prefix, flags); |
c7599718 MH |
1542 | |
1543 | /* | |
1544 | * `iterator_begin()` already takes care of prefix, but we | |
1545 | * might need to do some trimming: | |
1546 | */ | |
1547 | if (trim) | |
1548 | iter = prefix_ref_iterator_begin(iter, "", trim); | |
e121b9cb | 1549 | |
8738a8a4 MH |
1550 | /* Sanity check for subclasses: */ |
1551 | if (!iter->ordered) | |
1552 | BUG("reference iterator is not ordered"); | |
1553 | ||
e121b9cb MH |
1554 | return iter; |
1555 | } | |
1556 | ||
4c4de895 MH |
1557 | /* |
1558 | * Call fn for each reference in the specified submodule for which the | |
1559 | * refname begins with prefix. If trim is non-zero, then trim that | |
1560 | * many characters off the beginning of each refname before passing | |
1561 | * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to | |
1562 | * include broken references in the iteration. If fn ever returns a | |
1563 | * non-zero value, stop the iteration and return that value; | |
1564 | * otherwise, return 0. | |
1565 | */ | |
4a6067cd SB |
1566 | static int do_for_each_repo_ref(struct repository *r, const char *prefix, |
1567 | each_repo_ref_fn fn, int trim, int flags, | |
1568 | void *cb_data) | |
1569 | { | |
1570 | struct ref_iterator *iter; | |
1571 | struct ref_store *refs = get_main_ref_store(r); | |
1572 | ||
1573 | if (!refs) | |
1574 | return 0; | |
1575 | ||
1576 | iter = refs_ref_iterator_begin(refs, prefix, trim, flags); | |
1577 | ||
1578 | return do_for_each_repo_ref_iterator(r, iter, fn, cb_data); | |
1579 | } | |
1580 | ||
1581 | struct do_for_each_ref_help { | |
1582 | each_ref_fn *fn; | |
1583 | void *cb_data; | |
1584 | }; | |
1585 | ||
1586 | static int do_for_each_ref_helper(struct repository *r, | |
1587 | const char *refname, | |
1588 | const struct object_id *oid, | |
1589 | int flags, | |
1590 | void *cb_data) | |
1591 | { | |
1592 | struct do_for_each_ref_help *hp = cb_data; | |
1593 | ||
1594 | return hp->fn(refname, oid, flags, hp->cb_data); | |
1595 | } | |
1596 | ||
7d2df051 | 1597 | static int do_for_each_ref(struct ref_store *refs, const char *prefix, |
9aab952e JK |
1598 | each_ref_fn fn, int trim, |
1599 | enum do_for_each_ref_flags flags, void *cb_data) | |
4c4de895 MH |
1600 | { |
1601 | struct ref_iterator *iter; | |
4a6067cd | 1602 | struct do_for_each_ref_help hp = { fn, cb_data }; |
4c4de895 | 1603 | |
00eebe35 MH |
1604 | if (!refs) |
1605 | return 0; | |
1606 | ||
e121b9cb | 1607 | iter = refs_ref_iterator_begin(refs, prefix, trim, flags); |
4c4de895 | 1608 | |
4a6067cd SB |
1609 | return do_for_each_repo_ref_iterator(the_repository, iter, |
1610 | do_for_each_ref_helper, &hp); | |
4c4de895 MH |
1611 | } |
1612 | ||
7d2df051 NTND |
1613 | int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
1614 | { | |
1615 | return do_for_each_ref(refs, "", fn, 0, 0, cb_data); | |
1616 | } | |
1617 | ||
93770590 DT |
1618 | int for_each_ref(each_ref_fn fn, void *cb_data) |
1619 | { | |
23a3f0cb | 1620 | return refs_for_each_ref(get_main_ref_store(the_repository), fn, cb_data); |
93770590 DT |
1621 | } |
1622 | ||
7d2df051 NTND |
1623 | int refs_for_each_ref_in(struct ref_store *refs, const char *prefix, |
1624 | each_ref_fn fn, void *cb_data) | |
1625 | { | |
1626 | return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data); | |
93770590 DT |
1627 | } |
1628 | ||
1629 | int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) | |
1630 | { | |
23a3f0cb | 1631 | return refs_for_each_ref_in(get_main_ref_store(the_repository), prefix, fn, cb_data); |
93770590 DT |
1632 | } |
1633 | ||
67985e4e | 1634 | int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data) |
93770590 | 1635 | { |
23a3f0cb | 1636 | return do_for_each_ref(get_main_ref_store(the_repository), |
67985e4e | 1637 | prefix, fn, 0, 0, cb_data); |
93770590 DT |
1638 | } |
1639 | ||
073cf63c | 1640 | int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, |
67985e4e | 1641 | each_ref_fn fn, void *cb_data) |
03df567f | 1642 | { |
67985e4e | 1643 | return do_for_each_ref(refs, prefix, fn, 0, 0, cb_data); |
03df567f MH |
1644 | } |
1645 | ||
212e0f7e | 1646 | int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data) |
93770590 | 1647 | { |
97e61e0f | 1648 | const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; |
212e0f7e SB |
1649 | return do_for_each_repo_ref(r, git_replace_ref_base, fn, |
1650 | strlen(git_replace_ref_base), | |
1651 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); | |
93770590 DT |
1652 | } |
1653 | ||
1654 | int for_each_namespaced_ref(each_ref_fn fn, void *cb_data) | |
1655 | { | |
1656 | struct strbuf buf = STRBUF_INIT; | |
1657 | int ret; | |
1658 | strbuf_addf(&buf, "%srefs/", get_git_namespace()); | |
23a3f0cb | 1659 | ret = do_for_each_ref(get_main_ref_store(the_repository), |
7d2df051 | 1660 | buf.buf, fn, 0, 0, cb_data); |
93770590 DT |
1661 | strbuf_release(&buf); |
1662 | return ret; | |
1663 | } | |
1664 | ||
7d2df051 | 1665 | int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
93770590 | 1666 | { |
7d2df051 | 1667 | return do_for_each_ref(refs, "", fn, 0, |
93770590 DT |
1668 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); |
1669 | } | |
2d0663b2 | 1670 | |
7d2df051 NTND |
1671 | int for_each_rawref(each_ref_fn fn, void *cb_data) |
1672 | { | |
23a3f0cb | 1673 | return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data); |
7d2df051 NTND |
1674 | } |
1675 | ||
16b1985b TB |
1676 | static int qsort_strcmp(const void *va, const void *vb) |
1677 | { | |
1678 | const char *a = *(const char **)va; | |
1679 | const char *b = *(const char **)vb; | |
1680 | ||
1681 | return strcmp(a, b); | |
1682 | } | |
1683 | ||
1684 | static void find_longest_prefixes_1(struct string_list *out, | |
1685 | struct strbuf *prefix, | |
1686 | const char **patterns, size_t nr) | |
1687 | { | |
1688 | size_t i; | |
1689 | ||
1690 | for (i = 0; i < nr; i++) { | |
1691 | char c = patterns[i][prefix->len]; | |
1692 | if (!c || is_glob_special(c)) { | |
1693 | string_list_append(out, prefix->buf); | |
1694 | return; | |
1695 | } | |
1696 | } | |
1697 | ||
1698 | i = 0; | |
1699 | while (i < nr) { | |
1700 | size_t end; | |
1701 | ||
1702 | /* | |
1703 | * Set "end" to the index of the element _after_ the last one | |
1704 | * in our group. | |
1705 | */ | |
1706 | for (end = i + 1; end < nr; end++) { | |
1707 | if (patterns[i][prefix->len] != patterns[end][prefix->len]) | |
1708 | break; | |
1709 | } | |
1710 | ||
1711 | strbuf_addch(prefix, patterns[i][prefix->len]); | |
1712 | find_longest_prefixes_1(out, prefix, patterns + i, end - i); | |
1713 | strbuf_setlen(prefix, prefix->len - 1); | |
1714 | ||
1715 | i = end; | |
1716 | } | |
1717 | } | |
1718 | ||
1719 | static void find_longest_prefixes(struct string_list *out, | |
1720 | const char **patterns) | |
1721 | { | |
1722 | struct strvec sorted = STRVEC_INIT; | |
1723 | struct strbuf prefix = STRBUF_INIT; | |
1724 | ||
1725 | strvec_pushv(&sorted, patterns); | |
1726 | QSORT(sorted.v, sorted.nr, qsort_strcmp); | |
1727 | ||
1728 | find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr); | |
1729 | ||
1730 | strvec_clear(&sorted); | |
1731 | strbuf_release(&prefix); | |
1732 | } | |
1733 | ||
91e2ab15 JK |
1734 | int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store, |
1735 | const char *namespace, | |
1736 | const char **patterns, | |
1737 | each_ref_fn fn, void *cb_data) | |
16b1985b TB |
1738 | { |
1739 | struct string_list prefixes = STRING_LIST_INIT_DUP; | |
1740 | struct string_list_item *prefix; | |
1741 | struct strbuf buf = STRBUF_INIT; | |
1742 | int ret = 0, namespace_len; | |
1743 | ||
1744 | find_longest_prefixes(&prefixes, patterns); | |
1745 | ||
1746 | if (namespace) | |
1747 | strbuf_addstr(&buf, namespace); | |
1748 | namespace_len = buf.len; | |
1749 | ||
1750 | for_each_string_list_item(prefix, &prefixes) { | |
1751 | strbuf_addstr(&buf, prefix->string); | |
91e2ab15 | 1752 | ret = refs_for_each_fullref_in(ref_store, buf.buf, fn, cb_data); |
16b1985b TB |
1753 | if (ret) |
1754 | break; | |
1755 | strbuf_setlen(&buf, namespace_len); | |
1756 | } | |
1757 | ||
1758 | string_list_clear(&prefixes, 0); | |
1759 | strbuf_release(&buf); | |
1760 | return ret; | |
1761 | } | |
1762 | ||
e8115302 HWN |
1763 | static int refs_read_special_head(struct ref_store *ref_store, |
1764 | const char *refname, struct object_id *oid, | |
df3458e9 HWN |
1765 | struct strbuf *referent, unsigned int *type, |
1766 | int *failure_errno) | |
e8115302 HWN |
1767 | { |
1768 | struct strbuf full_path = STRBUF_INIT; | |
1769 | struct strbuf content = STRBUF_INIT; | |
1770 | int result = -1; | |
1771 | strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname); | |
1772 | ||
1773 | if (strbuf_read_file(&content, full_path.buf, 0) < 0) | |
1774 | goto done; | |
1775 | ||
df3458e9 HWN |
1776 | result = parse_loose_ref_contents(content.buf, oid, referent, type, |
1777 | failure_errno); | |
e8115302 HWN |
1778 | |
1779 | done: | |
1780 | strbuf_release(&full_path); | |
1781 | strbuf_release(&content); | |
1782 | return result; | |
1783 | } | |
1784 | ||
8b72fea7 HWN |
1785 | int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, |
1786 | struct object_id *oid, struct strbuf *referent, | |
1787 | unsigned int *type, int *failure_errno) | |
470be518 | 1788 | { |
8b72fea7 | 1789 | assert(failure_errno); |
e8115302 HWN |
1790 | if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) { |
1791 | return refs_read_special_head(ref_store, refname, oid, referent, | |
df3458e9 | 1792 | type, failure_errno); |
e8115302 HWN |
1793 | } |
1794 | ||
1795 | return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, | |
8b72fea7 | 1796 | type, failure_errno); |
470be518 MH |
1797 | } |
1798 | ||
cd475b3b PS |
1799 | int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname, |
1800 | struct strbuf *referent) | |
1801 | { | |
5b875404 | 1802 | return ref_store->be->read_symbolic_ref(ref_store, refname, referent); |
cd475b3b PS |
1803 | } |
1804 | ||
7d2df051 | 1805 | const char *refs_resolve_ref_unsafe(struct ref_store *refs, |
3c0cb0cb MH |
1806 | const char *refname, |
1807 | int resolve_flags, | |
ef18119d | 1808 | struct object_id *oid, |
ce14de03 | 1809 | int *flags) |
2d0663b2 DT |
1810 | { |
1811 | static struct strbuf sb_refname = STRBUF_INIT; | |
54fad661 | 1812 | struct object_id unused_oid; |
2d0663b2 DT |
1813 | int unused_flags; |
1814 | int symref_count; | |
1815 | ||
49e61479 | 1816 | if (!oid) |
1817 | oid = &unused_oid; | |
2d0663b2 DT |
1818 | if (!flags) |
1819 | flags = &unused_flags; | |
1820 | ||
1821 | *flags = 0; | |
1822 | ||
1823 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
1824 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || | |
ce14de03 | 1825 | !refname_is_safe(refname)) |
2d0663b2 | 1826 | return NULL; |
2d0663b2 DT |
1827 | |
1828 | /* | |
1829 | * dwim_ref() uses REF_ISBROKEN to distinguish between | |
1830 | * missing refs and refs that were present but invalid, | |
1831 | * to complain about the latter to stderr. | |
1832 | * | |
1833 | * We don't know whether the ref exists, so don't set | |
1834 | * REF_ISBROKEN yet. | |
1835 | */ | |
1836 | *flags |= REF_BAD_NAME; | |
1837 | } | |
1838 | ||
1839 | for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) { | |
1840 | unsigned int read_flags = 0; | |
ce14de03 | 1841 | int failure_errno; |
2d0663b2 | 1842 | |
8b72fea7 | 1843 | if (refs_read_raw_ref(refs, refname, oid, &sb_refname, |
ce14de03 | 1844 | &read_flags, &failure_errno)) { |
2d0663b2 | 1845 | *flags |= read_flags; |
a1c1d817 JK |
1846 | |
1847 | /* In reading mode, refs must eventually resolve */ | |
1848 | if (resolve_flags & RESOLVE_REF_READING) | |
1849 | return NULL; | |
1850 | ||
1851 | /* | |
1852 | * Otherwise a missing ref is OK. But the files backend | |
1853 | * may show errors besides ENOENT if there are | |
1854 | * similarly-named refs. | |
1855 | */ | |
ce14de03 ÆAB |
1856 | if (failure_errno != ENOENT && |
1857 | failure_errno != EISDIR && | |
1858 | failure_errno != ENOTDIR) | |
2d0663b2 | 1859 | return NULL; |
a1c1d817 | 1860 | |
49e61479 | 1861 | oidclr(oid); |
2d0663b2 DT |
1862 | if (*flags & REF_BAD_NAME) |
1863 | *flags |= REF_ISBROKEN; | |
1864 | return refname; | |
1865 | } | |
1866 | ||
1867 | *flags |= read_flags; | |
1868 | ||
1869 | if (!(read_flags & REF_ISSYMREF)) { | |
1870 | if (*flags & REF_BAD_NAME) { | |
49e61479 | 1871 | oidclr(oid); |
2d0663b2 DT |
1872 | *flags |= REF_ISBROKEN; |
1873 | } | |
1874 | return refname; | |
1875 | } | |
1876 | ||
1877 | refname = sb_refname.buf; | |
1878 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { | |
49e61479 | 1879 | oidclr(oid); |
2d0663b2 DT |
1880 | return refname; |
1881 | } | |
1882 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
1883 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || | |
ce14de03 | 1884 | !refname_is_safe(refname)) |
2d0663b2 | 1885 | return NULL; |
2d0663b2 DT |
1886 | |
1887 | *flags |= REF_ISBROKEN | REF_BAD_NAME; | |
1888 | } | |
1889 | } | |
1890 | ||
2d0663b2 DT |
1891 | return NULL; |
1892 | } | |
00eebe35 | 1893 | |
6fb5acfd DT |
1894 | /* backend functions */ |
1895 | int refs_init_db(struct strbuf *err) | |
1896 | { | |
23a3f0cb | 1897 | struct ref_store *refs = get_main_ref_store(the_repository); |
6fb5acfd DT |
1898 | |
1899 | return refs->be->init_db(refs, err); | |
1900 | } | |
1901 | ||
bd40dcda | 1902 | const char *resolve_ref_unsafe(const char *refname, int resolve_flags, |
49e61479 | 1903 | struct object_id *oid, int *flags) |
bd40dcda | 1904 | { |
23a3f0cb | 1905 | return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname, |
ce14de03 | 1906 | resolve_flags, oid, flags); |
bd40dcda MH |
1907 | } |
1908 | ||
a8355bb7 | 1909 | int resolve_gitlink_ref(const char *submodule, const char *refname, |
a98e6101 | 1910 | struct object_id *oid) |
424dcc76 | 1911 | { |
424dcc76 MH |
1912 | struct ref_store *refs; |
1913 | int flags; | |
1914 | ||
29babbee | 1915 | refs = get_submodule_ref_store(submodule); |
48a8475f | 1916 | |
424dcc76 MH |
1917 | if (!refs) |
1918 | return -1; | |
1919 | ||
ce14de03 ÆAB |
1920 | if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) || |
1921 | is_null_oid(oid)) | |
424dcc76 MH |
1922 | return -1; |
1923 | return 0; | |
1924 | } | |
1925 | ||
0c064d90 | 1926 | struct ref_store_hash_entry |
7d4558c4 | 1927 | { |
e2b5038d | 1928 | struct hashmap_entry ent; |
7d4558c4 MH |
1929 | |
1930 | struct ref_store *refs; | |
1931 | ||
0c064d90 NTND |
1932 | /* NUL-terminated identifier of the ref store: */ |
1933 | char name[FLEX_ARRAY]; | |
7d4558c4 MH |
1934 | }; |
1935 | ||
5cf88fd8 | 1936 | static int ref_store_hash_cmp(const void *cmp_data UNUSED, |
939af16e EW |
1937 | const struct hashmap_entry *eptr, |
1938 | const struct hashmap_entry *entry_or_key, | |
7d4558c4 MH |
1939 | const void *keydata) |
1940 | { | |
939af16e EW |
1941 | const struct ref_store_hash_entry *e1, *e2; |
1942 | const char *name; | |
1943 | ||
1944 | e1 = container_of(eptr, const struct ref_store_hash_entry, ent); | |
1945 | e2 = container_of(entry_or_key, const struct ref_store_hash_entry, ent); | |
1946 | name = keydata ? keydata : e2->name; | |
7d4558c4 | 1947 | |
0c064d90 | 1948 | return strcmp(e1->name, name); |
7d4558c4 MH |
1949 | } |
1950 | ||
0c064d90 NTND |
1951 | static struct ref_store_hash_entry *alloc_ref_store_hash_entry( |
1952 | const char *name, struct ref_store *refs) | |
7d4558c4 | 1953 | { |
0c064d90 | 1954 | struct ref_store_hash_entry *entry; |
7d4558c4 | 1955 | |
0c064d90 | 1956 | FLEX_ALLOC_STR(entry, name, name); |
d22245a2 | 1957 | hashmap_entry_init(&entry->ent, strhash(name)); |
7d4558c4 MH |
1958 | entry->refs = refs; |
1959 | return entry; | |
1960 | } | |
1961 | ||
7d4558c4 MH |
1962 | /* A hashmap of ref_stores, stored by submodule name: */ |
1963 | static struct hashmap submodule_ref_stores; | |
00eebe35 | 1964 | |
17eff96b NTND |
1965 | /* A hashmap of ref_stores, stored by worktree id: */ |
1966 | static struct hashmap worktree_ref_stores; | |
1967 | ||
c468da4e | 1968 | /* |
0c064d90 NTND |
1969 | * Look up a ref store by name. If that ref_store hasn't been |
1970 | * registered yet, return NULL. | |
c468da4e | 1971 | */ |
0c064d90 NTND |
1972 | static struct ref_store *lookup_ref_store_map(struct hashmap *map, |
1973 | const char *name) | |
00eebe35 | 1974 | { |
0c064d90 | 1975 | struct ref_store_hash_entry *entry; |
f23a4651 | 1976 | unsigned int hash; |
00eebe35 | 1977 | |
0c064d90 | 1978 | if (!map->tablesize) |
7d4558c4 MH |
1979 | /* It's initialized on demand in register_ref_store(). */ |
1980 | return NULL; | |
620a66b9 | 1981 | |
f23a4651 EW |
1982 | hash = strhash(name); |
1983 | entry = hashmap_get_entry_from_hash(map, hash, name, | |
1984 | struct ref_store_hash_entry, ent); | |
7d4558c4 | 1985 | return entry ? entry->refs : NULL; |
00eebe35 MH |
1986 | } |
1987 | ||
c468da4e MH |
1988 | /* |
1989 | * Create, record, and return a ref_store instance for the specified | |
5d0bc90e | 1990 | * gitdir. |
c468da4e | 1991 | */ |
34224e14 JT |
1992 | static struct ref_store *ref_store_init(struct repository *repo, |
1993 | const char *gitdir, | |
9e7ec634 | 1994 | unsigned int flags) |
00eebe35 MH |
1995 | { |
1996 | const char *be_name = "files"; | |
1997 | struct ref_storage_be *be = find_ref_storage_backend(be_name); | |
ba88add5 | 1998 | struct ref_store *refs; |
00eebe35 MH |
1999 | |
2000 | if (!be) | |
033abf97 | 2001 | BUG("reference backend %s is unknown", be_name); |
00eebe35 | 2002 | |
34224e14 | 2003 | refs = be->init(repo, gitdir, flags); |
ba88add5 | 2004 | return refs; |
00eebe35 MH |
2005 | } |
2006 | ||
64a74161 | 2007 | struct ref_store *get_main_ref_store(struct repository *r) |
24c8407e | 2008 | { |
02204610 JK |
2009 | if (r->refs_private) |
2010 | return r->refs_private; | |
24c8407e | 2011 | |
2dc417ab JK |
2012 | if (!r->gitdir) |
2013 | BUG("attempting to get main_ref_store outside of repository"); | |
2014 | ||
34224e14 | 2015 | r->refs_private = ref_store_init(r, r->gitdir, REF_STORE_ALL_CAPS); |
4441f427 | 2016 | r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private); |
02204610 | 2017 | return r->refs_private; |
378dc910 NTND |
2018 | } |
2019 | ||
2020 | /* | |
0c064d90 NTND |
2021 | * Associate a ref store with a name. It is a fatal error to call this |
2022 | * function twice for the same name. | |
378dc910 | 2023 | */ |
0c064d90 NTND |
2024 | static void register_ref_store_map(struct hashmap *map, |
2025 | const char *type, | |
2026 | struct ref_store *refs, | |
2027 | const char *name) | |
378dc910 | 2028 | { |
26b455f2 EW |
2029 | struct ref_store_hash_entry *entry; |
2030 | ||
0c064d90 | 2031 | if (!map->tablesize) |
7663cdc8 | 2032 | hashmap_init(map, ref_store_hash_cmp, NULL, 0); |
378dc910 | 2033 | |
26b455f2 EW |
2034 | entry = alloc_ref_store_hash_entry(name, refs); |
2035 | if (hashmap_put(map, &entry->ent)) | |
033abf97 | 2036 | BUG("%s ref_store '%s' initialized twice", type, name); |
24c8407e NTND |
2037 | } |
2038 | ||
18d0002d | 2039 | struct ref_store *get_submodule_ref_store(const char *submodule) |
00eebe35 | 2040 | { |
126c9e05 | 2041 | struct strbuf submodule_sb = STRBUF_INIT; |
00eebe35 | 2042 | struct ref_store *refs; |
29babbee NTND |
2043 | char *to_free = NULL; |
2044 | size_t len; | |
34224e14 | 2045 | struct repository *subrepo; |
00eebe35 | 2046 | |
82a150f2 NTND |
2047 | if (!submodule) |
2048 | return NULL; | |
2049 | ||
873ea90d NTND |
2050 | len = strlen(submodule); |
2051 | while (len && is_dir_sep(submodule[len - 1])) | |
2052 | len--; | |
2053 | if (!len) | |
2054 | return NULL; | |
00eebe35 | 2055 | |
29babbee NTND |
2056 | if (submodule[len]) |
2057 | /* We need to strip off one or more trailing slashes */ | |
2058 | submodule = to_free = xmemdupz(submodule, len); | |
00eebe35 | 2059 | |
0c064d90 | 2060 | refs = lookup_ref_store_map(&submodule_ref_stores, submodule); |
126c9e05 | 2061 | if (refs) |
2c616c17 | 2062 | goto done; |
00eebe35 | 2063 | |
126c9e05 | 2064 | strbuf_addstr(&submodule_sb, submodule); |
2c616c17 NTND |
2065 | if (!is_nonbare_repository_dir(&submodule_sb)) |
2066 | goto done; | |
00eebe35 | 2067 | |
2c616c17 NTND |
2068 | if (submodule_to_gitdir(&submodule_sb, submodule)) |
2069 | goto done; | |
00eebe35 | 2070 | |
34224e14 JT |
2071 | subrepo = xmalloc(sizeof(*subrepo)); |
2072 | /* | |
2073 | * NEEDSWORK: Make get_submodule_ref_store() work with arbitrary | |
2074 | * superprojects other than the_repository. This probably should be | |
2075 | * done by making it take a struct repository * parameter instead of a | |
2076 | * submodule path. | |
2077 | */ | |
2078 | if (repo_submodule_init(subrepo, the_repository, submodule, | |
2079 | null_oid())) { | |
2080 | free(subrepo); | |
2081 | goto done; | |
2082 | } | |
2083 | refs = ref_store_init(subrepo, submodule_sb.buf, | |
9e7ec634 | 2084 | REF_STORE_READ | REF_STORE_ODB); |
0c064d90 NTND |
2085 | register_ref_store_map(&submodule_ref_stores, "submodule", |
2086 | refs, submodule); | |
5d0bc90e | 2087 | |
2c616c17 | 2088 | done: |
5d0bc90e | 2089 | strbuf_release(&submodule_sb); |
29babbee NTND |
2090 | free(to_free); |
2091 | ||
00eebe35 MH |
2092 | return refs; |
2093 | } | |
2094 | ||
17eff96b NTND |
2095 | struct ref_store *get_worktree_ref_store(const struct worktree *wt) |
2096 | { | |
2097 | struct ref_store *refs; | |
2098 | const char *id; | |
2099 | ||
2100 | if (wt->is_current) | |
23a3f0cb | 2101 | return get_main_ref_store(the_repository); |
17eff96b NTND |
2102 | |
2103 | id = wt->id ? wt->id : "/"; | |
2104 | refs = lookup_ref_store_map(&worktree_ref_stores, id); | |
2105 | if (refs) | |
2106 | return refs; | |
2107 | ||
2108 | if (wt->id) | |
34224e14 JT |
2109 | refs = ref_store_init(the_repository, |
2110 | git_common_path("worktrees/%s", wt->id), | |
17eff96b NTND |
2111 | REF_STORE_ALL_CAPS); |
2112 | else | |
34224e14 JT |
2113 | refs = ref_store_init(the_repository, |
2114 | get_git_common_dir(), | |
17eff96b NTND |
2115 | REF_STORE_ALL_CAPS); |
2116 | ||
2117 | if (refs) | |
2118 | register_ref_store_map(&worktree_ref_stores, "worktree", | |
2119 | refs, id); | |
2120 | return refs; | |
2121 | } | |
2122 | ||
f9f7fd3b HWN |
2123 | void base_ref_store_init(struct ref_store *refs, struct repository *repo, |
2124 | const char *path, const struct ref_storage_be *be) | |
00eebe35 | 2125 | { |
620a66b9 | 2126 | refs->be = be; |
f9f7fd3b HWN |
2127 | refs->repo = repo; |
2128 | refs->gitdir = xstrdup(path); | |
00eebe35 | 2129 | } |
127b42a1 RS |
2130 | |
2131 | /* backend functions */ | |
7d2df051 | 2132 | int refs_pack_refs(struct ref_store *refs, unsigned int flags) |
8231527e | 2133 | { |
8231527e MH |
2134 | return refs->be->pack_refs(refs, flags); |
2135 | } | |
2136 | ||
36a31792 | 2137 | int peel_iterated_oid(const struct object_id *base, struct object_id *peeled) |
7d2df051 | 2138 | { |
36a31792 JK |
2139 | if (current_ref_iter && |
2140 | (current_ref_iter->oid == base || | |
2141 | oideq(current_ref_iter->oid, base))) | |
2142 | return ref_iterator_peel(current_ref_iter, peeled); | |
ba1c052f | 2143 | |
617480d7 | 2144 | return peel_object(base, peeled) ? -1 : 0; |
7d2df051 | 2145 | } |
bd427cf2 | 2146 | |
7d2df051 NTND |
2147 | int refs_create_symref(struct ref_store *refs, |
2148 | const char *ref_target, | |
2149 | const char *refs_heads_master, | |
2150 | const char *logmsg) | |
2151 | { | |
523fa69c JH |
2152 | char *msg; |
2153 | int retval; | |
2154 | ||
2155 | msg = normalize_reflog_message(logmsg); | |
2156 | retval = refs->be->create_symref(refs, ref_target, refs_heads_master, | |
2157 | msg); | |
2158 | free(msg); | |
2159 | return retval; | |
bd427cf2 MH |
2160 | } |
2161 | ||
284689ba MH |
2162 | int create_symref(const char *ref_target, const char *refs_heads_master, |
2163 | const char *logmsg) | |
2164 | { | |
23a3f0cb | 2165 | return refs_create_symref(get_main_ref_store(the_repository), ref_target, |
7d2df051 | 2166 | refs_heads_master, logmsg); |
284689ba MH |
2167 | } |
2168 | ||
2ced105c MH |
2169 | int ref_update_reject_duplicates(struct string_list *refnames, |
2170 | struct strbuf *err) | |
2171 | { | |
a552e50e | 2172 | size_t i, n = refnames->nr; |
2ced105c MH |
2173 | |
2174 | assert(err); | |
2175 | ||
8556f8d6 MH |
2176 | for (i = 1; i < n; i++) { |
2177 | int cmp = strcmp(refnames->items[i - 1].string, | |
2178 | refnames->items[i].string); | |
2179 | ||
2180 | if (!cmp) { | |
2ced105c | 2181 | strbuf_addf(err, |
661558f0 | 2182 | _("multiple updates for ref '%s' not allowed"), |
2ced105c MH |
2183 | refnames->items[i].string); |
2184 | return 1; | |
8556f8d6 | 2185 | } else if (cmp > 0) { |
033abf97 | 2186 | BUG("ref_update_reject_duplicates() received unsorted list"); |
2ced105c | 2187 | } |
8556f8d6 | 2188 | } |
2ced105c MH |
2189 | return 0; |
2190 | } | |
2191 | ||
67541597 PS |
2192 | static int run_transaction_hook(struct ref_transaction *transaction, |
2193 | const char *state) | |
2194 | { | |
2195 | struct child_process proc = CHILD_PROCESS_INIT; | |
2196 | struct strbuf buf = STRBUF_INIT; | |
0a0fbbe3 | 2197 | const char *hook; |
67541597 PS |
2198 | int ret = 0, i; |
2199 | ||
0a0fbbe3 | 2200 | hook = find_hook("reference-transaction"); |
67541597 | 2201 | if (!hook) |
67541597 | 2202 | return ret; |
67541597 | 2203 | |
c972bf4c | 2204 | strvec_pushl(&proc.args, hook, state, NULL); |
67541597 PS |
2205 | proc.in = -1; |
2206 | proc.stdout_to_stderr = 1; | |
2207 | proc.trace2_hook_name = "reference-transaction"; | |
2208 | ||
2209 | ret = start_command(&proc); | |
2210 | if (ret) | |
2211 | return ret; | |
2212 | ||
2213 | sigchain_push(SIGPIPE, SIG_IGN); | |
2214 | ||
2215 | for (i = 0; i < transaction->nr; i++) { | |
2216 | struct ref_update *update = transaction->updates[i]; | |
2217 | ||
2218 | strbuf_reset(&buf); | |
2219 | strbuf_addf(&buf, "%s %s %s\n", | |
2220 | oid_to_hex(&update->old_oid), | |
2221 | oid_to_hex(&update->new_oid), | |
2222 | update->refname); | |
2223 | ||
2224 | if (write_in_full(proc.in, buf.buf, buf.len) < 0) { | |
4755d7df ÆAB |
2225 | if (errno != EPIPE) { |
2226 | /* Don't leak errno outside this API */ | |
2227 | errno = 0; | |
67541597 | 2228 | ret = -1; |
4755d7df | 2229 | } |
67541597 PS |
2230 | break; |
2231 | } | |
2232 | } | |
2233 | ||
2234 | close(proc.in); | |
2235 | sigchain_pop(SIGPIPE); | |
2236 | strbuf_release(&buf); | |
2237 | ||
2238 | ret |= finish_command(&proc); | |
2239 | return ret; | |
2240 | } | |
2241 | ||
30173b88 MH |
2242 | int ref_transaction_prepare(struct ref_transaction *transaction, |
2243 | struct strbuf *err) | |
127b42a1 | 2244 | { |
c0fe4e8b | 2245 | struct ref_store *refs = transaction->ref_store; |
67541597 | 2246 | int ret; |
127b42a1 | 2247 | |
8d4240d3 MH |
2248 | switch (transaction->state) { |
2249 | case REF_TRANSACTION_OPEN: | |
2250 | /* Good. */ | |
2251 | break; | |
30173b88 | 2252 | case REF_TRANSACTION_PREPARED: |
033abf97 | 2253 | BUG("prepare called twice on reference transaction"); |
30173b88 | 2254 | break; |
8d4240d3 | 2255 | case REF_TRANSACTION_CLOSED: |
033abf97 | 2256 | BUG("prepare called on a closed reference transaction"); |
8d4240d3 MH |
2257 | break; |
2258 | default: | |
033abf97 | 2259 | BUG("unexpected reference transaction state"); |
8d4240d3 MH |
2260 | break; |
2261 | } | |
2262 | ||
ecd81dfc | 2263 | if (refs->repo->objects->odb->disable_ref_updates) { |
d8f4481c JK |
2264 | strbuf_addstr(err, |
2265 | _("ref updates forbidden inside quarantine environment")); | |
2266 | return -1; | |
2267 | } | |
2268 | ||
67541597 PS |
2269 | ret = refs->be->transaction_prepare(refs, transaction, err); |
2270 | if (ret) | |
2271 | return ret; | |
2272 | ||
2273 | ret = run_transaction_hook(transaction, "prepared"); | |
2274 | if (ret) { | |
2275 | ref_transaction_abort(transaction, err); | |
2276 | die(_("ref updates aborted by hook")); | |
2277 | } | |
2278 | ||
2279 | return 0; | |
30173b88 MH |
2280 | } |
2281 | ||
2282 | int ref_transaction_abort(struct ref_transaction *transaction, | |
2283 | struct strbuf *err) | |
2284 | { | |
2285 | struct ref_store *refs = transaction->ref_store; | |
2286 | int ret = 0; | |
2287 | ||
2288 | switch (transaction->state) { | |
2289 | case REF_TRANSACTION_OPEN: | |
2290 | /* No need to abort explicitly. */ | |
2291 | break; | |
2292 | case REF_TRANSACTION_PREPARED: | |
2293 | ret = refs->be->transaction_abort(refs, transaction, err); | |
2294 | break; | |
2295 | case REF_TRANSACTION_CLOSED: | |
033abf97 | 2296 | BUG("abort called on a closed reference transaction"); |
30173b88 MH |
2297 | break; |
2298 | default: | |
033abf97 | 2299 | BUG("unexpected reference transaction state"); |
30173b88 MH |
2300 | break; |
2301 | } | |
2302 | ||
67541597 PS |
2303 | run_transaction_hook(transaction, "aborted"); |
2304 | ||
30173b88 MH |
2305 | ref_transaction_free(transaction); |
2306 | return ret; | |
2307 | } | |
2308 | ||
2309 | int ref_transaction_commit(struct ref_transaction *transaction, | |
2310 | struct strbuf *err) | |
2311 | { | |
2312 | struct ref_store *refs = transaction->ref_store; | |
2313 | int ret; | |
2314 | ||
2315 | switch (transaction->state) { | |
2316 | case REF_TRANSACTION_OPEN: | |
2317 | /* Need to prepare first. */ | |
2318 | ret = ref_transaction_prepare(transaction, err); | |
2319 | if (ret) | |
2320 | return ret; | |
2321 | break; | |
2322 | case REF_TRANSACTION_PREPARED: | |
2323 | /* Fall through to finish. */ | |
2324 | break; | |
2325 | case REF_TRANSACTION_CLOSED: | |
033abf97 | 2326 | BUG("commit called on a closed reference transaction"); |
30173b88 MH |
2327 | break; |
2328 | default: | |
033abf97 | 2329 | BUG("unexpected reference transaction state"); |
30173b88 MH |
2330 | break; |
2331 | } | |
2332 | ||
67541597 PS |
2333 | ret = refs->be->transaction_finish(refs, transaction, err); |
2334 | if (!ret) | |
2335 | run_transaction_hook(transaction, "committed"); | |
2336 | return ret; | |
127b42a1 | 2337 | } |
62665823 | 2338 | |
7d2df051 NTND |
2339 | int refs_verify_refname_available(struct ref_store *refs, |
2340 | const char *refname, | |
b05855b5 | 2341 | const struct string_list *extras, |
7d2df051 NTND |
2342 | const struct string_list *skip, |
2343 | struct strbuf *err) | |
62665823 | 2344 | { |
b05855b5 MH |
2345 | const char *slash; |
2346 | const char *extra_refname; | |
2347 | struct strbuf dirname = STRBUF_INIT; | |
2348 | struct strbuf referent = STRBUF_INIT; | |
2349 | struct object_id oid; | |
2350 | unsigned int type; | |
2351 | struct ref_iterator *iter; | |
2352 | int ok; | |
2353 | int ret = -1; | |
2354 | ||
2355 | /* | |
2356 | * For the sake of comments in this function, suppose that | |
2357 | * refname is "refs/foo/bar". | |
2358 | */ | |
2359 | ||
2360 | assert(err); | |
2361 | ||
2362 | strbuf_grow(&dirname, strlen(refname) + 1); | |
2363 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { | |
8b72fea7 HWN |
2364 | /* |
2365 | * Just saying "Is a directory" when we e.g. can't | |
2366 | * lock some multi-level ref isn't very informative, | |
2367 | * the user won't be told *what* is a directory, so | |
2368 | * let's not use strerror() below. | |
2369 | */ | |
2370 | int ignore_errno; | |
b05855b5 MH |
2371 | /* Expand dirname to the new prefix, not including the trailing slash: */ |
2372 | strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len); | |
2373 | ||
2374 | /* | |
2375 | * We are still at a leading dir of the refname (e.g., | |
2376 | * "refs/foo"; if there is a reference with that name, | |
2377 | * it is a conflict, *unless* it is in skip. | |
2378 | */ | |
2379 | if (skip && string_list_has_string(skip, dirname.buf)) | |
2380 | continue; | |
2381 | ||
8b72fea7 HWN |
2382 | if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, |
2383 | &type, &ignore_errno)) { | |
661558f0 | 2384 | strbuf_addf(err, _("'%s' exists; cannot create '%s'"), |
b05855b5 MH |
2385 | dirname.buf, refname); |
2386 | goto cleanup; | |
2387 | } | |
2388 | ||
2389 | if (extras && string_list_has_string(extras, dirname.buf)) { | |
661558f0 | 2390 | strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"), |
b05855b5 MH |
2391 | refname, dirname.buf); |
2392 | goto cleanup; | |
2393 | } | |
2394 | } | |
2395 | ||
2396 | /* | |
2397 | * We are at the leaf of our refname (e.g., "refs/foo/bar"). | |
2398 | * There is no point in searching for a reference with that | |
2399 | * name, because a refname isn't considered to conflict with | |
2400 | * itself. But we still need to check for references whose | |
2401 | * names are in the "refs/foo/bar/" namespace, because they | |
2402 | * *do* conflict. | |
2403 | */ | |
2404 | strbuf_addstr(&dirname, refname + dirname.len); | |
2405 | strbuf_addch(&dirname, '/'); | |
2406 | ||
2407 | iter = refs_ref_iterator_begin(refs, dirname.buf, 0, | |
2408 | DO_FOR_EACH_INCLUDE_BROKEN); | |
2409 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
2410 | if (skip && | |
2411 | string_list_has_string(skip, iter->refname)) | |
2412 | continue; | |
2413 | ||
661558f0 | 2414 | strbuf_addf(err, _("'%s' exists; cannot create '%s'"), |
b05855b5 MH |
2415 | iter->refname, refname); |
2416 | ref_iterator_abort(iter); | |
2417 | goto cleanup; | |
2418 | } | |
2419 | ||
2420 | if (ok != ITER_DONE) | |
033abf97 | 2421 | BUG("error while iterating over references"); |
b05855b5 MH |
2422 | |
2423 | extra_refname = find_descendant_ref(dirname.buf, extras, skip); | |
2424 | if (extra_refname) | |
661558f0 | 2425 | strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"), |
b05855b5 MH |
2426 | refname, extra_refname); |
2427 | else | |
2428 | ret = 0; | |
2429 | ||
2430 | cleanup: | |
2431 | strbuf_release(&referent); | |
2432 | strbuf_release(&dirname); | |
2433 | return ret; | |
62665823 | 2434 | } |
e3688bd6 | 2435 | |
7d2df051 | 2436 | int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
e3688bd6 | 2437 | { |
e3688bd6 | 2438 | struct ref_iterator *iter; |
4a6067cd | 2439 | struct do_for_each_ref_help hp = { fn, cb_data }; |
e3688bd6 DT |
2440 | |
2441 | iter = refs->be->reflog_iterator_begin(refs); | |
2442 | ||
4a6067cd SB |
2443 | return do_for_each_repo_ref_iterator(the_repository, iter, |
2444 | do_for_each_ref_helper, &hp); | |
e3688bd6 DT |
2445 | } |
2446 | ||
7d2df051 | 2447 | int for_each_reflog(each_ref_fn fn, void *cb_data) |
e3688bd6 | 2448 | { |
23a3f0cb | 2449 | return refs_for_each_reflog(get_main_ref_store(the_repository), fn, cb_data); |
7d2df051 | 2450 | } |
e3688bd6 | 2451 | |
7d2df051 NTND |
2452 | int refs_for_each_reflog_ent_reverse(struct ref_store *refs, |
2453 | const char *refname, | |
2454 | each_reflog_ent_fn fn, | |
2455 | void *cb_data) | |
2456 | { | |
e3688bd6 DT |
2457 | return refs->be->for_each_reflog_ent_reverse(refs, refname, |
2458 | fn, cb_data); | |
2459 | } | |
2460 | ||
7d2df051 NTND |
2461 | int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, |
2462 | void *cb_data) | |
2463 | { | |
23a3f0cb | 2464 | return refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), |
7d2df051 NTND |
2465 | refname, fn, cb_data); |
2466 | } | |
2467 | ||
2468 | int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, | |
2469 | each_reflog_ent_fn fn, void *cb_data) | |
2470 | { | |
2471 | return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data); | |
2472 | } | |
2473 | ||
e3688bd6 DT |
2474 | int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, |
2475 | void *cb_data) | |
2476 | { | |
23a3f0cb | 2477 | return refs_for_each_reflog_ent(get_main_ref_store(the_repository), refname, |
7d2df051 NTND |
2478 | fn, cb_data); |
2479 | } | |
e3688bd6 | 2480 | |
7d2df051 NTND |
2481 | int refs_reflog_exists(struct ref_store *refs, const char *refname) |
2482 | { | |
2483 | return refs->be->reflog_exists(refs, refname); | |
e3688bd6 DT |
2484 | } |
2485 | ||
2486 | int reflog_exists(const char *refname) | |
2487 | { | |
23a3f0cb | 2488 | return refs_reflog_exists(get_main_ref_store(the_repository), refname); |
7d2df051 | 2489 | } |
e3688bd6 | 2490 | |
7d2df051 | 2491 | int refs_create_reflog(struct ref_store *refs, const char *refname, |
7b089120 | 2492 | struct strbuf *err) |
7d2df051 | 2493 | { |
7b089120 | 2494 | return refs->be->create_reflog(refs, refname, err); |
e3688bd6 DT |
2495 | } |
2496 | ||
7b089120 | 2497 | int safe_create_reflog(const char *refname, struct strbuf *err) |
e3688bd6 | 2498 | { |
23a3f0cb | 2499 | return refs_create_reflog(get_main_ref_store(the_repository), refname, |
7b089120 | 2500 | err); |
7d2df051 | 2501 | } |
e3688bd6 | 2502 | |
7d2df051 NTND |
2503 | int refs_delete_reflog(struct ref_store *refs, const char *refname) |
2504 | { | |
2505 | return refs->be->delete_reflog(refs, refname); | |
e3688bd6 DT |
2506 | } |
2507 | ||
2508 | int delete_reflog(const char *refname) | |
2509 | { | |
23a3f0cb | 2510 | return refs_delete_reflog(get_main_ref_store(the_repository), refname); |
7d2df051 | 2511 | } |
e3688bd6 | 2512 | |
7d2df051 | 2513 | int refs_reflog_expire(struct ref_store *refs, |
cc40b5ce | 2514 | const char *refname, |
7d2df051 NTND |
2515 | unsigned int flags, |
2516 | reflog_expiry_prepare_fn prepare_fn, | |
2517 | reflog_expiry_should_prune_fn should_prune_fn, | |
2518 | reflog_expiry_cleanup_fn cleanup_fn, | |
2519 | void *policy_cb_data) | |
2520 | { | |
cc40b5ce | 2521 | return refs->be->reflog_expire(refs, refname, flags, |
7d2df051 NTND |
2522 | prepare_fn, should_prune_fn, |
2523 | cleanup_fn, policy_cb_data); | |
e3688bd6 DT |
2524 | } |
2525 | ||
cc40b5ce | 2526 | int reflog_expire(const char *refname, |
e3688bd6 DT |
2527 | unsigned int flags, |
2528 | reflog_expiry_prepare_fn prepare_fn, | |
2529 | reflog_expiry_should_prune_fn should_prune_fn, | |
2530 | reflog_expiry_cleanup_fn cleanup_fn, | |
2531 | void *policy_cb_data) | |
2532 | { | |
23a3f0cb | 2533 | return refs_reflog_expire(get_main_ref_store(the_repository), |
cc40b5ce | 2534 | refname, flags, |
7d2df051 NTND |
2535 | prepare_fn, should_prune_fn, |
2536 | cleanup_fn, policy_cb_data); | |
e3688bd6 | 2537 | } |
fc681463 DT |
2538 | |
2539 | int initial_ref_transaction_commit(struct ref_transaction *transaction, | |
2540 | struct strbuf *err) | |
2541 | { | |
c0fe4e8b | 2542 | struct ref_store *refs = transaction->ref_store; |
fc681463 DT |
2543 | |
2544 | return refs->be->initial_transaction_commit(refs, transaction, err); | |
2545 | } | |
a27dcf89 | 2546 | |
4f2ba2d0 PS |
2547 | void ref_transaction_for_each_queued_update(struct ref_transaction *transaction, |
2548 | ref_transaction_for_each_queued_update_fn cb, | |
2549 | void *cb_data) | |
2550 | { | |
2551 | int i; | |
2552 | ||
2553 | for (i = 0; i < transaction->nr; i++) { | |
2554 | struct ref_update *update = transaction->updates[i]; | |
2555 | ||
2556 | cb(update->refname, | |
2557 | (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL, | |
2558 | (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL, | |
2559 | cb_data); | |
2560 | } | |
2561 | } | |
2562 | ||
523fa69c | 2563 | int refs_delete_refs(struct ref_store *refs, const char *logmsg, |
64da4199 | 2564 | struct string_list *refnames, unsigned int flags) |
a27dcf89 | 2565 | { |
523fa69c JH |
2566 | char *msg; |
2567 | int retval; | |
2568 | ||
2569 | msg = normalize_reflog_message(logmsg); | |
2570 | retval = refs->be->delete_refs(refs, msg, refnames, flags); | |
2571 | free(msg); | |
2572 | return retval; | |
a27dcf89 | 2573 | } |
9b6b40d9 | 2574 | |
64da4199 MH |
2575 | int delete_refs(const char *msg, struct string_list *refnames, |
2576 | unsigned int flags) | |
9b6b40d9 | 2577 | { |
23a3f0cb | 2578 | return refs_delete_refs(get_main_ref_store(the_repository), msg, refnames, flags); |
7d2df051 | 2579 | } |
9b6b40d9 | 2580 | |
7d2df051 NTND |
2581 | int refs_rename_ref(struct ref_store *refs, const char *oldref, |
2582 | const char *newref, const char *logmsg) | |
2583 | { | |
523fa69c JH |
2584 | char *msg; |
2585 | int retval; | |
2586 | ||
2587 | msg = normalize_reflog_message(logmsg); | |
2588 | retval = refs->be->rename_ref(refs, oldref, newref, msg); | |
2589 | free(msg); | |
2590 | return retval; | |
9b6b40d9 | 2591 | } |
7d2df051 NTND |
2592 | |
2593 | int rename_ref(const char *oldref, const char *newref, const char *logmsg) | |
2594 | { | |
23a3f0cb | 2595 | return refs_rename_ref(get_main_ref_store(the_repository), oldref, newref, logmsg); |
7d2df051 | 2596 | } |
52d59cc6 SD |
2597 | |
2598 | int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, | |
2599 | const char *newref, const char *logmsg) | |
2600 | { | |
523fa69c JH |
2601 | char *msg; |
2602 | int retval; | |
2603 | ||
2604 | msg = normalize_reflog_message(logmsg); | |
2605 | retval = refs->be->copy_ref(refs, oldref, newref, msg); | |
2606 | free(msg); | |
2607 | return retval; | |
52d59cc6 SD |
2608 | } |
2609 | ||
2610 | int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg) | |
2611 | { | |
23a3f0cb | 2612 | return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg); |
52d59cc6 | 2613 | } |