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