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