]>
Commit | Line | Data |
---|---|---|
7bd9bcf3 | 1 | #include "../cache.h" |
b2141fc1 | 2 | #include "../config.h" |
7bd9bcf3 MH |
3 | #include "../refs.h" |
4 | #include "refs-internal.h" | |
958f9646 | 5 | #include "ref-cache.h" |
67be7c5a | 6 | #include "packed-backend.h" |
3bc581b9 | 7 | #include "../iterator.h" |
2880d16f | 8 | #include "../dir-iterator.h" |
7bd9bcf3 MH |
9 | #include "../lockfile.h" |
10 | #include "../object.h" | |
11 | #include "../dir.h" | |
fb9c2d27 | 12 | #include "../chdir-notify.h" |
3a3b9d8c | 13 | #include "worktree.h" |
7bd9bcf3 | 14 | |
5ac95fee MH |
15 | /* |
16 | * This backend uses the following flags in `ref_update::flags` for | |
17 | * internal bookkeeping purposes. Their numerical values must not | |
91774afc | 18 | * conflict with REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, REF_HAVE_NEW, |
acedcde7 | 19 | * REF_HAVE_OLD, or REF_IS_PRUNING, which are also stored in |
5ac95fee MH |
20 | * `ref_update::flags`. |
21 | */ | |
22 | ||
23 | /* | |
24 | * Used as a flag in ref_update::flags when a loose ref is being | |
91774afc | 25 | * pruned. This flag must only be used when REF_NO_DEREF is set. |
5ac95fee | 26 | */ |
acedcde7 | 27 | #define REF_IS_PRUNING (1 << 4) |
5ac95fee MH |
28 | |
29 | /* | |
30 | * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken | |
31 | * refs (i.e., because the reference is about to be deleted anyway). | |
32 | */ | |
33 | #define REF_DELETING (1 << 5) | |
34 | ||
35 | /* | |
36 | * Used as a flag in ref_update::flags when the lockfile needs to be | |
37 | * committed. | |
38 | */ | |
39 | #define REF_NEEDS_COMMIT (1 << 6) | |
40 | ||
41 | /* | |
42 | * Used as a flag in ref_update::flags when we want to log a ref | |
43 | * update but not actually perform it. This is used when a symbolic | |
44 | * ref update is split up. | |
45 | */ | |
46 | #define REF_LOG_ONLY (1 << 7) | |
47 | ||
48 | /* | |
49 | * Used as a flag in ref_update::flags when the ref_update was via an | |
50 | * update to HEAD. | |
51 | */ | |
52 | #define REF_UPDATE_VIA_HEAD (1 << 8) | |
53 | ||
54 | /* | |
55 | * Used as a flag in ref_update::flags when the loose reference has | |
56 | * been deleted. | |
57 | */ | |
58 | #define REF_DELETED_LOOSE (1 << 9) | |
59 | ||
7bd9bcf3 MH |
60 | struct ref_lock { |
61 | char *ref_name; | |
ee4d8e45 | 62 | struct lock_file lk; |
7bd9bcf3 MH |
63 | struct object_id old_oid; |
64 | }; | |
65 | ||
00eebe35 MH |
66 | struct files_ref_store { |
67 | struct ref_store base; | |
9e7ec634 | 68 | unsigned int store_flags; |
32c597e7 | 69 | |
f57f37e2 NTND |
70 | char *gitdir; |
71 | char *gitcommondir; | |
33dfb9f3 | 72 | |
7c22bc8a | 73 | struct ref_cache *loose; |
55c6bc37 | 74 | |
e0cc8ac8 | 75 | struct ref_store *packed_ref_store; |
00eebe35 | 76 | }; |
7bd9bcf3 | 77 | |
65a0a8e5 | 78 | static void clear_loose_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 MH |
79 | { |
80 | if (refs->loose) { | |
7c22bc8a | 81 | free_ref_cache(refs->loose); |
7bd9bcf3 MH |
82 | refs->loose = NULL; |
83 | } | |
84 | } | |
85 | ||
a2d5156c JK |
86 | /* |
87 | * Create a new submodule ref cache and add it to the internal | |
88 | * set of caches. | |
89 | */ | |
9e7ec634 NTND |
90 | static struct ref_store *files_ref_store_create(const char *gitdir, |
91 | unsigned int flags) | |
7bd9bcf3 | 92 | { |
00eebe35 MH |
93 | struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); |
94 | struct ref_store *ref_store = (struct ref_store *)refs; | |
f57f37e2 | 95 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 | 96 | |
fbfd0a29 | 97 | base_ref_store_init(ref_store, &refs_be_files); |
9e7ec634 | 98 | refs->store_flags = flags; |
7bd9bcf3 | 99 | |
f57f37e2 NTND |
100 | refs->gitdir = xstrdup(gitdir); |
101 | get_common_dir_noenv(&sb, gitdir); | |
102 | refs->gitcommondir = strbuf_detach(&sb, NULL); | |
103 | strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir); | |
e0d48397 MH |
104 | refs->packed_ref_store = packed_ref_store_create(sb.buf, flags); |
105 | strbuf_release(&sb); | |
7bd9bcf3 | 106 | |
fb9c2d27 JK |
107 | chdir_notify_reparent("files-backend $GIT_DIR", |
108 | &refs->gitdir); | |
109 | chdir_notify_reparent("files-backend $GIT_COMMONDIR", | |
110 | &refs->gitcommondir); | |
111 | ||
00eebe35 | 112 | return ref_store; |
a2d5156c | 113 | } |
7bd9bcf3 | 114 | |
32c597e7 | 115 | /* |
9e7ec634 NTND |
116 | * Die if refs is not the main ref store. caller is used in any |
117 | * necessary error messages. | |
32c597e7 MH |
118 | */ |
119 | static void files_assert_main_repository(struct files_ref_store *refs, | |
120 | const char *caller) | |
121 | { | |
9e7ec634 NTND |
122 | if (refs->store_flags & REF_STORE_MAIN) |
123 | return; | |
124 | ||
033abf97 | 125 | BUG("operation %s only allowed for main ref store", caller); |
32c597e7 MH |
126 | } |
127 | ||
a2d5156c | 128 | /* |
00eebe35 | 129 | * Downcast ref_store to files_ref_store. Die if ref_store is not a |
9e7ec634 NTND |
130 | * files_ref_store. required_flags is compared with ref_store's |
131 | * store_flags to ensure the ref_store has all required capabilities. | |
132 | * "caller" is used in any necessary error messages. | |
a2d5156c | 133 | */ |
9e7ec634 NTND |
134 | static struct files_ref_store *files_downcast(struct ref_store *ref_store, |
135 | unsigned int required_flags, | |
136 | const char *caller) | |
a2d5156c | 137 | { |
32c597e7 MH |
138 | struct files_ref_store *refs; |
139 | ||
00eebe35 | 140 | if (ref_store->be != &refs_be_files) |
033abf97 | 141 | BUG("ref_store is type \"%s\" not \"files\" in %s", |
00eebe35 | 142 | ref_store->be->name, caller); |
2eed2780 | 143 | |
32c597e7 MH |
144 | refs = (struct files_ref_store *)ref_store; |
145 | ||
9e7ec634 | 146 | if ((refs->store_flags & required_flags) != required_flags) |
033abf97 | 147 | BUG("operation %s requires abilities 0x%x, but only have 0x%x", |
9e7ec634 | 148 | caller, required_flags, refs->store_flags); |
2eed2780 | 149 | |
32c597e7 | 150 | return refs; |
7bd9bcf3 MH |
151 | } |
152 | ||
3a3b9d8c NTND |
153 | static void files_reflog_path_other_worktrees(struct files_ref_store *refs, |
154 | struct strbuf *sb, | |
155 | const char *refname) | |
156 | { | |
157 | const char *real_ref; | |
158 | const char *worktree_name; | |
159 | int length; | |
160 | ||
161 | if (parse_worktree_ref(refname, &worktree_name, &length, &real_ref)) | |
162 | BUG("refname %s is not a other-worktree ref", refname); | |
163 | ||
164 | if (worktree_name) | |
165 | strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir, | |
166 | length, worktree_name, real_ref); | |
167 | else | |
168 | strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, | |
169 | real_ref); | |
170 | } | |
171 | ||
802de3da NTND |
172 | static void files_reflog_path(struct files_ref_store *refs, |
173 | struct strbuf *sb, | |
174 | const char *refname) | |
175 | { | |
f57f37e2 NTND |
176 | switch (ref_type(refname)) { |
177 | case REF_TYPE_PER_WORKTREE: | |
178 | case REF_TYPE_PSEUDOREF: | |
179 | strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname); | |
180 | break; | |
3a3b9d8c NTND |
181 | case REF_TYPE_OTHER_PSEUDOREF: |
182 | case REF_TYPE_MAIN_PSEUDOREF: | |
46c0eb58 NTND |
183 | files_reflog_path_other_worktrees(refs, sb, refname); |
184 | break; | |
f57f37e2 NTND |
185 | case REF_TYPE_NORMAL: |
186 | strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname); | |
187 | break; | |
188 | default: | |
033abf97 | 189 | BUG("unknown ref type %d of ref %s", |
f57f37e2 NTND |
190 | ref_type(refname), refname); |
191 | } | |
802de3da NTND |
192 | } |
193 | ||
19e02f4f NTND |
194 | static void files_ref_path(struct files_ref_store *refs, |
195 | struct strbuf *sb, | |
196 | const char *refname) | |
197 | { | |
f57f37e2 NTND |
198 | switch (ref_type(refname)) { |
199 | case REF_TYPE_PER_WORKTREE: | |
200 | case REF_TYPE_PSEUDOREF: | |
201 | strbuf_addf(sb, "%s/%s", refs->gitdir, refname); | |
202 | break; | |
3a3b9d8c NTND |
203 | case REF_TYPE_MAIN_PSEUDOREF: |
204 | if (!skip_prefix(refname, "main-worktree/", &refname)) | |
205 | BUG("ref %s is not a main pseudoref", refname); | |
206 | /* fallthrough */ | |
207 | case REF_TYPE_OTHER_PSEUDOREF: | |
f57f37e2 NTND |
208 | case REF_TYPE_NORMAL: |
209 | strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname); | |
210 | break; | |
211 | default: | |
033abf97 | 212 | BUG("unknown ref type %d of ref %s", |
f57f37e2 NTND |
213 | ref_type(refname), refname); |
214 | } | |
19e02f4f NTND |
215 | } |
216 | ||
09e65645 | 217 | /* |
b9317d55 | 218 | * Manually add refs/bisect, refs/rewritten and refs/worktree, which, being |
09e65645 NTND |
219 | * per-worktree, might not appear in the directory listing for |
220 | * refs/ in the main repo. | |
221 | */ | |
222 | static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname) | |
223 | { | |
b9317d55 | 224 | const char *prefixes[] = { "refs/bisect/", "refs/worktree/", "refs/rewritten/" }; |
90d31ff5 | 225 | int ip; |
09e65645 NTND |
226 | |
227 | if (strcmp(dirname, "refs/")) | |
228 | return; | |
229 | ||
90d31ff5 NTND |
230 | for (ip = 0; ip < ARRAY_SIZE(prefixes); ip++) { |
231 | const char *prefix = prefixes[ip]; | |
232 | int prefix_len = strlen(prefix); | |
233 | struct ref_entry *child_entry; | |
234 | int pos; | |
09e65645 | 235 | |
90d31ff5 NTND |
236 | pos = search_ref_dir(dir, prefix, prefix_len); |
237 | if (pos >= 0) | |
238 | continue; | |
239 | child_entry = create_dir_entry(dir->cache, prefix, prefix_len, 1); | |
09e65645 NTND |
240 | add_entry_to_dir(dir, child_entry); |
241 | } | |
242 | } | |
243 | ||
7bd9bcf3 MH |
244 | /* |
245 | * Read the loose references from the namespace dirname into dir | |
246 | * (without recursing). dirname must end with '/'. dir must be the | |
247 | * directory entry corresponding to dirname. | |
248 | */ | |
df308759 MH |
249 | static void loose_fill_ref_dir(struct ref_store *ref_store, |
250 | struct ref_dir *dir, const char *dirname) | |
7bd9bcf3 | 251 | { |
df308759 MH |
252 | struct files_ref_store *refs = |
253 | files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir"); | |
7bd9bcf3 MH |
254 | DIR *d; |
255 | struct dirent *de; | |
256 | int dirnamelen = strlen(dirname); | |
257 | struct strbuf refname; | |
258 | struct strbuf path = STRBUF_INIT; | |
259 | size_t path_baselen; | |
260 | ||
19e02f4f | 261 | files_ref_path(refs, &path, dirname); |
7bd9bcf3 MH |
262 | path_baselen = path.len; |
263 | ||
264 | d = opendir(path.buf); | |
265 | if (!d) { | |
266 | strbuf_release(&path); | |
267 | return; | |
268 | } | |
269 | ||
270 | strbuf_init(&refname, dirnamelen + 257); | |
271 | strbuf_add(&refname, dirname, dirnamelen); | |
272 | ||
273 | while ((de = readdir(d)) != NULL) { | |
4417df8c | 274 | struct object_id oid; |
7bd9bcf3 MH |
275 | struct stat st; |
276 | int flag; | |
277 | ||
278 | if (de->d_name[0] == '.') | |
279 | continue; | |
280 | if (ends_with(de->d_name, ".lock")) | |
281 | continue; | |
282 | strbuf_addstr(&refname, de->d_name); | |
283 | strbuf_addstr(&path, de->d_name); | |
284 | if (stat(path.buf, &st) < 0) { | |
285 | ; /* silently ignore */ | |
286 | } else if (S_ISDIR(st.st_mode)) { | |
287 | strbuf_addch(&refname, '/'); | |
288 | add_entry_to_dir(dir, | |
e00d1a4f | 289 | create_dir_entry(dir->cache, refname.buf, |
7bd9bcf3 MH |
290 | refname.len, 1)); |
291 | } else { | |
7d2df051 | 292 | if (!refs_resolve_ref_unsafe(&refs->base, |
3c0cb0cb MH |
293 | refname.buf, |
294 | RESOLVE_REF_READING, | |
49e61479 | 295 | &oid, &flag)) { |
4417df8c | 296 | oidclr(&oid); |
7bd9bcf3 | 297 | flag |= REF_ISBROKEN; |
4417df8c | 298 | } else if (is_null_oid(&oid)) { |
7bd9bcf3 MH |
299 | /* |
300 | * It is so astronomically unlikely | |
78fb4579 | 301 | * that null_oid is the OID of an |
7bd9bcf3 MH |
302 | * actual object that we consider its |
303 | * appearance in a loose reference | |
304 | * file to be repo corruption | |
305 | * (probably due to a software bug). | |
306 | */ | |
307 | flag |= REF_ISBROKEN; | |
308 | } | |
309 | ||
310 | if (check_refname_format(refname.buf, | |
311 | REFNAME_ALLOW_ONELEVEL)) { | |
312 | if (!refname_is_safe(refname.buf)) | |
313 | die("loose refname is dangerous: %s", refname.buf); | |
4417df8c | 314 | oidclr(&oid); |
7bd9bcf3 MH |
315 | flag |= REF_BAD_NAME | REF_ISBROKEN; |
316 | } | |
317 | add_entry_to_dir(dir, | |
c1da06c6 | 318 | create_ref_entry(refname.buf, &oid, flag)); |
7bd9bcf3 MH |
319 | } |
320 | strbuf_setlen(&refname, dirnamelen); | |
321 | strbuf_setlen(&path, path_baselen); | |
322 | } | |
323 | strbuf_release(&refname); | |
324 | strbuf_release(&path); | |
325 | closedir(d); | |
e3bf2989 | 326 | |
09e65645 | 327 | add_per_worktree_entries_to_dir(dir, dirname); |
7bd9bcf3 MH |
328 | } |
329 | ||
a714b19c | 330 | static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 MH |
331 | { |
332 | if (!refs->loose) { | |
333 | /* | |
334 | * Mark the top-level directory complete because we | |
335 | * are about to read the only subdirectory that can | |
336 | * hold references: | |
337 | */ | |
df308759 | 338 | refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir); |
7c22bc8a MH |
339 | |
340 | /* We're going to fill the top level ourselves: */ | |
341 | refs->loose->root->flag &= ~REF_INCOMPLETE; | |
342 | ||
7bd9bcf3 | 343 | /* |
7c22bc8a MH |
344 | * Add an incomplete entry for "refs/" (to be filled |
345 | * lazily): | |
7bd9bcf3 | 346 | */ |
7c22bc8a | 347 | add_entry_to_dir(get_ref_dir(refs->loose->root), |
e00d1a4f | 348 | create_dir_entry(refs->loose, "refs/", 5, 1)); |
7bd9bcf3 | 349 | } |
a714b19c | 350 | return refs->loose; |
7bd9bcf3 MH |
351 | } |
352 | ||
e1e33b72 | 353 | static int files_read_raw_ref(struct ref_store *ref_store, |
99afe91a | 354 | const char *refname, struct object_id *oid, |
e1e33b72 | 355 | struct strbuf *referent, unsigned int *type) |
7bd9bcf3 | 356 | { |
4308651c | 357 | struct files_ref_store *refs = |
9e7ec634 | 358 | files_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); |
42a38cf7 MH |
359 | struct strbuf sb_contents = STRBUF_INIT; |
360 | struct strbuf sb_path = STRBUF_INIT; | |
7048653a DT |
361 | const char *path; |
362 | const char *buf; | |
99afe91a | 363 | const char *p; |
7048653a DT |
364 | struct stat st; |
365 | int fd; | |
42a38cf7 MH |
366 | int ret = -1; |
367 | int save_errno; | |
e8c42cb9 | 368 | int remaining_retries = 3; |
7bd9bcf3 | 369 | |
fa96ea1b | 370 | *type = 0; |
42a38cf7 | 371 | strbuf_reset(&sb_path); |
34c7ad8f | 372 | |
19e02f4f | 373 | files_ref_path(refs, &sb_path, refname); |
34c7ad8f | 374 | |
42a38cf7 | 375 | path = sb_path.buf; |
7bd9bcf3 | 376 | |
7048653a DT |
377 | stat_ref: |
378 | /* | |
379 | * We might have to loop back here to avoid a race | |
380 | * condition: first we lstat() the file, then we try | |
381 | * to read it as a link or as a file. But if somebody | |
382 | * changes the type of the file (file <-> directory | |
383 | * <-> symlink) between the lstat() and reading, then | |
384 | * we don't want to report that as an error but rather | |
385 | * try again starting with the lstat(). | |
e8c42cb9 JK |
386 | * |
387 | * We'll keep a count of the retries, though, just to avoid | |
388 | * any confusing situation sending us into an infinite loop. | |
7048653a | 389 | */ |
7bd9bcf3 | 390 | |
e8c42cb9 JK |
391 | if (remaining_retries-- <= 0) |
392 | goto out; | |
393 | ||
7048653a DT |
394 | if (lstat(path, &st) < 0) { |
395 | if (errno != ENOENT) | |
42a38cf7 | 396 | goto out; |
e0cc8ac8 | 397 | if (refs_read_raw_ref(refs->packed_ref_store, refname, |
99afe91a | 398 | oid, referent, type)) { |
7048653a | 399 | errno = ENOENT; |
42a38cf7 | 400 | goto out; |
7bd9bcf3 | 401 | } |
42a38cf7 MH |
402 | ret = 0; |
403 | goto out; | |
7bd9bcf3 | 404 | } |
7bd9bcf3 | 405 | |
7048653a DT |
406 | /* Follow "normalized" - ie "refs/.." symlinks by hand */ |
407 | if (S_ISLNK(st.st_mode)) { | |
42a38cf7 | 408 | strbuf_reset(&sb_contents); |
765b496d | 409 | if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) { |
7048653a | 410 | if (errno == ENOENT || errno == EINVAL) |
7bd9bcf3 MH |
411 | /* inconsistent with lstat; retry */ |
412 | goto stat_ref; | |
413 | else | |
42a38cf7 | 414 | goto out; |
7bd9bcf3 | 415 | } |
42a38cf7 MH |
416 | if (starts_with(sb_contents.buf, "refs/") && |
417 | !check_refname_format(sb_contents.buf, 0)) { | |
92b38093 | 418 | strbuf_swap(&sb_contents, referent); |
3a0b6b9a | 419 | *type |= REF_ISSYMREF; |
42a38cf7 MH |
420 | ret = 0; |
421 | goto out; | |
7bd9bcf3 | 422 | } |
3f7bd767 JK |
423 | /* |
424 | * It doesn't look like a refname; fall through to just | |
425 | * treating it like a non-symlink, and reading whatever it | |
426 | * points to. | |
427 | */ | |
7048653a | 428 | } |
7bd9bcf3 | 429 | |
7048653a DT |
430 | /* Is it a directory? */ |
431 | if (S_ISDIR(st.st_mode)) { | |
e167a567 MH |
432 | /* |
433 | * Even though there is a directory where the loose | |
434 | * ref is supposed to be, there could still be a | |
435 | * packed ref: | |
436 | */ | |
e0cc8ac8 | 437 | if (refs_read_raw_ref(refs->packed_ref_store, refname, |
99afe91a | 438 | oid, referent, type)) { |
e167a567 MH |
439 | errno = EISDIR; |
440 | goto out; | |
441 | } | |
442 | ret = 0; | |
42a38cf7 | 443 | goto out; |
7048653a DT |
444 | } |
445 | ||
446 | /* | |
447 | * Anything else, just open it and try to use it as | |
448 | * a ref | |
449 | */ | |
450 | fd = open(path, O_RDONLY); | |
451 | if (fd < 0) { | |
3f7bd767 | 452 | if (errno == ENOENT && !S_ISLNK(st.st_mode)) |
7048653a DT |
453 | /* inconsistent with lstat; retry */ |
454 | goto stat_ref; | |
455 | else | |
42a38cf7 | 456 | goto out; |
7048653a | 457 | } |
42a38cf7 MH |
458 | strbuf_reset(&sb_contents); |
459 | if (strbuf_read(&sb_contents, fd, 256) < 0) { | |
7048653a DT |
460 | int save_errno = errno; |
461 | close(fd); | |
462 | errno = save_errno; | |
42a38cf7 | 463 | goto out; |
7048653a DT |
464 | } |
465 | close(fd); | |
42a38cf7 MH |
466 | strbuf_rtrim(&sb_contents); |
467 | buf = sb_contents.buf; | |
7048653a DT |
468 | if (starts_with(buf, "ref:")) { |
469 | buf += 4; | |
7bd9bcf3 MH |
470 | while (isspace(*buf)) |
471 | buf++; | |
7048653a | 472 | |
92b38093 MH |
473 | strbuf_reset(referent); |
474 | strbuf_addstr(referent, buf); | |
3a0b6b9a | 475 | *type |= REF_ISSYMREF; |
42a38cf7 MH |
476 | ret = 0; |
477 | goto out; | |
7bd9bcf3 | 478 | } |
7bd9bcf3 | 479 | |
7048653a DT |
480 | /* |
481 | * Please note that FETCH_HEAD has additional | |
482 | * data after the sha. | |
483 | */ | |
99afe91a | 484 | if (parse_oid_hex(buf, oid, &p) || |
485 | (*p != '\0' && !isspace(*p))) { | |
3a0b6b9a | 486 | *type |= REF_ISBROKEN; |
7048653a | 487 | errno = EINVAL; |
42a38cf7 | 488 | goto out; |
7048653a DT |
489 | } |
490 | ||
42a38cf7 | 491 | ret = 0; |
7bd9bcf3 | 492 | |
42a38cf7 MH |
493 | out: |
494 | save_errno = errno; | |
7bd9bcf3 MH |
495 | strbuf_release(&sb_path); |
496 | strbuf_release(&sb_contents); | |
42a38cf7 | 497 | errno = save_errno; |
7bd9bcf3 MH |
498 | return ret; |
499 | } | |
500 | ||
8415d247 MH |
501 | static void unlock_ref(struct ref_lock *lock) |
502 | { | |
ee4d8e45 | 503 | rollback_lock_file(&lock->lk); |
8415d247 | 504 | free(lock->ref_name); |
8415d247 MH |
505 | free(lock); |
506 | } | |
507 | ||
92b1551b MH |
508 | /* |
509 | * Lock refname, without following symrefs, and set *lock_p to point | |
510 | * at a newly-allocated lock object. Fill in lock->old_oid, referent, | |
511 | * and type similarly to read_raw_ref(). | |
512 | * | |
513 | * The caller must verify that refname is a "safe" reference name (in | |
514 | * the sense of refname_is_safe()) before calling this function. | |
515 | * | |
516 | * If the reference doesn't already exist, verify that refname doesn't | |
517 | * have a D/F conflict with any existing references. extras and skip | |
524a9fdb | 518 | * are passed to refs_verify_refname_available() for this check. |
92b1551b MH |
519 | * |
520 | * If mustexist is not set and the reference is not found or is | |
78fb4579 | 521 | * broken, lock the reference anyway but clear old_oid. |
92b1551b MH |
522 | * |
523 | * Return 0 on success. On failure, write an error message to err and | |
524 | * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR. | |
525 | * | |
526 | * Implementation note: This function is basically | |
527 | * | |
528 | * lock reference | |
529 | * read_raw_ref() | |
530 | * | |
531 | * but it includes a lot more code to | |
532 | * - Deal with possible races with other processes | |
524a9fdb | 533 | * - Avoid calling refs_verify_refname_available() when it can be |
92b1551b MH |
534 | * avoided, namely if we were successfully able to read the ref |
535 | * - Generate informative error messages in the case of failure | |
536 | */ | |
f7b0a987 MH |
537 | static int lock_raw_ref(struct files_ref_store *refs, |
538 | const char *refname, int mustexist, | |
92b1551b MH |
539 | const struct string_list *extras, |
540 | const struct string_list *skip, | |
541 | struct ref_lock **lock_p, | |
542 | struct strbuf *referent, | |
543 | unsigned int *type, | |
544 | struct strbuf *err) | |
545 | { | |
546 | struct ref_lock *lock; | |
547 | struct strbuf ref_file = STRBUF_INIT; | |
548 | int attempts_remaining = 3; | |
549 | int ret = TRANSACTION_GENERIC_ERROR; | |
550 | ||
551 | assert(err); | |
32c597e7 | 552 | files_assert_main_repository(refs, "lock_raw_ref"); |
f7b0a987 | 553 | |
92b1551b MH |
554 | *type = 0; |
555 | ||
556 | /* First lock the file so it can't change out from under us. */ | |
557 | ||
558 | *lock_p = lock = xcalloc(1, sizeof(*lock)); | |
559 | ||
560 | lock->ref_name = xstrdup(refname); | |
19e02f4f | 561 | files_ref_path(refs, &ref_file, refname); |
92b1551b MH |
562 | |
563 | retry: | |
564 | switch (safe_create_leading_directories(ref_file.buf)) { | |
565 | case SCLD_OK: | |
566 | break; /* success */ | |
567 | case SCLD_EXISTS: | |
568 | /* | |
569 | * Suppose refname is "refs/foo/bar". We just failed | |
570 | * to create the containing directory, "refs/foo", | |
571 | * because there was a non-directory in the way. This | |
572 | * indicates a D/F conflict, probably because of | |
573 | * another reference such as "refs/foo". There is no | |
574 | * reason to expect this error to be transitory. | |
575 | */ | |
7d2df051 NTND |
576 | if (refs_verify_refname_available(&refs->base, refname, |
577 | extras, skip, err)) { | |
92b1551b MH |
578 | if (mustexist) { |
579 | /* | |
580 | * To the user the relevant error is | |
581 | * that the "mustexist" reference is | |
582 | * missing: | |
583 | */ | |
584 | strbuf_reset(err); | |
585 | strbuf_addf(err, "unable to resolve reference '%s'", | |
586 | refname); | |
587 | } else { | |
588 | /* | |
589 | * The error message set by | |
524a9fdb MH |
590 | * refs_verify_refname_available() is |
591 | * OK. | |
92b1551b MH |
592 | */ |
593 | ret = TRANSACTION_NAME_CONFLICT; | |
594 | } | |
595 | } else { | |
596 | /* | |
597 | * The file that is in the way isn't a loose | |
598 | * reference. Report it as a low-level | |
599 | * failure. | |
600 | */ | |
601 | strbuf_addf(err, "unable to create lock file %s.lock; " | |
602 | "non-directory in the way", | |
603 | ref_file.buf); | |
604 | } | |
605 | goto error_return; | |
606 | case SCLD_VANISHED: | |
607 | /* Maybe another process was tidying up. Try again. */ | |
608 | if (--attempts_remaining > 0) | |
609 | goto retry; | |
610 | /* fall through */ | |
611 | default: | |
612 | strbuf_addf(err, "unable to create directory for %s", | |
613 | ref_file.buf); | |
614 | goto error_return; | |
615 | } | |
616 | ||
4ff0f01c | 617 | if (hold_lock_file_for_update_timeout( |
ee4d8e45 | 618 | &lock->lk, ref_file.buf, LOCK_NO_DEREF, |
4ff0f01c | 619 | get_files_ref_lock_timeout_ms()) < 0) { |
92b1551b MH |
620 | if (errno == ENOENT && --attempts_remaining > 0) { |
621 | /* | |
622 | * Maybe somebody just deleted one of the | |
623 | * directories leading to ref_file. Try | |
624 | * again: | |
625 | */ | |
626 | goto retry; | |
627 | } else { | |
628 | unable_to_lock_message(ref_file.buf, errno, err); | |
629 | goto error_return; | |
630 | } | |
631 | } | |
632 | ||
633 | /* | |
634 | * Now we hold the lock and can read the reference without | |
635 | * fear that its value will change. | |
636 | */ | |
637 | ||
f7b0a987 | 638 | if (files_read_raw_ref(&refs->base, refname, |
99afe91a | 639 | &lock->old_oid, referent, type)) { |
92b1551b MH |
640 | if (errno == ENOENT) { |
641 | if (mustexist) { | |
642 | /* Garden variety missing reference. */ | |
643 | strbuf_addf(err, "unable to resolve reference '%s'", | |
644 | refname); | |
645 | goto error_return; | |
646 | } else { | |
647 | /* | |
648 | * Reference is missing, but that's OK. We | |
649 | * know that there is not a conflict with | |
650 | * another loose reference because | |
651 | * (supposing that we are trying to lock | |
652 | * reference "refs/foo/bar"): | |
653 | * | |
654 | * - We were successfully able to create | |
655 | * the lockfile refs/foo/bar.lock, so we | |
656 | * know there cannot be a loose reference | |
657 | * named "refs/foo". | |
658 | * | |
659 | * - We got ENOENT and not EISDIR, so we | |
660 | * know that there cannot be a loose | |
661 | * reference named "refs/foo/bar/baz". | |
662 | */ | |
663 | } | |
664 | } else if (errno == EISDIR) { | |
665 | /* | |
666 | * There is a directory in the way. It might have | |
667 | * contained references that have been deleted. If | |
668 | * we don't require that the reference already | |
669 | * exists, try to remove the directory so that it | |
670 | * doesn't cause trouble when we want to rename the | |
671 | * lockfile into place later. | |
672 | */ | |
673 | if (mustexist) { | |
674 | /* Garden variety missing reference. */ | |
675 | strbuf_addf(err, "unable to resolve reference '%s'", | |
676 | refname); | |
677 | goto error_return; | |
678 | } else if (remove_dir_recursively(&ref_file, | |
679 | REMOVE_DIR_EMPTY_ONLY)) { | |
b05855b5 MH |
680 | if (refs_verify_refname_available( |
681 | &refs->base, refname, | |
682 | extras, skip, err)) { | |
92b1551b MH |
683 | /* |
684 | * The error message set by | |
685 | * verify_refname_available() is OK. | |
686 | */ | |
687 | ret = TRANSACTION_NAME_CONFLICT; | |
688 | goto error_return; | |
689 | } else { | |
690 | /* | |
691 | * We can't delete the directory, | |
692 | * but we also don't know of any | |
693 | * references that it should | |
694 | * contain. | |
695 | */ | |
696 | strbuf_addf(err, "there is a non-empty directory '%s' " | |
697 | "blocking reference '%s'", | |
698 | ref_file.buf, refname); | |
699 | goto error_return; | |
700 | } | |
701 | } | |
702 | } else if (errno == EINVAL && (*type & REF_ISBROKEN)) { | |
703 | strbuf_addf(err, "unable to resolve reference '%s': " | |
704 | "reference broken", refname); | |
705 | goto error_return; | |
706 | } else { | |
707 | strbuf_addf(err, "unable to resolve reference '%s': %s", | |
708 | refname, strerror(errno)); | |
709 | goto error_return; | |
710 | } | |
711 | ||
712 | /* | |
713 | * If the ref did not exist and we are creating it, | |
8ec617c8 MH |
714 | * make sure there is no existing packed ref that |
715 | * conflicts with refname: | |
92b1551b | 716 | */ |
524a9fdb | 717 | if (refs_verify_refname_available( |
8ec617c8 | 718 | refs->packed_ref_store, refname, |
524a9fdb | 719 | extras, skip, err)) |
92b1551b | 720 | goto error_return; |
92b1551b MH |
721 | } |
722 | ||
723 | ret = 0; | |
724 | goto out; | |
725 | ||
726 | error_return: | |
727 | unlock_ref(lock); | |
728 | *lock_p = NULL; | |
729 | ||
730 | out: | |
731 | strbuf_release(&ref_file); | |
732 | return ret; | |
733 | } | |
734 | ||
3bc581b9 MH |
735 | struct files_ref_iterator { |
736 | struct ref_iterator base; | |
737 | ||
3bc581b9 MH |
738 | struct ref_iterator *iter0; |
739 | unsigned int flags; | |
740 | }; | |
7bd9bcf3 | 741 | |
3bc581b9 MH |
742 | static int files_ref_iterator_advance(struct ref_iterator *ref_iterator) |
743 | { | |
744 | struct files_ref_iterator *iter = | |
745 | (struct files_ref_iterator *)ref_iterator; | |
746 | int ok; | |
7bd9bcf3 | 747 | |
3bc581b9 | 748 | while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) { |
0c09ec07 DT |
749 | if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY && |
750 | ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE) | |
751 | continue; | |
752 | ||
3bc581b9 MH |
753 | if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) && |
754 | !ref_resolves_to_object(iter->iter0->refname, | |
755 | iter->iter0->oid, | |
756 | iter->iter0->flags)) | |
757 | continue; | |
758 | ||
759 | iter->base.refname = iter->iter0->refname; | |
760 | iter->base.oid = iter->iter0->oid; | |
761 | iter->base.flags = iter->iter0->flags; | |
762 | return ITER_OK; | |
7bd9bcf3 MH |
763 | } |
764 | ||
3bc581b9 MH |
765 | iter->iter0 = NULL; |
766 | if (ref_iterator_abort(ref_iterator) != ITER_DONE) | |
767 | ok = ITER_ERROR; | |
768 | ||
769 | return ok; | |
7bd9bcf3 MH |
770 | } |
771 | ||
3bc581b9 MH |
772 | static int files_ref_iterator_peel(struct ref_iterator *ref_iterator, |
773 | struct object_id *peeled) | |
7bd9bcf3 | 774 | { |
3bc581b9 MH |
775 | struct files_ref_iterator *iter = |
776 | (struct files_ref_iterator *)ref_iterator; | |
93770590 | 777 | |
3bc581b9 MH |
778 | return ref_iterator_peel(iter->iter0, peeled); |
779 | } | |
780 | ||
781 | static int files_ref_iterator_abort(struct ref_iterator *ref_iterator) | |
782 | { | |
783 | struct files_ref_iterator *iter = | |
784 | (struct files_ref_iterator *)ref_iterator; | |
785 | int ok = ITER_DONE; | |
786 | ||
787 | if (iter->iter0) | |
788 | ok = ref_iterator_abort(iter->iter0); | |
789 | ||
3bc581b9 MH |
790 | base_ref_iterator_free(ref_iterator); |
791 | return ok; | |
792 | } | |
793 | ||
794 | static struct ref_iterator_vtable files_ref_iterator_vtable = { | |
795 | files_ref_iterator_advance, | |
796 | files_ref_iterator_peel, | |
797 | files_ref_iterator_abort | |
798 | }; | |
799 | ||
1a769003 | 800 | static struct ref_iterator *files_ref_iterator_begin( |
37b6f6d5 | 801 | struct ref_store *ref_store, |
3bc581b9 MH |
802 | const char *prefix, unsigned int flags) |
803 | { | |
9e7ec634 | 804 | struct files_ref_store *refs; |
8738a8a4 | 805 | struct ref_iterator *loose_iter, *packed_iter, *overlay_iter; |
3bc581b9 MH |
806 | struct files_ref_iterator *iter; |
807 | struct ref_iterator *ref_iterator; | |
0a0865b8 | 808 | unsigned int required_flags = REF_STORE_READ; |
3bc581b9 | 809 | |
0a0865b8 MH |
810 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) |
811 | required_flags |= REF_STORE_ODB; | |
3bc581b9 | 812 | |
0a0865b8 | 813 | refs = files_downcast(ref_store, required_flags, "ref_iterator_begin"); |
9e7ec634 | 814 | |
3bc581b9 MH |
815 | /* |
816 | * We must make sure that all loose refs are read before | |
817 | * accessing the packed-refs file; this avoids a race | |
818 | * condition if loose refs are migrated to the packed-refs | |
819 | * file by a simultaneous process, but our in-memory view is | |
820 | * from before the migration. We ensure this as follows: | |
059ae35a MH |
821 | * First, we call start the loose refs iteration with its |
822 | * `prime_ref` argument set to true. This causes the loose | |
823 | * references in the subtree to be pre-read into the cache. | |
824 | * (If they've already been read, that's OK; we only need to | |
825 | * guarantee that they're read before the packed refs, not | |
826 | * *how much* before.) After that, we call | |
38b86e81 MH |
827 | * packed_ref_iterator_begin(), which internally checks |
828 | * whether the packed-ref cache is up to date with what is on | |
829 | * disk, and re-reads it if not. | |
3bc581b9 MH |
830 | */ |
831 | ||
059ae35a MH |
832 | loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), |
833 | prefix, 1); | |
3bc581b9 | 834 | |
38b86e81 MH |
835 | /* |
836 | * The packed-refs file might contain broken references, for | |
837 | * example an old version of a reference that points at an | |
838 | * object that has since been garbage-collected. This is OK as | |
839 | * long as there is a corresponding loose reference that | |
840 | * overrides it, and we don't want to emit an error message in | |
841 | * this case. So ask the packed_ref_store for all of its | |
842 | * references, and (if needed) do our own check for broken | |
843 | * ones in files_ref_iterator_advance(), after we have merged | |
844 | * the packed and loose references. | |
845 | */ | |
e0cc8ac8 MH |
846 | packed_iter = refs_ref_iterator_begin( |
847 | refs->packed_ref_store, prefix, 0, | |
38b86e81 | 848 | DO_FOR_EACH_INCLUDE_BROKEN); |
3bc581b9 | 849 | |
8738a8a4 MH |
850 | overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter); |
851 | ||
852 | iter = xcalloc(1, sizeof(*iter)); | |
853 | ref_iterator = &iter->base; | |
854 | base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable, | |
855 | overlay_iter->ordered); | |
856 | iter->iter0 = overlay_iter; | |
3bc581b9 MH |
857 | iter->flags = flags; |
858 | ||
859 | return ref_iterator; | |
7bd9bcf3 MH |
860 | } |
861 | ||
7bd9bcf3 | 862 | /* |
4f01e508 | 863 | * Verify that the reference locked by lock has the value old_oid |
864 | * (unless it is NULL). Fail if the reference doesn't exist and | |
865 | * mustexist is set. Return 0 on success. On error, write an error | |
866 | * message to err, set errno, and return a negative value. | |
7bd9bcf3 | 867 | */ |
2f40e954 | 868 | static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock, |
4f01e508 | 869 | const struct object_id *old_oid, int mustexist, |
7bd9bcf3 MH |
870 | struct strbuf *err) |
871 | { | |
872 | assert(err); | |
873 | ||
2f40e954 NTND |
874 | if (refs_read_ref_full(ref_store, lock->ref_name, |
875 | mustexist ? RESOLVE_REF_READING : 0, | |
34c290a6 | 876 | &lock->old_oid, NULL)) { |
4f01e508 | 877 | if (old_oid) { |
6294dcb4 | 878 | int save_errno = errno; |
0568c8e9 | 879 | strbuf_addf(err, "can't verify ref '%s'", lock->ref_name); |
6294dcb4 JK |
880 | errno = save_errno; |
881 | return -1; | |
882 | } else { | |
c368dde9 | 883 | oidclr(&lock->old_oid); |
6294dcb4 JK |
884 | return 0; |
885 | } | |
7bd9bcf3 | 886 | } |
9001dc2a | 887 | if (old_oid && !oideq(&lock->old_oid, old_oid)) { |
0568c8e9 | 888 | strbuf_addf(err, "ref '%s' is at %s but expected %s", |
7bd9bcf3 | 889 | lock->ref_name, |
c368dde9 | 890 | oid_to_hex(&lock->old_oid), |
4f01e508 | 891 | oid_to_hex(old_oid)); |
7bd9bcf3 MH |
892 | errno = EBUSY; |
893 | return -1; | |
894 | } | |
895 | return 0; | |
896 | } | |
897 | ||
898 | static int remove_empty_directories(struct strbuf *path) | |
899 | { | |
900 | /* | |
901 | * we want to create a file but there is a directory there; | |
902 | * if that is an empty directory (or a directory that contains | |
903 | * only empty directories), remove them. | |
904 | */ | |
905 | return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY); | |
906 | } | |
907 | ||
3b5d3c98 MH |
908 | static int create_reflock(const char *path, void *cb) |
909 | { | |
910 | struct lock_file *lk = cb; | |
911 | ||
4ff0f01c MH |
912 | return hold_lock_file_for_update_timeout( |
913 | lk, path, LOCK_NO_DEREF, | |
914 | get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0; | |
3b5d3c98 MH |
915 | } |
916 | ||
7bd9bcf3 MH |
917 | /* |
918 | * Locks a ref returning the lock on success and NULL on failure. | |
919 | * On failure errno is set to something meaningful. | |
920 | */ | |
4f01e508 | 921 | static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, |
922 | const char *refname, | |
923 | const struct object_id *old_oid, | |
924 | const struct string_list *extras, | |
925 | const struct string_list *skip, | |
926 | unsigned int flags, int *type, | |
927 | struct strbuf *err) | |
7bd9bcf3 MH |
928 | { |
929 | struct strbuf ref_file = STRBUF_INIT; | |
7bd9bcf3 MH |
930 | struct ref_lock *lock; |
931 | int last_errno = 0; | |
4f01e508 | 932 | int mustexist = (old_oid && !is_null_oid(old_oid)); |
7a418f3a | 933 | int resolve_flags = RESOLVE_REF_NO_RECURSE; |
7a418f3a | 934 | int resolved; |
7bd9bcf3 | 935 | |
4f01e508 | 936 | files_assert_main_repository(refs, "lock_ref_oid_basic"); |
7bd9bcf3 MH |
937 | assert(err); |
938 | ||
939 | lock = xcalloc(1, sizeof(struct ref_lock)); | |
940 | ||
941 | if (mustexist) | |
942 | resolve_flags |= RESOLVE_REF_READING; | |
2859dcd4 | 943 | if (flags & REF_DELETING) |
7bd9bcf3 | 944 | resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; |
7bd9bcf3 | 945 | |
19e02f4f | 946 | files_ref_path(refs, &ref_file, refname); |
2f40e954 NTND |
947 | resolved = !!refs_resolve_ref_unsafe(&refs->base, |
948 | refname, resolve_flags, | |
49e61479 | 949 | &lock->old_oid, type); |
7a418f3a | 950 | if (!resolved && errno == EISDIR) { |
7bd9bcf3 MH |
951 | /* |
952 | * we are trying to lock foo but we used to | |
953 | * have foo/bar which now does not exist; | |
954 | * it is normal for the empty directory 'foo' | |
955 | * to remain. | |
956 | */ | |
7a418f3a | 957 | if (remove_empty_directories(&ref_file)) { |
7bd9bcf3 | 958 | last_errno = errno; |
b05855b5 MH |
959 | if (!refs_verify_refname_available( |
960 | &refs->base, | |
961 | refname, extras, skip, err)) | |
7bd9bcf3 | 962 | strbuf_addf(err, "there are still refs under '%s'", |
7a418f3a | 963 | refname); |
7bd9bcf3 MH |
964 | goto error_return; |
965 | } | |
2f40e954 NTND |
966 | resolved = !!refs_resolve_ref_unsafe(&refs->base, |
967 | refname, resolve_flags, | |
49e61479 | 968 | &lock->old_oid, type); |
7bd9bcf3 | 969 | } |
7a418f3a | 970 | if (!resolved) { |
7bd9bcf3 MH |
971 | last_errno = errno; |
972 | if (last_errno != ENOTDIR || | |
b05855b5 MH |
973 | !refs_verify_refname_available(&refs->base, refname, |
974 | extras, skip, err)) | |
0568c8e9 | 975 | strbuf_addf(err, "unable to resolve reference '%s': %s", |
7a418f3a | 976 | refname, strerror(last_errno)); |
7bd9bcf3 MH |
977 | |
978 | goto error_return; | |
979 | } | |
2859dcd4 | 980 | |
7bd9bcf3 MH |
981 | /* |
982 | * If the ref did not exist and we are creating it, make sure | |
983 | * there is no existing packed ref whose name begins with our | |
984 | * refname, nor a packed ref whose name is a proper prefix of | |
985 | * our refname. | |
986 | */ | |
987 | if (is_null_oid(&lock->old_oid) && | |
8ec617c8 | 988 | refs_verify_refname_available(refs->packed_ref_store, refname, |
524a9fdb | 989 | extras, skip, err)) { |
7bd9bcf3 MH |
990 | last_errno = ENOTDIR; |
991 | goto error_return; | |
992 | } | |
993 | ||
7bd9bcf3 | 994 | lock->ref_name = xstrdup(refname); |
7bd9bcf3 | 995 | |
ee4d8e45 | 996 | if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) { |
7bd9bcf3 | 997 | last_errno = errno; |
3b5d3c98 | 998 | unable_to_lock_message(ref_file.buf, errno, err); |
7bd9bcf3 MH |
999 | goto error_return; |
1000 | } | |
1001 | ||
4f01e508 | 1002 | if (verify_lock(&refs->base, lock, old_oid, mustexist, err)) { |
7bd9bcf3 MH |
1003 | last_errno = errno; |
1004 | goto error_return; | |
1005 | } | |
1006 | goto out; | |
1007 | ||
1008 | error_return: | |
1009 | unlock_ref(lock); | |
1010 | lock = NULL; | |
1011 | ||
1012 | out: | |
1013 | strbuf_release(&ref_file); | |
7bd9bcf3 MH |
1014 | errno = last_errno; |
1015 | return lock; | |
1016 | } | |
1017 | ||
7bd9bcf3 MH |
1018 | struct ref_to_prune { |
1019 | struct ref_to_prune *next; | |
49e99588 | 1020 | struct object_id oid; |
7bd9bcf3 MH |
1021 | char name[FLEX_ARRAY]; |
1022 | }; | |
1023 | ||
a8f0db2d MH |
1024 | enum { |
1025 | REMOVE_EMPTY_PARENTS_REF = 0x01, | |
1026 | REMOVE_EMPTY_PARENTS_REFLOG = 0x02 | |
1027 | }; | |
1028 | ||
7bd9bcf3 | 1029 | /* |
a8f0db2d MH |
1030 | * Remove empty parent directories associated with the specified |
1031 | * reference and/or its reflog, but spare [logs/]refs/ and immediate | |
1032 | * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or | |
1033 | * REMOVE_EMPTY_PARENTS_REFLOG. | |
7bd9bcf3 | 1034 | */ |
802de3da NTND |
1035 | static void try_remove_empty_parents(struct files_ref_store *refs, |
1036 | const char *refname, | |
1037 | unsigned int flags) | |
7bd9bcf3 | 1038 | { |
8bdaecb4 | 1039 | struct strbuf buf = STRBUF_INIT; |
e9dcc305 | 1040 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 MH |
1041 | char *p, *q; |
1042 | int i; | |
8bdaecb4 MH |
1043 | |
1044 | strbuf_addstr(&buf, refname); | |
1045 | p = buf.buf; | |
7bd9bcf3 MH |
1046 | for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ |
1047 | while (*p && *p != '/') | |
1048 | p++; | |
1049 | /* tolerate duplicate slashes; see check_refname_format() */ | |
1050 | while (*p == '/') | |
1051 | p++; | |
1052 | } | |
8bdaecb4 | 1053 | q = buf.buf + buf.len; |
a8f0db2d | 1054 | while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) { |
7bd9bcf3 MH |
1055 | while (q > p && *q != '/') |
1056 | q--; | |
1057 | while (q > p && *(q-1) == '/') | |
1058 | q--; | |
1059 | if (q == p) | |
1060 | break; | |
8bdaecb4 | 1061 | strbuf_setlen(&buf, q - buf.buf); |
e9dcc305 NTND |
1062 | |
1063 | strbuf_reset(&sb); | |
19e02f4f | 1064 | files_ref_path(refs, &sb, buf.buf); |
e9dcc305 | 1065 | if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf)) |
a8f0db2d | 1066 | flags &= ~REMOVE_EMPTY_PARENTS_REF; |
e9dcc305 NTND |
1067 | |
1068 | strbuf_reset(&sb); | |
802de3da | 1069 | files_reflog_path(refs, &sb, buf.buf); |
e9dcc305 | 1070 | if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf)) |
a8f0db2d | 1071 | flags &= ~REMOVE_EMPTY_PARENTS_REFLOG; |
7bd9bcf3 | 1072 | } |
8bdaecb4 | 1073 | strbuf_release(&buf); |
e9dcc305 | 1074 | strbuf_release(&sb); |
7bd9bcf3 MH |
1075 | } |
1076 | ||
1077 | /* make sure nobody touched the ref, and unlink */ | |
2f40e954 | 1078 | static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r) |
7bd9bcf3 MH |
1079 | { |
1080 | struct ref_transaction *transaction; | |
1081 | struct strbuf err = STRBUF_INIT; | |
b00f3cfa | 1082 | int ret = -1; |
7bd9bcf3 MH |
1083 | |
1084 | if (check_refname_format(r->name, 0)) | |
1085 | return; | |
1086 | ||
2f40e954 | 1087 | transaction = ref_store_transaction_begin(&refs->base, &err); |
b00f3cfa MH |
1088 | if (!transaction) |
1089 | goto cleanup; | |
1090 | ref_transaction_add_update( | |
1091 | transaction, r->name, | |
acedcde7 | 1092 | REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING, |
b00f3cfa MH |
1093 | &null_oid, &r->oid, NULL); |
1094 | if (ref_transaction_commit(transaction, &err)) | |
1095 | goto cleanup; | |
1096 | ||
1097 | ret = 0; | |
1098 | ||
1099 | cleanup: | |
1100 | if (ret) | |
7bd9bcf3 | 1101 | error("%s", err.buf); |
7bd9bcf3 | 1102 | strbuf_release(&err); |
b00f3cfa MH |
1103 | ref_transaction_free(transaction); |
1104 | return; | |
7bd9bcf3 MH |
1105 | } |
1106 | ||
22b09cdf MH |
1107 | /* |
1108 | * Prune the loose versions of the references in the linked list | |
1109 | * `*refs_to_prune`, freeing the entries in the list as we go. | |
1110 | */ | |
1111 | static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune) | |
7bd9bcf3 | 1112 | { |
22b09cdf MH |
1113 | while (*refs_to_prune) { |
1114 | struct ref_to_prune *r = *refs_to_prune; | |
1115 | *refs_to_prune = r->next; | |
2f40e954 | 1116 | prune_ref(refs, r); |
22b09cdf | 1117 | free(r); |
7bd9bcf3 MH |
1118 | } |
1119 | } | |
1120 | ||
531cc4a5 MH |
1121 | /* |
1122 | * Return true if the specified reference should be packed. | |
1123 | */ | |
1124 | static int should_pack_ref(const char *refname, | |
1125 | const struct object_id *oid, unsigned int ref_flags, | |
1126 | unsigned int pack_flags) | |
1127 | { | |
1128 | /* Do not pack per-worktree refs: */ | |
1129 | if (ref_type(refname) != REF_TYPE_NORMAL) | |
1130 | return 0; | |
1131 | ||
1132 | /* Do not pack non-tags unless PACK_REFS_ALL is set: */ | |
1133 | if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/")) | |
1134 | return 0; | |
1135 | ||
1136 | /* Do not pack symbolic refs: */ | |
1137 | if (ref_flags & REF_ISSYMREF) | |
1138 | return 0; | |
1139 | ||
1140 | /* Do not pack broken refs: */ | |
1141 | if (!ref_resolves_to_object(refname, oid, ref_flags)) | |
1142 | return 0; | |
1143 | ||
1144 | return 1; | |
1145 | } | |
1146 | ||
8231527e | 1147 | static int files_pack_refs(struct ref_store *ref_store, unsigned int flags) |
7bd9bcf3 | 1148 | { |
00eebe35 | 1149 | struct files_ref_store *refs = |
9e7ec634 NTND |
1150 | files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, |
1151 | "pack_refs"); | |
50c2d855 | 1152 | struct ref_iterator *iter; |
50c2d855 MH |
1153 | int ok; |
1154 | struct ref_to_prune *refs_to_prune = NULL; | |
3478983b | 1155 | struct strbuf err = STRBUF_INIT; |
27d03d04 MH |
1156 | struct ref_transaction *transaction; |
1157 | ||
1158 | transaction = ref_store_transaction_begin(refs->packed_ref_store, &err); | |
1159 | if (!transaction) | |
1160 | return -1; | |
7bd9bcf3 | 1161 | |
c8bed835 | 1162 | packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err); |
50c2d855 MH |
1163 | |
1164 | iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0); | |
1165 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
1166 | /* | |
1167 | * If the loose reference can be packed, add an entry | |
1168 | * in the packed ref cache. If the reference should be | |
1169 | * pruned, also add it to refs_to_prune. | |
1170 | */ | |
531cc4a5 MH |
1171 | if (!should_pack_ref(iter->refname, iter->oid, iter->flags, |
1172 | flags)) | |
50c2d855 MH |
1173 | continue; |
1174 | ||
1175 | /* | |
27d03d04 MH |
1176 | * Add a reference creation for this reference to the |
1177 | * packed-refs transaction: | |
50c2d855 | 1178 | */ |
27d03d04 | 1179 | if (ref_transaction_update(transaction, iter->refname, |
89f3bbdd | 1180 | iter->oid, NULL, |
91774afc | 1181 | REF_NO_DEREF, NULL, &err)) |
27d03d04 MH |
1182 | die("failure preparing to create packed reference %s: %s", |
1183 | iter->refname, err.buf); | |
50c2d855 MH |
1184 | |
1185 | /* Schedule the loose reference for pruning if requested. */ | |
1186 | if ((flags & PACK_REFS_PRUNE)) { | |
1187 | struct ref_to_prune *n; | |
1188 | FLEX_ALLOC_STR(n, name, iter->refname); | |
49e99588 | 1189 | oidcpy(&n->oid, iter->oid); |
50c2d855 MH |
1190 | n->next = refs_to_prune; |
1191 | refs_to_prune = n; | |
1192 | } | |
1193 | } | |
1194 | if (ok != ITER_DONE) | |
1195 | die("error while iterating over references"); | |
7bd9bcf3 | 1196 | |
27d03d04 MH |
1197 | if (ref_transaction_commit(transaction, &err)) |
1198 | die("unable to write new packed-refs: %s", err.buf); | |
1199 | ||
1200 | ref_transaction_free(transaction); | |
1201 | ||
42c7f7ff | 1202 | packed_refs_unlock(refs->packed_ref_store); |
7bd9bcf3 | 1203 | |
22b09cdf | 1204 | prune_refs(refs, &refs_to_prune); |
3478983b | 1205 | strbuf_release(&err); |
7bd9bcf3 MH |
1206 | return 0; |
1207 | } | |
1208 | ||
64da4199 | 1209 | static int files_delete_refs(struct ref_store *ref_store, const char *msg, |
a27dcf89 | 1210 | struct string_list *refnames, unsigned int flags) |
7bd9bcf3 | 1211 | { |
0a95ac5f | 1212 | struct files_ref_store *refs = |
9e7ec634 | 1213 | files_downcast(ref_store, REF_STORE_WRITE, "delete_refs"); |
7bd9bcf3 MH |
1214 | struct strbuf err = STRBUF_INIT; |
1215 | int i, result = 0; | |
1216 | ||
1217 | if (!refnames->nr) | |
1218 | return 0; | |
1219 | ||
e5cc7d7d MH |
1220 | if (packed_refs_lock(refs->packed_ref_store, 0, &err)) |
1221 | goto error; | |
7bd9bcf3 | 1222 | |
2fb330ca | 1223 | if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) { |
e5cc7d7d MH |
1224 | packed_refs_unlock(refs->packed_ref_store); |
1225 | goto error; | |
7bd9bcf3 MH |
1226 | } |
1227 | ||
e5cc7d7d MH |
1228 | packed_refs_unlock(refs->packed_ref_store); |
1229 | ||
7bd9bcf3 MH |
1230 | for (i = 0; i < refnames->nr; i++) { |
1231 | const char *refname = refnames->items[i].string; | |
1232 | ||
64da4199 | 1233 | if (refs_delete_ref(&refs->base, msg, refname, NULL, flags)) |
7bd9bcf3 MH |
1234 | result |= error(_("could not remove reference %s"), refname); |
1235 | } | |
1236 | ||
7bd9bcf3 MH |
1237 | strbuf_release(&err); |
1238 | return result; | |
e5cc7d7d MH |
1239 | |
1240 | error: | |
1241 | /* | |
1242 | * If we failed to rewrite the packed-refs file, then it is | |
1243 | * unsafe to try to remove loose refs, because doing so might | |
1244 | * expose an obsolete packed value for a reference that might | |
1245 | * even point at an object that has been garbage collected. | |
1246 | */ | |
1247 | if (refnames->nr == 1) | |
1248 | error(_("could not delete reference %s: %s"), | |
1249 | refnames->items[0].string, err.buf); | |
1250 | else | |
1251 | error(_("could not delete references: %s"), err.buf); | |
1252 | ||
1253 | strbuf_release(&err); | |
1254 | return -1; | |
7bd9bcf3 MH |
1255 | } |
1256 | ||
1257 | /* | |
1258 | * People using contrib's git-new-workdir have .git/logs/refs -> | |
1259 | * /some/other/path/.git/logs/refs, and that may live on another device. | |
1260 | * | |
1261 | * IOW, to avoid cross device rename errors, the temporary renamed log must | |
1262 | * live into logs/refs. | |
1263 | */ | |
a5c1efd6 | 1264 | #define TMP_RENAMED_LOG "refs/.tmp-renamed-log" |
7bd9bcf3 | 1265 | |
e9dcc305 NTND |
1266 | struct rename_cb { |
1267 | const char *tmp_renamed_log; | |
1268 | int true_errno; | |
1269 | }; | |
1270 | ||
1271 | static int rename_tmp_log_callback(const char *path, void *cb_data) | |
7bd9bcf3 | 1272 | { |
e9dcc305 | 1273 | struct rename_cb *cb = cb_data; |
7bd9bcf3 | 1274 | |
e9dcc305 | 1275 | if (rename(cb->tmp_renamed_log, path)) { |
6a7f3631 MH |
1276 | /* |
1277 | * rename(a, b) when b is an existing directory ought | |
1278 | * to result in ISDIR, but Solaris 5.8 gives ENOTDIR. | |
1279 | * Sheesh. Record the true errno for error reporting, | |
1280 | * but report EISDIR to raceproof_create_file() so | |
1281 | * that it knows to retry. | |
1282 | */ | |
e9dcc305 | 1283 | cb->true_errno = errno; |
6a7f3631 MH |
1284 | if (errno == ENOTDIR) |
1285 | errno = EISDIR; | |
1286 | return -1; | |
1287 | } else { | |
1288 | return 0; | |
7bd9bcf3 | 1289 | } |
6a7f3631 | 1290 | } |
7bd9bcf3 | 1291 | |
802de3da | 1292 | static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname) |
6a7f3631 | 1293 | { |
e9dcc305 NTND |
1294 | struct strbuf path = STRBUF_INIT; |
1295 | struct strbuf tmp = STRBUF_INIT; | |
1296 | struct rename_cb cb; | |
1297 | int ret; | |
6a7f3631 | 1298 | |
802de3da NTND |
1299 | files_reflog_path(refs, &path, newrefname); |
1300 | files_reflog_path(refs, &tmp, TMP_RENAMED_LOG); | |
e9dcc305 NTND |
1301 | cb.tmp_renamed_log = tmp.buf; |
1302 | ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb); | |
6a7f3631 MH |
1303 | if (ret) { |
1304 | if (errno == EISDIR) | |
e9dcc305 | 1305 | error("directory not empty: %s", path.buf); |
6a7f3631 | 1306 | else |
990c98d2 | 1307 | error("unable to move logfile %s to %s: %s", |
e9dcc305 NTND |
1308 | tmp.buf, path.buf, |
1309 | strerror(cb.true_errno)); | |
7bd9bcf3 | 1310 | } |
6a7f3631 | 1311 | |
e9dcc305 NTND |
1312 | strbuf_release(&path); |
1313 | strbuf_release(&tmp); | |
7bd9bcf3 MH |
1314 | return ret; |
1315 | } | |
1316 | ||
7bd9bcf3 | 1317 | static int write_ref_to_lockfile(struct ref_lock *lock, |
4417df8c | 1318 | const struct object_id *oid, struct strbuf *err); |
f18a7892 MH |
1319 | static int commit_ref_update(struct files_ref_store *refs, |
1320 | struct ref_lock *lock, | |
4417df8c | 1321 | const struct object_id *oid, const char *logmsg, |
5d9b2de4 | 1322 | struct strbuf *err); |
7bd9bcf3 | 1323 | |
52d59cc6 | 1324 | static int files_copy_or_rename_ref(struct ref_store *ref_store, |
9b6b40d9 | 1325 | const char *oldrefname, const char *newrefname, |
52d59cc6 | 1326 | const char *logmsg, int copy) |
7bd9bcf3 | 1327 | { |
9b6b40d9 | 1328 | struct files_ref_store *refs = |
9e7ec634 | 1329 | files_downcast(ref_store, REF_STORE_WRITE, "rename_ref"); |
4417df8c | 1330 | struct object_id oid, orig_oid; |
7bd9bcf3 MH |
1331 | int flag = 0, logmoved = 0; |
1332 | struct ref_lock *lock; | |
1333 | struct stat loginfo; | |
e9dcc305 NTND |
1334 | struct strbuf sb_oldref = STRBUF_INIT; |
1335 | struct strbuf sb_newref = STRBUF_INIT; | |
1336 | struct strbuf tmp_renamed_log = STRBUF_INIT; | |
1337 | int log, ret; | |
7bd9bcf3 MH |
1338 | struct strbuf err = STRBUF_INIT; |
1339 | ||
802de3da NTND |
1340 | files_reflog_path(refs, &sb_oldref, oldrefname); |
1341 | files_reflog_path(refs, &sb_newref, newrefname); | |
1342 | files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG); | |
e9dcc305 NTND |
1343 | |
1344 | log = !lstat(sb_oldref.buf, &loginfo); | |
0a3f07d6 NTND |
1345 | if (log && S_ISLNK(loginfo.st_mode)) { |
1346 | ret = error("reflog for %s is a symlink", oldrefname); | |
1347 | goto out; | |
1348 | } | |
7bd9bcf3 | 1349 | |
2f40e954 NTND |
1350 | if (!refs_resolve_ref_unsafe(&refs->base, oldrefname, |
1351 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, | |
49e61479 | 1352 | &orig_oid, &flag)) { |
0a3f07d6 NTND |
1353 | ret = error("refname %s not found", oldrefname); |
1354 | goto out; | |
1355 | } | |
e711b1af | 1356 | |
0a3f07d6 | 1357 | if (flag & REF_ISSYMREF) { |
52d59cc6 SD |
1358 | if (copy) |
1359 | ret = error("refname %s is a symbolic ref, copying it is not supported", | |
1360 | oldrefname); | |
1361 | else | |
1362 | ret = error("refname %s is a symbolic ref, renaming it is not supported", | |
1363 | oldrefname); | |
0a3f07d6 NTND |
1364 | goto out; |
1365 | } | |
7d2df051 | 1366 | if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) { |
0a3f07d6 NTND |
1367 | ret = 1; |
1368 | goto out; | |
1369 | } | |
7bd9bcf3 | 1370 | |
52d59cc6 | 1371 | if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) { |
a5c1efd6 | 1372 | ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s", |
0a3f07d6 NTND |
1373 | oldrefname, strerror(errno)); |
1374 | goto out; | |
1375 | } | |
7bd9bcf3 | 1376 | |
52d59cc6 SD |
1377 | if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) { |
1378 | ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s", | |
1379 | oldrefname, strerror(errno)); | |
1380 | goto out; | |
1381 | } | |
1382 | ||
1383 | if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname, | |
91774afc | 1384 | &orig_oid, REF_NO_DEREF)) { |
7bd9bcf3 MH |
1385 | error("unable to delete old %s", oldrefname); |
1386 | goto rollback; | |
1387 | } | |
1388 | ||
12fd3496 | 1389 | /* |
4417df8c | 1390 | * Since we are doing a shallow lookup, oid is not the |
1391 | * correct value to pass to delete_ref as old_oid. But that | |
1392 | * doesn't matter, because an old_oid check wouldn't add to | |
12fd3496 DT |
1393 | * the safety anyway; we want to delete the reference whatever |
1394 | * its current value. | |
1395 | */ | |
52d59cc6 | 1396 | if (!copy && !refs_read_ref_full(&refs->base, newrefname, |
2f40e954 | 1397 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, |
34c290a6 | 1398 | &oid, NULL) && |
2f40e954 | 1399 | refs_delete_ref(&refs->base, NULL, newrefname, |
91774afc | 1400 | NULL, REF_NO_DEREF)) { |
58364324 | 1401 | if (errno == EISDIR) { |
7bd9bcf3 MH |
1402 | struct strbuf path = STRBUF_INIT; |
1403 | int result; | |
1404 | ||
19e02f4f | 1405 | files_ref_path(refs, &path, newrefname); |
7bd9bcf3 MH |
1406 | result = remove_empty_directories(&path); |
1407 | strbuf_release(&path); | |
1408 | ||
1409 | if (result) { | |
1410 | error("Directory not empty: %s", newrefname); | |
1411 | goto rollback; | |
1412 | } | |
1413 | } else { | |
1414 | error("unable to delete existing %s", newrefname); | |
1415 | goto rollback; | |
1416 | } | |
1417 | } | |
1418 | ||
802de3da | 1419 | if (log && rename_tmp_log(refs, newrefname)) |
7bd9bcf3 MH |
1420 | goto rollback; |
1421 | ||
1422 | logmoved = log; | |
1423 | ||
4f01e508 | 1424 | lock = lock_ref_oid_basic(refs, newrefname, NULL, NULL, NULL, |
91774afc | 1425 | REF_NO_DEREF, NULL, &err); |
7bd9bcf3 | 1426 | if (!lock) { |
52d59cc6 SD |
1427 | if (copy) |
1428 | error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf); | |
1429 | else | |
1430 | error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf); | |
7bd9bcf3 MH |
1431 | strbuf_release(&err); |
1432 | goto rollback; | |
1433 | } | |
4417df8c | 1434 | oidcpy(&lock->old_oid, &orig_oid); |
7bd9bcf3 | 1435 | |
4417df8c | 1436 | if (write_ref_to_lockfile(lock, &orig_oid, &err) || |
1437 | commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) { | |
7bd9bcf3 MH |
1438 | error("unable to write current sha1 into %s: %s", newrefname, err.buf); |
1439 | strbuf_release(&err); | |
1440 | goto rollback; | |
1441 | } | |
1442 | ||
0a3f07d6 NTND |
1443 | ret = 0; |
1444 | goto out; | |
7bd9bcf3 MH |
1445 | |
1446 | rollback: | |
4f01e508 | 1447 | lock = lock_ref_oid_basic(refs, oldrefname, NULL, NULL, NULL, |
91774afc | 1448 | REF_NO_DEREF, NULL, &err); |
7bd9bcf3 MH |
1449 | if (!lock) { |
1450 | error("unable to lock %s for rollback: %s", oldrefname, err.buf); | |
1451 | strbuf_release(&err); | |
1452 | goto rollbacklog; | |
1453 | } | |
1454 | ||
1455 | flag = log_all_ref_updates; | |
341fb286 | 1456 | log_all_ref_updates = LOG_REFS_NONE; |
4417df8c | 1457 | if (write_ref_to_lockfile(lock, &orig_oid, &err) || |
1458 | commit_ref_update(refs, lock, &orig_oid, NULL, &err)) { | |
7bd9bcf3 MH |
1459 | error("unable to write current sha1 into %s: %s", oldrefname, err.buf); |
1460 | strbuf_release(&err); | |
1461 | } | |
1462 | log_all_ref_updates = flag; | |
1463 | ||
1464 | rollbacklog: | |
e9dcc305 | 1465 | if (logmoved && rename(sb_newref.buf, sb_oldref.buf)) |
7bd9bcf3 MH |
1466 | error("unable to restore logfile %s from %s: %s", |
1467 | oldrefname, newrefname, strerror(errno)); | |
1468 | if (!logmoved && log && | |
e9dcc305 | 1469 | rename(tmp_renamed_log.buf, sb_oldref.buf)) |
a5c1efd6 | 1470 | error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s", |
7bd9bcf3 | 1471 | oldrefname, strerror(errno)); |
0a3f07d6 NTND |
1472 | ret = 1; |
1473 | out: | |
e9dcc305 NTND |
1474 | strbuf_release(&sb_newref); |
1475 | strbuf_release(&sb_oldref); | |
1476 | strbuf_release(&tmp_renamed_log); | |
1477 | ||
0a3f07d6 | 1478 | return ret; |
7bd9bcf3 MH |
1479 | } |
1480 | ||
52d59cc6 SD |
1481 | static int files_rename_ref(struct ref_store *ref_store, |
1482 | const char *oldrefname, const char *newrefname, | |
1483 | const char *logmsg) | |
1484 | { | |
1485 | return files_copy_or_rename_ref(ref_store, oldrefname, | |
1486 | newrefname, logmsg, 0); | |
1487 | } | |
1488 | ||
1489 | static int files_copy_ref(struct ref_store *ref_store, | |
1490 | const char *oldrefname, const char *newrefname, | |
1491 | const char *logmsg) | |
1492 | { | |
1493 | return files_copy_or_rename_ref(ref_store, oldrefname, | |
1494 | newrefname, logmsg, 1); | |
1495 | } | |
1496 | ||
83a3069a | 1497 | static int close_ref_gently(struct ref_lock *lock) |
7bd9bcf3 | 1498 | { |
ee4d8e45 | 1499 | if (close_lock_file_gently(&lock->lk)) |
7bd9bcf3 MH |
1500 | return -1; |
1501 | return 0; | |
1502 | } | |
1503 | ||
1504 | static int commit_ref(struct ref_lock *lock) | |
1505 | { | |
ee4d8e45 | 1506 | char *path = get_locked_file_path(&lock->lk); |
5387c0d8 MH |
1507 | struct stat st; |
1508 | ||
1509 | if (!lstat(path, &st) && S_ISDIR(st.st_mode)) { | |
1510 | /* | |
1511 | * There is a directory at the path we want to rename | |
1512 | * the lockfile to. Hopefully it is empty; try to | |
1513 | * delete it. | |
1514 | */ | |
1515 | size_t len = strlen(path); | |
1516 | struct strbuf sb_path = STRBUF_INIT; | |
1517 | ||
1518 | strbuf_attach(&sb_path, path, len, len); | |
1519 | ||
1520 | /* | |
1521 | * If this fails, commit_lock_file() will also fail | |
1522 | * and will report the problem. | |
1523 | */ | |
1524 | remove_empty_directories(&sb_path); | |
1525 | strbuf_release(&sb_path); | |
1526 | } else { | |
1527 | free(path); | |
1528 | } | |
1529 | ||
ee4d8e45 | 1530 | if (commit_lock_file(&lock->lk)) |
7bd9bcf3 MH |
1531 | return -1; |
1532 | return 0; | |
1533 | } | |
1534 | ||
1fb0c809 MH |
1535 | static int open_or_create_logfile(const char *path, void *cb) |
1536 | { | |
1537 | int *fd = cb; | |
1538 | ||
1539 | *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666); | |
1540 | return (*fd < 0) ? -1 : 0; | |
1541 | } | |
1542 | ||
7bd9bcf3 | 1543 | /* |
4533e534 MH |
1544 | * Create a reflog for a ref. If force_create = 0, only create the |
1545 | * reflog for certain refs (those for which should_autocreate_reflog | |
1546 | * returns non-zero). Otherwise, create it regardless of the reference | |
1547 | * name. If the logfile already existed or was created, return 0 and | |
1548 | * set *logfd to the file descriptor opened for appending to the file. | |
1549 | * If no logfile exists and we decided not to create one, return 0 and | |
1550 | * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and | |
1551 | * return -1. | |
7bd9bcf3 | 1552 | */ |
802de3da NTND |
1553 | static int log_ref_setup(struct files_ref_store *refs, |
1554 | const char *refname, int force_create, | |
4533e534 | 1555 | int *logfd, struct strbuf *err) |
7bd9bcf3 | 1556 | { |
802de3da NTND |
1557 | struct strbuf logfile_sb = STRBUF_INIT; |
1558 | char *logfile; | |
1559 | ||
1560 | files_reflog_path(refs, &logfile_sb, refname); | |
1561 | logfile = strbuf_detach(&logfile_sb, NULL); | |
7bd9bcf3 | 1562 | |
7bd9bcf3 | 1563 | if (force_create || should_autocreate_reflog(refname)) { |
4533e534 | 1564 | if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) { |
1fb0c809 MH |
1565 | if (errno == ENOENT) |
1566 | strbuf_addf(err, "unable to create directory for '%s': " | |
4533e534 | 1567 | "%s", logfile, strerror(errno)); |
1fb0c809 MH |
1568 | else if (errno == EISDIR) |
1569 | strbuf_addf(err, "there are still logs under '%s'", | |
4533e534 | 1570 | logfile); |
1fb0c809 | 1571 | else |
854bda6b | 1572 | strbuf_addf(err, "unable to append to '%s': %s", |
4533e534 | 1573 | logfile, strerror(errno)); |
7bd9bcf3 | 1574 | |
4533e534 | 1575 | goto error; |
7bd9bcf3 | 1576 | } |
854bda6b | 1577 | } else { |
4533e534 | 1578 | *logfd = open(logfile, O_APPEND | O_WRONLY, 0666); |
e404f459 | 1579 | if (*logfd < 0) { |
854bda6b MH |
1580 | if (errno == ENOENT || errno == EISDIR) { |
1581 | /* | |
1582 | * The logfile doesn't already exist, | |
1583 | * but that is not an error; it only | |
1584 | * means that we won't write log | |
1585 | * entries to it. | |
1586 | */ | |
1587 | ; | |
1588 | } else { | |
1589 | strbuf_addf(err, "unable to append to '%s': %s", | |
4533e534 MH |
1590 | logfile, strerror(errno)); |
1591 | goto error; | |
854bda6b | 1592 | } |
7bd9bcf3 MH |
1593 | } |
1594 | } | |
1595 | ||
e404f459 | 1596 | if (*logfd >= 0) |
4533e534 | 1597 | adjust_shared_perm(logfile); |
854bda6b | 1598 | |
4533e534 | 1599 | free(logfile); |
7bd9bcf3 | 1600 | return 0; |
7bd9bcf3 | 1601 | |
4533e534 MH |
1602 | error: |
1603 | free(logfile); | |
1604 | return -1; | |
7bd9bcf3 | 1605 | } |
7bd9bcf3 | 1606 | |
e3688bd6 DT |
1607 | static int files_create_reflog(struct ref_store *ref_store, |
1608 | const char *refname, int force_create, | |
1609 | struct strbuf *err) | |
7bd9bcf3 | 1610 | { |
802de3da | 1611 | struct files_ref_store *refs = |
9e7ec634 | 1612 | files_downcast(ref_store, REF_STORE_WRITE, "create_reflog"); |
e404f459 | 1613 | int fd; |
7bd9bcf3 | 1614 | |
802de3da | 1615 | if (log_ref_setup(refs, refname, force_create, &fd, err)) |
4533e534 MH |
1616 | return -1; |
1617 | ||
e404f459 MH |
1618 | if (fd >= 0) |
1619 | close(fd); | |
4533e534 MH |
1620 | |
1621 | return 0; | |
7bd9bcf3 MH |
1622 | } |
1623 | ||
4417df8c | 1624 | static int log_ref_write_fd(int fd, const struct object_id *old_oid, |
1625 | const struct object_id *new_oid, | |
7bd9bcf3 MH |
1626 | const char *committer, const char *msg) |
1627 | { | |
80a6c207 BP |
1628 | struct strbuf sb = STRBUF_INIT; |
1629 | int ret = 0; | |
1630 | ||
1631 | strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer); | |
1632 | if (msg && *msg) | |
1633 | copy_reflog_msg(&sb, msg); | |
1634 | strbuf_addch(&sb, '\n'); | |
1635 | if (write_in_full(fd, sb.buf, sb.len) < 0) | |
1636 | ret = -1; | |
1637 | strbuf_release(&sb); | |
1638 | return ret; | |
7bd9bcf3 MH |
1639 | } |
1640 | ||
802de3da | 1641 | static int files_log_ref_write(struct files_ref_store *refs, |
4417df8c | 1642 | const char *refname, const struct object_id *old_oid, |
1643 | const struct object_id *new_oid, const char *msg, | |
11f8457f | 1644 | int flags, struct strbuf *err) |
7bd9bcf3 | 1645 | { |
e404f459 | 1646 | int logfd, result; |
7bd9bcf3 | 1647 | |
341fb286 CW |
1648 | if (log_all_ref_updates == LOG_REFS_UNSET) |
1649 | log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL; | |
7bd9bcf3 | 1650 | |
802de3da NTND |
1651 | result = log_ref_setup(refs, refname, |
1652 | flags & REF_FORCE_CREATE_REFLOG, | |
4533e534 | 1653 | &logfd, err); |
7bd9bcf3 MH |
1654 | |
1655 | if (result) | |
1656 | return result; | |
1657 | ||
7bd9bcf3 MH |
1658 | if (logfd < 0) |
1659 | return 0; | |
4417df8c | 1660 | result = log_ref_write_fd(logfd, old_oid, new_oid, |
7bd9bcf3 MH |
1661 | git_committer_info(0), msg); |
1662 | if (result) { | |
e9dcc305 | 1663 | struct strbuf sb = STRBUF_INIT; |
87b21e05 MH |
1664 | int save_errno = errno; |
1665 | ||
802de3da | 1666 | files_reflog_path(refs, &sb, refname); |
87b21e05 | 1667 | strbuf_addf(err, "unable to append to '%s': %s", |
e9dcc305 NTND |
1668 | sb.buf, strerror(save_errno)); |
1669 | strbuf_release(&sb); | |
7bd9bcf3 MH |
1670 | close(logfd); |
1671 | return -1; | |
1672 | } | |
1673 | if (close(logfd)) { | |
e9dcc305 | 1674 | struct strbuf sb = STRBUF_INIT; |
87b21e05 MH |
1675 | int save_errno = errno; |
1676 | ||
802de3da | 1677 | files_reflog_path(refs, &sb, refname); |
87b21e05 | 1678 | strbuf_addf(err, "unable to append to '%s': %s", |
e9dcc305 NTND |
1679 | sb.buf, strerror(save_errno)); |
1680 | strbuf_release(&sb); | |
7bd9bcf3 MH |
1681 | return -1; |
1682 | } | |
1683 | return 0; | |
1684 | } | |
1685 | ||
7bd9bcf3 | 1686 | /* |
78fb4579 MH |
1687 | * Write oid into the open lockfile, then close the lockfile. On |
1688 | * errors, rollback the lockfile, fill in *err and return -1. | |
7bd9bcf3 MH |
1689 | */ |
1690 | static int write_ref_to_lockfile(struct ref_lock *lock, | |
4417df8c | 1691 | const struct object_id *oid, struct strbuf *err) |
7bd9bcf3 MH |
1692 | { |
1693 | static char term = '\n'; | |
1694 | struct object *o; | |
1695 | int fd; | |
1696 | ||
109cd76d | 1697 | o = parse_object(the_repository, oid); |
7bd9bcf3 MH |
1698 | if (!o) { |
1699 | strbuf_addf(err, | |
0568c8e9 | 1700 | "trying to write ref '%s' with nonexistent object %s", |
4417df8c | 1701 | lock->ref_name, oid_to_hex(oid)); |
7bd9bcf3 MH |
1702 | unlock_ref(lock); |
1703 | return -1; | |
1704 | } | |
1705 | if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) { | |
1706 | strbuf_addf(err, | |
0568c8e9 | 1707 | "trying to write non-commit object %s to branch '%s'", |
4417df8c | 1708 | oid_to_hex(oid), lock->ref_name); |
7bd9bcf3 MH |
1709 | unlock_ref(lock); |
1710 | return -1; | |
1711 | } | |
ee4d8e45 | 1712 | fd = get_lock_file_fd(&lock->lk); |
2ae2e2a1 | 1713 | if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 || |
06f46f23 | 1714 | write_in_full(fd, &term, 1) < 0 || |
83a3069a | 1715 | close_ref_gently(lock) < 0) { |
7bd9bcf3 | 1716 | strbuf_addf(err, |
ee4d8e45 | 1717 | "couldn't write '%s'", get_lock_file_path(&lock->lk)); |
7bd9bcf3 MH |
1718 | unlock_ref(lock); |
1719 | return -1; | |
1720 | } | |
1721 | return 0; | |
1722 | } | |
1723 | ||
1724 | /* | |
1725 | * Commit a change to a loose reference that has already been written | |
1726 | * to the loose reference lockfile. Also update the reflogs if | |
1727 | * necessary, using the specified lockmsg (which can be NULL). | |
1728 | */ | |
f18a7892 MH |
1729 | static int commit_ref_update(struct files_ref_store *refs, |
1730 | struct ref_lock *lock, | |
4417df8c | 1731 | const struct object_id *oid, const char *logmsg, |
5d9b2de4 | 1732 | struct strbuf *err) |
7bd9bcf3 | 1733 | { |
32c597e7 | 1734 | files_assert_main_repository(refs, "commit_ref_update"); |
00eebe35 MH |
1735 | |
1736 | clear_loose_ref_cache(refs); | |
802de3da | 1737 | if (files_log_ref_write(refs, lock->ref_name, |
4417df8c | 1738 | &lock->old_oid, oid, |
81b1b6d4 | 1739 | logmsg, 0, err)) { |
7bd9bcf3 | 1740 | char *old_msg = strbuf_detach(err, NULL); |
0568c8e9 | 1741 | strbuf_addf(err, "cannot update the ref '%s': %s", |
7bd9bcf3 MH |
1742 | lock->ref_name, old_msg); |
1743 | free(old_msg); | |
1744 | unlock_ref(lock); | |
1745 | return -1; | |
1746 | } | |
7a418f3a MH |
1747 | |
1748 | if (strcmp(lock->ref_name, "HEAD") != 0) { | |
7bd9bcf3 MH |
1749 | /* |
1750 | * Special hack: If a branch is updated directly and HEAD | |
1751 | * points to it (may happen on the remote side of a push | |
1752 | * for example) then logically the HEAD reflog should be | |
1753 | * updated too. | |
1754 | * A generic solution implies reverse symref information, | |
1755 | * but finding all symrefs pointing to the given branch | |
1756 | * would be rather costly for this rare event (the direct | |
1757 | * update of a branch) to be worth it. So let's cheat and | |
1758 | * check with HEAD only which should cover 99% of all usage | |
1759 | * scenarios (even 100% of the default ones). | |
1760 | */ | |
7bd9bcf3 MH |
1761 | int head_flag; |
1762 | const char *head_ref; | |
7a418f3a | 1763 | |
2f40e954 NTND |
1764 | head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD", |
1765 | RESOLVE_REF_READING, | |
e691b027 | 1766 | NULL, &head_flag); |
7bd9bcf3 MH |
1767 | if (head_ref && (head_flag & REF_ISSYMREF) && |
1768 | !strcmp(head_ref, lock->ref_name)) { | |
1769 | struct strbuf log_err = STRBUF_INIT; | |
802de3da | 1770 | if (files_log_ref_write(refs, "HEAD", |
4417df8c | 1771 | &lock->old_oid, oid, |
802de3da | 1772 | logmsg, 0, &log_err)) { |
7bd9bcf3 MH |
1773 | error("%s", log_err.buf); |
1774 | strbuf_release(&log_err); | |
1775 | } | |
1776 | } | |
1777 | } | |
7a418f3a | 1778 | |
7bd9bcf3 | 1779 | if (commit_ref(lock)) { |
0568c8e9 | 1780 | strbuf_addf(err, "couldn't set '%s'", lock->ref_name); |
7bd9bcf3 MH |
1781 | unlock_ref(lock); |
1782 | return -1; | |
1783 | } | |
1784 | ||
1785 | unlock_ref(lock); | |
1786 | return 0; | |
1787 | } | |
1788 | ||
370e5ad6 | 1789 | static int create_ref_symlink(struct ref_lock *lock, const char *target) |
7bd9bcf3 | 1790 | { |
370e5ad6 | 1791 | int ret = -1; |
7bd9bcf3 | 1792 | #ifndef NO_SYMLINK_HEAD |
ee4d8e45 | 1793 | char *ref_path = get_locked_file_path(&lock->lk); |
370e5ad6 JK |
1794 | unlink(ref_path); |
1795 | ret = symlink(target, ref_path); | |
1796 | free(ref_path); | |
1797 | ||
1798 | if (ret) | |
7bd9bcf3 | 1799 | fprintf(stderr, "no symlink - falling back to symbolic ref\n"); |
7bd9bcf3 | 1800 | #endif |
370e5ad6 JK |
1801 | return ret; |
1802 | } | |
7bd9bcf3 | 1803 | |
802de3da NTND |
1804 | static void update_symref_reflog(struct files_ref_store *refs, |
1805 | struct ref_lock *lock, const char *refname, | |
370e5ad6 JK |
1806 | const char *target, const char *logmsg) |
1807 | { | |
1808 | struct strbuf err = STRBUF_INIT; | |
4417df8c | 1809 | struct object_id new_oid; |
2f40e954 NTND |
1810 | if (logmsg && |
1811 | !refs_read_ref_full(&refs->base, target, | |
34c290a6 | 1812 | RESOLVE_REF_READING, &new_oid, NULL) && |
4417df8c | 1813 | files_log_ref_write(refs, refname, &lock->old_oid, |
1814 | &new_oid, logmsg, 0, &err)) { | |
7bd9bcf3 MH |
1815 | error("%s", err.buf); |
1816 | strbuf_release(&err); | |
1817 | } | |
370e5ad6 | 1818 | } |
7bd9bcf3 | 1819 | |
802de3da NTND |
1820 | static int create_symref_locked(struct files_ref_store *refs, |
1821 | struct ref_lock *lock, const char *refname, | |
370e5ad6 JK |
1822 | const char *target, const char *logmsg) |
1823 | { | |
1824 | if (prefer_symlink_refs && !create_ref_symlink(lock, target)) { | |
802de3da | 1825 | update_symref_reflog(refs, lock, refname, target, logmsg); |
370e5ad6 JK |
1826 | return 0; |
1827 | } | |
1828 | ||
ee4d8e45 | 1829 | if (!fdopen_lock_file(&lock->lk, "w")) |
370e5ad6 | 1830 | return error("unable to fdopen %s: %s", |
ee4d8e45 | 1831 | lock->lk.tempfile->filename.buf, strerror(errno)); |
370e5ad6 | 1832 | |
802de3da | 1833 | update_symref_reflog(refs, lock, refname, target, logmsg); |
396da8f7 | 1834 | |
370e5ad6 | 1835 | /* no error check; commit_ref will check ferror */ |
ee4d8e45 | 1836 | fprintf(lock->lk.tempfile->fp, "ref: %s\n", target); |
370e5ad6 JK |
1837 | if (commit_ref(lock) < 0) |
1838 | return error("unable to write symref for %s: %s", refname, | |
1839 | strerror(errno)); | |
7bd9bcf3 MH |
1840 | return 0; |
1841 | } | |
1842 | ||
284689ba MH |
1843 | static int files_create_symref(struct ref_store *ref_store, |
1844 | const char *refname, const char *target, | |
1845 | const char *logmsg) | |
370e5ad6 | 1846 | { |
7eb27cdf | 1847 | struct files_ref_store *refs = |
9e7ec634 | 1848 | files_downcast(ref_store, REF_STORE_WRITE, "create_symref"); |
370e5ad6 JK |
1849 | struct strbuf err = STRBUF_INIT; |
1850 | struct ref_lock *lock; | |
1851 | int ret; | |
1852 | ||
4f01e508 | 1853 | lock = lock_ref_oid_basic(refs, refname, NULL, |
91774afc | 1854 | NULL, NULL, REF_NO_DEREF, NULL, |
4f01e508 | 1855 | &err); |
370e5ad6 JK |
1856 | if (!lock) { |
1857 | error("%s", err.buf); | |
1858 | strbuf_release(&err); | |
1859 | return -1; | |
1860 | } | |
1861 | ||
802de3da | 1862 | ret = create_symref_locked(refs, lock, refname, target, logmsg); |
370e5ad6 JK |
1863 | unlock_ref(lock); |
1864 | return ret; | |
1865 | } | |
1866 | ||
e3688bd6 DT |
1867 | static int files_reflog_exists(struct ref_store *ref_store, |
1868 | const char *refname) | |
7bd9bcf3 | 1869 | { |
802de3da | 1870 | struct files_ref_store *refs = |
9e7ec634 | 1871 | files_downcast(ref_store, REF_STORE_READ, "reflog_exists"); |
e9dcc305 | 1872 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 | 1873 | struct stat st; |
e9dcc305 | 1874 | int ret; |
7bd9bcf3 | 1875 | |
802de3da | 1876 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
1877 | ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode); |
1878 | strbuf_release(&sb); | |
1879 | return ret; | |
7bd9bcf3 MH |
1880 | } |
1881 | ||
e3688bd6 DT |
1882 | static int files_delete_reflog(struct ref_store *ref_store, |
1883 | const char *refname) | |
7bd9bcf3 | 1884 | { |
802de3da | 1885 | struct files_ref_store *refs = |
9e7ec634 | 1886 | files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog"); |
e9dcc305 NTND |
1887 | struct strbuf sb = STRBUF_INIT; |
1888 | int ret; | |
1889 | ||
802de3da | 1890 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
1891 | ret = remove_path(sb.buf); |
1892 | strbuf_release(&sb); | |
1893 | return ret; | |
7bd9bcf3 MH |
1894 | } |
1895 | ||
1896 | static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data) | |
1897 | { | |
9461d272 | 1898 | struct object_id ooid, noid; |
7bd9bcf3 | 1899 | char *email_end, *message; |
dddbad72 | 1900 | timestamp_t timestamp; |
7bd9bcf3 | 1901 | int tz; |
43bc3b6c | 1902 | const char *p = sb->buf; |
7bd9bcf3 MH |
1903 | |
1904 | /* old SP new SP name <email> SP time TAB msg LF */ | |
43bc3b6c | 1905 | if (!sb->len || sb->buf[sb->len - 1] != '\n' || |
1906 | parse_oid_hex(p, &ooid, &p) || *p++ != ' ' || | |
1907 | parse_oid_hex(p, &noid, &p) || *p++ != ' ' || | |
1908 | !(email_end = strchr(p, '>')) || | |
7bd9bcf3 | 1909 | email_end[1] != ' ' || |
1aeb7e75 | 1910 | !(timestamp = parse_timestamp(email_end + 2, &message, 10)) || |
7bd9bcf3 MH |
1911 | !message || message[0] != ' ' || |
1912 | (message[1] != '+' && message[1] != '-') || | |
1913 | !isdigit(message[2]) || !isdigit(message[3]) || | |
1914 | !isdigit(message[4]) || !isdigit(message[5])) | |
1915 | return 0; /* corrupt? */ | |
1916 | email_end[1] = '\0'; | |
1917 | tz = strtol(message + 1, NULL, 10); | |
1918 | if (message[6] != '\t') | |
1919 | message += 6; | |
1920 | else | |
1921 | message += 7; | |
43bc3b6c | 1922 | return fn(&ooid, &noid, p, timestamp, tz, message, cb_data); |
7bd9bcf3 MH |
1923 | } |
1924 | ||
1925 | static char *find_beginning_of_line(char *bob, char *scan) | |
1926 | { | |
1927 | while (bob < scan && *(--scan) != '\n') | |
1928 | ; /* keep scanning backwards */ | |
1929 | /* | |
1930 | * Return either beginning of the buffer, or LF at the end of | |
1931 | * the previous line. | |
1932 | */ | |
1933 | return scan; | |
1934 | } | |
1935 | ||
e3688bd6 DT |
1936 | static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store, |
1937 | const char *refname, | |
1938 | each_reflog_ent_fn fn, | |
1939 | void *cb_data) | |
7bd9bcf3 | 1940 | { |
802de3da | 1941 | struct files_ref_store *refs = |
9e7ec634 NTND |
1942 | files_downcast(ref_store, REF_STORE_READ, |
1943 | "for_each_reflog_ent_reverse"); | |
7bd9bcf3 MH |
1944 | struct strbuf sb = STRBUF_INIT; |
1945 | FILE *logfp; | |
1946 | long pos; | |
1947 | int ret = 0, at_tail = 1; | |
1948 | ||
802de3da | 1949 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
1950 | logfp = fopen(sb.buf, "r"); |
1951 | strbuf_release(&sb); | |
7bd9bcf3 MH |
1952 | if (!logfp) |
1953 | return -1; | |
1954 | ||
1955 | /* Jump to the end */ | |
1956 | if (fseek(logfp, 0, SEEK_END) < 0) | |
be686f03 RS |
1957 | ret = error("cannot seek back reflog for %s: %s", |
1958 | refname, strerror(errno)); | |
7bd9bcf3 MH |
1959 | pos = ftell(logfp); |
1960 | while (!ret && 0 < pos) { | |
1961 | int cnt; | |
1962 | size_t nread; | |
1963 | char buf[BUFSIZ]; | |
1964 | char *endp, *scanp; | |
1965 | ||
1966 | /* Fill next block from the end */ | |
1967 | cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos; | |
be686f03 RS |
1968 | if (fseek(logfp, pos - cnt, SEEK_SET)) { |
1969 | ret = error("cannot seek back reflog for %s: %s", | |
1970 | refname, strerror(errno)); | |
1971 | break; | |
1972 | } | |
7bd9bcf3 | 1973 | nread = fread(buf, cnt, 1, logfp); |
be686f03 RS |
1974 | if (nread != 1) { |
1975 | ret = error("cannot read %d bytes from reflog for %s: %s", | |
1976 | cnt, refname, strerror(errno)); | |
1977 | break; | |
1978 | } | |
7bd9bcf3 MH |
1979 | pos -= cnt; |
1980 | ||
1981 | scanp = endp = buf + cnt; | |
1982 | if (at_tail && scanp[-1] == '\n') | |
1983 | /* Looking at the final LF at the end of the file */ | |
1984 | scanp--; | |
1985 | at_tail = 0; | |
1986 | ||
1987 | while (buf < scanp) { | |
1988 | /* | |
1989 | * terminating LF of the previous line, or the beginning | |
1990 | * of the buffer. | |
1991 | */ | |
1992 | char *bp; | |
1993 | ||
1994 | bp = find_beginning_of_line(buf, scanp); | |
1995 | ||
1996 | if (*bp == '\n') { | |
1997 | /* | |
1998 | * The newline is the end of the previous line, | |
1999 | * so we know we have complete line starting | |
2000 | * at (bp + 1). Prefix it onto any prior data | |
2001 | * we collected for the line and process it. | |
2002 | */ | |
2003 | strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1)); | |
2004 | scanp = bp; | |
2005 | endp = bp + 1; | |
2006 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
2007 | strbuf_reset(&sb); | |
2008 | if (ret) | |
2009 | break; | |
2010 | } else if (!pos) { | |
2011 | /* | |
2012 | * We are at the start of the buffer, and the | |
2013 | * start of the file; there is no previous | |
2014 | * line, and we have everything for this one. | |
2015 | * Process it, and we can end the loop. | |
2016 | */ | |
2017 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
2018 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
2019 | strbuf_reset(&sb); | |
2020 | break; | |
2021 | } | |
2022 | ||
2023 | if (bp == buf) { | |
2024 | /* | |
2025 | * We are at the start of the buffer, and there | |
2026 | * is more file to read backwards. Which means | |
2027 | * we are in the middle of a line. Note that we | |
2028 | * may get here even if *bp was a newline; that | |
2029 | * just means we are at the exact end of the | |
2030 | * previous line, rather than some spot in the | |
2031 | * middle. | |
2032 | * | |
2033 | * Save away what we have to be combined with | |
2034 | * the data from the next read. | |
2035 | */ | |
2036 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
2037 | break; | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | } | |
2042 | if (!ret && sb.len) | |
033abf97 | 2043 | BUG("reverse reflog parser had leftover data"); |
7bd9bcf3 MH |
2044 | |
2045 | fclose(logfp); | |
2046 | strbuf_release(&sb); | |
2047 | return ret; | |
2048 | } | |
2049 | ||
e3688bd6 DT |
2050 | static int files_for_each_reflog_ent(struct ref_store *ref_store, |
2051 | const char *refname, | |
2052 | each_reflog_ent_fn fn, void *cb_data) | |
7bd9bcf3 | 2053 | { |
802de3da | 2054 | struct files_ref_store *refs = |
9e7ec634 NTND |
2055 | files_downcast(ref_store, REF_STORE_READ, |
2056 | "for_each_reflog_ent"); | |
7bd9bcf3 MH |
2057 | FILE *logfp; |
2058 | struct strbuf sb = STRBUF_INIT; | |
2059 | int ret = 0; | |
2060 | ||
802de3da | 2061 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
2062 | logfp = fopen(sb.buf, "r"); |
2063 | strbuf_release(&sb); | |
7bd9bcf3 MH |
2064 | if (!logfp) |
2065 | return -1; | |
2066 | ||
2067 | while (!ret && !strbuf_getwholeline(&sb, logfp, '\n')) | |
2068 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
2069 | fclose(logfp); | |
2070 | strbuf_release(&sb); | |
2071 | return ret; | |
2072 | } | |
7bd9bcf3 | 2073 | |
2880d16f MH |
2074 | struct files_reflog_iterator { |
2075 | struct ref_iterator base; | |
7bd9bcf3 | 2076 | |
2f40e954 | 2077 | struct ref_store *ref_store; |
2880d16f MH |
2078 | struct dir_iterator *dir_iterator; |
2079 | struct object_id oid; | |
2080 | }; | |
7bd9bcf3 | 2081 | |
2880d16f MH |
2082 | static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator) |
2083 | { | |
2084 | struct files_reflog_iterator *iter = | |
2085 | (struct files_reflog_iterator *)ref_iterator; | |
2086 | struct dir_iterator *diter = iter->dir_iterator; | |
2087 | int ok; | |
2088 | ||
2089 | while ((ok = dir_iterator_advance(diter)) == ITER_OK) { | |
2090 | int flags; | |
2091 | ||
2092 | if (!S_ISREG(diter->st.st_mode)) | |
7bd9bcf3 | 2093 | continue; |
2880d16f MH |
2094 | if (diter->basename[0] == '.') |
2095 | continue; | |
2096 | if (ends_with(diter->basename, ".lock")) | |
7bd9bcf3 | 2097 | continue; |
7bd9bcf3 | 2098 | |
2f40e954 NTND |
2099 | if (refs_read_ref_full(iter->ref_store, |
2100 | diter->relative_path, 0, | |
34c290a6 | 2101 | &iter->oid, &flags)) { |
2880d16f MH |
2102 | error("bad ref for %s", diter->path.buf); |
2103 | continue; | |
7bd9bcf3 | 2104 | } |
2880d16f MH |
2105 | |
2106 | iter->base.refname = diter->relative_path; | |
2107 | iter->base.oid = &iter->oid; | |
2108 | iter->base.flags = flags; | |
2109 | return ITER_OK; | |
7bd9bcf3 | 2110 | } |
2880d16f MH |
2111 | |
2112 | iter->dir_iterator = NULL; | |
2113 | if (ref_iterator_abort(ref_iterator) == ITER_ERROR) | |
2114 | ok = ITER_ERROR; | |
2115 | return ok; | |
2116 | } | |
2117 | ||
2118 | static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator, | |
2119 | struct object_id *peeled) | |
2120 | { | |
033abf97 | 2121 | BUG("ref_iterator_peel() called for reflog_iterator"); |
2880d16f MH |
2122 | } |
2123 | ||
2124 | static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator) | |
2125 | { | |
2126 | struct files_reflog_iterator *iter = | |
2127 | (struct files_reflog_iterator *)ref_iterator; | |
2128 | int ok = ITER_DONE; | |
2129 | ||
2130 | if (iter->dir_iterator) | |
2131 | ok = dir_iterator_abort(iter->dir_iterator); | |
2132 | ||
2133 | base_ref_iterator_free(ref_iterator); | |
2134 | return ok; | |
2135 | } | |
2136 | ||
2137 | static struct ref_iterator_vtable files_reflog_iterator_vtable = { | |
2138 | files_reflog_iterator_advance, | |
2139 | files_reflog_iterator_peel, | |
2140 | files_reflog_iterator_abort | |
2141 | }; | |
2142 | ||
944b4e30 NTND |
2143 | static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store, |
2144 | const char *gitdir) | |
2880d16f | 2145 | { |
3012397e MT |
2146 | struct dir_iterator *diter; |
2147 | struct files_reflog_iterator *iter; | |
2148 | struct ref_iterator *ref_iterator; | |
e9dcc305 | 2149 | struct strbuf sb = STRBUF_INIT; |
2880d16f | 2150 | |
944b4e30 | 2151 | strbuf_addf(&sb, "%s/logs", gitdir); |
3012397e | 2152 | |
fa1da7d2 | 2153 | diter = dir_iterator_begin(sb.buf, 0); |
9b7b0295 RS |
2154 | if (!diter) { |
2155 | strbuf_release(&sb); | |
3012397e | 2156 | return empty_ref_iterator_begin(); |
9b7b0295 | 2157 | } |
3012397e MT |
2158 | |
2159 | iter = xcalloc(1, sizeof(*iter)); | |
2160 | ref_iterator = &iter->base; | |
2161 | ||
2162 | base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0); | |
2163 | iter->dir_iterator = diter; | |
2f40e954 | 2164 | iter->ref_store = ref_store; |
e9dcc305 | 2165 | strbuf_release(&sb); |
944b4e30 | 2166 | |
2880d16f | 2167 | return ref_iterator; |
7bd9bcf3 MH |
2168 | } |
2169 | ||
944b4e30 NTND |
2170 | static enum iterator_selection reflog_iterator_select( |
2171 | struct ref_iterator *iter_worktree, | |
2172 | struct ref_iterator *iter_common, | |
2173 | void *cb_data) | |
2174 | { | |
2175 | if (iter_worktree) { | |
2176 | /* | |
2177 | * We're a bit loose here. We probably should ignore | |
2178 | * common refs if they are accidentally added as | |
2179 | * per-worktree refs. | |
2180 | */ | |
2181 | return ITER_SELECT_0; | |
2182 | } else if (iter_common) { | |
2183 | if (ref_type(iter_common->refname) == REF_TYPE_NORMAL) | |
2184 | return ITER_SELECT_1; | |
2185 | ||
2186 | /* | |
2187 | * The main ref store may contain main worktree's | |
2188 | * per-worktree refs, which should be ignored | |
2189 | */ | |
2190 | return ITER_SKIP_1; | |
2191 | } else | |
2192 | return ITER_DONE; | |
2193 | } | |
2194 | ||
2195 | static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store) | |
2196 | { | |
2197 | struct files_ref_store *refs = | |
2198 | files_downcast(ref_store, REF_STORE_READ, | |
2199 | "reflog_iterator_begin"); | |
2200 | ||
2201 | if (!strcmp(refs->gitdir, refs->gitcommondir)) { | |
2202 | return reflog_iterator_begin(ref_store, refs->gitcommondir); | |
2203 | } else { | |
2204 | return merge_ref_iterator_begin( | |
1a2e1a76 | 2205 | 0, |
944b4e30 NTND |
2206 | reflog_iterator_begin(ref_store, refs->gitdir), |
2207 | reflog_iterator_begin(ref_store, refs->gitcommondir), | |
2208 | reflog_iterator_select, refs); | |
2209 | } | |
2210 | } | |
2211 | ||
165056b2 | 2212 | /* |
92b1551b MH |
2213 | * If update is a direct update of head_ref (the reference pointed to |
2214 | * by HEAD), then add an extra REF_LOG_ONLY update for HEAD. | |
2215 | */ | |
2216 | static int split_head_update(struct ref_update *update, | |
2217 | struct ref_transaction *transaction, | |
2218 | const char *head_ref, | |
2219 | struct string_list *affected_refnames, | |
2220 | struct strbuf *err) | |
2221 | { | |
2222 | struct string_list_item *item; | |
2223 | struct ref_update *new_update; | |
2224 | ||
2225 | if ((update->flags & REF_LOG_ONLY) || | |
acedcde7 | 2226 | (update->flags & REF_IS_PRUNING) || |
92b1551b MH |
2227 | (update->flags & REF_UPDATE_VIA_HEAD)) |
2228 | return 0; | |
2229 | ||
2230 | if (strcmp(update->refname, head_ref)) | |
2231 | return 0; | |
2232 | ||
2233 | /* | |
2234 | * First make sure that HEAD is not already in the | |
276d0e35 | 2235 | * transaction. This check is O(lg N) in the transaction |
92b1551b MH |
2236 | * size, but it happens at most once per transaction. |
2237 | */ | |
276d0e35 | 2238 | if (string_list_has_string(affected_refnames, "HEAD")) { |
92b1551b MH |
2239 | /* An entry already existed */ |
2240 | strbuf_addf(err, | |
2241 | "multiple updates for 'HEAD' (including one " | |
2242 | "via its referent '%s') are not allowed", | |
2243 | update->refname); | |
2244 | return TRANSACTION_NAME_CONFLICT; | |
2245 | } | |
2246 | ||
2247 | new_update = ref_transaction_add_update( | |
2248 | transaction, "HEAD", | |
91774afc | 2249 | update->flags | REF_LOG_ONLY | REF_NO_DEREF, |
89f3bbdd | 2250 | &update->new_oid, &update->old_oid, |
92b1551b MH |
2251 | update->msg); |
2252 | ||
276d0e35 MÅ |
2253 | /* |
2254 | * Add "HEAD". This insertion is O(N) in the transaction | |
2255 | * size, but it happens at most once per transaction. | |
2256 | * Add new_update->refname instead of a literal "HEAD". | |
2257 | */ | |
2258 | if (strcmp(new_update->refname, "HEAD")) | |
2259 | BUG("%s unexpectedly not 'HEAD'", new_update->refname); | |
2260 | item = string_list_insert(affected_refnames, new_update->refname); | |
92b1551b MH |
2261 | item->util = new_update; |
2262 | ||
2263 | return 0; | |
2264 | } | |
2265 | ||
2266 | /* | |
2267 | * update is for a symref that points at referent and doesn't have | |
91774afc MH |
2268 | * REF_NO_DEREF set. Split it into two updates: |
2269 | * - The original update, but with REF_LOG_ONLY and REF_NO_DEREF set | |
92b1551b MH |
2270 | * - A new, separate update for the referent reference |
2271 | * Note that the new update will itself be subject to splitting when | |
2272 | * the iteration gets to it. | |
2273 | */ | |
10dee40e | 2274 | static int split_symref_update(struct ref_update *update, |
92b1551b MH |
2275 | const char *referent, |
2276 | struct ref_transaction *transaction, | |
2277 | struct string_list *affected_refnames, | |
2278 | struct strbuf *err) | |
2279 | { | |
2280 | struct string_list_item *item; | |
2281 | struct ref_update *new_update; | |
2282 | unsigned int new_flags; | |
2283 | ||
2284 | /* | |
2285 | * First make sure that referent is not already in the | |
c299468b | 2286 | * transaction. This check is O(lg N) in the transaction |
92b1551b MH |
2287 | * size, but it happens at most once per symref in a |
2288 | * transaction. | |
2289 | */ | |
c299468b MÅ |
2290 | if (string_list_has_string(affected_refnames, referent)) { |
2291 | /* An entry already exists */ | |
92b1551b MH |
2292 | strbuf_addf(err, |
2293 | "multiple updates for '%s' (including one " | |
2294 | "via symref '%s') are not allowed", | |
2295 | referent, update->refname); | |
2296 | return TRANSACTION_NAME_CONFLICT; | |
2297 | } | |
2298 | ||
2299 | new_flags = update->flags; | |
2300 | if (!strcmp(update->refname, "HEAD")) { | |
2301 | /* | |
2302 | * Record that the new update came via HEAD, so that | |
2303 | * when we process it, split_head_update() doesn't try | |
2304 | * to add another reflog update for HEAD. Note that | |
2305 | * this bit will be propagated if the new_update | |
2306 | * itself needs to be split. | |
2307 | */ | |
2308 | new_flags |= REF_UPDATE_VIA_HEAD; | |
2309 | } | |
2310 | ||
2311 | new_update = ref_transaction_add_update( | |
2312 | transaction, referent, new_flags, | |
89f3bbdd | 2313 | &update->new_oid, &update->old_oid, |
92b1551b MH |
2314 | update->msg); |
2315 | ||
6e30b2f6 MH |
2316 | new_update->parent_update = update; |
2317 | ||
2318 | /* | |
2319 | * Change the symbolic ref update to log only. Also, it | |
78fb4579 | 2320 | * doesn't need to check its old OID value, as that will be |
6e30b2f6 MH |
2321 | * done when new_update is processed. |
2322 | */ | |
91774afc | 2323 | update->flags |= REF_LOG_ONLY | REF_NO_DEREF; |
6e30b2f6 | 2324 | update->flags &= ~REF_HAVE_OLD; |
92b1551b | 2325 | |
c299468b MÅ |
2326 | /* |
2327 | * Add the referent. This insertion is O(N) in the transaction | |
2328 | * size, but it happens at most once per symref in a | |
2329 | * transaction. Make sure to add new_update->refname, which will | |
2330 | * be valid as long as affected_refnames is in use, and NOT | |
2331 | * referent, which might soon be freed by our caller. | |
2332 | */ | |
2333 | item = string_list_insert(affected_refnames, new_update->refname); | |
2334 | if (item->util) | |
2335 | BUG("%s unexpectedly found in affected_refnames", | |
2336 | new_update->refname); | |
92b1551b MH |
2337 | item->util = new_update; |
2338 | ||
2339 | return 0; | |
2340 | } | |
2341 | ||
6e30b2f6 MH |
2342 | /* |
2343 | * Return the refname under which update was originally requested. | |
2344 | */ | |
2345 | static const char *original_update_refname(struct ref_update *update) | |
2346 | { | |
2347 | while (update->parent_update) | |
2348 | update = update->parent_update; | |
2349 | ||
2350 | return update->refname; | |
2351 | } | |
2352 | ||
e3f51039 MH |
2353 | /* |
2354 | * Check whether the REF_HAVE_OLD and old_oid values stored in update | |
2355 | * are consistent with oid, which is the reference's current value. If | |
2356 | * everything is OK, return 0; otherwise, write an error message to | |
2357 | * err and return -1. | |
2358 | */ | |
2359 | static int check_old_oid(struct ref_update *update, struct object_id *oid, | |
2360 | struct strbuf *err) | |
2361 | { | |
2362 | if (!(update->flags & REF_HAVE_OLD) || | |
4a7e27e9 | 2363 | oideq(oid, &update->old_oid)) |
e3f51039 MH |
2364 | return 0; |
2365 | ||
98491298 | 2366 | if (is_null_oid(&update->old_oid)) |
e3f51039 MH |
2367 | strbuf_addf(err, "cannot lock ref '%s': " |
2368 | "reference already exists", | |
2369 | original_update_refname(update)); | |
2370 | else if (is_null_oid(oid)) | |
2371 | strbuf_addf(err, "cannot lock ref '%s': " | |
2372 | "reference is missing but expected %s", | |
2373 | original_update_refname(update), | |
98491298 | 2374 | oid_to_hex(&update->old_oid)); |
e3f51039 MH |
2375 | else |
2376 | strbuf_addf(err, "cannot lock ref '%s': " | |
2377 | "is at %s but expected %s", | |
2378 | original_update_refname(update), | |
2379 | oid_to_hex(oid), | |
98491298 | 2380 | oid_to_hex(&update->old_oid)); |
e3f51039 MH |
2381 | |
2382 | return -1; | |
2383 | } | |
2384 | ||
92b1551b MH |
2385 | /* |
2386 | * Prepare for carrying out update: | |
2387 | * - Lock the reference referred to by update. | |
2388 | * - Read the reference under lock. | |
78fb4579 | 2389 | * - Check that its old OID value (if specified) is correct, and in |
92b1551b MH |
2390 | * any case record it in update->lock->old_oid for later use when |
2391 | * writing the reflog. | |
91774afc | 2392 | * - If it is a symref update without REF_NO_DEREF, split it up into a |
92b1551b MH |
2393 | * REF_LOG_ONLY update of the symref and add a separate update for |
2394 | * the referent to transaction. | |
2395 | * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY | |
2396 | * update of HEAD. | |
165056b2 | 2397 | */ |
b3bbbc5c MH |
2398 | static int lock_ref_for_update(struct files_ref_store *refs, |
2399 | struct ref_update *update, | |
165056b2 | 2400 | struct ref_transaction *transaction, |
92b1551b | 2401 | const char *head_ref, |
165056b2 MH |
2402 | struct string_list *affected_refnames, |
2403 | struct strbuf *err) | |
2404 | { | |
92b1551b MH |
2405 | struct strbuf referent = STRBUF_INIT; |
2406 | int mustexist = (update->flags & REF_HAVE_OLD) && | |
98491298 | 2407 | !is_null_oid(&update->old_oid); |
851e1fbd | 2408 | int ret = 0; |
92b1551b | 2409 | struct ref_lock *lock; |
165056b2 | 2410 | |
32c597e7 | 2411 | files_assert_main_repository(refs, "lock_ref_for_update"); |
b3bbbc5c | 2412 | |
98491298 | 2413 | if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid)) |
165056b2 | 2414 | update->flags |= REF_DELETING; |
92b1551b MH |
2415 | |
2416 | if (head_ref) { | |
2417 | ret = split_head_update(update, transaction, head_ref, | |
2418 | affected_refnames, err); | |
2419 | if (ret) | |
851e1fbd | 2420 | goto out; |
92b1551b MH |
2421 | } |
2422 | ||
f7b0a987 | 2423 | ret = lock_raw_ref(refs, update->refname, mustexist, |
92b1551b | 2424 | affected_refnames, NULL, |
7d618264 | 2425 | &lock, &referent, |
92b1551b | 2426 | &update->type, err); |
92b1551b | 2427 | if (ret) { |
165056b2 MH |
2428 | char *reason; |
2429 | ||
165056b2 MH |
2430 | reason = strbuf_detach(err, NULL); |
2431 | strbuf_addf(err, "cannot lock ref '%s': %s", | |
e3f51039 | 2432 | original_update_refname(update), reason); |
165056b2 | 2433 | free(reason); |
851e1fbd | 2434 | goto out; |
165056b2 | 2435 | } |
92b1551b | 2436 | |
7d618264 | 2437 | update->backend_data = lock; |
92b1551b | 2438 | |
8169d0d0 | 2439 | if (update->type & REF_ISSYMREF) { |
91774afc | 2440 | if (update->flags & REF_NO_DEREF) { |
6e30b2f6 MH |
2441 | /* |
2442 | * We won't be reading the referent as part of | |
2443 | * the transaction, so we have to read it here | |
78fb4579 | 2444 | * to record and possibly check old_oid: |
6e30b2f6 | 2445 | */ |
2f40e954 NTND |
2446 | if (refs_read_ref_full(&refs->base, |
2447 | referent.buf, 0, | |
34c290a6 | 2448 | &lock->old_oid, NULL)) { |
6e30b2f6 MH |
2449 | if (update->flags & REF_HAVE_OLD) { |
2450 | strbuf_addf(err, "cannot lock ref '%s': " | |
e3f51039 MH |
2451 | "error reading reference", |
2452 | original_update_refname(update)); | |
3f5ef95b | 2453 | ret = TRANSACTION_GENERIC_ERROR; |
851e1fbd | 2454 | goto out; |
6e30b2f6 | 2455 | } |
e3f51039 | 2456 | } else if (check_old_oid(update, &lock->old_oid, err)) { |
851e1fbd MÅ |
2457 | ret = TRANSACTION_GENERIC_ERROR; |
2458 | goto out; | |
8169d0d0 | 2459 | } |
6e30b2f6 MH |
2460 | } else { |
2461 | /* | |
2462 | * Create a new update for the reference this | |
2463 | * symref is pointing at. Also, we will record | |
78fb4579 | 2464 | * and verify old_oid for this update as part |
6e30b2f6 MH |
2465 | * of processing the split-off update, so we |
2466 | * don't have to do it here. | |
2467 | */ | |
10dee40e | 2468 | ret = split_symref_update(update, |
fcc42ea0 | 2469 | referent.buf, transaction, |
92b1551b MH |
2470 | affected_refnames, err); |
2471 | if (ret) | |
851e1fbd | 2472 | goto out; |
92b1551b | 2473 | } |
6e30b2f6 MH |
2474 | } else { |
2475 | struct ref_update *parent_update; | |
8169d0d0 | 2476 | |
851e1fbd MÅ |
2477 | if (check_old_oid(update, &lock->old_oid, err)) { |
2478 | ret = TRANSACTION_GENERIC_ERROR; | |
2479 | goto out; | |
2480 | } | |
e3f51039 | 2481 | |
6e30b2f6 MH |
2482 | /* |
2483 | * If this update is happening indirectly because of a | |
78fb4579 | 2484 | * symref update, record the old OID in the parent |
6e30b2f6 MH |
2485 | * update: |
2486 | */ | |
2487 | for (parent_update = update->parent_update; | |
2488 | parent_update; | |
2489 | parent_update = parent_update->parent_update) { | |
7d618264 DT |
2490 | struct ref_lock *parent_lock = parent_update->backend_data; |
2491 | oidcpy(&parent_lock->old_oid, &lock->old_oid); | |
6e30b2f6 | 2492 | } |
92b1551b MH |
2493 | } |
2494 | ||
165056b2 MH |
2495 | if ((update->flags & REF_HAVE_NEW) && |
2496 | !(update->flags & REF_DELETING) && | |
2497 | !(update->flags & REF_LOG_ONLY)) { | |
92b1551b | 2498 | if (!(update->type & REF_ISSYMREF) && |
4a7e27e9 | 2499 | oideq(&lock->old_oid, &update->new_oid)) { |
165056b2 MH |
2500 | /* |
2501 | * The reference already has the desired | |
2502 | * value, so we don't need to write it. | |
2503 | */ | |
4417df8c | 2504 | } else if (write_ref_to_lockfile(lock, &update->new_oid, |
165056b2 MH |
2505 | err)) { |
2506 | char *write_err = strbuf_detach(err, NULL); | |
2507 | ||
2508 | /* | |
2509 | * The lock was freed upon failure of | |
2510 | * write_ref_to_lockfile(): | |
2511 | */ | |
7d618264 | 2512 | update->backend_data = NULL; |
165056b2 | 2513 | strbuf_addf(err, |
e3f51039 | 2514 | "cannot update ref '%s': %s", |
165056b2 MH |
2515 | update->refname, write_err); |
2516 | free(write_err); | |
851e1fbd MÅ |
2517 | ret = TRANSACTION_GENERIC_ERROR; |
2518 | goto out; | |
165056b2 MH |
2519 | } else { |
2520 | update->flags |= REF_NEEDS_COMMIT; | |
2521 | } | |
2522 | } | |
2523 | if (!(update->flags & REF_NEEDS_COMMIT)) { | |
2524 | /* | |
2525 | * We didn't call write_ref_to_lockfile(), so | |
2526 | * the lockfile is still open. Close it to | |
2527 | * free up the file descriptor: | |
2528 | */ | |
83a3069a | 2529 | if (close_ref_gently(lock)) { |
165056b2 MH |
2530 | strbuf_addf(err, "couldn't close '%s.lock'", |
2531 | update->refname); | |
851e1fbd MÅ |
2532 | ret = TRANSACTION_GENERIC_ERROR; |
2533 | goto out; | |
165056b2 MH |
2534 | } |
2535 | } | |
851e1fbd MÅ |
2536 | |
2537 | out: | |
2538 | strbuf_release(&referent); | |
2539 | return ret; | |
165056b2 MH |
2540 | } |
2541 | ||
dc39e099 MH |
2542 | struct files_transaction_backend_data { |
2543 | struct ref_transaction *packed_transaction; | |
2544 | int packed_refs_locked; | |
2545 | }; | |
2546 | ||
c0ca9357 MH |
2547 | /* |
2548 | * Unlock any references in `transaction` that are still locked, and | |
2549 | * mark the transaction closed. | |
2550 | */ | |
dc39e099 MH |
2551 | static void files_transaction_cleanup(struct files_ref_store *refs, |
2552 | struct ref_transaction *transaction) | |
c0ca9357 MH |
2553 | { |
2554 | size_t i; | |
dc39e099 MH |
2555 | struct files_transaction_backend_data *backend_data = |
2556 | transaction->backend_data; | |
2557 | struct strbuf err = STRBUF_INIT; | |
c0ca9357 MH |
2558 | |
2559 | for (i = 0; i < transaction->nr; i++) { | |
2560 | struct ref_update *update = transaction->updates[i]; | |
2561 | struct ref_lock *lock = update->backend_data; | |
2562 | ||
2563 | if (lock) { | |
2564 | unlock_ref(lock); | |
2565 | update->backend_data = NULL; | |
2566 | } | |
2567 | } | |
2568 | ||
dc39e099 MH |
2569 | if (backend_data->packed_transaction && |
2570 | ref_transaction_abort(backend_data->packed_transaction, &err)) { | |
2571 | error("error aborting transaction: %s", err.buf); | |
2572 | strbuf_release(&err); | |
2573 | } | |
2574 | ||
2575 | if (backend_data->packed_refs_locked) | |
2576 | packed_refs_unlock(refs->packed_ref_store); | |
2577 | ||
2578 | free(backend_data); | |
2579 | ||
c0ca9357 MH |
2580 | transaction->state = REF_TRANSACTION_CLOSED; |
2581 | } | |
2582 | ||
30173b88 MH |
2583 | static int files_transaction_prepare(struct ref_store *ref_store, |
2584 | struct ref_transaction *transaction, | |
2585 | struct strbuf *err) | |
7bd9bcf3 | 2586 | { |
00eebe35 | 2587 | struct files_ref_store *refs = |
9e7ec634 | 2588 | files_downcast(ref_store, REF_STORE_WRITE, |
30173b88 | 2589 | "ref_transaction_prepare"); |
43a2dfde MH |
2590 | size_t i; |
2591 | int ret = 0; | |
7bd9bcf3 | 2592 | struct string_list affected_refnames = STRING_LIST_INIT_NODUP; |
92b1551b MH |
2593 | char *head_ref = NULL; |
2594 | int head_type; | |
dc39e099 MH |
2595 | struct files_transaction_backend_data *backend_data; |
2596 | struct ref_transaction *packed_transaction = NULL; | |
7bd9bcf3 MH |
2597 | |
2598 | assert(err); | |
2599 | ||
c0ca9357 MH |
2600 | if (!transaction->nr) |
2601 | goto cleanup; | |
7bd9bcf3 | 2602 | |
dc39e099 MH |
2603 | backend_data = xcalloc(1, sizeof(*backend_data)); |
2604 | transaction->backend_data = backend_data; | |
2605 | ||
92b1551b MH |
2606 | /* |
2607 | * Fail if a refname appears more than once in the | |
2608 | * transaction. (If we end up splitting up any updates using | |
2609 | * split_symref_update() or split_head_update(), those | |
2610 | * functions will check that the new updates don't have the | |
62c72d1f | 2611 | * same refname as any existing ones.) Also fail if any of the |
acedcde7 | 2612 | * updates use REF_IS_PRUNING without REF_NO_DEREF. |
92b1551b MH |
2613 | */ |
2614 | for (i = 0; i < transaction->nr; i++) { | |
2615 | struct ref_update *update = transaction->updates[i]; | |
2616 | struct string_list_item *item = | |
2617 | string_list_append(&affected_refnames, update->refname); | |
2618 | ||
acedcde7 | 2619 | if ((update->flags & REF_IS_PRUNING) && |
91774afc | 2620 | !(update->flags & REF_NO_DEREF)) |
acedcde7 | 2621 | BUG("REF_IS_PRUNING set without REF_NO_DEREF"); |
62c72d1f | 2622 | |
92b1551b MH |
2623 | /* |
2624 | * We store a pointer to update in item->util, but at | |
2625 | * the moment we never use the value of this field | |
2626 | * except to check whether it is non-NULL. | |
2627 | */ | |
2628 | item->util = update; | |
2629 | } | |
7bd9bcf3 MH |
2630 | string_list_sort(&affected_refnames); |
2631 | if (ref_update_reject_duplicates(&affected_refnames, err)) { | |
2632 | ret = TRANSACTION_GENERIC_ERROR; | |
2633 | goto cleanup; | |
2634 | } | |
2635 | ||
92b1551b MH |
2636 | /* |
2637 | * Special hack: If a branch is updated directly and HEAD | |
2638 | * points to it (may happen on the remote side of a push | |
2639 | * for example) then logically the HEAD reflog should be | |
2640 | * updated too. | |
2641 | * | |
2642 | * A generic solution would require reverse symref lookups, | |
2643 | * but finding all symrefs pointing to a given branch would be | |
2644 | * rather costly for this rare event (the direct update of a | |
2645 | * branch) to be worth it. So let's cheat and check with HEAD | |
2646 | * only, which should cover 99% of all usage scenarios (even | |
2647 | * 100% of the default ones). | |
2648 | * | |
2649 | * So if HEAD is a symbolic reference, then record the name of | |
2650 | * the reference that it points to. If we see an update of | |
2651 | * head_ref within the transaction, then split_head_update() | |
2652 | * arranges for the reflog of HEAD to be updated, too. | |
2653 | */ | |
2f40e954 NTND |
2654 | head_ref = refs_resolve_refdup(ref_store, "HEAD", |
2655 | RESOLVE_REF_NO_RECURSE, | |
872ccb2c | 2656 | NULL, &head_type); |
92b1551b MH |
2657 | |
2658 | if (head_ref && !(head_type & REF_ISSYMREF)) { | |
6a83d902 | 2659 | FREE_AND_NULL(head_ref); |
92b1551b MH |
2660 | } |
2661 | ||
7bd9bcf3 MH |
2662 | /* |
2663 | * Acquire all locks, verify old values if provided, check | |
2664 | * that new values are valid, and write new values to the | |
2665 | * lockfiles, ready to be activated. Only keep one lockfile | |
2666 | * open at a time to avoid running out of file descriptors. | |
30173b88 MH |
2667 | * Note that lock_ref_for_update() might append more updates |
2668 | * to the transaction. | |
7bd9bcf3 | 2669 | */ |
efe47281 MH |
2670 | for (i = 0; i < transaction->nr; i++) { |
2671 | struct ref_update *update = transaction->updates[i]; | |
7bd9bcf3 | 2672 | |
b3bbbc5c MH |
2673 | ret = lock_ref_for_update(refs, update, transaction, |
2674 | head_ref, &affected_refnames, err); | |
165056b2 | 2675 | if (ret) |
da5267f1 | 2676 | goto cleanup; |
dc39e099 MH |
2677 | |
2678 | if (update->flags & REF_DELETING && | |
2679 | !(update->flags & REF_LOG_ONLY) && | |
acedcde7 | 2680 | !(update->flags & REF_IS_PRUNING)) { |
dc39e099 MH |
2681 | /* |
2682 | * This reference has to be deleted from | |
2683 | * packed-refs if it exists there. | |
2684 | */ | |
2685 | if (!packed_transaction) { | |
2686 | packed_transaction = ref_store_transaction_begin( | |
2687 | refs->packed_ref_store, err); | |
2688 | if (!packed_transaction) { | |
2689 | ret = TRANSACTION_GENERIC_ERROR; | |
2690 | goto cleanup; | |
2691 | } | |
2692 | ||
2693 | backend_data->packed_transaction = | |
2694 | packed_transaction; | |
2695 | } | |
2696 | ||
2697 | ref_transaction_add_update( | |
2698 | packed_transaction, update->refname, | |
91774afc | 2699 | REF_HAVE_NEW | REF_NO_DEREF, |
b0ca4110 | 2700 | &update->new_oid, NULL, |
dc39e099 MH |
2701 | NULL); |
2702 | } | |
2703 | } | |
2704 | ||
2705 | if (packed_transaction) { | |
2706 | if (packed_refs_lock(refs->packed_ref_store, 0, err)) { | |
2707 | ret = TRANSACTION_GENERIC_ERROR; | |
2708 | goto cleanup; | |
2709 | } | |
2710 | backend_data->packed_refs_locked = 1; | |
7c6bd25c MH |
2711 | |
2712 | if (is_packed_transaction_needed(refs->packed_ref_store, | |
2713 | packed_transaction)) { | |
2714 | ret = ref_transaction_prepare(packed_transaction, err); | |
249e8dc7 JK |
2715 | /* |
2716 | * A failure during the prepare step will abort | |
2717 | * itself, but not free. Do that now, and disconnect | |
2718 | * from the files_transaction so it does not try to | |
2719 | * abort us when we hit the cleanup code below. | |
2720 | */ | |
2721 | if (ret) { | |
2722 | ref_transaction_free(packed_transaction); | |
2723 | backend_data->packed_transaction = NULL; | |
2724 | } | |
7c6bd25c MH |
2725 | } else { |
2726 | /* | |
2727 | * We can skip rewriting the `packed-refs` | |
2728 | * file. But we do need to leave it locked, so | |
2729 | * that somebody else doesn't pack a reference | |
2730 | * that we are trying to delete. | |
d3322eb2 JK |
2731 | * |
2732 | * We need to disconnect our transaction from | |
2733 | * backend_data, since the abort (whether successful or | |
2734 | * not) will free it. | |
7c6bd25c | 2735 | */ |
d3322eb2 | 2736 | backend_data->packed_transaction = NULL; |
7c6bd25c MH |
2737 | if (ref_transaction_abort(packed_transaction, err)) { |
2738 | ret = TRANSACTION_GENERIC_ERROR; | |
2739 | goto cleanup; | |
2740 | } | |
7c6bd25c | 2741 | } |
30173b88 MH |
2742 | } |
2743 | ||
2744 | cleanup: | |
2745 | free(head_ref); | |
2746 | string_list_clear(&affected_refnames, 0); | |
2747 | ||
2748 | if (ret) | |
dc39e099 | 2749 | files_transaction_cleanup(refs, transaction); |
30173b88 MH |
2750 | else |
2751 | transaction->state = REF_TRANSACTION_PREPARED; | |
2752 | ||
2753 | return ret; | |
2754 | } | |
2755 | ||
2756 | static int files_transaction_finish(struct ref_store *ref_store, | |
2757 | struct ref_transaction *transaction, | |
2758 | struct strbuf *err) | |
2759 | { | |
2760 | struct files_ref_store *refs = | |
2761 | files_downcast(ref_store, 0, "ref_transaction_finish"); | |
2762 | size_t i; | |
2763 | int ret = 0; | |
30173b88 | 2764 | struct strbuf sb = STRBUF_INIT; |
dc39e099 MH |
2765 | struct files_transaction_backend_data *backend_data; |
2766 | struct ref_transaction *packed_transaction; | |
2767 | ||
30173b88 MH |
2768 | |
2769 | assert(err); | |
2770 | ||
2771 | if (!transaction->nr) { | |
2772 | transaction->state = REF_TRANSACTION_CLOSED; | |
2773 | return 0; | |
7bd9bcf3 | 2774 | } |
7bd9bcf3 | 2775 | |
dc39e099 MH |
2776 | backend_data = transaction->backend_data; |
2777 | packed_transaction = backend_data->packed_transaction; | |
2778 | ||
7bd9bcf3 | 2779 | /* Perform updates first so live commits remain referenced */ |
efe47281 MH |
2780 | for (i = 0; i < transaction->nr; i++) { |
2781 | struct ref_update *update = transaction->updates[i]; | |
7d618264 | 2782 | struct ref_lock *lock = update->backend_data; |
7bd9bcf3 | 2783 | |
d99aa884 DT |
2784 | if (update->flags & REF_NEEDS_COMMIT || |
2785 | update->flags & REF_LOG_ONLY) { | |
802de3da NTND |
2786 | if (files_log_ref_write(refs, |
2787 | lock->ref_name, | |
4417df8c | 2788 | &lock->old_oid, |
2789 | &update->new_oid, | |
81b1b6d4 MH |
2790 | update->msg, update->flags, |
2791 | err)) { | |
92b1551b MH |
2792 | char *old_msg = strbuf_detach(err, NULL); |
2793 | ||
2794 | strbuf_addf(err, "cannot update the ref '%s': %s", | |
2795 | lock->ref_name, old_msg); | |
2796 | free(old_msg); | |
2797 | unlock_ref(lock); | |
7d618264 | 2798 | update->backend_data = NULL; |
7bd9bcf3 MH |
2799 | ret = TRANSACTION_GENERIC_ERROR; |
2800 | goto cleanup; | |
7bd9bcf3 MH |
2801 | } |
2802 | } | |
7bd9bcf3 | 2803 | if (update->flags & REF_NEEDS_COMMIT) { |
00eebe35 | 2804 | clear_loose_ref_cache(refs); |
92b1551b MH |
2805 | if (commit_ref(lock)) { |
2806 | strbuf_addf(err, "couldn't set '%s'", lock->ref_name); | |
2807 | unlock_ref(lock); | |
7d618264 | 2808 | update->backend_data = NULL; |
7bd9bcf3 MH |
2809 | ret = TRANSACTION_GENERIC_ERROR; |
2810 | goto cleanup; | |
7bd9bcf3 MH |
2811 | } |
2812 | } | |
2813 | } | |
dc39e099 | 2814 | |
5e00a6c8 MH |
2815 | /* |
2816 | * Now that updates are safely completed, we can perform | |
2817 | * deletes. First delete the reflogs of any references that | |
2818 | * will be deleted, since (in the unexpected event of an | |
2819 | * error) leaving a reference without a reflog is less bad | |
2820 | * than leaving a reflog without a reference (the latter is a | |
2821 | * mildly invalid repository state): | |
2822 | */ | |
2823 | for (i = 0; i < transaction->nr; i++) { | |
2824 | struct ref_update *update = transaction->updates[i]; | |
2825 | if (update->flags & REF_DELETING && | |
2826 | !(update->flags & REF_LOG_ONLY) && | |
acedcde7 | 2827 | !(update->flags & REF_IS_PRUNING)) { |
5e00a6c8 MH |
2828 | strbuf_reset(&sb); |
2829 | files_reflog_path(refs, &sb, update->refname); | |
2830 | if (!unlink_or_warn(sb.buf)) | |
2831 | try_remove_empty_parents(refs, update->refname, | |
2832 | REMOVE_EMPTY_PARENTS_REFLOG); | |
2833 | } | |
2834 | } | |
2835 | ||
dc39e099 MH |
2836 | /* |
2837 | * Perform deletes now that updates are safely completed. | |
2838 | * | |
2839 | * First delete any packed versions of the references, while | |
2840 | * retaining the packed-refs lock: | |
2841 | */ | |
2842 | if (packed_transaction) { | |
2843 | ret = ref_transaction_commit(packed_transaction, err); | |
2844 | ref_transaction_free(packed_transaction); | |
2845 | packed_transaction = NULL; | |
2846 | backend_data->packed_transaction = NULL; | |
2847 | if (ret) | |
2848 | goto cleanup; | |
2849 | } | |
2850 | ||
2851 | /* Now delete the loose versions of the references: */ | |
efe47281 MH |
2852 | for (i = 0; i < transaction->nr; i++) { |
2853 | struct ref_update *update = transaction->updates[i]; | |
7d618264 | 2854 | struct ref_lock *lock = update->backend_data; |
7bd9bcf3 | 2855 | |
d99aa884 DT |
2856 | if (update->flags & REF_DELETING && |
2857 | !(update->flags & REF_LOG_ONLY)) { | |
ce0af24d MH |
2858 | if (!(update->type & REF_ISPACKED) || |
2859 | update->type & REF_ISSYMREF) { | |
2860 | /* It is a loose reference. */ | |
e9dcc305 | 2861 | strbuf_reset(&sb); |
19e02f4f | 2862 | files_ref_path(refs, &sb, lock->ref_name); |
e9dcc305 | 2863 | if (unlink_or_msg(sb.buf, err)) { |
ce0af24d MH |
2864 | ret = TRANSACTION_GENERIC_ERROR; |
2865 | goto cleanup; | |
2866 | } | |
44639777 | 2867 | update->flags |= REF_DELETED_LOOSE; |
7bd9bcf3 | 2868 | } |
7bd9bcf3 MH |
2869 | } |
2870 | } | |
2871 | ||
00eebe35 | 2872 | clear_loose_ref_cache(refs); |
7bd9bcf3 MH |
2873 | |
2874 | cleanup: | |
dc39e099 | 2875 | files_transaction_cleanup(refs, transaction); |
7bd9bcf3 | 2876 | |
44639777 MH |
2877 | for (i = 0; i < transaction->nr; i++) { |
2878 | struct ref_update *update = transaction->updates[i]; | |
44639777 MH |
2879 | |
2880 | if (update->flags & REF_DELETED_LOOSE) { | |
2881 | /* | |
2882 | * The loose reference was deleted. Delete any | |
2883 | * empty parent directories. (Note that this | |
2884 | * can only work because we have already | |
2885 | * removed the lockfile.) | |
2886 | */ | |
802de3da | 2887 | try_remove_empty_parents(refs, update->refname, |
44639777 MH |
2888 | REMOVE_EMPTY_PARENTS_REF); |
2889 | } | |
2890 | } | |
2891 | ||
30173b88 | 2892 | strbuf_release(&sb); |
7bd9bcf3 MH |
2893 | return ret; |
2894 | } | |
2895 | ||
30173b88 MH |
2896 | static int files_transaction_abort(struct ref_store *ref_store, |
2897 | struct ref_transaction *transaction, | |
2898 | struct strbuf *err) | |
2899 | { | |
dc39e099 MH |
2900 | struct files_ref_store *refs = |
2901 | files_downcast(ref_store, 0, "ref_transaction_abort"); | |
2902 | ||
2903 | files_transaction_cleanup(refs, transaction); | |
30173b88 MH |
2904 | return 0; |
2905 | } | |
2906 | ||
7bd9bcf3 MH |
2907 | static int ref_present(const char *refname, |
2908 | const struct object_id *oid, int flags, void *cb_data) | |
2909 | { | |
2910 | struct string_list *affected_refnames = cb_data; | |
2911 | ||
2912 | return string_list_has_string(affected_refnames, refname); | |
2913 | } | |
2914 | ||
fc681463 DT |
2915 | static int files_initial_transaction_commit(struct ref_store *ref_store, |
2916 | struct ref_transaction *transaction, | |
2917 | struct strbuf *err) | |
7bd9bcf3 | 2918 | { |
d99825ab | 2919 | struct files_ref_store *refs = |
9e7ec634 NTND |
2920 | files_downcast(ref_store, REF_STORE_WRITE, |
2921 | "initial_ref_transaction_commit"); | |
43a2dfde MH |
2922 | size_t i; |
2923 | int ret = 0; | |
7bd9bcf3 | 2924 | struct string_list affected_refnames = STRING_LIST_INIT_NODUP; |
1444bfe0 | 2925 | struct ref_transaction *packed_transaction = NULL; |
7bd9bcf3 MH |
2926 | |
2927 | assert(err); | |
2928 | ||
2929 | if (transaction->state != REF_TRANSACTION_OPEN) | |
033abf97 | 2930 | BUG("commit called for transaction that is not open"); |
7bd9bcf3 MH |
2931 | |
2932 | /* Fail if a refname appears more than once in the transaction: */ | |
efe47281 MH |
2933 | for (i = 0; i < transaction->nr; i++) |
2934 | string_list_append(&affected_refnames, | |
2935 | transaction->updates[i]->refname); | |
7bd9bcf3 MH |
2936 | string_list_sort(&affected_refnames); |
2937 | if (ref_update_reject_duplicates(&affected_refnames, err)) { | |
2938 | ret = TRANSACTION_GENERIC_ERROR; | |
2939 | goto cleanup; | |
2940 | } | |
2941 | ||
2942 | /* | |
2943 | * It's really undefined to call this function in an active | |
2944 | * repository or when there are existing references: we are | |
2945 | * only locking and changing packed-refs, so (1) any | |
2946 | * simultaneous processes might try to change a reference at | |
2947 | * the same time we do, and (2) any existing loose versions of | |
2948 | * the references that we are setting would have precedence | |
2949 | * over our values. But some remote helpers create the remote | |
2950 | * "HEAD" and "master" branches before calling this function, | |
2951 | * so here we really only check that none of the references | |
2952 | * that we are creating already exists. | |
2953 | */ | |
2f40e954 NTND |
2954 | if (refs_for_each_rawref(&refs->base, ref_present, |
2955 | &affected_refnames)) | |
033abf97 | 2956 | BUG("initial ref transaction called with existing refs"); |
7bd9bcf3 | 2957 | |
1444bfe0 MH |
2958 | packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err); |
2959 | if (!packed_transaction) { | |
2960 | ret = TRANSACTION_GENERIC_ERROR; | |
2961 | goto cleanup; | |
2962 | } | |
2963 | ||
efe47281 MH |
2964 | for (i = 0; i < transaction->nr; i++) { |
2965 | struct ref_update *update = transaction->updates[i]; | |
7bd9bcf3 MH |
2966 | |
2967 | if ((update->flags & REF_HAVE_OLD) && | |
98491298 | 2968 | !is_null_oid(&update->old_oid)) |
033abf97 | 2969 | BUG("initial ref transaction with old_sha1 set"); |
7d2df051 NTND |
2970 | if (refs_verify_refname_available(&refs->base, update->refname, |
2971 | &affected_refnames, NULL, | |
2972 | err)) { | |
7bd9bcf3 MH |
2973 | ret = TRANSACTION_NAME_CONFLICT; |
2974 | goto cleanup; | |
2975 | } | |
1444bfe0 MH |
2976 | |
2977 | /* | |
2978 | * Add a reference creation for this reference to the | |
2979 | * packed-refs transaction: | |
2980 | */ | |
2981 | ref_transaction_add_update(packed_transaction, update->refname, | |
2982 | update->flags & ~REF_HAVE_OLD, | |
89f3bbdd | 2983 | &update->new_oid, &update->old_oid, |
1444bfe0 | 2984 | NULL); |
7bd9bcf3 MH |
2985 | } |
2986 | ||
c8bed835 | 2987 | if (packed_refs_lock(refs->packed_ref_store, 0, err)) { |
7bd9bcf3 MH |
2988 | ret = TRANSACTION_GENERIC_ERROR; |
2989 | goto cleanup; | |
2990 | } | |
2991 | ||
1444bfe0 | 2992 | if (initial_ref_transaction_commit(packed_transaction, err)) { |
7bd9bcf3 | 2993 | ret = TRANSACTION_GENERIC_ERROR; |
7bd9bcf3 MH |
2994 | } |
2995 | ||
81fcb698 | 2996 | packed_refs_unlock(refs->packed_ref_store); |
7bd9bcf3 | 2997 | cleanup: |
1444bfe0 MH |
2998 | if (packed_transaction) |
2999 | ref_transaction_free(packed_transaction); | |
7bd9bcf3 MH |
3000 | transaction->state = REF_TRANSACTION_CLOSED; |
3001 | string_list_clear(&affected_refnames, 0); | |
3002 | return ret; | |
3003 | } | |
3004 | ||
3005 | struct expire_reflog_cb { | |
3006 | unsigned int flags; | |
3007 | reflog_expiry_should_prune_fn *should_prune_fn; | |
3008 | void *policy_cb; | |
3009 | FILE *newlog; | |
9461d272 | 3010 | struct object_id last_kept_oid; |
7bd9bcf3 MH |
3011 | }; |
3012 | ||
9461d272 | 3013 | static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid, |
dddbad72 | 3014 | const char *email, timestamp_t timestamp, int tz, |
7bd9bcf3 MH |
3015 | const char *message, void *cb_data) |
3016 | { | |
3017 | struct expire_reflog_cb *cb = cb_data; | |
3018 | struct expire_reflog_policy_cb *policy_cb = cb->policy_cb; | |
3019 | ||
3020 | if (cb->flags & EXPIRE_REFLOGS_REWRITE) | |
9461d272 | 3021 | ooid = &cb->last_kept_oid; |
7bd9bcf3 | 3022 | |
4322478a | 3023 | if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz, |
7bd9bcf3 MH |
3024 | message, policy_cb)) { |
3025 | if (!cb->newlog) | |
3026 | printf("would prune %s", message); | |
3027 | else if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
3028 | printf("prune %s", message); | |
3029 | } else { | |
3030 | if (cb->newlog) { | |
cb71f8bd | 3031 | fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s", |
9461d272 | 3032 | oid_to_hex(ooid), oid_to_hex(noid), |
7bd9bcf3 | 3033 | email, timestamp, tz, message); |
9461d272 | 3034 | oidcpy(&cb->last_kept_oid, noid); |
7bd9bcf3 MH |
3035 | } |
3036 | if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
3037 | printf("keep %s", message); | |
3038 | } | |
3039 | return 0; | |
3040 | } | |
3041 | ||
e3688bd6 | 3042 | static int files_reflog_expire(struct ref_store *ref_store, |
0155f710 | 3043 | const char *refname, const struct object_id *oid, |
e3688bd6 DT |
3044 | unsigned int flags, |
3045 | reflog_expiry_prepare_fn prepare_fn, | |
3046 | reflog_expiry_should_prune_fn should_prune_fn, | |
3047 | reflog_expiry_cleanup_fn cleanup_fn, | |
3048 | void *policy_cb_data) | |
7bd9bcf3 | 3049 | { |
7eb27cdf | 3050 | struct files_ref_store *refs = |
9e7ec634 | 3051 | files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire"); |
b2275868 | 3052 | struct lock_file reflog_lock = LOCK_INIT; |
7bd9bcf3 MH |
3053 | struct expire_reflog_cb cb; |
3054 | struct ref_lock *lock; | |
802de3da | 3055 | struct strbuf log_file_sb = STRBUF_INIT; |
7bd9bcf3 MH |
3056 | char *log_file; |
3057 | int status = 0; | |
3058 | int type; | |
3059 | struct strbuf err = STRBUF_INIT; | |
3060 | ||
3061 | memset(&cb, 0, sizeof(cb)); | |
3062 | cb.flags = flags; | |
3063 | cb.policy_cb = policy_cb_data; | |
3064 | cb.should_prune_fn = should_prune_fn; | |
3065 | ||
3066 | /* | |
3067 | * The reflog file is locked by holding the lock on the | |
3068 | * reference itself, plus we might need to update the | |
3069 | * reference if --updateref was specified: | |
3070 | */ | |
4f01e508 | 3071 | lock = lock_ref_oid_basic(refs, refname, oid, |
91774afc | 3072 | NULL, NULL, REF_NO_DEREF, |
4f01e508 | 3073 | &type, &err); |
7bd9bcf3 MH |
3074 | if (!lock) { |
3075 | error("cannot lock ref '%s': %s", refname, err.buf); | |
3076 | strbuf_release(&err); | |
3077 | return -1; | |
3078 | } | |
2f40e954 | 3079 | if (!refs_reflog_exists(ref_store, refname)) { |
7bd9bcf3 MH |
3080 | unlock_ref(lock); |
3081 | return 0; | |
3082 | } | |
3083 | ||
802de3da NTND |
3084 | files_reflog_path(refs, &log_file_sb, refname); |
3085 | log_file = strbuf_detach(&log_file_sb, NULL); | |
7bd9bcf3 MH |
3086 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { |
3087 | /* | |
3088 | * Even though holding $GIT_DIR/logs/$reflog.lock has | |
3089 | * no locking implications, we use the lock_file | |
3090 | * machinery here anyway because it does a lot of the | |
3091 | * work we need, including cleaning up if the program | |
3092 | * exits unexpectedly. | |
3093 | */ | |
3094 | if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) { | |
3095 | struct strbuf err = STRBUF_INIT; | |
3096 | unable_to_lock_message(log_file, errno, &err); | |
3097 | error("%s", err.buf); | |
3098 | strbuf_release(&err); | |
3099 | goto failure; | |
3100 | } | |
3101 | cb.newlog = fdopen_lock_file(&reflog_lock, "w"); | |
3102 | if (!cb.newlog) { | |
3103 | error("cannot fdopen %s (%s)", | |
3104 | get_lock_file_path(&reflog_lock), strerror(errno)); | |
3105 | goto failure; | |
3106 | } | |
3107 | } | |
3108 | ||
0155f710 | 3109 | (*prepare_fn)(refname, oid, cb.policy_cb); |
2f40e954 | 3110 | refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb); |
7bd9bcf3 MH |
3111 | (*cleanup_fn)(cb.policy_cb); |
3112 | ||
3113 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { | |
3114 | /* | |
3115 | * It doesn't make sense to adjust a reference pointed | |
3116 | * to by a symbolic ref based on expiring entries in | |
3117 | * the symbolic reference's reflog. Nor can we update | |
3118 | * a reference if there are no remaining reflog | |
3119 | * entries. | |
3120 | */ | |
3121 | int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) && | |
3122 | !(type & REF_ISSYMREF) && | |
9461d272 | 3123 | !is_null_oid(&cb.last_kept_oid); |
7bd9bcf3 | 3124 | |
83a3069a | 3125 | if (close_lock_file_gently(&reflog_lock)) { |
7bd9bcf3 MH |
3126 | status |= error("couldn't write %s: %s", log_file, |
3127 | strerror(errno)); | |
83a3069a | 3128 | rollback_lock_file(&reflog_lock); |
7bd9bcf3 | 3129 | } else if (update && |
ee4d8e45 | 3130 | (write_in_full(get_lock_file_fd(&lock->lk), |
2ae2e2a1 | 3131 | oid_to_hex(&cb.last_kept_oid), the_hash_algo->hexsz) < 0 || |
88780c37 | 3132 | write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 || |
83a3069a | 3133 | close_ref_gently(lock) < 0)) { |
7bd9bcf3 | 3134 | status |= error("couldn't write %s", |
ee4d8e45 | 3135 | get_lock_file_path(&lock->lk)); |
7bd9bcf3 MH |
3136 | rollback_lock_file(&reflog_lock); |
3137 | } else if (commit_lock_file(&reflog_lock)) { | |
e0048d3e | 3138 | status |= error("unable to write reflog '%s' (%s)", |
7bd9bcf3 MH |
3139 | log_file, strerror(errno)); |
3140 | } else if (update && commit_ref(lock)) { | |
3141 | status |= error("couldn't set %s", lock->ref_name); | |
3142 | } | |
3143 | } | |
3144 | free(log_file); | |
3145 | unlock_ref(lock); | |
3146 | return status; | |
3147 | ||
3148 | failure: | |
3149 | rollback_lock_file(&reflog_lock); | |
3150 | free(log_file); | |
3151 | unlock_ref(lock); | |
3152 | return -1; | |
3153 | } | |
3dce444f | 3154 | |
6fb5acfd DT |
3155 | static int files_init_db(struct ref_store *ref_store, struct strbuf *err) |
3156 | { | |
19e02f4f | 3157 | struct files_ref_store *refs = |
9e7ec634 | 3158 | files_downcast(ref_store, REF_STORE_WRITE, "init_db"); |
e9dcc305 NTND |
3159 | struct strbuf sb = STRBUF_INIT; |
3160 | ||
6fb5acfd DT |
3161 | /* |
3162 | * Create .git/refs/{heads,tags} | |
3163 | */ | |
19e02f4f | 3164 | files_ref_path(refs, &sb, "refs/heads"); |
e9dcc305 NTND |
3165 | safe_create_dir(sb.buf, 1); |
3166 | ||
3167 | strbuf_reset(&sb); | |
19e02f4f | 3168 | files_ref_path(refs, &sb, "refs/tags"); |
e9dcc305 NTND |
3169 | safe_create_dir(sb.buf, 1); |
3170 | ||
3171 | strbuf_release(&sb); | |
6fb5acfd DT |
3172 | return 0; |
3173 | } | |
3174 | ||
3dce444f RS |
3175 | struct ref_storage_be refs_be_files = { |
3176 | NULL, | |
00eebe35 | 3177 | "files", |
127b42a1 | 3178 | files_ref_store_create, |
6fb5acfd | 3179 | files_init_db, |
30173b88 MH |
3180 | files_transaction_prepare, |
3181 | files_transaction_finish, | |
3182 | files_transaction_abort, | |
fc681463 | 3183 | files_initial_transaction_commit, |
e1e33b72 | 3184 | |
8231527e | 3185 | files_pack_refs, |
284689ba | 3186 | files_create_symref, |
a27dcf89 | 3187 | files_delete_refs, |
9b6b40d9 | 3188 | files_rename_ref, |
52d59cc6 | 3189 | files_copy_ref, |
8231527e | 3190 | |
1a769003 | 3191 | files_ref_iterator_begin, |
62665823 | 3192 | files_read_raw_ref, |
e3688bd6 DT |
3193 | |
3194 | files_reflog_iterator_begin, | |
3195 | files_for_each_reflog_ent, | |
3196 | files_for_each_reflog_ent_reverse, | |
3197 | files_reflog_exists, | |
3198 | files_create_reflog, | |
3199 | files_delete_reflog, | |
3200 | files_reflog_expire | |
3dce444f | 3201 | }; |