Line | Count | Source |
1 | | /* |
2 | | * The backend-independent part of the reference module. |
3 | | */ |
4 | | |
5 | | #define USE_THE_REPOSITORY_VARIABLE |
6 | | |
7 | | #include "git-compat-util.h" |
8 | | #include "advice.h" |
9 | | #include "config.h" |
10 | | #include "environment.h" |
11 | | #include "strmap.h" |
12 | | #include "gettext.h" |
13 | | #include "hex.h" |
14 | | #include "lockfile.h" |
15 | | #include "iterator.h" |
16 | | #include "refs.h" |
17 | | #include "refs/refs-internal.h" |
18 | | #include "run-command.h" |
19 | | #include "hook.h" |
20 | | #include "object-name.h" |
21 | | #include "odb.h" |
22 | | #include "object.h" |
23 | | #include "path.h" |
24 | | #include "submodule.h" |
25 | | #include "worktree.h" |
26 | | #include "strvec.h" |
27 | | #include "repo-settings.h" |
28 | | #include "setup.h" |
29 | | #include "sigchain.h" |
30 | | #include "date.h" |
31 | | #include "commit.h" |
32 | | #include "wildmatch.h" |
33 | | #include "ident.h" |
34 | | #include "fsck.h" |
35 | | |
36 | | /* |
37 | | * List of all available backends |
38 | | */ |
39 | | static const struct ref_storage_be *refs_backends[] = { |
40 | | [REF_STORAGE_FORMAT_FILES] = &refs_be_files, |
41 | | [REF_STORAGE_FORMAT_REFTABLE] = &refs_be_reftable, |
42 | | }; |
43 | | |
44 | | static const struct ref_storage_be *find_ref_storage_backend( |
45 | | enum ref_storage_format ref_storage_format) |
46 | 0 | { |
47 | 0 | if (ref_storage_format < ARRAY_SIZE(refs_backends)) |
48 | 0 | return refs_backends[ref_storage_format]; |
49 | 0 | return NULL; |
50 | 0 | } |
51 | | |
52 | | enum ref_storage_format ref_storage_format_by_name(const char *name) |
53 | 0 | { |
54 | 0 | for (unsigned int i = 0; i < ARRAY_SIZE(refs_backends); i++) |
55 | 0 | if (refs_backends[i] && !strcmp(refs_backends[i]->name, name)) |
56 | 0 | return i; |
57 | 0 | return REF_STORAGE_FORMAT_UNKNOWN; |
58 | 0 | } |
59 | | |
60 | | const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format) |
61 | 0 | { |
62 | 0 | const struct ref_storage_be *be = find_ref_storage_backend(ref_storage_format); |
63 | 0 | if (!be) |
64 | 0 | return "unknown"; |
65 | 0 | return be->name; |
66 | 0 | } |
67 | | |
68 | | /* |
69 | | * How to handle various characters in refnames: |
70 | | * 0: An acceptable character for refs |
71 | | * 1: End-of-component |
72 | | * 2: ., look for a preceding . to reject .. in refs |
73 | | * 3: {, look for a preceding @ to reject @{ in refs |
74 | | * 4: A bad character: ASCII control characters, and |
75 | | * ":", "?", "[", "\", "^", "~", SP, or TAB |
76 | | * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set |
77 | | */ |
78 | | static unsigned char refname_disposition[256] = { |
79 | | 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
80 | | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
81 | | 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1, |
82 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, |
83 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
84 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0, |
85 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
86 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4 |
87 | | }; |
88 | | |
89 | | struct ref_namespace_info ref_namespace[] = { |
90 | | [NAMESPACE_HEAD] = { |
91 | | .ref = "HEAD", |
92 | | .decoration = DECORATION_REF_HEAD, |
93 | | .exact = 1, |
94 | | }, |
95 | | [NAMESPACE_BRANCHES] = { |
96 | | .ref = "refs/heads/", |
97 | | .decoration = DECORATION_REF_LOCAL, |
98 | | }, |
99 | | [NAMESPACE_TAGS] = { |
100 | | .ref = "refs/tags/", |
101 | | .decoration = DECORATION_REF_TAG, |
102 | | }, |
103 | | [NAMESPACE_REMOTE_REFS] = { |
104 | | /* |
105 | | * The default refspec for new remotes copies refs from |
106 | | * refs/heads/ on the remote into refs/remotes/<remote>/. |
107 | | * As such, "refs/remotes/" has special handling. |
108 | | */ |
109 | | .ref = "refs/remotes/", |
110 | | .decoration = DECORATION_REF_REMOTE, |
111 | | }, |
112 | | [NAMESPACE_STASH] = { |
113 | | /* |
114 | | * The single ref "refs/stash" stores the latest stash. |
115 | | * Older stashes can be found in the reflog. |
116 | | */ |
117 | | .ref = "refs/stash", |
118 | | .exact = 1, |
119 | | .decoration = DECORATION_REF_STASH, |
120 | | }, |
121 | | [NAMESPACE_REPLACE] = { |
122 | | /* |
123 | | * This namespace allows Git to act as if one object ID |
124 | | * points to the content of another. Unlike the other |
125 | | * ref namespaces, this one can be changed by the |
126 | | * GIT_REPLACE_REF_BASE environment variable. This |
127 | | * .namespace value will be overwritten in setup_git_env(). |
128 | | */ |
129 | | .ref = "refs/replace/", |
130 | | .decoration = DECORATION_GRAFTED, |
131 | | }, |
132 | | [NAMESPACE_NOTES] = { |
133 | | /* |
134 | | * The refs/notes/commit ref points to the tip of a |
135 | | * parallel commit history that adds metadata to commits |
136 | | * in the normal history. This ref can be overwritten |
137 | | * by the core.notesRef config variable or the |
138 | | * GIT_NOTES_REFS environment variable. |
139 | | */ |
140 | | .ref = "refs/notes/commit", |
141 | | .exact = 1, |
142 | | }, |
143 | | [NAMESPACE_PREFETCH] = { |
144 | | /* |
145 | | * Prefetch refs are written by the background 'fetch' |
146 | | * maintenance task. It allows faster foreground fetches |
147 | | * by advertising these previously-downloaded tips without |
148 | | * updating refs/remotes/ without user intervention. |
149 | | */ |
150 | | .ref = "refs/prefetch/", |
151 | | }, |
152 | | [NAMESPACE_REWRITTEN] = { |
153 | | /* |
154 | | * Rewritten refs are used by the 'label' command in the |
155 | | * sequencer. These are particularly useful during an |
156 | | * interactive rebase that uses the 'merge' command. |
157 | | */ |
158 | | .ref = "refs/rewritten/", |
159 | | }, |
160 | | }; |
161 | | |
162 | | void update_ref_namespace(enum ref_namespace namespace, char *ref) |
163 | 0 | { |
164 | 0 | struct ref_namespace_info *info = &ref_namespace[namespace]; |
165 | 0 | if (info->ref_updated) |
166 | 0 | free((char *)info->ref); |
167 | 0 | info->ref = ref; |
168 | 0 | info->ref_updated = 1; |
169 | 0 | } |
170 | | |
171 | | /* |
172 | | * Try to read one refname component from the front of refname. |
173 | | * Return the length of the component found, or -1 if the component is |
174 | | * not legal. It is legal if it is something reasonable to have under |
175 | | * ".git/refs/"; We do not like it if: |
176 | | * |
177 | | * - it begins with ".", or |
178 | | * - it has double dots "..", or |
179 | | * - it has ASCII control characters, or |
180 | | * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or |
181 | | * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or |
182 | | * - it ends with a "/", or |
183 | | * - it ends with ".lock", or |
184 | | * - it contains a "@{" portion |
185 | | * |
186 | | * When sanitized is not NULL, instead of rejecting the input refname |
187 | | * as an error, try to come up with a usable replacement for the input |
188 | | * refname in it. |
189 | | */ |
190 | | static int check_refname_component(const char *refname, int *flags, |
191 | | struct strbuf *sanitized) |
192 | 0 | { |
193 | 0 | const char *cp; |
194 | 0 | char last = '\0'; |
195 | 0 | size_t component_start = 0; /* garbage - not a reasonable initial value */ |
196 | |
|
197 | 0 | if (sanitized) |
198 | 0 | component_start = sanitized->len; |
199 | |
|
200 | 0 | for (cp = refname; ; cp++) { |
201 | 0 | int ch = *cp & 255; |
202 | 0 | unsigned char disp = refname_disposition[ch]; |
203 | |
|
204 | 0 | if (sanitized && disp != 1) |
205 | 0 | strbuf_addch(sanitized, ch); |
206 | |
|
207 | 0 | switch (disp) { |
208 | 0 | case 1: |
209 | 0 | goto out; |
210 | 0 | case 2: |
211 | 0 | if (last == '.') { /* Refname contains "..". */ |
212 | 0 | if (sanitized) |
213 | | /* collapse ".." to single "." */ |
214 | 0 | strbuf_setlen(sanitized, sanitized->len - 1); |
215 | 0 | else |
216 | 0 | return -1; |
217 | 0 | } |
218 | 0 | break; |
219 | 0 | case 3: |
220 | 0 | if (last == '@') { /* Refname contains "@{". */ |
221 | 0 | if (sanitized) |
222 | 0 | sanitized->buf[sanitized->len-1] = '-'; |
223 | 0 | else |
224 | 0 | return -1; |
225 | 0 | } |
226 | 0 | break; |
227 | 0 | case 4: |
228 | | /* forbidden char */ |
229 | 0 | if (sanitized) |
230 | 0 | sanitized->buf[sanitized->len-1] = '-'; |
231 | 0 | else |
232 | 0 | return -1; |
233 | 0 | break; |
234 | 0 | case 5: |
235 | 0 | if (!(*flags & REFNAME_REFSPEC_PATTERN)) { |
236 | | /* refspec can't be a pattern */ |
237 | 0 | if (sanitized) |
238 | 0 | sanitized->buf[sanitized->len-1] = '-'; |
239 | 0 | else |
240 | 0 | return -1; |
241 | 0 | } |
242 | | |
243 | | /* |
244 | | * Unset the pattern flag so that we only accept |
245 | | * a single asterisk for one side of refspec. |
246 | | */ |
247 | 0 | *flags &= ~ REFNAME_REFSPEC_PATTERN; |
248 | 0 | break; |
249 | 0 | } |
250 | 0 | last = ch; |
251 | 0 | } |
252 | 0 | out: |
253 | 0 | if (cp == refname) |
254 | 0 | return 0; /* Component has zero length. */ |
255 | | |
256 | 0 | if (refname[0] == '.') { /* Component starts with '.'. */ |
257 | 0 | if (sanitized) |
258 | 0 | sanitized->buf[component_start] = '-'; |
259 | 0 | else |
260 | 0 | return -1; |
261 | 0 | } |
262 | 0 | if (cp - refname >= LOCK_SUFFIX_LEN && |
263 | 0 | !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) { |
264 | 0 | if (!sanitized) |
265 | 0 | return -1; |
266 | | /* Refname ends with ".lock". */ |
267 | 0 | while (strbuf_strip_suffix(sanitized, LOCK_SUFFIX)) { |
268 | | /* try again in case we have .lock.lock */ |
269 | 0 | } |
270 | 0 | } |
271 | 0 | return cp - refname; |
272 | 0 | } |
273 | | |
274 | | static int check_or_sanitize_refname(const char *refname, int flags, |
275 | | struct strbuf *sanitized) |
276 | 0 | { |
277 | 0 | int component_len, component_count = 0; |
278 | |
|
279 | 0 | if (!strcmp(refname, "@")) { |
280 | | /* Refname is a single character '@'. */ |
281 | 0 | if (sanitized) |
282 | 0 | strbuf_addch(sanitized, '-'); |
283 | 0 | else |
284 | 0 | return -1; |
285 | 0 | } |
286 | | |
287 | 0 | while (1) { |
288 | 0 | if (sanitized && sanitized->len) |
289 | 0 | strbuf_complete(sanitized, '/'); |
290 | | |
291 | | /* We are at the start of a path component. */ |
292 | 0 | component_len = check_refname_component(refname, &flags, |
293 | 0 | sanitized); |
294 | 0 | if (sanitized && component_len == 0) |
295 | 0 | ; /* OK, omit empty component */ |
296 | 0 | else if (component_len <= 0) |
297 | 0 | return -1; |
298 | | |
299 | 0 | component_count++; |
300 | 0 | if (refname[component_len] == '\0') |
301 | 0 | break; |
302 | | /* Skip to next component. */ |
303 | 0 | refname += component_len + 1; |
304 | 0 | } |
305 | | |
306 | 0 | if (refname[component_len - 1] == '.') { |
307 | | /* Refname ends with '.'. */ |
308 | 0 | if (sanitized) |
309 | 0 | ; /* omit ending dot */ |
310 | 0 | else |
311 | 0 | return -1; |
312 | 0 | } |
313 | 0 | if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2) |
314 | 0 | return -1; /* Refname has only one component. */ |
315 | 0 | return 0; |
316 | 0 | } |
317 | | |
318 | | int check_refname_format(const char *refname, int flags) |
319 | 0 | { |
320 | 0 | return check_or_sanitize_refname(refname, flags, NULL); |
321 | 0 | } |
322 | | |
323 | | int refs_fsck(struct ref_store *refs, struct fsck_options *o, |
324 | | struct worktree *wt) |
325 | 0 | { |
326 | 0 | if (o->verbose) |
327 | 0 | fprintf_ln(stderr, _("Checking references consistency")); |
328 | |
|
329 | 0 | return refs->be->fsck(refs, o, wt); |
330 | 0 | } |
331 | | |
332 | | void sanitize_refname_component(const char *refname, struct strbuf *out) |
333 | 0 | { |
334 | 0 | if (check_or_sanitize_refname(refname, REFNAME_ALLOW_ONELEVEL, out)) |
335 | 0 | BUG("sanitizing refname '%s' check returned error", refname); |
336 | 0 | } |
337 | | |
338 | | int refname_is_safe(const char *refname) |
339 | 0 | { |
340 | 0 | const char *rest; |
341 | |
|
342 | 0 | if (skip_prefix(refname, "refs/", &rest)) { |
343 | 0 | char *buf; |
344 | 0 | int result; |
345 | 0 | size_t restlen = strlen(rest); |
346 | | |
347 | | /* rest must not be empty, or start or end with "/" */ |
348 | 0 | if (!restlen || *rest == '/' || rest[restlen - 1] == '/') |
349 | 0 | return 0; |
350 | | |
351 | | /* |
352 | | * Does the refname try to escape refs/? |
353 | | * For example: refs/foo/../bar is safe but refs/foo/../../bar |
354 | | * is not. |
355 | | */ |
356 | 0 | buf = xmallocz(restlen); |
357 | 0 | result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest); |
358 | 0 | free(buf); |
359 | 0 | return result; |
360 | 0 | } |
361 | | |
362 | 0 | do { |
363 | 0 | if (!isupper(*refname) && *refname != '_') |
364 | 0 | return 0; |
365 | 0 | refname++; |
366 | 0 | } while (*refname); |
367 | 0 | return 1; |
368 | 0 | } |
369 | | |
370 | | /* |
371 | | * Return true if refname, which has the specified oid and flags, can |
372 | | * be resolved to an object in the database. If the referred-to object |
373 | | * does not exist, emit a warning and return false. |
374 | | */ |
375 | | int ref_resolves_to_object(const char *refname, |
376 | | struct repository *repo, |
377 | | const struct object_id *oid, |
378 | | unsigned int flags) |
379 | 0 | { |
380 | 0 | if (flags & REF_ISBROKEN) |
381 | 0 | return 0; |
382 | 0 | if (!odb_has_object(repo->objects, oid, |
383 | 0 | HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) { |
384 | 0 | error(_("%s does not point to a valid object!"), refname); |
385 | 0 | return 0; |
386 | 0 | } |
387 | 0 | return 1; |
388 | 0 | } |
389 | | |
390 | | char *refs_resolve_refdup(struct ref_store *refs, |
391 | | const char *refname, int resolve_flags, |
392 | | struct object_id *oid, int *flags) |
393 | 0 | { |
394 | 0 | const char *result; |
395 | |
|
396 | 0 | result = refs_resolve_ref_unsafe(refs, refname, resolve_flags, |
397 | 0 | oid, flags); |
398 | 0 | return xstrdup_or_null(result); |
399 | 0 | } |
400 | | |
401 | | /* The argument to for_each_filter_refs */ |
402 | | struct for_each_ref_filter { |
403 | | const char *pattern; |
404 | | const char *prefix; |
405 | | each_ref_fn *fn; |
406 | | void *cb_data; |
407 | | }; |
408 | | |
409 | | int refs_read_ref_full(struct ref_store *refs, const char *refname, |
410 | | int resolve_flags, struct object_id *oid, int *flags) |
411 | 0 | { |
412 | 0 | if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, |
413 | 0 | oid, flags)) |
414 | 0 | return 0; |
415 | 0 | return -1; |
416 | 0 | } |
417 | | |
418 | | int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid) |
419 | 0 | { |
420 | 0 | return refs_read_ref_full(refs, refname, RESOLVE_REF_READING, oid, NULL); |
421 | 0 | } |
422 | | |
423 | | int refs_ref_exists(struct ref_store *refs, const char *refname) |
424 | 0 | { |
425 | 0 | return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING, |
426 | 0 | NULL, NULL); |
427 | 0 | } |
428 | | |
429 | | static int for_each_filter_refs(const struct reference *ref, void *data) |
430 | 0 | { |
431 | 0 | struct for_each_ref_filter *filter = data; |
432 | |
|
433 | 0 | if (wildmatch(filter->pattern, ref->name, 0)) |
434 | 0 | return 0; |
435 | 0 | if (filter->prefix) { |
436 | 0 | struct reference skipped = *ref; |
437 | 0 | skip_prefix(skipped.name, filter->prefix, &skipped.name); |
438 | 0 | return filter->fn(&skipped, filter->cb_data); |
439 | 0 | } else { |
440 | 0 | return filter->fn(ref, filter->cb_data); |
441 | 0 | } |
442 | 0 | } |
443 | | |
444 | | struct warn_if_dangling_data { |
445 | | struct ref_store *refs; |
446 | | FILE *fp; |
447 | | const struct string_list *refnames; |
448 | | const char *indent; |
449 | | int dry_run; |
450 | | }; |
451 | | |
452 | | static int warn_if_dangling_symref(const struct reference *ref, void *cb_data) |
453 | 0 | { |
454 | 0 | struct warn_if_dangling_data *d = cb_data; |
455 | 0 | const char *resolves_to, *msg; |
456 | |
|
457 | 0 | if (!(ref->flags & REF_ISSYMREF)) |
458 | 0 | return 0; |
459 | | |
460 | 0 | resolves_to = refs_resolve_ref_unsafe(d->refs, ref->name, 0, NULL, NULL); |
461 | 0 | if (!resolves_to |
462 | 0 | || !string_list_has_string(d->refnames, resolves_to)) { |
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | 0 | msg = d->dry_run |
467 | 0 | ? _("%s%s will become dangling after %s is deleted\n") |
468 | 0 | : _("%s%s has become dangling after %s was deleted\n"); |
469 | 0 | fprintf(d->fp, msg, d->indent, ref->name, resolves_to); |
470 | 0 | return 0; |
471 | 0 | } |
472 | | |
473 | | void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp, |
474 | | const char *indent, int dry_run, |
475 | | const struct string_list *refnames) |
476 | 0 | { |
477 | 0 | struct warn_if_dangling_data data = { |
478 | 0 | .refs = refs, |
479 | 0 | .fp = fp, |
480 | 0 | .refnames = refnames, |
481 | 0 | .indent = indent, |
482 | 0 | .dry_run = dry_run, |
483 | 0 | }; |
484 | 0 | refs_for_each_rawref(refs, warn_if_dangling_symref, &data); |
485 | 0 | } |
486 | | |
487 | | int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
488 | 0 | { |
489 | 0 | return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data); |
490 | 0 | } |
491 | | |
492 | | int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
493 | 0 | { |
494 | 0 | return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data); |
495 | 0 | } |
496 | | |
497 | | int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
498 | 0 | { |
499 | 0 | return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data); |
500 | 0 | } |
501 | | |
502 | | int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
503 | 0 | { |
504 | 0 | struct strbuf buf = STRBUF_INIT; |
505 | 0 | int ret = 0; |
506 | 0 | struct object_id oid; |
507 | 0 | int flag; |
508 | |
|
509 | 0 | strbuf_addf(&buf, "%sHEAD", get_git_namespace()); |
510 | 0 | if (!refs_read_ref_full(refs, buf.buf, RESOLVE_REF_READING, &oid, &flag)) { |
511 | 0 | struct reference ref = { |
512 | 0 | .name = buf.buf, |
513 | 0 | .oid = &oid, |
514 | 0 | .flags = flag, |
515 | 0 | }; |
516 | |
|
517 | 0 | ret = fn(&ref, cb_data); |
518 | 0 | } |
519 | 0 | strbuf_release(&buf); |
520 | |
|
521 | 0 | return ret; |
522 | 0 | } |
523 | | |
524 | | void normalize_glob_ref(struct string_list_item *item, const char *prefix, |
525 | | const char *pattern) |
526 | 0 | { |
527 | 0 | struct strbuf normalized_pattern = STRBUF_INIT; |
528 | |
|
529 | 0 | if (*pattern == '/') |
530 | 0 | BUG("pattern must not start with '/'"); |
531 | | |
532 | 0 | if (prefix) |
533 | 0 | strbuf_addstr(&normalized_pattern, prefix); |
534 | 0 | else if (!starts_with(pattern, "refs/") && |
535 | 0 | strcmp(pattern, "HEAD")) |
536 | 0 | strbuf_addstr(&normalized_pattern, "refs/"); |
537 | | /* |
538 | | * NEEDSWORK: Special case other symrefs such as REBASE_HEAD, |
539 | | * MERGE_HEAD, etc. |
540 | | */ |
541 | |
|
542 | 0 | strbuf_addstr(&normalized_pattern, pattern); |
543 | 0 | strbuf_strip_suffix(&normalized_pattern, "/"); |
544 | |
|
545 | 0 | item->string = strbuf_detach(&normalized_pattern, NULL); |
546 | 0 | item->util = has_glob_specials(pattern) ? NULL : item->string; |
547 | 0 | strbuf_release(&normalized_pattern); |
548 | 0 | } |
549 | | |
550 | | int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn, |
551 | | const char *pattern, const char *prefix, void *cb_data) |
552 | 0 | { |
553 | 0 | struct strbuf real_pattern = STRBUF_INIT; |
554 | 0 | struct for_each_ref_filter filter; |
555 | 0 | int ret; |
556 | |
|
557 | 0 | if (!prefix && !starts_with(pattern, "refs/")) |
558 | 0 | strbuf_addstr(&real_pattern, "refs/"); |
559 | 0 | else if (prefix) |
560 | 0 | strbuf_addstr(&real_pattern, prefix); |
561 | 0 | strbuf_addstr(&real_pattern, pattern); |
562 | |
|
563 | 0 | if (!has_glob_specials(pattern)) { |
564 | | /* Append implied '/' '*' if not present. */ |
565 | 0 | strbuf_complete(&real_pattern, '/'); |
566 | | /* No need to check for '*', there is none. */ |
567 | 0 | strbuf_addch(&real_pattern, '*'); |
568 | 0 | } |
569 | |
|
570 | 0 | filter.pattern = real_pattern.buf; |
571 | 0 | filter.prefix = prefix; |
572 | 0 | filter.fn = fn; |
573 | 0 | filter.cb_data = cb_data; |
574 | 0 | ret = refs_for_each_ref(refs, for_each_filter_refs, &filter); |
575 | |
|
576 | 0 | strbuf_release(&real_pattern); |
577 | 0 | return ret; |
578 | 0 | } |
579 | | |
580 | | int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn, |
581 | | const char *pattern, void *cb_data) |
582 | 0 | { |
583 | 0 | return refs_for_each_glob_ref_in(refs, fn, pattern, NULL, cb_data); |
584 | 0 | } |
585 | | |
586 | | const char *prettify_refname(const char *name) |
587 | 0 | { |
588 | 0 | if (skip_prefix(name, "refs/heads/", &name) || |
589 | 0 | skip_prefix(name, "refs/tags/", &name) || |
590 | 0 | skip_prefix(name, "refs/remotes/", &name)) |
591 | 0 | ; /* nothing */ |
592 | 0 | return name; |
593 | 0 | } |
594 | | |
595 | | static const char *ref_rev_parse_rules[] = { |
596 | | "%.*s", |
597 | | "refs/%.*s", |
598 | | "refs/tags/%.*s", |
599 | | "refs/heads/%.*s", |
600 | | "refs/remotes/%.*s", |
601 | | "refs/remotes/%.*s/HEAD", |
602 | | NULL |
603 | | }; |
604 | | |
605 | 0 | #define NUM_REV_PARSE_RULES (ARRAY_SIZE(ref_rev_parse_rules) - 1) |
606 | | |
607 | | /* |
608 | | * Is it possible that the caller meant full_name with abbrev_name? |
609 | | * If so return a non-zero value to signal "yes"; the magnitude of |
610 | | * the returned value gives the precedence used for disambiguation. |
611 | | * |
612 | | * If abbrev_name cannot mean full_name, return 0. |
613 | | */ |
614 | | int refname_match(const char *abbrev_name, const char *full_name) |
615 | 0 | { |
616 | 0 | const char **p; |
617 | 0 | const int abbrev_name_len = strlen(abbrev_name); |
618 | 0 | const int num_rules = NUM_REV_PARSE_RULES; |
619 | |
|
620 | 0 | for (p = ref_rev_parse_rules; *p; p++) |
621 | 0 | if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) |
622 | 0 | return &ref_rev_parse_rules[num_rules] - p; |
623 | | |
624 | 0 | return 0; |
625 | 0 | } |
626 | | |
627 | | /* |
628 | | * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add |
629 | | * the results to 'prefixes' |
630 | | */ |
631 | | void expand_ref_prefix(struct strvec *prefixes, const char *prefix) |
632 | 0 | { |
633 | 0 | const char **p; |
634 | 0 | int len = strlen(prefix); |
635 | |
|
636 | 0 | for (p = ref_rev_parse_rules; *p; p++) |
637 | 0 | strvec_pushf(prefixes, *p, len, prefix); |
638 | 0 | } |
639 | | |
640 | | #ifndef WITH_BREAKING_CHANGES |
641 | | static const char default_branch_name_advice[] = N_( |
642 | | "Using '%s' as the name for the initial branch. This default branch name\n" |
643 | | "will change to \"main\" in Git 3.0. To configure the initial branch name\n" |
644 | | "to use in all of your new repositories, which will suppress this warning,\n" |
645 | | "call:\n" |
646 | | "\n" |
647 | | "\tgit config --global init.defaultBranch <name>\n" |
648 | | "\n" |
649 | | "Names commonly chosen instead of 'master' are 'main', 'trunk' and\n" |
650 | | "'development'. The just-created branch can be renamed via this command:\n" |
651 | | "\n" |
652 | | "\tgit branch -m <name>\n" |
653 | | ); |
654 | | #else |
655 | | static const char default_branch_name_advice[] = N_( |
656 | | "Using '%s' as the name for the initial branch since Git 3.0.\n" |
657 | | "If you expected Git to create 'master', the just-created\n" |
658 | | "branch can be renamed via this command:\n" |
659 | | "\n" |
660 | | "\tgit branch -m master\n" |
661 | | ); |
662 | | #endif /* WITH_BREAKING_CHANGES */ |
663 | | |
664 | | char *repo_default_branch_name(struct repository *r, int quiet) |
665 | 0 | { |
666 | 0 | const char *config_key = "init.defaultbranch"; |
667 | 0 | const char *config_display_key = "init.defaultBranch"; |
668 | 0 | char *ret = NULL, *full_ref; |
669 | 0 | const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME"); |
670 | |
|
671 | 0 | if (env && *env) |
672 | 0 | ret = xstrdup(env); |
673 | 0 | if (!ret && repo_config_get_string(r, config_key, &ret) < 0) |
674 | 0 | die(_("could not retrieve `%s`"), config_display_key); |
675 | | |
676 | 0 | if (!ret) { |
677 | | #ifdef WITH_BREAKING_CHANGES |
678 | | ret = xstrdup("main"); |
679 | | #else |
680 | 0 | ret = xstrdup("master"); |
681 | 0 | #endif /* WITH_BREAKING_CHANGES */ |
682 | 0 | if (!quiet) |
683 | 0 | advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME, |
684 | 0 | _(default_branch_name_advice), ret); |
685 | 0 | } |
686 | |
|
687 | 0 | full_ref = xstrfmt("refs/heads/%s", ret); |
688 | 0 | if (check_refname_format(full_ref, 0)) |
689 | 0 | die(_("invalid branch name: %s = %s"), config_display_key, ret); |
690 | 0 | free(full_ref); |
691 | |
|
692 | 0 | return ret; |
693 | 0 | } |
694 | | |
695 | | /* |
696 | | * *string and *len will only be substituted, and *string returned (for |
697 | | * later free()ing) if the string passed in is a magic short-hand form |
698 | | * to name a branch. |
699 | | */ |
700 | | static char *substitute_branch_name(struct repository *r, |
701 | | const char **string, int *len, |
702 | | int nonfatal_dangling_mark) |
703 | 0 | { |
704 | 0 | struct strbuf buf = STRBUF_INIT; |
705 | 0 | struct interpret_branch_name_options options = { |
706 | 0 | .nonfatal_dangling_mark = nonfatal_dangling_mark |
707 | 0 | }; |
708 | 0 | int ret = repo_interpret_branch_name(r, *string, *len, &buf, &options); |
709 | |
|
710 | 0 | if (ret == *len) { |
711 | 0 | size_t size; |
712 | 0 | *string = strbuf_detach(&buf, &size); |
713 | 0 | *len = size; |
714 | 0 | return (char *)*string; |
715 | 0 | } |
716 | | |
717 | 0 | return NULL; |
718 | 0 | } |
719 | | |
720 | | void copy_branchname(struct strbuf *sb, const char *name, unsigned allowed) |
721 | 0 | { |
722 | 0 | int len = strlen(name); |
723 | 0 | struct interpret_branch_name_options options = { |
724 | 0 | .allowed = allowed |
725 | 0 | }; |
726 | 0 | int used = repo_interpret_branch_name(the_repository, name, len, sb, |
727 | 0 | &options); |
728 | |
|
729 | 0 | if (used < 0) |
730 | 0 | used = 0; |
731 | 0 | strbuf_add(sb, name + used, len - used); |
732 | 0 | } |
733 | | |
734 | | int check_branch_ref(struct strbuf *sb, const char *name) |
735 | 0 | { |
736 | 0 | if (startup_info->have_repository) |
737 | 0 | copy_branchname(sb, name, INTERPRET_BRANCH_LOCAL); |
738 | 0 | else |
739 | 0 | strbuf_addstr(sb, name); |
740 | | |
741 | | /* |
742 | | * This splice must be done even if we end up rejecting the |
743 | | * name; builtin/branch.c::copy_or_rename_branch() still wants |
744 | | * to see what the name expanded to so that "branch -m" can be |
745 | | * used as a tool to correct earlier mistakes. |
746 | | */ |
747 | 0 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
748 | |
|
749 | 0 | if (*name == '-' || |
750 | 0 | !strcmp(sb->buf, "refs/heads/HEAD")) |
751 | 0 | return -1; |
752 | | |
753 | 0 | return check_refname_format(sb->buf, 0); |
754 | 0 | } |
755 | | |
756 | | int check_tag_ref(struct strbuf *sb, const char *name) |
757 | 0 | { |
758 | 0 | if (name[0] == '-' || !strcmp(name, "HEAD")) |
759 | 0 | return -1; |
760 | | |
761 | 0 | strbuf_reset(sb); |
762 | 0 | strbuf_addf(sb, "refs/tags/%s", name); |
763 | |
|
764 | 0 | return check_refname_format(sb->buf, 0); |
765 | 0 | } |
766 | | |
767 | | int repo_dwim_ref(struct repository *r, const char *str, int len, |
768 | | struct object_id *oid, char **ref, int nonfatal_dangling_mark) |
769 | 0 | { |
770 | 0 | char *last_branch = substitute_branch_name(r, &str, &len, |
771 | 0 | nonfatal_dangling_mark); |
772 | 0 | int refs_found = expand_ref(r, str, len, oid, ref); |
773 | 0 | free(last_branch); |
774 | 0 | return refs_found; |
775 | 0 | } |
776 | | |
777 | | int expand_ref(struct repository *repo, const char *str, int len, |
778 | | struct object_id *oid, char **ref) |
779 | 0 | { |
780 | 0 | const char **p, *r; |
781 | 0 | int refs_found = 0; |
782 | 0 | struct strbuf fullref = STRBUF_INIT; |
783 | |
|
784 | 0 | *ref = NULL; |
785 | 0 | for (p = ref_rev_parse_rules; *p; p++) { |
786 | 0 | struct object_id oid_from_ref; |
787 | 0 | struct object_id *this_result; |
788 | 0 | int flag; |
789 | 0 | struct ref_store *refs = get_main_ref_store(repo); |
790 | |
|
791 | 0 | this_result = refs_found ? &oid_from_ref : oid; |
792 | 0 | strbuf_reset(&fullref); |
793 | 0 | strbuf_addf(&fullref, *p, len, str); |
794 | 0 | r = refs_resolve_ref_unsafe(refs, fullref.buf, |
795 | 0 | RESOLVE_REF_READING, |
796 | 0 | this_result, &flag); |
797 | 0 | if (r) { |
798 | 0 | if (!refs_found++) |
799 | 0 | *ref = xstrdup(r); |
800 | 0 | if (!repo_settings_get_warn_ambiguous_refs(repo)) |
801 | 0 | break; |
802 | 0 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) { |
803 | 0 | warning(_("ignoring dangling symref %s"), fullref.buf); |
804 | 0 | } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) { |
805 | 0 | warning(_("ignoring broken ref %s"), fullref.buf); |
806 | 0 | } |
807 | 0 | } |
808 | 0 | strbuf_release(&fullref); |
809 | 0 | return refs_found; |
810 | 0 | } |
811 | | |
812 | | int repo_dwim_log(struct repository *r, const char *str, int len, |
813 | | struct object_id *oid, char **log) |
814 | 0 | { |
815 | 0 | struct ref_store *refs = get_main_ref_store(r); |
816 | 0 | char *last_branch = substitute_branch_name(r, &str, &len, 0); |
817 | 0 | const char **p; |
818 | 0 | int logs_found = 0; |
819 | 0 | struct strbuf path = STRBUF_INIT; |
820 | |
|
821 | 0 | *log = NULL; |
822 | 0 | for (p = ref_rev_parse_rules; *p; p++) { |
823 | 0 | struct object_id hash; |
824 | 0 | const char *ref, *it; |
825 | |
|
826 | 0 | strbuf_reset(&path); |
827 | 0 | strbuf_addf(&path, *p, len, str); |
828 | 0 | ref = refs_resolve_ref_unsafe(refs, path.buf, |
829 | 0 | RESOLVE_REF_READING, |
830 | 0 | oid ? &hash : NULL, NULL); |
831 | 0 | if (!ref) |
832 | 0 | continue; |
833 | 0 | if (refs_reflog_exists(refs, path.buf)) |
834 | 0 | it = path.buf; |
835 | 0 | else if (strcmp(ref, path.buf) && |
836 | 0 | refs_reflog_exists(refs, ref)) |
837 | 0 | it = ref; |
838 | 0 | else |
839 | 0 | continue; |
840 | 0 | if (!logs_found++) { |
841 | 0 | *log = xstrdup(it); |
842 | 0 | if (oid) |
843 | 0 | oidcpy(oid, &hash); |
844 | 0 | } |
845 | 0 | if (!repo_settings_get_warn_ambiguous_refs(r)) |
846 | 0 | break; |
847 | 0 | } |
848 | 0 | strbuf_release(&path); |
849 | 0 | free(last_branch); |
850 | 0 | return logs_found; |
851 | 0 | } |
852 | | |
853 | | int is_per_worktree_ref(const char *refname) |
854 | 0 | { |
855 | 0 | return starts_with(refname, "refs/worktree/") || |
856 | 0 | starts_with(refname, "refs/bisect/") || |
857 | 0 | starts_with(refname, "refs/rewritten/"); |
858 | 0 | } |
859 | | |
860 | | int is_pseudo_ref(const char *refname) |
861 | 0 | { |
862 | 0 | static const char * const pseudo_refs[] = { |
863 | 0 | "FETCH_HEAD", |
864 | 0 | "MERGE_HEAD", |
865 | 0 | }; |
866 | 0 | size_t i; |
867 | |
|
868 | 0 | for (i = 0; i < ARRAY_SIZE(pseudo_refs); i++) |
869 | 0 | if (!strcmp(refname, pseudo_refs[i])) |
870 | 0 | return 1; |
871 | | |
872 | 0 | return 0; |
873 | 0 | } |
874 | | |
875 | | static int is_root_ref_syntax(const char *refname) |
876 | 0 | { |
877 | 0 | const char *c; |
878 | |
|
879 | 0 | for (c = refname; *c; c++) { |
880 | 0 | if (!isupper(*c) && *c != '-' && *c != '_') |
881 | 0 | return 0; |
882 | 0 | } |
883 | | |
884 | 0 | return 1; |
885 | 0 | } |
886 | | |
887 | | int is_root_ref(const char *refname) |
888 | 0 | { |
889 | 0 | static const char *const irregular_root_refs[] = { |
890 | 0 | "HEAD", |
891 | 0 | "AUTO_MERGE", |
892 | 0 | "BISECT_EXPECTED_REV", |
893 | 0 | "NOTES_MERGE_PARTIAL", |
894 | 0 | "NOTES_MERGE_REF", |
895 | 0 | "MERGE_AUTOSTASH", |
896 | 0 | }; |
897 | 0 | size_t i; |
898 | |
|
899 | 0 | if (!is_root_ref_syntax(refname) || |
900 | 0 | is_pseudo_ref(refname)) |
901 | 0 | return 0; |
902 | | |
903 | 0 | if (ends_with(refname, "_HEAD")) |
904 | 0 | return 1; |
905 | | |
906 | 0 | for (i = 0; i < ARRAY_SIZE(irregular_root_refs); i++) |
907 | 0 | if (!strcmp(refname, irregular_root_refs[i])) |
908 | 0 | return 1; |
909 | | |
910 | 0 | return 0; |
911 | 0 | } |
912 | | |
913 | 0 | static int is_current_worktree_ref(const char *ref) { |
914 | 0 | return is_root_ref_syntax(ref) || is_per_worktree_ref(ref); |
915 | 0 | } |
916 | | |
917 | | enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref, |
918 | | const char **worktree_name, int *worktree_name_length, |
919 | | const char **bare_refname) |
920 | 0 | { |
921 | 0 | const char *name_dummy; |
922 | 0 | int name_length_dummy; |
923 | 0 | const char *ref_dummy; |
924 | |
|
925 | 0 | if (!worktree_name) |
926 | 0 | worktree_name = &name_dummy; |
927 | 0 | if (!worktree_name_length) |
928 | 0 | worktree_name_length = &name_length_dummy; |
929 | 0 | if (!bare_refname) |
930 | 0 | bare_refname = &ref_dummy; |
931 | |
|
932 | 0 | if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) { |
933 | 0 | const char *slash = strchr(*bare_refname, '/'); |
934 | |
|
935 | 0 | *worktree_name = *bare_refname; |
936 | 0 | if (!slash) { |
937 | 0 | *worktree_name_length = strlen(*worktree_name); |
938 | | |
939 | | /* This is an error condition, and the caller tell because the bare_refname is "" */ |
940 | 0 | *bare_refname = *worktree_name + *worktree_name_length; |
941 | 0 | return REF_WORKTREE_OTHER; |
942 | 0 | } |
943 | | |
944 | 0 | *worktree_name_length = slash - *bare_refname; |
945 | 0 | *bare_refname = slash + 1; |
946 | |
|
947 | 0 | if (is_current_worktree_ref(*bare_refname)) |
948 | 0 | return REF_WORKTREE_OTHER; |
949 | 0 | } |
950 | | |
951 | 0 | *worktree_name = NULL; |
952 | 0 | *worktree_name_length = 0; |
953 | |
|
954 | 0 | if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname) |
955 | 0 | && is_current_worktree_ref(*bare_refname)) |
956 | 0 | return REF_WORKTREE_MAIN; |
957 | | |
958 | 0 | *bare_refname = maybe_worktree_ref; |
959 | 0 | if (is_current_worktree_ref(maybe_worktree_ref)) |
960 | 0 | return REF_WORKTREE_CURRENT; |
961 | | |
962 | 0 | return REF_WORKTREE_SHARED; |
963 | 0 | } |
964 | | |
965 | | long get_files_ref_lock_timeout_ms(void) |
966 | 0 | { |
967 | 0 | static int configured = 0; |
968 | | |
969 | | /* The default timeout is 100 ms: */ |
970 | 0 | static int timeout_ms = 100; |
971 | |
|
972 | 0 | if (!configured) { |
973 | 0 | repo_config_get_int(the_repository, "core.filesreflocktimeout", &timeout_ms); |
974 | 0 | configured = 1; |
975 | 0 | } |
976 | |
|
977 | 0 | return timeout_ms; |
978 | 0 | } |
979 | | |
980 | | int refs_delete_ref(struct ref_store *refs, const char *msg, |
981 | | const char *refname, |
982 | | const struct object_id *old_oid, |
983 | | unsigned int flags) |
984 | 0 | { |
985 | 0 | struct ref_transaction *transaction; |
986 | 0 | struct strbuf err = STRBUF_INIT; |
987 | |
|
988 | 0 | transaction = ref_store_transaction_begin(refs, 0, &err); |
989 | 0 | if (!transaction || |
990 | 0 | ref_transaction_delete(transaction, refname, old_oid, |
991 | 0 | NULL, flags, msg, &err) || |
992 | 0 | ref_transaction_commit(transaction, &err)) { |
993 | 0 | error("%s", err.buf); |
994 | 0 | ref_transaction_free(transaction); |
995 | 0 | strbuf_release(&err); |
996 | 0 | return 1; |
997 | 0 | } |
998 | 0 | ref_transaction_free(transaction); |
999 | 0 | strbuf_release(&err); |
1000 | 0 | return 0; |
1001 | 0 | } |
1002 | | |
1003 | | static void copy_reflog_msg(struct strbuf *sb, const char *msg) |
1004 | 0 | { |
1005 | 0 | char c; |
1006 | 0 | int wasspace = 1; |
1007 | |
|
1008 | 0 | while ((c = *msg++)) { |
1009 | 0 | if (wasspace && isspace(c)) |
1010 | 0 | continue; |
1011 | 0 | wasspace = isspace(c); |
1012 | 0 | if (wasspace) |
1013 | 0 | c = ' '; |
1014 | 0 | strbuf_addch(sb, c); |
1015 | 0 | } |
1016 | 0 | strbuf_rtrim(sb); |
1017 | 0 | } |
1018 | | |
1019 | | static char *normalize_reflog_message(const char *msg) |
1020 | 0 | { |
1021 | 0 | struct strbuf sb = STRBUF_INIT; |
1022 | |
|
1023 | 0 | if (msg && *msg) |
1024 | 0 | copy_reflog_msg(&sb, msg); |
1025 | 0 | return strbuf_detach(&sb, NULL); |
1026 | 0 | } |
1027 | | |
1028 | | int should_autocreate_reflog(enum log_refs_config log_all_ref_updates, |
1029 | | const char *refname) |
1030 | 0 | { |
1031 | 0 | switch (log_all_ref_updates) { |
1032 | 0 | case LOG_REFS_ALWAYS: |
1033 | 0 | return 1; |
1034 | 0 | case LOG_REFS_NORMAL: |
1035 | 0 | return starts_with(refname, "refs/heads/") || |
1036 | 0 | starts_with(refname, "refs/remotes/") || |
1037 | 0 | starts_with(refname, "refs/notes/") || |
1038 | 0 | !strcmp(refname, "HEAD"); |
1039 | 0 | default: |
1040 | 0 | return 0; |
1041 | 0 | } |
1042 | 0 | } |
1043 | | |
1044 | | int is_branch(const char *refname) |
1045 | 0 | { |
1046 | 0 | return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/"); |
1047 | 0 | } |
1048 | | |
1049 | | struct read_ref_at_cb { |
1050 | | timestamp_t at_time; |
1051 | | int cnt; |
1052 | | int reccnt; |
1053 | | struct object_id *oid; |
1054 | | int found_it; |
1055 | | |
1056 | | struct object_id ooid; |
1057 | | struct object_id noid; |
1058 | | int tz; |
1059 | | timestamp_t date; |
1060 | | char **msg; |
1061 | | timestamp_t *cutoff_time; |
1062 | | int *cutoff_tz; |
1063 | | int *cutoff_cnt; |
1064 | | }; |
1065 | | |
1066 | | static void set_read_ref_cutoffs(struct read_ref_at_cb *cb, |
1067 | | timestamp_t timestamp, int tz, const char *message) |
1068 | 0 | { |
1069 | 0 | if (cb->msg) |
1070 | 0 | *cb->msg = xstrdup(message); |
1071 | 0 | if (cb->cutoff_time) |
1072 | 0 | *cb->cutoff_time = timestamp; |
1073 | 0 | if (cb->cutoff_tz) |
1074 | 0 | *cb->cutoff_tz = tz; |
1075 | 0 | if (cb->cutoff_cnt) |
1076 | 0 | *cb->cutoff_cnt = cb->reccnt; |
1077 | 0 | } |
1078 | | |
1079 | | static int read_ref_at_ent(const char *refname, |
1080 | | struct object_id *ooid, struct object_id *noid, |
1081 | | const char *email UNUSED, |
1082 | | timestamp_t timestamp, int tz, |
1083 | | const char *message, void *cb_data) |
1084 | 0 | { |
1085 | 0 | struct read_ref_at_cb *cb = cb_data; |
1086 | |
|
1087 | 0 | cb->tz = tz; |
1088 | 0 | cb->date = timestamp; |
1089 | |
|
1090 | 0 | if (timestamp <= cb->at_time || cb->cnt == 0) { |
1091 | 0 | set_read_ref_cutoffs(cb, timestamp, tz, message); |
1092 | | /* |
1093 | | * we have not yet updated cb->[n|o]oid so they still |
1094 | | * hold the values for the previous record. |
1095 | | */ |
1096 | 0 | if (!is_null_oid(&cb->ooid)) { |
1097 | 0 | oidcpy(cb->oid, noid); |
1098 | 0 | if (!oideq(&cb->ooid, noid)) |
1099 | 0 | warning(_("log for ref %s has gap after %s"), |
1100 | 0 | refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); |
1101 | 0 | } |
1102 | 0 | else if (cb->date == cb->at_time) |
1103 | 0 | oidcpy(cb->oid, noid); |
1104 | 0 | else if (!oideq(noid, cb->oid)) |
1105 | 0 | warning(_("log for ref %s unexpectedly ended on %s"), |
1106 | 0 | refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); |
1107 | 0 | cb->reccnt++; |
1108 | 0 | oidcpy(&cb->ooid, ooid); |
1109 | 0 | oidcpy(&cb->noid, noid); |
1110 | 0 | cb->found_it = 1; |
1111 | 0 | return 1; |
1112 | 0 | } |
1113 | 0 | cb->reccnt++; |
1114 | 0 | oidcpy(&cb->ooid, ooid); |
1115 | 0 | oidcpy(&cb->noid, noid); |
1116 | 0 | if (cb->cnt > 0) |
1117 | 0 | cb->cnt--; |
1118 | 0 | return 0; |
1119 | 0 | } |
1120 | | |
1121 | | static int read_ref_at_ent_oldest(const char *refname UNUSED, |
1122 | | struct object_id *ooid, struct object_id *noid, |
1123 | | const char *email UNUSED, |
1124 | | timestamp_t timestamp, int tz, |
1125 | | const char *message, void *cb_data) |
1126 | 0 | { |
1127 | 0 | struct read_ref_at_cb *cb = cb_data; |
1128 | |
|
1129 | 0 | set_read_ref_cutoffs(cb, timestamp, tz, message); |
1130 | 0 | oidcpy(cb->oid, ooid); |
1131 | 0 | if (cb->at_time && is_null_oid(cb->oid)) |
1132 | 0 | oidcpy(cb->oid, noid); |
1133 | | /* We just want the first entry */ |
1134 | 0 | return 1; |
1135 | 0 | } |
1136 | | |
1137 | | int read_ref_at(struct ref_store *refs, const char *refname, |
1138 | | unsigned int flags, timestamp_t at_time, int cnt, |
1139 | | struct object_id *oid, char **msg, |
1140 | | timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt) |
1141 | 0 | { |
1142 | 0 | struct read_ref_at_cb cb; |
1143 | |
|
1144 | 0 | memset(&cb, 0, sizeof(cb)); |
1145 | 0 | cb.at_time = at_time; |
1146 | 0 | cb.cnt = cnt; |
1147 | 0 | cb.msg = msg; |
1148 | 0 | cb.cutoff_time = cutoff_time; |
1149 | 0 | cb.cutoff_tz = cutoff_tz; |
1150 | 0 | cb.cutoff_cnt = cutoff_cnt; |
1151 | 0 | cb.oid = oid; |
1152 | |
|
1153 | 0 | refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb); |
1154 | |
|
1155 | 0 | if (!cb.reccnt) { |
1156 | 0 | if (cnt == 0) { |
1157 | | /* |
1158 | | * The caller asked for ref@{0}, and we had no entries. |
1159 | | * It's a bit subtle, but in practice all callers have |
1160 | | * prepped the "oid" field with the current value of |
1161 | | * the ref, which is the most reasonable fallback. |
1162 | | * |
1163 | | * We'll put dummy values into the out-parameters (so |
1164 | | * they're not just uninitialized garbage), and the |
1165 | | * caller can take our return value as a hint that |
1166 | | * we did not find any such reflog. |
1167 | | */ |
1168 | 0 | set_read_ref_cutoffs(&cb, 0, 0, "empty reflog"); |
1169 | 0 | return 1; |
1170 | 0 | } |
1171 | 0 | if (flags & GET_OID_QUIETLY) |
1172 | 0 | exit(128); |
1173 | 0 | else |
1174 | 0 | die(_("log for %s is empty"), refname); |
1175 | 0 | } |
1176 | 0 | if (cb.found_it) |
1177 | 0 | return 0; |
1178 | | |
1179 | 0 | refs_for_each_reflog_ent(refs, refname, read_ref_at_ent_oldest, &cb); |
1180 | |
|
1181 | 0 | return 1; |
1182 | 0 | } |
1183 | | |
1184 | | struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, |
1185 | | unsigned int flags, |
1186 | | struct strbuf *err) |
1187 | 0 | { |
1188 | 0 | struct ref_transaction *tr; |
1189 | 0 | assert(err); |
1190 | |
|
1191 | 0 | CALLOC_ARRAY(tr, 1); |
1192 | 0 | tr->ref_store = refs; |
1193 | 0 | tr->flags = flags; |
1194 | 0 | string_list_init_dup(&tr->refnames); |
1195 | |
|
1196 | 0 | if (flags & REF_TRANSACTION_ALLOW_FAILURE) |
1197 | 0 | CALLOC_ARRAY(tr->rejections, 1); |
1198 | |
|
1199 | 0 | return tr; |
1200 | 0 | } |
1201 | | |
1202 | | void ref_transaction_free(struct ref_transaction *transaction) |
1203 | 0 | { |
1204 | 0 | size_t i; |
1205 | |
|
1206 | 0 | if (!transaction) |
1207 | 0 | return; |
1208 | | |
1209 | 0 | switch (transaction->state) { |
1210 | 0 | case REF_TRANSACTION_OPEN: |
1211 | 0 | case REF_TRANSACTION_CLOSED: |
1212 | | /* OK */ |
1213 | 0 | break; |
1214 | 0 | case REF_TRANSACTION_PREPARED: |
1215 | 0 | BUG("free called on a prepared reference transaction"); |
1216 | 0 | break; |
1217 | 0 | default: |
1218 | 0 | BUG("unexpected reference transaction state"); |
1219 | 0 | break; |
1220 | 0 | } |
1221 | | |
1222 | 0 | for (i = 0; i < transaction->nr; i++) { |
1223 | 0 | free(transaction->updates[i]->msg); |
1224 | 0 | free(transaction->updates[i]->committer_info); |
1225 | 0 | free((char *)transaction->updates[i]->new_target); |
1226 | 0 | free((char *)transaction->updates[i]->old_target); |
1227 | 0 | free(transaction->updates[i]); |
1228 | 0 | } |
1229 | |
|
1230 | 0 | if (transaction->rejections) |
1231 | 0 | free(transaction->rejections->update_indices); |
1232 | 0 | free(transaction->rejections); |
1233 | |
|
1234 | 0 | string_list_clear(&transaction->refnames, 0); |
1235 | 0 | free(transaction->updates); |
1236 | 0 | free(transaction); |
1237 | 0 | } |
1238 | | |
1239 | | int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction, |
1240 | | size_t update_idx, |
1241 | | enum ref_transaction_error err) |
1242 | 0 | { |
1243 | 0 | if (update_idx >= transaction->nr) |
1244 | 0 | BUG("trying to set rejection on invalid update index"); |
1245 | | |
1246 | 0 | if (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE)) |
1247 | 0 | return 0; |
1248 | | |
1249 | 0 | if (!transaction->rejections) |
1250 | 0 | BUG("transaction not initialized with failure support"); |
1251 | | |
1252 | | /* |
1253 | | * Don't accept generic errors, since these errors are not user |
1254 | | * input related. |
1255 | | */ |
1256 | 0 | if (err == REF_TRANSACTION_ERROR_GENERIC) |
1257 | 0 | return 0; |
1258 | | |
1259 | | /* |
1260 | | * Rejected refnames shouldn't be considered in the availability |
1261 | | * checks, so remove them from the list. |
1262 | | */ |
1263 | 0 | string_list_remove(&transaction->refnames, |
1264 | 0 | transaction->updates[update_idx]->refname, 0); |
1265 | |
|
1266 | 0 | transaction->updates[update_idx]->rejection_err = err; |
1267 | 0 | ALLOC_GROW(transaction->rejections->update_indices, |
1268 | 0 | transaction->rejections->nr + 1, |
1269 | 0 | transaction->rejections->alloc); |
1270 | 0 | transaction->rejections->update_indices[transaction->rejections->nr++] = update_idx; |
1271 | |
|
1272 | 0 | return 1; |
1273 | 0 | } |
1274 | | |
1275 | | struct ref_update *ref_transaction_add_update( |
1276 | | struct ref_transaction *transaction, |
1277 | | const char *refname, unsigned int flags, |
1278 | | const struct object_id *new_oid, |
1279 | | const struct object_id *old_oid, |
1280 | | const char *new_target, const char *old_target, |
1281 | | const char *committer_info, |
1282 | | const char *msg) |
1283 | 0 | { |
1284 | 0 | struct string_list_item *item; |
1285 | 0 | struct ref_update *update; |
1286 | |
|
1287 | 0 | if (transaction->state != REF_TRANSACTION_OPEN) |
1288 | 0 | BUG("update called for transaction that is not open"); |
1289 | | |
1290 | 0 | if (old_oid && old_target) |
1291 | 0 | BUG("only one of old_oid and old_target should be non NULL"); |
1292 | 0 | if (new_oid && new_target) |
1293 | 0 | BUG("only one of new_oid and new_target should be non NULL"); |
1294 | | |
1295 | 0 | FLEX_ALLOC_STR(update, refname, refname); |
1296 | 0 | ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); |
1297 | 0 | transaction->updates[transaction->nr++] = update; |
1298 | |
|
1299 | 0 | update->flags = flags; |
1300 | 0 | update->rejection_err = 0; |
1301 | |
|
1302 | 0 | update->new_target = xstrdup_or_null(new_target); |
1303 | 0 | update->old_target = xstrdup_or_null(old_target); |
1304 | 0 | if ((flags & REF_HAVE_NEW) && new_oid) |
1305 | 0 | oidcpy(&update->new_oid, new_oid); |
1306 | 0 | if ((flags & REF_HAVE_OLD) && old_oid) |
1307 | 0 | oidcpy(&update->old_oid, old_oid); |
1308 | 0 | if (!(flags & REF_SKIP_CREATE_REFLOG)) { |
1309 | 0 | update->committer_info = xstrdup_or_null(committer_info); |
1310 | 0 | update->msg = normalize_reflog_message(msg); |
1311 | 0 | } |
1312 | | |
1313 | | /* |
1314 | | * This list is generally used by the backends to avoid duplicates. |
1315 | | * But we do support multiple log updates for a given refname within |
1316 | | * a single transaction. |
1317 | | */ |
1318 | 0 | if (!(update->flags & REF_LOG_ONLY)) { |
1319 | 0 | item = string_list_append(&transaction->refnames, refname); |
1320 | 0 | item->util = update; |
1321 | 0 | } |
1322 | |
|
1323 | 0 | return update; |
1324 | 0 | } |
1325 | | |
1326 | | static int transaction_refname_valid(const char *refname, |
1327 | | const struct object_id *new_oid, |
1328 | | unsigned int flags, struct strbuf *err) |
1329 | 0 | { |
1330 | 0 | if (flags & REF_SKIP_REFNAME_VERIFICATION) |
1331 | 0 | return 1; |
1332 | | |
1333 | 0 | if (is_pseudo_ref(refname)) { |
1334 | 0 | const char *refusal_msg; |
1335 | 0 | if (flags & REF_LOG_ONLY) |
1336 | 0 | refusal_msg = _("refusing to update reflog for pseudoref '%s'"); |
1337 | 0 | else |
1338 | 0 | refusal_msg = _("refusing to update pseudoref '%s'"); |
1339 | 0 | strbuf_addf(err, refusal_msg, refname); |
1340 | 0 | return 0; |
1341 | 0 | } else if ((new_oid && !is_null_oid(new_oid)) ? |
1342 | 0 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) : |
1343 | 0 | !refname_is_safe(refname)) { |
1344 | 0 | const char *refusal_msg; |
1345 | 0 | if (flags & REF_LOG_ONLY) |
1346 | 0 | refusal_msg = _("refusing to update reflog with bad name '%s'"); |
1347 | 0 | else |
1348 | 0 | refusal_msg = _("refusing to update ref with bad name '%s'"); |
1349 | 0 | strbuf_addf(err, refusal_msg, refname); |
1350 | 0 | return 0; |
1351 | 0 | } |
1352 | | |
1353 | 0 | return 1; |
1354 | 0 | } |
1355 | | |
1356 | | int ref_transaction_update(struct ref_transaction *transaction, |
1357 | | const char *refname, |
1358 | | const struct object_id *new_oid, |
1359 | | const struct object_id *old_oid, |
1360 | | const char *new_target, |
1361 | | const char *old_target, |
1362 | | unsigned int flags, const char *msg, |
1363 | | struct strbuf *err) |
1364 | 0 | { |
1365 | 0 | assert(err); |
1366 | |
|
1367 | 0 | if ((flags & REF_FORCE_CREATE_REFLOG) && |
1368 | 0 | (flags & REF_SKIP_CREATE_REFLOG)) { |
1369 | 0 | strbuf_addstr(err, _("refusing to force and skip creation of reflog")); |
1370 | 0 | return -1; |
1371 | 0 | } |
1372 | | |
1373 | 0 | if (!transaction_refname_valid(refname, new_oid, flags, err)) |
1374 | 0 | return -1; |
1375 | | |
1376 | 0 | if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS) |
1377 | 0 | BUG("illegal flags 0x%x passed to ref_transaction_update()", flags); |
1378 | | |
1379 | | /* |
1380 | | * Clear flags outside the allowed set; this should be a noop because |
1381 | | * of the BUG() check above, but it works around a -Wnonnull warning |
1382 | | * with some versions of "gcc -O3". |
1383 | | */ |
1384 | 0 | flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; |
1385 | |
|
1386 | 0 | flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); |
1387 | 0 | flags |= (new_target ? REF_HAVE_NEW : 0) | (old_target ? REF_HAVE_OLD : 0); |
1388 | |
|
1389 | 0 | ref_transaction_add_update(transaction, refname, flags, |
1390 | 0 | new_oid, old_oid, new_target, |
1391 | 0 | old_target, NULL, msg); |
1392 | |
|
1393 | 0 | return 0; |
1394 | 0 | } |
1395 | | |
1396 | | int ref_transaction_update_reflog(struct ref_transaction *transaction, |
1397 | | const char *refname, |
1398 | | const struct object_id *new_oid, |
1399 | | const struct object_id *old_oid, |
1400 | | const char *committer_info, |
1401 | | const char *msg, |
1402 | | uint64_t index, |
1403 | | struct strbuf *err) |
1404 | 0 | { |
1405 | 0 | struct ref_update *update; |
1406 | 0 | unsigned int flags; |
1407 | |
|
1408 | 0 | assert(err); |
1409 | |
|
1410 | 0 | flags = REF_HAVE_OLD | REF_HAVE_NEW | REF_LOG_ONLY | REF_FORCE_CREATE_REFLOG | REF_NO_DEREF | |
1411 | 0 | REF_LOG_USE_PROVIDED_OIDS; |
1412 | |
|
1413 | 0 | if (!transaction_refname_valid(refname, new_oid, flags, err)) |
1414 | 0 | return -1; |
1415 | | |
1416 | 0 | update = ref_transaction_add_update(transaction, refname, flags, |
1417 | 0 | new_oid, old_oid, NULL, NULL, |
1418 | 0 | committer_info, msg); |
1419 | 0 | update->index = index; |
1420 | | |
1421 | | /* |
1422 | | * Reference backends may need to know the max index to optimize |
1423 | | * their writes. So we store the max_index on the transaction level. |
1424 | | */ |
1425 | 0 | if (index > transaction->max_index) |
1426 | 0 | transaction->max_index = index; |
1427 | |
|
1428 | 0 | return 0; |
1429 | 0 | } |
1430 | | |
1431 | | int ref_transaction_create(struct ref_transaction *transaction, |
1432 | | const char *refname, |
1433 | | const struct object_id *new_oid, |
1434 | | const char *new_target, |
1435 | | unsigned int flags, const char *msg, |
1436 | | struct strbuf *err) |
1437 | 0 | { |
1438 | 0 | if (new_oid && new_target) |
1439 | 0 | BUG("create called with both new_oid and new_target set"); |
1440 | 0 | if ((!new_oid || is_null_oid(new_oid)) && !new_target) { |
1441 | 0 | strbuf_addf(err, "'%s' has neither a valid OID nor a target", refname); |
1442 | 0 | return 1; |
1443 | 0 | } |
1444 | 0 | return ref_transaction_update(transaction, refname, new_oid, |
1445 | 0 | null_oid(the_hash_algo), new_target, NULL, flags, |
1446 | 0 | msg, err); |
1447 | 0 | } |
1448 | | |
1449 | | int ref_transaction_delete(struct ref_transaction *transaction, |
1450 | | const char *refname, |
1451 | | const struct object_id *old_oid, |
1452 | | const char *old_target, |
1453 | | unsigned int flags, |
1454 | | const char *msg, |
1455 | | struct strbuf *err) |
1456 | 0 | { |
1457 | 0 | if (old_oid && is_null_oid(old_oid)) |
1458 | 0 | BUG("delete called with old_oid set to zeros"); |
1459 | 0 | if (old_oid && old_target) |
1460 | 0 | BUG("delete called with both old_oid and old_target set"); |
1461 | 0 | if (old_target && !(flags & REF_NO_DEREF)) |
1462 | 0 | BUG("delete cannot operate on symrefs with deref mode"); |
1463 | 0 | return ref_transaction_update(transaction, refname, |
1464 | 0 | null_oid(the_hash_algo), old_oid, |
1465 | 0 | NULL, old_target, flags, |
1466 | 0 | msg, err); |
1467 | 0 | } |
1468 | | |
1469 | | int ref_transaction_verify(struct ref_transaction *transaction, |
1470 | | const char *refname, |
1471 | | const struct object_id *old_oid, |
1472 | | const char *old_target, |
1473 | | unsigned int flags, |
1474 | | struct strbuf *err) |
1475 | 0 | { |
1476 | 0 | if (!old_target && !old_oid) |
1477 | 0 | BUG("verify called with old_oid and old_target set to NULL"); |
1478 | 0 | if (old_oid && old_target) |
1479 | 0 | BUG("verify called with both old_oid and old_target set"); |
1480 | 0 | if (old_target && !(flags & REF_NO_DEREF)) |
1481 | 0 | BUG("verify cannot operate on symrefs with deref mode"); |
1482 | 0 | return ref_transaction_update(transaction, refname, |
1483 | 0 | NULL, old_oid, |
1484 | 0 | NULL, old_target, |
1485 | 0 | flags, NULL, err); |
1486 | 0 | } |
1487 | | |
1488 | | int refs_update_ref(struct ref_store *refs, const char *msg, |
1489 | | const char *refname, const struct object_id *new_oid, |
1490 | | const struct object_id *old_oid, unsigned int flags, |
1491 | | enum action_on_err onerr) |
1492 | 0 | { |
1493 | 0 | struct ref_transaction *t = NULL; |
1494 | 0 | struct strbuf err = STRBUF_INIT; |
1495 | 0 | int ret = 0; |
1496 | |
|
1497 | 0 | t = ref_store_transaction_begin(refs, 0, &err); |
1498 | 0 | if (!t || |
1499 | 0 | ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL, |
1500 | 0 | flags, msg, &err) || |
1501 | 0 | ref_transaction_commit(t, &err)) { |
1502 | 0 | ret = 1; |
1503 | 0 | ref_transaction_free(t); |
1504 | 0 | } |
1505 | 0 | if (ret) { |
1506 | 0 | const char *str = _("update_ref failed for ref '%s': %s"); |
1507 | |
|
1508 | 0 | switch (onerr) { |
1509 | 0 | case UPDATE_REFS_MSG_ON_ERR: |
1510 | 0 | error(str, refname, err.buf); |
1511 | 0 | break; |
1512 | 0 | case UPDATE_REFS_DIE_ON_ERR: |
1513 | 0 | die(str, refname, err.buf); |
1514 | 0 | break; |
1515 | 0 | case UPDATE_REFS_QUIET_ON_ERR: |
1516 | 0 | break; |
1517 | 0 | } |
1518 | 0 | strbuf_release(&err); |
1519 | 0 | return 1; |
1520 | 0 | } |
1521 | 0 | strbuf_release(&err); |
1522 | 0 | if (t) |
1523 | 0 | ref_transaction_free(t); |
1524 | 0 | return 0; |
1525 | 0 | } |
1526 | | |
1527 | | /* |
1528 | | * Check that the string refname matches a rule of the form |
1529 | | * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule |
1530 | | * "foo/%.*s/baz", and return the string "bar". |
1531 | | */ |
1532 | | static const char *match_parse_rule(const char *refname, const char *rule, |
1533 | | size_t *len) |
1534 | 0 | { |
1535 | | /* |
1536 | | * Check that rule matches refname up to the first percent in the rule. |
1537 | | * We can bail immediately if not, but otherwise we leave "rule" at the |
1538 | | * %-placeholder, and "refname" at the start of the potential matched |
1539 | | * name. |
1540 | | */ |
1541 | 0 | while (*rule != '%') { |
1542 | 0 | if (!*rule) |
1543 | 0 | BUG("rev-parse rule did not have percent"); |
1544 | 0 | if (*refname++ != *rule++) |
1545 | 0 | return NULL; |
1546 | 0 | } |
1547 | | |
1548 | | /* |
1549 | | * Check that our "%" is the expected placeholder. This assumes there |
1550 | | * are no other percents (placeholder or quoted) in the string, but |
1551 | | * that is sufficient for our rev-parse rules. |
1552 | | */ |
1553 | 0 | if (!skip_prefix(rule, "%.*s", &rule)) |
1554 | 0 | return NULL; |
1555 | | |
1556 | | /* |
1557 | | * And now check that our suffix (if any) matches. |
1558 | | */ |
1559 | 0 | if (!strip_suffix(refname, rule, len)) |
1560 | 0 | return NULL; |
1561 | | |
1562 | 0 | return refname; /* len set by strip_suffix() */ |
1563 | 0 | } |
1564 | | |
1565 | | char *refs_shorten_unambiguous_ref(struct ref_store *refs, |
1566 | | const char *refname, int strict) |
1567 | 0 | { |
1568 | 0 | int i; |
1569 | 0 | struct strbuf resolved_buf = STRBUF_INIT; |
1570 | | |
1571 | | /* skip first rule, it will always match */ |
1572 | 0 | for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) { |
1573 | 0 | int j; |
1574 | 0 | int rules_to_fail = i; |
1575 | 0 | const char *short_name; |
1576 | 0 | size_t short_name_len; |
1577 | |
|
1578 | 0 | short_name = match_parse_rule(refname, ref_rev_parse_rules[i], |
1579 | 0 | &short_name_len); |
1580 | 0 | if (!short_name) |
1581 | 0 | continue; |
1582 | | |
1583 | | /* |
1584 | | * in strict mode, all (except the matched one) rules |
1585 | | * must fail to resolve to a valid non-ambiguous ref |
1586 | | */ |
1587 | 0 | if (strict) |
1588 | 0 | rules_to_fail = NUM_REV_PARSE_RULES; |
1589 | | |
1590 | | /* |
1591 | | * check if the short name resolves to a valid ref, |
1592 | | * but use only rules prior to the matched one |
1593 | | */ |
1594 | 0 | for (j = 0; j < rules_to_fail; j++) { |
1595 | 0 | const char *rule = ref_rev_parse_rules[j]; |
1596 | | |
1597 | | /* skip matched rule */ |
1598 | 0 | if (i == j) |
1599 | 0 | continue; |
1600 | | |
1601 | | /* |
1602 | | * the short name is ambiguous, if it resolves |
1603 | | * (with this previous rule) to a valid ref |
1604 | | * read_ref() returns 0 on success |
1605 | | */ |
1606 | 0 | strbuf_reset(&resolved_buf); |
1607 | 0 | strbuf_addf(&resolved_buf, rule, |
1608 | 0 | cast_size_t_to_int(short_name_len), |
1609 | 0 | short_name); |
1610 | 0 | if (refs_ref_exists(refs, resolved_buf.buf)) |
1611 | 0 | break; |
1612 | 0 | } |
1613 | | |
1614 | | /* |
1615 | | * short name is non-ambiguous if all previous rules |
1616 | | * haven't resolved to a valid ref |
1617 | | */ |
1618 | 0 | if (j == rules_to_fail) { |
1619 | 0 | strbuf_release(&resolved_buf); |
1620 | 0 | return xmemdupz(short_name, short_name_len); |
1621 | 0 | } |
1622 | 0 | } |
1623 | | |
1624 | 0 | strbuf_release(&resolved_buf); |
1625 | 0 | return xstrdup(refname); |
1626 | 0 | } |
1627 | | |
1628 | | int parse_hide_refs_config(const char *var, const char *value, const char *section, |
1629 | | struct strvec *hide_refs) |
1630 | 0 | { |
1631 | 0 | const char *key; |
1632 | 0 | if (!strcmp("transfer.hiderefs", var) || |
1633 | 0 | (!parse_config_key(var, section, NULL, NULL, &key) && |
1634 | 0 | !strcmp(key, "hiderefs"))) { |
1635 | 0 | char *ref; |
1636 | 0 | int len; |
1637 | |
|
1638 | 0 | if (!value) |
1639 | 0 | return config_error_nonbool(var); |
1640 | | |
1641 | | /* drop const to remove trailing '/' characters */ |
1642 | 0 | ref = (char *)strvec_push(hide_refs, value); |
1643 | 0 | len = strlen(ref); |
1644 | 0 | while (len && ref[len - 1] == '/') |
1645 | 0 | ref[--len] = '\0'; |
1646 | 0 | } |
1647 | 0 | return 0; |
1648 | 0 | } |
1649 | | |
1650 | | int ref_is_hidden(const char *refname, const char *refname_full, |
1651 | | const struct strvec *hide_refs) |
1652 | 0 | { |
1653 | 0 | int i; |
1654 | |
|
1655 | 0 | for (i = hide_refs->nr - 1; i >= 0; i--) { |
1656 | 0 | const char *match = hide_refs->v[i]; |
1657 | 0 | const char *subject; |
1658 | 0 | int neg = 0; |
1659 | 0 | const char *p; |
1660 | |
|
1661 | 0 | if (*match == '!') { |
1662 | 0 | neg = 1; |
1663 | 0 | match++; |
1664 | 0 | } |
1665 | |
|
1666 | 0 | if (*match == '^') { |
1667 | 0 | subject = refname_full; |
1668 | 0 | match++; |
1669 | 0 | } else { |
1670 | 0 | subject = refname; |
1671 | 0 | } |
1672 | | |
1673 | | /* refname can be NULL when namespaces are used. */ |
1674 | 0 | if (subject && |
1675 | 0 | skip_prefix(subject, match, &p) && |
1676 | 0 | (!*p || *p == '/')) |
1677 | 0 | return !neg; |
1678 | 0 | } |
1679 | 0 | return 0; |
1680 | 0 | } |
1681 | | |
1682 | | const char **hidden_refs_to_excludes(const struct strvec *hide_refs) |
1683 | 0 | { |
1684 | 0 | const char **pattern; |
1685 | 0 | for (pattern = hide_refs->v; *pattern; pattern++) { |
1686 | | /* |
1687 | | * We can't feed any excludes from hidden refs config |
1688 | | * sections, since later rules may override previous |
1689 | | * ones. For example, with rules "refs/foo" and |
1690 | | * "!refs/foo/bar", we should show "refs/foo/bar" (and |
1691 | | * everything underneath it), but the earlier exclusion |
1692 | | * would cause us to skip all of "refs/foo". We |
1693 | | * likewise don't implement the namespace stripping |
1694 | | * required for '^' rules. |
1695 | | * |
1696 | | * Both are possible to do, but complicated, so avoid |
1697 | | * populating the jump list at all if we see either of |
1698 | | * these patterns. |
1699 | | */ |
1700 | 0 | if (**pattern == '!' || **pattern == '^') |
1701 | 0 | return NULL; |
1702 | 0 | } |
1703 | 0 | return hide_refs->v; |
1704 | 0 | } |
1705 | | |
1706 | | const char **get_namespaced_exclude_patterns(const char **exclude_patterns, |
1707 | | const char *namespace, |
1708 | | struct strvec *out) |
1709 | 0 | { |
1710 | 0 | if (!namespace || !*namespace || !exclude_patterns || !*exclude_patterns) |
1711 | 0 | return exclude_patterns; |
1712 | | |
1713 | 0 | for (size_t i = 0; exclude_patterns[i]; i++) |
1714 | 0 | strvec_pushf(out, "%s%s", namespace, exclude_patterns[i]); |
1715 | |
|
1716 | 0 | return out->v; |
1717 | 0 | } |
1718 | | |
1719 | | const char *find_descendant_ref(const char *dirname, |
1720 | | const struct string_list *extras, |
1721 | | const struct string_list *skip) |
1722 | 0 | { |
1723 | 0 | if (!extras) |
1724 | 0 | return NULL; |
1725 | | |
1726 | | /* |
1727 | | * Look at the place where dirname would be inserted into |
1728 | | * extras. If there is an entry at that position that starts |
1729 | | * with dirname (remember, dirname includes the trailing |
1730 | | * slash) and is not in skip, then we have a conflict. |
1731 | | */ |
1732 | 0 | for (size_t pos = string_list_find_insert_index(extras, dirname, NULL); |
1733 | 0 | pos < extras->nr; pos++) { |
1734 | 0 | const char *extra_refname = extras->items[pos].string; |
1735 | |
|
1736 | 0 | if (!starts_with(extra_refname, dirname)) |
1737 | 0 | break; |
1738 | | |
1739 | 0 | if (!skip || !string_list_has_string(skip, extra_refname)) |
1740 | 0 | return extra_refname; |
1741 | 0 | } |
1742 | 0 | return NULL; |
1743 | 0 | } |
1744 | | |
1745 | | int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
1746 | 0 | { |
1747 | 0 | struct object_id oid; |
1748 | 0 | int flag; |
1749 | |
|
1750 | 0 | if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING, |
1751 | 0 | &oid, &flag)) { |
1752 | 0 | struct reference ref = { |
1753 | 0 | .name = "HEAD", |
1754 | 0 | .oid = &oid, |
1755 | 0 | .flags = flag, |
1756 | 0 | }; |
1757 | |
|
1758 | 0 | return fn(&ref, cb_data); |
1759 | 0 | } |
1760 | | |
1761 | 0 | return 0; |
1762 | 0 | } |
1763 | | |
1764 | | struct ref_iterator *refs_ref_iterator_begin( |
1765 | | struct ref_store *refs, |
1766 | | const char *prefix, |
1767 | | const char **exclude_patterns, |
1768 | | int trim, |
1769 | | enum do_for_each_ref_flags flags) |
1770 | 0 | { |
1771 | 0 | struct ref_iterator *iter; |
1772 | 0 | struct strvec normalized_exclude_patterns = STRVEC_INIT; |
1773 | |
|
1774 | 0 | if (exclude_patterns) { |
1775 | 0 | for (size_t i = 0; exclude_patterns[i]; i++) { |
1776 | 0 | const char *pattern = exclude_patterns[i]; |
1777 | 0 | size_t len = strlen(pattern); |
1778 | 0 | if (!len) |
1779 | 0 | continue; |
1780 | | |
1781 | 0 | if (pattern[len - 1] == '/') |
1782 | 0 | strvec_push(&normalized_exclude_patterns, pattern); |
1783 | 0 | else |
1784 | 0 | strvec_pushf(&normalized_exclude_patterns, "%s/", |
1785 | 0 | pattern); |
1786 | 0 | } |
1787 | |
|
1788 | 0 | exclude_patterns = normalized_exclude_patterns.v; |
1789 | 0 | } |
1790 | |
|
1791 | 0 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) { |
1792 | 0 | static int ref_paranoia = -1; |
1793 | |
|
1794 | 0 | if (ref_paranoia < 0) |
1795 | 0 | ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 1); |
1796 | 0 | if (ref_paranoia) { |
1797 | 0 | flags |= DO_FOR_EACH_INCLUDE_BROKEN; |
1798 | 0 | flags |= DO_FOR_EACH_OMIT_DANGLING_SYMREFS; |
1799 | 0 | } |
1800 | 0 | } |
1801 | |
|
1802 | 0 | iter = refs->be->iterator_begin(refs, prefix, exclude_patterns, flags); |
1803 | | /* |
1804 | | * `iterator_begin()` already takes care of prefix, but we |
1805 | | * might need to do some trimming: |
1806 | | */ |
1807 | 0 | if (trim) |
1808 | 0 | iter = prefix_ref_iterator_begin(iter, "", trim); |
1809 | |
|
1810 | 0 | strvec_clear(&normalized_exclude_patterns); |
1811 | |
|
1812 | 0 | return iter; |
1813 | 0 | } |
1814 | | |
1815 | | static int do_for_each_ref(struct ref_store *refs, const char *prefix, |
1816 | | const char **exclude_patterns, |
1817 | | each_ref_fn fn, int trim, |
1818 | | enum do_for_each_ref_flags flags, void *cb_data) |
1819 | 0 | { |
1820 | 0 | struct ref_iterator *iter; |
1821 | |
|
1822 | 0 | if (!refs) |
1823 | 0 | return 0; |
1824 | | |
1825 | 0 | iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, trim, |
1826 | 0 | flags); |
1827 | |
|
1828 | 0 | return do_for_each_ref_iterator(iter, fn, cb_data); |
1829 | 0 | } |
1830 | | |
1831 | | int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
1832 | 0 | { |
1833 | 0 | return do_for_each_ref(refs, "", NULL, fn, 0, 0, cb_data); |
1834 | 0 | } |
1835 | | |
1836 | | int refs_for_each_ref_in(struct ref_store *refs, const char *prefix, |
1837 | | each_ref_fn fn, void *cb_data) |
1838 | 0 | { |
1839 | 0 | return do_for_each_ref(refs, prefix, NULL, fn, strlen(prefix), 0, cb_data); |
1840 | 0 | } |
1841 | | |
1842 | | int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, |
1843 | | const char **exclude_patterns, |
1844 | | each_ref_fn fn, void *cb_data) |
1845 | 0 | { |
1846 | 0 | return do_for_each_ref(refs, prefix, exclude_patterns, fn, 0, 0, cb_data); |
1847 | 0 | } |
1848 | | |
1849 | | int refs_for_each_replace_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
1850 | 0 | { |
1851 | 0 | const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; |
1852 | 0 | return do_for_each_ref(refs, git_replace_ref_base, NULL, fn, |
1853 | 0 | strlen(git_replace_ref_base), |
1854 | 0 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); |
1855 | 0 | } |
1856 | | |
1857 | | int refs_for_each_namespaced_ref(struct ref_store *refs, |
1858 | | const char **exclude_patterns, |
1859 | | each_ref_fn fn, void *cb_data) |
1860 | 0 | { |
1861 | 0 | struct strvec namespaced_exclude_patterns = STRVEC_INIT; |
1862 | 0 | struct strbuf prefix = STRBUF_INIT; |
1863 | 0 | int ret; |
1864 | |
|
1865 | 0 | exclude_patterns = get_namespaced_exclude_patterns(exclude_patterns, |
1866 | 0 | get_git_namespace(), |
1867 | 0 | &namespaced_exclude_patterns); |
1868 | |
|
1869 | 0 | strbuf_addf(&prefix, "%srefs/", get_git_namespace()); |
1870 | 0 | ret = do_for_each_ref(refs, prefix.buf, exclude_patterns, fn, 0, 0, cb_data); |
1871 | |
|
1872 | 0 | strvec_clear(&namespaced_exclude_patterns); |
1873 | 0 | strbuf_release(&prefix); |
1874 | 0 | return ret; |
1875 | 0 | } |
1876 | | |
1877 | | int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
1878 | 0 | { |
1879 | 0 | return refs_for_each_rawref_in(refs, "", fn, cb_data); |
1880 | 0 | } |
1881 | | |
1882 | | int refs_for_each_rawref_in(struct ref_store *refs, const char *prefix, |
1883 | | each_ref_fn fn, void *cb_data) |
1884 | 0 | { |
1885 | 0 | return do_for_each_ref(refs, prefix, NULL, fn, 0, |
1886 | 0 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); |
1887 | 0 | } |
1888 | | |
1889 | | int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn, |
1890 | | void *cb_data) |
1891 | 0 | { |
1892 | 0 | return do_for_each_ref(refs, "", NULL, fn, 0, |
1893 | 0 | DO_FOR_EACH_INCLUDE_ROOT_REFS, cb_data); |
1894 | 0 | } |
1895 | | |
1896 | | static int qsort_strcmp(const void *va, const void *vb) |
1897 | 0 | { |
1898 | 0 | const char *a = *(const char **)va; |
1899 | 0 | const char *b = *(const char **)vb; |
1900 | |
|
1901 | 0 | return strcmp(a, b); |
1902 | 0 | } |
1903 | | |
1904 | | static void find_longest_prefixes_1(struct string_list *out, |
1905 | | struct strbuf *prefix, |
1906 | | const char **patterns, size_t nr) |
1907 | 0 | { |
1908 | 0 | size_t i; |
1909 | |
|
1910 | 0 | for (i = 0; i < nr; i++) { |
1911 | 0 | char c = patterns[i][prefix->len]; |
1912 | 0 | if (!c || is_glob_special(c)) { |
1913 | 0 | string_list_append(out, prefix->buf); |
1914 | 0 | return; |
1915 | 0 | } |
1916 | 0 | } |
1917 | | |
1918 | 0 | i = 0; |
1919 | 0 | while (i < nr) { |
1920 | 0 | size_t end; |
1921 | | |
1922 | | /* |
1923 | | * Set "end" to the index of the element _after_ the last one |
1924 | | * in our group. |
1925 | | */ |
1926 | 0 | for (end = i + 1; end < nr; end++) { |
1927 | 0 | if (patterns[i][prefix->len] != patterns[end][prefix->len]) |
1928 | 0 | break; |
1929 | 0 | } |
1930 | |
|
1931 | 0 | strbuf_addch(prefix, patterns[i][prefix->len]); |
1932 | 0 | find_longest_prefixes_1(out, prefix, patterns + i, end - i); |
1933 | 0 | strbuf_setlen(prefix, prefix->len - 1); |
1934 | |
|
1935 | 0 | i = end; |
1936 | 0 | } |
1937 | 0 | } |
1938 | | |
1939 | | static void find_longest_prefixes(struct string_list *out, |
1940 | | const char **patterns) |
1941 | 0 | { |
1942 | 0 | struct strvec sorted = STRVEC_INIT; |
1943 | 0 | struct strbuf prefix = STRBUF_INIT; |
1944 | |
|
1945 | 0 | strvec_pushv(&sorted, patterns); |
1946 | 0 | QSORT(sorted.v, sorted.nr, qsort_strcmp); |
1947 | |
|
1948 | 0 | find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr); |
1949 | |
|
1950 | 0 | strvec_clear(&sorted); |
1951 | 0 | strbuf_release(&prefix); |
1952 | 0 | } |
1953 | | |
1954 | | int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store, |
1955 | | const char *namespace, |
1956 | | const char **patterns, |
1957 | | const char **exclude_patterns, |
1958 | | each_ref_fn fn, void *cb_data) |
1959 | 0 | { |
1960 | 0 | struct strvec namespaced_exclude_patterns = STRVEC_INIT; |
1961 | 0 | struct string_list prefixes = STRING_LIST_INIT_DUP; |
1962 | 0 | struct string_list_item *prefix; |
1963 | 0 | struct strbuf buf = STRBUF_INIT; |
1964 | 0 | int ret = 0, namespace_len; |
1965 | |
|
1966 | 0 | find_longest_prefixes(&prefixes, patterns); |
1967 | |
|
1968 | 0 | if (namespace) |
1969 | 0 | strbuf_addstr(&buf, namespace); |
1970 | 0 | namespace_len = buf.len; |
1971 | |
|
1972 | 0 | exclude_patterns = get_namespaced_exclude_patterns(exclude_patterns, |
1973 | 0 | namespace, |
1974 | 0 | &namespaced_exclude_patterns); |
1975 | |
|
1976 | 0 | for_each_string_list_item(prefix, &prefixes) { |
1977 | 0 | strbuf_addstr(&buf, prefix->string); |
1978 | 0 | ret = refs_for_each_fullref_in(ref_store, buf.buf, |
1979 | 0 | exclude_patterns, fn, cb_data); |
1980 | 0 | if (ret) |
1981 | 0 | break; |
1982 | 0 | strbuf_setlen(&buf, namespace_len); |
1983 | 0 | } |
1984 | |
|
1985 | 0 | strvec_clear(&namespaced_exclude_patterns); |
1986 | 0 | string_list_clear(&prefixes, 0); |
1987 | 0 | strbuf_release(&buf); |
1988 | 0 | return ret; |
1989 | 0 | } |
1990 | | |
1991 | | static int refs_read_special_head(struct ref_store *ref_store, |
1992 | | const char *refname, struct object_id *oid, |
1993 | | struct strbuf *referent, unsigned int *type, |
1994 | | int *failure_errno) |
1995 | 0 | { |
1996 | 0 | struct strbuf full_path = STRBUF_INIT; |
1997 | 0 | struct strbuf content = STRBUF_INIT; |
1998 | 0 | int result = -1; |
1999 | 0 | strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname); |
2000 | |
|
2001 | 0 | if (strbuf_read_file(&content, full_path.buf, 0) < 0) { |
2002 | 0 | *failure_errno = errno; |
2003 | 0 | goto done; |
2004 | 0 | } |
2005 | | |
2006 | 0 | result = parse_loose_ref_contents(ref_store->repo->hash_algo, content.buf, |
2007 | 0 | oid, referent, type, NULL, failure_errno); |
2008 | |
|
2009 | 0 | done: |
2010 | 0 | strbuf_release(&full_path); |
2011 | 0 | strbuf_release(&content); |
2012 | 0 | return result; |
2013 | 0 | } |
2014 | | |
2015 | | int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, |
2016 | | struct object_id *oid, struct strbuf *referent, |
2017 | | unsigned int *type, int *failure_errno) |
2018 | 0 | { |
2019 | 0 | assert(failure_errno); |
2020 | 0 | if (is_pseudo_ref(refname)) |
2021 | 0 | return refs_read_special_head(ref_store, refname, oid, referent, |
2022 | 0 | type, failure_errno); |
2023 | | |
2024 | 0 | return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, |
2025 | 0 | type, failure_errno); |
2026 | 0 | } |
2027 | | |
2028 | | int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname, |
2029 | | struct strbuf *referent) |
2030 | 0 | { |
2031 | 0 | return ref_store->be->read_symbolic_ref(ref_store, refname, referent); |
2032 | 0 | } |
2033 | | |
2034 | | const char *refs_resolve_ref_unsafe(struct ref_store *refs, |
2035 | | const char *refname, |
2036 | | int resolve_flags, |
2037 | | struct object_id *oid, |
2038 | | int *flags) |
2039 | 0 | { |
2040 | 0 | static struct strbuf sb_refname = STRBUF_INIT; |
2041 | 0 | struct object_id unused_oid; |
2042 | 0 | int unused_flags; |
2043 | 0 | int symref_count; |
2044 | |
|
2045 | 0 | if (!oid) |
2046 | 0 | oid = &unused_oid; |
2047 | 0 | if (!flags) |
2048 | 0 | flags = &unused_flags; |
2049 | |
|
2050 | 0 | *flags = 0; |
2051 | |
|
2052 | 0 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
2053 | 0 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || |
2054 | 0 | !refname_is_safe(refname)) |
2055 | 0 | return NULL; |
2056 | | |
2057 | | /* |
2058 | | * repo_dwim_ref() uses REF_ISBROKEN to distinguish between |
2059 | | * missing refs and refs that were present but invalid, |
2060 | | * to complain about the latter to stderr. |
2061 | | * |
2062 | | * We don't know whether the ref exists, so don't set |
2063 | | * REF_ISBROKEN yet. |
2064 | | */ |
2065 | 0 | *flags |= REF_BAD_NAME; |
2066 | 0 | } |
2067 | | |
2068 | 0 | for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) { |
2069 | 0 | unsigned int read_flags = 0; |
2070 | 0 | int failure_errno; |
2071 | |
|
2072 | 0 | if (refs_read_raw_ref(refs, refname, oid, &sb_refname, |
2073 | 0 | &read_flags, &failure_errno)) { |
2074 | 0 | *flags |= read_flags; |
2075 | | |
2076 | | /* In reading mode, refs must eventually resolve */ |
2077 | 0 | if (resolve_flags & RESOLVE_REF_READING) |
2078 | 0 | return NULL; |
2079 | | |
2080 | | /* |
2081 | | * Otherwise a missing ref is OK. But the files backend |
2082 | | * may show errors besides ENOENT if there are |
2083 | | * similarly-named refs. |
2084 | | */ |
2085 | 0 | if (failure_errno != ENOENT && |
2086 | 0 | failure_errno != EISDIR && |
2087 | 0 | failure_errno != ENOTDIR) |
2088 | 0 | return NULL; |
2089 | | |
2090 | 0 | oidclr(oid, refs->repo->hash_algo); |
2091 | 0 | if (*flags & REF_BAD_NAME) |
2092 | 0 | *flags |= REF_ISBROKEN; |
2093 | 0 | return refname; |
2094 | 0 | } |
2095 | | |
2096 | 0 | *flags |= read_flags; |
2097 | |
|
2098 | 0 | if (!(read_flags & REF_ISSYMREF)) { |
2099 | 0 | if (*flags & REF_BAD_NAME) { |
2100 | 0 | oidclr(oid, refs->repo->hash_algo); |
2101 | 0 | *flags |= REF_ISBROKEN; |
2102 | 0 | } |
2103 | 0 | return refname; |
2104 | 0 | } |
2105 | | |
2106 | 0 | refname = sb_refname.buf; |
2107 | 0 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { |
2108 | 0 | oidclr(oid, refs->repo->hash_algo); |
2109 | 0 | return refname; |
2110 | 0 | } |
2111 | 0 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
2112 | 0 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || |
2113 | 0 | !refname_is_safe(refname)) |
2114 | 0 | return NULL; |
2115 | | |
2116 | 0 | *flags |= REF_ISBROKEN | REF_BAD_NAME; |
2117 | 0 | } |
2118 | 0 | } |
2119 | | |
2120 | 0 | return NULL; |
2121 | 0 | } |
2122 | | |
2123 | | /* backend functions */ |
2124 | | int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err) |
2125 | 0 | { |
2126 | 0 | return refs->be->create_on_disk(refs, flags, err); |
2127 | 0 | } |
2128 | | |
2129 | | int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err) |
2130 | 0 | { |
2131 | 0 | return refs->be->remove_on_disk(refs, err); |
2132 | 0 | } |
2133 | | |
2134 | | int repo_resolve_gitlink_ref(struct repository *r, |
2135 | | const char *submodule, const char *refname, |
2136 | | struct object_id *oid) |
2137 | 0 | { |
2138 | 0 | struct ref_store *refs; |
2139 | 0 | int flags; |
2140 | |
|
2141 | 0 | refs = repo_get_submodule_ref_store(r, submodule); |
2142 | 0 | if (!refs) |
2143 | 0 | return -1; |
2144 | | |
2145 | 0 | if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) || |
2146 | 0 | is_null_oid(oid)) |
2147 | 0 | return -1; |
2148 | 0 | return 0; |
2149 | 0 | } |
2150 | | |
2151 | | /* |
2152 | | * Look up a ref store by name. If that ref_store hasn't been |
2153 | | * registered yet, return NULL. |
2154 | | */ |
2155 | | static struct ref_store *lookup_ref_store_map(struct strmap *map, |
2156 | | const char *name) |
2157 | 0 | { |
2158 | 0 | struct strmap_entry *entry; |
2159 | |
|
2160 | 0 | if (!map->map.tablesize) |
2161 | | /* It's initialized on demand in register_ref_store(). */ |
2162 | 0 | return NULL; |
2163 | | |
2164 | 0 | entry = strmap_get_entry(map, name); |
2165 | 0 | return entry ? entry->value : NULL; |
2166 | 0 | } |
2167 | | |
2168 | | /* |
2169 | | * Create, record, and return a ref_store instance for the specified |
2170 | | * gitdir using the given ref storage format. |
2171 | | */ |
2172 | | static struct ref_store *ref_store_init(struct repository *repo, |
2173 | | enum ref_storage_format format, |
2174 | | const char *gitdir, |
2175 | | unsigned int flags) |
2176 | 0 | { |
2177 | 0 | const struct ref_storage_be *be; |
2178 | 0 | struct ref_store *refs; |
2179 | |
|
2180 | 0 | be = find_ref_storage_backend(format); |
2181 | 0 | if (!be) |
2182 | 0 | BUG("reference backend is unknown"); |
2183 | | |
2184 | 0 | refs = be->init(repo, gitdir, flags); |
2185 | 0 | return refs; |
2186 | 0 | } |
2187 | | |
2188 | | void ref_store_release(struct ref_store *ref_store) |
2189 | 0 | { |
2190 | 0 | ref_store->be->release(ref_store); |
2191 | 0 | free(ref_store->gitdir); |
2192 | 0 | } |
2193 | | |
2194 | | struct ref_store *get_main_ref_store(struct repository *r) |
2195 | 0 | { |
2196 | 0 | if (r->refs_private) |
2197 | 0 | return r->refs_private; |
2198 | | |
2199 | 0 | if (!r->gitdir) |
2200 | 0 | BUG("attempting to get main_ref_store outside of repository"); |
2201 | | |
2202 | 0 | r->refs_private = ref_store_init(r, r->ref_storage_format, |
2203 | 0 | r->gitdir, REF_STORE_ALL_CAPS); |
2204 | 0 | r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private); |
2205 | 0 | return r->refs_private; |
2206 | 0 | } |
2207 | | |
2208 | | /* |
2209 | | * Associate a ref store with a name. It is a fatal error to call this |
2210 | | * function twice for the same name. |
2211 | | */ |
2212 | | static void register_ref_store_map(struct strmap *map, |
2213 | | const char *type, |
2214 | | struct ref_store *refs, |
2215 | | const char *name) |
2216 | 0 | { |
2217 | 0 | if (!map->map.tablesize) |
2218 | 0 | strmap_init(map); |
2219 | 0 | if (strmap_put(map, name, refs)) |
2220 | 0 | BUG("%s ref_store '%s' initialized twice", type, name); |
2221 | 0 | } |
2222 | | |
2223 | | struct ref_store *repo_get_submodule_ref_store(struct repository *repo, |
2224 | | const char *submodule) |
2225 | 0 | { |
2226 | 0 | struct strbuf submodule_sb = STRBUF_INIT; |
2227 | 0 | struct ref_store *refs; |
2228 | 0 | char *to_free = NULL; |
2229 | 0 | size_t len; |
2230 | 0 | struct repository *subrepo; |
2231 | |
|
2232 | 0 | if (!submodule) |
2233 | 0 | return NULL; |
2234 | | |
2235 | 0 | len = strlen(submodule); |
2236 | 0 | while (len && is_dir_sep(submodule[len - 1])) |
2237 | 0 | len--; |
2238 | 0 | if (!len) |
2239 | 0 | return NULL; |
2240 | | |
2241 | 0 | if (submodule[len]) |
2242 | | /* We need to strip off one or more trailing slashes */ |
2243 | 0 | submodule = to_free = xmemdupz(submodule, len); |
2244 | |
|
2245 | 0 | refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule); |
2246 | 0 | if (refs) |
2247 | 0 | goto done; |
2248 | | |
2249 | 0 | strbuf_addstr(&submodule_sb, submodule); |
2250 | 0 | if (!is_nonbare_repository_dir(&submodule_sb)) |
2251 | 0 | goto done; |
2252 | | |
2253 | 0 | if (submodule_to_gitdir(repo, &submodule_sb, submodule)) |
2254 | 0 | goto done; |
2255 | | |
2256 | 0 | subrepo = xmalloc(sizeof(*subrepo)); |
2257 | |
|
2258 | 0 | if (repo_submodule_init(subrepo, repo, submodule, |
2259 | 0 | null_oid(the_hash_algo))) { |
2260 | 0 | free(subrepo); |
2261 | 0 | goto done; |
2262 | 0 | } |
2263 | 0 | refs = ref_store_init(subrepo, subrepo->ref_storage_format, |
2264 | 0 | submodule_sb.buf, |
2265 | 0 | REF_STORE_READ | REF_STORE_ODB); |
2266 | 0 | register_ref_store_map(&repo->submodule_ref_stores, "submodule", |
2267 | 0 | refs, submodule); |
2268 | |
|
2269 | 0 | done: |
2270 | 0 | strbuf_release(&submodule_sb); |
2271 | 0 | free(to_free); |
2272 | |
|
2273 | 0 | return refs; |
2274 | 0 | } |
2275 | | |
2276 | | struct ref_store *get_worktree_ref_store(const struct worktree *wt) |
2277 | 0 | { |
2278 | 0 | struct ref_store *refs; |
2279 | 0 | const char *id; |
2280 | |
|
2281 | 0 | if (wt->is_current) |
2282 | 0 | return get_main_ref_store(wt->repo); |
2283 | | |
2284 | 0 | id = wt->id ? wt->id : "/"; |
2285 | 0 | refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id); |
2286 | 0 | if (refs) |
2287 | 0 | return refs; |
2288 | | |
2289 | 0 | if (wt->id) { |
2290 | 0 | struct strbuf common_path = STRBUF_INIT; |
2291 | 0 | repo_common_path_append(wt->repo, &common_path, |
2292 | 0 | "worktrees/%s", wt->id); |
2293 | 0 | refs = ref_store_init(wt->repo, wt->repo->ref_storage_format, |
2294 | 0 | common_path.buf, REF_STORE_ALL_CAPS); |
2295 | 0 | strbuf_release(&common_path); |
2296 | 0 | } else { |
2297 | 0 | refs = ref_store_init(wt->repo, wt->repo->ref_storage_format, |
2298 | 0 | wt->repo->commondir, REF_STORE_ALL_CAPS); |
2299 | 0 | } |
2300 | |
|
2301 | 0 | if (refs) |
2302 | 0 | register_ref_store_map(&wt->repo->worktree_ref_stores, |
2303 | 0 | "worktree", refs, id); |
2304 | |
|
2305 | 0 | return refs; |
2306 | 0 | } |
2307 | | |
2308 | | void base_ref_store_init(struct ref_store *refs, struct repository *repo, |
2309 | | const char *path, const struct ref_storage_be *be) |
2310 | 0 | { |
2311 | 0 | refs->be = be; |
2312 | 0 | refs->repo = repo; |
2313 | 0 | refs->gitdir = xstrdup(path); |
2314 | 0 | } |
2315 | | |
2316 | | int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts) |
2317 | 0 | { |
2318 | 0 | return refs->be->optimize(refs, opts); |
2319 | 0 | } |
2320 | | |
2321 | | int refs_optimize_required(struct ref_store *refs, |
2322 | | struct refs_optimize_opts *opts, |
2323 | | bool *required) |
2324 | 0 | { |
2325 | 0 | return refs->be->optimize_required(refs, opts, required); |
2326 | 0 | } |
2327 | | |
2328 | | int reference_get_peeled_oid(struct repository *repo, |
2329 | | const struct reference *ref, |
2330 | | struct object_id *peeled_oid) |
2331 | 0 | { |
2332 | 0 | if (ref->peeled_oid) { |
2333 | 0 | oidcpy(peeled_oid, ref->peeled_oid); |
2334 | 0 | return 0; |
2335 | 0 | } |
2336 | | |
2337 | 0 | return peel_object(repo, ref->oid, peeled_oid, 0) ? -1 : 0; |
2338 | 0 | } |
2339 | | |
2340 | | int refs_update_symref(struct ref_store *refs, const char *ref, |
2341 | | const char *target, const char *logmsg) |
2342 | 0 | { |
2343 | 0 | return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0); |
2344 | 0 | } |
2345 | | |
2346 | | int refs_update_symref_extended(struct ref_store *refs, const char *ref, |
2347 | | const char *target, const char *logmsg, |
2348 | | struct strbuf *referent, int create_only) |
2349 | 0 | { |
2350 | 0 | struct ref_transaction *transaction; |
2351 | 0 | struct strbuf err = STRBUF_INIT; |
2352 | 0 | int ret = 0, prepret = 0; |
2353 | |
|
2354 | 0 | transaction = ref_store_transaction_begin(refs, 0, &err); |
2355 | 0 | if (!transaction) { |
2356 | 0 | error_return: |
2357 | 0 | ret = error("%s", err.buf); |
2358 | 0 | goto cleanup; |
2359 | 0 | } |
2360 | 0 | if (create_only) { |
2361 | 0 | if (ref_transaction_create(transaction, ref, NULL, target, |
2362 | 0 | REF_NO_DEREF, logmsg, &err)) |
2363 | 0 | goto error_return; |
2364 | 0 | prepret = ref_transaction_prepare(transaction, &err); |
2365 | 0 | if (prepret && prepret != REF_TRANSACTION_ERROR_CREATE_EXISTS) |
2366 | 0 | goto error_return; |
2367 | 0 | } else { |
2368 | 0 | if (ref_transaction_update(transaction, ref, NULL, NULL, |
2369 | 0 | target, NULL, REF_NO_DEREF, |
2370 | 0 | logmsg, &err) || |
2371 | 0 | ref_transaction_prepare(transaction, &err)) |
2372 | 0 | goto error_return; |
2373 | 0 | } |
2374 | | |
2375 | 0 | if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) { |
2376 | 0 | struct object_id oid; |
2377 | 0 | if (!refs_read_ref(refs, ref, &oid)) { |
2378 | 0 | strbuf_addstr(referent, oid_to_hex(&oid)); |
2379 | 0 | ret = NOT_A_SYMREF; |
2380 | 0 | } |
2381 | 0 | } |
2382 | |
|
2383 | 0 | if (prepret == REF_TRANSACTION_ERROR_CREATE_EXISTS) |
2384 | 0 | goto cleanup; |
2385 | | |
2386 | 0 | if (ref_transaction_commit(transaction, &err)) |
2387 | 0 | goto error_return; |
2388 | | |
2389 | 0 | cleanup: |
2390 | 0 | strbuf_release(&err); |
2391 | 0 | if (transaction) |
2392 | 0 | ref_transaction_free(transaction); |
2393 | |
|
2394 | 0 | return ret; |
2395 | 0 | } |
2396 | | |
2397 | | /* |
2398 | | * Write an error to `err` and return a nonzero value iff the same |
2399 | | * refname appears multiple times in `refnames`. `refnames` must be |
2400 | | * sorted on entry to this function. |
2401 | | */ |
2402 | | static int ref_update_reject_duplicates(struct string_list *refnames, |
2403 | | struct strbuf *err) |
2404 | 0 | { |
2405 | 0 | size_t i, n = refnames->nr; |
2406 | |
|
2407 | 0 | assert(err); |
2408 | |
|
2409 | 0 | for (i = 1; i < n; i++) { |
2410 | 0 | int cmp = strcmp(refnames->items[i - 1].string, |
2411 | 0 | refnames->items[i].string); |
2412 | |
|
2413 | 0 | if (!cmp) { |
2414 | 0 | strbuf_addf(err, |
2415 | 0 | _("multiple updates for ref '%s' not allowed"), |
2416 | 0 | refnames->items[i].string); |
2417 | 0 | return 1; |
2418 | 0 | } else if (cmp > 0) { |
2419 | 0 | BUG("ref_update_reject_duplicates() received unsorted list"); |
2420 | 0 | } |
2421 | 0 | } |
2422 | 0 | return 0; |
2423 | 0 | } |
2424 | | |
2425 | | static int run_transaction_hook(struct ref_transaction *transaction, |
2426 | | const char *state) |
2427 | 0 | { |
2428 | 0 | struct child_process proc = CHILD_PROCESS_INIT; |
2429 | 0 | struct strbuf buf = STRBUF_INIT; |
2430 | 0 | const char *hook; |
2431 | 0 | int ret = 0; |
2432 | |
|
2433 | 0 | hook = find_hook(transaction->ref_store->repo, "reference-transaction"); |
2434 | 0 | if (!hook) |
2435 | 0 | return ret; |
2436 | | |
2437 | 0 | strvec_pushl(&proc.args, hook, state, NULL); |
2438 | 0 | proc.in = -1; |
2439 | 0 | proc.stdout_to_stderr = 1; |
2440 | 0 | proc.trace2_hook_name = "reference-transaction"; |
2441 | |
|
2442 | 0 | ret = start_command(&proc); |
2443 | 0 | if (ret) |
2444 | 0 | return ret; |
2445 | | |
2446 | 0 | sigchain_push(SIGPIPE, SIG_IGN); |
2447 | |
|
2448 | 0 | for (size_t i = 0; i < transaction->nr; i++) { |
2449 | 0 | struct ref_update *update = transaction->updates[i]; |
2450 | |
|
2451 | 0 | if (update->flags & REF_LOG_ONLY) |
2452 | 0 | continue; |
2453 | | |
2454 | 0 | strbuf_reset(&buf); |
2455 | |
|
2456 | 0 | if (!(update->flags & REF_HAVE_OLD)) |
2457 | 0 | strbuf_addf(&buf, "%s ", oid_to_hex(null_oid(the_hash_algo))); |
2458 | 0 | else if (update->old_target) |
2459 | 0 | strbuf_addf(&buf, "ref:%s ", update->old_target); |
2460 | 0 | else |
2461 | 0 | strbuf_addf(&buf, "%s ", oid_to_hex(&update->old_oid)); |
2462 | |
|
2463 | 0 | if (!(update->flags & REF_HAVE_NEW)) |
2464 | 0 | strbuf_addf(&buf, "%s ", oid_to_hex(null_oid(the_hash_algo))); |
2465 | 0 | else if (update->new_target) |
2466 | 0 | strbuf_addf(&buf, "ref:%s ", update->new_target); |
2467 | 0 | else |
2468 | 0 | strbuf_addf(&buf, "%s ", oid_to_hex(&update->new_oid)); |
2469 | |
|
2470 | 0 | strbuf_addf(&buf, "%s\n", update->refname); |
2471 | |
|
2472 | 0 | if (write_in_full(proc.in, buf.buf, buf.len) < 0) { |
2473 | 0 | if (errno != EPIPE) { |
2474 | | /* Don't leak errno outside this API */ |
2475 | 0 | errno = 0; |
2476 | 0 | ret = -1; |
2477 | 0 | } |
2478 | 0 | break; |
2479 | 0 | } |
2480 | 0 | } |
2481 | |
|
2482 | 0 | close(proc.in); |
2483 | 0 | sigchain_pop(SIGPIPE); |
2484 | 0 | strbuf_release(&buf); |
2485 | |
|
2486 | 0 | ret |= finish_command(&proc); |
2487 | 0 | return ret; |
2488 | 0 | } |
2489 | | |
2490 | | int ref_transaction_prepare(struct ref_transaction *transaction, |
2491 | | struct strbuf *err) |
2492 | 0 | { |
2493 | 0 | struct ref_store *refs = transaction->ref_store; |
2494 | 0 | int ret; |
2495 | |
|
2496 | 0 | switch (transaction->state) { |
2497 | 0 | case REF_TRANSACTION_OPEN: |
2498 | | /* Good. */ |
2499 | 0 | break; |
2500 | 0 | case REF_TRANSACTION_PREPARED: |
2501 | 0 | BUG("prepare called twice on reference transaction"); |
2502 | 0 | break; |
2503 | 0 | case REF_TRANSACTION_CLOSED: |
2504 | 0 | BUG("prepare called on a closed reference transaction"); |
2505 | 0 | break; |
2506 | 0 | default: |
2507 | 0 | BUG("unexpected reference transaction state"); |
2508 | 0 | break; |
2509 | 0 | } |
2510 | | |
2511 | 0 | if (refs->repo->disable_ref_updates) { |
2512 | 0 | strbuf_addstr(err, |
2513 | 0 | _("ref updates forbidden inside quarantine environment")); |
2514 | 0 | return -1; |
2515 | 0 | } |
2516 | | |
2517 | 0 | string_list_sort(&transaction->refnames); |
2518 | 0 | if (ref_update_reject_duplicates(&transaction->refnames, err)) |
2519 | 0 | return REF_TRANSACTION_ERROR_GENERIC; |
2520 | | |
2521 | 0 | ret = refs->be->transaction_prepare(refs, transaction, err); |
2522 | 0 | if (ret) |
2523 | 0 | return ret; |
2524 | | |
2525 | 0 | ret = run_transaction_hook(transaction, "prepared"); |
2526 | 0 | if (ret) { |
2527 | 0 | ref_transaction_abort(transaction, err); |
2528 | 0 | die(_("ref updates aborted by hook")); |
2529 | 0 | } |
2530 | | |
2531 | 0 | return 0; |
2532 | 0 | } |
2533 | | |
2534 | | int ref_transaction_abort(struct ref_transaction *transaction, |
2535 | | struct strbuf *err) |
2536 | 0 | { |
2537 | 0 | struct ref_store *refs = transaction->ref_store; |
2538 | 0 | int ret = 0; |
2539 | |
|
2540 | 0 | switch (transaction->state) { |
2541 | 0 | case REF_TRANSACTION_OPEN: |
2542 | | /* No need to abort explicitly. */ |
2543 | 0 | break; |
2544 | 0 | case REF_TRANSACTION_PREPARED: |
2545 | 0 | ret = refs->be->transaction_abort(refs, transaction, err); |
2546 | 0 | break; |
2547 | 0 | case REF_TRANSACTION_CLOSED: |
2548 | 0 | BUG("abort called on a closed reference transaction"); |
2549 | 0 | break; |
2550 | 0 | default: |
2551 | 0 | BUG("unexpected reference transaction state"); |
2552 | 0 | break; |
2553 | 0 | } |
2554 | | |
2555 | 0 | run_transaction_hook(transaction, "aborted"); |
2556 | |
|
2557 | 0 | ref_transaction_free(transaction); |
2558 | 0 | return ret; |
2559 | 0 | } |
2560 | | |
2561 | | int ref_transaction_commit(struct ref_transaction *transaction, |
2562 | | struct strbuf *err) |
2563 | 0 | { |
2564 | 0 | struct ref_store *refs = transaction->ref_store; |
2565 | 0 | int ret; |
2566 | |
|
2567 | 0 | switch (transaction->state) { |
2568 | 0 | case REF_TRANSACTION_OPEN: |
2569 | | /* Need to prepare first. */ |
2570 | 0 | ret = ref_transaction_prepare(transaction, err); |
2571 | 0 | if (ret) |
2572 | 0 | return ret; |
2573 | 0 | break; |
2574 | 0 | case REF_TRANSACTION_PREPARED: |
2575 | | /* Fall through to finish. */ |
2576 | 0 | break; |
2577 | 0 | case REF_TRANSACTION_CLOSED: |
2578 | 0 | BUG("commit called on a closed reference transaction"); |
2579 | 0 | break; |
2580 | 0 | default: |
2581 | 0 | BUG("unexpected reference transaction state"); |
2582 | 0 | break; |
2583 | 0 | } |
2584 | | |
2585 | 0 | ret = refs->be->transaction_finish(refs, transaction, err); |
2586 | 0 | if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL)) |
2587 | 0 | run_transaction_hook(transaction, "committed"); |
2588 | 0 | return ret; |
2589 | 0 | } |
2590 | | |
2591 | | enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs, |
2592 | | const struct string_list *refnames, |
2593 | | const struct string_list *extras, |
2594 | | const struct string_list *skip, |
2595 | | struct ref_transaction *transaction, |
2596 | | unsigned int initial_transaction, |
2597 | | struct strbuf *err) |
2598 | 0 | { |
2599 | 0 | struct strbuf dirname = STRBUF_INIT; |
2600 | 0 | struct strbuf referent = STRBUF_INIT; |
2601 | 0 | struct string_list_item *item; |
2602 | 0 | struct ref_iterator *iter = NULL; |
2603 | 0 | struct strset conflicting_dirnames; |
2604 | 0 | struct strset dirnames; |
2605 | 0 | int ret = REF_TRANSACTION_ERROR_NAME_CONFLICT; |
2606 | | |
2607 | | /* |
2608 | | * For the sake of comments in this function, suppose that |
2609 | | * refname is "refs/foo/bar". |
2610 | | */ |
2611 | |
|
2612 | 0 | assert(err); |
2613 | |
|
2614 | 0 | strset_init(&conflicting_dirnames); |
2615 | 0 | strset_init(&dirnames); |
2616 | |
|
2617 | 0 | for_each_string_list_item(item, refnames) { |
2618 | 0 | const size_t *update_idx = (size_t *)item->util; |
2619 | 0 | const char *refname = item->string; |
2620 | 0 | const char *extra_refname; |
2621 | 0 | struct object_id oid; |
2622 | 0 | unsigned int type; |
2623 | 0 | const char *slash; |
2624 | |
|
2625 | 0 | strbuf_reset(&dirname); |
2626 | |
|
2627 | 0 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { |
2628 | | /* |
2629 | | * Just saying "Is a directory" when we e.g. can't |
2630 | | * lock some multi-level ref isn't very informative, |
2631 | | * the user won't be told *what* is a directory, so |
2632 | | * let's not use strerror() below. |
2633 | | */ |
2634 | 0 | int ignore_errno; |
2635 | | |
2636 | | /* Expand dirname to the new prefix, not including the trailing slash: */ |
2637 | 0 | strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len); |
2638 | | |
2639 | | /* |
2640 | | * We are still at a leading dir of the refname (e.g., |
2641 | | * "refs/foo"; if there is a reference with that name, |
2642 | | * it is a conflict, *unless* it is in skip. |
2643 | | */ |
2644 | 0 | if (skip && string_list_has_string(skip, dirname.buf)) |
2645 | 0 | continue; |
2646 | | |
2647 | | /* |
2648 | | * If we've already seen the directory we don't need to |
2649 | | * process it again. Skip it to avoid checking common |
2650 | | * prefixes like "refs/heads/" repeatedly. |
2651 | | */ |
2652 | 0 | if (!strset_add(&dirnames, dirname.buf)) |
2653 | 0 | continue; |
2654 | | |
2655 | 0 | if (!initial_transaction && |
2656 | 0 | (strset_contains(&conflicting_dirnames, dirname.buf) || |
2657 | 0 | !refs_read_raw_ref(refs, dirname.buf, &oid, &referent, |
2658 | 0 | &type, &ignore_errno))) { |
2659 | 0 | if (transaction && ref_transaction_maybe_set_rejected( |
2660 | 0 | transaction, *update_idx, |
2661 | 0 | REF_TRANSACTION_ERROR_NAME_CONFLICT)) { |
2662 | 0 | strset_remove(&dirnames, dirname.buf); |
2663 | 0 | strset_add(&conflicting_dirnames, dirname.buf); |
2664 | 0 | continue; |
2665 | 0 | } |
2666 | | |
2667 | 0 | strbuf_addf(err, _("'%s' exists; cannot create '%s'"), |
2668 | 0 | dirname.buf, refname); |
2669 | 0 | goto cleanup; |
2670 | 0 | } |
2671 | | |
2672 | 0 | if (extras && string_list_has_string(extras, dirname.buf)) { |
2673 | 0 | if (transaction && ref_transaction_maybe_set_rejected( |
2674 | 0 | transaction, *update_idx, |
2675 | 0 | REF_TRANSACTION_ERROR_NAME_CONFLICT)) { |
2676 | 0 | strset_remove(&dirnames, dirname.buf); |
2677 | 0 | continue; |
2678 | 0 | } |
2679 | | |
2680 | 0 | strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"), |
2681 | 0 | refname, dirname.buf); |
2682 | 0 | goto cleanup; |
2683 | 0 | } |
2684 | 0 | } |
2685 | | |
2686 | | /* |
2687 | | * We are at the leaf of our refname (e.g., "refs/foo/bar"). |
2688 | | * There is no point in searching for a reference with that |
2689 | | * name, because a refname isn't considered to conflict with |
2690 | | * itself. But we still need to check for references whose |
2691 | | * names are in the "refs/foo/bar/" namespace, because they |
2692 | | * *do* conflict. |
2693 | | */ |
2694 | 0 | strbuf_addstr(&dirname, refname + dirname.len); |
2695 | 0 | strbuf_addch(&dirname, '/'); |
2696 | |
|
2697 | 0 | if (!initial_transaction) { |
2698 | 0 | int ok; |
2699 | |
|
2700 | 0 | if (!iter) |
2701 | 0 | iter = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0, |
2702 | 0 | DO_FOR_EACH_INCLUDE_BROKEN); |
2703 | 0 | else if (ref_iterator_seek(iter, dirname.buf, |
2704 | 0 | REF_ITERATOR_SEEK_SET_PREFIX) < 0) |
2705 | 0 | goto cleanup; |
2706 | | |
2707 | 0 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { |
2708 | 0 | if (skip && |
2709 | 0 | string_list_has_string(skip, iter->ref.name)) |
2710 | 0 | continue; |
2711 | | |
2712 | 0 | if (transaction && ref_transaction_maybe_set_rejected( |
2713 | 0 | transaction, *update_idx, |
2714 | 0 | REF_TRANSACTION_ERROR_NAME_CONFLICT)) |
2715 | 0 | continue; |
2716 | | |
2717 | 0 | strbuf_addf(err, _("'%s' exists; cannot create '%s'"), |
2718 | 0 | iter->ref.name, refname); |
2719 | 0 | goto cleanup; |
2720 | 0 | } |
2721 | | |
2722 | 0 | if (ok != ITER_DONE) |
2723 | 0 | BUG("error while iterating over references"); |
2724 | 0 | } |
2725 | | |
2726 | 0 | extra_refname = find_descendant_ref(dirname.buf, extras, skip); |
2727 | 0 | if (extra_refname) { |
2728 | 0 | if (transaction && ref_transaction_maybe_set_rejected( |
2729 | 0 | transaction, *update_idx, |
2730 | 0 | REF_TRANSACTION_ERROR_NAME_CONFLICT)) |
2731 | 0 | continue; |
2732 | | |
2733 | 0 | strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"), |
2734 | 0 | refname, extra_refname); |
2735 | 0 | goto cleanup; |
2736 | 0 | } |
2737 | 0 | } |
2738 | | |
2739 | 0 | ret = 0; |
2740 | |
|
2741 | 0 | cleanup: |
2742 | 0 | strbuf_release(&referent); |
2743 | 0 | strbuf_release(&dirname); |
2744 | 0 | strset_clear(&conflicting_dirnames); |
2745 | 0 | strset_clear(&dirnames); |
2746 | 0 | ref_iterator_free(iter); |
2747 | 0 | return ret; |
2748 | 0 | } |
2749 | | |
2750 | | enum ref_transaction_error refs_verify_refname_available( |
2751 | | struct ref_store *refs, |
2752 | | const char *refname, |
2753 | | const struct string_list *extras, |
2754 | | const struct string_list *skip, |
2755 | | unsigned int initial_transaction, |
2756 | | struct strbuf *err) |
2757 | 0 | { |
2758 | 0 | struct string_list_item item = { .string = (char *) refname }; |
2759 | 0 | struct string_list refnames = { |
2760 | 0 | .items = &item, |
2761 | 0 | .nr = 1, |
2762 | 0 | }; |
2763 | |
|
2764 | 0 | return refs_verify_refnames_available(refs, &refnames, extras, skip, |
2765 | 0 | NULL, initial_transaction, err); |
2766 | 0 | } |
2767 | | |
2768 | | struct do_for_each_reflog_help { |
2769 | | each_reflog_fn *fn; |
2770 | | void *cb_data; |
2771 | | }; |
2772 | | |
2773 | | static int do_for_each_reflog_helper(const struct reference *ref, void *cb_data) |
2774 | 0 | { |
2775 | 0 | struct do_for_each_reflog_help *hp = cb_data; |
2776 | 0 | return hp->fn(ref->name, hp->cb_data); |
2777 | 0 | } |
2778 | | |
2779 | | int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data) |
2780 | 0 | { |
2781 | 0 | struct ref_iterator *iter; |
2782 | 0 | struct do_for_each_reflog_help hp = { fn, cb_data }; |
2783 | |
|
2784 | 0 | iter = refs->be->reflog_iterator_begin(refs); |
2785 | |
|
2786 | 0 | return do_for_each_ref_iterator(iter, do_for_each_reflog_helper, &hp); |
2787 | 0 | } |
2788 | | |
2789 | | int refs_for_each_reflog_ent_reverse(struct ref_store *refs, |
2790 | | const char *refname, |
2791 | | each_reflog_ent_fn fn, |
2792 | | void *cb_data) |
2793 | 0 | { |
2794 | 0 | return refs->be->for_each_reflog_ent_reverse(refs, refname, |
2795 | 0 | fn, cb_data); |
2796 | 0 | } |
2797 | | |
2798 | | int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, |
2799 | | each_reflog_ent_fn fn, void *cb_data) |
2800 | 0 | { |
2801 | 0 | return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data); |
2802 | 0 | } |
2803 | | |
2804 | | int refs_reflog_exists(struct ref_store *refs, const char *refname) |
2805 | 0 | { |
2806 | 0 | return refs->be->reflog_exists(refs, refname); |
2807 | 0 | } |
2808 | | |
2809 | | int refs_create_reflog(struct ref_store *refs, const char *refname, |
2810 | | struct strbuf *err) |
2811 | 0 | { |
2812 | 0 | return refs->be->create_reflog(refs, refname, err); |
2813 | 0 | } |
2814 | | |
2815 | | int refs_delete_reflog(struct ref_store *refs, const char *refname) |
2816 | 0 | { |
2817 | 0 | return refs->be->delete_reflog(refs, refname); |
2818 | 0 | } |
2819 | | |
2820 | | int refs_reflog_expire(struct ref_store *refs, |
2821 | | const char *refname, |
2822 | | unsigned int flags, |
2823 | | reflog_expiry_prepare_fn prepare_fn, |
2824 | | reflog_expiry_should_prune_fn should_prune_fn, |
2825 | | reflog_expiry_cleanup_fn cleanup_fn, |
2826 | | void *policy_cb_data) |
2827 | 0 | { |
2828 | 0 | return refs->be->reflog_expire(refs, refname, flags, |
2829 | 0 | prepare_fn, should_prune_fn, |
2830 | 0 | cleanup_fn, policy_cb_data); |
2831 | 0 | } |
2832 | | |
2833 | | void ref_transaction_for_each_queued_update(struct ref_transaction *transaction, |
2834 | | ref_transaction_for_each_queued_update_fn cb, |
2835 | | void *cb_data) |
2836 | 0 | { |
2837 | 0 | for (size_t i = 0; i < transaction->nr; i++) { |
2838 | 0 | struct ref_update *update = transaction->updates[i]; |
2839 | |
|
2840 | 0 | cb(update->refname, |
2841 | 0 | (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL, |
2842 | 0 | (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL, |
2843 | 0 | cb_data); |
2844 | 0 | } |
2845 | 0 | } |
2846 | | |
2847 | | void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction, |
2848 | | ref_transaction_for_each_rejected_update_fn cb, |
2849 | | void *cb_data) |
2850 | 0 | { |
2851 | 0 | if (!transaction->rejections) |
2852 | 0 | return; |
2853 | | |
2854 | 0 | for (size_t i = 0; i < transaction->rejections->nr; i++) { |
2855 | 0 | size_t update_index = transaction->rejections->update_indices[i]; |
2856 | 0 | struct ref_update *update = transaction->updates[update_index]; |
2857 | |
|
2858 | 0 | if (!update->rejection_err) |
2859 | 0 | continue; |
2860 | | |
2861 | 0 | cb(update->refname, |
2862 | 0 | (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL, |
2863 | 0 | (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL, |
2864 | 0 | update->old_target, update->new_target, |
2865 | 0 | update->rejection_err, cb_data); |
2866 | 0 | } |
2867 | 0 | } |
2868 | | |
2869 | | int refs_delete_refs(struct ref_store *refs, const char *logmsg, |
2870 | | struct string_list *refnames, unsigned int flags) |
2871 | 0 | { |
2872 | 0 | struct ref_transaction *transaction; |
2873 | 0 | struct strbuf err = STRBUF_INIT; |
2874 | 0 | struct string_list_item *item; |
2875 | 0 | int ret = 0, failures = 0; |
2876 | 0 | char *msg; |
2877 | |
|
2878 | 0 | if (!refnames->nr) |
2879 | 0 | return 0; |
2880 | | |
2881 | 0 | msg = normalize_reflog_message(logmsg); |
2882 | | |
2883 | | /* |
2884 | | * Since we don't check the references' old_oids, the |
2885 | | * individual updates can't fail, so we can pack all of the |
2886 | | * updates into a single transaction. |
2887 | | */ |
2888 | 0 | transaction = ref_store_transaction_begin(refs, 0, &err); |
2889 | 0 | if (!transaction) { |
2890 | 0 | ret = error("%s", err.buf); |
2891 | 0 | goto out; |
2892 | 0 | } |
2893 | | |
2894 | 0 | for_each_string_list_item(item, refnames) { |
2895 | 0 | ret = ref_transaction_delete(transaction, item->string, |
2896 | 0 | NULL, NULL, flags, msg, &err); |
2897 | 0 | if (ret) { |
2898 | 0 | warning(_("could not delete reference %s: %s"), |
2899 | 0 | item->string, err.buf); |
2900 | 0 | strbuf_reset(&err); |
2901 | 0 | failures = 1; |
2902 | 0 | } |
2903 | 0 | } |
2904 | |
|
2905 | 0 | ret = ref_transaction_commit(transaction, &err); |
2906 | 0 | if (ret) { |
2907 | 0 | if (refnames->nr == 1) |
2908 | 0 | error(_("could not delete reference %s: %s"), |
2909 | 0 | refnames->items[0].string, err.buf); |
2910 | 0 | else |
2911 | 0 | error(_("could not delete references: %s"), err.buf); |
2912 | 0 | } |
2913 | |
|
2914 | 0 | out: |
2915 | 0 | if (!ret && failures) |
2916 | 0 | ret = -1; |
2917 | 0 | ref_transaction_free(transaction); |
2918 | 0 | strbuf_release(&err); |
2919 | 0 | free(msg); |
2920 | 0 | return ret; |
2921 | 0 | } |
2922 | | |
2923 | | int refs_rename_ref(struct ref_store *refs, const char *oldref, |
2924 | | const char *newref, const char *logmsg) |
2925 | 0 | { |
2926 | 0 | char *msg; |
2927 | 0 | int retval; |
2928 | |
|
2929 | 0 | msg = normalize_reflog_message(logmsg); |
2930 | 0 | retval = refs->be->rename_ref(refs, oldref, newref, msg); |
2931 | 0 | free(msg); |
2932 | 0 | return retval; |
2933 | 0 | } |
2934 | | |
2935 | | int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, |
2936 | | const char *newref, const char *logmsg) |
2937 | 0 | { |
2938 | 0 | char *msg; |
2939 | 0 | int retval; |
2940 | |
|
2941 | 0 | msg = normalize_reflog_message(logmsg); |
2942 | 0 | retval = refs->be->copy_ref(refs, oldref, newref, msg); |
2943 | 0 | free(msg); |
2944 | 0 | return retval; |
2945 | 0 | } |
2946 | | |
2947 | | const char *ref_update_original_update_refname(struct ref_update *update) |
2948 | 0 | { |
2949 | 0 | while (update->parent_update) |
2950 | 0 | update = update->parent_update; |
2951 | |
|
2952 | 0 | return update->refname; |
2953 | 0 | } |
2954 | | |
2955 | | int ref_update_has_null_new_value(struct ref_update *update) |
2956 | 0 | { |
2957 | 0 | return !update->new_target && is_null_oid(&update->new_oid); |
2958 | 0 | } |
2959 | | |
2960 | | enum ref_transaction_error ref_update_check_old_target(const char *referent, |
2961 | | struct ref_update *update, |
2962 | | struct strbuf *err) |
2963 | 0 | { |
2964 | 0 | if (!update->old_target) |
2965 | 0 | BUG("called without old_target set"); |
2966 | | |
2967 | 0 | if (!strcmp(referent, update->old_target)) |
2968 | 0 | return 0; |
2969 | | |
2970 | 0 | if (!strcmp(referent, "")) { |
2971 | 0 | strbuf_addf(err, "verifying symref target: '%s': " |
2972 | 0 | "reference is missing but expected %s", |
2973 | 0 | ref_update_original_update_refname(update), |
2974 | 0 | update->old_target); |
2975 | 0 | return REF_TRANSACTION_ERROR_NONEXISTENT_REF; |
2976 | 0 | } |
2977 | | |
2978 | 0 | strbuf_addf(err, "verifying symref target: '%s': is at %s but expected %s", |
2979 | 0 | ref_update_original_update_refname(update), |
2980 | 0 | referent, update->old_target); |
2981 | 0 | return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE; |
2982 | 0 | } |
2983 | | |
2984 | | struct migration_data { |
2985 | | struct ref_store *old_refs; |
2986 | | struct ref_transaction *transaction; |
2987 | | struct strbuf *errbuf; |
2988 | | struct strbuf sb, name, mail; |
2989 | | uint64_t index; |
2990 | | }; |
2991 | | |
2992 | | static int migrate_one_ref(const struct reference *ref, void *cb_data) |
2993 | 0 | { |
2994 | 0 | struct migration_data *data = cb_data; |
2995 | 0 | struct strbuf symref_target = STRBUF_INIT; |
2996 | 0 | int ret; |
2997 | |
|
2998 | 0 | if (ref->flags & REF_ISSYMREF) { |
2999 | 0 | ret = refs_read_symbolic_ref(data->old_refs, ref->name, &symref_target); |
3000 | 0 | if (ret < 0) |
3001 | 0 | goto done; |
3002 | | |
3003 | 0 | ret = ref_transaction_update(data->transaction, ref->name, NULL, null_oid(the_hash_algo), |
3004 | 0 | symref_target.buf, NULL, |
3005 | 0 | REF_SKIP_CREATE_REFLOG | REF_NO_DEREF, NULL, data->errbuf); |
3006 | 0 | if (ret < 0) |
3007 | 0 | goto done; |
3008 | 0 | } else { |
3009 | 0 | ret = ref_transaction_create(data->transaction, ref->name, ref->oid, NULL, |
3010 | 0 | REF_SKIP_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION, |
3011 | 0 | NULL, data->errbuf); |
3012 | 0 | if (ret < 0) |
3013 | 0 | goto done; |
3014 | 0 | } |
3015 | | |
3016 | 0 | done: |
3017 | 0 | strbuf_release(&symref_target); |
3018 | 0 | return ret; |
3019 | 0 | } |
3020 | | |
3021 | | static int migrate_one_reflog_entry(const char *refname, |
3022 | | struct object_id *old_oid, |
3023 | | struct object_id *new_oid, |
3024 | | const char *committer, |
3025 | | timestamp_t timestamp, int tz, |
3026 | | const char *msg, void *cb_data) |
3027 | 0 | { |
3028 | 0 | struct migration_data *data = cb_data; |
3029 | 0 | struct ident_split ident; |
3030 | 0 | const char *date; |
3031 | 0 | int ret; |
3032 | |
|
3033 | 0 | if (split_ident_line(&ident, committer, strlen(committer)) < 0) |
3034 | 0 | return -1; |
3035 | | |
3036 | 0 | strbuf_reset(&data->name); |
3037 | 0 | strbuf_add(&data->name, ident.name_begin, ident.name_end - ident.name_begin); |
3038 | 0 | strbuf_reset(&data->mail); |
3039 | 0 | strbuf_add(&data->mail, ident.mail_begin, ident.mail_end - ident.mail_begin); |
3040 | |
|
3041 | 0 | date = show_date(timestamp, tz, DATE_MODE(NORMAL)); |
3042 | 0 | strbuf_reset(&data->sb); |
3043 | 0 | strbuf_addstr(&data->sb, fmt_ident(data->name.buf, data->mail.buf, WANT_BLANK_IDENT, date, 0)); |
3044 | |
|
3045 | 0 | ret = ref_transaction_update_reflog(data->transaction, refname, |
3046 | 0 | new_oid, old_oid, data->sb.buf, |
3047 | 0 | msg, data->index++, data->errbuf); |
3048 | 0 | return ret; |
3049 | 0 | } |
3050 | | |
3051 | | static int migrate_one_reflog(const char *refname, void *cb_data) |
3052 | 0 | { |
3053 | 0 | struct migration_data *migration_data = cb_data; |
3054 | 0 | return refs_for_each_reflog_ent(migration_data->old_refs, refname, |
3055 | 0 | migrate_one_reflog_entry, migration_data); |
3056 | 0 | } |
3057 | | |
3058 | | static int move_files(const char *from_path, const char *to_path, struct strbuf *errbuf) |
3059 | 0 | { |
3060 | 0 | struct strbuf from_buf = STRBUF_INIT, to_buf = STRBUF_INIT; |
3061 | 0 | size_t from_len, to_len; |
3062 | 0 | DIR *from_dir; |
3063 | 0 | int ret; |
3064 | |
|
3065 | 0 | from_dir = opendir(from_path); |
3066 | 0 | if (!from_dir) { |
3067 | 0 | strbuf_addf(errbuf, "could not open source directory '%s': %s", |
3068 | 0 | from_path, strerror(errno)); |
3069 | 0 | ret = -1; |
3070 | 0 | goto done; |
3071 | 0 | } |
3072 | | |
3073 | 0 | strbuf_addstr(&from_buf, from_path); |
3074 | 0 | strbuf_complete(&from_buf, '/'); |
3075 | 0 | from_len = from_buf.len; |
3076 | |
|
3077 | 0 | strbuf_addstr(&to_buf, to_path); |
3078 | 0 | strbuf_complete(&to_buf, '/'); |
3079 | 0 | to_len = to_buf.len; |
3080 | |
|
3081 | 0 | while (1) { |
3082 | 0 | struct dirent *ent; |
3083 | |
|
3084 | 0 | errno = 0; |
3085 | 0 | ent = readdir(from_dir); |
3086 | 0 | if (!ent) |
3087 | 0 | break; |
3088 | | |
3089 | 0 | if (!strcmp(ent->d_name, ".") || |
3090 | 0 | !strcmp(ent->d_name, "..")) |
3091 | 0 | continue; |
3092 | | |
3093 | 0 | strbuf_setlen(&from_buf, from_len); |
3094 | 0 | strbuf_addstr(&from_buf, ent->d_name); |
3095 | |
|
3096 | 0 | strbuf_setlen(&to_buf, to_len); |
3097 | 0 | strbuf_addstr(&to_buf, ent->d_name); |
3098 | |
|
3099 | 0 | ret = rename(from_buf.buf, to_buf.buf); |
3100 | 0 | if (ret < 0) { |
3101 | 0 | strbuf_addf(errbuf, "could not link file '%s' to '%s': %s", |
3102 | 0 | from_buf.buf, to_buf.buf, strerror(errno)); |
3103 | 0 | goto done; |
3104 | 0 | } |
3105 | 0 | } |
3106 | | |
3107 | 0 | if (errno) { |
3108 | 0 | strbuf_addf(errbuf, "could not read entry from directory '%s': %s", |
3109 | 0 | from_path, strerror(errno)); |
3110 | 0 | ret = -1; |
3111 | 0 | goto done; |
3112 | 0 | } |
3113 | | |
3114 | 0 | ret = 0; |
3115 | |
|
3116 | 0 | done: |
3117 | 0 | strbuf_release(&from_buf); |
3118 | 0 | strbuf_release(&to_buf); |
3119 | 0 | if (from_dir) |
3120 | 0 | closedir(from_dir); |
3121 | 0 | return ret; |
3122 | 0 | } |
3123 | | |
3124 | | static int has_worktrees(void) |
3125 | 0 | { |
3126 | 0 | struct worktree **worktrees = get_worktrees(); |
3127 | 0 | int ret = 0; |
3128 | 0 | size_t i; |
3129 | |
|
3130 | 0 | for (i = 0; worktrees[i]; i++) { |
3131 | 0 | if (is_main_worktree(worktrees[i])) |
3132 | 0 | continue; |
3133 | 0 | ret = 1; |
3134 | 0 | } |
3135 | |
|
3136 | 0 | free_worktrees(worktrees); |
3137 | 0 | return ret; |
3138 | 0 | } |
3139 | | |
3140 | | int repo_migrate_ref_storage_format(struct repository *repo, |
3141 | | enum ref_storage_format format, |
3142 | | unsigned int flags, |
3143 | | struct strbuf *errbuf) |
3144 | 0 | { |
3145 | 0 | struct ref_store *old_refs = NULL, *new_refs = NULL; |
3146 | 0 | struct ref_transaction *transaction = NULL; |
3147 | 0 | struct strbuf new_gitdir = STRBUF_INIT; |
3148 | 0 | struct migration_data data = { |
3149 | 0 | .sb = STRBUF_INIT, |
3150 | 0 | .name = STRBUF_INIT, |
3151 | 0 | .mail = STRBUF_INIT, |
3152 | 0 | }; |
3153 | 0 | int did_migrate_refs = 0; |
3154 | 0 | int ret; |
3155 | |
|
3156 | 0 | if (repo->ref_storage_format == format) { |
3157 | 0 | strbuf_addstr(errbuf, "current and new ref storage format are equal"); |
3158 | 0 | ret = -1; |
3159 | 0 | goto done; |
3160 | 0 | } |
3161 | | |
3162 | 0 | old_refs = get_main_ref_store(repo); |
3163 | | |
3164 | | /* |
3165 | | * Worktrees complicate the migration because every worktree has a |
3166 | | * separate ref storage. While it should be feasible to implement, this |
3167 | | * is pushed out to a future iteration. |
3168 | | * |
3169 | | * TODO: we should really be passing the caller-provided repository to |
3170 | | * `has_worktrees()`, but our worktree subsystem doesn't yet support |
3171 | | * that. |
3172 | | */ |
3173 | 0 | if (has_worktrees()) { |
3174 | 0 | strbuf_addstr(errbuf, "migrating repositories with worktrees is not supported yet"); |
3175 | 0 | ret = -1; |
3176 | 0 | goto done; |
3177 | 0 | } |
3178 | | |
3179 | | /* |
3180 | | * The overall logic looks like this: |
3181 | | * |
3182 | | * 1. Set up a new temporary directory and initialize it with the new |
3183 | | * format. This is where all refs will be migrated into. |
3184 | | * |
3185 | | * 2. Enumerate all refs and write them into the new ref storage. |
3186 | | * This operation is safe as we do not yet modify the main |
3187 | | * repository. |
3188 | | * |
3189 | | * 3. Enumerate all reflogs and write them into the new ref storage. |
3190 | | * This operation is safe as we do not yet modify the main |
3191 | | * repository. |
3192 | | * |
3193 | | * 4. If we're in dry-run mode then we are done and can hand over the |
3194 | | * directory to the caller for inspection. If not, we now start |
3195 | | * with the destructive part. |
3196 | | * |
3197 | | * 5. Delete the old ref storage from disk. As we have a copy of refs |
3198 | | * in the new ref storage it's okay(ish) if we now get interrupted |
3199 | | * as there is an equivalent copy of all refs available. |
3200 | | * |
3201 | | * 6. Move the new ref storage files into place. |
3202 | | * |
3203 | | * 7. Change the repository format to the new ref format. |
3204 | | */ |
3205 | 0 | strbuf_addf(&new_gitdir, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX"); |
3206 | 0 | if (!mkdtemp(new_gitdir.buf)) { |
3207 | 0 | strbuf_addf(errbuf, "cannot create migration directory: %s", |
3208 | 0 | strerror(errno)); |
3209 | 0 | ret = -1; |
3210 | 0 | goto done; |
3211 | 0 | } |
3212 | | |
3213 | 0 | new_refs = ref_store_init(repo, format, new_gitdir.buf, |
3214 | 0 | REF_STORE_ALL_CAPS); |
3215 | 0 | ret = ref_store_create_on_disk(new_refs, 0, errbuf); |
3216 | 0 | if (ret < 0) |
3217 | 0 | goto done; |
3218 | | |
3219 | 0 | transaction = ref_store_transaction_begin(new_refs, REF_TRANSACTION_FLAG_INITIAL, |
3220 | 0 | errbuf); |
3221 | 0 | if (!transaction) |
3222 | 0 | goto done; |
3223 | | |
3224 | 0 | data.old_refs = old_refs; |
3225 | 0 | data.transaction = transaction; |
3226 | 0 | data.errbuf = errbuf; |
3227 | | |
3228 | | /* |
3229 | | * We need to use the internal `do_for_each_ref()` here so that we can |
3230 | | * also include broken refs and symrefs. These would otherwise be |
3231 | | * skipped silently. |
3232 | | * |
3233 | | * Ideally, we would do this call while locking the old ref storage |
3234 | | * such that there cannot be any concurrent modifications. We do not |
3235 | | * have the infra for that though, and the "files" backend does not |
3236 | | * allow for a central lock due to its design. It's thus on the user to |
3237 | | * ensure that there are no concurrent writes. |
3238 | | */ |
3239 | 0 | ret = do_for_each_ref(old_refs, "", NULL, migrate_one_ref, 0, |
3240 | 0 | DO_FOR_EACH_INCLUDE_ROOT_REFS | DO_FOR_EACH_INCLUDE_BROKEN, |
3241 | 0 | &data); |
3242 | 0 | if (ret < 0) |
3243 | 0 | goto done; |
3244 | | |
3245 | 0 | if (!(flags & REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG)) { |
3246 | 0 | ret = refs_for_each_reflog(old_refs, migrate_one_reflog, &data); |
3247 | 0 | if (ret < 0) |
3248 | 0 | goto done; |
3249 | 0 | } |
3250 | | |
3251 | 0 | ret = ref_transaction_commit(transaction, errbuf); |
3252 | 0 | if (ret < 0) |
3253 | 0 | goto done; |
3254 | 0 | did_migrate_refs = 1; |
3255 | |
|
3256 | 0 | if (flags & REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN) { |
3257 | 0 | printf(_("Finished dry-run migration of refs, " |
3258 | 0 | "the result can be found at '%s'\n"), new_gitdir.buf); |
3259 | 0 | ret = 0; |
3260 | 0 | goto done; |
3261 | 0 | } |
3262 | | |
3263 | | /* |
3264 | | * Release the new ref store such that any potentially-open files will |
3265 | | * be closed. This is required for platforms like Cygwin, where |
3266 | | * renaming an open file results in EPERM. |
3267 | | */ |
3268 | 0 | ref_store_release(new_refs); |
3269 | 0 | FREE_AND_NULL(new_refs); |
3270 | | |
3271 | | /* |
3272 | | * Until now we were in the non-destructive phase, where we only |
3273 | | * populated the new ref store. From hereon though we are about |
3274 | | * to get hands by deleting the old ref store and then moving |
3275 | | * the new one into place. |
3276 | | * |
3277 | | * Assuming that there were no concurrent writes, the new ref |
3278 | | * store should have all information. So if we fail from hereon |
3279 | | * we may be in an in-between state, but it would still be able |
3280 | | * to recover by manually moving remaining files from the |
3281 | | * temporary migration directory into place. |
3282 | | */ |
3283 | 0 | ret = ref_store_remove_on_disk(old_refs, errbuf); |
3284 | 0 | if (ret < 0) |
3285 | 0 | goto done; |
3286 | | |
3287 | 0 | ret = move_files(new_gitdir.buf, old_refs->gitdir, errbuf); |
3288 | 0 | if (ret < 0) |
3289 | 0 | goto done; |
3290 | | |
3291 | 0 | if (rmdir(new_gitdir.buf) < 0) |
3292 | 0 | warning_errno(_("could not remove temporary migration directory '%s'"), |
3293 | 0 | new_gitdir.buf); |
3294 | | |
3295 | | /* |
3296 | | * We have migrated the repository, so we now need to adjust the |
3297 | | * repository format so that clients will use the new ref store. |
3298 | | * We also need to swap out the repository's main ref store. |
3299 | | */ |
3300 | 0 | initialize_repository_version(hash_algo_by_ptr(repo->hash_algo), format, 1); |
3301 | | |
3302 | | /* |
3303 | | * Unset the old ref store and release it. `get_main_ref_store()` will |
3304 | | * make sure to lazily re-initialize the repository's ref store with |
3305 | | * the new format. |
3306 | | */ |
3307 | 0 | ref_store_release(old_refs); |
3308 | 0 | FREE_AND_NULL(old_refs); |
3309 | 0 | repo->refs_private = NULL; |
3310 | |
|
3311 | 0 | ret = 0; |
3312 | |
|
3313 | 0 | done: |
3314 | 0 | if (ret && did_migrate_refs) { |
3315 | 0 | strbuf_complete(errbuf, '\n'); |
3316 | 0 | strbuf_addf(errbuf, _("migrated refs can be found at '%s'"), |
3317 | 0 | new_gitdir.buf); |
3318 | 0 | } |
3319 | |
|
3320 | 0 | if (new_refs) { |
3321 | 0 | ref_store_release(new_refs); |
3322 | 0 | free(new_refs); |
3323 | 0 | } |
3324 | 0 | ref_transaction_free(transaction); |
3325 | 0 | strbuf_release(&new_gitdir); |
3326 | 0 | strbuf_release(&data.sb); |
3327 | 0 | strbuf_release(&data.name); |
3328 | 0 | strbuf_release(&data.mail); |
3329 | 0 | return ret; |
3330 | 0 | } |
3331 | | |
3332 | | int ref_update_expects_existing_old_ref(struct ref_update *update) |
3333 | 0 | { |
3334 | 0 | if (update->flags & REF_LOG_ONLY) |
3335 | 0 | return 0; |
3336 | | |
3337 | 0 | return (update->flags & REF_HAVE_OLD) && |
3338 | 0 | (!is_null_oid(&update->old_oid) || update->old_target); |
3339 | 0 | } |
3340 | | |
3341 | | const char *ref_transaction_error_msg(enum ref_transaction_error err) |
3342 | 0 | { |
3343 | 0 | switch (err) { |
3344 | 0 | case REF_TRANSACTION_ERROR_NAME_CONFLICT: |
3345 | 0 | return "refname conflict"; |
3346 | 0 | case REF_TRANSACTION_ERROR_CREATE_EXISTS: |
3347 | 0 | return "reference already exists"; |
3348 | 0 | case REF_TRANSACTION_ERROR_NONEXISTENT_REF: |
3349 | 0 | return "reference does not exist"; |
3350 | 0 | case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE: |
3351 | 0 | return "incorrect old value provided"; |
3352 | 0 | case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE: |
3353 | 0 | return "invalid new value provided"; |
3354 | 0 | case REF_TRANSACTION_ERROR_EXPECTED_SYMREF: |
3355 | 0 | return "expected symref but found regular ref"; |
3356 | 0 | case REF_TRANSACTION_ERROR_CASE_CONFLICT: |
3357 | 0 | return "reference conflict due to case-insensitive filesystem"; |
3358 | 0 | default: |
3359 | 0 | return "unknown failure"; |
3360 | 0 | } |
3361 | 0 | } |