Coverage Report

Created: 2025-12-14 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/refs/files-backend.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
#define DISABLE_SIGN_COMPARE_WARNINGS
3
4
#include "../git-compat-util.h"
5
#include "../abspath.h"
6
#include "../config.h"
7
#include "../copy.h"
8
#include "../environment.h"
9
#include "../gettext.h"
10
#include "../hash.h"
11
#include "../hex.h"
12
#include "../fsck.h"
13
#include "../refs.h"
14
#include "../repo-settings.h"
15
#include "refs-internal.h"
16
#include "ref-cache.h"
17
#include "packed-backend.h"
18
#include "../ident.h"
19
#include "../iterator.h"
20
#include "../dir-iterator.h"
21
#include "../lockfile.h"
22
#include "../object.h"
23
#include "../path.h"
24
#include "../dir.h"
25
#include "../chdir-notify.h"
26
#include "../setup.h"
27
#include "../worktree.h"
28
#include "../wrapper.h"
29
#include "../write-or-die.h"
30
#include "../revision.h"
31
#include <wildmatch.h>
32
33
/*
34
 * This backend uses the following flags in `ref_update::flags` for
35
 * internal bookkeeping purposes. Their numerical values must not
36
 * conflict with REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, REF_HAVE_NEW,
37
 * or REF_HAVE_OLD, which are also stored in `ref_update::flags`.
38
 */
39
40
/*
41
 * Used as a flag in ref_update::flags when a loose ref is being
42
 * pruned. This flag must only be used when REF_NO_DEREF is set.
43
 */
44
0
#define REF_IS_PRUNING (1 << 4)
45
46
/*
47
 * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
48
 * refs (i.e., because the reference is about to be deleted anyway).
49
 */
50
0
#define REF_DELETING (1 << 5)
51
52
/*
53
 * Used as a flag in ref_update::flags when the lockfile needs to be
54
 * committed.
55
 */
56
0
#define REF_NEEDS_COMMIT (1 << 6)
57
58
/*
59
 * Used as a flag in ref_update::flags when the ref_update was via an
60
 * update to HEAD.
61
 */
62
0
#define REF_UPDATE_VIA_HEAD (1 << 8)
63
64
/*
65
 * Used as a flag in ref_update::flags when a reference has been
66
 * deleted and the ref's parent directories may need cleanup.
67
 */
68
0
#define REF_DELETED_RMDIR (1 << 9)
69
70
/*
71
 * Used to indicate that the reflog-only update has been created via
72
 * `split_head_update()`.
73
 */
74
0
#define REF_LOG_VIA_SPLIT (1 << 14)
75
76
struct ref_lock {
77
  char *ref_name;
78
  struct lock_file lk;
79
  struct object_id old_oid;
80
  unsigned int count; /* track users of the lock (ref update + reflog updates) */
81
};
82
83
struct files_ref_store {
84
  struct ref_store base;
85
  unsigned int store_flags;
86
87
  char *gitcommondir;
88
  enum log_refs_config log_all_ref_updates;
89
  int prefer_symlink_refs;
90
91
  struct ref_cache *loose;
92
93
  struct ref_store *packed_ref_store;
94
};
95
96
static void clear_loose_ref_cache(struct files_ref_store *refs)
97
0
{
98
0
  if (refs->loose) {
99
0
    free_ref_cache(refs->loose);
100
0
    refs->loose = NULL;
101
0
  }
102
0
}
103
104
/*
105
 * Create a new submodule ref cache and add it to the internal
106
 * set of caches.
107
 */
108
static struct ref_store *files_ref_store_init(struct repository *repo,
109
                const char *gitdir,
110
                unsigned int flags)
111
0
{
112
0
  struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
113
0
  struct ref_store *ref_store = (struct ref_store *)refs;
114
0
  struct strbuf sb = STRBUF_INIT;
115
116
0
  base_ref_store_init(ref_store, repo, gitdir, &refs_be_files);
117
0
  refs->store_flags = flags;
118
0
  get_common_dir_noenv(&sb, gitdir);
119
0
  refs->gitcommondir = strbuf_detach(&sb, NULL);
120
0
  refs->packed_ref_store =
121
0
    packed_ref_store_init(repo, refs->gitcommondir, flags);
122
0
  refs->log_all_ref_updates = repo_settings_get_log_all_ref_updates(repo);
123
0
  repo_config_get_bool(repo, "core.prefersymlinkrefs", &refs->prefer_symlink_refs);
124
125
0
  chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
126
0
  chdir_notify_reparent("files-backend $GIT_COMMONDIR",
127
0
            &refs->gitcommondir);
128
129
0
  return ref_store;
130
0
}
131
132
/*
133
 * Die if refs is not the main ref store. caller is used in any
134
 * necessary error messages.
135
 */
136
static void files_assert_main_repository(struct files_ref_store *refs,
137
           const char *caller)
138
0
{
139
0
  if (refs->store_flags & REF_STORE_MAIN)
140
0
    return;
141
142
0
  BUG("operation %s only allowed for main ref store", caller);
143
0
}
144
145
/*
146
 * Downcast ref_store to files_ref_store. Die if ref_store is not a
147
 * files_ref_store. required_flags is compared with ref_store's
148
 * store_flags to ensure the ref_store has all required capabilities.
149
 * "caller" is used in any necessary error messages.
150
 */
151
static struct files_ref_store *files_downcast(struct ref_store *ref_store,
152
                unsigned int required_flags,
153
                const char *caller)
154
0
{
155
0
  struct files_ref_store *refs;
156
157
0
  if (ref_store->be != &refs_be_files)
158
0
    BUG("ref_store is type \"%s\" not \"files\" in %s",
159
0
        ref_store->be->name, caller);
160
161
0
  refs = (struct files_ref_store *)ref_store;
162
163
0
  if ((refs->store_flags & required_flags) != required_flags)
164
0
    BUG("operation %s requires abilities 0x%x, but only have 0x%x",
165
0
        caller, required_flags, refs->store_flags);
166
167
0
  return refs;
168
0
}
169
170
static void files_ref_store_release(struct ref_store *ref_store)
171
0
{
172
0
  struct files_ref_store *refs = files_downcast(ref_store, 0, "release");
173
0
  free_ref_cache(refs->loose);
174
0
  free(refs->gitcommondir);
175
0
  ref_store_release(refs->packed_ref_store);
176
0
  free(refs->packed_ref_store);
177
0
}
178
179
static void files_reflog_path(struct files_ref_store *refs,
180
            struct strbuf *sb,
181
            const char *refname)
182
0
{
183
0
  const char *bare_refname;
184
0
  const char *wtname;
185
0
  int wtname_len;
186
0
  enum ref_worktree_type wt_type = parse_worktree_ref(
187
0
    refname, &wtname, &wtname_len, &bare_refname);
188
189
0
  switch (wt_type) {
190
0
  case REF_WORKTREE_CURRENT:
191
0
    strbuf_addf(sb, "%s/logs/%s", refs->base.gitdir, refname);
192
0
    break;
193
0
  case REF_WORKTREE_SHARED:
194
0
  case REF_WORKTREE_MAIN:
195
0
    strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, bare_refname);
196
0
    break;
197
0
  case REF_WORKTREE_OTHER:
198
0
    strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir,
199
0
          wtname_len, wtname, bare_refname);
200
0
    break;
201
0
  default:
202
0
    BUG("unknown ref type %d of ref %s", wt_type, refname);
203
0
  }
204
0
}
205
206
static void files_ref_path(struct files_ref_store *refs,
207
         struct strbuf *sb,
208
         const char *refname)
209
0
{
210
0
  const char *bare_refname;
211
0
  const char *wtname;
212
0
  int wtname_len;
213
0
  enum ref_worktree_type wt_type = parse_worktree_ref(
214
0
    refname, &wtname, &wtname_len, &bare_refname);
215
0
  switch (wt_type) {
216
0
  case REF_WORKTREE_CURRENT:
217
0
    strbuf_addf(sb, "%s/%s", refs->base.gitdir, refname);
218
0
    break;
219
0
  case REF_WORKTREE_OTHER:
220
0
    strbuf_addf(sb, "%s/worktrees/%.*s/%s", refs->gitcommondir,
221
0
          wtname_len, wtname, bare_refname);
222
0
    break;
223
0
  case REF_WORKTREE_SHARED:
224
0
  case REF_WORKTREE_MAIN:
225
0
    strbuf_addf(sb, "%s/%s", refs->gitcommondir, bare_refname);
226
0
    break;
227
0
  default:
228
0
    BUG("unknown ref type %d of ref %s", wt_type, refname);
229
0
  }
230
0
}
231
232
/*
233
 * Manually add refs/bisect, refs/rewritten and refs/worktree, which, being
234
 * per-worktree, might not appear in the directory listing for
235
 * refs/ in the main repo.
236
 */
237
static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname)
238
0
{
239
0
  const char *prefixes[] = { "refs/bisect/", "refs/worktree/", "refs/rewritten/" };
240
0
  int ip;
241
242
0
  if (strcmp(dirname, "refs/"))
243
0
    return;
244
245
0
  for (ip = 0; ip < ARRAY_SIZE(prefixes); ip++) {
246
0
    const char *prefix = prefixes[ip];
247
0
    int prefix_len = strlen(prefix);
248
0
    struct ref_entry *child_entry;
249
0
    int pos;
250
251
0
    pos = search_ref_dir(dir, prefix, prefix_len);
252
0
    if (pos >= 0)
253
0
      continue;
254
0
    child_entry = create_dir_entry(dir->cache, prefix, prefix_len);
255
0
    add_entry_to_dir(dir, child_entry);
256
0
  }
257
0
}
258
259
static void loose_fill_ref_dir_regular_file(struct files_ref_store *refs,
260
              const char *refname,
261
              struct ref_dir *dir)
262
0
{
263
0
  struct object_id oid;
264
0
  int flag;
265
0
  const char *referent = refs_resolve_ref_unsafe(&refs->base,
266
0
                   refname,
267
0
                   RESOLVE_REF_READING,
268
0
                   &oid, &flag);
269
270
0
  if (!referent) {
271
0
    oidclr(&oid, refs->base.repo->hash_algo);
272
0
    flag |= REF_ISBROKEN;
273
0
  } else if (is_null_oid(&oid)) {
274
    /*
275
     * It is so astronomically unlikely
276
     * that null_oid is the OID of an
277
     * actual object that we consider its
278
     * appearance in a loose reference
279
     * file to be repo corruption
280
     * (probably due to a software bug).
281
     */
282
0
    flag |= REF_ISBROKEN;
283
0
  }
284
285
0
  if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
286
0
    if (!refname_is_safe(refname))
287
0
      die("loose refname is dangerous: %s", refname);
288
0
    oidclr(&oid, refs->base.repo->hash_algo);
289
0
    flag |= REF_BAD_NAME | REF_ISBROKEN;
290
0
  }
291
292
0
  if (!(flag & REF_ISSYMREF))
293
0
    referent = NULL;
294
295
0
  add_entry_to_dir(dir, create_ref_entry(refname, referent, &oid, flag));
296
0
}
297
298
/*
299
 * Read the loose references from the namespace dirname into dir
300
 * (without recursing).  dirname must end with '/'.  dir must be the
301
 * directory entry corresponding to dirname.
302
 */
303
static void loose_fill_ref_dir(struct ref_store *ref_store,
304
             struct ref_dir *dir, const char *dirname)
305
0
{
306
0
  struct files_ref_store *refs =
307
0
    files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
308
0
  DIR *d;
309
0
  struct dirent *de;
310
0
  int dirnamelen = strlen(dirname);
311
0
  struct strbuf refname;
312
0
  struct strbuf path = STRBUF_INIT;
313
314
0
  files_ref_path(refs, &path, dirname);
315
316
0
  d = opendir(path.buf);
317
0
  if (!d) {
318
0
    strbuf_release(&path);
319
0
    return;
320
0
  }
321
322
0
  strbuf_init(&refname, dirnamelen + 257);
323
0
  strbuf_add(&refname, dirname, dirnamelen);
324
325
0
  while ((de = readdir(d)) != NULL) {
326
0
    unsigned char dtype;
327
328
0
    if (de->d_name[0] == '.')
329
0
      continue;
330
0
    if (ends_with(de->d_name, ".lock"))
331
0
      continue;
332
0
    strbuf_addstr(&refname, de->d_name);
333
334
0
    dtype = get_dtype(de, &path, 1);
335
0
    if (dtype == DT_DIR) {
336
0
      strbuf_addch(&refname, '/');
337
0
      add_entry_to_dir(dir,
338
0
           create_dir_entry(dir->cache, refname.buf,
339
0
                refname.len));
340
0
    } else if (dtype == DT_REG) {
341
0
      loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
342
0
    }
343
0
    strbuf_setlen(&refname, dirnamelen);
344
0
  }
345
0
  strbuf_release(&refname);
346
0
  strbuf_release(&path);
347
0
  closedir(d);
348
349
0
  add_per_worktree_entries_to_dir(dir, dirname);
350
0
}
351
352
static int for_each_root_ref(struct files_ref_store *refs,
353
           int (*cb)(const char *refname, void *cb_data),
354
           void *cb_data)
355
0
{
356
0
  struct strbuf path = STRBUF_INIT, refname = STRBUF_INIT;
357
0
  const char *dirname = refs->loose->root->name;
358
0
  struct dirent *de;
359
0
  size_t dirnamelen;
360
0
  int ret;
361
0
  DIR *d;
362
363
0
  files_ref_path(refs, &path, dirname);
364
365
0
  d = opendir(path.buf);
366
0
  if (!d) {
367
0
    strbuf_release(&path);
368
0
    return -1;
369
0
  }
370
371
0
  strbuf_addstr(&refname, dirname);
372
0
  dirnamelen = refname.len;
373
374
0
  while ((de = readdir(d)) != NULL) {
375
0
    unsigned char dtype;
376
377
0
    if (de->d_name[0] == '.')
378
0
      continue;
379
0
    if (ends_with(de->d_name, ".lock"))
380
0
      continue;
381
0
    strbuf_addstr(&refname, de->d_name);
382
383
0
    dtype = get_dtype(de, &path, 1);
384
0
    if (dtype == DT_REG && is_root_ref(de->d_name)) {
385
0
      ret = cb(refname.buf, cb_data);
386
0
      if (ret)
387
0
        goto done;
388
0
    }
389
390
0
    strbuf_setlen(&refname, dirnamelen);
391
0
  }
392
393
0
  ret = 0;
394
395
0
done:
396
0
  strbuf_release(&refname);
397
0
  strbuf_release(&path);
398
0
  closedir(d);
399
0
  return ret;
400
0
}
401
402
struct fill_root_ref_data {
403
  struct files_ref_store *refs;
404
  struct ref_dir *dir;
405
};
406
407
static int fill_root_ref(const char *refname, void *cb_data)
408
0
{
409
0
  struct fill_root_ref_data *data = cb_data;
410
0
  loose_fill_ref_dir_regular_file(data->refs, refname, data->dir);
411
0
  return 0;
412
0
}
413
414
/*
415
 * Add root refs to the ref dir by parsing the directory for any files which
416
 * follow the root ref syntax.
417
 */
418
static void add_root_refs(struct files_ref_store *refs,
419
        struct ref_dir *dir)
420
0
{
421
0
  struct fill_root_ref_data data = {
422
0
    .refs = refs,
423
0
    .dir = dir,
424
0
  };
425
426
0
  for_each_root_ref(refs, fill_root_ref, &data);
427
0
}
428
429
static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
430
               unsigned int flags)
431
0
{
432
0
  if (!refs->loose) {
433
0
    struct ref_dir *dir;
434
435
    /*
436
     * Mark the top-level directory complete because we
437
     * are about to read the only subdirectory that can
438
     * hold references:
439
     */
440
0
    refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
441
442
    /* We're going to fill the top level ourselves: */
443
0
    refs->loose->root->flag &= ~REF_INCOMPLETE;
444
445
0
    dir = get_ref_dir(refs->loose->root);
446
447
0
    if (flags & DO_FOR_EACH_INCLUDE_ROOT_REFS)
448
0
      add_root_refs(refs, dir);
449
450
    /*
451
     * Add an incomplete entry for "refs/" (to be filled
452
     * lazily):
453
     */
454
0
    add_entry_to_dir(dir, create_dir_entry(refs->loose, "refs/", 5));
455
0
  }
456
0
  return refs->loose;
457
0
}
458
459
static int read_ref_internal(struct ref_store *ref_store, const char *refname,
460
           struct object_id *oid, struct strbuf *referent,
461
           unsigned int *type, int *failure_errno, int skip_packed_refs)
462
0
{
463
0
  struct files_ref_store *refs =
464
0
    files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
465
0
  struct strbuf sb_contents = STRBUF_INIT;
466
0
  struct strbuf sb_path = STRBUF_INIT;
467
0
  const char *path;
468
0
  const char *buf;
469
0
  struct stat st;
470
0
  int fd;
471
0
  int ret = -1;
472
0
  int remaining_retries = 3;
473
0
  int myerr = 0;
474
475
0
  *type = 0;
476
0
  strbuf_reset(&sb_path);
477
478
0
  files_ref_path(refs, &sb_path, refname);
479
480
0
  path = sb_path.buf;
481
482
0
stat_ref:
483
  /*
484
   * We might have to loop back here to avoid a race
485
   * condition: first we lstat() the file, then we try
486
   * to read it as a link or as a file.  But if somebody
487
   * changes the type of the file (file <-> directory
488
   * <-> symlink) between the lstat() and reading, then
489
   * we don't want to report that as an error but rather
490
   * try again starting with the lstat().
491
   *
492
   * We'll keep a count of the retries, though, just to avoid
493
   * any confusing situation sending us into an infinite loop.
494
   */
495
496
0
  if (remaining_retries-- <= 0)
497
0
    goto out;
498
499
0
  if (lstat(path, &st) < 0) {
500
0
    int ignore_errno;
501
0
    myerr = errno;
502
0
    if (myerr != ENOENT || skip_packed_refs)
503
0
      goto out;
504
0
    if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
505
0
              referent, type, &ignore_errno)) {
506
0
      myerr = ENOENT;
507
0
      goto out;
508
0
    }
509
0
    ret = 0;
510
0
    goto out;
511
0
  }
512
513
  /* Follow "normalized" - ie "refs/.." symlinks by hand */
514
0
  if (S_ISLNK(st.st_mode)) {
515
0
    strbuf_reset(&sb_contents);
516
0
    if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) {
517
0
      myerr = errno;
518
0
      if (myerr == ENOENT || myerr == EINVAL)
519
        /* inconsistent with lstat; retry */
520
0
        goto stat_ref;
521
0
      else
522
0
        goto out;
523
0
    }
524
0
    if (starts_with(sb_contents.buf, "refs/") &&
525
0
        !check_refname_format(sb_contents.buf, 0)) {
526
0
      strbuf_swap(&sb_contents, referent);
527
0
      *type |= REF_ISSYMREF;
528
0
      ret = 0;
529
0
      goto out;
530
0
    }
531
    /*
532
     * It doesn't look like a refname; fall through to just
533
     * treating it like a non-symlink, and reading whatever it
534
     * points to.
535
     */
536
0
  }
537
538
  /* Is it a directory? */
539
0
  if (S_ISDIR(st.st_mode)) {
540
0
    int ignore_errno;
541
    /*
542
     * Even though there is a directory where the loose
543
     * ref is supposed to be, there could still be a
544
     * packed ref:
545
     */
546
0
    if (skip_packed_refs ||
547
0
        refs_read_raw_ref(refs->packed_ref_store, refname, oid,
548
0
              referent, type, &ignore_errno)) {
549
0
      myerr = EISDIR;
550
0
      goto out;
551
0
    }
552
0
    ret = 0;
553
0
    goto out;
554
0
  }
555
556
  /*
557
   * Anything else, just open it and try to use it as
558
   * a ref
559
   */
560
0
  fd = open(path, O_RDONLY);
561
0
  if (fd < 0) {
562
0
    myerr = errno;
563
0
    if (myerr == ENOENT && !S_ISLNK(st.st_mode))
564
      /* inconsistent with lstat; retry */
565
0
      goto stat_ref;
566
0
    else
567
0
      goto out;
568
0
  }
569
0
  strbuf_reset(&sb_contents);
570
0
  if (strbuf_read(&sb_contents, fd, 256) < 0) {
571
0
    myerr = errno;
572
0
    close(fd);
573
0
    goto out;
574
0
  }
575
0
  close(fd);
576
0
  strbuf_rtrim(&sb_contents);
577
0
  buf = sb_contents.buf;
578
579
0
  ret = parse_loose_ref_contents(ref_store->repo->hash_algo, buf,
580
0
               oid, referent, type, NULL, &myerr);
581
582
0
out:
583
0
  if (ret && !myerr)
584
0
    BUG("returning non-zero %d, should have set myerr!", ret);
585
0
  *failure_errno = myerr;
586
587
0
  strbuf_release(&sb_path);
588
0
  strbuf_release(&sb_contents);
589
0
  errno = 0;
590
0
  return ret;
591
0
}
592
593
static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
594
            struct object_id *oid, struct strbuf *referent,
595
            unsigned int *type, int *failure_errno)
596
0
{
597
0
  return read_ref_internal(ref_store, refname, oid, referent, type, failure_errno, 0);
598
0
}
599
600
static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
601
           struct strbuf *referent)
602
0
{
603
0
  struct object_id oid;
604
0
  int failure_errno, ret;
605
0
  unsigned int type;
606
607
0
  ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
608
0
  if (!ret && !(type & REF_ISSYMREF))
609
0
    return NOT_A_SYMREF;
610
0
  return ret;
611
0
}
612
613
int parse_loose_ref_contents(const struct git_hash_algo *algop,
614
           const char *buf, struct object_id *oid,
615
           struct strbuf *referent, unsigned int *type,
616
           const char **trailing, int *failure_errno)
617
0
{
618
0
  const char *p;
619
0
  if (skip_prefix(buf, "ref:", &buf)) {
620
0
    while (isspace(*buf))
621
0
      buf++;
622
623
0
    strbuf_reset(referent);
624
0
    strbuf_addstr(referent, buf);
625
0
    *type |= REF_ISSYMREF;
626
0
    return 0;
627
0
  }
628
629
  /*
630
   * FETCH_HEAD has additional data after the sha.
631
   */
632
0
  if (parse_oid_hex_algop(buf, oid, &p, algop) ||
633
0
      (*p != '\0' && !isspace(*p))) {
634
0
    *type |= REF_ISBROKEN;
635
0
    *failure_errno = EINVAL;
636
0
    return -1;
637
0
  }
638
639
0
  if (trailing)
640
0
    *trailing = p;
641
642
0
  return 0;
643
0
}
644
645
static void unlock_ref(struct ref_lock *lock)
646
0
{
647
0
  lock->count--;
648
0
  if (!lock->count) {
649
0
    rollback_lock_file(&lock->lk);
650
0
    free(lock->ref_name);
651
0
    free(lock);
652
0
  }
653
0
}
654
655
/*
656
 * Check if the transaction has another update with a case-insensitive refname
657
 * match.
658
 *
659
 * If the update is part of the transaction, we only check up to that index.
660
 * Further updates are expected to call this function to match previous indices.
661
 */
662
static bool transaction_has_case_conflicting_update(struct ref_transaction *transaction,
663
                struct ref_update *update)
664
0
{
665
0
  for (size_t i = 0; i < transaction->nr; i++) {
666
0
    if (transaction->updates[i] == update)
667
0
      break;
668
669
0
    if (!strcasecmp(transaction->updates[i]->refname, update->refname))
670
0
      return true;
671
0
  }
672
0
  return false;
673
0
}
674
675
/*
676
 * Lock refname, without following symrefs, and set *lock_p to point
677
 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
678
 * and type similarly to read_raw_ref().
679
 *
680
 * The caller must verify that refname is a "safe" reference name (in
681
 * the sense of refname_is_safe()) before calling this function.
682
 *
683
 * If the reference doesn't already exist, verify that refname doesn't
684
 * have a D/F conflict with any existing references. extras and skip
685
 * are passed to refs_verify_refname_available() for this check.
686
 *
687
 * If mustexist is not set and the reference is not found or is
688
 * broken, lock the reference anyway but clear old_oid.
689
 *
690
 * Return 0 on success. On failure, write an error message to err and
691
 * return REF_TRANSACTION_ERROR_NAME_CONFLICT or REF_TRANSACTION_ERROR_GENERIC.
692
 *
693
 * Implementation note: This function is basically
694
 *
695
 *     lock reference
696
 *     read_raw_ref()
697
 *
698
 * but it includes a lot more code to
699
 * - Deal with possible races with other processes
700
 * - Avoid calling refs_verify_refname_available() when it can be
701
 *   avoided, namely if we were successfully able to read the ref
702
 * - Generate informative error messages in the case of failure
703
 */
704
static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
705
                 struct ref_transaction *transaction,
706
                 size_t update_idx,
707
                 int mustexist,
708
                 struct string_list *refnames_to_check,
709
                 struct ref_lock **lock_p,
710
                 struct strbuf *referent,
711
                 struct strbuf *err)
712
0
{
713
0
  enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;
714
0
  struct ref_update *update = transaction->updates[update_idx];
715
0
  const struct string_list *extras = &transaction->refnames;
716
0
  const char *refname = update->refname;
717
0
  unsigned int *type = &update->type;
718
0
  struct ref_lock *lock;
719
0
  struct strbuf ref_file = STRBUF_INIT;
720
0
  int attempts_remaining = 3;
721
0
  int failure_errno;
722
723
0
  assert(err);
724
0
  files_assert_main_repository(refs, "lock_raw_ref");
725
726
0
  *type = 0;
727
728
  /* First lock the file so it can't change out from under us. */
729
730
0
  *lock_p = CALLOC_ARRAY(lock, 1);
731
732
0
  lock->ref_name = xstrdup(refname);
733
0
  lock->count = 1;
734
0
  files_ref_path(refs, &ref_file, refname);
735
736
0
retry:
737
0
  switch (safe_create_leading_directories(the_repository, ref_file.buf)) {
738
0
  case SCLD_OK:
739
0
    break; /* success */
740
0
  case SCLD_EXISTS:
741
    /*
742
     * Suppose refname is "refs/foo/bar". We just failed
743
     * to create the containing directory, "refs/foo",
744
     * because there was a non-directory in the way. This
745
     * indicates a D/F conflict, probably because of
746
     * another reference such as "refs/foo". There is no
747
     * reason to expect this error to be transitory.
748
     */
749
0
    if (refs_verify_refname_available(&refs->base, refname,
750
0
              extras, NULL, 0, err)) {
751
0
      if (mustexist) {
752
        /*
753
         * To the user the relevant error is
754
         * that the "mustexist" reference is
755
         * missing:
756
         */
757
0
        strbuf_reset(err);
758
0
        strbuf_addf(err, "unable to resolve reference '%s'",
759
0
              refname);
760
0
        ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
761
0
      } else {
762
        /*
763
         * The error message set by
764
         * refs_verify_refname_available() is
765
         * OK.
766
         */
767
0
        ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
768
0
      }
769
0
    } else {
770
      /*
771
       * The file that is in the way isn't a loose
772
       * reference. Report it as a low-level
773
       * failure.
774
       */
775
0
      strbuf_addf(err, "unable to create lock file %s.lock; "
776
0
            "non-directory in the way",
777
0
            ref_file.buf);
778
0
    }
779
0
    goto error_return;
780
0
  case SCLD_VANISHED:
781
    /* Maybe another process was tidying up. Try again. */
782
0
    if (--attempts_remaining > 0)
783
0
      goto retry;
784
    /* fall through */
785
0
  default:
786
0
    strbuf_addf(err, "unable to create directory for %s",
787
0
          ref_file.buf);
788
0
    goto error_return;
789
0
  }
790
791
0
  if (hold_lock_file_for_update_timeout(
792
0
          &lock->lk, ref_file.buf, LOCK_NO_DEREF,
793
0
          get_files_ref_lock_timeout_ms()) < 0) {
794
0
    int myerr = errno;
795
0
    errno = 0;
796
0
    if (myerr == ENOENT && --attempts_remaining > 0) {
797
      /*
798
       * Maybe somebody just deleted one of the
799
       * directories leading to ref_file.  Try
800
       * again:
801
       */
802
0
      goto retry;
803
0
    } else {
804
0
      unable_to_lock_message(ref_file.buf, myerr, err);
805
0
      if (myerr == EEXIST) {
806
0
        if (ignore_case &&
807
0
            transaction_has_case_conflicting_update(transaction, update)) {
808
          /*
809
           * In case-insensitive filesystems, ensure that conflicts within a
810
           * given transaction are handled. Pre-existing refs on a
811
           * case-insensitive system will be overridden without any issue.
812
           */
813
0
          ret = REF_TRANSACTION_ERROR_CASE_CONFLICT;
814
0
        } else {
815
          /*
816
           * Pre-existing case-conflicting reference locks should also be
817
           * specially categorized to avoid failing all batched updates.
818
           */
819
0
          ret = REF_TRANSACTION_ERROR_CREATE_EXISTS;
820
0
        }
821
0
      }
822
823
0
      goto error_return;
824
0
    }
825
0
  }
826
827
  /*
828
   * Now we hold the lock and can read the reference without
829
   * fear that its value will change.
830
   */
831
832
0
  if (files_read_raw_ref(&refs->base, refname, &lock->old_oid, referent,
833
0
             type, &failure_errno)) {
834
0
    struct string_list_item *item;
835
836
0
    if (failure_errno == ENOENT) {
837
0
      if (mustexist) {
838
        /* Garden variety missing reference. */
839
0
        strbuf_addf(err, "unable to resolve reference '%s'",
840
0
              refname);
841
0
        ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
842
0
        goto error_return;
843
0
      } else {
844
        /*
845
         * Reference is missing, but that's OK. We
846
         * know that there is not a conflict with
847
         * another loose reference because
848
         * (supposing that we are trying to lock
849
         * reference "refs/foo/bar"):
850
         *
851
         * - We were successfully able to create
852
         *   the lockfile refs/foo/bar.lock, so we
853
         *   know there cannot be a loose reference
854
         *   named "refs/foo".
855
         *
856
         * - We got ENOENT and not EISDIR, so we
857
         *   know that there cannot be a loose
858
         *   reference named "refs/foo/bar/baz".
859
         */
860
0
      }
861
0
    } else if (failure_errno == EISDIR) {
862
      /*
863
       * There is a directory in the way. It might have
864
       * contained references that have been deleted. If
865
       * we don't require that the reference already
866
       * exists, try to remove the directory so that it
867
       * doesn't cause trouble when we want to rename the
868
       * lockfile into place later.
869
       */
870
0
      if (mustexist) {
871
        /* Garden variety missing reference. */
872
0
        strbuf_addf(err, "unable to resolve reference '%s'",
873
0
              refname);
874
0
        ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
875
0
        goto error_return;
876
0
      } else if (remove_dir_recursively(&ref_file,
877
0
                REMOVE_DIR_EMPTY_ONLY)) {
878
0
        ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
879
0
        if (refs_verify_refname_available(
880
0
                &refs->base, refname,
881
0
                extras, NULL, 0, err)) {
882
          /*
883
           * The error message set by
884
           * verify_refname_available() is OK.
885
           */
886
0
          goto error_return;
887
0
        } else {
888
          /*
889
           * Directory conflicts can occur if there
890
           * is an existing lock file in the directory
891
           * or if the filesystem is case-insensitive
892
           * and the directory contains a valid reference
893
           * but conflicts with the update.
894
           */
895
0
          strbuf_addf(err, "there is a non-empty directory '%s' "
896
0
                "blocking reference '%s'",
897
0
                ref_file.buf, refname);
898
0
          goto error_return;
899
0
        }
900
0
      }
901
0
    } else if (failure_errno == EINVAL && (*type & REF_ISBROKEN)) {
902
0
      strbuf_addf(err, "unable to resolve reference '%s': "
903
0
            "reference broken", refname);
904
0
      goto error_return;
905
0
    } else {
906
0
      strbuf_addf(err, "unable to resolve reference '%s': %s",
907
0
            refname, strerror(failure_errno));
908
0
      goto error_return;
909
0
    }
910
911
    /*
912
     * If the ref did not exist and we are creating it, we have to
913
     * make sure there is no existing packed ref that conflicts
914
     * with refname. This check is deferred so that we can batch it.
915
     *
916
     * For case-insensitive filesystems, we should also check for F/D
917
     * conflicts between 'foo' and 'Foo/bar'. So let's lowercase
918
     * the refname.
919
     */
920
0
    if (ignore_case) {
921
0
      struct strbuf lower = STRBUF_INIT;
922
923
0
      strbuf_addstr(&lower, refname);
924
0
      strbuf_tolower(&lower);
925
926
0
      item = string_list_append_nodup(refnames_to_check,
927
0
              strbuf_detach(&lower, NULL));
928
0
    } else {
929
0
      item = string_list_append(refnames_to_check, refname);
930
0
    }
931
932
0
    item->util = xmalloc(sizeof(update_idx));
933
0
    memcpy(item->util, &update_idx, sizeof(update_idx));
934
0
  }
935
936
0
  ret = 0;
937
0
  goto out;
938
939
0
error_return:
940
0
  unlock_ref(lock);
941
0
  *lock_p = NULL;
942
943
0
out:
944
0
  strbuf_release(&ref_file);
945
0
  return ret;
946
0
}
947
948
struct files_ref_iterator {
949
  struct ref_iterator base;
950
951
  struct ref_iterator *iter0;
952
  struct repository *repo;
953
  unsigned int flags;
954
};
955
956
static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
957
0
{
958
0
  struct files_ref_iterator *iter =
959
0
    (struct files_ref_iterator *)ref_iterator;
960
0
  int ok;
961
962
0
  while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
963
0
    if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
964
0
        parse_worktree_ref(iter->iter0->ref.name, NULL, NULL,
965
0
               NULL) != REF_WORKTREE_CURRENT)
966
0
      continue;
967
968
0
    if ((iter->flags & DO_FOR_EACH_OMIT_DANGLING_SYMREFS) &&
969
0
        (iter->iter0->ref.flags & REF_ISSYMREF) &&
970
0
        (iter->iter0->ref.flags & REF_ISBROKEN))
971
0
      continue;
972
973
0
    if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
974
0
        !ref_resolves_to_object(iter->iter0->ref.name,
975
0
              iter->repo,
976
0
              iter->iter0->ref.oid,
977
0
              iter->iter0->ref.flags))
978
0
      continue;
979
980
0
    iter->base.ref = iter->iter0->ref;
981
982
0
    return ITER_OK;
983
0
  }
984
985
0
  return ok;
986
0
}
987
988
static int files_ref_iterator_seek(struct ref_iterator *ref_iterator,
989
           const char *refname, unsigned int flags)
990
0
{
991
0
  struct files_ref_iterator *iter =
992
0
    (struct files_ref_iterator *)ref_iterator;
993
0
  return ref_iterator_seek(iter->iter0, refname, flags);
994
0
}
995
996
static void files_ref_iterator_release(struct ref_iterator *ref_iterator)
997
0
{
998
0
  struct files_ref_iterator *iter =
999
0
    (struct files_ref_iterator *)ref_iterator;
1000
0
  ref_iterator_free(iter->iter0);
1001
0
}
1002
1003
static struct ref_iterator_vtable files_ref_iterator_vtable = {
1004
  .advance = files_ref_iterator_advance,
1005
  .seek = files_ref_iterator_seek,
1006
  .release = files_ref_iterator_release,
1007
};
1008
1009
static struct ref_iterator *files_ref_iterator_begin(
1010
    struct ref_store *ref_store,
1011
    const char *prefix, const char **exclude_patterns,
1012
    unsigned int flags)
1013
0
{
1014
0
  struct files_ref_store *refs;
1015
0
  struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
1016
0
  struct files_ref_iterator *iter;
1017
0
  struct ref_iterator *ref_iterator;
1018
0
  unsigned int required_flags = REF_STORE_READ;
1019
1020
0
  if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
1021
0
    required_flags |= REF_STORE_ODB;
1022
1023
0
  refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
1024
1025
  /*
1026
   * We must make sure that all loose refs are read before
1027
   * accessing the packed-refs file; this avoids a race
1028
   * condition if loose refs are migrated to the packed-refs
1029
   * file by a simultaneous process, but our in-memory view is
1030
   * from before the migration. We ensure this as follows:
1031
   * First, we call start the loose refs iteration with its
1032
   * `prime_ref` argument set to true. This causes the loose
1033
   * references in the subtree to be pre-read into the cache.
1034
   * (If they've already been read, that's OK; we only need to
1035
   * guarantee that they're read before the packed refs, not
1036
   * *how much* before.) After that, we call
1037
   * packed_ref_iterator_begin(), which internally checks
1038
   * whether the packed-ref cache is up to date with what is on
1039
   * disk, and re-reads it if not.
1040
   */
1041
1042
0
  loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
1043
0
                prefix, ref_store->repo, 1);
1044
1045
  /*
1046
   * The packed-refs file might contain broken references, for
1047
   * example an old version of a reference that points at an
1048
   * object that has since been garbage-collected. This is OK as
1049
   * long as there is a corresponding loose reference that
1050
   * overrides it, and we don't want to emit an error message in
1051
   * this case. So ask the packed_ref_store for all of its
1052
   * references, and (if needed) do our own check for broken
1053
   * ones in files_ref_iterator_advance(), after we have merged
1054
   * the packed and loose references.
1055
   */
1056
0
  packed_iter = refs_ref_iterator_begin(
1057
0
      refs->packed_ref_store, prefix, exclude_patterns, 0,
1058
0
      DO_FOR_EACH_INCLUDE_BROKEN);
1059
1060
0
  overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter);
1061
1062
0
  CALLOC_ARRAY(iter, 1);
1063
0
  ref_iterator = &iter->base;
1064
0
  base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
1065
0
  iter->iter0 = overlay_iter;
1066
0
  iter->repo = ref_store->repo;
1067
0
  iter->flags = flags;
1068
1069
0
  return ref_iterator;
1070
0
}
1071
1072
/*
1073
 * Callback function for raceproof_create_file(). This function is
1074
 * expected to do something that makes dirname(path) permanent despite
1075
 * the fact that other processes might be cleaning up empty
1076
 * directories at the same time. Usually it will create a file named
1077
 * path, but alternatively it could create another file in that
1078
 * directory, or even chdir() into that directory. The function should
1079
 * return 0 if the action was completed successfully. On error, it
1080
 * should return a nonzero result and set errno.
1081
 * raceproof_create_file() treats two errno values specially:
1082
 *
1083
 * - ENOENT -- dirname(path) does not exist. In this case,
1084
 *             raceproof_create_file() tries creating dirname(path)
1085
 *             (and any parent directories, if necessary) and calls
1086
 *             the function again.
1087
 *
1088
 * - EISDIR -- the file already exists and is a directory. In this
1089
 *             case, raceproof_create_file() removes the directory if
1090
 *             it is empty (and recursively any empty directories that
1091
 *             it contains) and calls the function again.
1092
 *
1093
 * Any other errno causes raceproof_create_file() to fail with the
1094
 * callback's return value and errno.
1095
 *
1096
 * Obviously, this function should be OK with being called again if it
1097
 * fails with ENOENT or EISDIR. In other scenarios it will not be
1098
 * called again.
1099
 */
1100
typedef int create_file_fn(const char *path, void *cb);
1101
1102
/*
1103
 * Create a file in dirname(path) by calling fn, creating leading
1104
 * directories if necessary. Retry a few times in case we are racing
1105
 * with another process that is trying to clean up the directory that
1106
 * contains path. See the documentation for create_file_fn for more
1107
 * details.
1108
 *
1109
 * Return the value and set the errno that resulted from the most
1110
 * recent call of fn. fn is always called at least once, and will be
1111
 * called more than once if it returns ENOENT or EISDIR.
1112
 */
1113
static int raceproof_create_file(const char *path, create_file_fn fn, void *cb)
1114
0
{
1115
  /*
1116
   * The number of times we will try to remove empty directories
1117
   * in the way of path. This is only 1 because if another
1118
   * process is racily creating directories that conflict with
1119
   * us, we don't want to fight against them.
1120
   */
1121
0
  int remove_directories_remaining = 1;
1122
1123
  /*
1124
   * The number of times that we will try to create the
1125
   * directories containing path. We are willing to attempt this
1126
   * more than once, because another process could be trying to
1127
   * clean up empty directories at the same time as we are
1128
   * trying to create them.
1129
   */
1130
0
  int create_directories_remaining = 3;
1131
1132
  /* A scratch copy of path, filled lazily if we need it: */
1133
0
  struct strbuf path_copy = STRBUF_INIT;
1134
1135
0
  int ret, save_errno;
1136
1137
  /* Sanity check: */
1138
0
  assert(*path);
1139
1140
0
retry_fn:
1141
0
  ret = fn(path, cb);
1142
0
  save_errno = errno;
1143
0
  if (!ret)
1144
0
    goto out;
1145
1146
0
  if (errno == EISDIR && remove_directories_remaining-- > 0) {
1147
    /*
1148
     * A directory is in the way. Maybe it is empty; try
1149
     * to remove it:
1150
     */
1151
0
    if (!path_copy.len)
1152
0
      strbuf_addstr(&path_copy, path);
1153
1154
0
    if (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY))
1155
0
      goto retry_fn;
1156
0
  } else if (errno == ENOENT && create_directories_remaining-- > 0) {
1157
    /*
1158
     * Maybe the containing directory didn't exist, or
1159
     * maybe it was just deleted by a process that is
1160
     * racing with us to clean up empty directories. Try
1161
     * to create it:
1162
     */
1163
0
    enum scld_error scld_result;
1164
1165
0
    if (!path_copy.len)
1166
0
      strbuf_addstr(&path_copy, path);
1167
1168
0
    do {
1169
0
      scld_result = safe_create_leading_directories(the_repository, path_copy.buf);
1170
0
      if (scld_result == SCLD_OK)
1171
0
        goto retry_fn;
1172
0
    } while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0);
1173
0
  }
1174
1175
0
out:
1176
0
  strbuf_release(&path_copy);
1177
0
  errno = save_errno;
1178
0
  return ret;
1179
0
}
1180
1181
static int remove_empty_directories(struct strbuf *path)
1182
0
{
1183
  /*
1184
   * we want to create a file but there is a directory there;
1185
   * if that is an empty directory (or a directory that contains
1186
   * only empty directories), remove them.
1187
   */
1188
0
  return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1189
0
}
1190
1191
static int create_reflock(const char *path, void *cb)
1192
0
{
1193
0
  struct lock_file *lk = cb;
1194
1195
0
  return hold_lock_file_for_update_timeout(
1196
0
      lk, path, LOCK_NO_DEREF,
1197
0
      get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
1198
0
}
1199
1200
/*
1201
 * Locks a ref returning the lock on success and NULL on failure.
1202
 */
1203
static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
1204
             const char *refname,
1205
             struct strbuf *err)
1206
0
{
1207
0
  struct strbuf ref_file = STRBUF_INIT;
1208
0
  struct ref_lock *lock;
1209
1210
0
  files_assert_main_repository(refs, "lock_ref_oid_basic");
1211
0
  assert(err);
1212
1213
0
  CALLOC_ARRAY(lock, 1);
1214
1215
0
  files_ref_path(refs, &ref_file, refname);
1216
1217
  /*
1218
   * If the ref did not exist and we are creating it, make sure
1219
   * there is no existing packed ref whose name begins with our
1220
   * refname, nor a packed ref whose name is a proper prefix of
1221
   * our refname.
1222
   */
1223
0
  if (is_null_oid(&lock->old_oid) &&
1224
0
      refs_verify_refname_available(refs->packed_ref_store, refname,
1225
0
            NULL, NULL, 0, err))
1226
0
    goto error_return;
1227
1228
0
  lock->ref_name = xstrdup(refname);
1229
0
  lock->count = 1;
1230
1231
0
  if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) {
1232
0
    unable_to_lock_message(ref_file.buf, errno, err);
1233
0
    goto error_return;
1234
0
  }
1235
1236
0
  if (!refs_resolve_ref_unsafe(&refs->base, lock->ref_name, 0,
1237
0
             &lock->old_oid, NULL))
1238
0
    oidclr(&lock->old_oid, refs->base.repo->hash_algo);
1239
0
  goto out;
1240
1241
0
 error_return:
1242
0
  unlock_ref(lock);
1243
0
  lock = NULL;
1244
1245
0
 out:
1246
0
  strbuf_release(&ref_file);
1247
0
  return lock;
1248
0
}
1249
1250
struct ref_to_prune {
1251
  struct ref_to_prune *next;
1252
  struct object_id oid;
1253
  char name[FLEX_ARRAY];
1254
};
1255
1256
enum {
1257
  REMOVE_EMPTY_PARENTS_REF = 0x01,
1258
  REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1259
};
1260
1261
/*
1262
 * Remove empty parent directories associated with the specified
1263
 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1264
 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1265
 * REMOVE_EMPTY_PARENTS_REFLOG.
1266
 */
1267
static void try_remove_empty_parents(struct files_ref_store *refs,
1268
             const char *refname,
1269
             unsigned int flags)
1270
0
{
1271
0
  struct strbuf buf = STRBUF_INIT;
1272
0
  struct strbuf sb = STRBUF_INIT;
1273
0
  char *p, *q;
1274
0
  int i;
1275
1276
0
  strbuf_addstr(&buf, refname);
1277
0
  p = buf.buf;
1278
0
  for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1279
0
    while (*p && *p != '/')
1280
0
      p++;
1281
    /* tolerate duplicate slashes; see check_refname_format() */
1282
0
    while (*p == '/')
1283
0
      p++;
1284
0
  }
1285
0
  q = buf.buf + buf.len;
1286
0
  while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
1287
0
    while (q > p && *q != '/')
1288
0
      q--;
1289
0
    while (q > p && *(q-1) == '/')
1290
0
      q--;
1291
0
    if (q == p)
1292
0
      break;
1293
0
    strbuf_setlen(&buf, q - buf.buf);
1294
1295
0
    strbuf_reset(&sb);
1296
0
    files_ref_path(refs, &sb, buf.buf);
1297
0
    if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
1298
0
      flags &= ~REMOVE_EMPTY_PARENTS_REF;
1299
1300
0
    strbuf_reset(&sb);
1301
0
    files_reflog_path(refs, &sb, buf.buf);
1302
0
    if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
1303
0
      flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
1304
0
  }
1305
0
  strbuf_release(&buf);
1306
0
  strbuf_release(&sb);
1307
0
}
1308
1309
/* make sure nobody touched the ref, and unlink */
1310
static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
1311
0
{
1312
0
  struct ref_transaction *transaction;
1313
0
  struct strbuf err = STRBUF_INIT;
1314
0
  int ret = -1;
1315
1316
0
  if (check_refname_format(r->name, 0))
1317
0
    return;
1318
1319
0
  transaction = ref_store_transaction_begin(&refs->base, 0, &err);
1320
0
  if (!transaction)
1321
0
    goto cleanup;
1322
0
  ref_transaction_add_update(
1323
0
      transaction, r->name,
1324
0
      REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING,
1325
0
      null_oid(the_hash_algo), &r->oid, NULL, NULL, NULL, NULL);
1326
0
  if (ref_transaction_commit(transaction, &err))
1327
0
    goto cleanup;
1328
1329
0
  ret = 0;
1330
1331
0
cleanup:
1332
0
  if (ret)
1333
0
    error("%s", err.buf);
1334
0
  strbuf_release(&err);
1335
0
  ref_transaction_free(transaction);
1336
0
  return;
1337
0
}
1338
1339
/*
1340
 * Prune the loose versions of the references in the linked list
1341
 * `*refs_to_prune`, freeing the entries in the list as we go.
1342
 */
1343
static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune)
1344
0
{
1345
0
  while (*refs_to_prune) {
1346
0
    struct ref_to_prune *r = *refs_to_prune;
1347
0
    *refs_to_prune = r->next;
1348
0
    prune_ref(refs, r);
1349
0
    free(r);
1350
0
  }
1351
0
}
1352
1353
/*
1354
 * Return true if the specified reference should be packed.
1355
 */
1356
static int should_pack_ref(struct files_ref_store *refs,
1357
         const struct reference *ref,
1358
         struct refs_optimize_opts *opts)
1359
0
{
1360
0
  struct string_list_item *item;
1361
1362
  /* Do not pack per-worktree refs: */
1363
0
  if (parse_worktree_ref(ref->name, NULL, NULL, NULL) !=
1364
0
      REF_WORKTREE_SHARED)
1365
0
    return 0;
1366
1367
  /* Do not pack symbolic refs: */
1368
0
  if (ref->flags & REF_ISSYMREF)
1369
0
    return 0;
1370
1371
  /* Do not pack broken refs: */
1372
0
  if (!ref_resolves_to_object(ref->name, refs->base.repo, ref->oid, ref->flags))
1373
0
    return 0;
1374
1375
0
  if (ref_excluded(opts->exclusions, ref->name))
1376
0
    return 0;
1377
1378
0
  for_each_string_list_item(item, opts->includes)
1379
0
    if (!wildmatch(item->string, ref->name, 0))
1380
0
      return 1;
1381
1382
0
  return 0;
1383
0
}
1384
1385
static int should_pack_refs(struct files_ref_store *refs,
1386
          struct refs_optimize_opts *opts)
1387
0
{
1388
0
  struct ref_iterator *iter;
1389
0
  size_t packed_size;
1390
0
  size_t refcount = 0;
1391
0
  size_t limit;
1392
0
  int ret;
1393
1394
0
  if (!(opts->flags & REFS_OPTIMIZE_AUTO))
1395
0
    return 1;
1396
1397
0
  ret = packed_refs_size(refs->packed_ref_store, &packed_size);
1398
0
  if (ret < 0)
1399
0
    die("cannot determine packed-refs size");
1400
1401
  /*
1402
   * Packing loose references into the packed-refs file scales with the
1403
   * number of references we're about to write. We thus decide whether we
1404
   * repack refs by weighing the current size of the packed-refs file
1405
   * against the number of loose references. This is done such that we do
1406
   * not repack too often on repositories with a huge number of
1407
   * references, where we can expect a lot of churn in the number of
1408
   * references.
1409
   *
1410
   * As a heuristic, we repack if the number of loose references in the
1411
   * repository exceeds `log2(nr_packed_refs) * 5`, where we estimate
1412
   * `nr_packed_refs = packed_size / 100`, which scales as following:
1413
   *
1414
   * - 1kB ~ 10 packed refs: 16 refs
1415
   * - 10kB ~ 100 packed refs: 33 refs
1416
   * - 100kB ~ 1k packed refs: 49 refs
1417
   * - 1MB ~ 10k packed refs: 66 refs
1418
   * - 10MB ~ 100k packed refs: 82 refs
1419
   * - 100MB ~ 1m packed refs: 99 refs
1420
   *
1421
   * We thus allow roughly 16 additional loose refs per factor of ten of
1422
   * packed refs. This heuristic may be tweaked in the future, but should
1423
   * serve as a sufficiently good first iteration.
1424
   */
1425
0
  limit = log2u(packed_size / 100) * 5;
1426
0
  if (limit < 16)
1427
0
    limit = 16;
1428
1429
0
  iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
1430
0
          refs->base.repo, 0);
1431
0
  while ((ret = ref_iterator_advance(iter)) == ITER_OK) {
1432
0
    if (should_pack_ref(refs, &iter->ref, opts))
1433
0
      refcount++;
1434
0
    if (refcount >= limit) {
1435
0
      ref_iterator_free(iter);
1436
0
      return 1;
1437
0
    }
1438
0
  }
1439
1440
0
  if (ret != ITER_DONE)
1441
0
    die("error while iterating over references");
1442
1443
0
  ref_iterator_free(iter);
1444
0
  return 0;
1445
0
}
1446
1447
static int files_optimize(struct ref_store *ref_store,
1448
        struct refs_optimize_opts *opts)
1449
0
{
1450
0
  struct files_ref_store *refs =
1451
0
    files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1452
0
             "pack_refs");
1453
0
  struct ref_iterator *iter;
1454
0
  int ok;
1455
0
  struct ref_to_prune *refs_to_prune = NULL;
1456
0
  struct strbuf err = STRBUF_INIT;
1457
0
  struct ref_transaction *transaction;
1458
1459
0
  if (!should_pack_refs(refs, opts))
1460
0
    return 0;
1461
1462
0
  transaction = ref_store_transaction_begin(refs->packed_ref_store,
1463
0
              0, &err);
1464
0
  if (!transaction)
1465
0
    return -1;
1466
1467
0
  packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
1468
1469
0
  iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
1470
0
          refs->base.repo, 0);
1471
0
  while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1472
    /*
1473
     * If the loose reference can be packed, add an entry
1474
     * in the packed ref cache. If the reference should be
1475
     * pruned, also add it to refs_to_prune.
1476
     */
1477
0
    if (!should_pack_ref(refs, &iter->ref, opts))
1478
0
      continue;
1479
1480
    /*
1481
     * Add a reference creation for this reference to the
1482
     * packed-refs transaction:
1483
     */
1484
0
    if (ref_transaction_update(transaction, iter->ref.name,
1485
0
             iter->ref.oid, NULL, NULL, NULL,
1486
0
             REF_NO_DEREF, NULL, &err))
1487
0
      die("failure preparing to create packed reference %s: %s",
1488
0
          iter->ref.name, err.buf);
1489
1490
    /* Schedule the loose reference for pruning if requested. */
1491
0
    if ((opts->flags & REFS_OPTIMIZE_PRUNE)) {
1492
0
      struct ref_to_prune *n;
1493
0
      FLEX_ALLOC_STR(n, name, iter->ref.name);
1494
0
      oidcpy(&n->oid, iter->ref.oid);
1495
0
      n->next = refs_to_prune;
1496
0
      refs_to_prune = n;
1497
0
    }
1498
0
  }
1499
0
  if (ok != ITER_DONE)
1500
0
    die("error while iterating over references");
1501
1502
0
  if (ref_transaction_commit(transaction, &err))
1503
0
    die("unable to write new packed-refs: %s", err.buf);
1504
1505
0
  ref_transaction_free(transaction);
1506
1507
0
  packed_refs_unlock(refs->packed_ref_store);
1508
1509
0
  prune_refs(refs, &refs_to_prune);
1510
0
  ref_iterator_free(iter);
1511
0
  strbuf_release(&err);
1512
0
  return 0;
1513
0
}
1514
1515
static int files_optimize_required(struct ref_store *ref_store,
1516
           struct refs_optimize_opts *opts,
1517
           bool *required)
1518
0
{
1519
0
  struct files_ref_store *refs = files_downcast(ref_store, REF_STORE_READ,
1520
0
                  "optimize_required");
1521
0
  *required = should_pack_refs(refs, opts);
1522
0
  return 0;
1523
0
}
1524
1525
/*
1526
 * People using contrib's git-new-workdir have .git/logs/refs ->
1527
 * /some/other/path/.git/logs/refs, and that may live on another device.
1528
 *
1529
 * IOW, to avoid cross device rename errors, the temporary renamed log must
1530
 * live into logs/refs.
1531
 */
1532
0
#define TMP_RENAMED_LOG  "refs/.tmp-renamed-log"
1533
1534
struct rename_cb {
1535
  const char *tmp_renamed_log;
1536
  int true_errno;
1537
};
1538
1539
static int rename_tmp_log_callback(const char *path, void *cb_data)
1540
0
{
1541
0
  struct rename_cb *cb = cb_data;
1542
1543
0
  if (rename(cb->tmp_renamed_log, path)) {
1544
    /*
1545
     * rename(a, b) when b is an existing directory ought
1546
     * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1547
     * Sheesh. Record the true errno for error reporting,
1548
     * but report EISDIR to raceproof_create_file() so
1549
     * that it knows to retry.
1550
     */
1551
0
    cb->true_errno = errno;
1552
0
    if (errno == ENOTDIR)
1553
0
      errno = EISDIR;
1554
0
    return -1;
1555
0
  } else {
1556
0
    return 0;
1557
0
  }
1558
0
}
1559
1560
static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1561
0
{
1562
0
  struct strbuf path = STRBUF_INIT;
1563
0
  struct strbuf tmp = STRBUF_INIT;
1564
0
  struct rename_cb cb;
1565
0
  int ret;
1566
1567
0
  files_reflog_path(refs, &path, newrefname);
1568
0
  files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
1569
0
  cb.tmp_renamed_log = tmp.buf;
1570
0
  ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
1571
0
  if (ret) {
1572
0
    if (errno == EISDIR)
1573
0
      error("directory not empty: %s", path.buf);
1574
0
    else
1575
0
      error("unable to move logfile %s to %s: %s",
1576
0
            tmp.buf, path.buf,
1577
0
            strerror(cb.true_errno));
1578
0
  }
1579
1580
0
  strbuf_release(&path);
1581
0
  strbuf_release(&tmp);
1582
0
  return ret;
1583
0
}
1584
1585
static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,
1586
              struct ref_lock *lock,
1587
              const struct object_id *oid,
1588
              int skip_oid_verification,
1589
              struct strbuf *err);
1590
static int commit_ref_update(struct files_ref_store *refs,
1591
           struct ref_lock *lock,
1592
           const struct object_id *oid, const char *logmsg,
1593
           int flags,
1594
           struct strbuf *err);
1595
1596
/*
1597
 * Emit a better error message than lockfile.c's
1598
 * unable_to_lock_message() would in case there is a D/F conflict with
1599
 * another existing reference. If there would be a conflict, emit an error
1600
 * message and return false; otherwise, return true.
1601
 *
1602
 * Note that this function is not safe against all races with other
1603
 * processes, and that's not its job. We'll emit a more verbose error on D/f
1604
 * conflicts if we get past it into lock_ref_oid_basic().
1605
 */
1606
static int refs_rename_ref_available(struct ref_store *refs,
1607
            const char *old_refname,
1608
            const char *new_refname)
1609
0
{
1610
0
  struct string_list skip = STRING_LIST_INIT_NODUP;
1611
0
  struct strbuf err = STRBUF_INIT;
1612
0
  int ok;
1613
1614
0
  string_list_insert(&skip, old_refname);
1615
0
  ok = !refs_verify_refname_available(refs, new_refname,
1616
0
              NULL, &skip, 0, &err);
1617
0
  if (!ok)
1618
0
    error("%s", err.buf);
1619
1620
0
  string_list_clear(&skip, 0);
1621
0
  strbuf_release(&err);
1622
0
  return ok;
1623
0
}
1624
1625
static int files_copy_or_rename_ref(struct ref_store *ref_store,
1626
          const char *oldrefname, const char *newrefname,
1627
          const char *logmsg, int copy)
1628
0
{
1629
0
  struct files_ref_store *refs =
1630
0
    files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1631
0
  struct object_id orig_oid;
1632
0
  int flag = 0, logmoved = 0;
1633
0
  struct ref_lock *lock;
1634
0
  struct stat loginfo;
1635
0
  struct strbuf sb_oldref = STRBUF_INIT;
1636
0
  struct strbuf sb_newref = STRBUF_INIT;
1637
0
  struct strbuf tmp_renamed_log = STRBUF_INIT;
1638
0
  int log, ret;
1639
0
  struct strbuf err = STRBUF_INIT;
1640
1641
0
  files_reflog_path(refs, &sb_oldref, oldrefname);
1642
0
  files_reflog_path(refs, &sb_newref, newrefname);
1643
0
  files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
1644
1645
0
  log = !lstat(sb_oldref.buf, &loginfo);
1646
0
  if (log && S_ISLNK(loginfo.st_mode)) {
1647
0
    ret = error("reflog for %s is a symlink", oldrefname);
1648
0
    goto out;
1649
0
  }
1650
1651
0
  if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1652
0
             RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1653
0
             &orig_oid, &flag)) {
1654
0
    ret = error("refname %s not found", oldrefname);
1655
0
    goto out;
1656
0
  }
1657
1658
0
  if (flag & REF_ISSYMREF) {
1659
0
    if (copy)
1660
0
      ret = error("refname %s is a symbolic ref, copying it is not supported",
1661
0
            oldrefname);
1662
0
    else
1663
0
      ret = error("refname %s is a symbolic ref, renaming it is not supported",
1664
0
            oldrefname);
1665
0
    goto out;
1666
0
  }
1667
0
  if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
1668
0
    ret = 1;
1669
0
    goto out;
1670
0
  }
1671
1672
0
  if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
1673
0
    ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1674
0
          oldrefname, strerror(errno));
1675
0
    goto out;
1676
0
  }
1677
1678
0
  if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
1679
0
    ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1680
0
          oldrefname, strerror(errno));
1681
0
    goto out;
1682
0
  }
1683
1684
0
  if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,
1685
0
          &orig_oid, REF_NO_DEREF)) {
1686
0
    error("unable to delete old %s", oldrefname);
1687
0
    goto rollback;
1688
0
  }
1689
1690
  /*
1691
   * Since we are doing a shallow lookup, oid is not the
1692
   * correct value to pass to delete_ref as old_oid. But that
1693
   * doesn't matter, because an old_oid check wouldn't add to
1694
   * the safety anyway; we want to delete the reference whatever
1695
   * its current value.
1696
   */
1697
0
  if (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,
1698
0
               RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1699
0
               NULL, NULL) &&
1700
0
      refs_delete_ref(&refs->base, NULL, newrefname,
1701
0
          NULL, REF_NO_DEREF)) {
1702
0
    if (errno == EISDIR) {
1703
0
      struct strbuf path = STRBUF_INIT;
1704
0
      int result;
1705
1706
0
      files_ref_path(refs, &path, newrefname);
1707
0
      result = remove_empty_directories(&path);
1708
0
      strbuf_release(&path);
1709
1710
0
      if (result) {
1711
0
        error("Directory not empty: %s", newrefname);
1712
0
        goto rollback;
1713
0
      }
1714
0
    } else {
1715
0
      error("unable to delete existing %s", newrefname);
1716
0
      goto rollback;
1717
0
    }
1718
0
  }
1719
1720
0
  if (log && rename_tmp_log(refs, newrefname))
1721
0
    goto rollback;
1722
1723
0
  logmoved = log;
1724
1725
0
  lock = lock_ref_oid_basic(refs, newrefname, &err);
1726
0
  if (!lock) {
1727
0
    if (copy)
1728
0
      error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1729
0
    else
1730
0
      error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1731
0
    strbuf_release(&err);
1732
0
    goto rollback;
1733
0
  }
1734
0
  oidcpy(&lock->old_oid, &orig_oid);
1735
1736
0
  if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
1737
0
      commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {
1738
0
    error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1739
0
    strbuf_release(&err);
1740
0
    goto rollback;
1741
0
  }
1742
1743
0
  ret = 0;
1744
0
  goto out;
1745
1746
0
 rollback:
1747
0
  lock = lock_ref_oid_basic(refs, oldrefname, &err);
1748
0
  if (!lock) {
1749
0
    error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1750
0
    strbuf_release(&err);
1751
0
    goto rollbacklog;
1752
0
  }
1753
1754
0
  if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
1755
0
      commit_ref_update(refs, lock, &orig_oid, NULL, REF_SKIP_CREATE_REFLOG, &err)) {
1756
0
    error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1757
0
    strbuf_release(&err);
1758
0
  }
1759
1760
0
 rollbacklog:
1761
0
  if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
1762
0
    error("unable to restore logfile %s from %s: %s",
1763
0
      oldrefname, newrefname, strerror(errno));
1764
0
  if (!logmoved && log &&
1765
0
      rename(tmp_renamed_log.buf, sb_oldref.buf))
1766
0
    error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
1767
0
      oldrefname, strerror(errno));
1768
0
  ret = 1;
1769
0
 out:
1770
0
  strbuf_release(&sb_newref);
1771
0
  strbuf_release(&sb_oldref);
1772
0
  strbuf_release(&tmp_renamed_log);
1773
1774
0
  return ret;
1775
0
}
1776
1777
static int files_rename_ref(struct ref_store *ref_store,
1778
          const char *oldrefname, const char *newrefname,
1779
          const char *logmsg)
1780
0
{
1781
0
  return files_copy_or_rename_ref(ref_store, oldrefname,
1782
0
         newrefname, logmsg, 0);
1783
0
}
1784
1785
static int files_copy_ref(struct ref_store *ref_store,
1786
          const char *oldrefname, const char *newrefname,
1787
          const char *logmsg)
1788
0
{
1789
0
  return files_copy_or_rename_ref(ref_store, oldrefname,
1790
0
         newrefname, logmsg, 1);
1791
0
}
1792
1793
static int close_ref_gently(struct ref_lock *lock)
1794
0
{
1795
0
  if (close_lock_file_gently(&lock->lk))
1796
0
    return -1;
1797
0
  return 0;
1798
0
}
1799
1800
static int commit_ref(struct ref_lock *lock)
1801
0
{
1802
0
  char *path = get_locked_file_path(&lock->lk);
1803
0
  struct stat st;
1804
1805
0
  if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1806
    /*
1807
     * There is a directory at the path we want to rename
1808
     * the lockfile to. Hopefully it is empty; try to
1809
     * delete it.
1810
     */
1811
0
    size_t len = strlen(path);
1812
0
    struct strbuf sb_path = STRBUF_INIT;
1813
1814
0
    strbuf_attach(&sb_path, path, len, len);
1815
1816
    /*
1817
     * If this fails, commit_lock_file() will also fail
1818
     * and will report the problem.
1819
     */
1820
0
    remove_empty_directories(&sb_path);
1821
0
    strbuf_release(&sb_path);
1822
0
  } else {
1823
0
    free(path);
1824
0
  }
1825
1826
0
  if (commit_lock_file(&lock->lk))
1827
0
    return -1;
1828
0
  return 0;
1829
0
}
1830
1831
static int open_or_create_logfile(const char *path, void *cb)
1832
0
{
1833
0
  int *fd = cb;
1834
1835
0
  *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1836
0
  return (*fd < 0) ? -1 : 0;
1837
0
}
1838
1839
/*
1840
 * Create a reflog for a ref. If force_create = 0, only create the
1841
 * reflog for certain refs (those for which should_autocreate_reflog
1842
 * returns non-zero). Otherwise, create it regardless of the reference
1843
 * name. If the logfile already existed or was created, return 0 and
1844
 * set *logfd to the file descriptor opened for appending to the file.
1845
 * If no logfile exists and we decided not to create one, return 0 and
1846
 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1847
 * return -1.
1848
 */
1849
static int log_ref_setup(struct files_ref_store *refs,
1850
       const char *refname, int force_create,
1851
       int *logfd, struct strbuf *err)
1852
0
{
1853
0
  enum log_refs_config log_refs_cfg = refs->log_all_ref_updates;
1854
0
  struct strbuf logfile_sb = STRBUF_INIT;
1855
0
  char *logfile;
1856
1857
0
  if (log_refs_cfg == LOG_REFS_UNSET)
1858
0
    log_refs_cfg = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1859
1860
0
  files_reflog_path(refs, &logfile_sb, refname);
1861
0
  logfile = strbuf_detach(&logfile_sb, NULL);
1862
1863
0
  if (force_create || should_autocreate_reflog(log_refs_cfg, refname)) {
1864
0
    if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1865
0
      if (errno == ENOENT)
1866
0
        strbuf_addf(err, "unable to create directory for '%s': "
1867
0
              "%s", logfile, strerror(errno));
1868
0
      else if (errno == EISDIR)
1869
0
        strbuf_addf(err, "there are still logs under '%s'",
1870
0
              logfile);
1871
0
      else
1872
0
        strbuf_addf(err, "unable to append to '%s': %s",
1873
0
              logfile, strerror(errno));
1874
1875
0
      goto error;
1876
0
    }
1877
0
  } else {
1878
0
    *logfd = open(logfile, O_APPEND | O_WRONLY);
1879
0
    if (*logfd < 0) {
1880
0
      if (errno == ENOENT || errno == EISDIR) {
1881
        /*
1882
         * The logfile doesn't already exist,
1883
         * but that is not an error; it only
1884
         * means that we won't write log
1885
         * entries to it.
1886
         */
1887
0
        ;
1888
0
      } else {
1889
0
        strbuf_addf(err, "unable to append to '%s': %s",
1890
0
              logfile, strerror(errno));
1891
0
        goto error;
1892
0
      }
1893
0
    }
1894
0
  }
1895
1896
0
  if (*logfd >= 0)
1897
0
    adjust_shared_perm(the_repository, logfile);
1898
1899
0
  free(logfile);
1900
0
  return 0;
1901
1902
0
error:
1903
0
  free(logfile);
1904
0
  return -1;
1905
0
}
1906
1907
static int files_create_reflog(struct ref_store *ref_store, const char *refname,
1908
             struct strbuf *err)
1909
0
{
1910
0
  struct files_ref_store *refs =
1911
0
    files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
1912
0
  int fd;
1913
1914
0
  if (log_ref_setup(refs, refname, 1, &fd, err))
1915
0
    return -1;
1916
1917
0
  if (fd >= 0)
1918
0
    close(fd);
1919
1920
0
  return 0;
1921
0
}
1922
1923
static int log_ref_write_fd(int fd, const struct object_id *old_oid,
1924
          const struct object_id *new_oid,
1925
          const char *committer, const char *msg)
1926
0
{
1927
0
  struct strbuf sb = STRBUF_INIT;
1928
0
  int ret = 0;
1929
1930
0
  if (!committer)
1931
0
    committer = git_committer_info(0);
1932
1933
0
  strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);
1934
0
  if (msg && *msg) {
1935
0
    strbuf_addch(&sb, '\t');
1936
0
    strbuf_addstr(&sb, msg);
1937
0
  }
1938
0
  strbuf_addch(&sb, '\n');
1939
0
  if (write_in_full(fd, sb.buf, sb.len) < 0)
1940
0
    ret = -1;
1941
0
  strbuf_release(&sb);
1942
0
  return ret;
1943
0
}
1944
1945
static int files_log_ref_write(struct files_ref_store *refs,
1946
             const char *refname,
1947
             const struct object_id *old_oid,
1948
             const struct object_id *new_oid,
1949
             const char *committer_info, const char *msg,
1950
             int flags, struct strbuf *err)
1951
0
{
1952
0
  int logfd, result;
1953
1954
0
  if (flags & REF_SKIP_CREATE_REFLOG)
1955
0
    return 0;
1956
1957
0
  result = log_ref_setup(refs, refname,
1958
0
             flags & REF_FORCE_CREATE_REFLOG,
1959
0
             &logfd, err);
1960
1961
0
  if (result)
1962
0
    return result;
1963
1964
0
  if (logfd < 0)
1965
0
    return 0;
1966
0
  result = log_ref_write_fd(logfd, old_oid, new_oid, committer_info, msg);
1967
0
  if (result) {
1968
0
    struct strbuf sb = STRBUF_INIT;
1969
0
    int save_errno = errno;
1970
1971
0
    files_reflog_path(refs, &sb, refname);
1972
0
    strbuf_addf(err, "unable to append to '%s': %s",
1973
0
          sb.buf, strerror(save_errno));
1974
0
    strbuf_release(&sb);
1975
0
    close(logfd);
1976
0
    return -1;
1977
0
  }
1978
0
  if (close(logfd)) {
1979
0
    struct strbuf sb = STRBUF_INIT;
1980
0
    int save_errno = errno;
1981
1982
0
    files_reflog_path(refs, &sb, refname);
1983
0
    strbuf_addf(err, "unable to append to '%s': %s",
1984
0
          sb.buf, strerror(save_errno));
1985
0
    strbuf_release(&sb);
1986
0
    return -1;
1987
0
  }
1988
0
  return 0;
1989
0
}
1990
1991
/*
1992
 * Write oid into the open lockfile, then close the lockfile. On
1993
 * errors, rollback the lockfile, fill in *err and return -1.
1994
 */
1995
static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,
1996
              struct ref_lock *lock,
1997
              const struct object_id *oid,
1998
              int skip_oid_verification,
1999
              struct strbuf *err)
2000
0
{
2001
0
  static char term = '\n';
2002
0
  struct object *o;
2003
0
  int fd;
2004
2005
0
  if (!skip_oid_verification) {
2006
0
    o = parse_object(refs->base.repo, oid);
2007
0
    if (!o) {
2008
0
      strbuf_addf(
2009
0
        err,
2010
0
        "trying to write ref '%s' with nonexistent object %s",
2011
0
        lock->ref_name, oid_to_hex(oid));
2012
0
      unlock_ref(lock);
2013
0
      return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;
2014
0
    }
2015
0
    if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2016
0
      strbuf_addf(
2017
0
        err,
2018
0
        "trying to write non-commit object %s to branch '%s'",
2019
0
        oid_to_hex(oid), lock->ref_name);
2020
0
      unlock_ref(lock);
2021
0
      return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;
2022
0
    }
2023
0
  }
2024
0
  fd = get_lock_file_fd(&lock->lk);
2025
0
  if (write_in_full(fd, oid_to_hex(oid), refs->base.repo->hash_algo->hexsz) < 0 ||
2026
0
      write_in_full(fd, &term, 1) < 0 ||
2027
0
      fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&lock->lk)) < 0 ||
2028
0
      close_ref_gently(lock) < 0) {
2029
0
    strbuf_addf(err,
2030
0
          "couldn't write '%s'", get_lock_file_path(&lock->lk));
2031
0
    unlock_ref(lock);
2032
0
    return REF_TRANSACTION_ERROR_GENERIC;
2033
0
  }
2034
0
  return 0;
2035
0
}
2036
2037
/*
2038
 * Commit a change to a loose reference that has already been written
2039
 * to the loose reference lockfile. Also update the reflogs if
2040
 * necessary, using the specified lockmsg (which can be NULL).
2041
 */
2042
static int commit_ref_update(struct files_ref_store *refs,
2043
           struct ref_lock *lock,
2044
           const struct object_id *oid, const char *logmsg,
2045
           int flags,
2046
           struct strbuf *err)
2047
0
{
2048
0
  files_assert_main_repository(refs, "commit_ref_update");
2049
2050
0
  clear_loose_ref_cache(refs);
2051
0
  if (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, oid, NULL,
2052
0
        logmsg, flags, err)) {
2053
0
    char *old_msg = strbuf_detach(err, NULL);
2054
0
    strbuf_addf(err, "cannot update the ref '%s': %s",
2055
0
          lock->ref_name, old_msg);
2056
0
    free(old_msg);
2057
0
    unlock_ref(lock);
2058
0
    return -1;
2059
0
  }
2060
2061
0
  if (strcmp(lock->ref_name, "HEAD") != 0) {
2062
    /*
2063
     * Special hack: If a branch is updated directly and HEAD
2064
     * points to it (may happen on the remote side of a push
2065
     * for example) then logically the HEAD reflog should be
2066
     * updated too.
2067
     * A generic solution implies reverse symref information,
2068
     * but finding all symrefs pointing to the given branch
2069
     * would be rather costly for this rare event (the direct
2070
     * update of a branch) to be worth it.  So let's cheat and
2071
     * check with HEAD only which should cover 99% of all usage
2072
     * scenarios (even 100% of the default ones).
2073
     */
2074
0
    int head_flag;
2075
0
    const char *head_ref;
2076
2077
0
    head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2078
0
               RESOLVE_REF_READING,
2079
0
               NULL, &head_flag);
2080
0
    if (head_ref && (head_flag & REF_ISSYMREF) &&
2081
0
        !strcmp(head_ref, lock->ref_name)) {
2082
0
      struct strbuf log_err = STRBUF_INIT;
2083
0
      if (files_log_ref_write(refs, "HEAD", &lock->old_oid,
2084
0
            oid, NULL, logmsg, flags,
2085
0
            &log_err)) {
2086
0
        error("%s", log_err.buf);
2087
0
        strbuf_release(&log_err);
2088
0
      }
2089
0
    }
2090
0
  }
2091
2092
0
  if (commit_ref(lock)) {
2093
0
    strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
2094
0
    unlock_ref(lock);
2095
0
    return -1;
2096
0
  }
2097
2098
0
  unlock_ref(lock);
2099
0
  return 0;
2100
0
}
2101
2102
#if defined(NO_SYMLINK_HEAD) || defined(WITH_BREAKING_CHANGES)
2103
#define create_ref_symlink(a, b) (-1)
2104
#else
2105
static int create_ref_symlink(struct ref_lock *lock, const char *target)
2106
0
{
2107
0
  static int warn_once = 1;
2108
0
  char *ref_path;
2109
0
  int ret = -1;
2110
2111
0
  ref_path = get_locked_file_path(&lock->lk);
2112
0
  unlink(ref_path);
2113
0
  ret = symlink(target, ref_path);
2114
0
  free(ref_path);
2115
2116
0
  if (ret)
2117
0
    fprintf(stderr, "no symlink - falling back to symbolic ref\n");
2118
2119
0
  if (warn_once)
2120
0
    warning(_("'core.preferSymlinkRefs=true' is nominated for removal.\n"
2121
0
        "hint: The use of symbolic links for symbolic refs is deprecated\n"
2122
0
        "hint: and will be removed in Git 3.0. The configuration that\n"
2123
0
        "hint: tells Git to use them is thus going away. You can unset\n"
2124
0
        "hint: it with:\n"
2125
0
        "hint:\n"
2126
0
        "hint:\tgit config unset core.preferSymlinkRefs\n"
2127
0
        "hint:\n"
2128
0
        "hint: Git will then use the textual symref format instead."));
2129
0
  warn_once = 0;
2130
2131
0
  return ret;
2132
0
}
2133
#endif
2134
2135
static int create_symref_lock(struct ref_lock *lock, const char *target,
2136
            struct strbuf *err)
2137
0
{
2138
0
  if (!fdopen_lock_file(&lock->lk, "w")) {
2139
0
    strbuf_addf(err, "unable to fdopen %s: %s",
2140
0
           get_lock_file_path(&lock->lk), strerror(errno));
2141
0
    return -1;
2142
0
  }
2143
2144
0
  if (fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target) < 0) {
2145
0
    strbuf_addf(err, "unable to write to %s: %s",
2146
0
           get_lock_file_path(&lock->lk), strerror(errno));
2147
0
    return -1;
2148
0
  }
2149
2150
0
  return 0;
2151
0
}
2152
2153
static int files_reflog_exists(struct ref_store *ref_store,
2154
             const char *refname)
2155
0
{
2156
0
  struct files_ref_store *refs =
2157
0
    files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
2158
0
  struct strbuf sb = STRBUF_INIT;
2159
0
  struct stat st;
2160
0
  int ret;
2161
2162
0
  files_reflog_path(refs, &sb, refname);
2163
0
  ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
2164
0
  strbuf_release(&sb);
2165
0
  return ret;
2166
0
}
2167
2168
static int files_delete_reflog(struct ref_store *ref_store,
2169
             const char *refname)
2170
0
{
2171
0
  struct files_ref_store *refs =
2172
0
    files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
2173
0
  struct strbuf sb = STRBUF_INIT;
2174
0
  int ret;
2175
2176
0
  files_reflog_path(refs, &sb, refname);
2177
0
  ret = remove_path(sb.buf);
2178
0
  strbuf_release(&sb);
2179
0
  return ret;
2180
0
}
2181
2182
static int show_one_reflog_ent(struct files_ref_store *refs,
2183
             const char *refname,
2184
             struct strbuf *sb,
2185
             each_reflog_ent_fn fn, void *cb_data)
2186
0
{
2187
0
  struct object_id ooid, noid;
2188
0
  char *email_end, *message;
2189
0
  timestamp_t timestamp;
2190
0
  int tz;
2191
0
  const char *p = sb->buf;
2192
2193
  /* old SP new SP name <email> SP time TAB msg LF */
2194
0
  if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
2195
0
      parse_oid_hex_algop(p, &ooid, &p, refs->base.repo->hash_algo) || *p++ != ' ' ||
2196
0
      parse_oid_hex_algop(p, &noid, &p, refs->base.repo->hash_algo) || *p++ != ' ' ||
2197
0
      !(email_end = strchr(p, '>')) ||
2198
0
      email_end[1] != ' ' ||
2199
0
      !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||
2200
0
      !message || message[0] != ' ' ||
2201
0
      (message[1] != '+' && message[1] != '-') ||
2202
0
      !isdigit(message[2]) || !isdigit(message[3]) ||
2203
0
      !isdigit(message[4]) || !isdigit(message[5]))
2204
0
    return 0; /* corrupt? */
2205
0
  email_end[1] = '\0';
2206
0
  tz = strtol(message + 1, NULL, 10);
2207
0
  if (message[6] != '\t')
2208
0
    message += 6;
2209
0
  else
2210
0
    message += 7;
2211
0
  return fn(refname, &ooid, &noid, p, timestamp, tz, message, cb_data);
2212
0
}
2213
2214
static char *find_beginning_of_line(char *bob, char *scan)
2215
0
{
2216
0
  while (bob < scan && *(--scan) != '\n')
2217
0
    ; /* keep scanning backwards */
2218
  /*
2219
   * Return either beginning of the buffer, or LF at the end of
2220
   * the previous line.
2221
   */
2222
0
  return scan;
2223
0
}
2224
2225
static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2226
               const char *refname,
2227
               each_reflog_ent_fn fn,
2228
               void *cb_data)
2229
0
{
2230
0
  struct files_ref_store *refs =
2231
0
    files_downcast(ref_store, REF_STORE_READ,
2232
0
             "for_each_reflog_ent_reverse");
2233
0
  struct strbuf sb = STRBUF_INIT;
2234
0
  FILE *logfp;
2235
0
  long pos;
2236
0
  int ret = 0, at_tail = 1;
2237
2238
0
  files_reflog_path(refs, &sb, refname);
2239
0
  logfp = fopen(sb.buf, "r");
2240
0
  strbuf_release(&sb);
2241
0
  if (!logfp)
2242
0
    return -1;
2243
2244
  /* Jump to the end */
2245
0
  if (fseek(logfp, 0, SEEK_END) < 0)
2246
0
    ret = error("cannot seek back reflog for %s: %s",
2247
0
          refname, strerror(errno));
2248
0
  pos = ftell(logfp);
2249
0
  while (!ret && 0 < pos) {
2250
0
    int cnt;
2251
0
    size_t nread;
2252
0
    char buf[BUFSIZ];
2253
0
    char *endp, *scanp;
2254
2255
    /* Fill next block from the end */
2256
0
    cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
2257
0
    if (fseek(logfp, pos - cnt, SEEK_SET)) {
2258
0
      ret = error("cannot seek back reflog for %s: %s",
2259
0
            refname, strerror(errno));
2260
0
      break;
2261
0
    }
2262
0
    nread = fread(buf, cnt, 1, logfp);
2263
0
    if (nread != 1) {
2264
0
      ret = error("cannot read %d bytes from reflog for %s: %s",
2265
0
            cnt, refname, strerror(errno));
2266
0
      break;
2267
0
    }
2268
0
    pos -= cnt;
2269
2270
0
    scanp = endp = buf + cnt;
2271
0
    if (at_tail && scanp[-1] == '\n')
2272
      /* Looking at the final LF at the end of the file */
2273
0
      scanp--;
2274
0
    at_tail = 0;
2275
2276
0
    while (buf < scanp) {
2277
      /*
2278
       * terminating LF of the previous line, or the beginning
2279
       * of the buffer.
2280
       */
2281
0
      char *bp;
2282
2283
0
      bp = find_beginning_of_line(buf, scanp);
2284
2285
0
      if (*bp == '\n') {
2286
        /*
2287
         * The newline is the end of the previous line,
2288
         * so we know we have complete line starting
2289
         * at (bp + 1). Prefix it onto any prior data
2290
         * we collected for the line and process it.
2291
         */
2292
0
        strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2293
0
        scanp = bp;
2294
0
        endp = bp + 1;
2295
0
        ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
2296
0
        strbuf_reset(&sb);
2297
0
        if (ret)
2298
0
          break;
2299
0
      } else if (!pos) {
2300
        /*
2301
         * We are at the start of the buffer, and the
2302
         * start of the file; there is no previous
2303
         * line, and we have everything for this one.
2304
         * Process it, and we can end the loop.
2305
         */
2306
0
        strbuf_splice(&sb, 0, 0, buf, endp - buf);
2307
0
        ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
2308
0
        strbuf_reset(&sb);
2309
0
        break;
2310
0
      }
2311
2312
0
      if (bp == buf) {
2313
        /*
2314
         * We are at the start of the buffer, and there
2315
         * is more file to read backwards. Which means
2316
         * we are in the middle of a line. Note that we
2317
         * may get here even if *bp was a newline; that
2318
         * just means we are at the exact end of the
2319
         * previous line, rather than some spot in the
2320
         * middle.
2321
         *
2322
         * Save away what we have to be combined with
2323
         * the data from the next read.
2324
         */
2325
0
        strbuf_splice(&sb, 0, 0, buf, endp - buf);
2326
0
        break;
2327
0
      }
2328
0
    }
2329
2330
0
  }
2331
0
  if (!ret && sb.len)
2332
0
    BUG("reverse reflog parser had leftover data");
2333
2334
0
  fclose(logfp);
2335
0
  strbuf_release(&sb);
2336
0
  return ret;
2337
0
}
2338
2339
static int files_for_each_reflog_ent(struct ref_store *ref_store,
2340
             const char *refname,
2341
             each_reflog_ent_fn fn, void *cb_data)
2342
0
{
2343
0
  struct files_ref_store *refs =
2344
0
    files_downcast(ref_store, REF_STORE_READ,
2345
0
             "for_each_reflog_ent");
2346
0
  FILE *logfp;
2347
0
  struct strbuf sb = STRBUF_INIT;
2348
0
  int ret = 0;
2349
2350
0
  files_reflog_path(refs, &sb, refname);
2351
0
  logfp = fopen(sb.buf, "r");
2352
0
  strbuf_release(&sb);
2353
0
  if (!logfp)
2354
0
    return -1;
2355
2356
0
  while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2357
0
    ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
2358
0
  fclose(logfp);
2359
0
  strbuf_release(&sb);
2360
0
  return ret;
2361
0
}
2362
2363
struct files_reflog_iterator {
2364
  struct ref_iterator base;
2365
  struct ref_store *ref_store;
2366
  struct dir_iterator *dir_iterator;
2367
};
2368
2369
static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
2370
0
{
2371
0
  struct files_reflog_iterator *iter =
2372
0
    (struct files_reflog_iterator *)ref_iterator;
2373
0
  struct dir_iterator *diter = iter->dir_iterator;
2374
0
  int ok;
2375
2376
0
  while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2377
0
    if (!S_ISREG(diter->st.st_mode))
2378
0
      continue;
2379
0
    if (check_refname_format(diter->basename,
2380
0
           REFNAME_ALLOW_ONELEVEL))
2381
0
      continue;
2382
2383
0
    iter->base.ref.name = diter->relative_path;
2384
0
    return ITER_OK;
2385
0
  }
2386
2387
0
  return ok;
2388
0
}
2389
2390
static int files_reflog_iterator_seek(struct ref_iterator *ref_iterator UNUSED,
2391
              const char *refname UNUSED,
2392
              unsigned int flags UNUSED)
2393
0
{
2394
0
  BUG("ref_iterator_seek() called for reflog_iterator");
2395
0
}
2396
2397
static void files_reflog_iterator_release(struct ref_iterator *ref_iterator)
2398
0
{
2399
0
  struct files_reflog_iterator *iter =
2400
0
    (struct files_reflog_iterator *)ref_iterator;
2401
0
  dir_iterator_free(iter->dir_iterator);
2402
0
}
2403
2404
static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2405
  .advance = files_reflog_iterator_advance,
2406
  .seek = files_reflog_iterator_seek,
2407
  .release = files_reflog_iterator_release,
2408
};
2409
2410
static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
2411
              const char *gitdir)
2412
0
{
2413
0
  struct dir_iterator *diter;
2414
0
  struct files_reflog_iterator *iter;
2415
0
  struct ref_iterator *ref_iterator;
2416
0
  struct strbuf sb = STRBUF_INIT;
2417
2418
0
  strbuf_addf(&sb, "%s/logs", gitdir);
2419
2420
0
  diter = dir_iterator_begin(sb.buf, DIR_ITERATOR_SORTED);
2421
0
  if (!diter) {
2422
0
    strbuf_release(&sb);
2423
0
    return empty_ref_iterator_begin();
2424
0
  }
2425
2426
0
  CALLOC_ARRAY(iter, 1);
2427
0
  ref_iterator = &iter->base;
2428
2429
0
  base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
2430
0
  iter->dir_iterator = diter;
2431
0
  iter->ref_store = ref_store;
2432
0
  strbuf_release(&sb);
2433
2434
0
  return ref_iterator;
2435
0
}
2436
2437
static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
2438
0
{
2439
0
  struct files_ref_store *refs =
2440
0
    files_downcast(ref_store, REF_STORE_READ,
2441
0
             "reflog_iterator_begin");
2442
2443
0
  if (!strcmp(refs->base.gitdir, refs->gitcommondir)) {
2444
0
    return reflog_iterator_begin(ref_store, refs->gitcommondir);
2445
0
  } else {
2446
0
    return merge_ref_iterator_begin(
2447
0
      reflog_iterator_begin(ref_store, refs->base.gitdir),
2448
0
      reflog_iterator_begin(ref_store, refs->gitcommondir),
2449
0
      ref_iterator_select, refs);
2450
0
  }
2451
0
}
2452
2453
/*
2454
 * If update is a direct update of head_ref (the reference pointed to
2455
 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2456
 */
2457
static enum ref_transaction_error split_head_update(struct ref_update *update,
2458
                struct ref_transaction *transaction,
2459
                const char *head_ref,
2460
                struct strbuf *err)
2461
0
{
2462
0
  struct ref_update *new_update;
2463
2464
0
  if ((update->flags & REF_LOG_ONLY) ||
2465
0
      (update->flags & REF_SKIP_CREATE_REFLOG) ||
2466
0
      (update->flags & REF_IS_PRUNING) ||
2467
0
      (update->flags & REF_UPDATE_VIA_HEAD))
2468
0
    return 0;
2469
2470
0
  if (strcmp(update->refname, head_ref))
2471
0
    return 0;
2472
2473
  /*
2474
   * First make sure that HEAD is not already in the
2475
   * transaction. This check is O(lg N) in the transaction
2476
   * size, but it happens at most once per transaction.
2477
   */
2478
0
  if (string_list_has_string(&transaction->refnames, "HEAD")) {
2479
    /* An entry already existed */
2480
0
    strbuf_addf(err,
2481
0
          "multiple updates for 'HEAD' (including one "
2482
0
          "via its referent '%s') are not allowed",
2483
0
          update->refname);
2484
0
    return REF_TRANSACTION_ERROR_NAME_CONFLICT;
2485
0
  }
2486
2487
0
  new_update = ref_transaction_add_update(
2488
0
      transaction, "HEAD",
2489
0
      update->flags | REF_LOG_ONLY | REF_NO_DEREF | REF_LOG_VIA_SPLIT,
2490
0
      &update->new_oid, &update->old_oid,
2491
0
      NULL, NULL, update->committer_info, update->msg);
2492
0
  new_update->parent_update = update;
2493
2494
  /*
2495
   * Add "HEAD". This insertion is O(N) in the transaction
2496
   * size, but it happens at most once per transaction.
2497
   * Add new_update->refname instead of a literal "HEAD".
2498
   */
2499
0
  if (strcmp(new_update->refname, "HEAD"))
2500
0
    BUG("%s unexpectedly not 'HEAD'", new_update->refname);
2501
2502
0
  return 0;
2503
0
}
2504
2505
/*
2506
 * update is for a symref that points at referent and doesn't have
2507
 * REF_NO_DEREF set. Split it into two updates:
2508
 * - The original update, but with REF_LOG_ONLY and REF_NO_DEREF set
2509
 * - A new, separate update for the referent reference
2510
 * Note that the new update will itself be subject to splitting when
2511
 * the iteration gets to it.
2512
 */
2513
static enum ref_transaction_error split_symref_update(struct ref_update *update,
2514
                  const char *referent,
2515
                  struct ref_transaction *transaction,
2516
                  struct strbuf *err)
2517
0
{
2518
0
  struct ref_update *new_update;
2519
0
  unsigned int new_flags;
2520
2521
  /*
2522
   * First make sure that referent is not already in the
2523
   * transaction. This check is O(lg N) in the transaction
2524
   * size, but it happens at most once per symref in a
2525
   * transaction.
2526
   */
2527
0
  if (string_list_has_string(&transaction->refnames, referent)) {
2528
    /* An entry already exists */
2529
0
    strbuf_addf(err,
2530
0
          "multiple updates for '%s' (including one "
2531
0
          "via symref '%s') are not allowed",
2532
0
          referent, update->refname);
2533
0
    return REF_TRANSACTION_ERROR_NAME_CONFLICT;
2534
0
  }
2535
2536
0
  new_flags = update->flags;
2537
0
  if (!strcmp(update->refname, "HEAD")) {
2538
    /*
2539
     * Record that the new update came via HEAD, so that
2540
     * when we process it, split_head_update() doesn't try
2541
     * to add another reflog update for HEAD. Note that
2542
     * this bit will be propagated if the new_update
2543
     * itself needs to be split.
2544
     */
2545
0
    new_flags |= REF_UPDATE_VIA_HEAD;
2546
0
  }
2547
2548
0
  new_update = ref_transaction_add_update(
2549
0
      transaction, referent, new_flags,
2550
0
      update->new_target ? NULL : &update->new_oid,
2551
0
      update->old_target ? NULL : &update->old_oid,
2552
0
      update->new_target, update->old_target, NULL,
2553
0
      update->msg);
2554
2555
0
  new_update->parent_update = update;
2556
2557
  /*
2558
   * Change the symbolic ref update to log only. Also, it
2559
   * doesn't need to check its old OID value, as that will be
2560
   * done when new_update is processed.
2561
   */
2562
0
  update->flags |= REF_LOG_ONLY | REF_NO_DEREF;
2563
2564
0
  return 0;
2565
0
}
2566
2567
/*
2568
 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2569
 * are consistent with oid, which is the reference's current value. If
2570
 * everything is OK, return 0; otherwise, write an error message to
2571
 * err and return -1.
2572
 */
2573
static enum ref_transaction_error check_old_oid(struct ref_update *update,
2574
            struct object_id *oid,
2575
            struct strbuf *referent,
2576
            struct strbuf *err)
2577
0
{
2578
0
  if (update->flags & REF_LOG_ONLY ||
2579
0
      !(update->flags & REF_HAVE_OLD))
2580
0
    return 0;
2581
2582
0
  if (oideq(oid, &update->old_oid)) {
2583
    /*
2584
     * Normally matching the expected old oid is enough. Either we
2585
     * found the ref at the expected state, or we are creating and
2586
     * expect the null oid (and likewise found nothing).
2587
     *
2588
     * But there is one exception for the null oid: if we found a
2589
     * symref pointing to nothing we'll also get the null oid. In
2590
     * regular recursive mode, that's good (we'll write to what the
2591
     * symref points to, which doesn't exist). But in no-deref
2592
     * mode, it means we'll clobber the symref, even though the
2593
     * caller asked for this to be a creation event. So flag
2594
     * that case to preserve the dangling symref.
2595
     */
2596
0
    if ((update->flags & REF_NO_DEREF) && referent->len &&
2597
0
        is_null_oid(oid)) {
2598
0
      strbuf_addf(err, "cannot lock ref '%s': "
2599
0
            "dangling symref already exists",
2600
0
            ref_update_original_update_refname(update));
2601
0
      return REF_TRANSACTION_ERROR_CREATE_EXISTS;
2602
0
    }
2603
0
    return 0;
2604
0
  }
2605
2606
0
  if (is_null_oid(&update->old_oid)) {
2607
0
    strbuf_addf(err, "cannot lock ref '%s': "
2608
0
          "reference already exists",
2609
0
          ref_update_original_update_refname(update));
2610
0
    return REF_TRANSACTION_ERROR_CREATE_EXISTS;
2611
0
  } else if (is_null_oid(oid)) {
2612
0
    strbuf_addf(err, "cannot lock ref '%s': "
2613
0
          "reference is missing but expected %s",
2614
0
          ref_update_original_update_refname(update),
2615
0
          oid_to_hex(&update->old_oid));
2616
0
    return REF_TRANSACTION_ERROR_NONEXISTENT_REF;
2617
0
  }
2618
2619
0
  strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
2620
0
        ref_update_original_update_refname(update), oid_to_hex(oid),
2621
0
        oid_to_hex(&update->old_oid));
2622
2623
0
  return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;
2624
0
}
2625
2626
struct files_transaction_backend_data {
2627
  struct ref_transaction *packed_transaction;
2628
  int packed_refs_locked;
2629
  struct strmap ref_locks;
2630
};
2631
2632
/*
2633
 * Prepare for carrying out update:
2634
 * - Lock the reference referred to by update.
2635
 * - Read the reference under lock.
2636
 * - Check that its old OID value (if specified) is correct, and in
2637
 *   any case record it in update->lock->old_oid for later use when
2638
 *   writing the reflog.
2639
 * - If it is a symref update without REF_NO_DEREF, split it up into a
2640
 *   REF_LOG_ONLY update of the symref and add a separate update for
2641
 *   the referent to transaction.
2642
 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2643
 *   update of HEAD.
2644
 */
2645
static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *refs,
2646
                  struct ref_update *update,
2647
                  size_t update_idx,
2648
                  struct ref_transaction *transaction,
2649
                  const char *head_ref,
2650
                  struct string_list *refnames_to_check,
2651
                  struct strbuf *err)
2652
0
{
2653
0
  struct strbuf referent = STRBUF_INIT;
2654
0
  int mustexist = ref_update_expects_existing_old_ref(update);
2655
0
  struct files_transaction_backend_data *backend_data;
2656
0
  enum ref_transaction_error ret = 0;
2657
0
  struct ref_lock *lock;
2658
2659
0
  files_assert_main_repository(refs, "lock_ref_for_update");
2660
2661
0
  backend_data = transaction->backend_data;
2662
2663
0
  if ((update->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(update))
2664
0
    update->flags |= REF_DELETING;
2665
2666
0
  if (head_ref) {
2667
0
    ret = split_head_update(update, transaction, head_ref, err);
2668
0
    if (ret)
2669
0
      goto out;
2670
0
  }
2671
2672
0
  lock = strmap_get(&backend_data->ref_locks, update->refname);
2673
0
  if (lock) {
2674
0
    lock->count++;
2675
0
  } else {
2676
0
    ret = lock_raw_ref(refs, transaction, update_idx, mustexist,
2677
0
           refnames_to_check, &lock, &referent, err);
2678
0
    if (ret) {
2679
0
      char *reason;
2680
2681
0
      reason = strbuf_detach(err, NULL);
2682
0
      strbuf_addf(err, "cannot lock ref '%s': %s",
2683
0
            ref_update_original_update_refname(update), reason);
2684
0
      free(reason);
2685
0
      goto out;
2686
0
    }
2687
2688
0
    strmap_put(&backend_data->ref_locks, update->refname, lock);
2689
0
  }
2690
2691
0
  update->backend_data = lock;
2692
2693
0
  if (update->flags & REF_LOG_VIA_SPLIT) {
2694
0
    struct ref_lock *parent_lock;
2695
2696
0
    if (!update->parent_update)
2697
0
      BUG("split update without a parent");
2698
2699
0
    parent_lock = update->parent_update->backend_data;
2700
2701
    /*
2702
     * Check that "HEAD" didn't racily change since we have looked
2703
     * it up. If it did we must refuse to write the reflog entry.
2704
     *
2705
     * Note that this does not catch all races: if "HEAD" was
2706
     * racily changed to point to one of the refs part of the
2707
     * transaction then we would miss writing the split reflog
2708
     * entry for "HEAD".
2709
     */
2710
0
    if (!(update->type & REF_ISSYMREF) ||
2711
0
        strcmp(update->parent_update->refname, referent.buf)) {
2712
0
      strbuf_addstr(err, "HEAD has been racily updated");
2713
0
      ret = REF_TRANSACTION_ERROR_GENERIC;
2714
0
      goto out;
2715
0
    }
2716
2717
0
    if (update->flags & REF_HAVE_OLD) {
2718
0
      oidcpy(&lock->old_oid, &update->old_oid);
2719
0
    } else {
2720
0
      oidcpy(&lock->old_oid, &parent_lock->old_oid);
2721
0
    }
2722
0
  } else if (update->type & REF_ISSYMREF) {
2723
0
    if (update->flags & REF_NO_DEREF) {
2724
      /*
2725
       * We won't be reading the referent as part of
2726
       * the transaction, so we have to read it here
2727
       * to record and possibly check old_oid:
2728
       */
2729
0
      if (!refs_resolve_ref_unsafe(&refs->base,
2730
0
                 referent.buf, 0,
2731
0
                 &lock->old_oid, NULL)) {
2732
0
        if (update->flags & REF_HAVE_OLD) {
2733
0
          strbuf_addf(err, "cannot lock ref '%s': "
2734
0
                "error reading reference",
2735
0
                ref_update_original_update_refname(update));
2736
0
          ret = REF_TRANSACTION_ERROR_GENERIC;
2737
0
          goto out;
2738
0
        }
2739
0
      }
2740
2741
0
      if (update->old_target)
2742
0
        ret = ref_update_check_old_target(referent.buf, update, err);
2743
0
      else
2744
0
        ret = check_old_oid(update, &lock->old_oid,
2745
0
                &referent, err);
2746
0
      if (ret)
2747
0
        goto out;
2748
0
    } else {
2749
      /*
2750
       * Create a new update for the reference this
2751
       * symref is pointing at. Also, we will record
2752
       * and verify old_oid for this update as part
2753
       * of processing the split-off update, so we
2754
       * don't have to do it here.
2755
       */
2756
0
      ret = split_symref_update(update, referent.buf,
2757
0
              transaction, err);
2758
0
      if (ret)
2759
0
        goto out;
2760
0
    }
2761
0
  } else {
2762
0
    struct ref_update *parent_update;
2763
2764
    /*
2765
     * Even if the ref is a regular ref, if `old_target` is set, we
2766
     * fail with an error.
2767
     */
2768
0
    if (update->old_target) {
2769
0
      strbuf_addf(err, _("cannot lock ref '%s': "
2770
0
             "expected symref with target '%s': "
2771
0
             "but is a regular ref"),
2772
0
            ref_update_original_update_refname(update),
2773
0
            update->old_target);
2774
0
      ret = REF_TRANSACTION_ERROR_EXPECTED_SYMREF;
2775
0
      goto out;
2776
0
    } else {
2777
0
      ret = check_old_oid(update, &lock->old_oid,
2778
0
              &referent, err);
2779
0
      if  (ret) {
2780
0
        goto out;
2781
0
      }
2782
0
    }
2783
2784
    /*
2785
     * If this update is happening indirectly because of a
2786
     * symref update, record the old OID in the parent
2787
     * update:
2788
     */
2789
0
    for (parent_update = update->parent_update;
2790
0
         parent_update;
2791
0
         parent_update = parent_update->parent_update) {
2792
0
      struct ref_lock *parent_lock = parent_update->backend_data;
2793
0
      oidcpy(&parent_lock->old_oid, &lock->old_oid);
2794
0
    }
2795
0
  }
2796
2797
0
  if (update->new_target && !(update->flags & REF_LOG_ONLY)) {
2798
0
    if (create_symref_lock(lock, update->new_target, err)) {
2799
0
      ret = REF_TRANSACTION_ERROR_GENERIC;
2800
0
      goto out;
2801
0
    }
2802
2803
0
    if (close_ref_gently(lock)) {
2804
0
      strbuf_addf(err, "couldn't close '%s.lock'",
2805
0
            update->refname);
2806
0
      ret = REF_TRANSACTION_ERROR_GENERIC;
2807
0
      goto out;
2808
0
    }
2809
2810
    /*
2811
     * Once we have created the symref lock, the commit
2812
     * phase of the transaction only needs to commit the lock.
2813
     */
2814
0
    update->flags |= REF_NEEDS_COMMIT;
2815
0
  } else if ((update->flags & REF_HAVE_NEW) &&
2816
0
       !(update->flags & REF_DELETING) &&
2817
0
       !(update->flags & REF_LOG_ONLY)) {
2818
0
    if (!(update->type & REF_ISSYMREF) &&
2819
0
        oideq(&lock->old_oid, &update->new_oid)) {
2820
      /*
2821
       * The reference already has the desired
2822
       * value, so we don't need to write it.
2823
       */
2824
0
    } else {
2825
0
      ret = write_ref_to_lockfile(
2826
0
        refs, lock, &update->new_oid,
2827
0
        update->flags & REF_SKIP_OID_VERIFICATION,
2828
0
        err);
2829
0
      if (ret) {
2830
0
        char *write_err = strbuf_detach(err, NULL);
2831
2832
        /*
2833
         * The lock was freed upon failure of
2834
         * write_ref_to_lockfile():
2835
         */
2836
0
        update->backend_data = NULL;
2837
0
        strbuf_addf(err,
2838
0
              "cannot update ref '%s': %s",
2839
0
              update->refname, write_err);
2840
0
        free(write_err);
2841
0
        goto out;
2842
0
      } else {
2843
0
        update->flags |= REF_NEEDS_COMMIT;
2844
0
      }
2845
0
    }
2846
0
  }
2847
0
  if (!(update->flags & REF_NEEDS_COMMIT)) {
2848
    /*
2849
     * We didn't call write_ref_to_lockfile(), so
2850
     * the lockfile is still open. Close it to
2851
     * free up the file descriptor:
2852
     */
2853
0
    if (close_ref_gently(lock)) {
2854
0
      strbuf_addf(err, "couldn't close '%s.lock'",
2855
0
            update->refname);
2856
0
      ret = REF_TRANSACTION_ERROR_GENERIC;
2857
0
      goto out;
2858
0
    }
2859
0
  }
2860
2861
0
out:
2862
0
  strbuf_release(&referent);
2863
0
  return ret;
2864
0
}
2865
2866
/*
2867
 * Unlock any references in `transaction` that are still locked, and
2868
 * mark the transaction closed.
2869
 */
2870
static void files_transaction_cleanup(struct files_ref_store *refs,
2871
              struct ref_transaction *transaction)
2872
0
{
2873
0
  size_t i;
2874
0
  struct files_transaction_backend_data *backend_data =
2875
0
    transaction->backend_data;
2876
0
  struct strbuf err = STRBUF_INIT;
2877
2878
0
  for (i = 0; i < transaction->nr; i++) {
2879
0
    struct ref_update *update = transaction->updates[i];
2880
0
    struct ref_lock *lock = update->backend_data;
2881
2882
0
    if (lock) {
2883
0
      unlock_ref(lock);
2884
0
      try_remove_empty_parents(refs, update->refname,
2885
0
             REMOVE_EMPTY_PARENTS_REF);
2886
0
      update->backend_data = NULL;
2887
0
    }
2888
0
  }
2889
2890
0
  if (backend_data) {
2891
0
    if (backend_data->packed_transaction &&
2892
0
        ref_transaction_abort(backend_data->packed_transaction, &err)) {
2893
0
      error("error aborting transaction: %s", err.buf);
2894
0
      strbuf_release(&err);
2895
0
    }
2896
2897
0
    if (backend_data->packed_refs_locked)
2898
0
      packed_refs_unlock(refs->packed_ref_store);
2899
2900
0
    strmap_clear(&backend_data->ref_locks, 0);
2901
2902
0
    free(backend_data);
2903
0
  }
2904
2905
0
  transaction->state = REF_TRANSACTION_CLOSED;
2906
0
}
2907
2908
static int files_transaction_prepare(struct ref_store *ref_store,
2909
             struct ref_transaction *transaction,
2910
             struct strbuf *err)
2911
0
{
2912
0
  struct files_ref_store *refs =
2913
0
    files_downcast(ref_store, REF_STORE_WRITE,
2914
0
             "ref_transaction_prepare");
2915
0
  size_t i;
2916
0
  int ret = 0;
2917
0
  struct string_list refnames_to_check = STRING_LIST_INIT_DUP;
2918
0
  char *head_ref = NULL;
2919
0
  int head_type;
2920
0
  struct files_transaction_backend_data *backend_data;
2921
0
  struct ref_transaction *packed_transaction = NULL;
2922
2923
0
  assert(err);
2924
2925
0
  if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
2926
0
    goto cleanup;
2927
0
  if (!transaction->nr)
2928
0
    goto cleanup;
2929
2930
0
  CALLOC_ARRAY(backend_data, 1);
2931
0
  strmap_init(&backend_data->ref_locks);
2932
0
  transaction->backend_data = backend_data;
2933
2934
  /*
2935
   * Fail if any of the updates use REF_IS_PRUNING without REF_NO_DEREF.
2936
   */
2937
0
  for (i = 0; i < transaction->nr; i++) {
2938
0
    struct ref_update *update = transaction->updates[i];
2939
2940
0
    if ((update->flags & REF_IS_PRUNING) &&
2941
0
        !(update->flags & REF_NO_DEREF))
2942
0
      BUG("REF_IS_PRUNING set without REF_NO_DEREF");
2943
0
  }
2944
2945
  /*
2946
   * Special hack: If a branch is updated directly and HEAD
2947
   * points to it (may happen on the remote side of a push
2948
   * for example) then logically the HEAD reflog should be
2949
   * updated too.
2950
   *
2951
   * A generic solution would require reverse symref lookups,
2952
   * but finding all symrefs pointing to a given branch would be
2953
   * rather costly for this rare event (the direct update of a
2954
   * branch) to be worth it. So let's cheat and check with HEAD
2955
   * only, which should cover 99% of all usage scenarios (even
2956
   * 100% of the default ones).
2957
   *
2958
   * So if HEAD is a symbolic reference, then record the name of
2959
   * the reference that it points to. If we see an update of
2960
   * head_ref within the transaction, then split_head_update()
2961
   * arranges for the reflog of HEAD to be updated, too.
2962
   */
2963
0
  head_ref = refs_resolve_refdup(ref_store, "HEAD",
2964
0
               RESOLVE_REF_NO_RECURSE,
2965
0
               NULL, &head_type);
2966
2967
0
  if (head_ref && !(head_type & REF_ISSYMREF)) {
2968
0
    FREE_AND_NULL(head_ref);
2969
0
  }
2970
2971
  /*
2972
   * Acquire all locks, verify old values if provided, check
2973
   * that new values are valid, and write new values to the
2974
   * lockfiles, ready to be activated. Only keep one lockfile
2975
   * open at a time to avoid running out of file descriptors.
2976
   * Note that lock_ref_for_update() might append more updates
2977
   * to the transaction.
2978
   */
2979
0
  for (i = 0; i < transaction->nr; i++) {
2980
0
    struct ref_update *update = transaction->updates[i];
2981
2982
0
    ret = lock_ref_for_update(refs, update, i, transaction,
2983
0
            head_ref, &refnames_to_check,
2984
0
            err);
2985
0
    if (ret) {
2986
0
      if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
2987
0
        strbuf_reset(err);
2988
0
        ret = 0;
2989
2990
0
        continue;
2991
0
      }
2992
0
      goto cleanup;
2993
0
    }
2994
2995
0
    if (update->flags & REF_DELETING &&
2996
0
        !(update->flags & REF_LOG_ONLY) &&
2997
0
        !(update->flags & REF_IS_PRUNING)) {
2998
      /*
2999
       * This reference has to be deleted from
3000
       * packed-refs if it exists there.
3001
       */
3002
0
      if (!packed_transaction) {
3003
0
        packed_transaction = ref_store_transaction_begin(
3004
0
            refs->packed_ref_store,
3005
0
            transaction->flags, err);
3006
0
        if (!packed_transaction) {
3007
0
          ret = REF_TRANSACTION_ERROR_GENERIC;
3008
0
          goto cleanup;
3009
0
        }
3010
3011
0
        backend_data->packed_transaction =
3012
0
          packed_transaction;
3013
0
      }
3014
3015
0
      ref_transaction_add_update(
3016
0
          packed_transaction, update->refname,
3017
0
          REF_HAVE_NEW | REF_NO_DEREF,
3018
0
          &update->new_oid, NULL,
3019
0
          NULL, NULL, NULL, NULL);
3020
0
    }
3021
0
  }
3022
3023
  /*
3024
   * Verify that none of the loose reference that we're about to write
3025
   * conflict with any existing packed references. Ideally, we'd do this
3026
   * check after the packed-refs are locked so that the file cannot
3027
   * change underneath our feet. But introducing such a lock now would
3028
   * probably do more harm than good as users rely on there not being a
3029
   * global lock with the "files" backend.
3030
   *
3031
   * Another alternative would be to do the check after the (optional)
3032
   * lock, but that would extend the time we spend in the globally-locked
3033
   * state.
3034
   *
3035
   * So instead, we accept the race for now.
3036
   */
3037
0
  if (refs_verify_refnames_available(refs->packed_ref_store, &refnames_to_check,
3038
0
             &transaction->refnames, NULL, transaction,
3039
0
             0, err)) {
3040
0
    ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
3041
0
    goto cleanup;
3042
0
  }
3043
3044
0
  if (packed_transaction) {
3045
0
    if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
3046
0
      ret = REF_TRANSACTION_ERROR_GENERIC;
3047
0
      goto cleanup;
3048
0
    }
3049
0
    backend_data->packed_refs_locked = 1;
3050
3051
0
    if (is_packed_transaction_needed(refs->packed_ref_store,
3052
0
             packed_transaction)) {
3053
0
      ret = ref_transaction_prepare(packed_transaction, err);
3054
      /*
3055
       * A failure during the prepare step will abort
3056
       * itself, but not free. Do that now, and disconnect
3057
       * from the files_transaction so it does not try to
3058
       * abort us when we hit the cleanup code below.
3059
       */
3060
0
      if (ret) {
3061
0
        ref_transaction_free(packed_transaction);
3062
0
        backend_data->packed_transaction = NULL;
3063
0
      }
3064
0
    } else {
3065
      /*
3066
       * We can skip rewriting the `packed-refs`
3067
       * file. But we do need to leave it locked, so
3068
       * that somebody else doesn't pack a reference
3069
       * that we are trying to delete.
3070
       *
3071
       * We need to disconnect our transaction from
3072
       * backend_data, since the abort (whether successful or
3073
       * not) will free it.
3074
       */
3075
0
      backend_data->packed_transaction = NULL;
3076
0
      if (ref_transaction_abort(packed_transaction, err)) {
3077
0
        ret = REF_TRANSACTION_ERROR_GENERIC;
3078
0
        goto cleanup;
3079
0
      }
3080
0
    }
3081
0
  }
3082
3083
0
cleanup:
3084
0
  free(head_ref);
3085
0
  string_list_clear(&refnames_to_check, 1);
3086
3087
0
  if (ret)
3088
0
    files_transaction_cleanup(refs, transaction);
3089
0
  else
3090
0
    transaction->state = REF_TRANSACTION_PREPARED;
3091
3092
0
  return ret;
3093
0
}
3094
3095
static int parse_and_write_reflog(struct files_ref_store *refs,
3096
          struct ref_update *update,
3097
          struct ref_lock *lock,
3098
          struct strbuf *err)
3099
0
{
3100
0
  struct object_id *old_oid = &lock->old_oid;
3101
3102
0
  if (update->flags & REF_LOG_USE_PROVIDED_OIDS) {
3103
0
    if (!(update->flags & REF_HAVE_OLD) ||
3104
0
        !(update->flags & REF_HAVE_NEW) ||
3105
0
        !(update->flags & REF_LOG_ONLY)) {
3106
0
      strbuf_addf(err, _("trying to write reflog for '%s' "
3107
0
             "with incomplete values"), update->refname);
3108
0
      return REF_TRANSACTION_ERROR_GENERIC;
3109
0
    }
3110
3111
0
    old_oid = &update->old_oid;
3112
0
  }
3113
3114
0
  if (update->new_target) {
3115
    /*
3116
     * We want to get the resolved OID for the target, to ensure
3117
     * that the correct value is added to the reflog.
3118
     */
3119
0
    if (!refs_resolve_ref_unsafe(&refs->base, update->new_target,
3120
0
               RESOLVE_REF_READING,
3121
0
               &update->new_oid, NULL)) {
3122
      /*
3123
       * TODO: currently we skip creating reflogs for dangling
3124
       * symref updates. It would be nice to capture this as
3125
       * zero oid updates however.
3126
       */
3127
0
      return 0;
3128
0
    }
3129
0
  }
3130
3131
0
  if (files_log_ref_write(refs, lock->ref_name, old_oid,
3132
0
        &update->new_oid, update->committer_info,
3133
0
        update->msg, update->flags, err)) {
3134
0
    char *old_msg = strbuf_detach(err, NULL);
3135
3136
0
    strbuf_addf(err, "cannot update the ref '%s': %s",
3137
0
          lock->ref_name, old_msg);
3138
0
    free(old_msg);
3139
0
    unlock_ref(lock);
3140
0
    update->backend_data = NULL;
3141
0
    return -1;
3142
0
  }
3143
3144
0
  return 0;
3145
0
}
3146
3147
static int ref_present(const struct reference *ref, void *cb_data)
3148
0
{
3149
0
  struct string_list *affected_refnames = cb_data;
3150
3151
0
  return string_list_has_string(affected_refnames, ref->name);
3152
0
}
3153
3154
static int files_transaction_finish_initial(struct files_ref_store *refs,
3155
              struct ref_transaction *transaction,
3156
              struct strbuf *err)
3157
0
{
3158
0
  size_t i;
3159
0
  int ret = 0;
3160
0
  struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
3161
0
  struct string_list refnames_to_check = STRING_LIST_INIT_NODUP;
3162
0
  struct ref_transaction *packed_transaction = NULL;
3163
0
  struct ref_transaction *loose_transaction = NULL;
3164
3165
0
  assert(err);
3166
3167
0
  if (transaction->state != REF_TRANSACTION_PREPARED)
3168
0
    BUG("commit called for transaction that is not prepared");
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
   */
3182
0
  if (refs_for_each_rawref(&refs->base, ref_present,
3183
0
         &transaction->refnames))
3184
0
    BUG("initial ref transaction called with existing refs");
3185
3186
0
  packed_transaction = ref_store_transaction_begin(refs->packed_ref_store,
3187
0
               transaction->flags, err);
3188
0
  if (!packed_transaction) {
3189
0
    ret = REF_TRANSACTION_ERROR_GENERIC;
3190
0
    goto cleanup;
3191
0
  }
3192
3193
0
  for (i = 0; i < transaction->nr; i++) {
3194
0
    struct ref_update *update = transaction->updates[i];
3195
3196
0
    if (!(update->flags & REF_LOG_ONLY) &&
3197
0
        (update->flags & REF_HAVE_OLD) &&
3198
0
        !is_null_oid(&update->old_oid))
3199
0
      BUG("initial ref transaction with old_sha1 set");
3200
3201
0
    string_list_append(&refnames_to_check, update->refname);
3202
3203
    /*
3204
     * packed-refs don't support symbolic refs, root refs and reflogs,
3205
     * so we have to queue these references via the loose transaction.
3206
     */
3207
0
    if (update->new_target ||
3208
0
        is_root_ref(update->refname) ||
3209
0
        (update->flags & REF_LOG_ONLY)) {
3210
0
      if (!loose_transaction) {
3211
0
        loose_transaction = ref_store_transaction_begin(&refs->base, 0, err);
3212
0
        if (!loose_transaction) {
3213
0
          ret = REF_TRANSACTION_ERROR_GENERIC;
3214
0
          goto cleanup;
3215
0
        }
3216
0
      }
3217
3218
0
      if (update->flags & REF_LOG_ONLY)
3219
0
        ref_transaction_add_update(loose_transaction, update->refname,
3220
0
                 update->flags, &update->new_oid,
3221
0
                 &update->old_oid, NULL, NULL,
3222
0
                 update->committer_info, update->msg);
3223
0
      else
3224
0
        ref_transaction_add_update(loose_transaction, update->refname,
3225
0
                 update->flags & ~REF_HAVE_OLD,
3226
0
                 update->new_target ? NULL : &update->new_oid, NULL,
3227
0
                 update->new_target, NULL, update->committer_info,
3228
0
                 NULL);
3229
0
    } else {
3230
0
      ref_transaction_add_update(packed_transaction, update->refname,
3231
0
               update->flags & ~REF_HAVE_OLD,
3232
0
               &update->new_oid, &update->old_oid,
3233
0
               NULL, NULL, update->committer_info, NULL);
3234
0
    }
3235
0
  }
3236
3237
0
  if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
3238
0
    ret = REF_TRANSACTION_ERROR_GENERIC;
3239
0
    goto cleanup;
3240
0
  }
3241
3242
0
  if (refs_verify_refnames_available(&refs->base, &refnames_to_check,
3243
0
             &affected_refnames, NULL, transaction,
3244
0
             1, err)) {
3245
0
    packed_refs_unlock(refs->packed_ref_store);
3246
0
    ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
3247
0
    goto cleanup;
3248
0
  }
3249
3250
0
  if (ref_transaction_commit(packed_transaction, err)) {
3251
0
    ret = REF_TRANSACTION_ERROR_GENERIC;
3252
0
    goto cleanup;
3253
0
  }
3254
0
  packed_refs_unlock(refs->packed_ref_store);
3255
3256
0
  if (loose_transaction) {
3257
0
    if (ref_transaction_prepare(loose_transaction, err) ||
3258
0
        ref_transaction_commit(loose_transaction, err)) {
3259
0
      ret = REF_TRANSACTION_ERROR_GENERIC;
3260
0
      goto cleanup;
3261
0
    }
3262
0
  }
3263
3264
0
cleanup:
3265
0
  if (loose_transaction)
3266
0
    ref_transaction_free(loose_transaction);
3267
0
  if (packed_transaction)
3268
0
    ref_transaction_free(packed_transaction);
3269
0
  transaction->state = REF_TRANSACTION_CLOSED;
3270
0
  string_list_clear(&affected_refnames, 0);
3271
0
  string_list_clear(&refnames_to_check, 0);
3272
0
  return ret;
3273
0
}
3274
3275
static int files_transaction_finish(struct ref_store *ref_store,
3276
            struct ref_transaction *transaction,
3277
            struct strbuf *err)
3278
0
{
3279
0
  struct files_ref_store *refs =
3280
0
    files_downcast(ref_store, 0, "ref_transaction_finish");
3281
0
  size_t i;
3282
0
  int ret = 0;
3283
0
  struct strbuf sb = STRBUF_INIT;
3284
0
  struct files_transaction_backend_data *backend_data;
3285
0
  struct ref_transaction *packed_transaction;
3286
3287
3288
0
  assert(err);
3289
3290
0
  if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
3291
0
    return files_transaction_finish_initial(refs, transaction, err);
3292
0
  if (!transaction->nr) {
3293
0
    transaction->state = REF_TRANSACTION_CLOSED;
3294
0
    return 0;
3295
0
  }
3296
3297
0
  backend_data = transaction->backend_data;
3298
0
  packed_transaction = backend_data->packed_transaction;
3299
3300
  /* Perform updates first so live commits remain referenced */
3301
0
  for (i = 0; i < transaction->nr; i++) {
3302
0
    struct ref_update *update = transaction->updates[i];
3303
0
    struct ref_lock *lock = update->backend_data;
3304
3305
0
    if (update->rejection_err)
3306
0
      continue;
3307
3308
0
    if (update->flags & REF_NEEDS_COMMIT ||
3309
0
        update->flags & REF_LOG_ONLY) {
3310
0
      if (parse_and_write_reflog(refs, update, lock, err)) {
3311
0
        ret = REF_TRANSACTION_ERROR_GENERIC;
3312
0
        goto cleanup;
3313
0
      }
3314
0
    }
3315
3316
    /*
3317
     * We try creating a symlink, if that succeeds we continue to the
3318
     * next update. If not, we try and create a regular symref.
3319
     */
3320
0
    if (update->new_target && refs->prefer_symlink_refs)
3321
      /*
3322
       * By using the `NOT_CONSTANT()` trick, we can avoid
3323
       * errors by `clang`'s `-Wunreachable` logic that would
3324
       * report that the `continue` statement is not reachable
3325
       * when `NO_SYMLINK_HEAD` is `#define`d.
3326
       */
3327
0
      if (NOT_CONSTANT(!create_ref_symlink(lock, update->new_target)))
3328
0
        continue;
3329
3330
0
    if (update->flags & REF_NEEDS_COMMIT) {
3331
0
      clear_loose_ref_cache(refs);
3332
0
      if (commit_ref(lock)) {
3333
0
        strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3334
0
        unlock_ref(lock);
3335
0
        update->backend_data = NULL;
3336
0
        ret = REF_TRANSACTION_ERROR_GENERIC;
3337
0
        goto cleanup;
3338
0
      }
3339
0
    }
3340
0
  }
3341
3342
  /*
3343
   * Now that updates are safely completed, we can perform
3344
   * deletes. First delete the reflogs of any references that
3345
   * will be deleted, since (in the unexpected event of an
3346
   * error) leaving a reference without a reflog is less bad
3347
   * than leaving a reflog without a reference (the latter is a
3348
   * mildly invalid repository state):
3349
   */
3350
0
  for (i = 0; i < transaction->nr; i++) {
3351
0
    struct ref_update *update = transaction->updates[i];
3352
3353
0
    if (update->rejection_err)
3354
0
      continue;
3355
3356
0
    if (update->flags & REF_DELETING &&
3357
0
        !(update->flags & REF_LOG_ONLY) &&
3358
0
        !(update->flags & REF_IS_PRUNING)) {
3359
0
      strbuf_reset(&sb);
3360
0
      files_reflog_path(refs, &sb, update->refname);
3361
0
      if (!unlink_or_warn(sb.buf))
3362
0
        try_remove_empty_parents(refs, update->refname,
3363
0
               REMOVE_EMPTY_PARENTS_REFLOG);
3364
0
    }
3365
0
  }
3366
3367
  /*
3368
   * Perform deletes now that updates are safely completed.
3369
   *
3370
   * First delete any packed versions of the references, while
3371
   * retaining the packed-refs lock:
3372
   */
3373
0
  if (packed_transaction) {
3374
0
    ret = ref_transaction_commit(packed_transaction, err);
3375
0
    ref_transaction_free(packed_transaction);
3376
0
    packed_transaction = NULL;
3377
0
    backend_data->packed_transaction = NULL;
3378
0
    if (ret)
3379
0
      goto cleanup;
3380
0
  }
3381
3382
  /* Now delete the loose versions of the references: */
3383
0
  for (i = 0; i < transaction->nr; i++) {
3384
0
    struct ref_update *update = transaction->updates[i];
3385
0
    struct ref_lock *lock = update->backend_data;
3386
3387
0
    if (update->rejection_err)
3388
0
      continue;
3389
3390
0
    if (update->flags & REF_DELETING &&
3391
0
        !(update->flags & REF_LOG_ONLY)) {
3392
0
      update->flags |= REF_DELETED_RMDIR;
3393
0
      if (!(update->type & REF_ISPACKED) ||
3394
0
          update->type & REF_ISSYMREF) {
3395
        /* It is a loose reference. */
3396
0
        strbuf_reset(&sb);
3397
0
        files_ref_path(refs, &sb, lock->ref_name);
3398
0
        if (unlink_or_msg(sb.buf, err)) {
3399
0
          ret = REF_TRANSACTION_ERROR_GENERIC;
3400
0
          goto cleanup;
3401
0
        }
3402
0
      }
3403
0
    }
3404
0
  }
3405
3406
0
  clear_loose_ref_cache(refs);
3407
3408
0
cleanup:
3409
0
  files_transaction_cleanup(refs, transaction);
3410
3411
0
  for (i = 0; i < transaction->nr; i++) {
3412
0
    struct ref_update *update = transaction->updates[i];
3413
3414
0
    if (update->flags & REF_DELETED_RMDIR) {
3415
      /*
3416
       * The reference was deleted. Delete any
3417
       * empty parent directories. (Note that this
3418
       * can only work because we have already
3419
       * removed the lockfile.)
3420
       */
3421
0
      try_remove_empty_parents(refs, update->refname,
3422
0
             REMOVE_EMPTY_PARENTS_REF);
3423
0
    }
3424
0
  }
3425
3426
0
  strbuf_release(&sb);
3427
0
  return ret;
3428
0
}
3429
3430
static int files_transaction_abort(struct ref_store *ref_store,
3431
           struct ref_transaction *transaction,
3432
           struct strbuf *err UNUSED)
3433
0
{
3434
0
  struct files_ref_store *refs =
3435
0
    files_downcast(ref_store, 0, "ref_transaction_abort");
3436
3437
0
  files_transaction_cleanup(refs, transaction);
3438
0
  return 0;
3439
0
}
3440
3441
struct expire_reflog_cb {
3442
  reflog_expiry_should_prune_fn *should_prune_fn;
3443
  void *policy_cb;
3444
  FILE *newlog;
3445
  struct object_id last_kept_oid;
3446
  unsigned int rewrite:1,
3447
         dry_run:1;
3448
};
3449
3450
static int expire_reflog_ent(const char *refname UNUSED,
3451
           struct object_id *ooid, struct object_id *noid,
3452
           const char *email, timestamp_t timestamp, int tz,
3453
           const char *message, void *cb_data)
3454
0
{
3455
0
  struct expire_reflog_cb *cb = cb_data;
3456
0
  reflog_expiry_should_prune_fn *fn = cb->should_prune_fn;
3457
3458
0
  if (cb->rewrite)
3459
0
    ooid = &cb->last_kept_oid;
3460
3461
0
  if (fn(ooid, noid, email, timestamp, tz, message, cb->policy_cb))
3462
0
    return 0;
3463
3464
0
  if (cb->dry_run)
3465
0
    return 0; /* --dry-run */
3466
3467
0
  fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s", oid_to_hex(ooid),
3468
0
    oid_to_hex(noid), email, timestamp, tz, message);
3469
0
  oidcpy(&cb->last_kept_oid, noid);
3470
3471
0
  return 0;
3472
0
}
3473
3474
static int files_reflog_expire(struct ref_store *ref_store,
3475
             const char *refname,
3476
             unsigned int expire_flags,
3477
             reflog_expiry_prepare_fn prepare_fn,
3478
             reflog_expiry_should_prune_fn should_prune_fn,
3479
             reflog_expiry_cleanup_fn cleanup_fn,
3480
             void *policy_cb_data)
3481
0
{
3482
0
  struct files_ref_store *refs =
3483
0
    files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
3484
0
  struct lock_file reflog_lock = LOCK_INIT;
3485
0
  struct expire_reflog_cb cb;
3486
0
  struct ref_lock *lock;
3487
0
  struct strbuf log_file_sb = STRBUF_INIT;
3488
0
  char *log_file;
3489
0
  int status = 0;
3490
0
  struct strbuf err = STRBUF_INIT;
3491
0
  const struct object_id *oid;
3492
3493
0
  memset(&cb, 0, sizeof(cb));
3494
0
  cb.rewrite = !!(expire_flags & EXPIRE_REFLOGS_REWRITE);
3495
0
  cb.dry_run = !!(expire_flags & EXPIRE_REFLOGS_DRY_RUN);
3496
0
  cb.policy_cb = policy_cb_data;
3497
0
  cb.should_prune_fn = should_prune_fn;
3498
3499
  /*
3500
   * The reflog file is locked by holding the lock on the
3501
   * reference itself, plus we might need to update the
3502
   * reference if --updateref was specified:
3503
   */
3504
0
  lock = lock_ref_oid_basic(refs, refname, &err);
3505
0
  if (!lock) {
3506
0
    error("cannot lock ref '%s': %s", refname, err.buf);
3507
0
    strbuf_release(&err);
3508
0
    return -1;
3509
0
  }
3510
0
  oid = &lock->old_oid;
3511
3512
  /*
3513
   * When refs are deleted, their reflog is deleted before the
3514
   * ref itself is deleted. This is because there is no separate
3515
   * lock for reflog; instead we take a lock on the ref with
3516
   * lock_ref_oid_basic().
3517
   *
3518
   * If a race happens and the reflog doesn't exist after we've
3519
   * acquired the lock that's OK. We've got nothing more to do;
3520
   * We were asked to delete the reflog, but someone else
3521
   * deleted it! The caller doesn't care that we deleted it,
3522
   * just that it is deleted. So we can return successfully.
3523
   */
3524
0
  if (!refs_reflog_exists(ref_store, refname)) {
3525
0
    unlock_ref(lock);
3526
0
    return 0;
3527
0
  }
3528
3529
0
  files_reflog_path(refs, &log_file_sb, refname);
3530
0
  log_file = strbuf_detach(&log_file_sb, NULL);
3531
0
  if (!cb.dry_run) {
3532
    /*
3533
     * Even though holding $GIT_DIR/logs/$reflog.lock has
3534
     * no locking implications, we use the lock_file
3535
     * machinery here anyway because it does a lot of the
3536
     * work we need, including cleaning up if the program
3537
     * exits unexpectedly.
3538
     */
3539
0
    if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3540
0
      struct strbuf err = STRBUF_INIT;
3541
0
      unable_to_lock_message(log_file, errno, &err);
3542
0
      error("%s", err.buf);
3543
0
      strbuf_release(&err);
3544
0
      goto failure;
3545
0
    }
3546
0
    cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3547
0
    if (!cb.newlog) {
3548
0
      error("cannot fdopen %s (%s)",
3549
0
            get_lock_file_path(&reflog_lock), strerror(errno));
3550
0
      goto failure;
3551
0
    }
3552
0
  }
3553
3554
0
  (*prepare_fn)(refname, oid, cb.policy_cb);
3555
0
  refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
3556
0
  (*cleanup_fn)(cb.policy_cb);
3557
3558
0
  if (!cb.dry_run) {
3559
    /*
3560
     * It doesn't make sense to adjust a reference pointed
3561
     * to by a symbolic ref based on expiring entries in
3562
     * the symbolic reference's reflog. Nor can we update
3563
     * a reference if there are no remaining reflog
3564
     * entries.
3565
     */
3566
0
    int update = 0;
3567
3568
0
    if ((expire_flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3569
0
        !is_null_oid(&cb.last_kept_oid)) {
3570
0
      int type;
3571
0
      const char *ref;
3572
3573
0
      ref = refs_resolve_ref_unsafe(&refs->base, refname,
3574
0
                  RESOLVE_REF_NO_RECURSE,
3575
0
                  NULL, &type);
3576
0
      update = !!(ref && !(type & REF_ISSYMREF));
3577
0
    }
3578
3579
0
    if (close_lock_file_gently(&reflog_lock)) {
3580
0
      status |= error("couldn't write %s: %s", log_file,
3581
0
          strerror(errno));
3582
0
      rollback_lock_file(&reflog_lock);
3583
0
    } else if (update &&
3584
0
         (write_in_full(get_lock_file_fd(&lock->lk),
3585
0
        oid_to_hex(&cb.last_kept_oid), refs->base.repo->hash_algo->hexsz) < 0 ||
3586
0
          write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
3587
0
          close_ref_gently(lock) < 0)) {
3588
0
      status |= error("couldn't write %s",
3589
0
          get_lock_file_path(&lock->lk));
3590
0
      rollback_lock_file(&reflog_lock);
3591
0
    } else if (commit_lock_file(&reflog_lock)) {
3592
0
      status |= error("unable to write reflog '%s' (%s)",
3593
0
          log_file, strerror(errno));
3594
0
    } else if (update && commit_ref(lock)) {
3595
0
      status |= error("couldn't set %s", lock->ref_name);
3596
0
    }
3597
0
  }
3598
0
  free(log_file);
3599
0
  unlock_ref(lock);
3600
0
  return status;
3601
3602
0
 failure:
3603
0
  rollback_lock_file(&reflog_lock);
3604
0
  free(log_file);
3605
0
  unlock_ref(lock);
3606
0
  return -1;
3607
0
}
3608
3609
static int files_ref_store_create_on_disk(struct ref_store *ref_store,
3610
            int flags,
3611
            struct strbuf *err UNUSED)
3612
0
{
3613
0
  struct files_ref_store *refs =
3614
0
    files_downcast(ref_store, REF_STORE_WRITE, "create");
3615
0
  struct strbuf sb = STRBUF_INIT;
3616
3617
  /*
3618
   * We need to create a "refs" dir in any case so that older versions of
3619
   * Git can tell that this is a repository. This serves two main purposes:
3620
   *
3621
   * - Clients will know to stop walking the parent-directory chain when
3622
   *   detecting the Git repository. Otherwise they may end up detecting
3623
   *   a Git repository in a parent directory instead.
3624
   *
3625
   * - Instead of failing to detect a repository with unknown reference
3626
   *   format altogether, old clients will print an error saying that
3627
   *   they do not understand the reference format extension.
3628
   */
3629
0
  strbuf_addf(&sb, "%s/refs", ref_store->gitdir);
3630
0
  safe_create_dir(the_repository, sb.buf, 1);
3631
0
  adjust_shared_perm(the_repository, sb.buf);
3632
3633
  /*
3634
   * There is no need to create directories for common refs when creating
3635
   * a worktree ref store.
3636
   */
3637
0
  if (!(flags & REF_STORE_CREATE_ON_DISK_IS_WORKTREE)) {
3638
    /*
3639
     * Create .git/refs/{heads,tags}
3640
     */
3641
0
    strbuf_reset(&sb);
3642
0
    files_ref_path(refs, &sb, "refs/heads");
3643
0
    safe_create_dir(the_repository, sb.buf, 1);
3644
3645
0
    strbuf_reset(&sb);
3646
0
    files_ref_path(refs, &sb, "refs/tags");
3647
0
    safe_create_dir(the_repository, sb.buf, 1);
3648
0
  }
3649
3650
0
  strbuf_release(&sb);
3651
0
  return 0;
3652
0
}
3653
3654
struct remove_one_root_ref_data {
3655
  const char *gitdir;
3656
  struct strbuf *err;
3657
};
3658
3659
static int remove_one_root_ref(const char *refname,
3660
             void *cb_data)
3661
0
{
3662
0
  struct remove_one_root_ref_data *data = cb_data;
3663
0
  struct strbuf buf = STRBUF_INIT;
3664
0
  int ret = 0;
3665
3666
0
  strbuf_addf(&buf, "%s/%s", data->gitdir, refname);
3667
3668
0
  ret = unlink(buf.buf);
3669
0
  if (ret < 0)
3670
0
    strbuf_addf(data->err, "could not delete %s: %s\n",
3671
0
          refname, strerror(errno));
3672
3673
0
  strbuf_release(&buf);
3674
0
  return ret;
3675
0
}
3676
3677
static int files_ref_store_remove_on_disk(struct ref_store *ref_store,
3678
            struct strbuf *err)
3679
0
{
3680
0
  struct files_ref_store *refs =
3681
0
    files_downcast(ref_store, REF_STORE_WRITE, "remove");
3682
0
  struct remove_one_root_ref_data data = {
3683
0
    .gitdir = refs->base.gitdir,
3684
0
    .err = err,
3685
0
  };
3686
0
  struct strbuf sb = STRBUF_INIT;
3687
0
  int ret = 0;
3688
3689
0
  strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
3690
0
  if (remove_dir_recursively(&sb, 0) < 0) {
3691
0
    strbuf_addf(err, "could not delete refs: %s",
3692
0
          strerror(errno));
3693
0
    ret = -1;
3694
0
  }
3695
0
  strbuf_reset(&sb);
3696
3697
0
  strbuf_addf(&sb, "%s/logs", refs->base.gitdir);
3698
0
  if (remove_dir_recursively(&sb, 0) < 0) {
3699
0
    strbuf_addf(err, "could not delete logs: %s",
3700
0
          strerror(errno));
3701
0
    ret = -1;
3702
0
  }
3703
0
  strbuf_reset(&sb);
3704
3705
0
  if (for_each_root_ref(refs, remove_one_root_ref, &data) < 0)
3706
0
    ret = -1;
3707
3708
0
  if (ref_store_remove_on_disk(refs->packed_ref_store, err) < 0)
3709
0
    ret = -1;
3710
3711
0
  strbuf_release(&sb);
3712
0
  return ret;
3713
0
}
3714
3715
/*
3716
 * For refs and reflogs, they share a unified interface when scanning
3717
 * the whole directory. This function is used as the callback for each
3718
 * regular file or symlink in the directory.
3719
 */
3720
typedef int (*files_fsck_refs_fn)(struct ref_store *ref_store,
3721
          struct fsck_options *o,
3722
          const char *refname,
3723
          struct dir_iterator *iter);
3724
3725
static int files_fsck_symref_target(struct fsck_options *o,
3726
            struct fsck_ref_report *report,
3727
            struct strbuf *referent,
3728
            unsigned int symbolic_link)
3729
0
{
3730
0
  int is_referent_root;
3731
0
  char orig_last_byte;
3732
0
  size_t orig_len;
3733
0
  int ret = 0;
3734
3735
0
  orig_len = referent->len;
3736
0
  orig_last_byte = referent->buf[orig_len - 1];
3737
0
  if (!symbolic_link)
3738
0
    strbuf_rtrim(referent);
3739
3740
0
  is_referent_root = is_root_ref(referent->buf);
3741
0
  if (!is_referent_root &&
3742
0
      !starts_with(referent->buf, "refs/") &&
3743
0
      !starts_with(referent->buf, "worktrees/")) {
3744
0
    ret = fsck_report_ref(o, report,
3745
0
              FSCK_MSG_SYMREF_TARGET_IS_NOT_A_REF,
3746
0
              "points to non-ref target '%s'", referent->buf);
3747
3748
0
  }
3749
3750
0
  if (!is_referent_root && check_refname_format(referent->buf, 0)) {
3751
0
    ret = fsck_report_ref(o, report,
3752
0
              FSCK_MSG_BAD_REFERENT_NAME,
3753
0
              "points to invalid refname '%s'", referent->buf);
3754
0
    goto out;
3755
0
  }
3756
3757
0
  if (symbolic_link)
3758
0
    goto out;
3759
3760
0
  if (referent->len == orig_len ||
3761
0
      (referent->len < orig_len && orig_last_byte != '\n')) {
3762
0
    ret = fsck_report_ref(o, report,
3763
0
              FSCK_MSG_REF_MISSING_NEWLINE,
3764
0
              "misses LF at the end");
3765
0
  }
3766
3767
0
  if (referent->len != orig_len && referent->len != orig_len - 1) {
3768
0
    ret = fsck_report_ref(o, report,
3769
0
              FSCK_MSG_TRAILING_REF_CONTENT,
3770
0
              "has trailing whitespaces or newlines");
3771
0
  }
3772
3773
0
out:
3774
0
  return ret;
3775
0
}
3776
3777
static int files_fsck_refs_content(struct ref_store *ref_store,
3778
           struct fsck_options *o,
3779
           const char *target_name,
3780
           struct dir_iterator *iter)
3781
0
{
3782
0
  struct strbuf ref_content = STRBUF_INIT;
3783
0
  struct strbuf abs_gitdir = STRBUF_INIT;
3784
0
  struct strbuf referent = STRBUF_INIT;
3785
0
  struct fsck_ref_report report = { 0 };
3786
0
  const char *trailing = NULL;
3787
0
  unsigned int type = 0;
3788
0
  int failure_errno = 0;
3789
0
  struct object_id oid;
3790
0
  int ret = 0;
3791
3792
0
  report.path = target_name;
3793
3794
0
  if (S_ISLNK(iter->st.st_mode)) {
3795
0
    const char *relative_referent_path = NULL;
3796
3797
0
    ret = fsck_report_ref(o, &report,
3798
0
              FSCK_MSG_SYMLINK_REF,
3799
0
              "use deprecated symbolic link for symref");
3800
3801
0
    strbuf_add_absolute_path(&abs_gitdir, ref_store->repo->gitdir);
3802
0
    strbuf_normalize_path(&abs_gitdir);
3803
0
    if (!is_dir_sep(abs_gitdir.buf[abs_gitdir.len - 1]))
3804
0
      strbuf_addch(&abs_gitdir, '/');
3805
3806
0
    strbuf_add_real_path(&ref_content, iter->path.buf);
3807
0
    skip_prefix(ref_content.buf, abs_gitdir.buf,
3808
0
          &relative_referent_path);
3809
3810
0
    if (relative_referent_path)
3811
0
      strbuf_addstr(&referent, relative_referent_path);
3812
0
    else
3813
0
      strbuf_addbuf(&referent, &ref_content);
3814
3815
0
    ret |= files_fsck_symref_target(o, &report, &referent, 1);
3816
0
    goto cleanup;
3817
0
  }
3818
3819
0
  if (strbuf_read_file(&ref_content, iter->path.buf, 0) < 0) {
3820
    /*
3821
     * Ref file could be removed by another concurrent process. We should
3822
     * ignore this error and continue to the next ref.
3823
     */
3824
0
    if (errno == ENOENT)
3825
0
      goto cleanup;
3826
3827
0
    ret = error_errno(_("cannot read ref file '%s'"), iter->path.buf);
3828
0
    goto cleanup;
3829
0
  }
3830
3831
0
  if (parse_loose_ref_contents(ref_store->repo->hash_algo,
3832
0
             ref_content.buf, &oid, &referent,
3833
0
             &type, &trailing, &failure_errno)) {
3834
0
    strbuf_rtrim(&ref_content);
3835
0
    ret = fsck_report_ref(o, &report,
3836
0
              FSCK_MSG_BAD_REF_CONTENT,
3837
0
              "%s", ref_content.buf);
3838
0
    goto cleanup;
3839
0
  }
3840
3841
0
  if (!(type & REF_ISSYMREF)) {
3842
0
    if (!*trailing) {
3843
0
      ret = fsck_report_ref(o, &report,
3844
0
                FSCK_MSG_REF_MISSING_NEWLINE,
3845
0
                "misses LF at the end");
3846
0
      goto cleanup;
3847
0
    }
3848
0
    if (*trailing != '\n' || *(trailing + 1)) {
3849
0
      ret = fsck_report_ref(o, &report,
3850
0
                FSCK_MSG_TRAILING_REF_CONTENT,
3851
0
                "has trailing garbage: '%s'", trailing);
3852
0
      goto cleanup;
3853
0
    }
3854
0
  } else {
3855
0
    ret = files_fsck_symref_target(o, &report, &referent, 0);
3856
0
    goto cleanup;
3857
0
  }
3858
3859
0
cleanup:
3860
0
  strbuf_release(&ref_content);
3861
0
  strbuf_release(&referent);
3862
0
  strbuf_release(&abs_gitdir);
3863
0
  return ret;
3864
0
}
3865
3866
static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
3867
        struct fsck_options *o,
3868
        const char *refname,
3869
        struct dir_iterator *iter)
3870
0
{
3871
0
  struct strbuf sb = STRBUF_INIT;
3872
0
  int ret = 0;
3873
3874
  /*
3875
   * Ignore the files ending with ".lock" as they may be lock files
3876
   * However, do not allow bare ".lock" files.
3877
   */
3878
0
  if (iter->basename[0] != '.' && ends_with(iter->basename, ".lock"))
3879
0
    goto cleanup;
3880
3881
  /*
3882
   * This works right now because we never check the root refs.
3883
   */
3884
0
  if (check_refname_format(refname, 0)) {
3885
0
    struct fsck_ref_report report = { 0 };
3886
3887
0
    report.path = refname;
3888
0
    ret = fsck_report_ref(o, &report,
3889
0
              FSCK_MSG_BAD_REF_NAME,
3890
0
              "invalid refname format");
3891
0
  }
3892
3893
0
cleanup:
3894
0
  strbuf_release(&sb);
3895
0
  return ret;
3896
0
}
3897
3898
static int files_fsck_refs_dir(struct ref_store *ref_store,
3899
             struct fsck_options *o,
3900
             const char *refs_check_dir,
3901
             struct worktree *wt,
3902
             files_fsck_refs_fn *fsck_refs_fn)
3903
0
{
3904
0
  struct strbuf refname = STRBUF_INIT;
3905
0
  struct strbuf sb = STRBUF_INIT;
3906
0
  struct dir_iterator *iter;
3907
0
  int iter_status;
3908
0
  int ret = 0;
3909
3910
0
  strbuf_addf(&sb, "%s/%s", ref_store->gitdir, refs_check_dir);
3911
3912
0
  iter = dir_iterator_begin(sb.buf, 0);
3913
0
  if (!iter) {
3914
0
    if (errno == ENOENT && !is_main_worktree(wt))
3915
0
      goto out;
3916
3917
0
    ret = error_errno(_("cannot open directory %s"), sb.buf);
3918
0
    goto out;
3919
0
  }
3920
3921
0
  while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
3922
0
    if (S_ISDIR(iter->st.st_mode)) {
3923
0
      continue;
3924
0
    } else if (S_ISREG(iter->st.st_mode) ||
3925
0
         S_ISLNK(iter->st.st_mode)) {
3926
0
      strbuf_reset(&refname);
3927
3928
0
      if (!is_main_worktree(wt))
3929
0
        strbuf_addf(&refname, "worktrees/%s/", wt->id);
3930
0
      strbuf_addf(&refname, "%s/%s", refs_check_dir,
3931
0
            iter->relative_path);
3932
3933
0
      if (o->verbose)
3934
0
        fprintf_ln(stderr, "Checking %s", refname.buf);
3935
3936
0
      for (size_t i = 0; fsck_refs_fn[i]; i++) {
3937
0
        if (fsck_refs_fn[i](ref_store, o, refname.buf, iter))
3938
0
          ret = -1;
3939
0
      }
3940
0
    } else {
3941
0
      struct fsck_ref_report report = { .path = iter->basename };
3942
0
      if (fsck_report_ref(o, &report,
3943
0
              FSCK_MSG_BAD_REF_FILETYPE,
3944
0
              "unexpected file type"))
3945
0
        ret = -1;
3946
0
    }
3947
0
  }
3948
3949
0
  if (iter_status != ITER_DONE)
3950
0
    ret = error(_("failed to iterate over '%s'"), sb.buf);
3951
3952
0
out:
3953
0
  dir_iterator_free(iter);
3954
0
  strbuf_release(&sb);
3955
0
  strbuf_release(&refname);
3956
0
  return ret;
3957
0
}
3958
3959
static int files_fsck_refs(struct ref_store *ref_store,
3960
         struct fsck_options *o,
3961
         struct worktree *wt)
3962
0
{
3963
0
  files_fsck_refs_fn fsck_refs_fn[]= {
3964
0
    files_fsck_refs_name,
3965
0
    files_fsck_refs_content,
3966
0
    NULL,
3967
0
  };
3968
3969
0
  return files_fsck_refs_dir(ref_store, o, "refs", wt, fsck_refs_fn);
3970
0
}
3971
3972
static int files_fsck(struct ref_store *ref_store,
3973
          struct fsck_options *o,
3974
          struct worktree *wt)
3975
0
{
3976
0
  struct files_ref_store *refs =
3977
0
    files_downcast(ref_store, REF_STORE_READ, "fsck");
3978
3979
0
  return files_fsck_refs(ref_store, o, wt) |
3980
0
         refs->packed_ref_store->be->fsck(refs->packed_ref_store, o, wt);
3981
0
}
3982
3983
struct ref_storage_be refs_be_files = {
3984
  .name = "files",
3985
  .init = files_ref_store_init,
3986
  .release = files_ref_store_release,
3987
  .create_on_disk = files_ref_store_create_on_disk,
3988
  .remove_on_disk = files_ref_store_remove_on_disk,
3989
3990
  .transaction_prepare = files_transaction_prepare,
3991
  .transaction_finish = files_transaction_finish,
3992
  .transaction_abort = files_transaction_abort,
3993
3994
  .optimize = files_optimize,
3995
  .optimize_required = files_optimize_required,
3996
  .rename_ref = files_rename_ref,
3997
  .copy_ref = files_copy_ref,
3998
3999
  .iterator_begin = files_ref_iterator_begin,
4000
  .read_raw_ref = files_read_raw_ref,
4001
  .read_symbolic_ref = files_read_symbolic_ref,
4002
4003
  .reflog_iterator_begin = files_reflog_iterator_begin,
4004
  .for_each_reflog_ent = files_for_each_reflog_ent,
4005
  .for_each_reflog_ent_reverse = files_for_each_reflog_ent_reverse,
4006
  .reflog_exists = files_reflog_exists,
4007
  .create_reflog = files_create_reflog,
4008
  .delete_reflog = files_delete_reflog,
4009
  .reflog_expire = files_reflog_expire,
4010
4011
  .fsck = files_fsck,
4012
};