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