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