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