blob: 079ba941ef7f1392d7895e6713daf90f9a2466c4 [file] [log] [blame]
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001#include "../cache.h"
2#include "../refs.h"
3#include "refs-internal.h"
Michael Haggerty958f9642017-04-16 08:41:31 +02004#include "ref-cache.h"
Michael Haggerty3bc581b2016-06-18 06:15:15 +02005#include "../iterator.h"
Michael Haggerty2880d162016-06-18 06:15:19 +02006#include "../dir-iterator.h"
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01007#include "../lockfile.h"
8#include "../object.h"
9#include "../dir.h"
10
11struct ref_lock {
12 char *ref_name;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010013 struct lock_file *lk;
14 struct object_id old_oid;
15};
16
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010017/*
Michael Haggertya8739242016-06-18 06:15:14 +020018 * Return true if refname, which has the specified oid and flags, can
19 * be resolved to an object in the database. If the referred-to object
20 * does not exist, emit a warning and return false.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010021 */
Michael Haggertya8739242016-06-18 06:15:14 +020022static int ref_resolves_to_object(const char *refname,
23 const struct object_id *oid,
24 unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010025{
Michael Haggertya8739242016-06-18 06:15:14 +020026 if (flags & REF_ISBROKEN)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010027 return 0;
Michael Haggertya8739242016-06-18 06:15:14 +020028 if (!has_sha1_file(oid->hash)) {
29 error("%s does not point to a valid object!", refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010030 return 0;
31 }
32 return 1;
33}
34
35/*
Michael Haggertya8739242016-06-18 06:15:14 +020036 * Return true if the reference described by entry can be resolved to
37 * an object in the database; otherwise, emit a warning and return
38 * false.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010039 */
Michael Haggertya8739242016-06-18 06:15:14 +020040static int entry_resolves_to_object(struct ref_entry *entry)
41{
42 return ref_resolves_to_object(entry->name,
43 &entry->u.value.oid, entry->flag);
44}
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010045
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010046struct packed_ref_cache {
Michael Haggerty7c22bc82017-04-16 08:41:32 +020047 struct ref_cache *cache;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010048
49 /*
50 * Count of references to the data structure in this instance,
Michael Haggerty65a0a8e2016-09-04 18:08:09 +020051 * including the pointer from files_ref_store::packed if any.
52 * The data will not be freed as long as the reference count
53 * is nonzero.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010054 */
55 unsigned int referrers;
56
57 /*
58 * Iff the packed-refs file associated with this instance is
59 * currently locked for writing, this points at the associated
60 * lock (which is owned by somebody else). The referrer count
61 * is also incremented when the file is locked and decremented
62 * when it is unlocked.
63 */
64 struct lock_file *lock;
65
66 /* The metadata from when this packed-refs cache was read */
67 struct stat_validity validity;
68};
69
70/*
71 * Future: need to be in "struct repository"
72 * when doing a full libification.
73 */
Michael Haggerty00eebe32016-09-04 18:08:11 +020074struct files_ref_store {
75 struct ref_store base;
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +070076 unsigned int store_flags;
Michael Haggerty32c597e2017-02-10 12:16:16 +010077
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +070078 char *gitdir;
79 char *gitcommondir;
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +070080 char *packed_refs_path;
81
Michael Haggerty7c22bc82017-04-16 08:41:32 +020082 struct ref_cache *loose;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010083 struct packed_ref_cache *packed;
Michael Haggerty00eebe32016-09-04 18:08:11 +020084};
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +010085
86/* Lock used for the main packed-refs file: */
87static struct lock_file packlock;
88
89/*
90 * Increment the reference count of *packed_refs.
91 */
92static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
93{
94 packed_refs->referrers++;
95}
96
97/*
98 * Decrease the reference count of *packed_refs. If it goes to zero,
99 * free *packed_refs and return true; otherwise return false.
100 */
101static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
102{
103 if (!--packed_refs->referrers) {
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200104 free_ref_cache(packed_refs->cache);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100105 stat_validity_clear(&packed_refs->validity);
106 free(packed_refs);
107 return 1;
108 } else {
109 return 0;
110 }
111}
112
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200113static void clear_packed_ref_cache(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100114{
115 if (refs->packed) {
116 struct packed_ref_cache *packed_refs = refs->packed;
117
118 if (packed_refs->lock)
119 die("internal error: packed-ref cache cleared while locked");
120 refs->packed = NULL;
121 release_packed_ref_cache(packed_refs);
122 }
123}
124
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200125static void clear_loose_ref_cache(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100126{
127 if (refs->loose) {
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200128 free_ref_cache(refs->loose);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100129 refs->loose = NULL;
130 }
131}
132
Jeff Kinga2d51562016-01-22 17:29:30 -0500133/*
134 * Create a new submodule ref cache and add it to the internal
135 * set of caches.
136 */
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700137static struct ref_store *files_ref_store_create(const char *gitdir,
138 unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100139{
Michael Haggerty00eebe32016-09-04 18:08:11 +0200140 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
141 struct ref_store *ref_store = (struct ref_store *)refs;
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700142 struct strbuf sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100143
Michael Haggertyfbfd0a22017-02-10 12:16:17 +0100144 base_ref_store_init(ref_store, &refs_be_files);
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700145 refs->store_flags = flags;
Jeff Kinga2d51562016-01-22 17:29:30 -0500146
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700147 refs->gitdir = xstrdup(gitdir);
148 get_common_dir_noenv(&sb, gitdir);
149 refs->gitcommondir = strbuf_detach(&sb, NULL);
150 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
151 refs->packed_refs_path = strbuf_detach(&sb, NULL);
Jeff Kinga2d51562016-01-22 17:29:30 -0500152
Michael Haggerty00eebe32016-09-04 18:08:11 +0200153 return ref_store;
Jeff Kinga2d51562016-01-22 17:29:30 -0500154}
155
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100156/*
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700157 * Die if refs is not the main ref store. caller is used in any
158 * necessary error messages.
Michael Haggerty32c597e2017-02-10 12:16:16 +0100159 */
160static void files_assert_main_repository(struct files_ref_store *refs,
161 const char *caller)
162{
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700163 if (refs->store_flags & REF_STORE_MAIN)
164 return;
165
166 die("BUG: operation %s only allowed for main ref store", caller);
Michael Haggerty32c597e2017-02-10 12:16:16 +0100167}
168
169/*
Michael Haggerty00eebe32016-09-04 18:08:11 +0200170 * 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 +0700171 * files_ref_store. required_flags is compared with ref_store's
172 * store_flags to ensure the ref_store has all required capabilities.
173 * "caller" is used in any necessary error messages.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100174 */
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700175static struct files_ref_store *files_downcast(struct ref_store *ref_store,
176 unsigned int required_flags,
177 const char *caller)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100178{
Michael Haggerty32c597e2017-02-10 12:16:16 +0100179 struct files_ref_store *refs;
180
Michael Haggerty00eebe32016-09-04 18:08:11 +0200181 if (ref_store->be != &refs_be_files)
182 die("BUG: ref_store is type \"%s\" not \"files\" in %s",
183 ref_store->be->name, caller);
Michael Haggerty2eed2782016-06-18 06:15:12 +0200184
Michael Haggerty32c597e2017-02-10 12:16:16 +0100185 refs = (struct files_ref_store *)ref_store;
Michael Haggerty2eed2782016-06-18 06:15:12 +0200186
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700187 if ((refs->store_flags & required_flags) != required_flags)
188 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
189 caller, required_flags, refs->store_flags);
Michael Haggerty32c597e2017-02-10 12:16:16 +0100190
191 return refs;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100192}
193
194/* The length of a peeled reference line in packed-refs, including EOL: */
195#define PEELED_LINE_LENGTH 42
196
197/*
198 * The packed-refs header line that we write out. Perhaps other
199 * traits will be added later. The trailing space is required.
200 */
201static const char PACKED_REFS_HEADER[] =
202 "# pack-refs with: peeled fully-peeled \n";
203
204/*
205 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
206 * Return a pointer to the refname within the line (null-terminated),
207 * or NULL if there was a problem.
208 */
209static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
210{
211 const char *ref;
212
213 /*
214 * 42: the answer to everything.
215 *
216 * In this case, it happens to be the answer to
217 * 40 (length of sha1 hex representation)
218 * +1 (space in between hex and name)
219 * +1 (newline at the end of the line)
220 */
221 if (line->len <= 42)
222 return NULL;
223
224 if (get_sha1_hex(line->buf, sha1) < 0)
225 return NULL;
226 if (!isspace(line->buf[40]))
227 return NULL;
228
229 ref = line->buf + 41;
230 if (isspace(*ref))
231 return NULL;
232
233 if (line->buf[line->len - 1] != '\n')
234 return NULL;
235 line->buf[--line->len] = 0;
236
237 return ref;
238}
239
240/*
241 * Read f, which is a packed-refs file, into dir.
242 *
243 * A comment line of the form "# pack-refs with: " may contain zero or
244 * more traits. We interpret the traits as follows:
245 *
246 * No traits:
247 *
248 * Probably no references are peeled. But if the file contains a
249 * peeled value for a reference, we will use it.
250 *
251 * peeled:
252 *
253 * References under "refs/tags/", if they *can* be peeled, *are*
254 * peeled in this file. References outside of "refs/tags/" are
255 * probably not peeled even if they could have been, but if we find
256 * a peeled value for such a reference we will use it.
257 *
258 * fully-peeled:
259 *
260 * All references in the file that can be peeled are peeled.
261 * Inversely (and this is more important), any references in the
262 * file for which no peeled value is recorded is not peelable. This
263 * trait should typically be written alongside "peeled" for
264 * compatibility with older clients, but we do not require it
265 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
266 */
267static void read_packed_refs(FILE *f, struct ref_dir *dir)
268{
269 struct ref_entry *last = NULL;
270 struct strbuf line = STRBUF_INIT;
271 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
272
273 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
274 unsigned char sha1[20];
275 const char *refname;
276 const char *traits;
277
278 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
279 if (strstr(traits, " fully-peeled "))
280 peeled = PEELED_FULLY;
281 else if (strstr(traits, " peeled "))
282 peeled = PEELED_TAGS;
283 /* perhaps other traits later as well */
284 continue;
285 }
286
287 refname = parse_ref_line(&line, sha1);
288 if (refname) {
289 int flag = REF_ISPACKED;
290
291 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
292 if (!refname_is_safe(refname))
293 die("packed refname is dangerous: %s", refname);
294 hashclr(sha1);
295 flag |= REF_BAD_NAME | REF_ISBROKEN;
296 }
297 last = create_ref_entry(refname, sha1, flag, 0);
298 if (peeled == PEELED_FULLY ||
299 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
300 last->flag |= REF_KNOWS_PEELED;
Michael Haggertya3ade2b2017-04-16 08:41:28 +0200301 add_ref_entry(dir, last);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100302 continue;
303 }
304 if (last &&
305 line.buf[0] == '^' &&
306 line.len == PEELED_LINE_LENGTH &&
307 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
308 !get_sha1_hex(line.buf + 1, sha1)) {
309 hashcpy(last->u.value.peeled.hash, sha1);
310 /*
311 * Regardless of what the file header said,
312 * we definitely know the value of *this*
313 * reference:
314 */
315 last->flag |= REF_KNOWS_PEELED;
316 }
317 }
318
319 strbuf_release(&line);
320}
321
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +0700322static const char *files_packed_refs_path(struct files_ref_store *refs)
323{
324 return refs->packed_refs_path;
325}
326
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +0700327static void files_reflog_path(struct files_ref_store *refs,
328 struct strbuf *sb,
329 const char *refname)
330{
331 if (!refname) {
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700332 /*
333 * FIXME: of course this is wrong in multi worktree
334 * setting. To be fixed real soon.
335 */
336 strbuf_addf(sb, "%s/logs", refs->gitcommondir);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +0700337 return;
338 }
339
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700340 switch (ref_type(refname)) {
341 case REF_TYPE_PER_WORKTREE:
342 case REF_TYPE_PSEUDOREF:
343 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname);
344 break;
345 case REF_TYPE_NORMAL:
346 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
347 break;
348 default:
349 die("BUG: unknown ref type %d of ref %s",
350 ref_type(refname), refname);
351 }
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +0700352}
353
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700354static void files_ref_path(struct files_ref_store *refs,
355 struct strbuf *sb,
356 const char *refname)
357{
Nguyễn Thái Ngọc Duyf57f37e2017-03-26 09:42:24 +0700358 switch (ref_type(refname)) {
359 case REF_TYPE_PER_WORKTREE:
360 case REF_TYPE_PSEUDOREF:
361 strbuf_addf(sb, "%s/%s", refs->gitdir, refname);
362 break;
363 case REF_TYPE_NORMAL:
364 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
365 break;
366 default:
367 die("BUG: unknown ref type %d of ref %s",
368 ref_type(refname), refname);
369 }
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700370}
371
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100372/*
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200373 * Get the packed_ref_cache for the specified files_ref_store,
374 * creating it if necessary.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100375 */
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200376static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100377{
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +0700378 const char *packed_refs_file = files_packed_refs_path(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100379
380 if (refs->packed &&
381 !stat_validity_check(&refs->packed->validity, packed_refs_file))
382 clear_packed_ref_cache(refs);
383
384 if (!refs->packed) {
385 FILE *f;
386
387 refs->packed = xcalloc(1, sizeof(*refs->packed));
388 acquire_packed_ref_cache(refs->packed);
Michael Haggertydf308752017-04-16 08:41:34 +0200389 refs->packed->cache = create_ref_cache(&refs->base, NULL);
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200390 refs->packed->cache->root->flag &= ~REF_INCOMPLETE;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100391 f = fopen(packed_refs_file, "r");
392 if (f) {
393 stat_validity_update(&refs->packed->validity, fileno(f));
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200394 read_packed_refs(f, get_ref_dir(refs->packed->cache->root));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100395 fclose(f);
396 }
397 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100398 return refs->packed;
399}
400
401static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
402{
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200403 return get_ref_dir(packed_ref_cache->cache->root);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100404}
405
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200406static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100407{
408 return get_packed_ref_dir(get_packed_ref_cache(refs));
409}
410
411/*
412 * Add a reference to the in-memory packed reference cache. This may
413 * only be called while the packed-refs file is locked (see
414 * lock_packed_refs()). To actually write the packed-refs file, call
415 * commit_packed_refs().
416 */
Michael Haggertyd99825a2016-09-04 18:08:12 +0200417static void add_packed_ref(struct files_ref_store *refs,
418 const char *refname, const unsigned char *sha1)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100419{
Michael Haggerty00eebe32016-09-04 18:08:11 +0200420 struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100421
422 if (!packed_ref_cache->lock)
423 die("internal error: packed refs not locked");
Michael Haggertya3ade2b2017-04-16 08:41:28 +0200424 add_ref_entry(get_packed_ref_dir(packed_ref_cache),
Michael Haggerty958f9642017-04-16 08:41:31 +0200425 create_ref_entry(refname, sha1, REF_ISPACKED, 1));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100426}
427
428/*
429 * Read the loose references from the namespace dirname into dir
430 * (without recursing). dirname must end with '/'. dir must be the
431 * directory entry corresponding to dirname.
432 */
Michael Haggertydf308752017-04-16 08:41:34 +0200433static void loose_fill_ref_dir(struct ref_store *ref_store,
434 struct ref_dir *dir, const char *dirname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100435{
Michael Haggertydf308752017-04-16 08:41:34 +0200436 struct files_ref_store *refs =
437 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100438 DIR *d;
439 struct dirent *de;
440 int dirnamelen = strlen(dirname);
441 struct strbuf refname;
442 struct strbuf path = STRBUF_INIT;
443 size_t path_baselen;
444
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700445 files_ref_path(refs, &path, dirname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100446 path_baselen = path.len;
447
448 d = opendir(path.buf);
449 if (!d) {
450 strbuf_release(&path);
451 return;
452 }
453
454 strbuf_init(&refname, dirnamelen + 257);
455 strbuf_add(&refname, dirname, dirnamelen);
456
457 while ((de = readdir(d)) != NULL) {
458 unsigned char sha1[20];
459 struct stat st;
460 int flag;
461
462 if (de->d_name[0] == '.')
463 continue;
464 if (ends_with(de->d_name, ".lock"))
465 continue;
466 strbuf_addstr(&refname, de->d_name);
467 strbuf_addstr(&path, de->d_name);
468 if (stat(path.buf, &st) < 0) {
469 ; /* silently ignore */
470 } else if (S_ISDIR(st.st_mode)) {
471 strbuf_addch(&refname, '/');
472 add_entry_to_dir(dir,
Michael Haggertye00d1a42017-04-16 08:41:33 +0200473 create_dir_entry(dir->cache, refname.buf,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100474 refname.len, 1));
475 } else {
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700476 if (!refs_resolve_ref_unsafe(&refs->base,
Michael Haggerty3c0cb0c2017-02-09 21:53:52 +0100477 refname.buf,
478 RESOLVE_REF_READING,
479 sha1, &flag)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100480 hashclr(sha1);
481 flag |= REF_ISBROKEN;
482 } else if (is_null_sha1(sha1)) {
483 /*
484 * It is so astronomically unlikely
485 * that NULL_SHA1 is the SHA-1 of an
486 * actual object that we consider its
487 * appearance in a loose reference
488 * file to be repo corruption
489 * (probably due to a software bug).
490 */
491 flag |= REF_ISBROKEN;
492 }
493
494 if (check_refname_format(refname.buf,
495 REFNAME_ALLOW_ONELEVEL)) {
496 if (!refname_is_safe(refname.buf))
497 die("loose refname is dangerous: %s", refname.buf);
498 hashclr(sha1);
499 flag |= REF_BAD_NAME | REF_ISBROKEN;
500 }
501 add_entry_to_dir(dir,
502 create_ref_entry(refname.buf, sha1, flag, 0));
503 }
504 strbuf_setlen(&refname, dirnamelen);
505 strbuf_setlen(&path, path_baselen);
506 }
507 strbuf_release(&refname);
508 strbuf_release(&path);
509 closedir(d);
Michael Haggertye3bf2982017-04-16 08:41:35 +0200510
511 /*
512 * Manually add refs/bisect, which, being per-worktree, might
513 * not appear in the directory listing for refs/ in the main
514 * repo.
515 */
516 if (!strcmp(dirname, "refs/")) {
517 int pos = search_ref_dir(dir, "refs/bisect/", 12);
518
519 if (pos < 0) {
520 struct ref_entry *child_entry = create_dir_entry(
521 dir->cache, "refs/bisect/", 12, 1);
522 add_entry_to_dir(dir, child_entry);
523 }
524 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100525}
526
Michael Haggerty65a0a8e2016-09-04 18:08:09 +0200527static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100528{
529 if (!refs->loose) {
530 /*
531 * Mark the top-level directory complete because we
532 * are about to read the only subdirectory that can
533 * hold references:
534 */
Michael Haggertydf308752017-04-16 08:41:34 +0200535 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200536
537 /* We're going to fill the top level ourselves: */
538 refs->loose->root->flag &= ~REF_INCOMPLETE;
539
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100540 /*
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200541 * Add an incomplete entry for "refs/" (to be filled
542 * lazily):
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100543 */
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200544 add_entry_to_dir(get_ref_dir(refs->loose->root),
Michael Haggertye00d1a42017-04-16 08:41:33 +0200545 create_dir_entry(refs->loose, "refs/", 5, 1));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100546 }
Michael Haggerty7c22bc82017-04-16 08:41:32 +0200547 return get_ref_dir(refs->loose->root);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100548}
549
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100550/*
551 * Return the ref_entry for the given refname from the packed
552 * references. If it does not exist, return NULL.
553 */
Michael Haggertyf0d21ef2016-09-04 18:08:13 +0200554static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
555 const char *refname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100556{
Michael Haggertybc1c6962017-04-16 08:41:29 +0200557 return find_ref_entry(get_packed_refs(refs), refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100558}
559
560/*
Michael Haggerty419c6f42016-04-07 15:02:55 -0400561 * A loose ref file doesn't exist; check for a packed ref.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100562 */
Michael Haggerty611118d2016-09-04 18:08:18 +0200563static int resolve_packed_ref(struct files_ref_store *refs,
564 const char *refname,
565 unsigned char *sha1, unsigned int *flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100566{
567 struct ref_entry *entry;
568
569 /*
570 * The loose reference file does not exist; check for a packed
571 * reference.
572 */
Michael Haggertyf0d21ef2016-09-04 18:08:13 +0200573 entry = get_packed_ref(refs, refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100574 if (entry) {
575 hashcpy(sha1, entry->u.value.oid.hash);
Michael Haggertya70a93b2016-04-07 15:02:57 -0400576 *flags |= REF_ISPACKED;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100577 return 0;
578 }
Michael Haggerty419c6f42016-04-07 15:02:55 -0400579 /* refname is not a packed reference. */
580 return -1;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100581}
582
Michael Haggertye1e33b72016-09-04 18:08:25 +0200583static int files_read_raw_ref(struct ref_store *ref_store,
584 const char *refname, unsigned char *sha1,
585 struct strbuf *referent, unsigned int *type)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100586{
Michael Haggerty43086512016-09-04 18:08:14 +0200587 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700588 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400589 struct strbuf sb_contents = STRBUF_INIT;
590 struct strbuf sb_path = STRBUF_INIT;
David Turner70486532016-04-07 15:03:01 -0400591 const char *path;
592 const char *buf;
593 struct stat st;
594 int fd;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400595 int ret = -1;
596 int save_errno;
Jeff Kinge8c42cb2016-10-06 12:48:42 -0400597 int remaining_retries = 3;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100598
Michael Haggertyfa96ea12016-04-22 01:11:17 +0200599 *type = 0;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400600 strbuf_reset(&sb_path);
Michael Haggerty34c7ad82016-09-04 18:08:20 +0200601
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700602 files_ref_path(refs, &sb_path, refname);
Michael Haggerty34c7ad82016-09-04 18:08:20 +0200603
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400604 path = sb_path.buf;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100605
David Turner70486532016-04-07 15:03:01 -0400606stat_ref:
607 /*
608 * We might have to loop back here to avoid a race
609 * condition: first we lstat() the file, then we try
610 * to read it as a link or as a file. But if somebody
611 * changes the type of the file (file <-> directory
612 * <-> symlink) between the lstat() and reading, then
613 * we don't want to report that as an error but rather
614 * try again starting with the lstat().
Jeff Kinge8c42cb2016-10-06 12:48:42 -0400615 *
616 * We'll keep a count of the retries, though, just to avoid
617 * any confusing situation sending us into an infinite loop.
David Turner70486532016-04-07 15:03:01 -0400618 */
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100619
Jeff Kinge8c42cb2016-10-06 12:48:42 -0400620 if (remaining_retries-- <= 0)
621 goto out;
622
David Turner70486532016-04-07 15:03:01 -0400623 if (lstat(path, &st) < 0) {
624 if (errno != ENOENT)
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400625 goto out;
Michael Haggerty611118d2016-09-04 18:08:18 +0200626 if (resolve_packed_ref(refs, refname, sha1, type)) {
David Turner70486532016-04-07 15:03:01 -0400627 errno = ENOENT;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400628 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100629 }
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400630 ret = 0;
631 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100632 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100633
David Turner70486532016-04-07 15:03:01 -0400634 /* Follow "normalized" - ie "refs/.." symlinks by hand */
635 if (S_ISLNK(st.st_mode)) {
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400636 strbuf_reset(&sb_contents);
637 if (strbuf_readlink(&sb_contents, path, 0) < 0) {
David Turner70486532016-04-07 15:03:01 -0400638 if (errno == ENOENT || errno == EINVAL)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100639 /* inconsistent with lstat; retry */
640 goto stat_ref;
641 else
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400642 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100643 }
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400644 if (starts_with(sb_contents.buf, "refs/") &&
645 !check_refname_format(sb_contents.buf, 0)) {
Michael Haggerty92b38092016-04-22 01:11:17 +0200646 strbuf_swap(&sb_contents, referent);
Michael Haggerty3a0b6b92016-04-26 03:06:23 +0200647 *type |= REF_ISSYMREF;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400648 ret = 0;
649 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100650 }
Jeff King3f7bd762016-10-06 15:41:08 -0400651 /*
652 * It doesn't look like a refname; fall through to just
653 * treating it like a non-symlink, and reading whatever it
654 * points to.
655 */
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100656 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100657
David Turner70486532016-04-07 15:03:01 -0400658 /* Is it a directory? */
659 if (S_ISDIR(st.st_mode)) {
Michael Haggertye167a562016-05-05 14:09:41 +0200660 /*
661 * Even though there is a directory where the loose
662 * ref is supposed to be, there could still be a
663 * packed ref:
664 */
Michael Haggerty611118d2016-09-04 18:08:18 +0200665 if (resolve_packed_ref(refs, refname, sha1, type)) {
Michael Haggertye167a562016-05-05 14:09:41 +0200666 errno = EISDIR;
667 goto out;
668 }
669 ret = 0;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400670 goto out;
David Turner70486532016-04-07 15:03:01 -0400671 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100672
David Turner70486532016-04-07 15:03:01 -0400673 /*
674 * Anything else, just open it and try to use it as
675 * a ref
676 */
677 fd = open(path, O_RDONLY);
678 if (fd < 0) {
Jeff King3f7bd762016-10-06 15:41:08 -0400679 if (errno == ENOENT && !S_ISLNK(st.st_mode))
David Turner70486532016-04-07 15:03:01 -0400680 /* inconsistent with lstat; retry */
681 goto stat_ref;
682 else
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400683 goto out;
David Turner70486532016-04-07 15:03:01 -0400684 }
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400685 strbuf_reset(&sb_contents);
686 if (strbuf_read(&sb_contents, fd, 256) < 0) {
David Turner70486532016-04-07 15:03:01 -0400687 int save_errno = errno;
688 close(fd);
689 errno = save_errno;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400690 goto out;
David Turner70486532016-04-07 15:03:01 -0400691 }
692 close(fd);
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400693 strbuf_rtrim(&sb_contents);
694 buf = sb_contents.buf;
David Turner70486532016-04-07 15:03:01 -0400695 if (starts_with(buf, "ref:")) {
696 buf += 4;
697 while (isspace(*buf))
698 buf++;
699
Michael Haggerty92b38092016-04-22 01:11:17 +0200700 strbuf_reset(referent);
701 strbuf_addstr(referent, buf);
Michael Haggerty3a0b6b92016-04-26 03:06:23 +0200702 *type |= REF_ISSYMREF;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400703 ret = 0;
704 goto out;
David Turner70486532016-04-07 15:03:01 -0400705 }
706
707 /*
708 * Please note that FETCH_HEAD has additional
709 * data after the sha.
710 */
711 if (get_sha1_hex(buf, sha1) ||
712 (buf[40] != '\0' && !isspace(buf[40]))) {
Michael Haggerty3a0b6b92016-04-26 03:06:23 +0200713 *type |= REF_ISBROKEN;
David Turner70486532016-04-07 15:03:01 -0400714 errno = EINVAL;
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400715 goto out;
David Turner70486532016-04-07 15:03:01 -0400716 }
717
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400718 ret = 0;
719
720out:
721 save_errno = errno;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100722 strbuf_release(&sb_path);
723 strbuf_release(&sb_contents);
Michael Haggerty42a38cf2016-04-07 15:03:02 -0400724 errno = save_errno;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100725 return ret;
726}
727
Michael Haggerty8415d242016-04-24 08:11:37 +0200728static void unlock_ref(struct ref_lock *lock)
729{
730 /* Do not free lock->lk -- atexit() still looks at them */
731 if (lock->lk)
732 rollback_lock_file(lock->lk);
733 free(lock->ref_name);
Michael Haggerty8415d242016-04-24 08:11:37 +0200734 free(lock);
735}
736
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100737/*
Michael Haggerty92b15512016-04-25 15:56:07 +0200738 * Lock refname, without following symrefs, and set *lock_p to point
739 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
740 * and type similarly to read_raw_ref().
741 *
742 * The caller must verify that refname is a "safe" reference name (in
743 * the sense of refname_is_safe()) before calling this function.
744 *
745 * If the reference doesn't already exist, verify that refname doesn't
746 * have a D/F conflict with any existing references. extras and skip
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200747 * are passed to refs_verify_refname_available() for this check.
Michael Haggerty92b15512016-04-25 15:56:07 +0200748 *
749 * If mustexist is not set and the reference is not found or is
750 * broken, lock the reference anyway but clear sha1.
751 *
752 * Return 0 on success. On failure, write an error message to err and
753 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
754 *
755 * Implementation note: This function is basically
756 *
757 * lock reference
758 * read_raw_ref()
759 *
760 * but it includes a lot more code to
761 * - Deal with possible races with other processes
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200762 * - Avoid calling refs_verify_refname_available() when it can be
Michael Haggerty92b15512016-04-25 15:56:07 +0200763 * avoided, namely if we were successfully able to read the ref
764 * - Generate informative error messages in the case of failure
765 */
Michael Haggertyf7b0a982016-09-04 18:08:31 +0200766static int lock_raw_ref(struct files_ref_store *refs,
767 const char *refname, int mustexist,
Michael Haggerty92b15512016-04-25 15:56:07 +0200768 const struct string_list *extras,
769 const struct string_list *skip,
770 struct ref_lock **lock_p,
771 struct strbuf *referent,
772 unsigned int *type,
773 struct strbuf *err)
774{
775 struct ref_lock *lock;
776 struct strbuf ref_file = STRBUF_INIT;
777 int attempts_remaining = 3;
778 int ret = TRANSACTION_GENERIC_ERROR;
779
780 assert(err);
Michael Haggerty32c597e2017-02-10 12:16:16 +0100781 files_assert_main_repository(refs, "lock_raw_ref");
Michael Haggertyf7b0a982016-09-04 18:08:31 +0200782
Michael Haggerty92b15512016-04-25 15:56:07 +0200783 *type = 0;
784
785 /* First lock the file so it can't change out from under us. */
786
787 *lock_p = lock = xcalloc(1, sizeof(*lock));
788
789 lock->ref_name = xstrdup(refname);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +0700790 files_ref_path(refs, &ref_file, refname);
Michael Haggerty92b15512016-04-25 15:56:07 +0200791
792retry:
793 switch (safe_create_leading_directories(ref_file.buf)) {
794 case SCLD_OK:
795 break; /* success */
796 case SCLD_EXISTS:
797 /*
798 * Suppose refname is "refs/foo/bar". We just failed
799 * to create the containing directory, "refs/foo",
800 * because there was a non-directory in the way. This
801 * indicates a D/F conflict, probably because of
802 * another reference such as "refs/foo". There is no
803 * reason to expect this error to be transitory.
804 */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700805 if (refs_verify_refname_available(&refs->base, refname,
806 extras, skip, err)) {
Michael Haggerty92b15512016-04-25 15:56:07 +0200807 if (mustexist) {
808 /*
809 * To the user the relevant error is
810 * that the "mustexist" reference is
811 * missing:
812 */
813 strbuf_reset(err);
814 strbuf_addf(err, "unable to resolve reference '%s'",
815 refname);
816 } else {
817 /*
818 * The error message set by
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200819 * refs_verify_refname_available() is
820 * OK.
Michael Haggerty92b15512016-04-25 15:56:07 +0200821 */
822 ret = TRANSACTION_NAME_CONFLICT;
823 }
824 } else {
825 /*
826 * The file that is in the way isn't a loose
827 * reference. Report it as a low-level
828 * failure.
829 */
830 strbuf_addf(err, "unable to create lock file %s.lock; "
831 "non-directory in the way",
832 ref_file.buf);
833 }
834 goto error_return;
835 case SCLD_VANISHED:
836 /* Maybe another process was tidying up. Try again. */
837 if (--attempts_remaining > 0)
838 goto retry;
839 /* fall through */
840 default:
841 strbuf_addf(err, "unable to create directory for %s",
842 ref_file.buf);
843 goto error_return;
844 }
845
846 if (!lock->lk)
847 lock->lk = xcalloc(1, sizeof(struct lock_file));
848
849 if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {
850 if (errno == ENOENT && --attempts_remaining > 0) {
851 /*
852 * Maybe somebody just deleted one of the
853 * directories leading to ref_file. Try
854 * again:
855 */
856 goto retry;
857 } else {
858 unable_to_lock_message(ref_file.buf, errno, err);
859 goto error_return;
860 }
861 }
862
863 /*
864 * Now we hold the lock and can read the reference without
865 * fear that its value will change.
866 */
867
Michael Haggertyf7b0a982016-09-04 18:08:31 +0200868 if (files_read_raw_ref(&refs->base, refname,
Michael Haggertye1e33b72016-09-04 18:08:25 +0200869 lock->old_oid.hash, referent, type)) {
Michael Haggerty92b15512016-04-25 15:56:07 +0200870 if (errno == ENOENT) {
871 if (mustexist) {
872 /* Garden variety missing reference. */
873 strbuf_addf(err, "unable to resolve reference '%s'",
874 refname);
875 goto error_return;
876 } else {
877 /*
878 * Reference is missing, but that's OK. We
879 * know that there is not a conflict with
880 * another loose reference because
881 * (supposing that we are trying to lock
882 * reference "refs/foo/bar"):
883 *
884 * - We were successfully able to create
885 * the lockfile refs/foo/bar.lock, so we
886 * know there cannot be a loose reference
887 * named "refs/foo".
888 *
889 * - We got ENOENT and not EISDIR, so we
890 * know that there cannot be a loose
891 * reference named "refs/foo/bar/baz".
892 */
893 }
894 } else if (errno == EISDIR) {
895 /*
896 * There is a directory in the way. It might have
897 * contained references that have been deleted. If
898 * we don't require that the reference already
899 * exists, try to remove the directory so that it
900 * doesn't cause trouble when we want to rename the
901 * lockfile into place later.
902 */
903 if (mustexist) {
904 /* Garden variety missing reference. */
905 strbuf_addf(err, "unable to resolve reference '%s'",
906 refname);
907 goto error_return;
908 } else if (remove_dir_recursively(&ref_file,
909 REMOVE_DIR_EMPTY_ONLY)) {
Michael Haggertyb05855b2017-04-16 08:41:26 +0200910 if (refs_verify_refname_available(
911 &refs->base, refname,
912 extras, skip, err)) {
Michael Haggerty92b15512016-04-25 15:56:07 +0200913 /*
914 * The error message set by
915 * verify_refname_available() is OK.
916 */
917 ret = TRANSACTION_NAME_CONFLICT;
918 goto error_return;
919 } else {
920 /*
921 * We can't delete the directory,
922 * but we also don't know of any
923 * references that it should
924 * contain.
925 */
926 strbuf_addf(err, "there is a non-empty directory '%s' "
927 "blocking reference '%s'",
928 ref_file.buf, refname);
929 goto error_return;
930 }
931 }
932 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
933 strbuf_addf(err, "unable to resolve reference '%s': "
934 "reference broken", refname);
935 goto error_return;
936 } else {
937 strbuf_addf(err, "unable to resolve reference '%s': %s",
938 refname, strerror(errno));
939 goto error_return;
940 }
941
942 /*
943 * If the ref did not exist and we are creating it,
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200944 * make sure there is no existing ref that conflicts
945 * with refname:
Michael Haggerty92b15512016-04-25 15:56:07 +0200946 */
Michael Haggerty524a9fd2017-04-16 08:41:27 +0200947 if (refs_verify_refname_available(
948 &refs->base, refname,
949 extras, skip, err))
Michael Haggerty92b15512016-04-25 15:56:07 +0200950 goto error_return;
Michael Haggerty92b15512016-04-25 15:56:07 +0200951 }
952
953 ret = 0;
954 goto out;
955
956error_return:
957 unlock_ref(lock);
958 *lock_p = NULL;
959
960out:
961 strbuf_release(&ref_file);
962 return ret;
963}
964
Michael Haggertybd427cf2016-09-04 18:08:29 +0200965static int files_peel_ref(struct ref_store *ref_store,
966 const char *refname, unsigned char *sha1)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100967{
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700968 struct files_ref_store *refs =
969 files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
970 "peel_ref");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100971 int flag;
972 unsigned char base[20];
973
Michael Haggerty4c4de892016-06-18 06:15:16 +0200974 if (current_ref_iter && current_ref_iter->refname == refname) {
975 struct object_id peeled;
976
977 if (ref_iterator_peel(current_ref_iter, &peeled))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100978 return -1;
Michael Haggerty4c4de892016-06-18 06:15:16 +0200979 hashcpy(sha1, peeled.hash);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100980 return 0;
981 }
982
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +0700983 if (refs_read_ref_full(ref_store, refname,
984 RESOLVE_REF_READING, base, &flag))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100985 return -1;
986
987 /*
988 * If the reference is packed, read its ref_entry from the
989 * cache in the hope that we already know its peeled value.
990 * We only try this optimization on packed references because
991 * (a) forcing the filling of the loose reference cache could
992 * be expensive and (b) loose references anyway usually do not
993 * have REF_KNOWS_PEELED.
994 */
995 if (flag & REF_ISPACKED) {
Michael Haggertyf0d21ef2016-09-04 18:08:13 +0200996 struct ref_entry *r = get_packed_ref(refs, refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +0100997 if (r) {
998 if (peel_entry(r, 0))
999 return -1;
1000 hashcpy(sha1, r->u.value.peeled.hash);
1001 return 0;
1002 }
1003 }
1004
1005 return peel_object(base, sha1);
1006}
1007
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001008struct files_ref_iterator {
1009 struct ref_iterator base;
1010
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001011 struct packed_ref_cache *packed_ref_cache;
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001012 struct ref_iterator *iter0;
1013 unsigned int flags;
1014};
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001015
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001016static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
1017{
1018 struct files_ref_iterator *iter =
1019 (struct files_ref_iterator *)ref_iterator;
1020 int ok;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001021
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001022 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
David Turner0c09ec02016-09-04 18:08:44 +02001023 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
1024 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
1025 continue;
1026
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001027 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
1028 !ref_resolves_to_object(iter->iter0->refname,
1029 iter->iter0->oid,
1030 iter->iter0->flags))
1031 continue;
1032
1033 iter->base.refname = iter->iter0->refname;
1034 iter->base.oid = iter->iter0->oid;
1035 iter->base.flags = iter->iter0->flags;
1036 return ITER_OK;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001037 }
1038
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001039 iter->iter0 = NULL;
1040 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
1041 ok = ITER_ERROR;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001042
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001043 return ok;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001044}
1045
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001046static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,
1047 struct object_id *peeled)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001048{
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001049 struct files_ref_iterator *iter =
1050 (struct files_ref_iterator *)ref_iterator;
David Turner93770592016-04-07 15:02:49 -04001051
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001052 return ref_iterator_peel(iter->iter0, peeled);
1053}
1054
1055static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
1056{
1057 struct files_ref_iterator *iter =
1058 (struct files_ref_iterator *)ref_iterator;
1059 int ok = ITER_DONE;
1060
1061 if (iter->iter0)
1062 ok = ref_iterator_abort(iter->iter0);
1063
1064 release_packed_ref_cache(iter->packed_ref_cache);
1065 base_ref_iterator_free(ref_iterator);
1066 return ok;
1067}
1068
1069static struct ref_iterator_vtable files_ref_iterator_vtable = {
1070 files_ref_iterator_advance,
1071 files_ref_iterator_peel,
1072 files_ref_iterator_abort
1073};
1074
Michael Haggerty1a769002016-09-04 18:08:37 +02001075static struct ref_iterator *files_ref_iterator_begin(
Michael Haggerty37b6f6d2016-09-04 18:08:36 +02001076 struct ref_store *ref_store,
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001077 const char *prefix, unsigned int flags)
1078{
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001079 struct files_ref_store *refs;
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001080 struct ref_dir *loose_dir, *packed_dir;
1081 struct ref_iterator *loose_iter, *packed_iter;
1082 struct files_ref_iterator *iter;
1083 struct ref_iterator *ref_iterator;
1084
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001085 if (ref_paranoia < 0)
1086 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
1087 if (ref_paranoia)
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001088 flags |= DO_FOR_EACH_INCLUDE_BROKEN;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001089
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001090 refs = files_downcast(ref_store,
1091 REF_STORE_READ | (ref_paranoia ? 0 : REF_STORE_ODB),
1092 "ref_iterator_begin");
1093
Michael Haggerty3bc581b2016-06-18 06:15:15 +02001094 iter = xcalloc(1, sizeof(*iter));
1095 ref_iterator = &iter->base;
1096 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
1097
1098 /*
1099 * We must make sure that all loose refs are read before
1100 * accessing the packed-refs file; this avoids a race
1101 * condition if loose refs are migrated to the packed-refs
1102 * file by a simultaneous process, but our in-memory view is
1103 * from before the migration. We ensure this as follows:
1104 * First, we call prime_ref_dir(), which pre-reads the loose
1105 * references for the subtree into the cache. (If they've
1106 * already been read, that's OK; we only need to guarantee
1107 * that they're read before the packed refs, not *how much*
1108 * before.) After that, we call get_packed_ref_cache(), which
1109 * internally checks whether the packed-ref cache is up to
1110 * date with what is on disk, and re-reads it if not.
1111 */
1112
1113 loose_dir = get_loose_refs(refs);
1114
1115 if (prefix && *prefix)
1116 loose_dir = find_containing_dir(loose_dir, prefix, 0);
1117
1118 if (loose_dir) {
1119 prime_ref_dir(loose_dir);
1120 loose_iter = cache_ref_iterator_begin(loose_dir);
1121 } else {
1122 /* There's nothing to iterate over. */
1123 loose_iter = empty_ref_iterator_begin();
1124 }
1125
1126 iter->packed_ref_cache = get_packed_ref_cache(refs);
1127 acquire_packed_ref_cache(iter->packed_ref_cache);
1128 packed_dir = get_packed_ref_dir(iter->packed_ref_cache);
1129
1130 if (prefix && *prefix)
1131 packed_dir = find_containing_dir(packed_dir, prefix, 0);
1132
1133 if (packed_dir) {
1134 packed_iter = cache_ref_iterator_begin(packed_dir);
1135 } else {
1136 /* There's nothing to iterate over. */
1137 packed_iter = empty_ref_iterator_begin();
1138 }
1139
1140 iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
1141 iter->flags = flags;
1142
1143 return ref_iterator;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001144}
1145
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001146/*
1147 * Verify that the reference locked by lock has the value old_sha1.
1148 * Fail if the reference doesn't exist and mustexist is set. Return 0
1149 * on success. On error, write an error message to err, set errno, and
1150 * return a negative value.
1151 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001152static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001153 const unsigned char *old_sha1, int mustexist,
1154 struct strbuf *err)
1155{
1156 assert(err);
1157
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001158 if (refs_read_ref_full(ref_store, lock->ref_name,
1159 mustexist ? RESOLVE_REF_READING : 0,
1160 lock->old_oid.hash, NULL)) {
Jeff King6294dcb2016-01-12 16:44:39 -05001161 if (old_sha1) {
1162 int save_errno = errno;
Michael Haggerty0568c8e2016-04-27 15:21:36 +02001163 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
Jeff King6294dcb2016-01-12 16:44:39 -05001164 errno = save_errno;
1165 return -1;
1166 } else {
brian m. carlsonc368dde2016-06-24 23:09:22 +00001167 oidclr(&lock->old_oid);
Jeff King6294dcb2016-01-12 16:44:39 -05001168 return 0;
1169 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001170 }
Jeff King6294dcb2016-01-12 16:44:39 -05001171 if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) {
Michael Haggerty0568c8e2016-04-27 15:21:36 +02001172 strbuf_addf(err, "ref '%s' is at %s but expected %s",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001173 lock->ref_name,
brian m. carlsonc368dde2016-06-24 23:09:22 +00001174 oid_to_hex(&lock->old_oid),
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001175 sha1_to_hex(old_sha1));
1176 errno = EBUSY;
1177 return -1;
1178 }
1179 return 0;
1180}
1181
1182static int remove_empty_directories(struct strbuf *path)
1183{
1184 /*
1185 * we want to create a file but there is a directory there;
1186 * if that is an empty directory (or a directory that contains
1187 * only empty directories), remove them.
1188 */
1189 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1190}
1191
Michael Haggerty3b5d3c92017-01-06 17:22:28 +01001192static int create_reflock(const char *path, void *cb)
1193{
1194 struct lock_file *lk = cb;
1195
1196 return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;
1197}
1198
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001199/*
1200 * Locks a ref returning the lock on success and NULL on failure.
1201 * On failure errno is set to something meaningful.
1202 */
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02001203static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
1204 const char *refname,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001205 const unsigned char *old_sha1,
1206 const struct string_list *extras,
1207 const struct string_list *skip,
Michael Haggertybcb497d2016-04-22 09:13:00 +02001208 unsigned int flags, int *type,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001209 struct strbuf *err)
1210{
1211 struct strbuf ref_file = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001212 struct ref_lock *lock;
1213 int last_errno = 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001214 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
Michael Haggerty7a418f32016-04-22 15:25:25 +02001215 int resolve_flags = RESOLVE_REF_NO_RECURSE;
Michael Haggerty7a418f32016-04-22 15:25:25 +02001216 int resolved;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001217
Michael Haggerty32c597e2017-02-10 12:16:16 +01001218 files_assert_main_repository(refs, "lock_ref_sha1_basic");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001219 assert(err);
1220
1221 lock = xcalloc(1, sizeof(struct ref_lock));
1222
1223 if (mustexist)
1224 resolve_flags |= RESOLVE_REF_READING;
Jeff King2859dcd2016-01-12 16:45:09 -05001225 if (flags & REF_DELETING)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001226 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001227
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07001228 files_ref_path(refs, &ref_file, refname);
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001229 resolved = !!refs_resolve_ref_unsafe(&refs->base,
1230 refname, resolve_flags,
1231 lock->old_oid.hash, type);
Michael Haggerty7a418f32016-04-22 15:25:25 +02001232 if (!resolved && errno == EISDIR) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001233 /*
1234 * we are trying to lock foo but we used to
1235 * have foo/bar which now does not exist;
1236 * it is normal for the empty directory 'foo'
1237 * to remain.
1238 */
Michael Haggerty7a418f32016-04-22 15:25:25 +02001239 if (remove_empty_directories(&ref_file)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001240 last_errno = errno;
Michael Haggertyb05855b2017-04-16 08:41:26 +02001241 if (!refs_verify_refname_available(
1242 &refs->base,
1243 refname, extras, skip, err))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001244 strbuf_addf(err, "there are still refs under '%s'",
Michael Haggerty7a418f32016-04-22 15:25:25 +02001245 refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001246 goto error_return;
1247 }
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001248 resolved = !!refs_resolve_ref_unsafe(&refs->base,
1249 refname, resolve_flags,
1250 lock->old_oid.hash, type);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001251 }
Michael Haggerty7a418f32016-04-22 15:25:25 +02001252 if (!resolved) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001253 last_errno = errno;
1254 if (last_errno != ENOTDIR ||
Michael Haggertyb05855b2017-04-16 08:41:26 +02001255 !refs_verify_refname_available(&refs->base, refname,
1256 extras, skip, err))
Michael Haggerty0568c8e2016-04-27 15:21:36 +02001257 strbuf_addf(err, "unable to resolve reference '%s': %s",
Michael Haggerty7a418f32016-04-22 15:25:25 +02001258 refname, strerror(last_errno));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001259
1260 goto error_return;
1261 }
Jeff King2859dcd2016-01-12 16:45:09 -05001262
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001263 /*
1264 * If the ref did not exist and we are creating it, make sure
1265 * there is no existing packed ref whose name begins with our
1266 * refname, nor a packed ref whose name is a proper prefix of
1267 * our refname.
1268 */
1269 if (is_null_oid(&lock->old_oid) &&
Michael Haggerty524a9fd2017-04-16 08:41:27 +02001270 refs_verify_refname_available(&refs->base, refname,
1271 extras, skip, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001272 last_errno = ENOTDIR;
1273 goto error_return;
1274 }
1275
1276 lock->lk = xcalloc(1, sizeof(struct lock_file));
1277
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001278 lock->ref_name = xstrdup(refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001279
Michael Haggerty3b5d3c92017-01-06 17:22:28 +01001280 if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001281 last_errno = errno;
Michael Haggerty3b5d3c92017-01-06 17:22:28 +01001282 unable_to_lock_message(ref_file.buf, errno, err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001283 goto error_return;
1284 }
1285
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001286 if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001287 last_errno = errno;
1288 goto error_return;
1289 }
1290 goto out;
1291
1292 error_return:
1293 unlock_ref(lock);
1294 lock = NULL;
1295
1296 out:
1297 strbuf_release(&ref_file);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001298 errno = last_errno;
1299 return lock;
1300}
1301
1302/*
1303 * Write an entry to the packed-refs file for the specified refname.
1304 * If peeled is non-NULL, write it as the entry's peeled value.
1305 */
1306static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1,
1307 unsigned char *peeled)
1308{
1309 fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
1310 if (peeled)
1311 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
1312}
1313
1314/*
1315 * An each_ref_entry_fn that writes the entry to a packed-refs file.
1316 */
1317static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
1318{
1319 enum peel_status peel_status = peel_entry(entry, 0);
1320
1321 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
1322 error("internal error: %s is not a valid packed reference!",
1323 entry->name);
1324 write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,
1325 peel_status == PEEL_PEELED ?
1326 entry->u.value.peeled.hash : NULL);
1327 return 0;
1328}
1329
1330/*
1331 * Lock the packed-refs file for writing. Flags is passed to
1332 * hold_lock_file_for_update(). Return 0 on success. On errors, set
1333 * errno appropriately and return a nonzero value.
1334 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001335static int lock_packed_refs(struct files_ref_store *refs, int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001336{
1337 static int timeout_configured = 0;
1338 static int timeout_value = 1000;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001339 struct packed_ref_cache *packed_ref_cache;
1340
Michael Haggerty32c597e2017-02-10 12:16:16 +01001341 files_assert_main_repository(refs, "lock_packed_refs");
Michael Haggerty49c0df62016-09-04 18:08:15 +02001342
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001343 if (!timeout_configured) {
1344 git_config_get_int("core.packedrefstimeout", &timeout_value);
1345 timeout_configured = 1;
1346 }
1347
1348 if (hold_lock_file_for_update_timeout(
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +07001349 &packlock, files_packed_refs_path(refs),
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001350 flags, timeout_value) < 0)
1351 return -1;
1352 /*
1353 * Get the current packed-refs while holding the lock. If the
1354 * packed-refs file has been modified since we last read it,
1355 * this will automatically invalidate the cache and re-read
1356 * the packed-refs file.
1357 */
Michael Haggerty00eebe32016-09-04 18:08:11 +02001358 packed_ref_cache = get_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001359 packed_ref_cache->lock = &packlock;
1360 /* Increment the reference count to prevent it from being freed: */
1361 acquire_packed_ref_cache(packed_ref_cache);
1362 return 0;
1363}
1364
1365/*
1366 * Write the current version of the packed refs cache from memory to
1367 * disk. The packed-refs file must already be locked for writing (see
1368 * lock_packed_refs()). Return zero on success. On errors, set errno
1369 * and return a nonzero value
1370 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001371static int commit_packed_refs(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001372{
1373 struct packed_ref_cache *packed_ref_cache =
Michael Haggerty00eebe32016-09-04 18:08:11 +02001374 get_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001375 int error = 0;
1376 int save_errno = 0;
1377 FILE *out;
1378
Michael Haggerty32c597e2017-02-10 12:16:16 +01001379 files_assert_main_repository(refs, "commit_packed_refs");
Michael Haggerty49c0df62016-09-04 18:08:15 +02001380
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001381 if (!packed_ref_cache->lock)
1382 die("internal error: packed-refs not locked");
1383
1384 out = fdopen_lock_file(packed_ref_cache->lock, "w");
1385 if (!out)
1386 die_errno("unable to fdopen packed-refs descriptor");
1387
1388 fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
1389 do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
1390 0, write_packed_entry_fn, out);
1391
1392 if (commit_lock_file(packed_ref_cache->lock)) {
1393 save_errno = errno;
1394 error = -1;
1395 }
1396 packed_ref_cache->lock = NULL;
1397 release_packed_ref_cache(packed_ref_cache);
1398 errno = save_errno;
1399 return error;
1400}
1401
1402/*
1403 * Rollback the lockfile for the packed-refs file, and discard the
1404 * in-memory packed reference cache. (The packed-refs file will be
1405 * read anew if it is needed again after this function is called.)
1406 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001407static void rollback_packed_refs(struct files_ref_store *refs)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001408{
1409 struct packed_ref_cache *packed_ref_cache =
Michael Haggerty00eebe32016-09-04 18:08:11 +02001410 get_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001411
Michael Haggerty32c597e2017-02-10 12:16:16 +01001412 files_assert_main_repository(refs, "rollback_packed_refs");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001413
1414 if (!packed_ref_cache->lock)
1415 die("internal error: packed-refs not locked");
1416 rollback_lock_file(packed_ref_cache->lock);
1417 packed_ref_cache->lock = NULL;
1418 release_packed_ref_cache(packed_ref_cache);
Michael Haggerty00eebe32016-09-04 18:08:11 +02001419 clear_packed_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001420}
1421
1422struct ref_to_prune {
1423 struct ref_to_prune *next;
1424 unsigned char sha1[20];
1425 char name[FLEX_ARRAY];
1426};
1427
1428struct pack_refs_cb_data {
1429 unsigned int flags;
1430 struct ref_dir *packed_refs;
1431 struct ref_to_prune *ref_to_prune;
1432};
1433
1434/*
1435 * An each_ref_entry_fn that is run over loose references only. If
1436 * the loose reference can be packed, add an entry in the packed ref
1437 * cache. If the reference should be pruned, also add it to
1438 * ref_to_prune in the pack_refs_cb_data.
1439 */
1440static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
1441{
1442 struct pack_refs_cb_data *cb = cb_data;
1443 enum peel_status peel_status;
1444 struct ref_entry *packed_entry;
1445 int is_tag_ref = starts_with(entry->name, "refs/tags/");
1446
1447 /* Do not pack per-worktree refs: */
1448 if (ref_type(entry->name) != REF_TYPE_NORMAL)
1449 return 0;
1450
1451 /* ALWAYS pack tags */
1452 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
1453 return 0;
1454
1455 /* Do not pack symbolic or broken refs: */
Michael Haggertyffeef642016-06-18 06:15:13 +02001456 if ((entry->flag & REF_ISSYMREF) || !entry_resolves_to_object(entry))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001457 return 0;
1458
1459 /* Add a packed ref cache entry equivalent to the loose entry. */
1460 peel_status = peel_entry(entry, 1);
1461 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
1462 die("internal error peeling reference %s (%s)",
1463 entry->name, oid_to_hex(&entry->u.value.oid));
Michael Haggertybc1c6962017-04-16 08:41:29 +02001464 packed_entry = find_ref_entry(cb->packed_refs, entry->name);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001465 if (packed_entry) {
1466 /* Overwrite existing packed entry with info from loose entry */
1467 packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED;
1468 oidcpy(&packed_entry->u.value.oid, &entry->u.value.oid);
1469 } else {
1470 packed_entry = create_ref_entry(entry->name, entry->u.value.oid.hash,
1471 REF_ISPACKED | REF_KNOWS_PEELED, 0);
Michael Haggertya3ade2b2017-04-16 08:41:28 +02001472 add_ref_entry(cb->packed_refs, packed_entry);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001473 }
1474 oidcpy(&packed_entry->u.value.peeled, &entry->u.value.peeled);
1475
1476 /* Schedule the loose reference for pruning if requested. */
1477 if ((cb->flags & PACK_REFS_PRUNE)) {
Jeff King96ffc062016-02-22 17:44:32 -05001478 struct ref_to_prune *n;
1479 FLEX_ALLOC_STR(n, name, entry->name);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001480 hashcpy(n->sha1, entry->u.value.oid.hash);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001481 n->next = cb->ref_to_prune;
1482 cb->ref_to_prune = n;
1483 }
1484 return 0;
1485}
1486
Michael Haggertya8f0db22017-01-06 17:22:42 +01001487enum {
1488 REMOVE_EMPTY_PARENTS_REF = 0x01,
1489 REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1490};
1491
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001492/*
Michael Haggertya8f0db22017-01-06 17:22:42 +01001493 * Remove empty parent directories associated with the specified
1494 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1495 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1496 * REMOVE_EMPTY_PARENTS_REFLOG.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001497 */
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001498static void try_remove_empty_parents(struct files_ref_store *refs,
1499 const char *refname,
1500 unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001501{
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001502 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001503 struct strbuf sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001504 char *p, *q;
1505 int i;
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001506
1507 strbuf_addstr(&buf, refname);
1508 p = buf.buf;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001509 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1510 while (*p && *p != '/')
1511 p++;
1512 /* tolerate duplicate slashes; see check_refname_format() */
1513 while (*p == '/')
1514 p++;
1515 }
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001516 q = buf.buf + buf.len;
Michael Haggertya8f0db22017-01-06 17:22:42 +01001517 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001518 while (q > p && *q != '/')
1519 q--;
1520 while (q > p && *(q-1) == '/')
1521 q--;
1522 if (q == p)
1523 break;
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001524 strbuf_setlen(&buf, q - buf.buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001525
1526 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07001527 files_ref_path(refs, &sb, buf.buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001528 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
Michael Haggertya8f0db22017-01-06 17:22:42 +01001529 flags &= ~REMOVE_EMPTY_PARENTS_REF;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001530
1531 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001532 files_reflog_path(refs, &sb, buf.buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001533 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
Michael Haggertya8f0db22017-01-06 17:22:42 +01001534 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001535 }
Michael Haggerty8bdaecb2017-01-06 17:22:41 +01001536 strbuf_release(&buf);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001537 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001538}
1539
1540/* make sure nobody touched the ref, and unlink */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001541static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001542{
1543 struct ref_transaction *transaction;
1544 struct strbuf err = STRBUF_INIT;
1545
1546 if (check_refname_format(r->name, 0))
1547 return;
1548
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001549 transaction = ref_store_transaction_begin(&refs->base, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001550 if (!transaction ||
1551 ref_transaction_delete(transaction, r->name, r->sha1,
Michael Haggertyc52ce242016-04-24 09:48:26 +02001552 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001553 ref_transaction_commit(transaction, &err)) {
1554 ref_transaction_free(transaction);
1555 error("%s", err.buf);
1556 strbuf_release(&err);
1557 return;
1558 }
1559 ref_transaction_free(transaction);
1560 strbuf_release(&err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001561}
1562
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001563static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001564{
1565 while (r) {
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001566 prune_ref(refs, r);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001567 r = r->next;
1568 }
1569}
1570
Michael Haggerty82315272016-09-04 18:08:27 +02001571static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001572{
Michael Haggerty00eebe32016-09-04 18:08:11 +02001573 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001574 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1575 "pack_refs");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001576 struct pack_refs_cb_data cbdata;
1577
1578 memset(&cbdata, 0, sizeof(cbdata));
1579 cbdata.flags = flags;
1580
Michael Haggerty49c0df62016-09-04 18:08:15 +02001581 lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
Michael Haggerty00eebe32016-09-04 18:08:11 +02001582 cbdata.packed_refs = get_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001583
Michael Haggerty00eebe32016-09-04 18:08:11 +02001584 do_for_each_entry_in_dir(get_loose_refs(refs), 0,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001585 pack_if_possible_fn, &cbdata);
1586
Michael Haggerty49c0df62016-09-04 18:08:15 +02001587 if (commit_packed_refs(refs))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001588 die_errno("unable to overwrite old ref-pack file");
1589
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001590 prune_refs(refs, cbdata.ref_to_prune);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001591 return 0;
1592}
1593
1594/*
1595 * Rewrite the packed-refs file, omitting any refs listed in
1596 * 'refnames'. On error, leave packed-refs unchanged, write an error
1597 * message to 'err', and return a nonzero value.
1598 *
1599 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
1600 */
Michael Haggerty0a95ac52016-09-04 18:08:30 +02001601static int repack_without_refs(struct files_ref_store *refs,
1602 struct string_list *refnames, struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001603{
1604 struct ref_dir *packed;
1605 struct string_list_item *refname;
1606 int ret, needs_repacking = 0, removed = 0;
1607
Michael Haggerty32c597e2017-02-10 12:16:16 +01001608 files_assert_main_repository(refs, "repack_without_refs");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001609 assert(err);
1610
1611 /* Look for a packed ref */
1612 for_each_string_list_item(refname, refnames) {
Michael Haggertyf0d21ef2016-09-04 18:08:13 +02001613 if (get_packed_ref(refs, refname->string)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001614 needs_repacking = 1;
1615 break;
1616 }
1617 }
1618
1619 /* Avoid locking if we have nothing to do */
1620 if (!needs_repacking)
1621 return 0; /* no refname exists in packed refs */
1622
Michael Haggerty49c0df62016-09-04 18:08:15 +02001623 if (lock_packed_refs(refs, 0)) {
Nguyễn Thái Ngọc Duy33dfb9f2017-03-26 09:42:18 +07001624 unable_to_lock_message(files_packed_refs_path(refs), errno, err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001625 return -1;
1626 }
Michael Haggerty00eebe32016-09-04 18:08:11 +02001627 packed = get_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001628
1629 /* Remove refnames from the cache */
1630 for_each_string_list_item(refname, refnames)
Michael Haggerty9fc3b062017-04-16 08:41:30 +02001631 if (remove_entry_from_dir(packed, refname->string) != -1)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001632 removed = 1;
1633 if (!removed) {
1634 /*
1635 * All packed entries disappeared while we were
1636 * acquiring the lock.
1637 */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001638 rollback_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001639 return 0;
1640 }
1641
1642 /* Write what remains */
Michael Haggerty49c0df62016-09-04 18:08:15 +02001643 ret = commit_packed_refs(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001644 if (ret)
1645 strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
1646 strerror(errno));
1647 return ret;
1648}
1649
David Turnera27dcf82016-09-04 18:08:40 +02001650static int files_delete_refs(struct ref_store *ref_store,
1651 struct string_list *refnames, unsigned int flags)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001652{
Michael Haggerty0a95ac52016-09-04 18:08:30 +02001653 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001654 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001655 struct strbuf err = STRBUF_INIT;
1656 int i, result = 0;
1657
1658 if (!refnames->nr)
1659 return 0;
1660
Michael Haggerty0a95ac52016-09-04 18:08:30 +02001661 result = repack_without_refs(refs, refnames, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001662 if (result) {
1663 /*
1664 * If we failed to rewrite the packed-refs file, then
1665 * it is unsafe to try to remove loose refs, because
1666 * doing so might expose an obsolete packed value for
1667 * a reference that might even point at an object that
1668 * has been garbage collected.
1669 */
1670 if (refnames->nr == 1)
1671 error(_("could not delete reference %s: %s"),
1672 refnames->items[0].string, err.buf);
1673 else
1674 error(_("could not delete references: %s"), err.buf);
1675
1676 goto out;
1677 }
1678
1679 for (i = 0; i < refnames->nr; i++) {
1680 const char *refname = refnames->items[i].string;
1681
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001682 if (refs_delete_ref(&refs->base, NULL, refname, NULL, flags))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001683 result |= error(_("could not remove reference %s"), refname);
1684 }
1685
1686out:
1687 strbuf_release(&err);
1688 return result;
1689}
1690
1691/*
1692 * People using contrib's git-new-workdir have .git/logs/refs ->
1693 * /some/other/path/.git/logs/refs, and that may live on another device.
1694 *
1695 * IOW, to avoid cross device rename errors, the temporary renamed log must
1696 * live into logs/refs.
1697 */
Nguyễn Thái Ngọc Duya5c1efd2017-03-26 09:42:21 +07001698#define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001699
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001700struct rename_cb {
1701 const char *tmp_renamed_log;
1702 int true_errno;
1703};
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001704
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001705static int rename_tmp_log_callback(const char *path, void *cb_data)
1706{
1707 struct rename_cb *cb = cb_data;
1708
1709 if (rename(cb->tmp_renamed_log, path)) {
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001710 /*
1711 * rename(a, b) when b is an existing directory ought
1712 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1713 * Sheesh. Record the true errno for error reporting,
1714 * but report EISDIR to raceproof_create_file() so
1715 * that it knows to retry.
1716 */
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001717 cb->true_errno = errno;
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001718 if (errno == ENOTDIR)
1719 errno = EISDIR;
1720 return -1;
1721 } else {
1722 return 0;
1723 }
1724}
1725
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001726static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001727{
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001728 struct strbuf path = STRBUF_INIT;
1729 struct strbuf tmp = STRBUF_INIT;
1730 struct rename_cb cb;
1731 int ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001732
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001733 files_reflog_path(refs, &path, newrefname);
1734 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001735 cb.tmp_renamed_log = tmp.buf;
1736 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001737 if (ret) {
1738 if (errno == EISDIR)
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001739 error("directory not empty: %s", path.buf);
Michael Haggerty6a7f3632017-01-06 17:22:29 +01001740 else
Michael Haggerty990c98d2017-01-06 17:22:30 +01001741 error("unable to move logfile %s to %s: %s",
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001742 tmp.buf, path.buf,
1743 strerror(cb.true_errno));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001744 }
1745
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001746 strbuf_release(&path);
1747 strbuf_release(&tmp);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001748 return ret;
1749}
1750
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001751static int write_ref_to_lockfile(struct ref_lock *lock,
1752 const unsigned char *sha1, struct strbuf *err);
Michael Haggertyf18a7892016-09-04 18:08:32 +02001753static int commit_ref_update(struct files_ref_store *refs,
1754 struct ref_lock *lock,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001755 const unsigned char *sha1, const char *logmsg,
Michael Haggerty5d9b2de2016-04-22 14:38:56 +02001756 struct strbuf *err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001757
David Turner9b6b40d2016-09-04 18:08:42 +02001758static int files_rename_ref(struct ref_store *ref_store,
1759 const char *oldrefname, const char *newrefname,
1760 const char *logmsg)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001761{
David Turner9b6b40d2016-09-04 18:08:42 +02001762 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07001763 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001764 unsigned char sha1[20], orig_sha1[20];
1765 int flag = 0, logmoved = 0;
1766 struct ref_lock *lock;
1767 struct stat loginfo;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001768 struct strbuf sb_oldref = STRBUF_INIT;
1769 struct strbuf sb_newref = STRBUF_INIT;
1770 struct strbuf tmp_renamed_log = STRBUF_INIT;
1771 int log, ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001772 struct strbuf err = STRBUF_INIT;
1773
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001774 files_reflog_path(refs, &sb_oldref, oldrefname);
1775 files_reflog_path(refs, &sb_newref, newrefname);
1776 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001777
1778 log = !lstat(sb_oldref.buf, &loginfo);
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001779 if (log && S_ISLNK(loginfo.st_mode)) {
1780 ret = error("reflog for %s is a symlink", oldrefname);
1781 goto out;
1782 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001783
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001784 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1785 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001786 orig_sha1, &flag)) {
1787 ret = error("refname %s not found", oldrefname);
1788 goto out;
1789 }
Michael Haggertye711b1a2016-04-21 23:42:19 +02001790
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001791 if (flag & REF_ISSYMREF) {
1792 ret = error("refname %s is a symbolic ref, renaming it is not supported",
1793 oldrefname);
1794 goto out;
1795 }
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +07001796 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001797 ret = 1;
1798 goto out;
1799 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001800
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001801 if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
Nguyễn Thái Ngọc Duya5c1efd2017-03-26 09:42:21 +07001802 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 +07001803 oldrefname, strerror(errno));
1804 goto out;
1805 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001806
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001807 if (refs_delete_ref(&refs->base, logmsg, oldrefname,
1808 orig_sha1, REF_NODEREF)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001809 error("unable to delete old %s", oldrefname);
1810 goto rollback;
1811 }
1812
David Turner12fd3492016-02-24 17:58:51 -05001813 /*
1814 * Since we are doing a shallow lookup, sha1 is not the
1815 * correct value to pass to delete_ref as old_sha1. But that
1816 * doesn't matter, because an old_sha1 check wouldn't add to
1817 * the safety anyway; we want to delete the reference whatever
1818 * its current value.
1819 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07001820 if (!refs_read_ref_full(&refs->base, newrefname,
1821 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1822 sha1, NULL) &&
1823 refs_delete_ref(&refs->base, NULL, newrefname,
1824 NULL, REF_NODEREF)) {
Michael Haggerty58364322017-01-06 17:22:21 +01001825 if (errno == EISDIR) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001826 struct strbuf path = STRBUF_INIT;
1827 int result;
1828
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07001829 files_ref_path(refs, &path, newrefname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001830 result = remove_empty_directories(&path);
1831 strbuf_release(&path);
1832
1833 if (result) {
1834 error("Directory not empty: %s", newrefname);
1835 goto rollback;
1836 }
1837 } else {
1838 error("unable to delete existing %s", newrefname);
1839 goto rollback;
1840 }
1841 }
1842
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001843 if (log && rename_tmp_log(refs, newrefname))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001844 goto rollback;
1845
1846 logmoved = log;
1847
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02001848 lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
1849 REF_NODEREF, NULL, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001850 if (!lock) {
1851 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1852 strbuf_release(&err);
1853 goto rollback;
1854 }
1855 hashcpy(lock->old_oid.hash, orig_sha1);
1856
1857 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
Michael Haggertyf18a7892016-09-04 18:08:32 +02001858 commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001859 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1860 strbuf_release(&err);
1861 goto rollback;
1862 }
1863
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001864 ret = 0;
1865 goto out;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001866
1867 rollback:
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02001868 lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,
1869 REF_NODEREF, NULL, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001870 if (!lock) {
1871 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1872 strbuf_release(&err);
1873 goto rollbacklog;
1874 }
1875
1876 flag = log_all_ref_updates;
Cornelius Weig341fb282017-01-27 11:09:47 +01001877 log_all_ref_updates = LOG_REFS_NONE;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001878 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
Michael Haggertyf18a7892016-09-04 18:08:32 +02001879 commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001880 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1881 strbuf_release(&err);
1882 }
1883 log_all_ref_updates = flag;
1884
1885 rollbacklog:
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001886 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001887 error("unable to restore logfile %s from %s: %s",
1888 oldrefname, newrefname, strerror(errno));
1889 if (!logmoved && log &&
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001890 rename(tmp_renamed_log.buf, sb_oldref.buf))
Nguyễn Thái Ngọc Duya5c1efd2017-03-26 09:42:21 +07001891 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001892 oldrefname, strerror(errno));
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001893 ret = 1;
1894 out:
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07001895 strbuf_release(&sb_newref);
1896 strbuf_release(&sb_oldref);
1897 strbuf_release(&tmp_renamed_log);
1898
Nguyễn Thái Ngọc Duy0a3f07d2017-03-26 09:42:19 +07001899 return ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001900}
1901
1902static int close_ref(struct ref_lock *lock)
1903{
1904 if (close_lock_file(lock->lk))
1905 return -1;
1906 return 0;
1907}
1908
1909static int commit_ref(struct ref_lock *lock)
1910{
Michael Haggerty5387c0d2016-05-05 15:33:03 +02001911 char *path = get_locked_file_path(lock->lk);
1912 struct stat st;
1913
1914 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1915 /*
1916 * There is a directory at the path we want to rename
1917 * the lockfile to. Hopefully it is empty; try to
1918 * delete it.
1919 */
1920 size_t len = strlen(path);
1921 struct strbuf sb_path = STRBUF_INIT;
1922
1923 strbuf_attach(&sb_path, path, len, len);
1924
1925 /*
1926 * If this fails, commit_lock_file() will also fail
1927 * and will report the problem.
1928 */
1929 remove_empty_directories(&sb_path);
1930 strbuf_release(&sb_path);
1931 } else {
1932 free(path);
1933 }
1934
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001935 if (commit_lock_file(lock->lk))
1936 return -1;
1937 return 0;
1938}
1939
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001940static int open_or_create_logfile(const char *path, void *cb)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001941{
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001942 int *fd = cb;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001943
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001944 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1945 return (*fd < 0) ? -1 : 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001946}
1947
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001948/*
Michael Haggerty4533e532017-01-06 17:22:36 +01001949 * Create a reflog for a ref. If force_create = 0, only create the
1950 * reflog for certain refs (those for which should_autocreate_reflog
1951 * returns non-zero). Otherwise, create it regardless of the reference
1952 * name. If the logfile already existed or was created, return 0 and
1953 * set *logfd to the file descriptor opened for appending to the file.
1954 * If no logfile exists and we decided not to create one, return 0 and
1955 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1956 * return -1.
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001957 */
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001958static int log_ref_setup(struct files_ref_store *refs,
1959 const char *refname, int force_create,
Michael Haggerty4533e532017-01-06 17:22:36 +01001960 int *logfd, struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001961{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07001962 struct strbuf logfile_sb = STRBUF_INIT;
1963 char *logfile;
1964
1965 files_reflog_path(refs, &logfile_sb, refname);
1966 logfile = strbuf_detach(&logfile_sb, NULL);
Michael Haggerty854bda62017-01-06 17:22:32 +01001967
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001968 if (force_create || should_autocreate_reflog(refname)) {
Michael Haggerty4533e532017-01-06 17:22:36 +01001969 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001970 if (errno == ENOENT)
1971 strbuf_addf(err, "unable to create directory for '%s': "
Michael Haggerty4533e532017-01-06 17:22:36 +01001972 "%s", logfile, strerror(errno));
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001973 else if (errno == EISDIR)
1974 strbuf_addf(err, "there are still logs under '%s'",
Michael Haggerty4533e532017-01-06 17:22:36 +01001975 logfile);
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001976 else
Michael Haggerty854bda62017-01-06 17:22:32 +01001977 strbuf_addf(err, "unable to append to '%s': %s",
Michael Haggerty4533e532017-01-06 17:22:36 +01001978 logfile, strerror(errno));
Michael Haggerty1fb0c802017-01-06 17:22:33 +01001979
Michael Haggerty4533e532017-01-06 17:22:36 +01001980 goto error;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001981 }
Michael Haggerty854bda62017-01-06 17:22:32 +01001982 } else {
Michael Haggerty4533e532017-01-06 17:22:36 +01001983 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
Michael Haggertye404f452017-01-06 17:22:34 +01001984 if (*logfd < 0) {
Michael Haggerty854bda62017-01-06 17:22:32 +01001985 if (errno == ENOENT || errno == EISDIR) {
1986 /*
1987 * The logfile doesn't already exist,
1988 * but that is not an error; it only
1989 * means that we won't write log
1990 * entries to it.
1991 */
1992 ;
1993 } else {
1994 strbuf_addf(err, "unable to append to '%s': %s",
Michael Haggerty4533e532017-01-06 17:22:36 +01001995 logfile, strerror(errno));
1996 goto error;
Michael Haggerty854bda62017-01-06 17:22:32 +01001997 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01001998 }
1999 }
2000
Michael Haggertye404f452017-01-06 17:22:34 +01002001 if (*logfd >= 0)
Michael Haggerty4533e532017-01-06 17:22:36 +01002002 adjust_shared_perm(logfile);
Michael Haggerty854bda62017-01-06 17:22:32 +01002003
Michael Haggerty4533e532017-01-06 17:22:36 +01002004 free(logfile);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002005 return 0;
Michael Haggerty4533e532017-01-06 17:22:36 +01002006
2007error:
2008 free(logfile);
2009 return -1;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002010}
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002011
David Turnere3688bd2016-09-04 18:08:38 +02002012static int files_create_reflog(struct ref_store *ref_store,
2013 const char *refname, int force_create,
2014 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002015{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002016 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002017 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
Michael Haggertye404f452017-01-06 17:22:34 +01002018 int fd;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002019
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002020 if (log_ref_setup(refs, refname, force_create, &fd, err))
Michael Haggerty4533e532017-01-06 17:22:36 +01002021 return -1;
2022
Michael Haggertye404f452017-01-06 17:22:34 +01002023 if (fd >= 0)
2024 close(fd);
Michael Haggerty4533e532017-01-06 17:22:36 +01002025
2026 return 0;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002027}
2028
2029static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
2030 const unsigned char *new_sha1,
2031 const char *committer, const char *msg)
2032{
2033 int msglen, written;
2034 unsigned maxlen, len;
2035 char *logrec;
2036
2037 msglen = msg ? strlen(msg) : 0;
2038 maxlen = strlen(committer) + msglen + 100;
2039 logrec = xmalloc(maxlen);
2040 len = xsnprintf(logrec, maxlen, "%s %s %s\n",
2041 sha1_to_hex(old_sha1),
2042 sha1_to_hex(new_sha1),
2043 committer);
2044 if (msglen)
2045 len += copy_reflog_msg(logrec + len - 1, msg) - 1;
2046
2047 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
2048 free(logrec);
2049 if (written != len)
2050 return -1;
2051
2052 return 0;
2053}
2054
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002055static int files_log_ref_write(struct files_ref_store *refs,
2056 const char *refname, const unsigned char *old_sha1,
Nguyễn Thái Ngọc Duy11f84572017-03-26 09:42:15 +07002057 const unsigned char *new_sha1, const char *msg,
2058 int flags, struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002059{
Michael Haggertye404f452017-01-06 17:22:34 +01002060 int logfd, result;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002061
Cornelius Weig341fb282017-01-27 11:09:47 +01002062 if (log_all_ref_updates == LOG_REFS_UNSET)
2063 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002064
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002065 result = log_ref_setup(refs, refname,
2066 flags & REF_FORCE_CREATE_REFLOG,
Michael Haggerty4533e532017-01-06 17:22:36 +01002067 &logfd, err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002068
2069 if (result)
2070 return result;
2071
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002072 if (logfd < 0)
2073 return 0;
2074 result = log_ref_write_fd(logfd, old_sha1, new_sha1,
2075 git_committer_info(0), msg);
2076 if (result) {
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002077 struct strbuf sb = STRBUF_INIT;
Michael Haggerty87b21e02017-01-06 17:22:35 +01002078 int save_errno = errno;
2079
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002080 files_reflog_path(refs, &sb, refname);
Michael Haggerty87b21e02017-01-06 17:22:35 +01002081 strbuf_addf(err, "unable to append to '%s': %s",
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002082 sb.buf, strerror(save_errno));
2083 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002084 close(logfd);
2085 return -1;
2086 }
2087 if (close(logfd)) {
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002088 struct strbuf sb = STRBUF_INIT;
Michael Haggerty87b21e02017-01-06 17:22:35 +01002089 int save_errno = errno;
2090
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002091 files_reflog_path(refs, &sb, refname);
Michael Haggerty87b21e02017-01-06 17:22:35 +01002092 strbuf_addf(err, "unable to append to '%s': %s",
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002093 sb.buf, strerror(save_errno));
2094 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002095 return -1;
2096 }
2097 return 0;
2098}
2099
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002100/*
2101 * Write sha1 into the open lockfile, then close the lockfile. On
2102 * errors, rollback the lockfile, fill in *err and
2103 * return -1.
2104 */
2105static int write_ref_to_lockfile(struct ref_lock *lock,
2106 const unsigned char *sha1, struct strbuf *err)
2107{
2108 static char term = '\n';
2109 struct object *o;
2110 int fd;
2111
2112 o = parse_object(sha1);
2113 if (!o) {
2114 strbuf_addf(err,
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002115 "trying to write ref '%s' with nonexistent object %s",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002116 lock->ref_name, sha1_to_hex(sha1));
2117 unlock_ref(lock);
2118 return -1;
2119 }
2120 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2121 strbuf_addf(err,
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002122 "trying to write non-commit object %s to branch '%s'",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002123 sha1_to_hex(sha1), lock->ref_name);
2124 unlock_ref(lock);
2125 return -1;
2126 }
2127 fd = get_lock_file_fd(lock->lk);
2128 if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
2129 write_in_full(fd, &term, 1) != 1 ||
2130 close_ref(lock) < 0) {
2131 strbuf_addf(err,
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002132 "couldn't write '%s'", get_lock_file_path(lock->lk));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002133 unlock_ref(lock);
2134 return -1;
2135 }
2136 return 0;
2137}
2138
2139/*
2140 * Commit a change to a loose reference that has already been written
2141 * to the loose reference lockfile. Also update the reflogs if
2142 * necessary, using the specified lockmsg (which can be NULL).
2143 */
Michael Haggertyf18a7892016-09-04 18:08:32 +02002144static int commit_ref_update(struct files_ref_store *refs,
2145 struct ref_lock *lock,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002146 const unsigned char *sha1, const char *logmsg,
Michael Haggerty5d9b2de2016-04-22 14:38:56 +02002147 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002148{
Michael Haggerty32c597e2017-02-10 12:16:16 +01002149 files_assert_main_repository(refs, "commit_ref_update");
Michael Haggerty00eebe32016-09-04 18:08:11 +02002150
2151 clear_loose_ref_cache(refs);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002152 if (files_log_ref_write(refs, lock->ref_name,
2153 lock->old_oid.hash, sha1,
Michael Haggerty81b1b6d2017-01-06 17:22:31 +01002154 logmsg, 0, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002155 char *old_msg = strbuf_detach(err, NULL);
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002156 strbuf_addf(err, "cannot update the ref '%s': %s",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002157 lock->ref_name, old_msg);
2158 free(old_msg);
2159 unlock_ref(lock);
2160 return -1;
2161 }
Michael Haggerty7a418f32016-04-22 15:25:25 +02002162
2163 if (strcmp(lock->ref_name, "HEAD") != 0) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002164 /*
2165 * Special hack: If a branch is updated directly and HEAD
2166 * points to it (may happen on the remote side of a push
2167 * for example) then logically the HEAD reflog should be
2168 * updated too.
2169 * A generic solution implies reverse symref information,
2170 * but finding all symrefs pointing to the given branch
2171 * would be rather costly for this rare event (the direct
2172 * update of a branch) to be worth it. So let's cheat and
2173 * check with HEAD only which should cover 99% of all usage
2174 * scenarios (even 100% of the default ones).
2175 */
2176 unsigned char head_sha1[20];
2177 int head_flag;
2178 const char *head_ref;
Michael Haggerty7a418f32016-04-22 15:25:25 +02002179
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002180 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2181 RESOLVE_REF_READING,
2182 head_sha1, &head_flag);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002183 if (head_ref && (head_flag & REF_ISSYMREF) &&
2184 !strcmp(head_ref, lock->ref_name)) {
2185 struct strbuf log_err = STRBUF_INIT;
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002186 if (files_log_ref_write(refs, "HEAD",
2187 lock->old_oid.hash, sha1,
2188 logmsg, 0, &log_err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002189 error("%s", log_err.buf);
2190 strbuf_release(&log_err);
2191 }
2192 }
2193 }
Michael Haggerty7a418f32016-04-22 15:25:25 +02002194
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002195 if (commit_ref(lock)) {
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002196 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002197 unlock_ref(lock);
2198 return -1;
2199 }
2200
2201 unlock_ref(lock);
2202 return 0;
2203}
2204
Jeff King370e5ad2015-12-29 00:57:01 -05002205static int create_ref_symlink(struct ref_lock *lock, const char *target)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002206{
Jeff King370e5ad2015-12-29 00:57:01 -05002207 int ret = -1;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002208#ifndef NO_SYMLINK_HEAD
Jeff King370e5ad2015-12-29 00:57:01 -05002209 char *ref_path = get_locked_file_path(lock->lk);
2210 unlink(ref_path);
2211 ret = symlink(target, ref_path);
2212 free(ref_path);
2213
2214 if (ret)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002215 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002216#endif
Jeff King370e5ad2015-12-29 00:57:01 -05002217 return ret;
2218}
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002219
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002220static void update_symref_reflog(struct files_ref_store *refs,
2221 struct ref_lock *lock, const char *refname,
Jeff King370e5ad2015-12-29 00:57:01 -05002222 const char *target, const char *logmsg)
2223{
2224 struct strbuf err = STRBUF_INIT;
2225 unsigned char new_sha1[20];
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002226 if (logmsg &&
2227 !refs_read_ref_full(&refs->base, target,
2228 RESOLVE_REF_READING, new_sha1, NULL) &&
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002229 files_log_ref_write(refs, refname, lock->old_oid.hash,
2230 new_sha1, logmsg, 0, &err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002231 error("%s", err.buf);
2232 strbuf_release(&err);
2233 }
Jeff King370e5ad2015-12-29 00:57:01 -05002234}
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002235
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002236static int create_symref_locked(struct files_ref_store *refs,
2237 struct ref_lock *lock, const char *refname,
Jeff King370e5ad2015-12-29 00:57:01 -05002238 const char *target, const char *logmsg)
2239{
2240 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002241 update_symref_reflog(refs, lock, refname, target, logmsg);
Jeff King370e5ad2015-12-29 00:57:01 -05002242 return 0;
2243 }
2244
2245 if (!fdopen_lock_file(lock->lk, "w"))
2246 return error("unable to fdopen %s: %s",
2247 lock->lk->tempfile.filename.buf, strerror(errno));
2248
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002249 update_symref_reflog(refs, lock, refname, target, logmsg);
Jeff King396da8f2015-12-29 00:57:25 -05002250
Jeff King370e5ad2015-12-29 00:57:01 -05002251 /* no error check; commit_ref will check ferror */
2252 fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
2253 if (commit_ref(lock) < 0)
2254 return error("unable to write symref for %s: %s", refname,
2255 strerror(errno));
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002256 return 0;
2257}
2258
Michael Haggerty284689b2016-09-04 18:08:28 +02002259static int files_create_symref(struct ref_store *ref_store,
2260 const char *refname, const char *target,
2261 const char *logmsg)
Jeff King370e5ad2015-12-29 00:57:01 -05002262{
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02002263 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002264 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
Jeff King370e5ad2015-12-29 00:57:01 -05002265 struct strbuf err = STRBUF_INIT;
2266 struct ref_lock *lock;
2267 int ret;
2268
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02002269 lock = lock_ref_sha1_basic(refs, refname, NULL,
2270 NULL, NULL, REF_NODEREF, NULL,
Jeff King370e5ad2015-12-29 00:57:01 -05002271 &err);
2272 if (!lock) {
2273 error("%s", err.buf);
2274 strbuf_release(&err);
2275 return -1;
2276 }
2277
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002278 ret = create_symref_locked(refs, lock, refname, target, logmsg);
Jeff King370e5ad2015-12-29 00:57:01 -05002279 unlock_ref(lock);
2280 return ret;
2281}
2282
Kyle Meyer39ee4c62017-02-20 20:10:35 -05002283int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
Kazuki Yamaguchi22330662016-03-27 23:37:13 +09002284{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002285 /*
2286 * FIXME: this obviously will not work well for future refs
2287 * backends. This function needs to die.
2288 */
2289 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002290 files_downcast(get_main_ref_store(),
2291 REF_STORE_WRITE,
2292 "set_head_symref");
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002293
Kazuki Yamaguchi22330662016-03-27 23:37:13 +09002294 static struct lock_file head_lock;
2295 struct ref_lock *lock;
Kazuki Yamaguchi22330662016-03-27 23:37:13 +09002296 struct strbuf head_path = STRBUF_INIT;
2297 const char *head_rel;
2298 int ret;
2299
2300 strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
2301 if (hold_lock_file_for_update(&head_lock, head_path.buf,
2302 LOCK_NO_DEREF) < 0) {
Kazuki Yamaguchi18eb3a92016-04-08 17:03:07 +09002303 struct strbuf err = STRBUF_INIT;
2304 unable_to_lock_message(head_path.buf, errno, &err);
Kazuki Yamaguchi22330662016-03-27 23:37:13 +09002305 error("%s", err.buf);
2306 strbuf_release(&err);
2307 strbuf_release(&head_path);
2308 return -1;
2309 }
2310
2311 /* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
2312 linked trees */
2313 head_rel = remove_leading_path(head_path.buf,
2314 absolute_path(get_git_common_dir()));
2315 /* to make use of create_symref_locked(), initialize ref_lock */
2316 lock = xcalloc(1, sizeof(struct ref_lock));
2317 lock->lk = &head_lock;
2318 lock->ref_name = xstrdup(head_rel);
Kazuki Yamaguchi22330662016-03-27 23:37:13 +09002319
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002320 ret = create_symref_locked(refs, lock, head_rel, target, logmsg);
Kazuki Yamaguchi22330662016-03-27 23:37:13 +09002321
2322 unlock_ref(lock); /* will free lock */
2323 strbuf_release(&head_path);
2324 return ret;
2325}
2326
David Turnere3688bd2016-09-04 18:08:38 +02002327static int files_reflog_exists(struct ref_store *ref_store,
2328 const char *refname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002329{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002330 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002331 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002332 struct strbuf sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002333 struct stat st;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002334 int ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002335
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002336 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002337 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
2338 strbuf_release(&sb);
2339 return ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002340}
2341
David Turnere3688bd2016-09-04 18:08:38 +02002342static int files_delete_reflog(struct ref_store *ref_store,
2343 const char *refname)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002344{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002345 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002346 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002347 struct strbuf sb = STRBUF_INIT;
2348 int ret;
2349
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002350 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002351 ret = remove_path(sb.buf);
2352 strbuf_release(&sb);
2353 return ret;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002354}
2355
2356static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
2357{
brian m. carlson9461d272017-02-21 23:47:32 +00002358 struct object_id ooid, noid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002359 char *email_end, *message;
2360 unsigned long timestamp;
2361 int tz;
brian m. carlson43bc3b62017-02-21 23:47:33 +00002362 const char *p = sb->buf;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002363
2364 /* old SP new SP name <email> SP time TAB msg LF */
brian m. carlson43bc3b62017-02-21 23:47:33 +00002365 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
2366 parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||
2367 parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
2368 !(email_end = strchr(p, '>')) ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002369 email_end[1] != ' ' ||
2370 !(timestamp = strtoul(email_end + 2, &message, 10)) ||
2371 !message || message[0] != ' ' ||
2372 (message[1] != '+' && message[1] != '-') ||
2373 !isdigit(message[2]) || !isdigit(message[3]) ||
2374 !isdigit(message[4]) || !isdigit(message[5]))
2375 return 0; /* corrupt? */
2376 email_end[1] = '\0';
2377 tz = strtol(message + 1, NULL, 10);
2378 if (message[6] != '\t')
2379 message += 6;
2380 else
2381 message += 7;
brian m. carlson43bc3b62017-02-21 23:47:33 +00002382 return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002383}
2384
2385static char *find_beginning_of_line(char *bob, char *scan)
2386{
2387 while (bob < scan && *(--scan) != '\n')
2388 ; /* keep scanning backwards */
2389 /*
2390 * Return either beginning of the buffer, or LF at the end of
2391 * the previous line.
2392 */
2393 return scan;
2394}
2395
David Turnere3688bd2016-09-04 18:08:38 +02002396static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2397 const char *refname,
2398 each_reflog_ent_fn fn,
2399 void *cb_data)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002400{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002401 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002402 files_downcast(ref_store, REF_STORE_READ,
2403 "for_each_reflog_ent_reverse");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002404 struct strbuf sb = STRBUF_INIT;
2405 FILE *logfp;
2406 long pos;
2407 int ret = 0, at_tail = 1;
2408
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002409 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002410 logfp = fopen(sb.buf, "r");
2411 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002412 if (!logfp)
2413 return -1;
2414
2415 /* Jump to the end */
2416 if (fseek(logfp, 0, SEEK_END) < 0)
2417 return error("cannot seek back reflog for %s: %s",
2418 refname, strerror(errno));
2419 pos = ftell(logfp);
2420 while (!ret && 0 < pos) {
2421 int cnt;
2422 size_t nread;
2423 char buf[BUFSIZ];
2424 char *endp, *scanp;
2425
2426 /* Fill next block from the end */
2427 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
2428 if (fseek(logfp, pos - cnt, SEEK_SET))
2429 return error("cannot seek back reflog for %s: %s",
2430 refname, strerror(errno));
2431 nread = fread(buf, cnt, 1, logfp);
2432 if (nread != 1)
2433 return error("cannot read %d bytes from reflog for %s: %s",
2434 cnt, refname, strerror(errno));
2435 pos -= cnt;
2436
2437 scanp = endp = buf + cnt;
2438 if (at_tail && scanp[-1] == '\n')
2439 /* Looking at the final LF at the end of the file */
2440 scanp--;
2441 at_tail = 0;
2442
2443 while (buf < scanp) {
2444 /*
2445 * terminating LF of the previous line, or the beginning
2446 * of the buffer.
2447 */
2448 char *bp;
2449
2450 bp = find_beginning_of_line(buf, scanp);
2451
2452 if (*bp == '\n') {
2453 /*
2454 * The newline is the end of the previous line,
2455 * so we know we have complete line starting
2456 * at (bp + 1). Prefix it onto any prior data
2457 * we collected for the line and process it.
2458 */
2459 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2460 scanp = bp;
2461 endp = bp + 1;
2462 ret = show_one_reflog_ent(&sb, fn, cb_data);
2463 strbuf_reset(&sb);
2464 if (ret)
2465 break;
2466 } else if (!pos) {
2467 /*
2468 * We are at the start of the buffer, and the
2469 * start of the file; there is no previous
2470 * line, and we have everything for this one.
2471 * Process it, and we can end the loop.
2472 */
2473 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2474 ret = show_one_reflog_ent(&sb, fn, cb_data);
2475 strbuf_reset(&sb);
2476 break;
2477 }
2478
2479 if (bp == buf) {
2480 /*
2481 * We are at the start of the buffer, and there
2482 * is more file to read backwards. Which means
2483 * we are in the middle of a line. Note that we
2484 * may get here even if *bp was a newline; that
2485 * just means we are at the exact end of the
2486 * previous line, rather than some spot in the
2487 * middle.
2488 *
2489 * Save away what we have to be combined with
2490 * the data from the next read.
2491 */
2492 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2493 break;
2494 }
2495 }
2496
2497 }
2498 if (!ret && sb.len)
2499 die("BUG: reverse reflog parser had leftover data");
2500
2501 fclose(logfp);
2502 strbuf_release(&sb);
2503 return ret;
2504}
2505
David Turnere3688bd2016-09-04 18:08:38 +02002506static int files_for_each_reflog_ent(struct ref_store *ref_store,
2507 const char *refname,
2508 each_reflog_ent_fn fn, void *cb_data)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002509{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002510 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002511 files_downcast(ref_store, REF_STORE_READ,
2512 "for_each_reflog_ent");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002513 FILE *logfp;
2514 struct strbuf sb = STRBUF_INIT;
2515 int ret = 0;
2516
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002517 files_reflog_path(refs, &sb, refname);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002518 logfp = fopen(sb.buf, "r");
2519 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002520 if (!logfp)
2521 return -1;
2522
2523 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2524 ret = show_one_reflog_ent(&sb, fn, cb_data);
2525 fclose(logfp);
2526 strbuf_release(&sb);
2527 return ret;
2528}
Michael Haggerty2880d162016-06-18 06:15:19 +02002529
2530struct files_reflog_iterator {
2531 struct ref_iterator base;
2532
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002533 struct ref_store *ref_store;
Michael Haggerty2880d162016-06-18 06:15:19 +02002534 struct dir_iterator *dir_iterator;
2535 struct object_id oid;
2536};
2537
2538static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002539{
Michael Haggerty2880d162016-06-18 06:15:19 +02002540 struct files_reflog_iterator *iter =
2541 (struct files_reflog_iterator *)ref_iterator;
2542 struct dir_iterator *diter = iter->dir_iterator;
2543 int ok;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002544
Michael Haggerty2880d162016-06-18 06:15:19 +02002545 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2546 int flags;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002547
Michael Haggerty2880d162016-06-18 06:15:19 +02002548 if (!S_ISREG(diter->st.st_mode))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002549 continue;
Michael Haggerty2880d162016-06-18 06:15:19 +02002550 if (diter->basename[0] == '.')
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002551 continue;
Michael Haggerty2880d162016-06-18 06:15:19 +02002552 if (ends_with(diter->basename, ".lock"))
2553 continue;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002554
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002555 if (refs_read_ref_full(iter->ref_store,
2556 diter->relative_path, 0,
2557 iter->oid.hash, &flags)) {
Michael Haggerty2880d162016-06-18 06:15:19 +02002558 error("bad ref for %s", diter->path.buf);
2559 continue;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002560 }
Michael Haggerty2880d162016-06-18 06:15:19 +02002561
2562 iter->base.refname = diter->relative_path;
2563 iter->base.oid = &iter->oid;
2564 iter->base.flags = flags;
2565 return ITER_OK;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002566 }
Michael Haggerty2880d162016-06-18 06:15:19 +02002567
2568 iter->dir_iterator = NULL;
2569 if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
2570 ok = ITER_ERROR;
2571 return ok;
2572}
2573
2574static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2575 struct object_id *peeled)
2576{
2577 die("BUG: ref_iterator_peel() called for reflog_iterator");
2578}
2579
2580static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
2581{
2582 struct files_reflog_iterator *iter =
2583 (struct files_reflog_iterator *)ref_iterator;
2584 int ok = ITER_DONE;
2585
2586 if (iter->dir_iterator)
2587 ok = dir_iterator_abort(iter->dir_iterator);
2588
2589 base_ref_iterator_free(ref_iterator);
2590 return ok;
2591}
2592
2593static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2594 files_reflog_iterator_advance,
2595 files_reflog_iterator_peel,
2596 files_reflog_iterator_abort
2597};
2598
David Turnere3688bd2016-09-04 18:08:38 +02002599static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
Michael Haggerty2880d162016-06-18 06:15:19 +02002600{
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002601 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002602 files_downcast(ref_store, REF_STORE_READ,
2603 "reflog_iterator_begin");
Michael Haggerty2880d162016-06-18 06:15:19 +02002604 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2605 struct ref_iterator *ref_iterator = &iter->base;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002606 struct strbuf sb = STRBUF_INIT;
Michael Haggerty2880d162016-06-18 06:15:19 +02002607
2608 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07002609 files_reflog_path(refs, &sb, NULL);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002610 iter->dir_iterator = dir_iterator_begin(sb.buf);
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002611 iter->ref_store = ref_store;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002612 strbuf_release(&sb);
Michael Haggerty2880d162016-06-18 06:15:19 +02002613 return ref_iterator;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002614}
2615
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002616static int ref_update_reject_duplicates(struct string_list *refnames,
2617 struct strbuf *err)
2618{
2619 int i, n = refnames->nr;
2620
2621 assert(err);
2622
2623 for (i = 1; i < n; i++)
2624 if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
2625 strbuf_addf(err,
Michael Haggerty0568c8e2016-04-27 15:21:36 +02002626 "multiple updates for ref '%s' not allowed.",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002627 refnames->items[i].string);
2628 return 1;
2629 }
2630 return 0;
2631}
2632
Michael Haggerty165056b2016-04-24 08:58:41 +02002633/*
Michael Haggerty92b15512016-04-25 15:56:07 +02002634 * If update is a direct update of head_ref (the reference pointed to
2635 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
Michael Haggerty165056b2016-04-24 08:58:41 +02002636 */
Michael Haggerty92b15512016-04-25 15:56:07 +02002637static int split_head_update(struct ref_update *update,
2638 struct ref_transaction *transaction,
2639 const char *head_ref,
2640 struct string_list *affected_refnames,
2641 struct strbuf *err)
2642{
2643 struct string_list_item *item;
2644 struct ref_update *new_update;
2645
2646 if ((update->flags & REF_LOG_ONLY) ||
2647 (update->flags & REF_ISPRUNING) ||
2648 (update->flags & REF_UPDATE_VIA_HEAD))
2649 return 0;
2650
2651 if (strcmp(update->refname, head_ref))
2652 return 0;
2653
2654 /*
2655 * First make sure that HEAD is not already in the
2656 * transaction. This insertion is O(N) in the transaction
2657 * size, but it happens at most once per transaction.
2658 */
2659 item = string_list_insert(affected_refnames, "HEAD");
2660 if (item->util) {
2661 /* An entry already existed */
2662 strbuf_addf(err,
2663 "multiple updates for 'HEAD' (including one "
2664 "via its referent '%s') are not allowed",
2665 update->refname);
2666 return TRANSACTION_NAME_CONFLICT;
2667 }
2668
2669 new_update = ref_transaction_add_update(
2670 transaction, "HEAD",
2671 update->flags | REF_LOG_ONLY | REF_NODEREF,
2672 update->new_sha1, update->old_sha1,
2673 update->msg);
2674
2675 item->util = new_update;
2676
2677 return 0;
2678}
2679
2680/*
2681 * update is for a symref that points at referent and doesn't have
2682 * REF_NODEREF set. Split it into two updates:
2683 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2684 * - A new, separate update for the referent reference
2685 * Note that the new update will itself be subject to splitting when
2686 * the iteration gets to it.
2687 */
Michael Haggertyfcc42ea2016-09-04 18:08:35 +02002688static int split_symref_update(struct files_ref_store *refs,
2689 struct ref_update *update,
Michael Haggerty92b15512016-04-25 15:56:07 +02002690 const char *referent,
Michael Haggerty165056b2016-04-24 08:58:41 +02002691 struct ref_transaction *transaction,
2692 struct string_list *affected_refnames,
2693 struct strbuf *err)
2694{
Michael Haggerty92b15512016-04-25 15:56:07 +02002695 struct string_list_item *item;
2696 struct ref_update *new_update;
2697 unsigned int new_flags;
Michael Haggerty165056b2016-04-24 08:58:41 +02002698
Michael Haggerty92b15512016-04-25 15:56:07 +02002699 /*
2700 * First make sure that referent is not already in the
2701 * transaction. This insertion is O(N) in the transaction
2702 * size, but it happens at most once per symref in a
2703 * transaction.
2704 */
2705 item = string_list_insert(affected_refnames, referent);
2706 if (item->util) {
2707 /* An entry already existed */
2708 strbuf_addf(err,
2709 "multiple updates for '%s' (including one "
2710 "via symref '%s') are not allowed",
2711 referent, update->refname);
2712 return TRANSACTION_NAME_CONFLICT;
2713 }
2714
2715 new_flags = update->flags;
2716 if (!strcmp(update->refname, "HEAD")) {
2717 /*
2718 * Record that the new update came via HEAD, so that
2719 * when we process it, split_head_update() doesn't try
2720 * to add another reflog update for HEAD. Note that
2721 * this bit will be propagated if the new_update
2722 * itself needs to be split.
2723 */
2724 new_flags |= REF_UPDATE_VIA_HEAD;
2725 }
2726
2727 new_update = ref_transaction_add_update(
2728 transaction, referent, new_flags,
2729 update->new_sha1, update->old_sha1,
2730 update->msg);
2731
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002732 new_update->parent_update = update;
2733
2734 /*
2735 * Change the symbolic ref update to log only. Also, it
2736 * doesn't need to check its old SHA-1 value, as that will be
2737 * done when new_update is processed.
2738 */
Michael Haggerty92b15512016-04-25 15:56:07 +02002739 update->flags |= REF_LOG_ONLY | REF_NODEREF;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002740 update->flags &= ~REF_HAVE_OLD;
Michael Haggerty92b15512016-04-25 15:56:07 +02002741
2742 item->util = new_update;
2743
2744 return 0;
2745}
2746
2747/*
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002748 * Return the refname under which update was originally requested.
2749 */
2750static const char *original_update_refname(struct ref_update *update)
2751{
2752 while (update->parent_update)
2753 update = update->parent_update;
2754
2755 return update->refname;
2756}
2757
2758/*
Michael Haggertye3f51032016-06-07 09:29:23 +02002759 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2760 * are consistent with oid, which is the reference's current value. If
2761 * everything is OK, return 0; otherwise, write an error message to
2762 * err and return -1.
2763 */
2764static int check_old_oid(struct ref_update *update, struct object_id *oid,
2765 struct strbuf *err)
2766{
2767 if (!(update->flags & REF_HAVE_OLD) ||
2768 !hashcmp(oid->hash, update->old_sha1))
2769 return 0;
2770
2771 if (is_null_sha1(update->old_sha1))
2772 strbuf_addf(err, "cannot lock ref '%s': "
2773 "reference already exists",
2774 original_update_refname(update));
2775 else if (is_null_oid(oid))
2776 strbuf_addf(err, "cannot lock ref '%s': "
2777 "reference is missing but expected %s",
2778 original_update_refname(update),
2779 sha1_to_hex(update->old_sha1));
2780 else
2781 strbuf_addf(err, "cannot lock ref '%s': "
2782 "is at %s but expected %s",
2783 original_update_refname(update),
2784 oid_to_hex(oid),
2785 sha1_to_hex(update->old_sha1));
2786
2787 return -1;
2788}
2789
2790/*
Michael Haggerty92b15512016-04-25 15:56:07 +02002791 * Prepare for carrying out update:
2792 * - Lock the reference referred to by update.
2793 * - Read the reference under lock.
2794 * - Check that its old SHA-1 value (if specified) is correct, and in
2795 * any case record it in update->lock->old_oid for later use when
2796 * writing the reflog.
2797 * - If it is a symref update without REF_NODEREF, split it up into a
2798 * REF_LOG_ONLY update of the symref and add a separate update for
2799 * the referent to transaction.
2800 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2801 * update of HEAD.
2802 */
Michael Haggertyb3bbbc52016-09-04 18:08:33 +02002803static int lock_ref_for_update(struct files_ref_store *refs,
2804 struct ref_update *update,
Michael Haggerty92b15512016-04-25 15:56:07 +02002805 struct ref_transaction *transaction,
2806 const char *head_ref,
2807 struct string_list *affected_refnames,
2808 struct strbuf *err)
2809{
2810 struct strbuf referent = STRBUF_INIT;
2811 int mustexist = (update->flags & REF_HAVE_OLD) &&
2812 !is_null_sha1(update->old_sha1);
2813 int ret;
2814 struct ref_lock *lock;
2815
Michael Haggerty32c597e2017-02-10 12:16:16 +01002816 files_assert_main_repository(refs, "lock_ref_for_update");
Michael Haggertyb3bbbc52016-09-04 18:08:33 +02002817
Michael Haggerty92b15512016-04-25 15:56:07 +02002818 if ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))
Michael Haggerty165056b2016-04-24 08:58:41 +02002819 update->flags |= REF_DELETING;
Michael Haggerty92b15512016-04-25 15:56:07 +02002820
2821 if (head_ref) {
2822 ret = split_head_update(update, transaction, head_ref,
2823 affected_refnames, err);
2824 if (ret)
2825 return ret;
2826 }
2827
Michael Haggertyf7b0a982016-09-04 18:08:31 +02002828 ret = lock_raw_ref(refs, update->refname, mustexist,
Michael Haggerty92b15512016-04-25 15:56:07 +02002829 affected_refnames, NULL,
David Turner7d618262016-09-04 18:08:43 +02002830 &lock, &referent,
Michael Haggerty92b15512016-04-25 15:56:07 +02002831 &update->type, err);
Michael Haggerty92b15512016-04-25 15:56:07 +02002832 if (ret) {
Michael Haggerty165056b2016-04-24 08:58:41 +02002833 char *reason;
2834
Michael Haggerty165056b2016-04-24 08:58:41 +02002835 reason = strbuf_detach(err, NULL);
2836 strbuf_addf(err, "cannot lock ref '%s': %s",
Michael Haggertye3f51032016-06-07 09:29:23 +02002837 original_update_refname(update), reason);
Michael Haggerty165056b2016-04-24 08:58:41 +02002838 free(reason);
2839 return ret;
2840 }
Michael Haggerty92b15512016-04-25 15:56:07 +02002841
David Turner7d618262016-09-04 18:08:43 +02002842 update->backend_data = lock;
Michael Haggerty92b15512016-04-25 15:56:07 +02002843
Michael Haggerty92b15512016-04-25 15:56:07 +02002844 if (update->type & REF_ISSYMREF) {
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002845 if (update->flags & REF_NODEREF) {
2846 /*
2847 * We won't be reading the referent as part of
2848 * the transaction, so we have to read it here
2849 * to record and possibly check old_sha1:
2850 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07002851 if (refs_read_ref_full(&refs->base,
2852 referent.buf, 0,
2853 lock->old_oid.hash, NULL)) {
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002854 if (update->flags & REF_HAVE_OLD) {
2855 strbuf_addf(err, "cannot lock ref '%s': "
Michael Haggertye3f51032016-06-07 09:29:23 +02002856 "error reading reference",
2857 original_update_refname(update));
2858 return -1;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002859 }
Michael Haggertye3f51032016-06-07 09:29:23 +02002860 } else if (check_old_oid(update, &lock->old_oid, err)) {
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002861 return TRANSACTION_GENERIC_ERROR;
2862 }
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002863 } else {
2864 /*
2865 * Create a new update for the reference this
2866 * symref is pointing at. Also, we will record
2867 * and verify old_sha1 for this update as part
2868 * of processing the split-off update, so we
2869 * don't have to do it here.
2870 */
Michael Haggertyfcc42ea2016-09-04 18:08:35 +02002871 ret = split_symref_update(refs, update,
2872 referent.buf, transaction,
Michael Haggerty92b15512016-04-25 15:56:07 +02002873 affected_refnames, err);
2874 if (ret)
2875 return ret;
2876 }
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002877 } else {
2878 struct ref_update *parent_update;
Michael Haggerty8169d0d2016-04-25 17:38:35 +02002879
Michael Haggertye3f51032016-06-07 09:29:23 +02002880 if (check_old_oid(update, &lock->old_oid, err))
2881 return TRANSACTION_GENERIC_ERROR;
2882
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002883 /*
2884 * If this update is happening indirectly because of a
2885 * symref update, record the old SHA-1 in the parent
2886 * update:
2887 */
2888 for (parent_update = update->parent_update;
2889 parent_update;
2890 parent_update = parent_update->parent_update) {
David Turner7d618262016-09-04 18:08:43 +02002891 struct ref_lock *parent_lock = parent_update->backend_data;
2892 oidcpy(&parent_lock->old_oid, &lock->old_oid);
Michael Haggerty6e30b2f2016-04-25 17:48:32 +02002893 }
Michael Haggerty92b15512016-04-25 15:56:07 +02002894 }
2895
Michael Haggerty165056b2016-04-24 08:58:41 +02002896 if ((update->flags & REF_HAVE_NEW) &&
2897 !(update->flags & REF_DELETING) &&
2898 !(update->flags & REF_LOG_ONLY)) {
Michael Haggerty92b15512016-04-25 15:56:07 +02002899 if (!(update->type & REF_ISSYMREF) &&
2900 !hashcmp(lock->old_oid.hash, update->new_sha1)) {
Michael Haggerty165056b2016-04-24 08:58:41 +02002901 /*
2902 * The reference already has the desired
2903 * value, so we don't need to write it.
2904 */
Michael Haggerty92b15512016-04-25 15:56:07 +02002905 } else if (write_ref_to_lockfile(lock, update->new_sha1,
Michael Haggerty165056b2016-04-24 08:58:41 +02002906 err)) {
2907 char *write_err = strbuf_detach(err, NULL);
2908
2909 /*
2910 * The lock was freed upon failure of
2911 * write_ref_to_lockfile():
2912 */
David Turner7d618262016-09-04 18:08:43 +02002913 update->backend_data = NULL;
Michael Haggerty165056b2016-04-24 08:58:41 +02002914 strbuf_addf(err,
Michael Haggertye3f51032016-06-07 09:29:23 +02002915 "cannot update ref '%s': %s",
Michael Haggerty165056b2016-04-24 08:58:41 +02002916 update->refname, write_err);
2917 free(write_err);
2918 return TRANSACTION_GENERIC_ERROR;
2919 } else {
2920 update->flags |= REF_NEEDS_COMMIT;
2921 }
2922 }
2923 if (!(update->flags & REF_NEEDS_COMMIT)) {
2924 /*
2925 * We didn't call write_ref_to_lockfile(), so
2926 * the lockfile is still open. Close it to
2927 * free up the file descriptor:
2928 */
Michael Haggerty92b15512016-04-25 15:56:07 +02002929 if (close_ref(lock)) {
Michael Haggerty165056b2016-04-24 08:58:41 +02002930 strbuf_addf(err, "couldn't close '%s.lock'",
2931 update->refname);
2932 return TRANSACTION_GENERIC_ERROR;
2933 }
2934 }
2935 return 0;
2936}
2937
Ronnie Sahlberg127b42a2016-09-04 18:08:16 +02002938static int files_transaction_commit(struct ref_store *ref_store,
2939 struct ref_transaction *transaction,
2940 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002941{
Michael Haggerty00eebe32016-09-04 18:08:11 +02002942 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07002943 files_downcast(ref_store, REF_STORE_WRITE,
2944 "ref_transaction_commit");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002945 int ret = 0, i;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002946 struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
2947 struct string_list_item *ref_to_delete;
2948 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
Michael Haggerty92b15512016-04-25 15:56:07 +02002949 char *head_ref = NULL;
2950 int head_type;
2951 struct object_id head_oid;
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07002952 struct strbuf sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002953
2954 assert(err);
2955
2956 if (transaction->state != REF_TRANSACTION_OPEN)
2957 die("BUG: commit called for transaction that is not open");
2958
Michael Haggertyefe47282016-04-22 00:02:50 +02002959 if (!transaction->nr) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002960 transaction->state = REF_TRANSACTION_CLOSED;
2961 return 0;
2962 }
2963
Michael Haggerty92b15512016-04-25 15:56:07 +02002964 /*
2965 * Fail if a refname appears more than once in the
2966 * transaction. (If we end up splitting up any updates using
2967 * split_symref_update() or split_head_update(), those
2968 * functions will check that the new updates don't have the
2969 * same refname as any existing ones.)
2970 */
2971 for (i = 0; i < transaction->nr; i++) {
2972 struct ref_update *update = transaction->updates[i];
2973 struct string_list_item *item =
2974 string_list_append(&affected_refnames, update->refname);
2975
2976 /*
2977 * We store a pointer to update in item->util, but at
2978 * the moment we never use the value of this field
2979 * except to check whether it is non-NULL.
2980 */
2981 item->util = update;
2982 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01002983 string_list_sort(&affected_refnames);
2984 if (ref_update_reject_duplicates(&affected_refnames, err)) {
2985 ret = TRANSACTION_GENERIC_ERROR;
2986 goto cleanup;
2987 }
2988
2989 /*
Michael Haggerty92b15512016-04-25 15:56:07 +02002990 * Special hack: If a branch is updated directly and HEAD
2991 * points to it (may happen on the remote side of a push
2992 * for example) then logically the HEAD reflog should be
2993 * updated too.
2994 *
2995 * A generic solution would require reverse symref lookups,
2996 * but finding all symrefs pointing to a given branch would be
2997 * rather costly for this rare event (the direct update of a
2998 * branch) to be worth it. So let's cheat and check with HEAD
2999 * only, which should cover 99% of all usage scenarios (even
3000 * 100% of the default ones).
3001 *
3002 * So if HEAD is a symbolic reference, then record the name of
3003 * the reference that it points to. If we see an update of
3004 * head_ref within the transaction, then split_head_update()
3005 * arranges for the reflog of HEAD to be updated, too.
3006 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07003007 head_ref = refs_resolve_refdup(ref_store, "HEAD",
3008 RESOLVE_REF_NO_RECURSE,
3009 head_oid.hash, &head_type);
Michael Haggerty92b15512016-04-25 15:56:07 +02003010
3011 if (head_ref && !(head_type & REF_ISSYMREF)) {
3012 free(head_ref);
3013 head_ref = NULL;
3014 }
3015
3016 /*
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003017 * Acquire all locks, verify old values if provided, check
3018 * that new values are valid, and write new values to the
3019 * lockfiles, ready to be activated. Only keep one lockfile
3020 * open at a time to avoid running out of file descriptors.
3021 */
Michael Haggertyefe47282016-04-22 00:02:50 +02003022 for (i = 0; i < transaction->nr; i++) {
3023 struct ref_update *update = transaction->updates[i];
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003024
Michael Haggertyb3bbbc52016-09-04 18:08:33 +02003025 ret = lock_ref_for_update(refs, update, transaction,
3026 head_ref, &affected_refnames, err);
Michael Haggerty165056b2016-04-24 08:58:41 +02003027 if (ret)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003028 goto cleanup;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003029 }
3030
3031 /* Perform updates first so live commits remain referenced */
Michael Haggertyefe47282016-04-22 00:02:50 +02003032 for (i = 0; i < transaction->nr; i++) {
3033 struct ref_update *update = transaction->updates[i];
David Turner7d618262016-09-04 18:08:43 +02003034 struct ref_lock *lock = update->backend_data;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003035
David Turnerd99aa882016-02-24 17:58:50 -05003036 if (update->flags & REF_NEEDS_COMMIT ||
3037 update->flags & REF_LOG_ONLY) {
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003038 if (files_log_ref_write(refs,
3039 lock->ref_name,
Michael Haggerty81b1b6d2017-01-06 17:22:31 +01003040 lock->old_oid.hash,
3041 update->new_sha1,
3042 update->msg, update->flags,
3043 err)) {
Michael Haggerty92b15512016-04-25 15:56:07 +02003044 char *old_msg = strbuf_detach(err, NULL);
3045
3046 strbuf_addf(err, "cannot update the ref '%s': %s",
3047 lock->ref_name, old_msg);
3048 free(old_msg);
3049 unlock_ref(lock);
David Turner7d618262016-09-04 18:08:43 +02003050 update->backend_data = NULL;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003051 ret = TRANSACTION_GENERIC_ERROR;
3052 goto cleanup;
Michael Haggerty92b15512016-04-25 15:56:07 +02003053 }
3054 }
3055 if (update->flags & REF_NEEDS_COMMIT) {
Michael Haggerty00eebe32016-09-04 18:08:11 +02003056 clear_loose_ref_cache(refs);
Michael Haggerty92b15512016-04-25 15:56:07 +02003057 if (commit_ref(lock)) {
3058 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3059 unlock_ref(lock);
David Turner7d618262016-09-04 18:08:43 +02003060 update->backend_data = NULL;
Michael Haggerty92b15512016-04-25 15:56:07 +02003061 ret = TRANSACTION_GENERIC_ERROR;
3062 goto cleanup;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003063 }
3064 }
3065 }
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003066 /* Perform deletes now that updates are safely completed */
Michael Haggertyefe47282016-04-22 00:02:50 +02003067 for (i = 0; i < transaction->nr; i++) {
3068 struct ref_update *update = transaction->updates[i];
David Turner7d618262016-09-04 18:08:43 +02003069 struct ref_lock *lock = update->backend_data;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003070
David Turnerd99aa882016-02-24 17:58:50 -05003071 if (update->flags & REF_DELETING &&
3072 !(update->flags & REF_LOG_ONLY)) {
Michael Haggertyce0af242017-01-06 17:22:39 +01003073 if (!(update->type & REF_ISPACKED) ||
3074 update->type & REF_ISSYMREF) {
3075 /* It is a loose reference. */
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003076 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003077 files_ref_path(refs, &sb, lock->ref_name);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003078 if (unlink_or_msg(sb.buf, err)) {
Michael Haggertyce0af242017-01-06 17:22:39 +01003079 ret = TRANSACTION_GENERIC_ERROR;
3080 goto cleanup;
3081 }
Michael Haggerty44639772017-01-06 17:22:43 +01003082 update->flags |= REF_DELETED_LOOSE;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003083 }
3084
3085 if (!(update->flags & REF_ISPRUNING))
3086 string_list_append(&refs_to_delete,
David Turner7d618262016-09-04 18:08:43 +02003087 lock->ref_name);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003088 }
3089 }
3090
Michael Haggerty0a95ac52016-09-04 18:08:30 +02003091 if (repack_without_refs(refs, &refs_to_delete, err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003092 ret = TRANSACTION_GENERIC_ERROR;
3093 goto cleanup;
3094 }
Michael Haggerty44639772017-01-06 17:22:43 +01003095
3096 /* Delete the reflogs of any references that were deleted: */
3097 for_each_string_list_item(ref_to_delete, &refs_to_delete) {
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003098 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003099 files_reflog_path(refs, &sb, ref_to_delete->string);
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003100 if (!unlink_or_warn(sb.buf))
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003101 try_remove_empty_parents(refs, ref_to_delete->string,
Michael Haggerty44639772017-01-06 17:22:43 +01003102 REMOVE_EMPTY_PARENTS_REFLOG);
3103 }
3104
Michael Haggerty00eebe32016-09-04 18:08:11 +02003105 clear_loose_ref_cache(refs);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003106
3107cleanup:
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003108 strbuf_release(&sb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003109 transaction->state = REF_TRANSACTION_CLOSED;
3110
Michael Haggerty44639772017-01-06 17:22:43 +01003111 for (i = 0; i < transaction->nr; i++) {
3112 struct ref_update *update = transaction->updates[i];
3113 struct ref_lock *lock = update->backend_data;
3114
3115 if (lock)
3116 unlock_ref(lock);
3117
3118 if (update->flags & REF_DELETED_LOOSE) {
3119 /*
3120 * The loose reference was deleted. Delete any
3121 * empty parent directories. (Note that this
3122 * can only work because we have already
3123 * removed the lockfile.)
3124 */
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003125 try_remove_empty_parents(refs, update->refname,
Michael Haggerty44639772017-01-06 17:22:43 +01003126 REMOVE_EMPTY_PARENTS_REF);
3127 }
3128 }
3129
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003130 string_list_clear(&refs_to_delete, 0);
Michael Haggerty92b15512016-04-25 15:56:07 +02003131 free(head_ref);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003132 string_list_clear(&affected_refnames, 0);
Michael Haggerty92b15512016-04-25 15:56:07 +02003133
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003134 return ret;
3135}
3136
3137static int ref_present(const char *refname,
3138 const struct object_id *oid, int flags, void *cb_data)
3139{
3140 struct string_list *affected_refnames = cb_data;
3141
3142 return string_list_has_string(affected_refnames, refname);
3143}
3144
David Turnerfc681462016-09-04 18:08:39 +02003145static int files_initial_transaction_commit(struct ref_store *ref_store,
3146 struct ref_transaction *transaction,
3147 struct strbuf *err)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003148{
Michael Haggertyd99825a2016-09-04 18:08:12 +02003149 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07003150 files_downcast(ref_store, REF_STORE_WRITE,
3151 "initial_ref_transaction_commit");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003152 int ret = 0, i;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003153 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
3154
3155 assert(err);
3156
3157 if (transaction->state != REF_TRANSACTION_OPEN)
3158 die("BUG: commit called for transaction that is not open");
3159
3160 /* Fail if a refname appears more than once in the transaction: */
Michael Haggertyefe47282016-04-22 00:02:50 +02003161 for (i = 0; i < transaction->nr; i++)
3162 string_list_append(&affected_refnames,
3163 transaction->updates[i]->refname);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003164 string_list_sort(&affected_refnames);
3165 if (ref_update_reject_duplicates(&affected_refnames, err)) {
3166 ret = TRANSACTION_GENERIC_ERROR;
3167 goto cleanup;
3168 }
3169
3170 /*
3171 * It's really undefined to call this function in an active
3172 * repository or when there are existing references: we are
3173 * only locking and changing packed-refs, so (1) any
3174 * simultaneous processes might try to change a reference at
3175 * the same time we do, and (2) any existing loose versions of
3176 * the references that we are setting would have precedence
3177 * over our values. But some remote helpers create the remote
3178 * "HEAD" and "master" branches before calling this function,
3179 * so here we really only check that none of the references
3180 * that we are creating already exists.
3181 */
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07003182 if (refs_for_each_rawref(&refs->base, ref_present,
3183 &affected_refnames))
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003184 die("BUG: initial ref transaction called with existing refs");
3185
Michael Haggertyefe47282016-04-22 00:02:50 +02003186 for (i = 0; i < transaction->nr; i++) {
3187 struct ref_update *update = transaction->updates[i];
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003188
3189 if ((update->flags & REF_HAVE_OLD) &&
3190 !is_null_sha1(update->old_sha1))
3191 die("BUG: initial ref transaction with old_sha1 set");
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +07003192 if (refs_verify_refname_available(&refs->base, update->refname,
3193 &affected_refnames, NULL,
3194 err)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003195 ret = TRANSACTION_NAME_CONFLICT;
3196 goto cleanup;
3197 }
3198 }
3199
Michael Haggerty49c0df62016-09-04 18:08:15 +02003200 if (lock_packed_refs(refs, 0)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003201 strbuf_addf(err, "unable to lock packed-refs file: %s",
3202 strerror(errno));
3203 ret = TRANSACTION_GENERIC_ERROR;
3204 goto cleanup;
3205 }
3206
Michael Haggertyefe47282016-04-22 00:02:50 +02003207 for (i = 0; i < transaction->nr; i++) {
3208 struct ref_update *update = transaction->updates[i];
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003209
3210 if ((update->flags & REF_HAVE_NEW) &&
3211 !is_null_sha1(update->new_sha1))
Michael Haggertyd99825a2016-09-04 18:08:12 +02003212 add_packed_ref(refs, update->refname, update->new_sha1);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003213 }
3214
Michael Haggerty49c0df62016-09-04 18:08:15 +02003215 if (commit_packed_refs(refs)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003216 strbuf_addf(err, "unable to commit packed-refs file: %s",
3217 strerror(errno));
3218 ret = TRANSACTION_GENERIC_ERROR;
3219 goto cleanup;
3220 }
3221
3222cleanup:
3223 transaction->state = REF_TRANSACTION_CLOSED;
3224 string_list_clear(&affected_refnames, 0);
3225 return ret;
3226}
3227
3228struct expire_reflog_cb {
3229 unsigned int flags;
3230 reflog_expiry_should_prune_fn *should_prune_fn;
3231 void *policy_cb;
3232 FILE *newlog;
brian m. carlson9461d272017-02-21 23:47:32 +00003233 struct object_id last_kept_oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003234};
3235
brian m. carlson9461d272017-02-21 23:47:32 +00003236static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003237 const char *email, unsigned long timestamp, int tz,
3238 const char *message, void *cb_data)
3239{
3240 struct expire_reflog_cb *cb = cb_data;
3241 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
3242
3243 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
brian m. carlson9461d272017-02-21 23:47:32 +00003244 ooid = &cb->last_kept_oid;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003245
brian m. carlson9461d272017-02-21 23:47:32 +00003246 if ((*cb->should_prune_fn)(ooid->hash, noid->hash, email, timestamp, tz,
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003247 message, policy_cb)) {
3248 if (!cb->newlog)
3249 printf("would prune %s", message);
3250 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3251 printf("prune %s", message);
3252 } else {
3253 if (cb->newlog) {
3254 fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s",
brian m. carlson9461d272017-02-21 23:47:32 +00003255 oid_to_hex(ooid), oid_to_hex(noid),
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003256 email, timestamp, tz, message);
brian m. carlson9461d272017-02-21 23:47:32 +00003257 oidcpy(&cb->last_kept_oid, noid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003258 }
3259 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3260 printf("keep %s", message);
3261 }
3262 return 0;
3263}
3264
David Turnere3688bd2016-09-04 18:08:38 +02003265static int files_reflog_expire(struct ref_store *ref_store,
3266 const char *refname, const unsigned char *sha1,
3267 unsigned int flags,
3268 reflog_expiry_prepare_fn prepare_fn,
3269 reflog_expiry_should_prune_fn should_prune_fn,
3270 reflog_expiry_cleanup_fn cleanup_fn,
3271 void *policy_cb_data)
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003272{
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02003273 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07003274 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003275 static struct lock_file reflog_lock;
3276 struct expire_reflog_cb cb;
3277 struct ref_lock *lock;
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003278 struct strbuf log_file_sb = STRBUF_INIT;
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003279 char *log_file;
3280 int status = 0;
3281 int type;
3282 struct strbuf err = STRBUF_INIT;
3283
3284 memset(&cb, 0, sizeof(cb));
3285 cb.flags = flags;
3286 cb.policy_cb = policy_cb_data;
3287 cb.should_prune_fn = should_prune_fn;
3288
3289 /*
3290 * The reflog file is locked by holding the lock on the
3291 * reference itself, plus we might need to update the
3292 * reference if --updateref was specified:
3293 */
Michael Haggerty7eb27cd2016-09-04 18:08:34 +02003294 lock = lock_ref_sha1_basic(refs, refname, sha1,
3295 NULL, NULL, REF_NODEREF,
David Turner41d796e2016-04-07 15:03:11 -04003296 &type, &err);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003297 if (!lock) {
3298 error("cannot lock ref '%s': %s", refname, err.buf);
3299 strbuf_release(&err);
3300 return -1;
3301 }
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07003302 if (!refs_reflog_exists(ref_store, refname)) {
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003303 unlock_ref(lock);
3304 return 0;
3305 }
3306
Nguyễn Thái Ngọc Duy802de3d2017-03-26 09:42:22 +07003307 files_reflog_path(refs, &log_file_sb, refname);
3308 log_file = strbuf_detach(&log_file_sb, NULL);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003309 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3310 /*
3311 * Even though holding $GIT_DIR/logs/$reflog.lock has
3312 * no locking implications, we use the lock_file
3313 * machinery here anyway because it does a lot of the
3314 * work we need, including cleaning up if the program
3315 * exits unexpectedly.
3316 */
3317 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3318 struct strbuf err = STRBUF_INIT;
3319 unable_to_lock_message(log_file, errno, &err);
3320 error("%s", err.buf);
3321 strbuf_release(&err);
3322 goto failure;
3323 }
3324 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3325 if (!cb.newlog) {
3326 error("cannot fdopen %s (%s)",
3327 get_lock_file_path(&reflog_lock), strerror(errno));
3328 goto failure;
3329 }
3330 }
3331
3332 (*prepare_fn)(refname, sha1, cb.policy_cb);
Nguyễn Thái Ngọc Duy2f40e952017-03-26 09:42:36 +07003333 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003334 (*cleanup_fn)(cb.policy_cb);
3335
3336 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3337 /*
3338 * It doesn't make sense to adjust a reference pointed
3339 * to by a symbolic ref based on expiring entries in
3340 * the symbolic reference's reflog. Nor can we update
3341 * a reference if there are no remaining reflog
3342 * entries.
3343 */
3344 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3345 !(type & REF_ISSYMREF) &&
brian m. carlson9461d272017-02-21 23:47:32 +00003346 !is_null_oid(&cb.last_kept_oid);
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003347
3348 if (close_lock_file(&reflog_lock)) {
3349 status |= error("couldn't write %s: %s", log_file,
3350 strerror(errno));
3351 } else if (update &&
3352 (write_in_full(get_lock_file_fd(lock->lk),
brian m. carlson9461d272017-02-21 23:47:32 +00003353 oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003354 write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
3355 close_ref(lock) < 0)) {
3356 status |= error("couldn't write %s",
3357 get_lock_file_path(lock->lk));
3358 rollback_lock_file(&reflog_lock);
3359 } else if (commit_lock_file(&reflog_lock)) {
Junio C Hamanoe0048d32015-12-11 10:40:54 -08003360 status |= error("unable to write reflog '%s' (%s)",
Michael Haggerty7bd9bcf2015-11-09 14:34:01 +01003361 log_file, strerror(errno));
3362 } else if (update && commit_ref(lock)) {
3363 status |= error("couldn't set %s", lock->ref_name);
3364 }
3365 }
3366 free(log_file);
3367 unlock_ref(lock);
3368 return status;
3369
3370 failure:
3371 rollback_lock_file(&reflog_lock);
3372 free(log_file);
3373 unlock_ref(lock);
3374 return -1;
3375}
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +02003376
David Turner6fb5acf2016-09-04 18:08:41 +02003377static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
3378{
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003379 struct files_ref_store *refs =
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +07003380 files_downcast(ref_store, REF_STORE_WRITE, "init_db");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003381 struct strbuf sb = STRBUF_INIT;
3382
David Turner6fb5acf2016-09-04 18:08:41 +02003383 /*
3384 * Create .git/refs/{heads,tags}
3385 */
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003386 files_ref_path(refs, &sb, "refs/heads");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003387 safe_create_dir(sb.buf, 1);
3388
3389 strbuf_reset(&sb);
Nguyễn Thái Ngọc Duy19e02f42017-03-26 09:42:23 +07003390 files_ref_path(refs, &sb, "refs/tags");
Nguyễn Thái Ngọc Duye9dcc302017-03-26 09:42:20 +07003391 safe_create_dir(sb.buf, 1);
3392
3393 strbuf_release(&sb);
David Turner6fb5acf2016-09-04 18:08:41 +02003394 return 0;
3395}
3396
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +02003397struct ref_storage_be refs_be_files = {
3398 NULL,
Michael Haggerty00eebe32016-09-04 18:08:11 +02003399 "files",
Ronnie Sahlberg127b42a2016-09-04 18:08:16 +02003400 files_ref_store_create,
David Turner6fb5acf2016-09-04 18:08:41 +02003401 files_init_db,
Michael Haggertye1e33b72016-09-04 18:08:25 +02003402 files_transaction_commit,
David Turnerfc681462016-09-04 18:08:39 +02003403 files_initial_transaction_commit,
Michael Haggertye1e33b72016-09-04 18:08:25 +02003404
Michael Haggerty82315272016-09-04 18:08:27 +02003405 files_pack_refs,
Michael Haggertybd427cf2016-09-04 18:08:29 +02003406 files_peel_ref,
Michael Haggerty284689b2016-09-04 18:08:28 +02003407 files_create_symref,
David Turnera27dcf82016-09-04 18:08:40 +02003408 files_delete_refs,
David Turner9b6b40d2016-09-04 18:08:42 +02003409 files_rename_ref,
Michael Haggerty82315272016-09-04 18:08:27 +02003410
Michael Haggerty1a769002016-09-04 18:08:37 +02003411 files_ref_iterator_begin,
Michael Haggerty62665822016-09-04 18:08:26 +02003412 files_read_raw_ref,
David Turnere3688bd2016-09-04 18:08:38 +02003413
3414 files_reflog_iterator_begin,
3415 files_for_each_reflog_ent,
3416 files_for_each_reflog_ent_reverse,
3417 files_reflog_exists,
3418 files_create_reflog,
3419 files_delete_reflog,
3420 files_reflog_expire
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +02003421};