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