blob: 3d636396654129cc576976db4b712071c24f460d [file] [log] [blame]
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001#include "../cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "../config.h"
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003#include "../refs.h"
4#include "refs-internal.h"
Michael Haggerty958f9642017-04-16 08:41:31 +02005#include "ref-cache.h"
Michael Haggerty3bc581b2016-06-18 06:15:15 +02006#include "../iterator.h"
Michael Haggerty2880d162016-06-18 06:15:19 +02007#include "../dir-iterator.h"
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01008#include "../lockfile.h"
9#include "../object.h"
10#include "../dir.h"
11
12struct ref_lock {
13 char *ref_name;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010014 struct lock_file *lk;
15 struct object_id old_oid;
16};
17
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010018/*
Michael Haggertya8739242016-06-18 06:15:14 +020019 * Return true if refname, which has the specified oid and flags, can
20 * be resolved to an object in the database. If the referred-to object
21 * does not exist, emit a warning and return false.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010022 */
Michael Haggertya8739242016-06-18 06:15:14 +020023static int ref_resolves_to_object(const char *refname,
24 const struct object_id *oid,
25 unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010026{
Michael Haggertya8739242016-06-18 06:15:14 +020027 if (flags & REF_ISBROKEN)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010028 return 0;
Michael Haggertya8739242016-06-18 06:15:14 +020029 if (!has_sha1_file(oid->hash)) {
30 error("%s does not point to a valid object!", refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010031 return 0;
32 }
33 return 1;
34}
35
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010036struct packed_ref_cache {
Michael Haggerty7c22bc82017-04-16 08:41:32 +020037 struct ref_cache *cache;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010038
39 /*
40 * Count of references to the data structure in this instance,
Michael Haggerty65a0a8e2016-09-04 18:08:09 +020041 * including the pointer from files_ref_store::packed if any.
42 * The data will not be freed as long as the reference count
43 * is nonzero.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010044 */
45 unsigned int referrers;
46
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010047 /* The metadata from when this packed-refs cache was read */
48 struct stat_validity validity;
49};
50
51/*
52 * Future: need to be in "struct repository"
53 * when doing a full libification.
54 */
Michael Haggerty00eebe32016-09-04 18:08:11 +020055struct files_ref_store {
56 struct ref_store base;
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +070057 unsigned int store_flags;
Michael Haggerty32c597e2017-02-10 12:16:16 +010058
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +070059 char *gitdir;
60 char *gitcommondir;
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +070061 char *packed_refs_path;
62
Michael Haggerty7c22bc82017-04-16 08:41:32 +020063 struct ref_cache *loose;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010064 struct packed_ref_cache *packed;
Michael Haggerty55c6bc32017-05-22 16:17:40 +020065
66 /*
Michael Haggerty00d17442017-05-22 16:17:41 +020067 * Lock used for the "packed-refs" file. Note that this (and
68 * thus the enclosing `files_ref_store`) must not be freed.
Michael Haggerty55c6bc32017-05-22 16:17:40 +020069 */
Michael Haggerty00d17442017-05-22 16:17:41 +020070 struct lock_file packed_refs_lock;
Michael Haggerty00eebe32016-09-04 18:08:11 +020071};
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010072
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010073/*
74 * Increment the reference count of *packed_refs.
75 */
76static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
77{
78 packed_refs->referrers++;
79}
80
81/*
82 * Decrease the reference count of *packed_refs. If it goes to zero,
83 * free *packed_refs and return true; otherwise return false.
84 */
85static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
86{
87 if (!--packed_refs->referrers) {
Michael Haggerty7c22bc82017-04-16 08:41:32 +020088 free_ref_cache(packed_refs->cache);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010089 stat_validity_clear(&packed_refs->validity);
90 free(packed_refs);
91 return 1;
92 } else {
93 return 0;
94 }
95}
96
Michael Haggerty65a0a8e2016-09-04 18:08:09 +020097static void clear_packed_ref_cache(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010098{
99 if (refs->packed) {
100 struct packed_ref_cache *packed_refs = refs->packed;
101
Michael Haggerty00d17442017-05-22 16:17:41 +0200102 if (is_lock_file_locked(&refs->packed_refs_lock))
Michael Haggerty04aea8d2017-05-22 16:17:34 +0200103 die("BUG: packed-ref cache cleared while locked");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100104 refs->packed = NULL;
105 release_packed_ref_cache(packed_refs);
106 }
107}
108
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200109static void clear_loose_ref_cache(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100110{
111 if (refs->loose) {
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200112 free_ref_cache(refs->loose);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100113 refs->loose = NULL;
114 }
115}
116
Jeff Kinga2d51562016-01-22 17:29:30 -0500117/*
118 * Create a new submodule ref cache and add it to the internal
119 * set of caches.
120 */
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700121static struct ref_store *files_ref_store_create(const char *gitdir,
122 unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100123{
Michael Haggerty00eebe32016-09-04 18:08:11 +0200124 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
125 struct ref_store *ref_store = (struct ref_store *)refs;
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700126 struct strbuf sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100127
Michael Haggertyfbfd0a22017-02-10 12:16:17 +0100128 base_ref_store_init(ref_store, &refs_be_files);
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700129 refs->store_flags = flags;
Jeff Kinga2d51562016-01-22 17:29:30 -0500130
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700131 refs->gitdir = xstrdup(gitdir);
132 get_common_dir_noenv(&sb, gitdir);
133 refs->gitcommondir = strbuf_detach(&sb, NULL);
134 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
135 refs->packed_refs_path = strbuf_detach(&sb, NULL);
Jeff Kinga2d51562016-01-22 17:29:30 -0500136
Michael Haggerty00eebe32016-09-04 18:08:11 +0200137 return ref_store;
Jeff Kinga2d51562016-01-22 17:29:30 -0500138}
139
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100140/*
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700141 * Die if refs is not the main ref store. caller is used in any
142 * necessary error messages.
Michael Haggerty32c597e2017-02-10 12:16:16 +0100143 */
144static void files_assert_main_repository(struct files_ref_store *refs,
145 const char *caller)
146{
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700147 if (refs->store_flags & REF_STORE_MAIN)
148 return;
149
150 die("BUG: operation %s only allowed for main ref store", caller);
Michael Haggerty32c597e2017-02-10 12:16:16 +0100151}
152
153/*
Michael Haggerty00eebe32016-09-04 18:08:11 +0200154 * Downcast ref_store to files_ref_store. Die if ref_store is not a
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700155 * files_ref_store. required_flags is compared with ref_store's
156 * store_flags to ensure the ref_store has all required capabilities.
157 * "caller" is used in any necessary error messages.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100158 */
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700159static struct files_ref_store *files_downcast(struct ref_store *ref_store,
160 unsigned int required_flags,
161 const char *caller)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100162{
Michael Haggerty32c597e2017-02-10 12:16:16 +0100163 struct files_ref_store *refs;
164
Michael Haggerty00eebe32016-09-04 18:08:11 +0200165 if (ref_store->be != &refs_be_files)
166 die("BUG: ref_store is type \"%s\" not \"files\" in %s",
167 ref_store->be->name, caller);
Michael Haggerty2eed2782016-06-18 06:15:12 +0200168
Michael Haggerty32c597e2017-02-10 12:16:16 +0100169 refs = (struct files_ref_store *)ref_store;
Michael Haggerty2eed2782016-06-18 06:15:12 +0200170
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700171 if ((refs->store_flags & required_flags) != required_flags)
172 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
173 caller, required_flags, refs->store_flags);
Michael Haggerty32c597e2017-02-10 12:16:16 +0100174
175 return refs;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100176}
177
178/* The length of a peeled reference line in packed-refs, including EOL: */
179#define PEELED_LINE_LENGTH 42
180
181/*
182 * The packed-refs header line that we write out. Perhaps other
183 * traits will be added later. The trailing space is required.
184 */
185static const char PACKED_REFS_HEADER[] =
186 "# pack-refs with: peeled fully-peeled \n";
187
188/*
189 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
190 * Return a pointer to the refname within the line (null-terminated),
191 * or NULL if there was a problem.
192 */
brian m. carlson4417df82017-05-06 22:10:24 +0000193static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100194{
195 const char *ref;
196
brian m. carlson4417df82017-05-06 22:10:24 +0000197 if (parse_oid_hex(line->buf, oid, &ref) < 0)
198 return NULL;
199 if (!isspace(*ref++))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100200 return NULL;
201
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100202 if (isspace(*ref))
203 return NULL;
204
205 if (line->buf[line->len - 1] != '\n')
206 return NULL;
207 line->buf[--line->len] = 0;
208
209 return ref;
210}
211
212/*
Michael Haggerty099a9122017-05-22 16:17:50 +0200213 * Read from `packed_refs_file` into a newly-allocated
214 * `packed_ref_cache` and return it. The return value will already
215 * have its reference count incremented.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100216 *
217 * A comment line of the form "# pack-refs with: " may contain zero or
218 * more traits. We interpret the traits as follows:
219 *
220 * No traits:
221 *
222 * Probably no references are peeled. But if the file contains a
223 * peeled value for a reference, we will use it.
224 *
225 * peeled:
226 *
227 * References under "refs/tags/", if they *can* be peeled, *are*
228 * peeled in this file. References outside of "refs/tags/" are
229 * probably not peeled even if they could have been, but if we find
230 * a peeled value for such a reference we will use it.
231 *
232 * fully-peeled:
233 *
234 * All references in the file that can be peeled are peeled.
235 * Inversely (and this is more important), any references in the
236 * file for which no peeled value is recorded is not peelable. This
237 * trait should typically be written alongside "peeled" for
238 * compatibility with older clients, but we do not require it
239 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
240 */
Michael Haggerty099a9122017-05-22 16:17:50 +0200241static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100242{
Michael Haggerty099a9122017-05-22 16:17:50 +0200243 FILE *f;
244 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100245 struct ref_entry *last = NULL;
246 struct strbuf line = STRBUF_INIT;
247 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
Michael Haggerty099a9122017-05-22 16:17:50 +0200248 struct ref_dir *dir;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100249
Michael Haggerty099a9122017-05-22 16:17:50 +0200250 acquire_packed_ref_cache(packed_refs);
251 packed_refs->cache = create_ref_cache(NULL, NULL);
252 packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
253
254 f = fopen(packed_refs_file, "r");
Michael Haggerty89c571d2017-05-22 16:17:51 +0200255 if (!f) {
256 if (errno == ENOENT) {
257 /*
258 * This is OK; it just means that no
259 * "packed-refs" file has been written yet,
260 * which is equivalent to it being empty.
261 */
262 return packed_refs;
263 } else {
264 die_errno("couldn't read %s", packed_refs_file);
265 }
266 }
Michael Haggerty099a9122017-05-22 16:17:50 +0200267
268 stat_validity_update(&packed_refs->validity, fileno(f));
269
270 dir = get_ref_dir(packed_refs->cache->root);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100271 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
brian m. carlson4417df82017-05-06 22:10:24 +0000272 struct object_id oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100273 const char *refname;
274 const char *traits;
275
276 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
277 if (strstr(traits, " fully-peeled "))
278 peeled = PEELED_FULLY;
279 else if (strstr(traits, " peeled "))
280 peeled = PEELED_TAGS;
281 /* perhaps other traits later as well */
282 continue;
283 }
284
brian m. carlson4417df82017-05-06 22:10:24 +0000285 refname = parse_ref_line(&line, &oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100286 if (refname) {
287 int flag = REF_ISPACKED;
288
289 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
290 if (!refname_is_safe(refname))
291 die("packed refname is dangerous: %s", refname);
brian m. carlson4417df82017-05-06 22:10:24 +0000292 oidclr(&oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100293 flag |= REF_BAD_NAME | REF_ISBROKEN;
294 }
Michael Haggertyc1da06c2017-05-22 16:17:53 +0200295 last = create_ref_entry(refname, &oid, flag);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100296 if (peeled == PEELED_FULLY ||
297 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
298 last->flag |= REF_KNOWS_PEELED;
Michael Haggertya3ade2b2017-04-16 08:41:28 +0200299 add_ref_entry(dir, last);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100300 continue;
301 }
302 if (last &&
303 line.buf[0] == '^' &&
304 line.len == PEELED_LINE_LENGTH &&
305 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
brian m. carlson4417df82017-05-06 22:10:24 +0000306 !get_oid_hex(line.buf + 1, &oid)) {
307 oidcpy(&last->u.value.peeled, &oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100308 /*
309 * Regardless of what the file header said,
310 * we definitely know the value of *this*
311 * reference:
312 */
313 last->flag |= REF_KNOWS_PEELED;
314 }
315 }
316
Michael Haggerty099a9122017-05-22 16:17:50 +0200317 fclose(f);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100318 strbuf_release(&line);
Michael Haggerty099a9122017-05-22 16:17:50 +0200319
320 return packed_refs;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100321}
322
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +0700323static const char *files_packed_refs_path(struct files_ref_store *refs)
324{
325 return refs->packed_refs_path;
326}
327
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +0700328static void files_reflog_path(struct files_ref_store *refs,
329 struct strbuf *sb,
330 const char *refname)
331{
332 if (!refname) {
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700333 /*
334 * FIXME: of course this is wrong in multi worktree
335 * setting. To be fixed real soon.
336 */
337 strbuf_addf(sb, "%s/logs", refs->gitcommondir);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +0700338 return;
339 }
340
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700341 switch (ref_type(refname)) {
342 case REF_TYPE_PER_WORKTREE:
343 case REF_TYPE_PSEUDOREF:
344 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname);
345 break;
346 case REF_TYPE_NORMAL:
347 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
348 break;
349 default:
350 die("BUG: unknown ref type %d of ref %s",
351 ref_type(refname), refname);
352 }
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +0700353}
354
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700355static void files_ref_path(struct files_ref_store *refs,
356 struct strbuf *sb,
357 const char *refname)
358{
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700359 switch (ref_type(refname)) {
360 case REF_TYPE_PER_WORKTREE:
361 case REF_TYPE_PSEUDOREF:
362 strbuf_addf(sb, "%s/%s", refs->gitdir, refname);
363 break;
364 case REF_TYPE_NORMAL:
365 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
366 break;
367 default:
368 die("BUG: unknown ref type %d of ref %s",
369 ref_type(refname), refname);
370 }
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700371}
372
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100373/*
Michael Haggertyfed6ebe2017-06-12 10:06:13 +0200374 * Check that the packed refs cache (if any) still reflects the
375 * contents of the file. If not, clear the cache.
376 */
377static void validate_packed_ref_cache(struct files_ref_store *refs)
378{
379 if (refs->packed &&
380 !stat_validity_check(&refs->packed->validity,
381 files_packed_refs_path(refs)))
382 clear_packed_ref_cache(refs);
383}
384
385/*
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200386 * Get the packed_ref_cache for the specified files_ref_store,
Michael Haggerty28ed9832017-05-22 16:17:49 +0200387 * creating and populating it if it hasn't been read before or if the
388 * file has been changed (according to its `validity` field) since it
389 * was last read. On the other hand, if we hold the lock, then assume
390 * that the file hasn't been changed out from under us, so skip the
391 * extra `stat()` call in `stat_validity_check()`.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100392 */
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200393static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100394{
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +0700395 const char *packed_refs_file = files_packed_refs_path(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100396
Michael Haggertyfed6ebe2017-06-12 10:06:13 +0200397 if (!is_lock_file_locked(&refs->packed_refs_lock))
398 validate_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100399
Michael Haggerty099a9122017-05-22 16:17:50 +0200400 if (!refs->packed)
401 refs->packed = read_packed_refs(packed_refs_file);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100402
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100403 return refs->packed;
404}
405
406static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
407{
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200408 return get_ref_dir(packed_ref_cache->cache->root);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100409}
410
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200411static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100412{
413 return get_packed_ref_dir(get_packed_ref_cache(refs));
414}
415
416/*
417 * Add a reference to the in-memory packed reference cache. This may
418 * only be called while the packed-refs file is locked (see
419 * lock_packed_refs()). To actually write the packed-refs file, call
420 * commit_packed_refs().
421 */
Michael Haggertyd99825a2016-09-04 18:08:12 +0200422static void add_packed_ref(struct files_ref_store *refs,
brian m. carlson4417df82017-05-06 22:10:24 +0000423 const char *refname, const struct object_id *oid)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100424{
Michael Haggerty00eebe32016-09-04 18:08:11 +0200425 struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100426
Michael Haggerty00d17442017-05-22 16:17:41 +0200427 if (!is_lock_file_locked(&refs->packed_refs_lock))
Michael Haggerty04aea8d2017-05-22 16:17:34 +0200428 die("BUG: packed refs not locked");
Michael Haggertyc1da06c2017-05-22 16:17:53 +0200429
430 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
431 die("Reference has invalid format: '%s'", refname);
432
Michael Haggertya3ade2b2017-04-16 08:41:28 +0200433 add_ref_entry(get_packed_ref_dir(packed_ref_cache),
Michael Haggertyc1da06c2017-05-22 16:17:53 +0200434 create_ref_entry(refname, oid, REF_ISPACKED));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100435}
436
437/*
438 * Read the loose references from the namespace dirname into dir
439 * (without recursing). dirname must end with '/'. dir must be the
440 * directory entry corresponding to dirname.
441 */
Michael Haggertydf308752017-04-16 08:41:34 +0200442static void loose_fill_ref_dir(struct ref_store *ref_store,
443 struct ref_dir *dir, const char *dirname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100444{
Michael Haggertydf308752017-04-16 08:41:34 +0200445 struct files_ref_store *refs =
446 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100447 DIR *d;
448 struct dirent *de;
449 int dirnamelen = strlen(dirname);
450 struct strbuf refname;
451 struct strbuf path = STRBUF_INIT;
452 size_t path_baselen;
453
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700454 files_ref_path(refs, &path, dirname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100455 path_baselen = path.len;
456
457 d = opendir(path.buf);
458 if (!d) {
459 strbuf_release(&path);
460 return;
461 }
462
463 strbuf_init(&refname, dirnamelen + 257);
464 strbuf_add(&refname, dirname, dirnamelen);
465
466 while ((de = readdir(d)) != NULL) {
brian m. carlson4417df82017-05-06 22:10:24 +0000467 struct object_id oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100468 struct stat st;
469 int flag;
470
471 if (de->d_name[0] == '.')
472 continue;
473 if (ends_with(de->d_name, ".lock"))
474 continue;
475 strbuf_addstr(&refname, de->d_name);
476 strbuf_addstr(&path, de->d_name);
477 if (stat(path.buf, &st) < 0) {
478 ; /* silently ignore */
479 } else if (S_ISDIR(st.st_mode)) {
480 strbuf_addch(&refname, '/');
481 add_entry_to_dir(dir,
Michael Haggertye00d1a42017-04-16 08:41:33 +0200482 create_dir_entry(dir->cache, refname.buf,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100483 refname.len, 1));
484 } else {
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700485 if (!refs_resolve_ref_unsafe(&refs->base,
Michael Haggerty3c0cb0c2017-02-09 21:53:52 +0100486 refname.buf,
487 RESOLVE_REF_READING,
brian m. carlson4417df82017-05-06 22:10:24 +0000488 oid.hash, &flag)) {
489 oidclr(&oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100490 flag |= REF_ISBROKEN;
brian m. carlson4417df82017-05-06 22:10:24 +0000491 } else if (is_null_oid(&oid)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100492 /*
493 * It is so astronomically unlikely
494 * that NULL_SHA1 is the SHA-1 of an
495 * actual object that we consider its
496 * appearance in a loose reference
497 * file to be repo corruption
498 * (probably due to a software bug).
499 */
500 flag |= REF_ISBROKEN;
501 }
502
503 if (check_refname_format(refname.buf,
504 REFNAME_ALLOW_ONELEVEL)) {
505 if (!refname_is_safe(refname.buf))
506 die("loose refname is dangerous: %s", refname.buf);
brian m. carlson4417df82017-05-06 22:10:24 +0000507 oidclr(&oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100508 flag |= REF_BAD_NAME | REF_ISBROKEN;
509 }
510 add_entry_to_dir(dir,
Michael Haggertyc1da06c2017-05-22 16:17:53 +0200511 create_ref_entry(refname.buf, &oid, flag));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100512 }
513 strbuf_setlen(&refname, dirnamelen);
514 strbuf_setlen(&path, path_baselen);
515 }
516 strbuf_release(&refname);
517 strbuf_release(&path);
518 closedir(d);
Michael Haggertye3bf2982017-04-16 08:41:35 +0200519
520 /*
521 * Manually add refs/bisect, which, being per-worktree, might
522 * not appear in the directory listing for refs/ in the main
523 * repo.
524 */
525 if (!strcmp(dirname, "refs/")) {
526 int pos = search_ref_dir(dir, "refs/bisect/", 12);
527
528 if (pos < 0) {
529 struct ref_entry *child_entry = create_dir_entry(
530 dir->cache, "refs/bisect/", 12, 1);
531 add_entry_to_dir(dir, child_entry);
532 }
533 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100534}
535
Michael Haggertya714b192017-04-16 08:41:38 +0200536static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100537{
538 if (!refs->loose) {
539 /*
540 * Mark the top-level directory complete because we
541 * are about to read the only subdirectory that can
542 * hold references:
543 */
Michael Haggertydf308752017-04-16 08:41:34 +0200544 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200545
546 /* We're going to fill the top level ourselves: */
547 refs->loose->root->flag &= ~REF_INCOMPLETE;
548
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100549 /*
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200550 * Add an incomplete entry for "refs/" (to be filled
551 * lazily):
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100552 */
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200553 add_entry_to_dir(get_ref_dir(refs->loose->root),
Michael Haggertye00d1a42017-04-16 08:41:33 +0200554 create_dir_entry(refs->loose, "refs/", 5, 1));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100555 }
Michael Haggertya714b192017-04-16 08:41:38 +0200556 return refs->loose;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100557}
558
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100559/*
560 * Return the ref_entry for the given refname from the packed
561 * references. If it does not exist, return NULL.
562 */
Michael Haggertyf0d21ef2016-09-04 18:08:13 +0200563static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
564 const char *refname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100565{
Michael Haggertybc1c6962017-04-16 08:41:29 +0200566 return find_ref_entry(get_packed_refs(refs), refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100567}
568
569/*
Michael Haggerty419c6f42016-04-07 15:02:55 -0400570 * A loose ref file doesn't exist; check for a packed ref.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100571 */
Michael Haggerty611118d2016-09-04 18:08:18 +0200572static int resolve_packed_ref(struct files_ref_store *refs,
573 const char *refname,
574 unsigned char *sha1, unsigned int *flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100575{
576 struct ref_entry *entry;
577
578 /*
579 * The loose reference file does not exist; check for a packed
580 * reference.
581 */
Michael Haggertyf0d21ef2016-09-04 18:08:13 +0200582 entry = get_packed_ref(refs, refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100583 if (entry) {
584 hashcpy(sha1, entry->u.value.oid.hash);
Michael Haggertya70a93b2016-04-07 15:02:57 -0400585 *flags |= REF_ISPACKED;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100586 return 0;
587 }
Michael Haggerty419c6f42016-04-07 15:02:55 -0400588 /* refname is not a packed reference. */
589 return -1;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100590}
591
Michael Haggertye1e33b72016-09-04 18:08:25 +0200592static int files_read_raw_ref(struct ref_store *ref_store,
593 const char *refname, unsigned char *sha1,
594 struct strbuf *referent, unsigned int *type)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100595{
Michael Haggerty43086512016-09-04 18:08:14 +0200596 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700597 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400598 struct strbuf sb_contents = STRBUF_INIT;
599 struct strbuf sb_path = STRBUF_INIT;
David Turner70486532016-04-07 15:03:01 -0400600 const char *path;
601 const char *buf;
602 struct stat st;
603 int fd;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400604 int ret = -1;
605 int save_errno;
Jeff Kinge8c42cb2016-10-06 12:48:42 -0400606 int remaining_retries = 3;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100607
Michael Haggertyfa96ea12016-04-22 01:11:17 +0200608 *type = 0;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400609 strbuf_reset(&sb_path);
Michael Haggerty34c7ad82016-09-04 18:08:20 +0200610
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700611 files_ref_path(refs, &sb_path, refname);
Michael Haggerty34c7ad82016-09-04 18:08:20 +0200612
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400613 path = sb_path.buf;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100614
David Turner70486532016-04-07 15:03:01 -0400615stat_ref:
616 /*
617 * We might have to loop back here to avoid a race
618 * condition: first we lstat() the file, then we try
619 * to read it as a link or as a file. But if somebody
620 * changes the type of the file (file <-> directory
621 * <-> symlink) between the lstat() and reading, then
622 * we don't want to report that as an error but rather
623 * try again starting with the lstat().
Jeff Kinge8c42cb2016-10-06 12:48:42 -0400624 *
625 * We'll keep a count of the retries, though, just to avoid
626 * any confusing situation sending us into an infinite loop.
David Turner70486532016-04-07 15:03:01 -0400627 */
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100628
Jeff Kinge8c42cb2016-10-06 12:48:42 -0400629 if (remaining_retries-- <= 0)
630 goto out;
631
David Turner70486532016-04-07 15:03:01 -0400632 if (lstat(path, &st) < 0) {
633 if (errno != ENOENT)
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400634 goto out;
Michael Haggerty611118d2016-09-04 18:08:18 +0200635 if (resolve_packed_ref(refs, refname, sha1, type)) {
David Turner70486532016-04-07 15:03:01 -0400636 errno = ENOENT;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400637 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100638 }
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400639 ret = 0;
640 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100641 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100642
David Turner70486532016-04-07 15:03:01 -0400643 /* Follow "normalized" - ie "refs/.." symlinks by hand */
644 if (S_ISLNK(st.st_mode)) {
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400645 strbuf_reset(&sb_contents);
646 if (strbuf_readlink(&sb_contents, path, 0) < 0) {
David Turner70486532016-04-07 15:03:01 -0400647 if (errno == ENOENT || errno == EINVAL)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100648 /* inconsistent with lstat; retry */
649 goto stat_ref;
650 else
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400651 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100652 }
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400653 if (starts_with(sb_contents.buf, "refs/") &&
654 !check_refname_format(sb_contents.buf, 0)) {
Michael Haggerty92b38092016-04-22 01:11:17 +0200655 strbuf_swap(&sb_contents, referent);
Michael Haggerty3a0b6b92016-04-26 03:06:23 +0200656 *type |= REF_ISSYMREF;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400657 ret = 0;
658 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100659 }
Jeff King3f7bd762016-10-06 15:41:08 -0400660 /*
661 * It doesn't look like a refname; fall through to just
662 * treating it like a non-symlink, and reading whatever it
663 * points to.
664 */
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100665 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100666
David Turner70486532016-04-07 15:03:01 -0400667 /* Is it a directory? */
668 if (S_ISDIR(st.st_mode)) {
Michael Haggertye167a562016-05-05 14:09:41 +0200669 /*
670 * Even though there is a directory where the loose
671 * ref is supposed to be, there could still be a
672 * packed ref:
673 */
Michael Haggerty611118d2016-09-04 18:08:18 +0200674 if (resolve_packed_ref(refs, refname, sha1, type)) {
Michael Haggertye167a562016-05-05 14:09:41 +0200675 errno = EISDIR;
676 goto out;
677 }
678 ret = 0;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400679 goto out;
David Turner70486532016-04-07 15:03:01 -0400680 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100681
David Turner70486532016-04-07 15:03:01 -0400682 /*
683 * Anything else, just open it and try to use it as
684 * a ref
685 */
686 fd = open(path, O_RDONLY);
687 if (fd < 0) {
Jeff King3f7bd762016-10-06 15:41:08 -0400688 if (errno == ENOENT && !S_ISLNK(st.st_mode))
David Turner70486532016-04-07 15:03:01 -0400689 /* inconsistent with lstat; retry */
690 goto stat_ref;
691 else
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400692 goto out;
David Turner70486532016-04-07 15:03:01 -0400693 }
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400694 strbuf_reset(&sb_contents);
695 if (strbuf_read(&sb_contents, fd, 256) < 0) {
David Turner70486532016-04-07 15:03:01 -0400696 int save_errno = errno;
697 close(fd);
698 errno = save_errno;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400699 goto out;
David Turner70486532016-04-07 15:03:01 -0400700 }
701 close(fd);
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400702 strbuf_rtrim(&sb_contents);
703 buf = sb_contents.buf;
David Turner70486532016-04-07 15:03:01 -0400704 if (starts_with(buf, "ref:")) {
705 buf += 4;
706 while (isspace(*buf))
707 buf++;
708
Michael Haggerty92b38092016-04-22 01:11:17 +0200709 strbuf_reset(referent);
710 strbuf_addstr(referent, buf);
Michael Haggerty3a0b6b92016-04-26 03:06:23 +0200711 *type |= REF_ISSYMREF;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400712 ret = 0;
713 goto out;
David Turner70486532016-04-07 15:03:01 -0400714 }
715
716 /*
717 * Please note that FETCH_HEAD has additional
718 * data after the sha.
719 */
720 if (get_sha1_hex(buf, sha1) ||
721 (buf[40] != '\0' && !isspace(buf[40]))) {
Michael Haggerty3a0b6b92016-04-26 03:06:23 +0200722 *type |= REF_ISBROKEN;
David Turner70486532016-04-07 15:03:01 -0400723 errno = EINVAL;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400724 goto out;
David Turner70486532016-04-07 15:03:01 -0400725 }
726
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400727 ret = 0;
728
729out:
730 save_errno = errno;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100731 strbuf_release(&sb_path);
732 strbuf_release(&sb_contents);
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400733 errno = save_errno;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100734 return ret;
735}
736
Michael Haggerty8415d242016-04-24 08:11:37 +0200737static void unlock_ref(struct ref_lock *lock)
738{
739 /* Do not free lock->lk -- atexit() still looks at them */
740 if (lock->lk)
741 rollback_lock_file(lock->lk);
742 free(lock->ref_name);
Michael Haggerty8415d242016-04-24 08:11:37 +0200743 free(lock);
744}
745
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100746/*
Michael Haggerty92b15512016-04-25 15:56:07 +0200747 * Lock refname, without following symrefs, and set *lock_p to point
748 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
749 * and type similarly to read_raw_ref().
750 *
751 * The caller must verify that refname is a "safe" reference name (in
752 * the sense of refname_is_safe()) before calling this function.
753 *
754 * If the reference doesn't already exist, verify that refname doesn't
755 * have a D/F conflict with any existing references. extras and skip
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200756 * are passed to refs_verify_refname_available() for this check.
Michael Haggerty92b15512016-04-25 15:56:07 +0200757 *
758 * If mustexist is not set and the reference is not found or is
759 * broken, lock the reference anyway but clear sha1.
760 *
761 * Return 0 on success. On failure, write an error message to err and
762 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
763 *
764 * Implementation note: This function is basically
765 *
766 * lock reference
767 * read_raw_ref()
768 *
769 * but it includes a lot more code to
770 * - Deal with possible races with other processes
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200771 * - Avoid calling refs_verify_refname_available() when it can be
Michael Haggerty92b15512016-04-25 15:56:07 +0200772 * avoided, namely if we were successfully able to read the ref
773 * - Generate informative error messages in the case of failure
774 */
Michael Haggertyf7b0a982016-09-04 18:08:31 +0200775static int lock_raw_ref(struct files_ref_store *refs,
776 const char *refname, int mustexist,
Michael Haggerty92b15512016-04-25 15:56:07 +0200777 const struct string_list *extras,
778 const struct string_list *skip,
779 struct ref_lock **lock_p,
780 struct strbuf *referent,
781 unsigned int *type,
782 struct strbuf *err)
783{
784 struct ref_lock *lock;
785 struct strbuf ref_file = STRBUF_INIT;
786 int attempts_remaining = 3;
787 int ret = TRANSACTION_GENERIC_ERROR;
788
789 assert(err);
Michael Haggerty32c597e2017-02-10 12:16:16 +0100790 files_assert_main_repository(refs, "lock_raw_ref");
Michael Haggertyf7b0a982016-09-04 18:08:31 +0200791
Michael Haggerty92b15512016-04-25 15:56:07 +0200792 *type = 0;
793
794 /* First lock the file so it can't change out from under us. */
795
796 *lock_p = lock = xcalloc(1, sizeof(*lock));
797
798 lock->ref_name = xstrdup(refname);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700799 files_ref_path(refs, &ref_file, refname);
Michael Haggerty92b15512016-04-25 15:56:07 +0200800
801retry:
802 switch (safe_create_leading_directories(ref_file.buf)) {
803 case SCLD_OK:
804 break; /* success */
805 case SCLD_EXISTS:
806 /*
807 * Suppose refname is "refs/foo/bar". We just failed
808 * to create the containing directory, "refs/foo",
809 * because there was a non-directory in the way. This
810 * indicates a D/F conflict, probably because of
811 * another reference such as "refs/foo". There is no
812 * reason to expect this error to be transitory.
813 */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700814 if (refs_verify_refname_available(&refs->base, refname,
815 extras, skip, err)) {
Michael Haggerty92b15512016-04-25 15:56:07 +0200816 if (mustexist) {
817 /*
818 * To the user the relevant error is
819 * that the "mustexist" reference is
820 * missing:
821 */
822 strbuf_reset(err);
823 strbuf_addf(err, "unable to resolve reference '%s'",
824 refname);
825 } else {
826 /*
827 * The error message set by
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200828 * refs_verify_refname_available() is
829 * OK.
Michael Haggerty92b15512016-04-25 15:56:07 +0200830 */
831 ret = TRANSACTION_NAME_CONFLICT;
832 }
833 } else {
834 /*
835 * The file that is in the way isn't a loose
836 * reference. Report it as a low-level
837 * failure.
838 */
839 strbuf_addf(err, "unable to create lock file %s.lock; "
840 "non-directory in the way",
841 ref_file.buf);
842 }
843 goto error_return;
844 case SCLD_VANISHED:
845 /* Maybe another process was tidying up. Try again. */
846 if (--attempts_remaining > 0)
847 goto retry;
848 /* fall through */
849 default:
850 strbuf_addf(err, "unable to create directory for %s",
851 ref_file.buf);
852 goto error_return;
853 }
854
855 if (!lock->lk)
856 lock->lk = xcalloc(1, sizeof(struct lock_file));
857
858 if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {
859 if (errno == ENOENT && --attempts_remaining > 0) {
860 /*
861 * Maybe somebody just deleted one of the
862 * directories leading to ref_file. Try
863 * again:
864 */
865 goto retry;
866 } else {
867 unable_to_lock_message(ref_file.buf, errno, err);
868 goto error_return;
869 }
870 }
871
872 /*
873 * Now we hold the lock and can read the reference without
874 * fear that its value will change.
875 */
876
Michael Haggertyf7b0a982016-09-04 18:08:31 +0200877 if (files_read_raw_ref(&refs->base, refname,
Michael Haggertye1e33b72016-09-04 18:08:25 +0200878 lock->old_oid.hash, referent, type)) {
Michael Haggerty92b15512016-04-25 15:56:07 +0200879 if (errno == ENOENT) {
880 if (mustexist) {
881 /* Garden variety missing reference. */
882 strbuf_addf(err, "unable to resolve reference '%s'",
883 refname);
884 goto error_return;
885 } else {
886 /*
887 * Reference is missing, but that's OK. We
888 * know that there is not a conflict with
889 * another loose reference because
890 * (supposing that we are trying to lock
891 * reference "refs/foo/bar"):
892 *
893 * - We were successfully able to create
894 * the lockfile refs/foo/bar.lock, so we
895 * know there cannot be a loose reference
896 * named "refs/foo".
897 *
898 * - We got ENOENT and not EISDIR, so we
899 * know that there cannot be a loose
900 * reference named "refs/foo/bar/baz".
901 */
902 }
903 } else if (errno == EISDIR) {
904 /*
905 * There is a directory in the way. It might have
906 * contained references that have been deleted. If
907 * we don't require that the reference already
908 * exists, try to remove the directory so that it
909 * doesn't cause trouble when we want to rename the
910 * lockfile into place later.
911 */
912 if (mustexist) {
913 /* Garden variety missing reference. */
914 strbuf_addf(err, "unable to resolve reference '%s'",
915 refname);
916 goto error_return;
917 } else if (remove_dir_recursively(&ref_file,
918 REMOVE_DIR_EMPTY_ONLY)) {
Michael Haggertyb05855b2017-04-16 08:41:26 +0200919 if (refs_verify_refname_available(
920 &refs->base, refname,
921 extras, skip, err)) {
Michael Haggerty92b15512016-04-25 15:56:07 +0200922 /*
923 * The error message set by
924 * verify_refname_available() is OK.
925 */
926 ret = TRANSACTION_NAME_CONFLICT;
927 goto error_return;
928 } else {
929 /*
930 * We can't delete the directory,
931 * but we also don't know of any
932 * references that it should
933 * contain.
934 */
935 strbuf_addf(err, "there is a non-empty directory '%s' "
936 "blocking reference '%s'",
937 ref_file.buf, refname);
938 goto error_return;
939 }
940 }
941 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
942 strbuf_addf(err, "unable to resolve reference '%s': "
943 "reference broken", refname);
944 goto error_return;
945 } else {
946 strbuf_addf(err, "unable to resolve reference '%s': %s",
947 refname, strerror(errno));
948 goto error_return;
949 }
950
951 /*
952 * If the ref did not exist and we are creating it,
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200953 * make sure there is no existing ref that conflicts
954 * with refname:
Michael Haggerty92b15512016-04-25 15:56:07 +0200955 */
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200956 if (refs_verify_refname_available(
957 &refs->base, refname,
958 extras, skip, err))
Michael Haggerty92b15512016-04-25 15:56:07 +0200959 goto error_return;
Michael Haggerty92b15512016-04-25 15:56:07 +0200960 }
961
962 ret = 0;
963 goto out;
964
965error_return:
966 unlock_ref(lock);
967 *lock_p = NULL;
968
969out:
970 strbuf_release(&ref_file);
971 return ret;
972}
973
Michael Haggertybd427cf2016-09-04 18:08:29 +0200974static int files_peel_ref(struct ref_store *ref_store,
975 const char *refname, unsigned char *sha1)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100976{
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700977 struct files_ref_store *refs =
978 files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
979 "peel_ref");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100980 int flag;
981 unsigned char base[20];
982
Michael Haggerty4c4de892016-06-18 06:15:16 +0200983 if (current_ref_iter && current_ref_iter->refname == refname) {
984 struct object_id peeled;
985
986 if (ref_iterator_peel(current_ref_iter, &peeled))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100987 return -1;
Michael Haggerty4c4de892016-06-18 06:15:16 +0200988 hashcpy(sha1, peeled.hash);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100989 return 0;
990 }
991
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +0700992 if (refs_read_ref_full(ref_store, refname,
993 RESOLVE_REF_READING, base, &flag))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100994 return -1;
995
996 /*
997 * If the reference is packed, read its ref_entry from the
998 * cache in the hope that we already know its peeled value.
999 * We only try this optimization on packed references because
1000 * (a) forcing the filling of the loose reference cache could
1001 * be expensive and (b) loose references anyway usually do not
1002 * have REF_KNOWS_PEELED.
1003 */
1004 if (flag & REF_ISPACKED) {
Michael Haggertyf0d21ef2016-09-04 18:08:13 +02001005 struct ref_entry *r = get_packed_ref(refs, refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001006 if (r) {
1007 if (peel_entry(r, 0))
1008 return -1;
1009 hashcpy(sha1, r->u.value.peeled.hash);
1010 return 0;
1011 }
1012 }
1013
1014 return peel_object(base, sha1);
1015}
1016
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001017struct files_ref_iterator {
1018 struct ref_iterator base;
1019
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001020 struct packed_ref_cache *packed_ref_cache;
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001021 struct ref_iterator *iter0;
1022 unsigned int flags;
1023};
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001024
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001025static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
1026{
1027 struct files_ref_iterator *iter =
1028 (struct files_ref_iterator *)ref_iterator;
1029 int ok;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001030
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001031 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
David Turner0c09ec02016-09-04 18:08:44 +02001032 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
1033 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
1034 continue;
1035
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001036 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
1037 !ref_resolves_to_object(iter->iter0->refname,
1038 iter->iter0->oid,
1039 iter->iter0->flags))
1040 continue;
1041
1042 iter->base.refname = iter->iter0->refname;
1043 iter->base.oid = iter->iter0->oid;
1044 iter->base.flags = iter->iter0->flags;
1045 return ITER_OK;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001046 }
1047
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001048 iter->iter0 = NULL;
1049 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
1050 ok = ITER_ERROR;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001051
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001052 return ok;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001053}
1054
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001055static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,
1056 struct object_id *peeled)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001057{
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001058 struct files_ref_iterator *iter =
1059 (struct files_ref_iterator *)ref_iterator;
David Turner93770592016-04-07 15:02:49 -04001060
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001061 return ref_iterator_peel(iter->iter0, peeled);
1062}
1063
1064static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
1065{
1066 struct files_ref_iterator *iter =
1067 (struct files_ref_iterator *)ref_iterator;
1068 int ok = ITER_DONE;
1069
1070 if (iter->iter0)
1071 ok = ref_iterator_abort(iter->iter0);
1072
1073 release_packed_ref_cache(iter->packed_ref_cache);
1074 base_ref_iterator_free(ref_iterator);
1075 return ok;
1076}
1077
1078static struct ref_iterator_vtable files_ref_iterator_vtable = {
1079 files_ref_iterator_advance,
1080 files_ref_iterator_peel,
1081 files_ref_iterator_abort
1082};
1083
Michael Haggerty1a769002016-09-04 18:08:37 +02001084static struct ref_iterator *files_ref_iterator_begin(
Michael Haggerty37b6f6d2016-09-04 18:08:36 +02001085 struct ref_store *ref_store,
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001086 const char *prefix, unsigned int flags)
1087{
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001088 struct files_ref_store *refs;
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001089 struct ref_iterator *loose_iter, *packed_iter;
1090 struct files_ref_iterator *iter;
1091 struct ref_iterator *ref_iterator;
Michael Haggerty0a0865b2017-05-22 16:17:52 +02001092 unsigned int required_flags = REF_STORE_READ;
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001093
Michael Haggerty0a0865b2017-05-22 16:17:52 +02001094 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
1095 required_flags |= REF_STORE_ODB;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001096
Michael Haggerty0a0865b2017-05-22 16:17:52 +02001097 refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001098
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001099 iter = xcalloc(1, sizeof(*iter));
1100 ref_iterator = &iter->base;
1101 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
1102
1103 /*
1104 * We must make sure that all loose refs are read before
1105 * accessing the packed-refs file; this avoids a race
1106 * condition if loose refs are migrated to the packed-refs
1107 * file by a simultaneous process, but our in-memory view is
1108 * from before the migration. We ensure this as follows:
Michael Haggerty059ae352017-04-16 08:41:39 +02001109 * First, we call start the loose refs iteration with its
1110 * `prime_ref` argument set to true. This causes the loose
1111 * references in the subtree to be pre-read into the cache.
1112 * (If they've already been read, that's OK; we only need to
1113 * guarantee that they're read before the packed refs, not
1114 * *how much* before.) After that, we call
1115 * get_packed_ref_cache(), which internally checks whether the
1116 * packed-ref cache is up to date with what is on disk, and
1117 * re-reads it if not.
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001118 */
1119
Michael Haggerty059ae352017-04-16 08:41:39 +02001120 loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
1121 prefix, 1);
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001122
1123 iter->packed_ref_cache = get_packed_ref_cache(refs);
1124 acquire_packed_ref_cache(iter->packed_ref_cache);
Michael Haggerty059ae352017-04-16 08:41:39 +02001125 packed_iter = cache_ref_iterator_begin(iter->packed_ref_cache->cache,
1126 prefix, 0);
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001127
1128 iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
1129 iter->flags = flags;
1130
1131 return ref_iterator;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001132}
1133
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001134/*
1135 * Verify that the reference locked by lock has the value old_sha1.
1136 * Fail if the reference doesn't exist and mustexist is set. Return 0
1137 * on success. On error, write an error message to err, set errno, and
1138 * return a negative value.
1139 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001140static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001141 const unsigned char *old_sha1, int mustexist,
1142 struct strbuf *err)
1143{
1144 assert(err);
1145
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001146 if (refs_read_ref_full(ref_store, lock->ref_name,
1147 mustexist ? RESOLVE_REF_READING : 0,
1148 lock->old_oid.hash, NULL)) {
Jeff King6294dcb2016-01-12 16:44:39 -05001149 if (old_sha1) {
1150 int save_errno = errno;
Michael Haggerty0568c8e2016-04-27 15:21:36 +02001151 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
Jeff King6294dcb2016-01-12 16:44:39 -05001152 errno = save_errno;
1153 return -1;
1154 } else {
brian m. carlsonc368dde2016-06-24 23:09:22 +00001155 oidclr(&lock->old_oid);
Jeff King6294dcb2016-01-12 16:44:39 -05001156 return 0;
1157 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001158 }
Jeff King6294dcb2016-01-12 16:44:39 -05001159 if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) {
Michael Haggerty0568c8e2016-04-27 15:21:36 +02001160 strbuf_addf(err, "ref '%s' is at %s but expected %s",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001161 lock->ref_name,
brian m. carlsonc368dde2016-06-24 23:09:22 +00001162 oid_to_hex(&lock->old_oid),
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001163 sha1_to_hex(old_sha1));
1164 errno = EBUSY;
1165 return -1;
1166 }
1167 return 0;
1168}
1169
1170static int remove_empty_directories(struct strbuf *path)
1171{
1172 /*
1173 * we want to create a file but there is a directory there;
1174 * if that is an empty directory (or a directory that contains
1175 * only empty directories), remove them.
1176 */
1177 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1178}
1179
Michael Haggerty3b5d3c92017-01-06 17:22:28 +01001180static int create_reflock(const char *path, void *cb)
1181{
1182 struct lock_file *lk = cb;
1183
1184 return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;
1185}
1186
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001187/*
1188 * Locks a ref returning the lock on success and NULL on failure.
1189 * On failure errno is set to something meaningful.
1190 */
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02001191static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
1192 const char *refname,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001193 const unsigned char *old_sha1,
1194 const struct string_list *extras,
1195 const struct string_list *skip,
Michael Haggertybcb497d2016-04-22 09:13:00 +02001196 unsigned int flags, int *type,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001197 struct strbuf *err)
1198{
1199 struct strbuf ref_file = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001200 struct ref_lock *lock;
1201 int last_errno = 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001202 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
Michael Haggerty7a418f32016-04-22 15:25:25 +02001203 int resolve_flags = RESOLVE_REF_NO_RECURSE;
Michael Haggerty7a418f32016-04-22 15:25:25 +02001204 int resolved;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001205
Michael Haggerty32c597e2017-02-10 12:16:16 +01001206 files_assert_main_repository(refs, "lock_ref_sha1_basic");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001207 assert(err);
1208
1209 lock = xcalloc(1, sizeof(struct ref_lock));
1210
1211 if (mustexist)
1212 resolve_flags |= RESOLVE_REF_READING;
Jeff King2859dcd2016-01-12 16:45:09 -05001213 if (flags & REF_DELETING)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001214 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001215
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07001216 files_ref_path(refs, &ref_file, refname);
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001217 resolved = !!refs_resolve_ref_unsafe(&refs->base,
1218 refname, resolve_flags,
1219 lock->old_oid.hash, type);
Michael Haggerty7a418f32016-04-22 15:25:25 +02001220 if (!resolved && errno == EISDIR) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001221 /*
1222 * we are trying to lock foo but we used to
1223 * have foo/bar which now does not exist;
1224 * it is normal for the empty directory 'foo'
1225 * to remain.
1226 */
Michael Haggerty7a418f32016-04-22 15:25:25 +02001227 if (remove_empty_directories(&ref_file)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001228 last_errno = errno;
Michael Haggertyb05855b2017-04-16 08:41:26 +02001229 if (!refs_verify_refname_available(
1230 &refs->base,
1231 refname, extras, skip, err))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001232 strbuf_addf(err, "there are still refs under '%s'",
Michael Haggerty7a418f32016-04-22 15:25:25 +02001233 refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001234 goto error_return;
1235 }
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001236 resolved = !!refs_resolve_ref_unsafe(&refs->base,
1237 refname, resolve_flags,
1238 lock->old_oid.hash, type);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001239 }
Michael Haggerty7a418f32016-04-22 15:25:25 +02001240 if (!resolved) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001241 last_errno = errno;
1242 if (last_errno != ENOTDIR ||
Michael Haggertyb05855b2017-04-16 08:41:26 +02001243 !refs_verify_refname_available(&refs->base, refname,
1244 extras, skip, err))
Michael Haggerty0568c8e2016-04-27 15:21:36 +02001245 strbuf_addf(err, "unable to resolve reference '%s': %s",
Michael Haggerty7a418f32016-04-22 15:25:25 +02001246 refname, strerror(last_errno));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001247
1248 goto error_return;
1249 }
Jeff King2859dcd2016-01-12 16:45:09 -05001250
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001251 /*
1252 * If the ref did not exist and we are creating it, make sure
1253 * there is no existing packed ref whose name begins with our
1254 * refname, nor a packed ref whose name is a proper prefix of
1255 * our refname.
1256 */
1257 if (is_null_oid(&lock->old_oid) &&
Michael Haggerty524a9fd2017-04-16 08:41:27 +02001258 refs_verify_refname_available(&refs->base, refname,
1259 extras, skip, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001260 last_errno = ENOTDIR;
1261 goto error_return;
1262 }
1263
1264 lock->lk = xcalloc(1, sizeof(struct lock_file));
1265
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001266 lock->ref_name = xstrdup(refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001267
Michael Haggerty3b5d3c92017-01-06 17:22:28 +01001268 if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001269 last_errno = errno;
Michael Haggerty3b5d3c92017-01-06 17:22:28 +01001270 unable_to_lock_message(ref_file.buf, errno, err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001271 goto error_return;
1272 }
1273
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001274 if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001275 last_errno = errno;
1276 goto error_return;
1277 }
1278 goto out;
1279
1280 error_return:
1281 unlock_ref(lock);
1282 lock = NULL;
1283
1284 out:
1285 strbuf_release(&ref_file);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001286 errno = last_errno;
1287 return lock;
1288}
1289
1290/*
1291 * Write an entry to the packed-refs file for the specified refname.
1292 * If peeled is non-NULL, write it as the entry's peeled value.
1293 */
Michael Haggerty1710fba2017-04-16 08:41:40 +02001294static void write_packed_entry(FILE *fh, const char *refname,
1295 const unsigned char *sha1,
1296 const unsigned char *peeled)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001297{
1298 fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
1299 if (peeled)
1300 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
1301}
1302
1303/*
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001304 * Lock the packed-refs file for writing. Flags is passed to
1305 * hold_lock_file_for_update(). Return 0 on success. On errors, set
1306 * errno appropriately and return a nonzero value.
1307 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001308static int lock_packed_refs(struct files_ref_store *refs, int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001309{
1310 static int timeout_configured = 0;
1311 static int timeout_value = 1000;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001312 struct packed_ref_cache *packed_ref_cache;
1313
Michael Haggerty32c597e2017-02-10 12:16:16 +01001314 files_assert_main_repository(refs, "lock_packed_refs");
Michael Haggerty49c0df62016-09-04 18:08:15 +02001315
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001316 if (!timeout_configured) {
1317 git_config_get_int("core.packedrefstimeout", &timeout_value);
1318 timeout_configured = 1;
1319 }
1320
1321 if (hold_lock_file_for_update_timeout(
Michael Haggerty00d17442017-05-22 16:17:41 +02001322 &refs->packed_refs_lock, files_packed_refs_path(refs),
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001323 flags, timeout_value) < 0)
1324 return -1;
Michael Haggertyfed6ebe2017-06-12 10:06:13 +02001325
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001326 /*
Michael Haggertyfed6ebe2017-06-12 10:06:13 +02001327 * Now that we hold the `packed-refs` lock, make sure that our
1328 * cache matches the current version of the file. Normally
1329 * `get_packed_ref_cache()` does that for us, but that
1330 * function assumes that when the file is locked, any existing
1331 * cache is still valid. We've just locked the file, but it
1332 * might have changed the moment *before* we locked it.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001333 */
Michael Haggertyfed6ebe2017-06-12 10:06:13 +02001334 validate_packed_ref_cache(refs);
1335
Michael Haggerty00eebe32016-09-04 18:08:11 +02001336 packed_ref_cache = get_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001337 /* Increment the reference count to prevent it from being freed: */
1338 acquire_packed_ref_cache(packed_ref_cache);
1339 return 0;
1340}
1341
1342/*
1343 * Write the current version of the packed refs cache from memory to
1344 * disk. The packed-refs file must already be locked for writing (see
1345 * lock_packed_refs()). Return zero on success. On errors, set errno
1346 * and return a nonzero value
1347 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001348static int commit_packed_refs(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001349{
1350 struct packed_ref_cache *packed_ref_cache =
Michael Haggerty00eebe32016-09-04 18:08:11 +02001351 get_packed_ref_cache(refs);
Michael Haggerty1710fba2017-04-16 08:41:40 +02001352 int ok, error = 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001353 int save_errno = 0;
1354 FILE *out;
Michael Haggerty1710fba2017-04-16 08:41:40 +02001355 struct ref_iterator *iter;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001356
Michael Haggerty32c597e2017-02-10 12:16:16 +01001357 files_assert_main_repository(refs, "commit_packed_refs");
Michael Haggerty49c0df62016-09-04 18:08:15 +02001358
Michael Haggerty00d17442017-05-22 16:17:41 +02001359 if (!is_lock_file_locked(&refs->packed_refs_lock))
Michael Haggerty04aea8d2017-05-22 16:17:34 +02001360 die("BUG: packed-refs not locked");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001361
Michael Haggerty00d17442017-05-22 16:17:41 +02001362 out = fdopen_lock_file(&refs->packed_refs_lock, "w");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001363 if (!out)
1364 die_errno("unable to fdopen packed-refs descriptor");
1365
1366 fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
Michael Haggerty1710fba2017-04-16 08:41:40 +02001367
1368 iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
1369 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1370 struct object_id peeled;
1371 int peel_error = ref_iterator_peel(iter, &peeled);
1372
1373 write_packed_entry(out, iter->refname, iter->oid->hash,
1374 peel_error ? NULL : peeled.hash);
1375 }
1376
1377 if (ok != ITER_DONE)
1378 die("error while iterating over references");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001379
Michael Haggerty00d17442017-05-22 16:17:41 +02001380 if (commit_lock_file(&refs->packed_refs_lock)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001381 save_errno = errno;
1382 error = -1;
1383 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001384 release_packed_ref_cache(packed_ref_cache);
1385 errno = save_errno;
1386 return error;
1387}
1388
1389/*
1390 * Rollback the lockfile for the packed-refs file, and discard the
1391 * in-memory packed reference cache. (The packed-refs file will be
1392 * read anew if it is needed again after this function is called.)
1393 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001394static void rollback_packed_refs(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001395{
1396 struct packed_ref_cache *packed_ref_cache =
Michael Haggerty00eebe32016-09-04 18:08:11 +02001397 get_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001398
Michael Haggerty32c597e2017-02-10 12:16:16 +01001399 files_assert_main_repository(refs, "rollback_packed_refs");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001400
Michael Haggerty00d17442017-05-22 16:17:41 +02001401 if (!is_lock_file_locked(&refs->packed_refs_lock))
Michael Haggerty04aea8d2017-05-22 16:17:34 +02001402 die("BUG: packed-refs not locked");
Michael Haggerty00d17442017-05-22 16:17:41 +02001403 rollback_lock_file(&refs->packed_refs_lock);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001404 release_packed_ref_cache(packed_ref_cache);
Michael Haggerty00eebe32016-09-04 18:08:11 +02001405 clear_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001406}
1407
1408struct ref_to_prune {
1409 struct ref_to_prune *next;
1410 unsigned char sha1[20];
1411 char name[FLEX_ARRAY];
1412};
1413
Michael Haggertya8f0db22017-01-06 17:22:42 +01001414enum {
1415 REMOVE_EMPTY_PARENTS_REF = 0x01,
1416 REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1417};
1418
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001419/*
Michael Haggertya8f0db22017-01-06 17:22:42 +01001420 * Remove empty parent directories associated with the specified
1421 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1422 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1423 * REMOVE_EMPTY_PARENTS_REFLOG.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001424 */
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001425static void try_remove_empty_parents(struct files_ref_store *refs,
1426 const char *refname,
1427 unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001428{
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001429 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001430 struct strbuf sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001431 char *p, *q;
1432 int i;
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001433
1434 strbuf_addstr(&buf, refname);
1435 p = buf.buf;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001436 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1437 while (*p && *p != '/')
1438 p++;
1439 /* tolerate duplicate slashes; see check_refname_format() */
1440 while (*p == '/')
1441 p++;
1442 }
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001443 q = buf.buf + buf.len;
Michael Haggertya8f0db22017-01-06 17:22:42 +01001444 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001445 while (q > p && *q != '/')
1446 q--;
1447 while (q > p && *(q-1) == '/')
1448 q--;
1449 if (q == p)
1450 break;
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001451 strbuf_setlen(&buf, q - buf.buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001452
1453 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07001454 files_ref_path(refs, &sb, buf.buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001455 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
Michael Haggertya8f0db22017-01-06 17:22:42 +01001456 flags &= ~REMOVE_EMPTY_PARENTS_REF;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001457
1458 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001459 files_reflog_path(refs, &sb, buf.buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001460 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
Michael Haggertya8f0db22017-01-06 17:22:42 +01001461 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001462 }
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001463 strbuf_release(&buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001464 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001465}
1466
1467/* make sure nobody touched the ref, and unlink */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001468static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001469{
1470 struct ref_transaction *transaction;
1471 struct strbuf err = STRBUF_INIT;
1472
1473 if (check_refname_format(r->name, 0))
1474 return;
1475
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001476 transaction = ref_store_transaction_begin(&refs->base, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001477 if (!transaction ||
1478 ref_transaction_delete(transaction, r->name, r->sha1,
Michael Haggertyc52ce242016-04-24 09:48:26 +02001479 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001480 ref_transaction_commit(transaction, &err)) {
1481 ref_transaction_free(transaction);
1482 error("%s", err.buf);
1483 strbuf_release(&err);
1484 return;
1485 }
1486 ref_transaction_free(transaction);
1487 strbuf_release(&err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001488}
1489
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001490static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001491{
1492 while (r) {
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001493 prune_ref(refs, r);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001494 r = r->next;
1495 }
1496}
1497
Michael Haggerty531cc4a2017-05-22 16:17:48 +02001498/*
1499 * Return true if the specified reference should be packed.
1500 */
1501static int should_pack_ref(const char *refname,
1502 const struct object_id *oid, unsigned int ref_flags,
1503 unsigned int pack_flags)
1504{
1505 /* Do not pack per-worktree refs: */
1506 if (ref_type(refname) != REF_TYPE_NORMAL)
1507 return 0;
1508
1509 /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1510 if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/"))
1511 return 0;
1512
1513 /* Do not pack symbolic refs: */
1514 if (ref_flags & REF_ISSYMREF)
1515 return 0;
1516
1517 /* Do not pack broken refs: */
1518 if (!ref_resolves_to_object(refname, oid, ref_flags))
1519 return 0;
1520
1521 return 1;
1522}
1523
Michael Haggerty82315272016-09-04 18:08:27 +02001524static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001525{
Michael Haggerty00eebe32016-09-04 18:08:11 +02001526 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001527 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1528 "pack_refs");
Michael Haggerty50c2d852017-04-16 08:41:41 +02001529 struct ref_iterator *iter;
1530 struct ref_dir *packed_refs;
1531 int ok;
1532 struct ref_to_prune *refs_to_prune = NULL;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001533
Michael Haggerty49c0df62016-09-04 18:08:15 +02001534 lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
Michael Haggerty50c2d852017-04-16 08:41:41 +02001535 packed_refs = get_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001536
Michael Haggerty50c2d852017-04-16 08:41:41 +02001537 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);
1538 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1539 /*
1540 * If the loose reference can be packed, add an entry
1541 * in the packed ref cache. If the reference should be
1542 * pruned, also add it to refs_to_prune.
1543 */
1544 struct ref_entry *packed_entry;
Michael Haggerty50c2d852017-04-16 08:41:41 +02001545
Michael Haggerty531cc4a2017-05-22 16:17:48 +02001546 if (!should_pack_ref(iter->refname, iter->oid, iter->flags,
1547 flags))
Michael Haggerty50c2d852017-04-16 08:41:41 +02001548 continue;
1549
1550 /*
1551 * Create an entry in the packed-refs cache equivalent
1552 * to the one from the loose ref cache, except that
1553 * we don't copy the peeled status, because we want it
1554 * to be re-peeled.
1555 */
1556 packed_entry = find_ref_entry(packed_refs, iter->refname);
1557 if (packed_entry) {
1558 /* Overwrite existing packed entry with info from loose entry */
1559 packed_entry->flag = REF_ISPACKED;
1560 oidcpy(&packed_entry->u.value.oid, iter->oid);
1561 } else {
brian m. carlson4417df82017-05-06 22:10:24 +00001562 packed_entry = create_ref_entry(iter->refname, iter->oid,
Michael Haggertyc1da06c2017-05-22 16:17:53 +02001563 REF_ISPACKED);
Michael Haggerty50c2d852017-04-16 08:41:41 +02001564 add_ref_entry(packed_refs, packed_entry);
1565 }
1566 oidclr(&packed_entry->u.value.peeled);
1567
1568 /* Schedule the loose reference for pruning if requested. */
1569 if ((flags & PACK_REFS_PRUNE)) {
1570 struct ref_to_prune *n;
1571 FLEX_ALLOC_STR(n, name, iter->refname);
1572 hashcpy(n->sha1, iter->oid->hash);
1573 n->next = refs_to_prune;
1574 refs_to_prune = n;
1575 }
1576 }
1577 if (ok != ITER_DONE)
1578 die("error while iterating over references");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001579
Michael Haggerty49c0df62016-09-04 18:08:15 +02001580 if (commit_packed_refs(refs))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001581 die_errno("unable to overwrite old ref-pack file");
1582
Michael Haggerty50c2d852017-04-16 08:41:41 +02001583 prune_refs(refs, refs_to_prune);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001584 return 0;
1585}
1586
1587/*
1588 * Rewrite the packed-refs file, omitting any refs listed in
1589 * 'refnames'. On error, leave packed-refs unchanged, write an error
1590 * message to 'err', and return a nonzero value.
1591 *
1592 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
1593 */
Michael Haggerty0a95ac52016-09-04 18:08:30 +02001594static int repack_without_refs(struct files_ref_store *refs,
1595 struct string_list *refnames, struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001596{
1597 struct ref_dir *packed;
1598 struct string_list_item *refname;
1599 int ret, needs_repacking = 0, removed = 0;
1600
Michael Haggerty32c597e2017-02-10 12:16:16 +01001601 files_assert_main_repository(refs, "repack_without_refs");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001602 assert(err);
1603
1604 /* Look for a packed ref */
1605 for_each_string_list_item(refname, refnames) {
Michael Haggertyf0d21ef2016-09-04 18:08:13 +02001606 if (get_packed_ref(refs, refname->string)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001607 needs_repacking = 1;
1608 break;
1609 }
1610 }
1611
1612 /* Avoid locking if we have nothing to do */
1613 if (!needs_repacking)
1614 return 0; /* no refname exists in packed refs */
1615
Michael Haggerty49c0df62016-09-04 18:08:15 +02001616 if (lock_packed_refs(refs, 0)) {
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +07001617 unable_to_lock_message(files_packed_refs_path(refs), errno, err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001618 return -1;
1619 }
Michael Haggerty00eebe32016-09-04 18:08:11 +02001620 packed = get_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001621
1622 /* Remove refnames from the cache */
1623 for_each_string_list_item(refname, refnames)
Michael Haggerty9fc3b062017-04-16 08:41:30 +02001624 if (remove_entry_from_dir(packed, refname->string) != -1)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001625 removed = 1;
1626 if (!removed) {
1627 /*
1628 * All packed entries disappeared while we were
1629 * acquiring the lock.
1630 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001631 rollback_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001632 return 0;
1633 }
1634
1635 /* Write what remains */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001636 ret = commit_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001637 if (ret)
1638 strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
1639 strerror(errno));
1640 return ret;
1641}
1642
Michael Haggerty64da4192017-05-22 16:17:38 +02001643static int files_delete_refs(struct ref_store *ref_store, const char *msg,
David Turnera27dcf82016-09-04 18:08:40 +02001644 struct string_list *refnames, unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001645{
Michael Haggerty0a95ac52016-09-04 18:08:30 +02001646 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001647 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001648 struct strbuf err = STRBUF_INIT;
1649 int i, result = 0;
1650
1651 if (!refnames->nr)
1652 return 0;
1653
Michael Haggerty0a95ac52016-09-04 18:08:30 +02001654 result = repack_without_refs(refs, refnames, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001655 if (result) {
1656 /*
1657 * If we failed to rewrite the packed-refs file, then
1658 * it is unsafe to try to remove loose refs, because
1659 * doing so might expose an obsolete packed value for
1660 * a reference that might even point at an object that
1661 * has been garbage collected.
1662 */
1663 if (refnames->nr == 1)
1664 error(_("could not delete reference %s: %s"),
1665 refnames->items[0].string, err.buf);
1666 else
1667 error(_("could not delete references: %s"), err.buf);
1668
1669 goto out;
1670 }
1671
1672 for (i = 0; i < refnames->nr; i++) {
1673 const char *refname = refnames->items[i].string;
1674
Michael Haggerty64da4192017-05-22 16:17:38 +02001675 if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001676 result |= error(_("could not remove reference %s"), refname);
1677 }
1678
1679out:
1680 strbuf_release(&err);
1681 return result;
1682}
1683
1684/*
1685 * People using contrib's git-new-workdir have .git/logs/refs ->
1686 * /some/other/path/.git/logs/refs, and that may live on another device.
1687 *
1688 * IOW, to avoid cross device rename errors, the temporary renamed log must
1689 * live into logs/refs.
1690 */
Nguyễn Thái Ngọc Duya5c1efd2017-03-26 09:42:21 +07001691#define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001692
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001693struct rename_cb {
1694 const char *tmp_renamed_log;
1695 int true_errno;
1696};
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001697
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001698static int rename_tmp_log_callback(const char *path, void *cb_data)
1699{
1700 struct rename_cb *cb = cb_data;
1701
1702 if (rename(cb->tmp_renamed_log, path)) {
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001703 /*
1704 * rename(a, b) when b is an existing directory ought
1705 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1706 * Sheesh. Record the true errno for error reporting,
1707 * but report EISDIR to raceproof_create_file() so
1708 * that it knows to retry.
1709 */
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001710 cb->true_errno = errno;
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001711 if (errno == ENOTDIR)
1712 errno = EISDIR;
1713 return -1;
1714 } else {
1715 return 0;
1716 }
1717}
1718
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001719static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001720{
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001721 struct strbuf path = STRBUF_INIT;
1722 struct strbuf tmp = STRBUF_INIT;
1723 struct rename_cb cb;
1724 int ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001725
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001726 files_reflog_path(refs, &path, newrefname);
1727 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001728 cb.tmp_renamed_log = tmp.buf;
1729 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001730 if (ret) {
1731 if (errno == EISDIR)
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001732 error("directory not empty: %s", path.buf);
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001733 else
Michael Haggerty990c98d2017-01-06 17:22:30 +01001734 error("unable to move logfile %s to %s: %s",
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001735 tmp.buf, path.buf,
1736 strerror(cb.true_errno));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001737 }
1738
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001739 strbuf_release(&path);
1740 strbuf_release(&tmp);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001741 return ret;
1742}
1743
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001744static int write_ref_to_lockfile(struct ref_lock *lock,
brian m. carlson4417df82017-05-06 22:10:24 +00001745 const struct object_id *oid, struct strbuf *err);
Michael Haggertyf18a7892016-09-04 18:08:32 +02001746static int commit_ref_update(struct files_ref_store *refs,
1747 struct ref_lock *lock,
brian m. carlson4417df82017-05-06 22:10:24 +00001748 const struct object_id *oid, const char *logmsg,
Michael Haggerty5d9b2de2016-04-22 14:38:56 +02001749 struct strbuf *err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001750
David Turner9b6b40d2016-09-04 18:08:42 +02001751static int files_rename_ref(struct ref_store *ref_store,
1752 const char *oldrefname, const char *newrefname,
1753 const char *logmsg)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001754{
David Turner9b6b40d2016-09-04 18:08:42 +02001755 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001756 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
brian m. carlson4417df82017-05-06 22:10:24 +00001757 struct object_id oid, orig_oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001758 int flag = 0, logmoved = 0;
1759 struct ref_lock *lock;
1760 struct stat loginfo;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001761 struct strbuf sb_oldref = STRBUF_INIT;
1762 struct strbuf sb_newref = STRBUF_INIT;
1763 struct strbuf tmp_renamed_log = STRBUF_INIT;
1764 int log, ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001765 struct strbuf err = STRBUF_INIT;
1766
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001767 files_reflog_path(refs, &sb_oldref, oldrefname);
1768 files_reflog_path(refs, &sb_newref, newrefname);
1769 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001770
1771 log = !lstat(sb_oldref.buf, &loginfo);
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001772 if (log && S_ISLNK(loginfo.st_mode)) {
1773 ret = error("reflog for %s is a symlink", oldrefname);
1774 goto out;
1775 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001776
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001777 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1778 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
brian m. carlson4417df82017-05-06 22:10:24 +00001779 orig_oid.hash, &flag)) {
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001780 ret = error("refname %s not found", oldrefname);
1781 goto out;
1782 }
Michael Haggertye711b1a2016-04-21 23:42:19 +02001783
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001784 if (flag & REF_ISSYMREF) {
1785 ret = error("refname %s is a symbolic ref, renaming it is not supported",
1786 oldrefname);
1787 goto out;
1788 }
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +07001789 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001790 ret = 1;
1791 goto out;
1792 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001793
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001794 if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
Nguyễn Thái Ngọc Duya5c1efd2017-03-26 09:42:21 +07001795 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001796 oldrefname, strerror(errno));
1797 goto out;
1798 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001799
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001800 if (refs_delete_ref(&refs->base, logmsg, oldrefname,
brian m. carlson4417df82017-05-06 22:10:24 +00001801 orig_oid.hash, REF_NODEREF)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001802 error("unable to delete old %s", oldrefname);
1803 goto rollback;
1804 }
1805
David Turner12fd3492016-02-24 17:58:51 -05001806 /*
brian m. carlson4417df82017-05-06 22:10:24 +00001807 * Since we are doing a shallow lookup, oid is not the
1808 * correct value to pass to delete_ref as old_oid. But that
1809 * doesn't matter, because an old_oid check wouldn't add to
David Turner12fd3492016-02-24 17:58:51 -05001810 * the safety anyway; we want to delete the reference whatever
1811 * its current value.
1812 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001813 if (!refs_read_ref_full(&refs->base, newrefname,
1814 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
brian m. carlson4417df82017-05-06 22:10:24 +00001815 oid.hash, NULL) &&
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001816 refs_delete_ref(&refs->base, NULL, newrefname,
1817 NULL, REF_NODEREF)) {
Michael Haggerty58364322017-01-06 17:22:21 +01001818 if (errno == EISDIR) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001819 struct strbuf path = STRBUF_INIT;
1820 int result;
1821
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07001822 files_ref_path(refs, &path, newrefname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001823 result = remove_empty_directories(&path);
1824 strbuf_release(&path);
1825
1826 if (result) {
1827 error("Directory not empty: %s", newrefname);
1828 goto rollback;
1829 }
1830 } else {
1831 error("unable to delete existing %s", newrefname);
1832 goto rollback;
1833 }
1834 }
1835
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001836 if (log && rename_tmp_log(refs, newrefname))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001837 goto rollback;
1838
1839 logmoved = log;
1840
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02001841 lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
1842 REF_NODEREF, NULL, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001843 if (!lock) {
1844 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1845 strbuf_release(&err);
1846 goto rollback;
1847 }
brian m. carlson4417df82017-05-06 22:10:24 +00001848 oidcpy(&lock->old_oid, &orig_oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001849
brian m. carlson4417df82017-05-06 22:10:24 +00001850 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1851 commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001852 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1853 strbuf_release(&err);
1854 goto rollback;
1855 }
1856
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001857 ret = 0;
1858 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001859
1860 rollback:
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02001861 lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,
1862 REF_NODEREF, NULL, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001863 if (!lock) {
1864 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1865 strbuf_release(&err);
1866 goto rollbacklog;
1867 }
1868
1869 flag = log_all_ref_updates;
Cornelius Weig341fb282017-01-27 11:09:47 +01001870 log_all_ref_updates = LOG_REFS_NONE;
brian m. carlson4417df82017-05-06 22:10:24 +00001871 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1872 commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001873 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1874 strbuf_release(&err);
1875 }
1876 log_all_ref_updates = flag;
1877
1878 rollbacklog:
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001879 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001880 error("unable to restore logfile %s from %s: %s",
1881 oldrefname, newrefname, strerror(errno));
1882 if (!logmoved && log &&
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001883 rename(tmp_renamed_log.buf, sb_oldref.buf))
Nguyễn Thái Ngọc Duya5c1efd2017-03-26 09:42:21 +07001884 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001885 oldrefname, strerror(errno));
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001886 ret = 1;
1887 out:
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001888 strbuf_release(&sb_newref);
1889 strbuf_release(&sb_oldref);
1890 strbuf_release(&tmp_renamed_log);
1891
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001892 return ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001893}
1894
1895static int close_ref(struct ref_lock *lock)
1896{
1897 if (close_lock_file(lock->lk))
1898 return -1;
1899 return 0;
1900}
1901
1902static int commit_ref(struct ref_lock *lock)
1903{
Michael Haggerty5387c0d2016-05-05 15:33:03 +02001904 char *path = get_locked_file_path(lock->lk);
1905 struct stat st;
1906
1907 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1908 /*
1909 * There is a directory at the path we want to rename
1910 * the lockfile to. Hopefully it is empty; try to
1911 * delete it.
1912 */
1913 size_t len = strlen(path);
1914 struct strbuf sb_path = STRBUF_INIT;
1915
1916 strbuf_attach(&sb_path, path, len, len);
1917
1918 /*
1919 * If this fails, commit_lock_file() will also fail
1920 * and will report the problem.
1921 */
1922 remove_empty_directories(&sb_path);
1923 strbuf_release(&sb_path);
1924 } else {
1925 free(path);
1926 }
1927
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001928 if (commit_lock_file(lock->lk))
1929 return -1;
1930 return 0;
1931}
1932
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001933static int open_or_create_logfile(const char *path, void *cb)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001934{
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001935 int *fd = cb;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001936
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001937 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1938 return (*fd < 0) ? -1 : 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001939}
1940
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001941/*
Michael Haggerty4533e532017-01-06 17:22:36 +01001942 * Create a reflog for a ref. If force_create = 0, only create the
1943 * reflog for certain refs (those for which should_autocreate_reflog
1944 * returns non-zero). Otherwise, create it regardless of the reference
1945 * name. If the logfile already existed or was created, return 0 and
1946 * set *logfd to the file descriptor opened for appending to the file.
1947 * If no logfile exists and we decided not to create one, return 0 and
1948 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1949 * return -1.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001950 */
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001951static int log_ref_setup(struct files_ref_store *refs,
1952 const char *refname, int force_create,
Michael Haggerty4533e532017-01-06 17:22:36 +01001953 int *logfd, struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001954{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001955 struct strbuf logfile_sb = STRBUF_INIT;
1956 char *logfile;
1957
1958 files_reflog_path(refs, &logfile_sb, refname);
1959 logfile = strbuf_detach(&logfile_sb, NULL);
Michael Haggerty854bda62017-01-06 17:22:32 +01001960
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001961 if (force_create || should_autocreate_reflog(refname)) {
Michael Haggerty4533e532017-01-06 17:22:36 +01001962 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001963 if (errno == ENOENT)
1964 strbuf_addf(err, "unable to create directory for '%s': "
Michael Haggerty4533e532017-01-06 17:22:36 +01001965 "%s", logfile, strerror(errno));
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001966 else if (errno == EISDIR)
1967 strbuf_addf(err, "there are still logs under '%s'",
Michael Haggerty4533e532017-01-06 17:22:36 +01001968 logfile);
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001969 else
Michael Haggerty854bda62017-01-06 17:22:32 +01001970 strbuf_addf(err, "unable to append to '%s': %s",
Michael Haggerty4533e532017-01-06 17:22:36 +01001971 logfile, strerror(errno));
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001972
Michael Haggerty4533e532017-01-06 17:22:36 +01001973 goto error;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001974 }
Michael Haggerty854bda62017-01-06 17:22:32 +01001975 } else {
Michael Haggerty4533e532017-01-06 17:22:36 +01001976 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
Michael Haggertye404f452017-01-06 17:22:34 +01001977 if (*logfd < 0) {
Michael Haggerty854bda62017-01-06 17:22:32 +01001978 if (errno == ENOENT || errno == EISDIR) {
1979 /*
1980 * The logfile doesn't already exist,
1981 * but that is not an error; it only
1982 * means that we won't write log
1983 * entries to it.
1984 */
1985 ;
1986 } else {
1987 strbuf_addf(err, "unable to append to '%s': %s",
Michael Haggerty4533e532017-01-06 17:22:36 +01001988 logfile, strerror(errno));
1989 goto error;
Michael Haggerty854bda62017-01-06 17:22:32 +01001990 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001991 }
1992 }
1993
Michael Haggertye404f452017-01-06 17:22:34 +01001994 if (*logfd >= 0)
Michael Haggerty4533e532017-01-06 17:22:36 +01001995 adjust_shared_perm(logfile);
Michael Haggerty854bda62017-01-06 17:22:32 +01001996
Michael Haggerty4533e532017-01-06 17:22:36 +01001997 free(logfile);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001998 return 0;
Michael Haggerty4533e532017-01-06 17:22:36 +01001999
2000error:
2001 free(logfile);
2002 return -1;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002003}
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002004
David Turnere3688bd2016-09-04 18:08:38 +02002005static int files_create_reflog(struct ref_store *ref_store,
2006 const char *refname, int force_create,
2007 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002008{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002009 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002010 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
Michael Haggertye404f452017-01-06 17:22:34 +01002011 int fd;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002012
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002013 if (log_ref_setup(refs, refname, force_create, &fd, err))
Michael Haggerty4533e532017-01-06 17:22:36 +01002014 return -1;
2015
Michael Haggertye404f452017-01-06 17:22:34 +01002016 if (fd >= 0)
2017 close(fd);
Michael Haggerty4533e532017-01-06 17:22:36 +01002018
2019 return 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002020}
2021
brian m. carlson4417df82017-05-06 22:10:24 +00002022static int log_ref_write_fd(int fd, const struct object_id *old_oid,
2023 const struct object_id *new_oid,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002024 const char *committer, const char *msg)
2025{
2026 int msglen, written;
2027 unsigned maxlen, len;
2028 char *logrec;
2029
2030 msglen = msg ? strlen(msg) : 0;
2031 maxlen = strlen(committer) + msglen + 100;
2032 logrec = xmalloc(maxlen);
2033 len = xsnprintf(logrec, maxlen, "%s %s %s\n",
brian m. carlson4417df82017-05-06 22:10:24 +00002034 oid_to_hex(old_oid),
2035 oid_to_hex(new_oid),
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002036 committer);
2037 if (msglen)
2038 len += copy_reflog_msg(logrec + len - 1, msg) - 1;
2039
2040 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
2041 free(logrec);
2042 if (written != len)
2043 return -1;
2044
2045 return 0;
2046}
2047
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002048static int files_log_ref_write(struct files_ref_store *refs,
brian m. carlson4417df82017-05-06 22:10:24 +00002049 const char *refname, const struct object_id *old_oid,
2050 const struct object_id *new_oid, const char *msg,
Nguyễn Thái Ngọc Duy11f84572017-03-26 09:42:15 +07002051 int flags, struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002052{
Michael Haggertye404f452017-01-06 17:22:34 +01002053 int logfd, result;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002054
Cornelius Weig341fb282017-01-27 11:09:47 +01002055 if (log_all_ref_updates == LOG_REFS_UNSET)
2056 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002057
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002058 result = log_ref_setup(refs, refname,
2059 flags & REF_FORCE_CREATE_REFLOG,
Michael Haggerty4533e532017-01-06 17:22:36 +01002060 &logfd, err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002061
2062 if (result)
2063 return result;
2064
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002065 if (logfd < 0)
2066 return 0;
brian m. carlson4417df82017-05-06 22:10:24 +00002067 result = log_ref_write_fd(logfd, old_oid, new_oid,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002068 git_committer_info(0), msg);
2069 if (result) {
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002070 struct strbuf sb = STRBUF_INIT;
Michael Haggerty87b21e02017-01-06 17:22:35 +01002071 int save_errno = errno;
2072
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002073 files_reflog_path(refs, &sb, refname);
Michael Haggerty87b21e02017-01-06 17:22:35 +01002074 strbuf_addf(err, "unable to append to '%s': %s",
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002075 sb.buf, strerror(save_errno));
2076 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002077 close(logfd);
2078 return -1;
2079 }
2080 if (close(logfd)) {
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002081 struct strbuf sb = STRBUF_INIT;
Michael Haggerty87b21e02017-01-06 17:22:35 +01002082 int save_errno = errno;
2083
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002084 files_reflog_path(refs, &sb, refname);
Michael Haggerty87b21e02017-01-06 17:22:35 +01002085 strbuf_addf(err, "unable to append to '%s': %s",
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002086 sb.buf, strerror(save_errno));
2087 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002088 return -1;
2089 }
2090 return 0;
2091}
2092
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002093/*
2094 * Write sha1 into the open lockfile, then close the lockfile. On
2095 * errors, rollback the lockfile, fill in *err and
2096 * return -1.
2097 */
2098static int write_ref_to_lockfile(struct ref_lock *lock,
brian m. carlson4417df82017-05-06 22:10:24 +00002099 const struct object_id *oid, struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002100{
2101 static char term = '\n';
2102 struct object *o;
2103 int fd;
2104
brian m. carlsonc251c832017-05-06 22:10:38 +00002105 o = parse_object(oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002106 if (!o) {
2107 strbuf_addf(err,
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002108 "trying to write ref '%s' with nonexistent object %s",
brian m. carlson4417df82017-05-06 22:10:24 +00002109 lock->ref_name, oid_to_hex(oid));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002110 unlock_ref(lock);
2111 return -1;
2112 }
2113 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2114 strbuf_addf(err,
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002115 "trying to write non-commit object %s to branch '%s'",
brian m. carlson4417df82017-05-06 22:10:24 +00002116 oid_to_hex(oid), lock->ref_name);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002117 unlock_ref(lock);
2118 return -1;
2119 }
2120 fd = get_lock_file_fd(lock->lk);
brian m. carlson4417df82017-05-06 22:10:24 +00002121 if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002122 write_in_full(fd, &term, 1) != 1 ||
2123 close_ref(lock) < 0) {
2124 strbuf_addf(err,
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002125 "couldn't write '%s'", get_lock_file_path(lock->lk));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002126 unlock_ref(lock);
2127 return -1;
2128 }
2129 return 0;
2130}
2131
2132/*
2133 * Commit a change to a loose reference that has already been written
2134 * to the loose reference lockfile. Also update the reflogs if
2135 * necessary, using the specified lockmsg (which can be NULL).
2136 */
Michael Haggertyf18a7892016-09-04 18:08:32 +02002137static int commit_ref_update(struct files_ref_store *refs,
2138 struct ref_lock *lock,
brian m. carlson4417df82017-05-06 22:10:24 +00002139 const struct object_id *oid, const char *logmsg,
Michael Haggerty5d9b2de2016-04-22 14:38:56 +02002140 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002141{
Michael Haggerty32c597e2017-02-10 12:16:16 +01002142 files_assert_main_repository(refs, "commit_ref_update");
Michael Haggerty00eebe32016-09-04 18:08:11 +02002143
2144 clear_loose_ref_cache(refs);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002145 if (files_log_ref_write(refs, lock->ref_name,
brian m. carlson4417df82017-05-06 22:10:24 +00002146 &lock->old_oid, oid,
Michael Haggerty81b1b6d2017-01-06 17:22:31 +01002147 logmsg, 0, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002148 char *old_msg = strbuf_detach(err, NULL);
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002149 strbuf_addf(err, "cannot update the ref '%s': %s",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002150 lock->ref_name, old_msg);
2151 free(old_msg);
2152 unlock_ref(lock);
2153 return -1;
2154 }
Michael Haggerty7a418f32016-04-22 15:25:25 +02002155
2156 if (strcmp(lock->ref_name, "HEAD") != 0) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002157 /*
2158 * Special hack: If a branch is updated directly and HEAD
2159 * points to it (may happen on the remote side of a push
2160 * for example) then logically the HEAD reflog should be
2161 * updated too.
2162 * A generic solution implies reverse symref information,
2163 * but finding all symrefs pointing to the given branch
2164 * would be rather costly for this rare event (the direct
2165 * update of a branch) to be worth it. So let's cheat and
2166 * check with HEAD only which should cover 99% of all usage
2167 * scenarios (even 100% of the default ones).
2168 */
brian m. carlson4417df82017-05-06 22:10:24 +00002169 struct object_id head_oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002170 int head_flag;
2171 const char *head_ref;
Michael Haggerty7a418f32016-04-22 15:25:25 +02002172
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002173 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2174 RESOLVE_REF_READING,
brian m. carlson4417df82017-05-06 22:10:24 +00002175 head_oid.hash, &head_flag);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002176 if (head_ref && (head_flag & REF_ISSYMREF) &&
2177 !strcmp(head_ref, lock->ref_name)) {
2178 struct strbuf log_err = STRBUF_INIT;
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002179 if (files_log_ref_write(refs, "HEAD",
brian m. carlson4417df82017-05-06 22:10:24 +00002180 &lock->old_oid, oid,
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002181 logmsg, 0, &log_err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002182 error("%s", log_err.buf);
2183 strbuf_release(&log_err);
2184 }
2185 }
2186 }
Michael Haggerty7a418f32016-04-22 15:25:25 +02002187
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002188 if (commit_ref(lock)) {
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002189 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002190 unlock_ref(lock);
2191 return -1;
2192 }
2193
2194 unlock_ref(lock);
2195 return 0;
2196}
2197
Jeff King370e5ad2015-12-29 00:57:01 -05002198static int create_ref_symlink(struct ref_lock *lock, const char *target)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002199{
Jeff King370e5ad2015-12-29 00:57:01 -05002200 int ret = -1;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002201#ifndef NO_SYMLINK_HEAD
Jeff King370e5ad2015-12-29 00:57:01 -05002202 char *ref_path = get_locked_file_path(lock->lk);
2203 unlink(ref_path);
2204 ret = symlink(target, ref_path);
2205 free(ref_path);
2206
2207 if (ret)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002208 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002209#endif
Jeff King370e5ad2015-12-29 00:57:01 -05002210 return ret;
2211}
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002212
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002213static void update_symref_reflog(struct files_ref_store *refs,
2214 struct ref_lock *lock, const char *refname,
Jeff King370e5ad2015-12-29 00:57:01 -05002215 const char *target, const char *logmsg)
2216{
2217 struct strbuf err = STRBUF_INIT;
brian m. carlson4417df82017-05-06 22:10:24 +00002218 struct object_id new_oid;
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002219 if (logmsg &&
2220 !refs_read_ref_full(&refs->base, target,
brian m. carlson4417df82017-05-06 22:10:24 +00002221 RESOLVE_REF_READING, new_oid.hash, NULL) &&
2222 files_log_ref_write(refs, refname, &lock->old_oid,
2223 &new_oid, logmsg, 0, &err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002224 error("%s", err.buf);
2225 strbuf_release(&err);
2226 }
Jeff King370e5ad2015-12-29 00:57:01 -05002227}
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002228
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002229static int create_symref_locked(struct files_ref_store *refs,
2230 struct ref_lock *lock, const char *refname,
Jeff King370e5ad2015-12-29 00:57:01 -05002231 const char *target, const char *logmsg)
2232{
2233 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002234 update_symref_reflog(refs, lock, refname, target, logmsg);
Jeff King370e5ad2015-12-29 00:57:01 -05002235 return 0;
2236 }
2237
2238 if (!fdopen_lock_file(lock->lk, "w"))
2239 return error("unable to fdopen %s: %s",
2240 lock->lk->tempfile.filename.buf, strerror(errno));
2241
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002242 update_symref_reflog(refs, lock, refname, target, logmsg);
Jeff King396da8f2015-12-29 00:57:25 -05002243
Jeff King370e5ad2015-12-29 00:57:01 -05002244 /* no error check; commit_ref will check ferror */
2245 fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
2246 if (commit_ref(lock) < 0)
2247 return error("unable to write symref for %s: %s", refname,
2248 strerror(errno));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002249 return 0;
2250}
2251
Michael Haggerty284689b2016-09-04 18:08:28 +02002252static int files_create_symref(struct ref_store *ref_store,
2253 const char *refname, const char *target,
2254 const char *logmsg)
Jeff King370e5ad2015-12-29 00:57:01 -05002255{
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02002256 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002257 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
Jeff King370e5ad2015-12-29 00:57:01 -05002258 struct strbuf err = STRBUF_INIT;
2259 struct ref_lock *lock;
2260 int ret;
2261
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02002262 lock = lock_ref_sha1_basic(refs, refname, NULL,
2263 NULL, NULL, REF_NODEREF, NULL,
Jeff King370e5ad2015-12-29 00:57:01 -05002264 &err);
2265 if (!lock) {
2266 error("%s", err.buf);
2267 strbuf_release(&err);
2268 return -1;
2269 }
2270
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002271 ret = create_symref_locked(refs, lock, refname, target, logmsg);
Jeff King370e5ad2015-12-29 00:57:01 -05002272 unlock_ref(lock);
2273 return ret;
2274}
2275
David Turnere3688bd2016-09-04 18:08:38 +02002276static int files_reflog_exists(struct ref_store *ref_store,
2277 const char *refname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002278{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002279 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002280 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002281 struct strbuf sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002282 struct stat st;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002283 int ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002284
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002285 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002286 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
2287 strbuf_release(&sb);
2288 return ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002289}
2290
David Turnere3688bd2016-09-04 18:08:38 +02002291static int files_delete_reflog(struct ref_store *ref_store,
2292 const char *refname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002293{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002294 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002295 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002296 struct strbuf sb = STRBUF_INIT;
2297 int ret;
2298
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002299 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002300 ret = remove_path(sb.buf);
2301 strbuf_release(&sb);
2302 return ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002303}
2304
2305static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
2306{
brian m. carlson9461d272017-02-21 23:47:32 +00002307 struct object_id ooid, noid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002308 char *email_end, *message;
Johannes Schindelindddbad72017-04-26 21:29:31 +02002309 timestamp_t timestamp;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002310 int tz;
brian m. carlson43bc3b62017-02-21 23:47:33 +00002311 const char *p = sb->buf;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002312
2313 /* old SP new SP name <email> SP time TAB msg LF */
brian m. carlson43bc3b62017-02-21 23:47:33 +00002314 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
2315 parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||
2316 parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
2317 !(email_end = strchr(p, '>')) ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002318 email_end[1] != ' ' ||
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +02002319 !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002320 !message || message[0] != ' ' ||
2321 (message[1] != '+' && message[1] != '-') ||
2322 !isdigit(message[2]) || !isdigit(message[3]) ||
2323 !isdigit(message[4]) || !isdigit(message[5]))
2324 return 0; /* corrupt? */
2325 email_end[1] = '\0';
2326 tz = strtol(message + 1, NULL, 10);
2327 if (message[6] != '\t')
2328 message += 6;
2329 else
2330 message += 7;
brian m. carlson43bc3b62017-02-21 23:47:33 +00002331 return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002332}
2333
2334static char *find_beginning_of_line(char *bob, char *scan)
2335{
2336 while (bob < scan && *(--scan) != '\n')
2337 ; /* keep scanning backwards */
2338 /*
2339 * Return either beginning of the buffer, or LF at the end of
2340 * the previous line.
2341 */
2342 return scan;
2343}
2344
David Turnere3688bd2016-09-04 18:08:38 +02002345static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2346 const char *refname,
2347 each_reflog_ent_fn fn,
2348 void *cb_data)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002349{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002350 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002351 files_downcast(ref_store, REF_STORE_READ,
2352 "for_each_reflog_ent_reverse");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002353 struct strbuf sb = STRBUF_INIT;
2354 FILE *logfp;
2355 long pos;
2356 int ret = 0, at_tail = 1;
2357
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002358 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002359 logfp = fopen(sb.buf, "r");
2360 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002361 if (!logfp)
2362 return -1;
2363
2364 /* Jump to the end */
2365 if (fseek(logfp, 0, SEEK_END) < 0)
René Scharfebe686f02017-04-16 18:55:46 +02002366 ret = error("cannot seek back reflog for %s: %s",
2367 refname, strerror(errno));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002368 pos = ftell(logfp);
2369 while (!ret && 0 < pos) {
2370 int cnt;
2371 size_t nread;
2372 char buf[BUFSIZ];
2373 char *endp, *scanp;
2374
2375 /* Fill next block from the end */
2376 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
René Scharfebe686f02017-04-16 18:55:46 +02002377 if (fseek(logfp, pos - cnt, SEEK_SET)) {
2378 ret = error("cannot seek back reflog for %s: %s",
2379 refname, strerror(errno));
2380 break;
2381 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002382 nread = fread(buf, cnt, 1, logfp);
René Scharfebe686f02017-04-16 18:55:46 +02002383 if (nread != 1) {
2384 ret = error("cannot read %d bytes from reflog for %s: %s",
2385 cnt, refname, strerror(errno));
2386 break;
2387 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002388 pos -= cnt;
2389
2390 scanp = endp = buf + cnt;
2391 if (at_tail && scanp[-1] == '\n')
2392 /* Looking at the final LF at the end of the file */
2393 scanp--;
2394 at_tail = 0;
2395
2396 while (buf < scanp) {
2397 /*
2398 * terminating LF of the previous line, or the beginning
2399 * of the buffer.
2400 */
2401 char *bp;
2402
2403 bp = find_beginning_of_line(buf, scanp);
2404
2405 if (*bp == '\n') {
2406 /*
2407 * The newline is the end of the previous line,
2408 * so we know we have complete line starting
2409 * at (bp + 1). Prefix it onto any prior data
2410 * we collected for the line and process it.
2411 */
2412 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2413 scanp = bp;
2414 endp = bp + 1;
2415 ret = show_one_reflog_ent(&sb, fn, cb_data);
2416 strbuf_reset(&sb);
2417 if (ret)
2418 break;
2419 } else if (!pos) {
2420 /*
2421 * We are at the start of the buffer, and the
2422 * start of the file; there is no previous
2423 * line, and we have everything for this one.
2424 * Process it, and we can end the loop.
2425 */
2426 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2427 ret = show_one_reflog_ent(&sb, fn, cb_data);
2428 strbuf_reset(&sb);
2429 break;
2430 }
2431
2432 if (bp == buf) {
2433 /*
2434 * We are at the start of the buffer, and there
2435 * is more file to read backwards. Which means
2436 * we are in the middle of a line. Note that we
2437 * may get here even if *bp was a newline; that
2438 * just means we are at the exact end of the
2439 * previous line, rather than some spot in the
2440 * middle.
2441 *
2442 * Save away what we have to be combined with
2443 * the data from the next read.
2444 */
2445 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2446 break;
2447 }
2448 }
2449
2450 }
2451 if (!ret && sb.len)
2452 die("BUG: reverse reflog parser had leftover data");
2453
2454 fclose(logfp);
2455 strbuf_release(&sb);
2456 return ret;
2457}
2458
David Turnere3688bd2016-09-04 18:08:38 +02002459static int files_for_each_reflog_ent(struct ref_store *ref_store,
2460 const char *refname,
2461 each_reflog_ent_fn fn, void *cb_data)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002462{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002463 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002464 files_downcast(ref_store, REF_STORE_READ,
2465 "for_each_reflog_ent");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002466 FILE *logfp;
2467 struct strbuf sb = STRBUF_INIT;
2468 int ret = 0;
2469
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002470 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002471 logfp = fopen(sb.buf, "r");
2472 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002473 if (!logfp)
2474 return -1;
2475
2476 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2477 ret = show_one_reflog_ent(&sb, fn, cb_data);
2478 fclose(logfp);
2479 strbuf_release(&sb);
2480 return ret;
2481}
Michael Haggerty2880d162016-06-18 06:15:19 +02002482
2483struct files_reflog_iterator {
2484 struct ref_iterator base;
2485
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002486 struct ref_store *ref_store;
Michael Haggerty2880d162016-06-18 06:15:19 +02002487 struct dir_iterator *dir_iterator;
2488 struct object_id oid;
2489};
2490
2491static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002492{
Michael Haggerty2880d162016-06-18 06:15:19 +02002493 struct files_reflog_iterator *iter =
2494 (struct files_reflog_iterator *)ref_iterator;
2495 struct dir_iterator *diter = iter->dir_iterator;
2496 int ok;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002497
Michael Haggerty2880d162016-06-18 06:15:19 +02002498 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2499 int flags;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002500
Michael Haggerty2880d162016-06-18 06:15:19 +02002501 if (!S_ISREG(diter->st.st_mode))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002502 continue;
Michael Haggerty2880d162016-06-18 06:15:19 +02002503 if (diter->basename[0] == '.')
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002504 continue;
Michael Haggerty2880d162016-06-18 06:15:19 +02002505 if (ends_with(diter->basename, ".lock"))
2506 continue;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002507
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002508 if (refs_read_ref_full(iter->ref_store,
2509 diter->relative_path, 0,
2510 iter->oid.hash, &flags)) {
Michael Haggerty2880d162016-06-18 06:15:19 +02002511 error("bad ref for %s", diter->path.buf);
2512 continue;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002513 }
Michael Haggerty2880d162016-06-18 06:15:19 +02002514
2515 iter->base.refname = diter->relative_path;
2516 iter->base.oid = &iter->oid;
2517 iter->base.flags = flags;
2518 return ITER_OK;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002519 }
Michael Haggerty2880d162016-06-18 06:15:19 +02002520
2521 iter->dir_iterator = NULL;
2522 if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
2523 ok = ITER_ERROR;
2524 return ok;
2525}
2526
2527static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2528 struct object_id *peeled)
2529{
2530 die("BUG: ref_iterator_peel() called for reflog_iterator");
2531}
2532
2533static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
2534{
2535 struct files_reflog_iterator *iter =
2536 (struct files_reflog_iterator *)ref_iterator;
2537 int ok = ITER_DONE;
2538
2539 if (iter->dir_iterator)
2540 ok = dir_iterator_abort(iter->dir_iterator);
2541
2542 base_ref_iterator_free(ref_iterator);
2543 return ok;
2544}
2545
2546static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2547 files_reflog_iterator_advance,
2548 files_reflog_iterator_peel,
2549 files_reflog_iterator_abort
2550};
2551
David Turnere3688bd2016-09-04 18:08:38 +02002552static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
Michael Haggerty2880d162016-06-18 06:15:19 +02002553{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002554 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002555 files_downcast(ref_store, REF_STORE_READ,
2556 "reflog_iterator_begin");
Michael Haggerty2880d162016-06-18 06:15:19 +02002557 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2558 struct ref_iterator *ref_iterator = &iter->base;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002559 struct strbuf sb = STRBUF_INIT;
Michael Haggerty2880d162016-06-18 06:15:19 +02002560
2561 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002562 files_reflog_path(refs, &sb, NULL);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002563 iter->dir_iterator = dir_iterator_begin(sb.buf);
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002564 iter->ref_store = ref_store;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002565 strbuf_release(&sb);
Michael Haggerty2880d162016-06-18 06:15:19 +02002566 return ref_iterator;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002567}
2568
Michael Haggerty165056b2016-04-24 08:58:41 +02002569/*
Michael Haggerty92b15512016-04-25 15:56:07 +02002570 * If update is a direct update of head_ref (the reference pointed to
2571 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
Michael Haggerty165056b2016-04-24 08:58:41 +02002572 */
Michael Haggerty92b15512016-04-25 15:56:07 +02002573static int split_head_update(struct ref_update *update,
2574 struct ref_transaction *transaction,
2575 const char *head_ref,
2576 struct string_list *affected_refnames,
2577 struct strbuf *err)
2578{
2579 struct string_list_item *item;
2580 struct ref_update *new_update;
2581
2582 if ((update->flags & REF_LOG_ONLY) ||
2583 (update->flags & REF_ISPRUNING) ||
2584 (update->flags & REF_UPDATE_VIA_HEAD))
2585 return 0;
2586
2587 if (strcmp(update->refname, head_ref))
2588 return 0;
2589
2590 /*
2591 * First make sure that HEAD is not already in the
2592 * transaction. This insertion is O(N) in the transaction
2593 * size, but it happens at most once per transaction.
2594 */
2595 item = string_list_insert(affected_refnames, "HEAD");
2596 if (item->util) {
2597 /* An entry already existed */
2598 strbuf_addf(err,
2599 "multiple updates for 'HEAD' (including one "
2600 "via its referent '%s') are not allowed",
2601 update->refname);
2602 return TRANSACTION_NAME_CONFLICT;
2603 }
2604
2605 new_update = ref_transaction_add_update(
2606 transaction, "HEAD",
2607 update->flags | REF_LOG_ONLY | REF_NODEREF,
brian m. carlson98491292017-05-06 22:10:23 +00002608 update->new_oid.hash, update->old_oid.hash,
Michael Haggerty92b15512016-04-25 15:56:07 +02002609 update->msg);
2610
2611 item->util = new_update;
2612
2613 return 0;
2614}
2615
2616/*
2617 * update is for a symref that points at referent and doesn't have
2618 * REF_NODEREF set. Split it into two updates:
2619 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2620 * - A new, separate update for the referent reference
2621 * Note that the new update will itself be subject to splitting when
2622 * the iteration gets to it.
2623 */
Michael Haggertyfcc42ea2016-09-04 18:08:35 +02002624static int split_symref_update(struct files_ref_store *refs,
2625 struct ref_update *update,
Michael Haggerty92b15512016-04-25 15:56:07 +02002626 const char *referent,
Michael Haggerty165056b2016-04-24 08:58:41 +02002627 struct ref_transaction *transaction,
2628 struct string_list *affected_refnames,
2629 struct strbuf *err)
2630{
Michael Haggerty92b15512016-04-25 15:56:07 +02002631 struct string_list_item *item;
2632 struct ref_update *new_update;
2633 unsigned int new_flags;
Michael Haggerty165056b2016-04-24 08:58:41 +02002634
Michael Haggerty92b15512016-04-25 15:56:07 +02002635 /*
2636 * First make sure that referent is not already in the
Martin Ågrenc2994682017-09-09 08:57:15 +02002637 * transaction. This check is O(lg N) in the transaction
Michael Haggerty92b15512016-04-25 15:56:07 +02002638 * size, but it happens at most once per symref in a
2639 * transaction.
2640 */
Martin Ågrenc2994682017-09-09 08:57:15 +02002641 if (string_list_has_string(affected_refnames, referent)) {
2642 /* An entry already exists */
Michael Haggerty92b15512016-04-25 15:56:07 +02002643 strbuf_addf(err,
2644 "multiple updates for '%s' (including one "
2645 "via symref '%s') are not allowed",
2646 referent, update->refname);
2647 return TRANSACTION_NAME_CONFLICT;
2648 }
2649
2650 new_flags = update->flags;
2651 if (!strcmp(update->refname, "HEAD")) {
2652 /*
2653 * Record that the new update came via HEAD, so that
2654 * when we process it, split_head_update() doesn't try
2655 * to add another reflog update for HEAD. Note that
2656 * this bit will be propagated if the new_update
2657 * itself needs to be split.
2658 */
2659 new_flags |= REF_UPDATE_VIA_HEAD;
2660 }
2661
2662 new_update = ref_transaction_add_update(
2663 transaction, referent, new_flags,
brian m. carlson98491292017-05-06 22:10:23 +00002664 update->new_oid.hash, update->old_oid.hash,
Michael Haggerty92b15512016-04-25 15:56:07 +02002665 update->msg);
2666
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002667 new_update->parent_update = update;
2668
2669 /*
2670 * Change the symbolic ref update to log only. Also, it
2671 * doesn't need to check its old SHA-1 value, as that will be
2672 * done when new_update is processed.
2673 */
Michael Haggerty92b15512016-04-25 15:56:07 +02002674 update->flags |= REF_LOG_ONLY | REF_NODEREF;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002675 update->flags &= ~REF_HAVE_OLD;
Michael Haggerty92b15512016-04-25 15:56:07 +02002676
Martin Ågrenc2994682017-09-09 08:57:15 +02002677 /*
2678 * Add the referent. This insertion is O(N) in the transaction
2679 * size, but it happens at most once per symref in a
2680 * transaction. Make sure to add new_update->refname, which will
2681 * be valid as long as affected_refnames is in use, and NOT
2682 * referent, which might soon be freed by our caller.
2683 */
2684 item = string_list_insert(affected_refnames, new_update->refname);
2685 if (item->util)
2686 BUG("%s unexpectedly found in affected_refnames",
2687 new_update->refname);
Michael Haggerty92b15512016-04-25 15:56:07 +02002688 item->util = new_update;
2689
2690 return 0;
2691}
2692
2693/*
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002694 * Return the refname under which update was originally requested.
2695 */
2696static const char *original_update_refname(struct ref_update *update)
2697{
2698 while (update->parent_update)
2699 update = update->parent_update;
2700
2701 return update->refname;
2702}
2703
2704/*
Michael Haggertye3f51032016-06-07 09:29:23 +02002705 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2706 * are consistent with oid, which is the reference's current value. If
2707 * everything is OK, return 0; otherwise, write an error message to
2708 * err and return -1.
2709 */
2710static int check_old_oid(struct ref_update *update, struct object_id *oid,
2711 struct strbuf *err)
2712{
2713 if (!(update->flags & REF_HAVE_OLD) ||
brian m. carlson98491292017-05-06 22:10:23 +00002714 !oidcmp(oid, &update->old_oid))
Michael Haggertye3f51032016-06-07 09:29:23 +02002715 return 0;
2716
brian m. carlson98491292017-05-06 22:10:23 +00002717 if (is_null_oid(&update->old_oid))
Michael Haggertye3f51032016-06-07 09:29:23 +02002718 strbuf_addf(err, "cannot lock ref '%s': "
2719 "reference already exists",
2720 original_update_refname(update));
2721 else if (is_null_oid(oid))
2722 strbuf_addf(err, "cannot lock ref '%s': "
2723 "reference is missing but expected %s",
2724 original_update_refname(update),
brian m. carlson98491292017-05-06 22:10:23 +00002725 oid_to_hex(&update->old_oid));
Michael Haggertye3f51032016-06-07 09:29:23 +02002726 else
2727 strbuf_addf(err, "cannot lock ref '%s': "
2728 "is at %s but expected %s",
2729 original_update_refname(update),
2730 oid_to_hex(oid),
brian m. carlson98491292017-05-06 22:10:23 +00002731 oid_to_hex(&update->old_oid));
Michael Haggertye3f51032016-06-07 09:29:23 +02002732
2733 return -1;
2734}
2735
2736/*
Michael Haggerty92b15512016-04-25 15:56:07 +02002737 * Prepare for carrying out update:
2738 * - Lock the reference referred to by update.
2739 * - Read the reference under lock.
2740 * - Check that its old SHA-1 value (if specified) is correct, and in
2741 * any case record it in update->lock->old_oid for later use when
2742 * writing the reflog.
2743 * - If it is a symref update without REF_NODEREF, split it up into a
2744 * REF_LOG_ONLY update of the symref and add a separate update for
2745 * the referent to transaction.
2746 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2747 * update of HEAD.
2748 */
Michael Haggertyb3bbbc52016-09-04 18:08:33 +02002749static int lock_ref_for_update(struct files_ref_store *refs,
2750 struct ref_update *update,
Michael Haggerty92b15512016-04-25 15:56:07 +02002751 struct ref_transaction *transaction,
2752 const char *head_ref,
2753 struct string_list *affected_refnames,
2754 struct strbuf *err)
2755{
2756 struct strbuf referent = STRBUF_INIT;
2757 int mustexist = (update->flags & REF_HAVE_OLD) &&
brian m. carlson98491292017-05-06 22:10:23 +00002758 !is_null_oid(&update->old_oid);
Martin Ågren851e1fb2017-09-09 08:57:16 +02002759 int ret = 0;
Michael Haggerty92b15512016-04-25 15:56:07 +02002760 struct ref_lock *lock;
2761
Michael Haggerty32c597e2017-02-10 12:16:16 +01002762 files_assert_main_repository(refs, "lock_ref_for_update");
Michael Haggertyb3bbbc52016-09-04 18:08:33 +02002763
brian m. carlson98491292017-05-06 22:10:23 +00002764 if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid))
Michael Haggerty165056b2016-04-24 08:58:41 +02002765 update->flags |= REF_DELETING;
Michael Haggerty92b15512016-04-25 15:56:07 +02002766
2767 if (head_ref) {
2768 ret = split_head_update(update, transaction, head_ref,
2769 affected_refnames, err);
2770 if (ret)
Martin Ågren851e1fb2017-09-09 08:57:16 +02002771 goto out;
Michael Haggerty92b15512016-04-25 15:56:07 +02002772 }
2773
Michael Haggertyf7b0a982016-09-04 18:08:31 +02002774 ret = lock_raw_ref(refs, update->refname, mustexist,
Michael Haggerty92b15512016-04-25 15:56:07 +02002775 affected_refnames, NULL,
David Turner7d618262016-09-04 18:08:43 +02002776 &lock, &referent,
Michael Haggerty92b15512016-04-25 15:56:07 +02002777 &update->type, err);
Michael Haggerty92b15512016-04-25 15:56:07 +02002778 if (ret) {
Michael Haggerty165056b2016-04-24 08:58:41 +02002779 char *reason;
2780
Michael Haggerty165056b2016-04-24 08:58:41 +02002781 reason = strbuf_detach(err, NULL);
2782 strbuf_addf(err, "cannot lock ref '%s': %s",
Michael Haggertye3f51032016-06-07 09:29:23 +02002783 original_update_refname(update), reason);
Michael Haggerty165056b2016-04-24 08:58:41 +02002784 free(reason);
Martin Ågren851e1fb2017-09-09 08:57:16 +02002785 goto out;
Michael Haggerty165056b2016-04-24 08:58:41 +02002786 }
Michael Haggerty92b15512016-04-25 15:56:07 +02002787
David Turner7d618262016-09-04 18:08:43 +02002788 update->backend_data = lock;
Michael Haggerty92b15512016-04-25 15:56:07 +02002789
Michael Haggerty92b15512016-04-25 15:56:07 +02002790 if (update->type & REF_ISSYMREF) {
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002791 if (update->flags & REF_NODEREF) {
2792 /*
2793 * We won't be reading the referent as part of
2794 * the transaction, so we have to read it here
2795 * to record and possibly check old_sha1:
2796 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002797 if (refs_read_ref_full(&refs->base,
2798 referent.buf, 0,
2799 lock->old_oid.hash, NULL)) {
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002800 if (update->flags & REF_HAVE_OLD) {
2801 strbuf_addf(err, "cannot lock ref '%s': "
Michael Haggertye3f51032016-06-07 09:29:23 +02002802 "error reading reference",
2803 original_update_refname(update));
Martin Ågren851e1fb2017-09-09 08:57:16 +02002804 ret = -1;
2805 goto out;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002806 }
Michael Haggertye3f51032016-06-07 09:29:23 +02002807 } else if (check_old_oid(update, &lock->old_oid, err)) {
Martin Ågren851e1fb2017-09-09 08:57:16 +02002808 ret = TRANSACTION_GENERIC_ERROR;
2809 goto out;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002810 }
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002811 } else {
2812 /*
2813 * Create a new update for the reference this
2814 * symref is pointing at. Also, we will record
2815 * and verify old_sha1 for this update as part
2816 * of processing the split-off update, so we
2817 * don't have to do it here.
2818 */
Michael Haggertyfcc42ea2016-09-04 18:08:35 +02002819 ret = split_symref_update(refs, update,
2820 referent.buf, transaction,
Michael Haggerty92b15512016-04-25 15:56:07 +02002821 affected_refnames, err);
2822 if (ret)
Martin Ågren851e1fb2017-09-09 08:57:16 +02002823 goto out;
Michael Haggerty92b15512016-04-25 15:56:07 +02002824 }
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002825 } else {
2826 struct ref_update *parent_update;
Michael Haggerty8169d0d2016-04-25 17:38:35 +02002827
Martin Ågren851e1fb2017-09-09 08:57:16 +02002828 if (check_old_oid(update, &lock->old_oid, err)) {
2829 ret = TRANSACTION_GENERIC_ERROR;
2830 goto out;
2831 }
Michael Haggertye3f51032016-06-07 09:29:23 +02002832
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002833 /*
2834 * If this update is happening indirectly because of a
2835 * symref update, record the old SHA-1 in the parent
2836 * update:
2837 */
2838 for (parent_update = update->parent_update;
2839 parent_update;
2840 parent_update = parent_update->parent_update) {
David Turner7d618262016-09-04 18:08:43 +02002841 struct ref_lock *parent_lock = parent_update->backend_data;
2842 oidcpy(&parent_lock->old_oid, &lock->old_oid);
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002843 }
Michael Haggerty92b15512016-04-25 15:56:07 +02002844 }
2845
Michael Haggerty165056b2016-04-24 08:58:41 +02002846 if ((update->flags & REF_HAVE_NEW) &&
2847 !(update->flags & REF_DELETING) &&
2848 !(update->flags & REF_LOG_ONLY)) {
Michael Haggerty92b15512016-04-25 15:56:07 +02002849 if (!(update->type & REF_ISSYMREF) &&
brian m. carlson98491292017-05-06 22:10:23 +00002850 !oidcmp(&lock->old_oid, &update->new_oid)) {
Michael Haggerty165056b2016-04-24 08:58:41 +02002851 /*
2852 * The reference already has the desired
2853 * value, so we don't need to write it.
2854 */
brian m. carlson4417df82017-05-06 22:10:24 +00002855 } else if (write_ref_to_lockfile(lock, &update->new_oid,
Michael Haggerty165056b2016-04-24 08:58:41 +02002856 err)) {
2857 char *write_err = strbuf_detach(err, NULL);
2858
2859 /*
2860 * The lock was freed upon failure of
2861 * write_ref_to_lockfile():
2862 */
David Turner7d618262016-09-04 18:08:43 +02002863 update->backend_data = NULL;
Michael Haggerty165056b2016-04-24 08:58:41 +02002864 strbuf_addf(err,
Michael Haggertye3f51032016-06-07 09:29:23 +02002865 "cannot update ref '%s': %s",
Michael Haggerty165056b2016-04-24 08:58:41 +02002866 update->refname, write_err);
2867 free(write_err);
Martin Ågren851e1fb2017-09-09 08:57:16 +02002868 ret = TRANSACTION_GENERIC_ERROR;
2869 goto out;
Michael Haggerty165056b2016-04-24 08:58:41 +02002870 } else {
2871 update->flags |= REF_NEEDS_COMMIT;
2872 }
2873 }
2874 if (!(update->flags & REF_NEEDS_COMMIT)) {
2875 /*
2876 * We didn't call write_ref_to_lockfile(), so
2877 * the lockfile is still open. Close it to
2878 * free up the file descriptor:
2879 */
Michael Haggerty92b15512016-04-25 15:56:07 +02002880 if (close_ref(lock)) {
Michael Haggerty165056b2016-04-24 08:58:41 +02002881 strbuf_addf(err, "couldn't close '%s.lock'",
2882 update->refname);
Martin Ågren851e1fb2017-09-09 08:57:16 +02002883 ret = TRANSACTION_GENERIC_ERROR;
2884 goto out;
Michael Haggerty165056b2016-04-24 08:58:41 +02002885 }
2886 }
Martin Ågren851e1fb2017-09-09 08:57:16 +02002887
2888out:
2889 strbuf_release(&referent);
2890 return ret;
Michael Haggerty165056b2016-04-24 08:58:41 +02002891}
2892
Michael Haggertyc0ca9352017-05-22 16:17:42 +02002893/*
2894 * Unlock any references in `transaction` that are still locked, and
2895 * mark the transaction closed.
2896 */
2897static void files_transaction_cleanup(struct ref_transaction *transaction)
2898{
2899 size_t i;
2900
2901 for (i = 0; i < transaction->nr; i++) {
2902 struct ref_update *update = transaction->updates[i];
2903 struct ref_lock *lock = update->backend_data;
2904
2905 if (lock) {
2906 unlock_ref(lock);
2907 update->backend_data = NULL;
2908 }
2909 }
2910
2911 transaction->state = REF_TRANSACTION_CLOSED;
2912}
2913
Michael Haggerty30173b82017-05-22 16:17:44 +02002914static int files_transaction_prepare(struct ref_store *ref_store,
2915 struct ref_transaction *transaction,
2916 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002917{
Michael Haggerty00eebe32016-09-04 18:08:11 +02002918 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002919 files_downcast(ref_store, REF_STORE_WRITE,
Michael Haggerty30173b82017-05-22 16:17:44 +02002920 "ref_transaction_prepare");
Michael Haggerty43a2dfd2017-05-22 16:17:37 +02002921 size_t i;
2922 int ret = 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002923 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
Michael Haggerty92b15512016-04-25 15:56:07 +02002924 char *head_ref = NULL;
2925 int head_type;
2926 struct object_id head_oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002927
2928 assert(err);
2929
Michael Haggertyc0ca9352017-05-22 16:17:42 +02002930 if (!transaction->nr)
2931 goto cleanup;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002932
Michael Haggerty92b15512016-04-25 15:56:07 +02002933 /*
2934 * Fail if a refname appears more than once in the
2935 * transaction. (If we end up splitting up any updates using
2936 * split_symref_update() or split_head_update(), those
2937 * functions will check that the new updates don't have the
2938 * same refname as any existing ones.)
2939 */
2940 for (i = 0; i < transaction->nr; i++) {
2941 struct ref_update *update = transaction->updates[i];
2942 struct string_list_item *item =
2943 string_list_append(&affected_refnames, update->refname);
2944
2945 /*
2946 * We store a pointer to update in item->util, but at
2947 * the moment we never use the value of this field
2948 * except to check whether it is non-NULL.
2949 */
2950 item->util = update;
2951 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002952 string_list_sort(&affected_refnames);
2953 if (ref_update_reject_duplicates(&affected_refnames, err)) {
2954 ret = TRANSACTION_GENERIC_ERROR;
2955 goto cleanup;
2956 }
2957
2958 /*
Michael Haggerty92b15512016-04-25 15:56:07 +02002959 * Special hack: If a branch is updated directly and HEAD
2960 * points to it (may happen on the remote side of a push
2961 * for example) then logically the HEAD reflog should be
2962 * updated too.
2963 *
2964 * A generic solution would require reverse symref lookups,
2965 * but finding all symrefs pointing to a given branch would be
2966 * rather costly for this rare event (the direct update of a
2967 * branch) to be worth it. So let's cheat and check with HEAD
2968 * only, which should cover 99% of all usage scenarios (even
2969 * 100% of the default ones).
2970 *
2971 * So if HEAD is a symbolic reference, then record the name of
2972 * the reference that it points to. If we see an update of
2973 * head_ref within the transaction, then split_head_update()
2974 * arranges for the reflog of HEAD to be updated, too.
2975 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002976 head_ref = refs_resolve_refdup(ref_store, "HEAD",
2977 RESOLVE_REF_NO_RECURSE,
2978 head_oid.hash, &head_type);
Michael Haggerty92b15512016-04-25 15:56:07 +02002979
2980 if (head_ref && !(head_type & REF_ISSYMREF)) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002981 FREE_AND_NULL(head_ref);
Michael Haggerty92b15512016-04-25 15:56:07 +02002982 }
2983
2984 /*
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002985 * Acquire all locks, verify old values if provided, check
2986 * that new values are valid, and write new values to the
2987 * lockfiles, ready to be activated. Only keep one lockfile
2988 * open at a time to avoid running out of file descriptors.
Michael Haggerty30173b82017-05-22 16:17:44 +02002989 * Note that lock_ref_for_update() might append more updates
2990 * to the transaction.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002991 */
Michael Haggertyefe47282016-04-22 00:02:50 +02002992 for (i = 0; i < transaction->nr; i++) {
2993 struct ref_update *update = transaction->updates[i];
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002994
Michael Haggertyb3bbbc52016-09-04 18:08:33 +02002995 ret = lock_ref_for_update(refs, update, transaction,
2996 head_ref, &affected_refnames, err);
Michael Haggerty165056b2016-04-24 08:58:41 +02002997 if (ret)
Michael Haggerty30173b82017-05-22 16:17:44 +02002998 break;
2999 }
3000
3001cleanup:
3002 free(head_ref);
3003 string_list_clear(&affected_refnames, 0);
3004
3005 if (ret)
3006 files_transaction_cleanup(transaction);
3007 else
3008 transaction->state = REF_TRANSACTION_PREPARED;
3009
3010 return ret;
3011}
3012
3013static int files_transaction_finish(struct ref_store *ref_store,
3014 struct ref_transaction *transaction,
3015 struct strbuf *err)
3016{
3017 struct files_ref_store *refs =
3018 files_downcast(ref_store, 0, "ref_transaction_finish");
3019 size_t i;
3020 int ret = 0;
3021 struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
3022 struct string_list_item *ref_to_delete;
3023 struct strbuf sb = STRBUF_INIT;
3024
3025 assert(err);
3026
3027 if (!transaction->nr) {
3028 transaction->state = REF_TRANSACTION_CLOSED;
3029 return 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003030 }
3031
3032 /* Perform updates first so live commits remain referenced */
Michael Haggertyefe47282016-04-22 00:02:50 +02003033 for (i = 0; i < transaction->nr; i++) {
3034 struct ref_update *update = transaction->updates[i];
David Turner7d618262016-09-04 18:08:43 +02003035 struct ref_lock *lock = update->backend_data;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003036
David Turnerd99aa882016-02-24 17:58:50 -05003037 if (update->flags & REF_NEEDS_COMMIT ||
3038 update->flags & REF_LOG_ONLY) {
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003039 if (files_log_ref_write(refs,
3040 lock->ref_name,
brian m. carlson4417df82017-05-06 22:10:24 +00003041 &lock->old_oid,
3042 &update->new_oid,
Michael Haggerty81b1b6d2017-01-06 17:22:31 +01003043 update->msg, update->flags,
3044 err)) {
Michael Haggerty92b15512016-04-25 15:56:07 +02003045 char *old_msg = strbuf_detach(err, NULL);
3046
3047 strbuf_addf(err, "cannot update the ref '%s': %s",
3048 lock->ref_name, old_msg);
3049 free(old_msg);
3050 unlock_ref(lock);
David Turner7d618262016-09-04 18:08:43 +02003051 update->backend_data = NULL;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003052 ret = TRANSACTION_GENERIC_ERROR;
3053 goto cleanup;
Michael Haggerty92b15512016-04-25 15:56:07 +02003054 }
3055 }
3056 if (update->flags & REF_NEEDS_COMMIT) {
Michael Haggerty00eebe32016-09-04 18:08:11 +02003057 clear_loose_ref_cache(refs);
Michael Haggerty92b15512016-04-25 15:56:07 +02003058 if (commit_ref(lock)) {
3059 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3060 unlock_ref(lock);
David Turner7d618262016-09-04 18:08:43 +02003061 update->backend_data = NULL;
Michael Haggerty92b15512016-04-25 15:56:07 +02003062 ret = TRANSACTION_GENERIC_ERROR;
3063 goto cleanup;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003064 }
3065 }
3066 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003067 /* Perform deletes now that updates are safely completed */
Michael Haggertyefe47282016-04-22 00:02:50 +02003068 for (i = 0; i < transaction->nr; i++) {
3069 struct ref_update *update = transaction->updates[i];
David Turner7d618262016-09-04 18:08:43 +02003070 struct ref_lock *lock = update->backend_data;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003071
David Turnerd99aa882016-02-24 17:58:50 -05003072 if (update->flags & REF_DELETING &&
3073 !(update->flags & REF_LOG_ONLY)) {
Michael Haggertyce0af242017-01-06 17:22:39 +01003074 if (!(update->type & REF_ISPACKED) ||
3075 update->type & REF_ISSYMREF) {
3076 /* It is a loose reference. */
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003077 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003078 files_ref_path(refs, &sb, lock->ref_name);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003079 if (unlink_or_msg(sb.buf, err)) {
Michael Haggertyce0af242017-01-06 17:22:39 +01003080 ret = TRANSACTION_GENERIC_ERROR;
3081 goto cleanup;
3082 }
Michael Haggerty44639772017-01-06 17:22:43 +01003083 update->flags |= REF_DELETED_LOOSE;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003084 }
3085
3086 if (!(update->flags & REF_ISPRUNING))
3087 string_list_append(&refs_to_delete,
David Turner7d618262016-09-04 18:08:43 +02003088 lock->ref_name);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003089 }
3090 }
3091
Michael Haggerty0a95ac52016-09-04 18:08:30 +02003092 if (repack_without_refs(refs, &refs_to_delete, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003093 ret = TRANSACTION_GENERIC_ERROR;
3094 goto cleanup;
3095 }
Michael Haggerty44639772017-01-06 17:22:43 +01003096
3097 /* Delete the reflogs of any references that were deleted: */
3098 for_each_string_list_item(ref_to_delete, &refs_to_delete) {
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003099 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003100 files_reflog_path(refs, &sb, ref_to_delete->string);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003101 if (!unlink_or_warn(sb.buf))
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003102 try_remove_empty_parents(refs, ref_to_delete->string,
Michael Haggerty44639772017-01-06 17:22:43 +01003103 REMOVE_EMPTY_PARENTS_REFLOG);
3104 }
3105
Michael Haggerty00eebe32016-09-04 18:08:11 +02003106 clear_loose_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003107
3108cleanup:
Michael Haggertyc0ca9352017-05-22 16:17:42 +02003109 files_transaction_cleanup(transaction);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003110
Michael Haggerty44639772017-01-06 17:22:43 +01003111 for (i = 0; i < transaction->nr; i++) {
3112 struct ref_update *update = transaction->updates[i];
Michael Haggerty44639772017-01-06 17:22:43 +01003113
3114 if (update->flags & REF_DELETED_LOOSE) {
3115 /*
3116 * The loose reference was deleted. Delete any
3117 * empty parent directories. (Note that this
3118 * can only work because we have already
3119 * removed the lockfile.)
3120 */
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003121 try_remove_empty_parents(refs, update->refname,
Michael Haggerty44639772017-01-06 17:22:43 +01003122 REMOVE_EMPTY_PARENTS_REF);
3123 }
3124 }
3125
Michael Haggerty30173b82017-05-22 16:17:44 +02003126 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003127 string_list_clear(&refs_to_delete, 0);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003128 return ret;
3129}
3130
Michael Haggerty30173b82017-05-22 16:17:44 +02003131static int files_transaction_abort(struct ref_store *ref_store,
3132 struct ref_transaction *transaction,
3133 struct strbuf *err)
3134{
3135 files_transaction_cleanup(transaction);
3136 return 0;
3137}
3138
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003139static int ref_present(const char *refname,
3140 const struct object_id *oid, int flags, void *cb_data)
3141{
3142 struct string_list *affected_refnames = cb_data;
3143
3144 return string_list_has_string(affected_refnames, refname);
3145}
3146
David Turnerfc681462016-09-04 18:08:39 +02003147static int files_initial_transaction_commit(struct ref_store *ref_store,
3148 struct ref_transaction *transaction,
3149 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003150{
Michael Haggertyd99825a2016-09-04 18:08:12 +02003151 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07003152 files_downcast(ref_store, REF_STORE_WRITE,
3153 "initial_ref_transaction_commit");
Michael Haggerty43a2dfd2017-05-22 16:17:37 +02003154 size_t i;
3155 int ret = 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003156 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
3157
3158 assert(err);
3159
3160 if (transaction->state != REF_TRANSACTION_OPEN)
3161 die("BUG: commit called for transaction that is not open");
3162
3163 /* Fail if a refname appears more than once in the transaction: */
Michael Haggertyefe47282016-04-22 00:02:50 +02003164 for (i = 0; i < transaction->nr; i++)
3165 string_list_append(&affected_refnames,
3166 transaction->updates[i]->refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003167 string_list_sort(&affected_refnames);
3168 if (ref_update_reject_duplicates(&affected_refnames, err)) {
3169 ret = TRANSACTION_GENERIC_ERROR;
3170 goto cleanup;
3171 }
3172
3173 /*
3174 * It's really undefined to call this function in an active
3175 * repository or when there are existing references: we are
3176 * only locking and changing packed-refs, so (1) any
3177 * simultaneous processes might try to change a reference at
3178 * the same time we do, and (2) any existing loose versions of
3179 * the references that we are setting would have precedence
3180 * over our values. But some remote helpers create the remote
3181 * "HEAD" and "master" branches before calling this function,
3182 * so here we really only check that none of the references
3183 * that we are creating already exists.
3184 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07003185 if (refs_for_each_rawref(&refs->base, ref_present,
3186 &affected_refnames))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003187 die("BUG: initial ref transaction called with existing refs");
3188
Michael Haggertyefe47282016-04-22 00:02:50 +02003189 for (i = 0; i < transaction->nr; i++) {
3190 struct ref_update *update = transaction->updates[i];
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003191
3192 if ((update->flags & REF_HAVE_OLD) &&
brian m. carlson98491292017-05-06 22:10:23 +00003193 !is_null_oid(&update->old_oid))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003194 die("BUG: initial ref transaction with old_sha1 set");
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +07003195 if (refs_verify_refname_available(&refs->base, update->refname,
3196 &affected_refnames, NULL,
3197 err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003198 ret = TRANSACTION_NAME_CONFLICT;
3199 goto cleanup;
3200 }
3201 }
3202
Michael Haggerty49c0df62016-09-04 18:08:15 +02003203 if (lock_packed_refs(refs, 0)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003204 strbuf_addf(err, "unable to lock packed-refs file: %s",
3205 strerror(errno));
3206 ret = TRANSACTION_GENERIC_ERROR;
3207 goto cleanup;
3208 }
3209
Michael Haggertyefe47282016-04-22 00:02:50 +02003210 for (i = 0; i < transaction->nr; i++) {
3211 struct ref_update *update = transaction->updates[i];
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003212
3213 if ((update->flags & REF_HAVE_NEW) &&
brian m. carlson98491292017-05-06 22:10:23 +00003214 !is_null_oid(&update->new_oid))
3215 add_packed_ref(refs, update->refname,
brian m. carlson4417df82017-05-06 22:10:24 +00003216 &update->new_oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003217 }
3218
Michael Haggerty49c0df62016-09-04 18:08:15 +02003219 if (commit_packed_refs(refs)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003220 strbuf_addf(err, "unable to commit packed-refs file: %s",
3221 strerror(errno));
3222 ret = TRANSACTION_GENERIC_ERROR;
3223 goto cleanup;
3224 }
3225
3226cleanup:
3227 transaction->state = REF_TRANSACTION_CLOSED;
3228 string_list_clear(&affected_refnames, 0);
3229 return ret;
3230}
3231
3232struct expire_reflog_cb {
3233 unsigned int flags;
3234 reflog_expiry_should_prune_fn *should_prune_fn;
3235 void *policy_cb;
3236 FILE *newlog;
brian m. carlson9461d272017-02-21 23:47:32 +00003237 struct object_id last_kept_oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003238};
3239
brian m. carlson9461d272017-02-21 23:47:32 +00003240static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +02003241 const char *email, timestamp_t timestamp, int tz,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003242 const char *message, void *cb_data)
3243{
3244 struct expire_reflog_cb *cb = cb_data;
3245 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
3246
3247 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
brian m. carlson9461d272017-02-21 23:47:32 +00003248 ooid = &cb->last_kept_oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003249
brian m. carlson43224782017-05-06 22:10:00 +00003250 if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003251 message, policy_cb)) {
3252 if (!cb->newlog)
3253 printf("would prune %s", message);
3254 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3255 printf("prune %s", message);
3256 } else {
3257 if (cb->newlog) {
Johannes Schindelincb71f8b2017-04-21 12:45:48 +02003258 fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
brian m. carlson9461d272017-02-21 23:47:32 +00003259 oid_to_hex(ooid), oid_to_hex(noid),
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003260 email, timestamp, tz, message);
brian m. carlson9461d272017-02-21 23:47:32 +00003261 oidcpy(&cb->last_kept_oid, noid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003262 }
3263 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3264 printf("keep %s", message);
3265 }
3266 return 0;
3267}
3268
David Turnere3688bd2016-09-04 18:08:38 +02003269static int files_reflog_expire(struct ref_store *ref_store,
3270 const char *refname, const unsigned char *sha1,
3271 unsigned int flags,
3272 reflog_expiry_prepare_fn prepare_fn,
3273 reflog_expiry_should_prune_fn should_prune_fn,
3274 reflog_expiry_cleanup_fn cleanup_fn,
3275 void *policy_cb_data)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003276{
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02003277 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07003278 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003279 static struct lock_file reflog_lock;
3280 struct expire_reflog_cb cb;
3281 struct ref_lock *lock;
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003282 struct strbuf log_file_sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003283 char *log_file;
3284 int status = 0;
3285 int type;
3286 struct strbuf err = STRBUF_INIT;
brian m. carlson43224782017-05-06 22:10:00 +00003287 struct object_id oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003288
3289 memset(&cb, 0, sizeof(cb));
3290 cb.flags = flags;
3291 cb.policy_cb = policy_cb_data;
3292 cb.should_prune_fn = should_prune_fn;
3293
3294 /*
3295 * The reflog file is locked by holding the lock on the
3296 * reference itself, plus we might need to update the
3297 * reference if --updateref was specified:
3298 */
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02003299 lock = lock_ref_sha1_basic(refs, refname, sha1,
3300 NULL, NULL, REF_NODEREF,
David Turner41d796e2016-04-07 15:03:11 -04003301 &type, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003302 if (!lock) {
3303 error("cannot lock ref '%s': %s", refname, err.buf);
3304 strbuf_release(&err);
3305 return -1;
3306 }
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07003307 if (!refs_reflog_exists(ref_store, refname)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003308 unlock_ref(lock);
3309 return 0;
3310 }
3311
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003312 files_reflog_path(refs, &log_file_sb, refname);
3313 log_file = strbuf_detach(&log_file_sb, NULL);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003314 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3315 /*
3316 * Even though holding $GIT_DIR/logs/$reflog.lock has
3317 * no locking implications, we use the lock_file
3318 * machinery here anyway because it does a lot of the
3319 * work we need, including cleaning up if the program
3320 * exits unexpectedly.
3321 */
3322 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3323 struct strbuf err = STRBUF_INIT;
3324 unable_to_lock_message(log_file, errno, &err);
3325 error("%s", err.buf);
3326 strbuf_release(&err);
3327 goto failure;
3328 }
3329 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3330 if (!cb.newlog) {
3331 error("cannot fdopen %s (%s)",
3332 get_lock_file_path(&reflog_lock), strerror(errno));
3333 goto failure;
3334 }
3335 }
3336
brian m. carlson43224782017-05-06 22:10:00 +00003337 hashcpy(oid.hash, sha1);
3338
3339 (*prepare_fn)(refname, &oid, cb.policy_cb);
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07003340 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003341 (*cleanup_fn)(cb.policy_cb);
3342
3343 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3344 /*
3345 * It doesn't make sense to adjust a reference pointed
3346 * to by a symbolic ref based on expiring entries in
3347 * the symbolic reference's reflog. Nor can we update
3348 * a reference if there are no remaining reflog
3349 * entries.
3350 */
3351 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3352 !(type & REF_ISSYMREF) &&
brian m. carlson9461d272017-02-21 23:47:32 +00003353 !is_null_oid(&cb.last_kept_oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003354
3355 if (close_lock_file(&reflog_lock)) {
3356 status |= error("couldn't write %s: %s", log_file,
3357 strerror(errno));
3358 } else if (update &&
3359 (write_in_full(get_lock_file_fd(lock->lk),
brian m. carlson9461d272017-02-21 23:47:32 +00003360 oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003361 write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
3362 close_ref(lock) < 0)) {
3363 status |= error("couldn't write %s",
3364 get_lock_file_path(lock->lk));
3365 rollback_lock_file(&reflog_lock);
3366 } else if (commit_lock_file(&reflog_lock)) {
Junio C Hamanoe0048d32015-12-11 10:40:54 -08003367 status |= error("unable to write reflog '%s' (%s)",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003368 log_file, strerror(errno));
3369 } else if (update && commit_ref(lock)) {
3370 status |= error("couldn't set %s", lock->ref_name);
3371 }
3372 }
3373 free(log_file);
3374 unlock_ref(lock);
3375 return status;
3376
3377 failure:
3378 rollback_lock_file(&reflog_lock);
3379 free(log_file);
3380 unlock_ref(lock);
3381 return -1;
3382}
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +02003383
David Turner6fb5acf2016-09-04 18:08:41 +02003384static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
3385{
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003386 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07003387 files_downcast(ref_store, REF_STORE_WRITE, "init_db");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003388 struct strbuf sb = STRBUF_INIT;
3389
David Turner6fb5acf2016-09-04 18:08:41 +02003390 /*
3391 * Create .git/refs/{heads,tags}
3392 */
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003393 files_ref_path(refs, &sb, "refs/heads");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003394 safe_create_dir(sb.buf, 1);
3395
3396 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003397 files_ref_path(refs, &sb, "refs/tags");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003398 safe_create_dir(sb.buf, 1);
3399
3400 strbuf_release(&sb);
David Turner6fb5acf2016-09-04 18:08:41 +02003401 return 0;
3402}
3403
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +02003404struct ref_storage_be refs_be_files = {
3405 NULL,
Michael Haggerty00eebe32016-09-04 18:08:11 +02003406 "files",
Ronnie Sahlberg127b42a2016-09-04 18:08:16 +02003407 files_ref_store_create,
David Turner6fb5acf2016-09-04 18:08:41 +02003408 files_init_db,
Michael Haggerty30173b82017-05-22 16:17:44 +02003409 files_transaction_prepare,
3410 files_transaction_finish,
3411 files_transaction_abort,
David Turnerfc681462016-09-04 18:08:39 +02003412 files_initial_transaction_commit,
Michael Haggertye1e33b72016-09-04 18:08:25 +02003413
Michael Haggerty82315272016-09-04 18:08:27 +02003414 files_pack_refs,
Michael Haggertybd427cf2016-09-04 18:08:29 +02003415 files_peel_ref,
Michael Haggerty284689b2016-09-04 18:08:28 +02003416 files_create_symref,
David Turnera27dcf82016-09-04 18:08:40 +02003417 files_delete_refs,
David Turner9b6b40d2016-09-04 18:08:42 +02003418 files_rename_ref,
Michael Haggerty82315272016-09-04 18:08:27 +02003419
Michael Haggerty1a769002016-09-04 18:08:37 +02003420 files_ref_iterator_begin,
Michael Haggerty62665822016-09-04 18:08:26 +02003421 files_read_raw_ref,
David Turnere3688bd2016-09-04 18:08:38 +02003422
3423 files_reflog_iterator_begin,
3424 files_for_each_reflog_ent,
3425 files_for_each_reflog_ent_reverse,
3426 files_reflog_exists,
3427 files_create_reflog,
3428 files_delete_reflog,
3429 files_reflog_expire
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +02003430};