Coverage Report

Created: 2023-11-19 07:08

/src/git/builtin/gc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * git gc builtin command
3
 *
4
 * Cleanup unreachable files and optimize the repository.
5
 *
6
 * Copyright (c) 2007 James Bowes
7
 *
8
 * Based on git-gc.sh, which is
9
 *
10
 * Copyright (c) 2006 Shawn O. Pearce
11
 */
12
13
#include "builtin.h"
14
#include "abspath.h"
15
#include "date.h"
16
#include "environment.h"
17
#include "hex.h"
18
#include "repository.h"
19
#include "config.h"
20
#include "tempfile.h"
21
#include "lockfile.h"
22
#include "parse-options.h"
23
#include "run-command.h"
24
#include "sigchain.h"
25
#include "strvec.h"
26
#include "commit.h"
27
#include "commit-graph.h"
28
#include "packfile.h"
29
#include "object-file.h"
30
#include "object-store-ll.h"
31
#include "pack.h"
32
#include "pack-objects.h"
33
#include "path.h"
34
#include "blob.h"
35
#include "tree.h"
36
#include "promisor-remote.h"
37
#include "refs.h"
38
#include "remote.h"
39
#include "exec-cmd.h"
40
#include "gettext.h"
41
#include "hook.h"
42
#include "setup.h"
43
#include "trace2.h"
44
45
0
#define FAILED_RUN "failed to run %s"
46
47
static const char * const builtin_gc_usage[] = {
48
  N_("git gc [<options>]"),
49
  NULL
50
};
51
52
static int pack_refs = 1;
53
static int prune_reflogs = 1;
54
static int cruft_packs = 1;
55
static unsigned long max_cruft_size;
56
static int aggressive_depth = 50;
57
static int aggressive_window = 250;
58
static int gc_auto_threshold = 6700;
59
static int gc_auto_pack_limit = 50;
60
static int detach_auto = 1;
61
static timestamp_t gc_log_expire_time;
62
static const char *gc_log_expire = "1.day.ago";
63
static const char *prune_expire = "2.weeks.ago";
64
static const char *prune_worktrees_expire = "3.months.ago";
65
static char *repack_filter;
66
static char *repack_filter_to;
67
static unsigned long big_pack_threshold;
68
static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
69
70
static struct strvec reflog = STRVEC_INIT;
71
static struct strvec repack = STRVEC_INIT;
72
static struct strvec prune = STRVEC_INIT;
73
static struct strvec prune_worktrees = STRVEC_INIT;
74
static struct strvec rerere = STRVEC_INIT;
75
76
static struct tempfile *pidfile;
77
static struct lock_file log_lock;
78
79
static struct string_list pack_garbage = STRING_LIST_INIT_DUP;
80
81
static void clean_pack_garbage(void)
82
0
{
83
0
  int i;
84
0
  for (i = 0; i < pack_garbage.nr; i++)
85
0
    unlink_or_warn(pack_garbage.items[i].string);
86
0
  string_list_clear(&pack_garbage, 0);
87
0
}
88
89
static void report_pack_garbage(unsigned seen_bits, const char *path)
90
0
{
91
0
  if (seen_bits == PACKDIR_FILE_IDX)
92
0
    string_list_append(&pack_garbage, path);
93
0
}
94
95
static void process_log_file(void)
96
0
{
97
0
  struct stat st;
98
0
  if (fstat(get_lock_file_fd(&log_lock), &st)) {
99
    /*
100
     * Perhaps there was an i/o error or another
101
     * unlikely situation.  Try to make a note of
102
     * this in gc.log along with any existing
103
     * messages.
104
     */
105
0
    int saved_errno = errno;
106
0
    fprintf(stderr, _("Failed to fstat %s: %s"),
107
0
      get_lock_file_path(&log_lock),
108
0
      strerror(saved_errno));
109
0
    fflush(stderr);
110
0
    commit_lock_file(&log_lock);
111
0
    errno = saved_errno;
112
0
  } else if (st.st_size) {
113
    /* There was some error recorded in the lock file */
114
0
    commit_lock_file(&log_lock);
115
0
  } else {
116
    /* No error, clean up any old gc.log */
117
0
    unlink(git_path("gc.log"));
118
0
    rollback_lock_file(&log_lock);
119
0
  }
120
0
}
121
122
static void process_log_file_at_exit(void)
123
0
{
124
0
  fflush(stderr);
125
0
  process_log_file();
126
0
}
127
128
static void process_log_file_on_signal(int signo)
129
0
{
130
0
  process_log_file();
131
0
  sigchain_pop(signo);
132
0
  raise(signo);
133
0
}
134
135
static int gc_config_is_timestamp_never(const char *var)
136
0
{
137
0
  const char *value;
138
0
  timestamp_t expire;
139
140
0
  if (!git_config_get_value(var, &value) && value) {
141
0
    if (parse_expiry_date(value, &expire))
142
0
      die(_("failed to parse '%s' value '%s'"), var, value);
143
0
    return expire == 0;
144
0
  }
145
0
  return 0;
146
0
}
147
148
static void gc_config(void)
149
0
{
150
0
  const char *value;
151
152
0
  if (!git_config_get_value("gc.packrefs", &value)) {
153
0
    if (value && !strcmp(value, "notbare"))
154
0
      pack_refs = -1;
155
0
    else
156
0
      pack_refs = git_config_bool("gc.packrefs", value);
157
0
  }
158
159
0
  if (gc_config_is_timestamp_never("gc.reflogexpire") &&
160
0
      gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
161
0
    prune_reflogs = 0;
162
163
0
  git_config_get_int("gc.aggressivewindow", &aggressive_window);
164
0
  git_config_get_int("gc.aggressivedepth", &aggressive_depth);
165
0
  git_config_get_int("gc.auto", &gc_auto_threshold);
166
0
  git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit);
167
0
  git_config_get_bool("gc.autodetach", &detach_auto);
168
0
  git_config_get_bool("gc.cruftpacks", &cruft_packs);
169
0
  git_config_get_ulong("gc.maxcruftsize", &max_cruft_size);
170
0
  git_config_get_expiry("gc.pruneexpire", &prune_expire);
171
0
  git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire);
172
0
  git_config_get_expiry("gc.logexpiry", &gc_log_expire);
173
174
0
  git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold);
175
0
  git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size);
176
177
0
  git_config_get_string("gc.repackfilter", &repack_filter);
178
0
  git_config_get_string("gc.repackfilterto", &repack_filter_to);
179
180
0
  git_config(git_default_config, NULL);
181
0
}
182
183
struct maintenance_run_opts;
184
static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
185
0
{
186
0
  struct child_process cmd = CHILD_PROCESS_INIT;
187
188
0
  cmd.git_cmd = 1;
189
0
  strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
190
0
  return run_command(&cmd);
191
0
}
192
193
static int too_many_loose_objects(void)
194
0
{
195
  /*
196
   * Quickly check if a "gc" is needed, by estimating how
197
   * many loose objects there are.  Because SHA-1 is evenly
198
   * distributed, we can check only one and get a reasonable
199
   * estimate.
200
   */
201
0
  DIR *dir;
202
0
  struct dirent *ent;
203
0
  int auto_threshold;
204
0
  int num_loose = 0;
205
0
  int needed = 0;
206
0
  const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
207
208
0
  dir = opendir(git_path("objects/17"));
209
0
  if (!dir)
210
0
    return 0;
211
212
0
  auto_threshold = DIV_ROUND_UP(gc_auto_threshold, 256);
213
0
  while ((ent = readdir(dir)) != NULL) {
214
0
    if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
215
0
        ent->d_name[hexsz_loose] != '\0')
216
0
      continue;
217
0
    if (++num_loose > auto_threshold) {
218
0
      needed = 1;
219
0
      break;
220
0
    }
221
0
  }
222
0
  closedir(dir);
223
0
  return needed;
224
0
}
225
226
static struct packed_git *find_base_packs(struct string_list *packs,
227
            unsigned long limit)
228
0
{
229
0
  struct packed_git *p, *base = NULL;
230
231
0
  for (p = get_all_packs(the_repository); p; p = p->next) {
232
0
    if (!p->pack_local || p->is_cruft)
233
0
      continue;
234
0
    if (limit) {
235
0
      if (p->pack_size >= limit)
236
0
        string_list_append(packs, p->pack_name);
237
0
    } else if (!base || base->pack_size < p->pack_size) {
238
0
      base = p;
239
0
    }
240
0
  }
241
242
0
  if (base)
243
0
    string_list_append(packs, base->pack_name);
244
245
0
  return base;
246
0
}
247
248
static int too_many_packs(void)
249
0
{
250
0
  struct packed_git *p;
251
0
  int cnt;
252
253
0
  if (gc_auto_pack_limit <= 0)
254
0
    return 0;
255
256
0
  for (cnt = 0, p = get_all_packs(the_repository); p; p = p->next) {
257
0
    if (!p->pack_local)
258
0
      continue;
259
0
    if (p->pack_keep)
260
0
      continue;
261
    /*
262
     * Perhaps check the size of the pack and count only
263
     * very small ones here?
264
     */
265
0
    cnt++;
266
0
  }
267
0
  return gc_auto_pack_limit < cnt;
268
0
}
269
270
static uint64_t total_ram(void)
271
0
{
272
0
#if defined(HAVE_SYSINFO)
273
0
  struct sysinfo si;
274
275
0
  if (!sysinfo(&si))
276
0
    return si.totalram;
277
#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
278
  int64_t physical_memory;
279
  int mib[2];
280
  size_t length;
281
282
  mib[0] = CTL_HW;
283
# if defined(HW_MEMSIZE)
284
  mib[1] = HW_MEMSIZE;
285
# else
286
  mib[1] = HW_PHYSMEM;
287
# endif
288
  length = sizeof(int64_t);
289
  if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
290
    return physical_memory;
291
#elif defined(GIT_WINDOWS_NATIVE)
292
  MEMORYSTATUSEX memInfo;
293
294
  memInfo.dwLength = sizeof(MEMORYSTATUSEX);
295
  if (GlobalMemoryStatusEx(&memInfo))
296
    return memInfo.ullTotalPhys;
297
#endif
298
0
  return 0;
299
0
}
300
301
static uint64_t estimate_repack_memory(struct packed_git *pack)
302
0
{
303
0
  unsigned long nr_objects = repo_approximate_object_count(the_repository);
304
0
  size_t os_cache, heap;
305
306
0
  if (!pack || !nr_objects)
307
0
    return 0;
308
309
  /*
310
   * First we have to scan through at least one pack.
311
   * Assume enough room in OS file cache to keep the entire pack
312
   * or we may accidentally evict data of other processes from
313
   * the cache.
314
   */
315
0
  os_cache = pack->pack_size + pack->index_size;
316
  /* then pack-objects needs lots more for book keeping */
317
0
  heap = sizeof(struct object_entry) * nr_objects;
318
  /*
319
   * internal rev-list --all --objects takes up some memory too,
320
   * let's say half of it is for blobs
321
   */
322
0
  heap += sizeof(struct blob) * nr_objects / 2;
323
  /*
324
   * and the other half is for trees (commits and tags are
325
   * usually insignificant)
326
   */
327
0
  heap += sizeof(struct tree) * nr_objects / 2;
328
  /* and then obj_hash[], underestimated in fact */
329
0
  heap += sizeof(struct object *) * nr_objects;
330
  /* revindex is used also */
331
0
  heap += (sizeof(off_t) + sizeof(uint32_t)) * nr_objects;
332
  /*
333
   * read_sha1_file() (either at delta calculation phase, or
334
   * writing phase) also fills up the delta base cache
335
   */
336
0
  heap += delta_base_cache_limit;
337
  /* and of course pack-objects has its own delta cache */
338
0
  heap += max_delta_cache_size;
339
340
0
  return os_cache + heap;
341
0
}
342
343
static int keep_one_pack(struct string_list_item *item, void *data UNUSED)
344
0
{
345
0
  strvec_pushf(&repack, "--keep-pack=%s", basename(item->string));
346
0
  return 0;
347
0
}
348
349
static void add_repack_all_option(struct string_list *keep_pack)
350
0
{
351
0
  if (prune_expire && !strcmp(prune_expire, "now"))
352
0
    strvec_push(&repack, "-a");
353
0
  else if (cruft_packs) {
354
0
    strvec_push(&repack, "--cruft");
355
0
    if (prune_expire)
356
0
      strvec_pushf(&repack, "--cruft-expiration=%s", prune_expire);
357
0
    if (max_cruft_size)
358
0
      strvec_pushf(&repack, "--max-cruft-size=%lu",
359
0
             max_cruft_size);
360
0
  } else {
361
0
    strvec_push(&repack, "-A");
362
0
    if (prune_expire)
363
0
      strvec_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
364
0
  }
365
366
0
  if (keep_pack)
367
0
    for_each_string_list(keep_pack, keep_one_pack, NULL);
368
369
0
  if (repack_filter && *repack_filter)
370
0
    strvec_pushf(&repack, "--filter=%s", repack_filter);
371
0
  if (repack_filter_to && *repack_filter_to)
372
0
    strvec_pushf(&repack, "--filter-to=%s", repack_filter_to);
373
0
}
374
375
static void add_repack_incremental_option(void)
376
0
{
377
0
  strvec_push(&repack, "--no-write-bitmap-index");
378
0
}
379
380
static int need_to_gc(void)
381
0
{
382
  /*
383
   * Setting gc.auto to 0 or negative can disable the
384
   * automatic gc.
385
   */
386
0
  if (gc_auto_threshold <= 0)
387
0
    return 0;
388
389
  /*
390
   * If there are too many loose objects, but not too many
391
   * packs, we run "repack -d -l".  If there are too many packs,
392
   * we run "repack -A -d -l".  Otherwise we tell the caller
393
   * there is no need.
394
   */
395
0
  if (too_many_packs()) {
396
0
    struct string_list keep_pack = STRING_LIST_INIT_NODUP;
397
398
0
    if (big_pack_threshold) {
399
0
      find_base_packs(&keep_pack, big_pack_threshold);
400
0
      if (keep_pack.nr >= gc_auto_pack_limit) {
401
0
        big_pack_threshold = 0;
402
0
        string_list_clear(&keep_pack, 0);
403
0
        find_base_packs(&keep_pack, 0);
404
0
      }
405
0
    } else {
406
0
      struct packed_git *p = find_base_packs(&keep_pack, 0);
407
0
      uint64_t mem_have, mem_want;
408
409
0
      mem_have = total_ram();
410
0
      mem_want = estimate_repack_memory(p);
411
412
      /*
413
       * Only allow 1/2 of memory for pack-objects, leave
414
       * the rest for the OS and other processes in the
415
       * system.
416
       */
417
0
      if (!mem_have || mem_want < mem_have / 2)
418
0
        string_list_clear(&keep_pack, 0);
419
0
    }
420
421
0
    add_repack_all_option(&keep_pack);
422
0
    string_list_clear(&keep_pack, 0);
423
0
  } else if (too_many_loose_objects())
424
0
    add_repack_incremental_option();
425
0
  else
426
0
    return 0;
427
428
0
  if (run_hooks("pre-auto-gc"))
429
0
    return 0;
430
0
  return 1;
431
0
}
432
433
/* return NULL on success, else hostname running the gc */
434
static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
435
0
{
436
0
  struct lock_file lock = LOCK_INIT;
437
0
  char my_host[HOST_NAME_MAX + 1];
438
0
  struct strbuf sb = STRBUF_INIT;
439
0
  struct stat st;
440
0
  uintmax_t pid;
441
0
  FILE *fp;
442
0
  int fd;
443
0
  char *pidfile_path;
444
445
0
  if (is_tempfile_active(pidfile))
446
    /* already locked */
447
0
    return NULL;
448
449
0
  if (xgethostname(my_host, sizeof(my_host)))
450
0
    xsnprintf(my_host, sizeof(my_host), "unknown");
451
452
0
  pidfile_path = git_pathdup("gc.pid");
453
0
  fd = hold_lock_file_for_update(&lock, pidfile_path,
454
0
               LOCK_DIE_ON_ERROR);
455
0
  if (!force) {
456
0
    static char locking_host[HOST_NAME_MAX + 1];
457
0
    static char *scan_fmt;
458
0
    int should_exit;
459
460
0
    if (!scan_fmt)
461
0
      scan_fmt = xstrfmt("%s %%%ds", "%"SCNuMAX, HOST_NAME_MAX);
462
0
    fp = fopen(pidfile_path, "r");
463
0
    memset(locking_host, 0, sizeof(locking_host));
464
0
    should_exit =
465
0
      fp != NULL &&
466
0
      !fstat(fileno(fp), &st) &&
467
      /*
468
       * 12 hour limit is very generous as gc should
469
       * never take that long. On the other hand we
470
       * don't really need a strict limit here,
471
       * running gc --auto one day late is not a big
472
       * problem. --force can be used in manual gc
473
       * after the user verifies that no gc is
474
       * running.
475
       */
476
0
      time(NULL) - st.st_mtime <= 12 * 3600 &&
477
0
      fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
478
      /* be gentle to concurrent "gc" on remote hosts */
479
0
      (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
480
0
    if (fp)
481
0
      fclose(fp);
482
0
    if (should_exit) {
483
0
      if (fd >= 0)
484
0
        rollback_lock_file(&lock);
485
0
      *ret_pid = pid;
486
0
      free(pidfile_path);
487
0
      return locking_host;
488
0
    }
489
0
  }
490
491
0
  strbuf_addf(&sb, "%"PRIuMAX" %s",
492
0
        (uintmax_t) getpid(), my_host);
493
0
  write_in_full(fd, sb.buf, sb.len);
494
0
  strbuf_release(&sb);
495
0
  commit_lock_file(&lock);
496
0
  pidfile = register_tempfile(pidfile_path);
497
0
  free(pidfile_path);
498
0
  return NULL;
499
0
}
500
501
/*
502
 * Returns 0 if there was no previous error and gc can proceed, 1 if
503
 * gc should not proceed due to an error in the last run. Prints a
504
 * message and returns with a non-[01] status code if an error occurred
505
 * while reading gc.log
506
 */
507
static int report_last_gc_error(void)
508
0
{
509
0
  struct strbuf sb = STRBUF_INIT;
510
0
  int ret = 0;
511
0
  ssize_t len;
512
0
  struct stat st;
513
0
  char *gc_log_path = git_pathdup("gc.log");
514
515
0
  if (stat(gc_log_path, &st)) {
516
0
    if (errno == ENOENT)
517
0
      goto done;
518
519
0
    ret = die_message_errno(_("cannot stat '%s'"), gc_log_path);
520
0
    goto done;
521
0
  }
522
523
0
  if (st.st_mtime < gc_log_expire_time)
524
0
    goto done;
525
526
0
  len = strbuf_read_file(&sb, gc_log_path, 0);
527
0
  if (len < 0)
528
0
    ret = die_message_errno(_("cannot read '%s'"), gc_log_path);
529
0
  else if (len > 0) {
530
    /*
531
     * A previous gc failed.  Report the error, and don't
532
     * bother with an automatic gc run since it is likely
533
     * to fail in the same way.
534
     */
535
0
    warning(_("The last gc run reported the following. "
536
0
             "Please correct the root cause\n"
537
0
             "and remove %s\n"
538
0
             "Automatic cleanup will not be performed "
539
0
             "until the file is removed.\n\n"
540
0
             "%s"),
541
0
          gc_log_path, sb.buf);
542
0
    ret = 1;
543
0
  }
544
0
  strbuf_release(&sb);
545
0
done:
546
0
  free(gc_log_path);
547
0
  return ret;
548
0
}
549
550
static void gc_before_repack(void)
551
0
{
552
  /*
553
   * We may be called twice, as both the pre- and
554
   * post-daemonized phases will call us, but running these
555
   * commands more than once is pointless and wasteful.
556
   */
557
0
  static int done = 0;
558
0
  if (done++)
559
0
    return;
560
561
0
  if (pack_refs && maintenance_task_pack_refs(NULL))
562
0
    die(FAILED_RUN, "pack-refs");
563
564
0
  if (prune_reflogs) {
565
0
    struct child_process cmd = CHILD_PROCESS_INIT;
566
567
0
    cmd.git_cmd = 1;
568
0
    strvec_pushv(&cmd.args, reflog.v);
569
0
    if (run_command(&cmd))
570
0
      die(FAILED_RUN, reflog.v[0]);
571
0
  }
572
0
}
573
574
int cmd_gc(int argc, const char **argv, const char *prefix)
575
0
{
576
0
  int aggressive = 0;
577
0
  int auto_gc = 0;
578
0
  int quiet = 0;
579
0
  int force = 0;
580
0
  const char *name;
581
0
  pid_t pid;
582
0
  int daemonized = 0;
583
0
  int keep_largest_pack = -1;
584
0
  timestamp_t dummy;
585
0
  struct child_process rerere_cmd = CHILD_PROCESS_INIT;
586
587
0
  struct option builtin_gc_options[] = {
588
0
    OPT__QUIET(&quiet, N_("suppress progress reporting")),
589
0
    { OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
590
0
      N_("prune unreferenced objects"),
591
0
      PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
592
0
    OPT_BOOL(0, "cruft", &cruft_packs, N_("pack unreferenced objects separately")),
593
0
    OPT_MAGNITUDE(0, "max-cruft-size", &max_cruft_size,
594
0
            N_("with --cruft, limit the size of new cruft packs")),
595
0
    OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
596
0
    OPT_BOOL_F(0, "auto", &auto_gc, N_("enable auto-gc mode"),
597
0
         PARSE_OPT_NOCOMPLETE),
598
0
    OPT_BOOL_F(0, "force", &force,
599
0
         N_("force running gc even if there may be another gc running"),
600
0
         PARSE_OPT_NOCOMPLETE),
601
0
    OPT_BOOL(0, "keep-largest-pack", &keep_largest_pack,
602
0
       N_("repack all other packs except the largest pack")),
603
0
    OPT_END()
604
0
  };
605
606
0
  if (argc == 2 && !strcmp(argv[1], "-h"))
607
0
    usage_with_options(builtin_gc_usage, builtin_gc_options);
608
609
0
  strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
610
0
  strvec_pushl(&repack, "repack", "-d", "-l", NULL);
611
0
  strvec_pushl(&prune, "prune", "--expire", NULL);
612
0
  strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL);
613
0
  strvec_pushl(&rerere, "rerere", "gc", NULL);
614
615
  /* default expiry time, overwritten in gc_config */
616
0
  gc_config();
617
0
  if (parse_expiry_date(gc_log_expire, &gc_log_expire_time))
618
0
    die(_("failed to parse gc.logExpiry value %s"), gc_log_expire);
619
620
0
  if (pack_refs < 0)
621
0
    pack_refs = !is_bare_repository();
622
623
0
  argc = parse_options(argc, argv, prefix, builtin_gc_options,
624
0
           builtin_gc_usage, 0);
625
0
  if (argc > 0)
626
0
    usage_with_options(builtin_gc_usage, builtin_gc_options);
627
628
0
  if (prune_expire && parse_expiry_date(prune_expire, &dummy))
629
0
    die(_("failed to parse prune expiry value %s"), prune_expire);
630
631
0
  if (aggressive) {
632
0
    strvec_push(&repack, "-f");
633
0
    if (aggressive_depth > 0)
634
0
      strvec_pushf(&repack, "--depth=%d", aggressive_depth);
635
0
    if (aggressive_window > 0)
636
0
      strvec_pushf(&repack, "--window=%d", aggressive_window);
637
0
  }
638
0
  if (quiet)
639
0
    strvec_push(&repack, "-q");
640
641
0
  if (auto_gc) {
642
    /*
643
     * Auto-gc should be least intrusive as possible.
644
     */
645
0
    if (!need_to_gc())
646
0
      return 0;
647
0
    if (!quiet) {
648
0
      if (detach_auto)
649
0
        fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
650
0
      else
651
0
        fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
652
0
      fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
653
0
    }
654
0
    if (detach_auto) {
655
0
      int ret = report_last_gc_error();
656
657
0
      if (ret == 1)
658
        /* Last gc --auto failed. Skip this one. */
659
0
        return 0;
660
0
      else if (ret)
661
        /* an I/O error occurred, already reported */
662
0
        return ret;
663
664
0
      if (lock_repo_for_gc(force, &pid))
665
0
        return 0;
666
0
      gc_before_repack(); /* dies on failure */
667
0
      delete_tempfile(&pidfile);
668
669
      /*
670
       * failure to daemonize is ok, we'll continue
671
       * in foreground
672
       */
673
0
      daemonized = !daemonize();
674
0
    }
675
0
  } else {
676
0
    struct string_list keep_pack = STRING_LIST_INIT_NODUP;
677
678
0
    if (keep_largest_pack != -1) {
679
0
      if (keep_largest_pack)
680
0
        find_base_packs(&keep_pack, 0);
681
0
    } else if (big_pack_threshold) {
682
0
      find_base_packs(&keep_pack, big_pack_threshold);
683
0
    }
684
685
0
    add_repack_all_option(&keep_pack);
686
0
    string_list_clear(&keep_pack, 0);
687
0
  }
688
689
0
  name = lock_repo_for_gc(force, &pid);
690
0
  if (name) {
691
0
    if (auto_gc)
692
0
      return 0; /* be quiet on --auto */
693
0
    die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
694
0
        name, (uintmax_t)pid);
695
0
  }
696
697
0
  if (daemonized) {
698
0
    hold_lock_file_for_update(&log_lock,
699
0
            git_path("gc.log"),
700
0
            LOCK_DIE_ON_ERROR);
701
0
    dup2(get_lock_file_fd(&log_lock), 2);
702
0
    sigchain_push_common(process_log_file_on_signal);
703
0
    atexit(process_log_file_at_exit);
704
0
  }
705
706
0
  gc_before_repack();
707
708
0
  if (!repository_format_precious_objects) {
709
0
    struct child_process repack_cmd = CHILD_PROCESS_INIT;
710
711
0
    repack_cmd.git_cmd = 1;
712
0
    repack_cmd.close_object_store = 1;
713
0
    strvec_pushv(&repack_cmd.args, repack.v);
714
0
    if (run_command(&repack_cmd))
715
0
      die(FAILED_RUN, repack.v[0]);
716
717
0
    if (prune_expire) {
718
0
      struct child_process prune_cmd = CHILD_PROCESS_INIT;
719
720
      /* run `git prune` even if using cruft packs */
721
0
      strvec_push(&prune, prune_expire);
722
0
      if (quiet)
723
0
        strvec_push(&prune, "--no-progress");
724
0
      if (repo_has_promisor_remote(the_repository))
725
0
        strvec_push(&prune,
726
0
              "--exclude-promisor-objects");
727
0
      prune_cmd.git_cmd = 1;
728
0
      strvec_pushv(&prune_cmd.args, prune.v);
729
0
      if (run_command(&prune_cmd))
730
0
        die(FAILED_RUN, prune.v[0]);
731
0
    }
732
0
  }
733
734
0
  if (prune_worktrees_expire) {
735
0
    struct child_process prune_worktrees_cmd = CHILD_PROCESS_INIT;
736
737
0
    strvec_push(&prune_worktrees, prune_worktrees_expire);
738
0
    prune_worktrees_cmd.git_cmd = 1;
739
0
    strvec_pushv(&prune_worktrees_cmd.args, prune_worktrees.v);
740
0
    if (run_command(&prune_worktrees_cmd))
741
0
      die(FAILED_RUN, prune_worktrees.v[0]);
742
0
  }
743
744
0
  rerere_cmd.git_cmd = 1;
745
0
  strvec_pushv(&rerere_cmd.args, rerere.v);
746
0
  if (run_command(&rerere_cmd))
747
0
    die(FAILED_RUN, rerere.v[0]);
748
749
0
  report_garbage = report_pack_garbage;
750
0
  reprepare_packed_git(the_repository);
751
0
  if (pack_garbage.nr > 0) {
752
0
    close_object_store(the_repository->objects);
753
0
    clean_pack_garbage();
754
0
  }
755
756
0
  if (the_repository->settings.gc_write_commit_graph == 1)
757
0
    write_commit_graph_reachable(the_repository->objects->odb,
758
0
               !quiet && !daemonized ? COMMIT_GRAPH_WRITE_PROGRESS : 0,
759
0
               NULL);
760
761
0
  if (auto_gc && too_many_loose_objects())
762
0
    warning(_("There are too many unreachable loose objects; "
763
0
      "run 'git prune' to remove them."));
764
765
0
  if (!daemonized)
766
0
    unlink(git_path("gc.log"));
767
768
0
  return 0;
769
0
}
770
771
static const char *const builtin_maintenance_run_usage[] = {
772
  N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>] [--schedule]"),
773
  NULL
774
};
775
776
enum schedule_priority {
777
  SCHEDULE_NONE = 0,
778
  SCHEDULE_WEEKLY = 1,
779
  SCHEDULE_DAILY = 2,
780
  SCHEDULE_HOURLY = 3,
781
};
782
783
static enum schedule_priority parse_schedule(const char *value)
784
0
{
785
0
  if (!value)
786
0
    return SCHEDULE_NONE;
787
0
  if (!strcasecmp(value, "hourly"))
788
0
    return SCHEDULE_HOURLY;
789
0
  if (!strcasecmp(value, "daily"))
790
0
    return SCHEDULE_DAILY;
791
0
  if (!strcasecmp(value, "weekly"))
792
0
    return SCHEDULE_WEEKLY;
793
0
  return SCHEDULE_NONE;
794
0
}
795
796
static int maintenance_opt_schedule(const struct option *opt, const char *arg,
797
            int unset)
798
0
{
799
0
  enum schedule_priority *priority = opt->value;
800
801
0
  if (unset)
802
0
    die(_("--no-schedule is not allowed"));
803
804
0
  *priority = parse_schedule(arg);
805
806
0
  if (!*priority)
807
0
    die(_("unrecognized --schedule argument '%s'"), arg);
808
809
0
  return 0;
810
0
}
811
812
struct maintenance_run_opts {
813
  int auto_flag;
814
  int quiet;
815
  enum schedule_priority schedule;
816
};
817
818
/* Remember to update object flag allocation in object.h */
819
0
#define SEEN    (1u<<0)
820
821
struct cg_auto_data {
822
  int num_not_in_graph;
823
  int limit;
824
};
825
826
static int dfs_on_ref(const char *refname UNUSED,
827
          const struct object_id *oid,
828
          int flags UNUSED,
829
          void *cb_data)
830
0
{
831
0
  struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
832
0
  int result = 0;
833
0
  struct object_id peeled;
834
0
  struct commit_list *stack = NULL;
835
0
  struct commit *commit;
836
837
0
  if (!peel_iterated_oid(oid, &peeled))
838
0
    oid = &peeled;
839
0
  if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
840
0
    return 0;
841
842
0
  commit = lookup_commit(the_repository, oid);
843
0
  if (!commit)
844
0
    return 0;
845
0
  if (repo_parse_commit(the_repository, commit) ||
846
0
      commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
847
0
    return 0;
848
849
0
  data->num_not_in_graph++;
850
851
0
  if (data->num_not_in_graph >= data->limit)
852
0
    return 1;
853
854
0
  commit_list_append(commit, &stack);
855
856
0
  while (!result && stack) {
857
0
    struct commit_list *parent;
858
859
0
    commit = pop_commit(&stack);
860
861
0
    for (parent = commit->parents; parent; parent = parent->next) {
862
0
      if (repo_parse_commit(the_repository, parent->item) ||
863
0
          commit_graph_position(parent->item) != COMMIT_NOT_FROM_GRAPH ||
864
0
          parent->item->object.flags & SEEN)
865
0
        continue;
866
867
0
      parent->item->object.flags |= SEEN;
868
0
      data->num_not_in_graph++;
869
870
0
      if (data->num_not_in_graph >= data->limit) {
871
0
        result = 1;
872
0
        break;
873
0
      }
874
875
0
      commit_list_append(parent->item, &stack);
876
0
    }
877
0
  }
878
879
0
  free_commit_list(stack);
880
0
  return result;
881
0
}
882
883
static int should_write_commit_graph(void)
884
0
{
885
0
  int result;
886
0
  struct cg_auto_data data;
887
888
0
  data.num_not_in_graph = 0;
889
0
  data.limit = 100;
890
0
  git_config_get_int("maintenance.commit-graph.auto",
891
0
         &data.limit);
892
893
0
  if (!data.limit)
894
0
    return 0;
895
0
  if (data.limit < 0)
896
0
    return 1;
897
898
0
  result = for_each_ref(dfs_on_ref, &data);
899
900
0
  repo_clear_commit_marks(the_repository, SEEN);
901
902
0
  return result;
903
0
}
904
905
static int run_write_commit_graph(struct maintenance_run_opts *opts)
906
0
{
907
0
  struct child_process child = CHILD_PROCESS_INIT;
908
909
0
  child.git_cmd = child.close_object_store = 1;
910
0
  strvec_pushl(&child.args, "commit-graph", "write",
911
0
         "--split", "--reachable", NULL);
912
913
0
  if (opts->quiet)
914
0
    strvec_push(&child.args, "--no-progress");
915
916
0
  return !!run_command(&child);
917
0
}
918
919
static int maintenance_task_commit_graph(struct maintenance_run_opts *opts)
920
0
{
921
0
  prepare_repo_settings(the_repository);
922
0
  if (!the_repository->settings.core_commit_graph)
923
0
    return 0;
924
925
0
  if (run_write_commit_graph(opts)) {
926
0
    error(_("failed to write commit-graph"));
927
0
    return 1;
928
0
  }
929
930
0
  return 0;
931
0
}
932
933
static int fetch_remote(struct remote *remote, void *cbdata)
934
0
{
935
0
  struct maintenance_run_opts *opts = cbdata;
936
0
  struct child_process child = CHILD_PROCESS_INIT;
937
938
0
  if (remote->skip_default_update)
939
0
    return 0;
940
941
0
  child.git_cmd = 1;
942
0
  strvec_pushl(&child.args, "fetch", remote->name,
943
0
         "--prefetch", "--prune", "--no-tags",
944
0
         "--no-write-fetch-head", "--recurse-submodules=no",
945
0
         NULL);
946
947
0
  if (opts->quiet)
948
0
    strvec_push(&child.args, "--quiet");
949
950
0
  return !!run_command(&child);
951
0
}
952
953
static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
954
0
{
955
0
  if (for_each_remote(fetch_remote, opts)) {
956
0
    error(_("failed to prefetch remotes"));
957
0
    return 1;
958
0
  }
959
960
0
  return 0;
961
0
}
962
963
static int maintenance_task_gc(struct maintenance_run_opts *opts)
964
0
{
965
0
  struct child_process child = CHILD_PROCESS_INIT;
966
967
0
  child.git_cmd = child.close_object_store = 1;
968
0
  strvec_push(&child.args, "gc");
969
970
0
  if (opts->auto_flag)
971
0
    strvec_push(&child.args, "--auto");
972
0
  if (opts->quiet)
973
0
    strvec_push(&child.args, "--quiet");
974
0
  else
975
0
    strvec_push(&child.args, "--no-quiet");
976
977
0
  return run_command(&child);
978
0
}
979
980
static int prune_packed(struct maintenance_run_opts *opts)
981
0
{
982
0
  struct child_process child = CHILD_PROCESS_INIT;
983
984
0
  child.git_cmd = 1;
985
0
  strvec_push(&child.args, "prune-packed");
986
987
0
  if (opts->quiet)
988
0
    strvec_push(&child.args, "--quiet");
989
990
0
  return !!run_command(&child);
991
0
}
992
993
struct write_loose_object_data {
994
  FILE *in;
995
  int count;
996
  int batch_size;
997
};
998
999
static int loose_object_auto_limit = 100;
1000
1001
static int loose_object_count(const struct object_id *oid UNUSED,
1002
            const char *path UNUSED,
1003
            void *data)
1004
0
{
1005
0
  int *count = (int*)data;
1006
0
  if (++(*count) >= loose_object_auto_limit)
1007
0
    return 1;
1008
0
  return 0;
1009
0
}
1010
1011
static int loose_object_auto_condition(void)
1012
0
{
1013
0
  int count = 0;
1014
1015
0
  git_config_get_int("maintenance.loose-objects.auto",
1016
0
         &loose_object_auto_limit);
1017
1018
0
  if (!loose_object_auto_limit)
1019
0
    return 0;
1020
0
  if (loose_object_auto_limit < 0)
1021
0
    return 1;
1022
1023
0
  return for_each_loose_file_in_objdir(the_repository->objects->odb->path,
1024
0
               loose_object_count,
1025
0
               NULL, NULL, &count);
1026
0
}
1027
1028
static int bail_on_loose(const struct object_id *oid UNUSED,
1029
       const char *path UNUSED,
1030
       void *data UNUSED)
1031
0
{
1032
0
  return 1;
1033
0
}
1034
1035
static int write_loose_object_to_stdin(const struct object_id *oid,
1036
               const char *path UNUSED,
1037
               void *data)
1038
0
{
1039
0
  struct write_loose_object_data *d = (struct write_loose_object_data *)data;
1040
1041
0
  fprintf(d->in, "%s\n", oid_to_hex(oid));
1042
1043
0
  return ++(d->count) > d->batch_size;
1044
0
}
1045
1046
static int pack_loose(struct maintenance_run_opts *opts)
1047
0
{
1048
0
  struct repository *r = the_repository;
1049
0
  int result = 0;
1050
0
  struct write_loose_object_data data;
1051
0
  struct child_process pack_proc = CHILD_PROCESS_INIT;
1052
1053
  /*
1054
   * Do not start pack-objects process
1055
   * if there are no loose objects.
1056
   */
1057
0
  if (!for_each_loose_file_in_objdir(r->objects->odb->path,
1058
0
             bail_on_loose,
1059
0
             NULL, NULL, NULL))
1060
0
    return 0;
1061
1062
0
  pack_proc.git_cmd = 1;
1063
1064
0
  strvec_push(&pack_proc.args, "pack-objects");
1065
0
  if (opts->quiet)
1066
0
    strvec_push(&pack_proc.args, "--quiet");
1067
0
  strvec_pushf(&pack_proc.args, "%s/pack/loose", r->objects->odb->path);
1068
1069
0
  pack_proc.in = -1;
1070
1071
0
  if (start_command(&pack_proc)) {
1072
0
    error(_("failed to start 'git pack-objects' process"));
1073
0
    return 1;
1074
0
  }
1075
1076
0
  data.in = xfdopen(pack_proc.in, "w");
1077
0
  data.count = 0;
1078
0
  data.batch_size = 50000;
1079
1080
0
  for_each_loose_file_in_objdir(r->objects->odb->path,
1081
0
              write_loose_object_to_stdin,
1082
0
              NULL,
1083
0
              NULL,
1084
0
              &data);
1085
1086
0
  fclose(data.in);
1087
1088
0
  if (finish_command(&pack_proc)) {
1089
0
    error(_("failed to finish 'git pack-objects' process"));
1090
0
    result = 1;
1091
0
  }
1092
1093
0
  return result;
1094
0
}
1095
1096
static int maintenance_task_loose_objects(struct maintenance_run_opts *opts)
1097
0
{
1098
0
  return prune_packed(opts) || pack_loose(opts);
1099
0
}
1100
1101
static int incremental_repack_auto_condition(void)
1102
0
{
1103
0
  struct packed_git *p;
1104
0
  int incremental_repack_auto_limit = 10;
1105
0
  int count = 0;
1106
1107
0
  prepare_repo_settings(the_repository);
1108
0
  if (!the_repository->settings.core_multi_pack_index)
1109
0
    return 0;
1110
1111
0
  git_config_get_int("maintenance.incremental-repack.auto",
1112
0
         &incremental_repack_auto_limit);
1113
1114
0
  if (!incremental_repack_auto_limit)
1115
0
    return 0;
1116
0
  if (incremental_repack_auto_limit < 0)
1117
0
    return 1;
1118
1119
0
  for (p = get_packed_git(the_repository);
1120
0
       count < incremental_repack_auto_limit && p;
1121
0
       p = p->next) {
1122
0
    if (!p->multi_pack_index)
1123
0
      count++;
1124
0
  }
1125
1126
0
  return count >= incremental_repack_auto_limit;
1127
0
}
1128
1129
static int multi_pack_index_write(struct maintenance_run_opts *opts)
1130
0
{
1131
0
  struct child_process child = CHILD_PROCESS_INIT;
1132
1133
0
  child.git_cmd = 1;
1134
0
  strvec_pushl(&child.args, "multi-pack-index", "write", NULL);
1135
1136
0
  if (opts->quiet)
1137
0
    strvec_push(&child.args, "--no-progress");
1138
1139
0
  if (run_command(&child))
1140
0
    return error(_("failed to write multi-pack-index"));
1141
1142
0
  return 0;
1143
0
}
1144
1145
static int multi_pack_index_expire(struct maintenance_run_opts *opts)
1146
0
{
1147
0
  struct child_process child = CHILD_PROCESS_INIT;
1148
1149
0
  child.git_cmd = child.close_object_store = 1;
1150
0
  strvec_pushl(&child.args, "multi-pack-index", "expire", NULL);
1151
1152
0
  if (opts->quiet)
1153
0
    strvec_push(&child.args, "--no-progress");
1154
1155
0
  if (run_command(&child))
1156
0
    return error(_("'git multi-pack-index expire' failed"));
1157
1158
0
  return 0;
1159
0
}
1160
1161
0
#define TWO_GIGABYTES (INT32_MAX)
1162
1163
static off_t get_auto_pack_size(void)
1164
0
{
1165
  /*
1166
   * The "auto" value is special: we optimize for
1167
   * one large pack-file (i.e. from a clone) and
1168
   * expect the rest to be small and they can be
1169
   * repacked quickly.
1170
   *
1171
   * The strategy we select here is to select a
1172
   * size that is one more than the second largest
1173
   * pack-file. This ensures that we will repack
1174
   * at least two packs if there are three or more
1175
   * packs.
1176
   */
1177
0
  off_t max_size = 0;
1178
0
  off_t second_largest_size = 0;
1179
0
  off_t result_size;
1180
0
  struct packed_git *p;
1181
0
  struct repository *r = the_repository;
1182
1183
0
  reprepare_packed_git(r);
1184
0
  for (p = get_all_packs(r); p; p = p->next) {
1185
0
    if (p->pack_size > max_size) {
1186
0
      second_largest_size = max_size;
1187
0
      max_size = p->pack_size;
1188
0
    } else if (p->pack_size > second_largest_size)
1189
0
      second_largest_size = p->pack_size;
1190
0
  }
1191
1192
0
  result_size = second_largest_size + 1;
1193
1194
  /* But limit ourselves to a batch size of 2g */
1195
0
  if (result_size > TWO_GIGABYTES)
1196
0
    result_size = TWO_GIGABYTES;
1197
1198
0
  return result_size;
1199
0
}
1200
1201
static int multi_pack_index_repack(struct maintenance_run_opts *opts)
1202
0
{
1203
0
  struct child_process child = CHILD_PROCESS_INIT;
1204
1205
0
  child.git_cmd = child.close_object_store = 1;
1206
0
  strvec_pushl(&child.args, "multi-pack-index", "repack", NULL);
1207
1208
0
  if (opts->quiet)
1209
0
    strvec_push(&child.args, "--no-progress");
1210
1211
0
  strvec_pushf(&child.args, "--batch-size=%"PRIuMAX,
1212
0
          (uintmax_t)get_auto_pack_size());
1213
1214
0
  if (run_command(&child))
1215
0
    return error(_("'git multi-pack-index repack' failed"));
1216
1217
0
  return 0;
1218
0
}
1219
1220
static int maintenance_task_incremental_repack(struct maintenance_run_opts *opts)
1221
0
{
1222
0
  prepare_repo_settings(the_repository);
1223
0
  if (!the_repository->settings.core_multi_pack_index) {
1224
0
    warning(_("skipping incremental-repack task because core.multiPackIndex is disabled"));
1225
0
    return 0;
1226
0
  }
1227
1228
0
  if (multi_pack_index_write(opts))
1229
0
    return 1;
1230
0
  if (multi_pack_index_expire(opts))
1231
0
    return 1;
1232
0
  if (multi_pack_index_repack(opts))
1233
0
    return 1;
1234
0
  return 0;
1235
0
}
1236
1237
typedef int maintenance_task_fn(struct maintenance_run_opts *opts);
1238
1239
/*
1240
 * An auto condition function returns 1 if the task should run
1241
 * and 0 if the task should NOT run. See needs_to_gc() for an
1242
 * example.
1243
 */
1244
typedef int maintenance_auto_fn(void);
1245
1246
struct maintenance_task {
1247
  const char *name;
1248
  maintenance_task_fn *fn;
1249
  maintenance_auto_fn *auto_condition;
1250
  unsigned enabled:1;
1251
1252
  enum schedule_priority schedule;
1253
1254
  /* -1 if not selected. */
1255
  int selected_order;
1256
};
1257
1258
enum maintenance_task_label {
1259
  TASK_PREFETCH,
1260
  TASK_LOOSE_OBJECTS,
1261
  TASK_INCREMENTAL_REPACK,
1262
  TASK_GC,
1263
  TASK_COMMIT_GRAPH,
1264
  TASK_PACK_REFS,
1265
1266
  /* Leave as final value */
1267
  TASK__COUNT
1268
};
1269
1270
static struct maintenance_task tasks[] = {
1271
  [TASK_PREFETCH] = {
1272
    "prefetch",
1273
    maintenance_task_prefetch,
1274
  },
1275
  [TASK_LOOSE_OBJECTS] = {
1276
    "loose-objects",
1277
    maintenance_task_loose_objects,
1278
    loose_object_auto_condition,
1279
  },
1280
  [TASK_INCREMENTAL_REPACK] = {
1281
    "incremental-repack",
1282
    maintenance_task_incremental_repack,
1283
    incremental_repack_auto_condition,
1284
  },
1285
  [TASK_GC] = {
1286
    "gc",
1287
    maintenance_task_gc,
1288
    need_to_gc,
1289
    1,
1290
  },
1291
  [TASK_COMMIT_GRAPH] = {
1292
    "commit-graph",
1293
    maintenance_task_commit_graph,
1294
    should_write_commit_graph,
1295
  },
1296
  [TASK_PACK_REFS] = {
1297
    "pack-refs",
1298
    maintenance_task_pack_refs,
1299
    NULL,
1300
  },
1301
};
1302
1303
static int compare_tasks_by_selection(const void *a_, const void *b_)
1304
0
{
1305
0
  const struct maintenance_task *a = a_;
1306
0
  const struct maintenance_task *b = b_;
1307
1308
0
  return b->selected_order - a->selected_order;
1309
0
}
1310
1311
static int maintenance_run_tasks(struct maintenance_run_opts *opts)
1312
0
{
1313
0
  int i, found_selected = 0;
1314
0
  int result = 0;
1315
0
  struct lock_file lk;
1316
0
  struct repository *r = the_repository;
1317
0
  char *lock_path = xstrfmt("%s/maintenance", r->objects->odb->path);
1318
1319
0
  if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
1320
    /*
1321
     * Another maintenance command is running.
1322
     *
1323
     * If --auto was provided, then it is likely due to a
1324
     * recursive process stack. Do not report an error in
1325
     * that case.
1326
     */
1327
0
    if (!opts->auto_flag && !opts->quiet)
1328
0
      warning(_("lock file '%s' exists, skipping maintenance"),
1329
0
        lock_path);
1330
0
    free(lock_path);
1331
0
    return 0;
1332
0
  }
1333
0
  free(lock_path);
1334
1335
0
  for (i = 0; !found_selected && i < TASK__COUNT; i++)
1336
0
    found_selected = tasks[i].selected_order >= 0;
1337
1338
0
  if (found_selected)
1339
0
    QSORT(tasks, TASK__COUNT, compare_tasks_by_selection);
1340
1341
0
  for (i = 0; i < TASK__COUNT; i++) {
1342
0
    if (found_selected && tasks[i].selected_order < 0)
1343
0
      continue;
1344
1345
0
    if (!found_selected && !tasks[i].enabled)
1346
0
      continue;
1347
1348
0
    if (opts->auto_flag &&
1349
0
        (!tasks[i].auto_condition ||
1350
0
         !tasks[i].auto_condition()))
1351
0
      continue;
1352
1353
0
    if (opts->schedule && tasks[i].schedule < opts->schedule)
1354
0
      continue;
1355
1356
0
    trace2_region_enter("maintenance", tasks[i].name, r);
1357
0
    if (tasks[i].fn(opts)) {
1358
0
      error(_("task '%s' failed"), tasks[i].name);
1359
0
      result = 1;
1360
0
    }
1361
0
    trace2_region_leave("maintenance", tasks[i].name, r);
1362
0
  }
1363
1364
0
  rollback_lock_file(&lk);
1365
0
  return result;
1366
0
}
1367
1368
static void initialize_maintenance_strategy(void)
1369
0
{
1370
0
  char *config_str;
1371
1372
0
  if (git_config_get_string("maintenance.strategy", &config_str))
1373
0
    return;
1374
1375
0
  if (!strcasecmp(config_str, "incremental")) {
1376
0
    tasks[TASK_GC].schedule = SCHEDULE_NONE;
1377
0
    tasks[TASK_COMMIT_GRAPH].enabled = 1;
1378
0
    tasks[TASK_COMMIT_GRAPH].schedule = SCHEDULE_HOURLY;
1379
0
    tasks[TASK_PREFETCH].enabled = 1;
1380
0
    tasks[TASK_PREFETCH].schedule = SCHEDULE_HOURLY;
1381
0
    tasks[TASK_INCREMENTAL_REPACK].enabled = 1;
1382
0
    tasks[TASK_INCREMENTAL_REPACK].schedule = SCHEDULE_DAILY;
1383
0
    tasks[TASK_LOOSE_OBJECTS].enabled = 1;
1384
0
    tasks[TASK_LOOSE_OBJECTS].schedule = SCHEDULE_DAILY;
1385
0
    tasks[TASK_PACK_REFS].enabled = 1;
1386
0
    tasks[TASK_PACK_REFS].schedule = SCHEDULE_WEEKLY;
1387
0
  }
1388
0
}
1389
1390
static void initialize_task_config(int schedule)
1391
0
{
1392
0
  int i;
1393
0
  struct strbuf config_name = STRBUF_INIT;
1394
0
  gc_config();
1395
1396
0
  if (schedule)
1397
0
    initialize_maintenance_strategy();
1398
1399
0
  for (i = 0; i < TASK__COUNT; i++) {
1400
0
    int config_value;
1401
0
    char *config_str;
1402
1403
0
    strbuf_reset(&config_name);
1404
0
    strbuf_addf(&config_name, "maintenance.%s.enabled",
1405
0
          tasks[i].name);
1406
1407
0
    if (!git_config_get_bool(config_name.buf, &config_value))
1408
0
      tasks[i].enabled = config_value;
1409
1410
0
    strbuf_reset(&config_name);
1411
0
    strbuf_addf(&config_name, "maintenance.%s.schedule",
1412
0
          tasks[i].name);
1413
1414
0
    if (!git_config_get_string(config_name.buf, &config_str)) {
1415
0
      tasks[i].schedule = parse_schedule(config_str);
1416
0
      free(config_str);
1417
0
    }
1418
0
  }
1419
1420
0
  strbuf_release(&config_name);
1421
0
}
1422
1423
static int task_option_parse(const struct option *opt UNUSED,
1424
           const char *arg, int unset)
1425
0
{
1426
0
  int i, num_selected = 0;
1427
0
  struct maintenance_task *task = NULL;
1428
1429
0
  BUG_ON_OPT_NEG(unset);
1430
1431
0
  for (i = 0; i < TASK__COUNT; i++) {
1432
0
    if (tasks[i].selected_order >= 0)
1433
0
      num_selected++;
1434
0
    if (!strcasecmp(tasks[i].name, arg)) {
1435
0
      task = &tasks[i];
1436
0
    }
1437
0
  }
1438
1439
0
  if (!task) {
1440
0
    error(_("'%s' is not a valid task"), arg);
1441
0
    return 1;
1442
0
  }
1443
1444
0
  if (task->selected_order >= 0) {
1445
0
    error(_("task '%s' cannot be selected multiple times"), arg);
1446
0
    return 1;
1447
0
  }
1448
1449
0
  task->selected_order = num_selected + 1;
1450
1451
0
  return 0;
1452
0
}
1453
1454
static int maintenance_run(int argc, const char **argv, const char *prefix)
1455
0
{
1456
0
  int i;
1457
0
  struct maintenance_run_opts opts;
1458
0
  struct option builtin_maintenance_run_options[] = {
1459
0
    OPT_BOOL(0, "auto", &opts.auto_flag,
1460
0
       N_("run tasks based on the state of the repository")),
1461
0
    OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1462
0
           N_("run tasks based on frequency"),
1463
0
           maintenance_opt_schedule),
1464
0
    OPT_BOOL(0, "quiet", &opts.quiet,
1465
0
       N_("do not report progress or other information over stderr")),
1466
0
    OPT_CALLBACK_F(0, "task", NULL, N_("task"),
1467
0
      N_("run a specific task"),
1468
0
      PARSE_OPT_NONEG, task_option_parse),
1469
0
    OPT_END()
1470
0
  };
1471
0
  memset(&opts, 0, sizeof(opts));
1472
1473
0
  opts.quiet = !isatty(2);
1474
1475
0
  for (i = 0; i < TASK__COUNT; i++)
1476
0
    tasks[i].selected_order = -1;
1477
1478
0
  argc = parse_options(argc, argv, prefix,
1479
0
           builtin_maintenance_run_options,
1480
0
           builtin_maintenance_run_usage,
1481
0
           PARSE_OPT_STOP_AT_NON_OPTION);
1482
1483
0
  if (opts.auto_flag && opts.schedule)
1484
0
    die(_("use at most one of --auto and --schedule=<frequency>"));
1485
1486
0
  initialize_task_config(opts.schedule);
1487
1488
0
  if (argc != 0)
1489
0
    usage_with_options(builtin_maintenance_run_usage,
1490
0
           builtin_maintenance_run_options);
1491
0
  return maintenance_run_tasks(&opts);
1492
0
}
1493
1494
static char *get_maintpath(void)
1495
0
{
1496
0
  struct strbuf sb = STRBUF_INIT;
1497
0
  const char *p = the_repository->worktree ?
1498
0
    the_repository->worktree : the_repository->gitdir;
1499
1500
0
  strbuf_realpath(&sb, p, 1);
1501
0
  return strbuf_detach(&sb, NULL);
1502
0
}
1503
1504
static char const * const builtin_maintenance_register_usage[] = {
1505
  "git maintenance register [--config-file <path>]",
1506
  NULL
1507
};
1508
1509
static int maintenance_register(int argc, const char **argv, const char *prefix)
1510
0
{
1511
0
  char *config_file = NULL;
1512
0
  struct option options[] = {
1513
0
    OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1514
0
    OPT_END(),
1515
0
  };
1516
0
  int found = 0;
1517
0
  const char *key = "maintenance.repo";
1518
0
  char *maintpath = get_maintpath();
1519
0
  struct string_list_item *item;
1520
0
  const struct string_list *list;
1521
1522
0
  argc = parse_options(argc, argv, prefix, options,
1523
0
           builtin_maintenance_register_usage, 0);
1524
0
  if (argc)
1525
0
    usage_with_options(builtin_maintenance_register_usage,
1526
0
           options);
1527
1528
  /* Disable foreground maintenance */
1529
0
  git_config_set("maintenance.auto", "false");
1530
1531
  /* Set maintenance strategy, if unset */
1532
0
  if (git_config_get("maintenance.strategy"))
1533
0
    git_config_set("maintenance.strategy", "incremental");
1534
1535
0
  if (!git_config_get_string_multi(key, &list)) {
1536
0
    for_each_string_list_item(item, list) {
1537
0
      if (!strcmp(maintpath, item->string)) {
1538
0
        found = 1;
1539
0
        break;
1540
0
      }
1541
0
    }
1542
0
  }
1543
1544
0
  if (!found) {
1545
0
    int rc;
1546
0
    char *user_config = NULL, *xdg_config = NULL;
1547
1548
0
    if (!config_file) {
1549
0
      git_global_config(&user_config, &xdg_config);
1550
0
      config_file = user_config;
1551
0
      if (!user_config)
1552
0
        die(_("$HOME not set"));
1553
0
    }
1554
0
    rc = git_config_set_multivar_in_file_gently(
1555
0
      config_file, "maintenance.repo", maintpath,
1556
0
      CONFIG_REGEX_NONE, 0);
1557
0
    free(user_config);
1558
0
    free(xdg_config);
1559
1560
0
    if (rc)
1561
0
      die(_("unable to add '%s' value of '%s'"),
1562
0
          key, maintpath);
1563
0
  }
1564
1565
0
  free(maintpath);
1566
0
  return 0;
1567
0
}
1568
1569
static char const * const builtin_maintenance_unregister_usage[] = {
1570
  "git maintenance unregister [--config-file <path>] [--force]",
1571
  NULL
1572
};
1573
1574
static int maintenance_unregister(int argc, const char **argv, const char *prefix)
1575
0
{
1576
0
  int force = 0;
1577
0
  char *config_file = NULL;
1578
0
  struct option options[] = {
1579
0
    OPT_STRING(0, "config-file", &config_file, N_("file"), N_("use given config file")),
1580
0
    OPT__FORCE(&force,
1581
0
         N_("return success even if repository was not registered"),
1582
0
         PARSE_OPT_NOCOMPLETE),
1583
0
    OPT_END(),
1584
0
  };
1585
0
  const char *key = "maintenance.repo";
1586
0
  char *maintpath = get_maintpath();
1587
0
  int found = 0;
1588
0
  struct string_list_item *item;
1589
0
  const struct string_list *list;
1590
0
  struct config_set cs = { { 0 } };
1591
1592
0
  argc = parse_options(argc, argv, prefix, options,
1593
0
           builtin_maintenance_unregister_usage, 0);
1594
0
  if (argc)
1595
0
    usage_with_options(builtin_maintenance_unregister_usage,
1596
0
           options);
1597
1598
0
  if (config_file) {
1599
0
    git_configset_init(&cs);
1600
0
    git_configset_add_file(&cs, config_file);
1601
0
  }
1602
0
  if (!(config_file
1603
0
        ? git_configset_get_string_multi(&cs, key, &list)
1604
0
        : git_config_get_string_multi(key, &list))) {
1605
0
    for_each_string_list_item(item, list) {
1606
0
      if (!strcmp(maintpath, item->string)) {
1607
0
        found = 1;
1608
0
        break;
1609
0
      }
1610
0
    }
1611
0
  }
1612
1613
0
  if (found) {
1614
0
    int rc;
1615
0
    char *user_config = NULL, *xdg_config = NULL;
1616
0
    if (!config_file) {
1617
0
      git_global_config(&user_config, &xdg_config);
1618
0
      config_file = user_config;
1619
0
      if (!user_config)
1620
0
        die(_("$HOME not set"));
1621
0
    }
1622
0
    rc = git_config_set_multivar_in_file_gently(
1623
0
      config_file, key, NULL, maintpath,
1624
0
      CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1625
0
    free(user_config);
1626
0
    free(xdg_config);
1627
1628
0
    if (rc &&
1629
0
        (!force || rc == CONFIG_NOTHING_SET))
1630
0
      die(_("unable to unset '%s' value of '%s'"),
1631
0
          key, maintpath);
1632
0
  } else if (!force) {
1633
0
    die(_("repository '%s' is not registered"), maintpath);
1634
0
  }
1635
1636
0
  git_configset_clear(&cs);
1637
0
  free(maintpath);
1638
0
  return 0;
1639
0
}
1640
1641
static const char *get_frequency(enum schedule_priority schedule)
1642
0
{
1643
0
  switch (schedule) {
1644
0
  case SCHEDULE_HOURLY:
1645
0
    return "hourly";
1646
0
  case SCHEDULE_DAILY:
1647
0
    return "daily";
1648
0
  case SCHEDULE_WEEKLY:
1649
0
    return "weekly";
1650
0
  default:
1651
0
    BUG("invalid schedule %d", schedule);
1652
0
  }
1653
0
}
1654
1655
/*
1656
 * get_schedule_cmd` reads the GIT_TEST_MAINT_SCHEDULER environment variable
1657
 * to mock the schedulers that `git maintenance start` rely on.
1658
 *
1659
 * For test purpose, GIT_TEST_MAINT_SCHEDULER can be set to a comma-separated
1660
 * list of colon-separated key/value pairs where each pair contains a scheduler
1661
 * and its corresponding mock.
1662
 *
1663
 * * If $GIT_TEST_MAINT_SCHEDULER is not set, return false and leave the
1664
 *   arguments unmodified.
1665
 *
1666
 * * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1667
 *   In this case, the *cmd value is read as input.
1668
 *
1669
 *   * if the input value *cmd is the key of one of the comma-separated list
1670
 *     item, then *is_available is set to true and *cmd is modified and becomes
1671
 *     the mock command.
1672
 *
1673
 *   * if the input value *cmd isn’t the key of any of the comma-separated list
1674
 *     item, then *is_available is set to false.
1675
 *
1676
 * Ex.:
1677
 *   GIT_TEST_MAINT_SCHEDULER not set
1678
 *     +-------+-------------------------------------------------+
1679
 *     | Input |                     Output                      |
1680
 *     | *cmd  | return code |       *cmd        | *is_available |
1681
 *     +-------+-------------+-------------------+---------------+
1682
 *     | "foo" |    false    | "foo" (unchanged) |  (unchanged)  |
1683
 *     +-------+-------------+-------------------+---------------+
1684
 *
1685
 *   GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1686
 *     +-------+-------------------------------------------------+
1687
 *     | Input |                     Output                      |
1688
 *     | *cmd  | return code |       *cmd        | *is_available |
1689
 *     +-------+-------------+-------------------+---------------+
1690
 *     | "foo" |    true     |  "./mock.foo.sh"  |     true      |
1691
 *     | "qux" |    true     | "qux" (unchanged) |     false     |
1692
 *     +-------+-------------+-------------------+---------------+
1693
 */
1694
static int get_schedule_cmd(const char **cmd, int *is_available)
1695
0
{
1696
0
  char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
1697
0
  struct string_list_item *item;
1698
0
  struct string_list list = STRING_LIST_INIT_NODUP;
1699
1700
0
  if (!testing)
1701
0
    return 0;
1702
1703
0
  if (is_available)
1704
0
    *is_available = 0;
1705
1706
0
  string_list_split_in_place(&list, testing, ",", -1);
1707
0
  for_each_string_list_item(item, &list) {
1708
0
    struct string_list pair = STRING_LIST_INIT_NODUP;
1709
1710
0
    if (string_list_split_in_place(&pair, item->string, ":", 2) != 2)
1711
0
      continue;
1712
1713
0
    if (!strcmp(*cmd, pair.items[0].string)) {
1714
0
      *cmd = pair.items[1].string;
1715
0
      if (is_available)
1716
0
        *is_available = 1;
1717
0
      string_list_clear(&list, 0);
1718
0
      UNLEAK(testing);
1719
0
      return 1;
1720
0
    }
1721
0
  }
1722
1723
0
  string_list_clear(&list, 0);
1724
0
  free(testing);
1725
0
  return 1;
1726
0
}
1727
1728
static int get_random_minute(void)
1729
0
{
1730
  /* Use a static value when under tests. */
1731
0
  if (getenv("GIT_TEST_MAINT_SCHEDULER"))
1732
0
    return 13;
1733
1734
0
  return git_rand() % 60;
1735
0
}
1736
1737
static int is_launchctl_available(void)
1738
0
{
1739
0
  const char *cmd = "launchctl";
1740
0
  int is_available;
1741
0
  if (get_schedule_cmd(&cmd, &is_available))
1742
0
    return is_available;
1743
1744
#ifdef __APPLE__
1745
  return 1;
1746
#else
1747
0
  return 0;
1748
0
#endif
1749
0
}
1750
1751
static char *launchctl_service_name(const char *frequency)
1752
0
{
1753
0
  struct strbuf label = STRBUF_INIT;
1754
0
  strbuf_addf(&label, "org.git-scm.git.%s", frequency);
1755
0
  return strbuf_detach(&label, NULL);
1756
0
}
1757
1758
static char *launchctl_service_filename(const char *name)
1759
0
{
1760
0
  char *expanded;
1761
0
  struct strbuf filename = STRBUF_INIT;
1762
0
  strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
1763
1764
0
  expanded = interpolate_path(filename.buf, 1);
1765
0
  if (!expanded)
1766
0
    die(_("failed to expand path '%s'"), filename.buf);
1767
1768
0
  strbuf_release(&filename);
1769
0
  return expanded;
1770
0
}
1771
1772
static char *launchctl_get_uid(void)
1773
0
{
1774
0
  return xstrfmt("gui/%d", getuid());
1775
0
}
1776
1777
static int launchctl_boot_plist(int enable, const char *filename)
1778
0
{
1779
0
  const char *cmd = "launchctl";
1780
0
  int result;
1781
0
  struct child_process child = CHILD_PROCESS_INIT;
1782
0
  char *uid = launchctl_get_uid();
1783
1784
0
  get_schedule_cmd(&cmd, NULL);
1785
0
  strvec_split(&child.args, cmd);
1786
0
  strvec_pushl(&child.args, enable ? "bootstrap" : "bootout", uid,
1787
0
         filename, NULL);
1788
1789
0
  child.no_stderr = 1;
1790
0
  child.no_stdout = 1;
1791
1792
0
  if (start_command(&child))
1793
0
    die(_("failed to start launchctl"));
1794
1795
0
  result = finish_command(&child);
1796
1797
0
  free(uid);
1798
0
  return result;
1799
0
}
1800
1801
static int launchctl_remove_plist(enum schedule_priority schedule)
1802
0
{
1803
0
  const char *frequency = get_frequency(schedule);
1804
0
  char *name = launchctl_service_name(frequency);
1805
0
  char *filename = launchctl_service_filename(name);
1806
0
  int result = launchctl_boot_plist(0, filename);
1807
0
  unlink(filename);
1808
0
  free(filename);
1809
0
  free(name);
1810
0
  return result;
1811
0
}
1812
1813
static int launchctl_remove_plists(void)
1814
0
{
1815
0
  return launchctl_remove_plist(SCHEDULE_HOURLY) ||
1816
0
         launchctl_remove_plist(SCHEDULE_DAILY) ||
1817
0
         launchctl_remove_plist(SCHEDULE_WEEKLY);
1818
0
}
1819
1820
static int launchctl_list_contains_plist(const char *name, const char *cmd)
1821
0
{
1822
0
  struct child_process child = CHILD_PROCESS_INIT;
1823
1824
0
  strvec_split(&child.args, cmd);
1825
0
  strvec_pushl(&child.args, "list", name, NULL);
1826
1827
0
  child.no_stderr = 1;
1828
0
  child.no_stdout = 1;
1829
1830
0
  if (start_command(&child))
1831
0
    die(_("failed to start launchctl"));
1832
1833
  /* Returns failure if 'name' doesn't exist. */
1834
0
  return !finish_command(&child);
1835
0
}
1836
1837
static int launchctl_schedule_plist(const char *exec_path, enum schedule_priority schedule)
1838
0
{
1839
0
  int i, fd;
1840
0
  const char *preamble, *repeat;
1841
0
  const char *frequency = get_frequency(schedule);
1842
0
  char *name = launchctl_service_name(frequency);
1843
0
  char *filename = launchctl_service_filename(name);
1844
0
  struct lock_file lk = LOCK_INIT;
1845
0
  static unsigned long lock_file_timeout_ms = ULONG_MAX;
1846
0
  struct strbuf plist = STRBUF_INIT, plist2 = STRBUF_INIT;
1847
0
  struct stat st;
1848
0
  const char *cmd = "launchctl";
1849
0
  int minute = get_random_minute();
1850
1851
0
  get_schedule_cmd(&cmd, NULL);
1852
0
  preamble = "<?xml version=\"1.0\"?>\n"
1853
0
       "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1854
0
       "<plist version=\"1.0\">"
1855
0
       "<dict>\n"
1856
0
       "<key>Label</key><string>%s</string>\n"
1857
0
       "<key>ProgramArguments</key>\n"
1858
0
       "<array>\n"
1859
0
       "<string>%s/git</string>\n"
1860
0
       "<string>--exec-path=%s</string>\n"
1861
0
       "<string>for-each-repo</string>\n"
1862
0
       "<string>--config=maintenance.repo</string>\n"
1863
0
       "<string>maintenance</string>\n"
1864
0
       "<string>run</string>\n"
1865
0
       "<string>--schedule=%s</string>\n"
1866
0
       "</array>\n"
1867
0
       "<key>StartCalendarInterval</key>\n"
1868
0
       "<array>\n";
1869
0
  strbuf_addf(&plist, preamble, name, exec_path, exec_path, frequency);
1870
1871
0
  switch (schedule) {
1872
0
  case SCHEDULE_HOURLY:
1873
0
    repeat = "<dict>\n"
1874
0
       "<key>Hour</key><integer>%d</integer>\n"
1875
0
       "<key>Minute</key><integer>%d</integer>\n"
1876
0
       "</dict>\n";
1877
0
    for (i = 1; i <= 23; i++)
1878
0
      strbuf_addf(&plist, repeat, i, minute);
1879
0
    break;
1880
1881
0
  case SCHEDULE_DAILY:
1882
0
    repeat = "<dict>\n"
1883
0
       "<key>Day</key><integer>%d</integer>\n"
1884
0
       "<key>Hour</key><integer>0</integer>\n"
1885
0
       "<key>Minute</key><integer>%d</integer>\n"
1886
0
       "</dict>\n";
1887
0
    for (i = 1; i <= 6; i++)
1888
0
      strbuf_addf(&plist, repeat, i, minute);
1889
0
    break;
1890
1891
0
  case SCHEDULE_WEEKLY:
1892
0
    strbuf_addf(&plist,
1893
0
          "<dict>\n"
1894
0
          "<key>Day</key><integer>0</integer>\n"
1895
0
          "<key>Hour</key><integer>0</integer>\n"
1896
0
          "<key>Minute</key><integer>%d</integer>\n"
1897
0
          "</dict>\n",
1898
0
          minute);
1899
0
    break;
1900
1901
0
  default:
1902
    /* unreachable */
1903
0
    break;
1904
0
  }
1905
0
  strbuf_addstr(&plist, "</array>\n</dict>\n</plist>\n");
1906
1907
0
  if (safe_create_leading_directories(filename))
1908
0
    die(_("failed to create directories for '%s'"), filename);
1909
1910
0
  if ((long)lock_file_timeout_ms < 0 &&
1911
0
      git_config_get_ulong("gc.launchctlplistlocktimeoutms",
1912
0
         &lock_file_timeout_ms))
1913
0
    lock_file_timeout_ms = 150;
1914
1915
0
  fd = hold_lock_file_for_update_timeout(&lk, filename, LOCK_DIE_ON_ERROR,
1916
0
                 lock_file_timeout_ms);
1917
1918
  /*
1919
   * Does this file already exist? With the intended contents? Is it
1920
   * registered already? Then it does not need to be re-registered.
1921
   */
1922
0
  if (!stat(filename, &st) && st.st_size == plist.len &&
1923
0
      strbuf_read_file(&plist2, filename, plist.len) == plist.len &&
1924
0
      !strbuf_cmp(&plist, &plist2) &&
1925
0
      launchctl_list_contains_plist(name, cmd))
1926
0
    rollback_lock_file(&lk);
1927
0
  else {
1928
0
    if (write_in_full(fd, plist.buf, plist.len) < 0 ||
1929
0
        commit_lock_file(&lk))
1930
0
      die_errno(_("could not write '%s'"), filename);
1931
1932
    /* bootout might fail if not already running, so ignore */
1933
0
    launchctl_boot_plist(0, filename);
1934
0
    if (launchctl_boot_plist(1, filename))
1935
0
      die(_("failed to bootstrap service %s"), filename);
1936
0
  }
1937
1938
0
  free(filename);
1939
0
  free(name);
1940
0
  strbuf_release(&plist);
1941
0
  strbuf_release(&plist2);
1942
0
  return 0;
1943
0
}
1944
1945
static int launchctl_add_plists(void)
1946
0
{
1947
0
  const char *exec_path = git_exec_path();
1948
1949
0
  return launchctl_schedule_plist(exec_path, SCHEDULE_HOURLY) ||
1950
0
         launchctl_schedule_plist(exec_path, SCHEDULE_DAILY) ||
1951
0
         launchctl_schedule_plist(exec_path, SCHEDULE_WEEKLY);
1952
0
}
1953
1954
static int launchctl_update_schedule(int run_maintenance, int fd UNUSED)
1955
0
{
1956
0
  if (run_maintenance)
1957
0
    return launchctl_add_plists();
1958
0
  else
1959
0
    return launchctl_remove_plists();
1960
0
}
1961
1962
static int is_schtasks_available(void)
1963
0
{
1964
0
  const char *cmd = "schtasks";
1965
0
  int is_available;
1966
0
  if (get_schedule_cmd(&cmd, &is_available))
1967
0
    return is_available;
1968
1969
#ifdef GIT_WINDOWS_NATIVE
1970
  return 1;
1971
#else
1972
0
  return 0;
1973
0
#endif
1974
0
}
1975
1976
static char *schtasks_task_name(const char *frequency)
1977
0
{
1978
0
  struct strbuf label = STRBUF_INIT;
1979
0
  strbuf_addf(&label, "Git Maintenance (%s)", frequency);
1980
0
  return strbuf_detach(&label, NULL);
1981
0
}
1982
1983
static int schtasks_remove_task(enum schedule_priority schedule)
1984
0
{
1985
0
  const char *cmd = "schtasks";
1986
0
  struct child_process child = CHILD_PROCESS_INIT;
1987
0
  const char *frequency = get_frequency(schedule);
1988
0
  char *name = schtasks_task_name(frequency);
1989
1990
0
  get_schedule_cmd(&cmd, NULL);
1991
0
  strvec_split(&child.args, cmd);
1992
0
  strvec_pushl(&child.args, "/delete", "/tn", name, "/f", NULL);
1993
0
  free(name);
1994
1995
0
  return run_command(&child);
1996
0
}
1997
1998
static int schtasks_remove_tasks(void)
1999
0
{
2000
0
  return schtasks_remove_task(SCHEDULE_HOURLY) ||
2001
0
         schtasks_remove_task(SCHEDULE_DAILY) ||
2002
0
         schtasks_remove_task(SCHEDULE_WEEKLY);
2003
0
}
2004
2005
static int schtasks_schedule_task(const char *exec_path, enum schedule_priority schedule)
2006
0
{
2007
0
  const char *cmd = "schtasks";
2008
0
  int result;
2009
0
  struct child_process child = CHILD_PROCESS_INIT;
2010
0
  const char *xml;
2011
0
  struct tempfile *tfile;
2012
0
  const char *frequency = get_frequency(schedule);
2013
0
  char *name = schtasks_task_name(frequency);
2014
0
  struct strbuf tfilename = STRBUF_INIT;
2015
0
  int minute = get_random_minute();
2016
2017
0
  get_schedule_cmd(&cmd, NULL);
2018
2019
0
  strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
2020
0
        get_git_common_dir(), frequency);
2021
0
  tfile = xmks_tempfile(tfilename.buf);
2022
0
  strbuf_release(&tfilename);
2023
2024
0
  if (!fdopen_tempfile(tfile, "w"))
2025
0
    die(_("failed to create temp xml file"));
2026
2027
0
  xml = "<?xml version=\"1.0\" ?>\n"
2028
0
        "<Task version=\"1.4\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
2029
0
        "<Triggers>\n"
2030
0
        "<CalendarTrigger>\n";
2031
0
  fputs(xml, tfile->fp);
2032
2033
0
  switch (schedule) {
2034
0
  case SCHEDULE_HOURLY:
2035
0
    fprintf(tfile->fp,
2036
0
      "<StartBoundary>2020-01-01T01:%02d:00</StartBoundary>\n"
2037
0
      "<Enabled>true</Enabled>\n"
2038
0
      "<ScheduleByDay>\n"
2039
0
      "<DaysInterval>1</DaysInterval>\n"
2040
0
      "</ScheduleByDay>\n"
2041
0
      "<Repetition>\n"
2042
0
      "<Interval>PT1H</Interval>\n"
2043
0
      "<Duration>PT23H</Duration>\n"
2044
0
      "<StopAtDurationEnd>false</StopAtDurationEnd>\n"
2045
0
      "</Repetition>\n",
2046
0
      minute);
2047
0
    break;
2048
2049
0
  case SCHEDULE_DAILY:
2050
0
    fprintf(tfile->fp,
2051
0
      "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2052
0
      "<Enabled>true</Enabled>\n"
2053
0
      "<ScheduleByWeek>\n"
2054
0
      "<DaysOfWeek>\n"
2055
0
      "<Monday />\n"
2056
0
      "<Tuesday />\n"
2057
0
      "<Wednesday />\n"
2058
0
      "<Thursday />\n"
2059
0
      "<Friday />\n"
2060
0
      "<Saturday />\n"
2061
0
      "</DaysOfWeek>\n"
2062
0
      "<WeeksInterval>1</WeeksInterval>\n"
2063
0
      "</ScheduleByWeek>\n",
2064
0
      minute);
2065
0
    break;
2066
2067
0
  case SCHEDULE_WEEKLY:
2068
0
    fprintf(tfile->fp,
2069
0
      "<StartBoundary>2020-01-01T00:%02d:00</StartBoundary>\n"
2070
0
      "<Enabled>true</Enabled>\n"
2071
0
      "<ScheduleByWeek>\n"
2072
0
      "<DaysOfWeek>\n"
2073
0
      "<Sunday />\n"
2074
0
      "</DaysOfWeek>\n"
2075
0
      "<WeeksInterval>1</WeeksInterval>\n"
2076
0
      "</ScheduleByWeek>\n",
2077
0
      minute);
2078
0
    break;
2079
2080
0
  default:
2081
0
    break;
2082
0
  }
2083
2084
0
  xml = "</CalendarTrigger>\n"
2085
0
        "</Triggers>\n"
2086
0
        "<Principals>\n"
2087
0
        "<Principal id=\"Author\">\n"
2088
0
        "<LogonType>InteractiveToken</LogonType>\n"
2089
0
        "<RunLevel>LeastPrivilege</RunLevel>\n"
2090
0
        "</Principal>\n"
2091
0
        "</Principals>\n"
2092
0
        "<Settings>\n"
2093
0
        "<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>\n"
2094
0
        "<Enabled>true</Enabled>\n"
2095
0
        "<Hidden>true</Hidden>\n"
2096
0
        "<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>\n"
2097
0
        "<WakeToRun>false</WakeToRun>\n"
2098
0
        "<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>\n"
2099
0
        "<Priority>7</Priority>\n"
2100
0
        "</Settings>\n"
2101
0
        "<Actions Context=\"Author\">\n"
2102
0
        "<Exec>\n"
2103
0
        "<Command>\"%s\\headless-git.exe\"</Command>\n"
2104
0
        "<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2105
0
        "</Exec>\n"
2106
0
        "</Actions>\n"
2107
0
        "</Task>\n";
2108
0
  fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
2109
0
  strvec_split(&child.args, cmd);
2110
0
  strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
2111
0
          get_tempfile_path(tfile), NULL);
2112
0
  close_tempfile_gently(tfile);
2113
2114
0
  child.no_stdout = 1;
2115
0
  child.no_stderr = 1;
2116
2117
0
  if (start_command(&child))
2118
0
    die(_("failed to start schtasks"));
2119
0
  result = finish_command(&child);
2120
2121
0
  delete_tempfile(&tfile);
2122
0
  free(name);
2123
0
  return result;
2124
0
}
2125
2126
static int schtasks_schedule_tasks(void)
2127
0
{
2128
0
  const char *exec_path = git_exec_path();
2129
2130
0
  return schtasks_schedule_task(exec_path, SCHEDULE_HOURLY) ||
2131
0
         schtasks_schedule_task(exec_path, SCHEDULE_DAILY) ||
2132
0
         schtasks_schedule_task(exec_path, SCHEDULE_WEEKLY);
2133
0
}
2134
2135
static int schtasks_update_schedule(int run_maintenance, int fd UNUSED)
2136
0
{
2137
0
  if (run_maintenance)
2138
0
    return schtasks_schedule_tasks();
2139
0
  else
2140
0
    return schtasks_remove_tasks();
2141
0
}
2142
2143
MAYBE_UNUSED
2144
static int check_crontab_process(const char *cmd)
2145
0
{
2146
0
  struct child_process child = CHILD_PROCESS_INIT;
2147
2148
0
  strvec_split(&child.args, cmd);
2149
0
  strvec_push(&child.args, "-l");
2150
0
  child.no_stdin = 1;
2151
0
  child.no_stdout = 1;
2152
0
  child.no_stderr = 1;
2153
0
  child.silent_exec_failure = 1;
2154
2155
0
  if (start_command(&child))
2156
0
    return 0;
2157
  /* Ignore exit code, as an empty crontab will return error. */
2158
0
  finish_command(&child);
2159
0
  return 1;
2160
0
}
2161
2162
static int is_crontab_available(void)
2163
0
{
2164
0
  const char *cmd = "crontab";
2165
0
  int is_available;
2166
2167
0
  if (get_schedule_cmd(&cmd, &is_available))
2168
0
    return is_available;
2169
2170
#ifdef __APPLE__
2171
  /*
2172
   * macOS has cron, but it requires special permissions and will
2173
   * create a UI alert when attempting to run this command.
2174
   */
2175
  return 0;
2176
#else
2177
0
  return check_crontab_process(cmd);
2178
0
#endif
2179
0
}
2180
2181
0
#define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2182
0
#define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2183
2184
static int crontab_update_schedule(int run_maintenance, int fd)
2185
0
{
2186
0
  const char *cmd = "crontab";
2187
0
  int result = 0;
2188
0
  int in_old_region = 0;
2189
0
  struct child_process crontab_list = CHILD_PROCESS_INIT;
2190
0
  struct child_process crontab_edit = CHILD_PROCESS_INIT;
2191
0
  FILE *cron_list, *cron_in;
2192
0
  struct strbuf line = STRBUF_INIT;
2193
0
  struct tempfile *tmpedit = NULL;
2194
0
  int minute = get_random_minute();
2195
2196
0
  get_schedule_cmd(&cmd, NULL);
2197
0
  strvec_split(&crontab_list.args, cmd);
2198
0
  strvec_push(&crontab_list.args, "-l");
2199
0
  crontab_list.in = -1;
2200
0
  crontab_list.out = dup(fd);
2201
0
  crontab_list.git_cmd = 0;
2202
2203
0
  if (start_command(&crontab_list))
2204
0
    return error(_("failed to run 'crontab -l'; your system might not support 'cron'"));
2205
2206
  /* Ignore exit code, as an empty crontab will return error. */
2207
0
  finish_command(&crontab_list);
2208
2209
0
  tmpedit = mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
2210
0
  if (!tmpedit) {
2211
0
    result = error(_("failed to create crontab temporary file"));
2212
0
    goto out;
2213
0
  }
2214
0
  cron_in = fdopen_tempfile(tmpedit, "w");
2215
0
  if (!cron_in) {
2216
0
    result = error(_("failed to open temporary file"));
2217
0
    goto out;
2218
0
  }
2219
2220
  /*
2221
   * Read from the .lock file, filtering out the old
2222
   * schedule while appending the new schedule.
2223
   */
2224
0
  cron_list = fdopen(fd, "r");
2225
0
  rewind(cron_list);
2226
2227
0
  while (!strbuf_getline_lf(&line, cron_list)) {
2228
0
    if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
2229
0
      in_old_region = 1;
2230
0
    else if (in_old_region && !strcmp(line.buf, END_LINE))
2231
0
      in_old_region = 0;
2232
0
    else if (!in_old_region)
2233
0
      fprintf(cron_in, "%s\n", line.buf);
2234
0
  }
2235
0
  strbuf_release(&line);
2236
2237
0
  if (run_maintenance) {
2238
0
    struct strbuf line_format = STRBUF_INIT;
2239
0
    const char *exec_path = git_exec_path();
2240
2241
0
    fprintf(cron_in, "%s\n", BEGIN_LINE);
2242
0
    fprintf(cron_in,
2243
0
      "# The following schedule was created by Git\n");
2244
0
    fprintf(cron_in, "# Any edits made in this region might be\n");
2245
0
    fprintf(cron_in,
2246
0
      "# replaced in the future by a Git command.\n\n");
2247
2248
0
    strbuf_addf(&line_format,
2249
0
          "%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2250
0
          exec_path, exec_path);
2251
0
    fprintf(cron_in, line_format.buf, minute, "1-23", "*", "hourly");
2252
0
    fprintf(cron_in, line_format.buf, minute, "0", "1-6", "daily");
2253
0
    fprintf(cron_in, line_format.buf, minute, "0", "0", "weekly");
2254
0
    strbuf_release(&line_format);
2255
2256
0
    fprintf(cron_in, "\n%s\n", END_LINE);
2257
0
  }
2258
2259
0
  fflush(cron_in);
2260
2261
0
  strvec_split(&crontab_edit.args, cmd);
2262
0
  strvec_push(&crontab_edit.args, get_tempfile_path(tmpedit));
2263
0
  crontab_edit.git_cmd = 0;
2264
2265
0
  if (start_command(&crontab_edit)) {
2266
0
    result = error(_("failed to run 'crontab'; your system might not support 'cron'"));
2267
0
    goto out;
2268
0
  }
2269
2270
0
  if (finish_command(&crontab_edit))
2271
0
    result = error(_("'crontab' died"));
2272
0
  else
2273
0
    fclose(cron_list);
2274
0
out:
2275
0
  delete_tempfile(&tmpedit);
2276
0
  return result;
2277
0
}
2278
2279
static int real_is_systemd_timer_available(void)
2280
0
{
2281
0
  struct child_process child = CHILD_PROCESS_INIT;
2282
2283
0
  strvec_pushl(&child.args, "systemctl", "--user", "list-timers", NULL);
2284
0
  child.no_stdin = 1;
2285
0
  child.no_stdout = 1;
2286
0
  child.no_stderr = 1;
2287
0
  child.silent_exec_failure = 1;
2288
2289
0
  if (start_command(&child))
2290
0
    return 0;
2291
0
  if (finish_command(&child))
2292
0
    return 0;
2293
0
  return 1;
2294
0
}
2295
2296
static int is_systemd_timer_available(void)
2297
0
{
2298
0
  const char *cmd = "systemctl";
2299
0
  int is_available;
2300
2301
0
  if (get_schedule_cmd(&cmd, &is_available))
2302
0
    return is_available;
2303
2304
0
  return real_is_systemd_timer_available();
2305
0
}
2306
2307
static char *xdg_config_home_systemd(const char *filename)
2308
0
{
2309
0
  return xdg_config_home_for("systemd/user", filename);
2310
0
}
2311
2312
0
#define SYSTEMD_UNIT_FORMAT "git-maintenance@%s.%s"
2313
2314
static int systemd_timer_delete_timer_file(enum schedule_priority priority)
2315
0
{
2316
0
  int ret = 0;
2317
0
  const char *frequency = get_frequency(priority);
2318
0
  char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2319
0
  char *filename = xdg_config_home_systemd(local_timer_name);
2320
2321
0
  if (unlink(filename) && !is_missing_file_error(errno))
2322
0
    ret = error_errno(_("failed to delete '%s'"), filename);
2323
2324
0
  free(filename);
2325
0
  free(local_timer_name);
2326
0
  return ret;
2327
0
}
2328
2329
static int systemd_timer_delete_service_template(void)
2330
0
{
2331
0
  int ret = 0;
2332
0
  char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2333
0
  char *filename = xdg_config_home_systemd(local_service_name);
2334
0
  if (unlink(filename) && !is_missing_file_error(errno))
2335
0
    ret = error_errno(_("failed to delete '%s'"), filename);
2336
2337
0
  free(filename);
2338
0
  free(local_service_name);
2339
0
  return ret;
2340
0
}
2341
2342
/*
2343
 * Write the schedule information into a git-maintenance@<schedule>.timer
2344
 * file using a custom minute. This timer file cannot use the templating
2345
 * system, so we generate a specific file for each.
2346
 */
2347
static int systemd_timer_write_timer_file(enum schedule_priority schedule,
2348
            int minute)
2349
0
{
2350
0
  int res = -1;
2351
0
  char *filename;
2352
0
  FILE *file;
2353
0
  const char *unit;
2354
0
  char *schedule_pattern = NULL;
2355
0
  const char *frequency = get_frequency(schedule);
2356
0
  char *local_timer_name = xstrfmt(SYSTEMD_UNIT_FORMAT, frequency, "timer");
2357
2358
0
  filename = xdg_config_home_systemd(local_timer_name);
2359
2360
0
  if (safe_create_leading_directories(filename)) {
2361
0
    error(_("failed to create directories for '%s'"), filename);
2362
0
    goto error;
2363
0
  }
2364
0
  file = fopen_or_warn(filename, "w");
2365
0
  if (!file)
2366
0
    goto error;
2367
2368
0
  switch (schedule) {
2369
0
  case SCHEDULE_HOURLY:
2370
0
    schedule_pattern = xstrfmt("*-*-* 1..23:%02d:00", minute);
2371
0
    break;
2372
2373
0
  case SCHEDULE_DAILY:
2374
0
    schedule_pattern = xstrfmt("Tue..Sun *-*-* 0:%02d:00", minute);
2375
0
    break;
2376
2377
0
  case SCHEDULE_WEEKLY:
2378
0
    schedule_pattern = xstrfmt("Mon 0:%02d:00", minute);
2379
0
    break;
2380
2381
0
  default:
2382
0
    BUG("Unhandled schedule_priority");
2383
0
  }
2384
2385
0
  unit = "# This file was created and is maintained by Git.\n"
2386
0
         "# Any edits made in this file might be replaced in the future\n"
2387
0
         "# by a Git command.\n"
2388
0
         "\n"
2389
0
         "[Unit]\n"
2390
0
         "Description=Optimize Git repositories data\n"
2391
0
         "\n"
2392
0
         "[Timer]\n"
2393
0
         "OnCalendar=%s\n"
2394
0
         "Persistent=true\n"
2395
0
         "\n"
2396
0
         "[Install]\n"
2397
0
         "WantedBy=timers.target\n";
2398
0
  if (fprintf(file, unit, schedule_pattern) < 0) {
2399
0
    error(_("failed to write to '%s'"), filename);
2400
0
    fclose(file);
2401
0
    goto error;
2402
0
  }
2403
0
  if (fclose(file) == EOF) {
2404
0
    error_errno(_("failed to flush '%s'"), filename);
2405
0
    goto error;
2406
0
  }
2407
2408
0
  res = 0;
2409
2410
0
error:
2411
0
  free(schedule_pattern);
2412
0
  free(local_timer_name);
2413
0
  free(filename);
2414
0
  return res;
2415
0
}
2416
2417
/*
2418
 * No matter the schedule, we use the same service and can make use of the
2419
 * templating system. When installing git-maintenance@<schedule>.timer,
2420
 * systemd will notice that git-maintenance@.service exists as a template
2421
 * and will use this file and insert the <schedule> into the template at
2422
 * the position of "%i".
2423
 */
2424
static int systemd_timer_write_service_template(const char *exec_path)
2425
0
{
2426
0
  int res = -1;
2427
0
  char *filename;
2428
0
  FILE *file;
2429
0
  const char *unit;
2430
0
  char *local_service_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "service");
2431
2432
0
  filename = xdg_config_home_systemd(local_service_name);
2433
0
  if (safe_create_leading_directories(filename)) {
2434
0
    error(_("failed to create directories for '%s'"), filename);
2435
0
    goto error;
2436
0
  }
2437
0
  file = fopen_or_warn(filename, "w");
2438
0
  if (!file)
2439
0
    goto error;
2440
2441
0
  unit = "# This file was created and is maintained by Git.\n"
2442
0
         "# Any edits made in this file might be replaced in the future\n"
2443
0
         "# by a Git command.\n"
2444
0
         "\n"
2445
0
         "[Unit]\n"
2446
0
         "Description=Optimize Git repositories data\n"
2447
0
         "\n"
2448
0
         "[Service]\n"
2449
0
         "Type=oneshot\n"
2450
0
         "ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2451
0
         "LockPersonality=yes\n"
2452
0
         "MemoryDenyWriteExecute=yes\n"
2453
0
         "NoNewPrivileges=yes\n"
2454
0
         "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_VSOCK\n"
2455
0
         "RestrictNamespaces=yes\n"
2456
0
         "RestrictRealtime=yes\n"
2457
0
         "RestrictSUIDSGID=yes\n"
2458
0
         "SystemCallArchitectures=native\n"
2459
0
         "SystemCallFilter=@system-service\n";
2460
0
  if (fprintf(file, unit, exec_path, exec_path) < 0) {
2461
0
    error(_("failed to write to '%s'"), filename);
2462
0
    fclose(file);
2463
0
    goto error;
2464
0
  }
2465
0
  if (fclose(file) == EOF) {
2466
0
    error_errno(_("failed to flush '%s'"), filename);
2467
0
    goto error;
2468
0
  }
2469
2470
0
  res = 0;
2471
2472
0
error:
2473
0
  free(local_service_name);
2474
0
  free(filename);
2475
0
  return res;
2476
0
}
2477
2478
static int systemd_timer_enable_unit(int enable,
2479
             enum schedule_priority schedule,
2480
             int minute)
2481
0
{
2482
0
  const char *cmd = "systemctl";
2483
0
  struct child_process child = CHILD_PROCESS_INIT;
2484
0
  const char *frequency = get_frequency(schedule);
2485
2486
  /*
2487
   * Disabling the systemd unit while it is already disabled makes
2488
   * systemctl print an error.
2489
   * Let's ignore it since it means we already are in the expected state:
2490
   * the unit is disabled.
2491
   *
2492
   * On the other hand, enabling a systemd unit which is already enabled
2493
   * produces no error.
2494
   */
2495
0
  if (!enable)
2496
0
    child.no_stderr = 1;
2497
0
  else if (systemd_timer_write_timer_file(schedule, minute))
2498
0
    return -1;
2499
2500
0
  get_schedule_cmd(&cmd, NULL);
2501
0
  strvec_split(&child.args, cmd);
2502
0
  strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2503
0
         "--now", NULL);
2504
0
  strvec_pushf(&child.args, SYSTEMD_UNIT_FORMAT, frequency, "timer");
2505
2506
0
  if (start_command(&child))
2507
0
    return error(_("failed to start systemctl"));
2508
0
  if (finish_command(&child))
2509
    /*
2510
     * Disabling an already disabled systemd unit makes
2511
     * systemctl fail.
2512
     * Let's ignore this failure.
2513
     *
2514
     * Enabling an enabled systemd unit doesn't fail.
2515
     */
2516
0
    if (enable)
2517
0
      return error(_("failed to run systemctl"));
2518
0
  return 0;
2519
0
}
2520
2521
/*
2522
 * A previous version of Git wrote the timer units as template files.
2523
 * Clean these up, if they exist.
2524
 */
2525
static void systemd_timer_delete_stale_timer_templates(void)
2526
0
{
2527
0
  char *timer_template_name = xstrfmt(SYSTEMD_UNIT_FORMAT, "", "timer");
2528
0
  char *filename = xdg_config_home_systemd(timer_template_name);
2529
2530
0
  if (unlink(filename) && !is_missing_file_error(errno))
2531
0
    warning(_("failed to delete '%s'"), filename);
2532
2533
0
  free(filename);
2534
0
  free(timer_template_name);
2535
0
}
2536
2537
static int systemd_timer_delete_unit_files(void)
2538
0
{
2539
0
  systemd_timer_delete_stale_timer_templates();
2540
2541
  /* Purposefully not short-circuited to make sure all are called. */
2542
0
  return systemd_timer_delete_timer_file(SCHEDULE_HOURLY) |
2543
0
         systemd_timer_delete_timer_file(SCHEDULE_DAILY) |
2544
0
         systemd_timer_delete_timer_file(SCHEDULE_WEEKLY) |
2545
0
         systemd_timer_delete_service_template();
2546
0
}
2547
2548
static int systemd_timer_delete_units(void)
2549
0
{
2550
0
  int minute = get_random_minute();
2551
  /* Purposefully not short-circuited to make sure all are called. */
2552
0
  return systemd_timer_enable_unit(0, SCHEDULE_HOURLY, minute) |
2553
0
         systemd_timer_enable_unit(0, SCHEDULE_DAILY, minute) |
2554
0
         systemd_timer_enable_unit(0, SCHEDULE_WEEKLY, minute) |
2555
0
         systemd_timer_delete_unit_files();
2556
0
}
2557
2558
static int systemd_timer_setup_units(void)
2559
0
{
2560
0
  int minute = get_random_minute();
2561
0
  const char *exec_path = git_exec_path();
2562
2563
0
  int ret = systemd_timer_write_service_template(exec_path) ||
2564
0
      systemd_timer_enable_unit(1, SCHEDULE_HOURLY, minute) ||
2565
0
      systemd_timer_enable_unit(1, SCHEDULE_DAILY, minute) ||
2566
0
      systemd_timer_enable_unit(1, SCHEDULE_WEEKLY, minute);
2567
2568
0
  if (ret)
2569
0
    systemd_timer_delete_units();
2570
0
  else
2571
0
    systemd_timer_delete_stale_timer_templates();
2572
2573
0
  return ret;
2574
0
}
2575
2576
static int systemd_timer_update_schedule(int run_maintenance, int fd UNUSED)
2577
0
{
2578
0
  if (run_maintenance)
2579
0
    return systemd_timer_setup_units();
2580
0
  else
2581
0
    return systemd_timer_delete_units();
2582
0
}
2583
2584
enum scheduler {
2585
  SCHEDULER_INVALID = -1,
2586
  SCHEDULER_AUTO,
2587
  SCHEDULER_CRON,
2588
  SCHEDULER_SYSTEMD,
2589
  SCHEDULER_LAUNCHCTL,
2590
  SCHEDULER_SCHTASKS,
2591
};
2592
2593
static const struct {
2594
  const char *name;
2595
  int (*is_available)(void);
2596
  int (*update_schedule)(int run_maintenance, int fd);
2597
} scheduler_fn[] = {
2598
  [SCHEDULER_CRON] = {
2599
    .name = "crontab",
2600
    .is_available = is_crontab_available,
2601
    .update_schedule = crontab_update_schedule,
2602
  },
2603
  [SCHEDULER_SYSTEMD] = {
2604
    .name = "systemctl",
2605
    .is_available = is_systemd_timer_available,
2606
    .update_schedule = systemd_timer_update_schedule,
2607
  },
2608
  [SCHEDULER_LAUNCHCTL] = {
2609
    .name = "launchctl",
2610
    .is_available = is_launchctl_available,
2611
    .update_schedule = launchctl_update_schedule,
2612
  },
2613
  [SCHEDULER_SCHTASKS] = {
2614
    .name = "schtasks",
2615
    .is_available = is_schtasks_available,
2616
    .update_schedule = schtasks_update_schedule,
2617
  },
2618
};
2619
2620
static enum scheduler parse_scheduler(const char *value)
2621
0
{
2622
0
  if (!value)
2623
0
    return SCHEDULER_INVALID;
2624
0
  else if (!strcasecmp(value, "auto"))
2625
0
    return SCHEDULER_AUTO;
2626
0
  else if (!strcasecmp(value, "cron") || !strcasecmp(value, "crontab"))
2627
0
    return SCHEDULER_CRON;
2628
0
  else if (!strcasecmp(value, "systemd") ||
2629
0
     !strcasecmp(value, "systemd-timer"))
2630
0
    return SCHEDULER_SYSTEMD;
2631
0
  else if (!strcasecmp(value, "launchctl"))
2632
0
    return SCHEDULER_LAUNCHCTL;
2633
0
  else if (!strcasecmp(value, "schtasks"))
2634
0
    return SCHEDULER_SCHTASKS;
2635
0
  else
2636
0
    return SCHEDULER_INVALID;
2637
0
}
2638
2639
static int maintenance_opt_scheduler(const struct option *opt, const char *arg,
2640
             int unset)
2641
0
{
2642
0
  enum scheduler *scheduler = opt->value;
2643
2644
0
  BUG_ON_OPT_NEG(unset);
2645
2646
0
  *scheduler = parse_scheduler(arg);
2647
0
  if (*scheduler == SCHEDULER_INVALID)
2648
0
    return error(_("unrecognized --scheduler argument '%s'"), arg);
2649
0
  return 0;
2650
0
}
2651
2652
struct maintenance_start_opts {
2653
  enum scheduler scheduler;
2654
};
2655
2656
static enum scheduler resolve_scheduler(enum scheduler scheduler)
2657
0
{
2658
0
  if (scheduler != SCHEDULER_AUTO)
2659
0
    return scheduler;
2660
2661
#if defined(__APPLE__)
2662
  return SCHEDULER_LAUNCHCTL;
2663
2664
#elif defined(GIT_WINDOWS_NATIVE)
2665
  return SCHEDULER_SCHTASKS;
2666
2667
#elif defined(__linux__)
2668
0
  if (is_systemd_timer_available())
2669
0
    return SCHEDULER_SYSTEMD;
2670
0
  else if (is_crontab_available())
2671
0
    return SCHEDULER_CRON;
2672
0
  else
2673
0
    die(_("neither systemd timers nor crontab are available"));
2674
2675
#else
2676
  return SCHEDULER_CRON;
2677
#endif
2678
0
}
2679
2680
static void validate_scheduler(enum scheduler scheduler)
2681
0
{
2682
0
  if (scheduler == SCHEDULER_INVALID)
2683
0
    BUG("invalid scheduler");
2684
0
  if (scheduler == SCHEDULER_AUTO)
2685
0
    BUG("resolve_scheduler should have been called before");
2686
2687
0
  if (!scheduler_fn[scheduler].is_available())
2688
0
    die(_("%s scheduler is not available"),
2689
0
        scheduler_fn[scheduler].name);
2690
0
}
2691
2692
static int update_background_schedule(const struct maintenance_start_opts *opts,
2693
              int enable)
2694
0
{
2695
0
  unsigned int i;
2696
0
  int result = 0;
2697
0
  struct lock_file lk;
2698
0
  char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path);
2699
2700
0
  if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) {
2701
0
    free(lock_path);
2702
0
    return error(_("another process is scheduling background maintenance"));
2703
0
  }
2704
2705
0
  for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) {
2706
0
    if (enable && opts->scheduler == i)
2707
0
      continue;
2708
0
    if (!scheduler_fn[i].is_available())
2709
0
      continue;
2710
0
    scheduler_fn[i].update_schedule(0, get_lock_file_fd(&lk));
2711
0
  }
2712
2713
0
  if (enable)
2714
0
    result = scheduler_fn[opts->scheduler].update_schedule(
2715
0
      1, get_lock_file_fd(&lk));
2716
2717
0
  rollback_lock_file(&lk);
2718
2719
0
  free(lock_path);
2720
0
  return result;
2721
0
}
2722
2723
static const char *const builtin_maintenance_start_usage[] = {
2724
  N_("git maintenance start [--scheduler=<scheduler>]"),
2725
  NULL
2726
};
2727
2728
static int maintenance_start(int argc, const char **argv, const char *prefix)
2729
0
{
2730
0
  struct maintenance_start_opts opts = { 0 };
2731
0
  struct option options[] = {
2732
0
    OPT_CALLBACK_F(
2733
0
      0, "scheduler", &opts.scheduler, N_("scheduler"),
2734
0
      N_("scheduler to trigger git maintenance run"),
2735
0
      PARSE_OPT_NONEG, maintenance_opt_scheduler),
2736
0
    OPT_END()
2737
0
  };
2738
0
  const char *register_args[] = { "register", NULL };
2739
2740
0
  argc = parse_options(argc, argv, prefix, options,
2741
0
           builtin_maintenance_start_usage, 0);
2742
0
  if (argc)
2743
0
    usage_with_options(builtin_maintenance_start_usage, options);
2744
2745
0
  opts.scheduler = resolve_scheduler(opts.scheduler);
2746
0
  validate_scheduler(opts.scheduler);
2747
2748
0
  if (update_background_schedule(&opts, 1))
2749
0
    die(_("failed to set up maintenance schedule"));
2750
2751
0
  if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
2752
0
    warning(_("failed to add repo to global config"));
2753
0
  return 0;
2754
0
}
2755
2756
static const char *const builtin_maintenance_stop_usage[] = {
2757
  "git maintenance stop",
2758
  NULL
2759
};
2760
2761
static int maintenance_stop(int argc, const char **argv, const char *prefix)
2762
0
{
2763
0
  struct option options[] = {
2764
0
    OPT_END()
2765
0
  };
2766
0
  argc = parse_options(argc, argv, prefix, options,
2767
0
           builtin_maintenance_stop_usage, 0);
2768
0
  if (argc)
2769
0
    usage_with_options(builtin_maintenance_stop_usage, options);
2770
0
  return update_background_schedule(NULL, 0);
2771
0
}
2772
2773
static const char * const builtin_maintenance_usage[] = {
2774
  N_("git maintenance <subcommand> [<options>]"),
2775
  NULL,
2776
};
2777
2778
int cmd_maintenance(int argc, const char **argv, const char *prefix)
2779
0
{
2780
0
  parse_opt_subcommand_fn *fn = NULL;
2781
0
  struct option builtin_maintenance_options[] = {
2782
0
    OPT_SUBCOMMAND("run", &fn, maintenance_run),
2783
0
    OPT_SUBCOMMAND("start", &fn, maintenance_start),
2784
0
    OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
2785
0
    OPT_SUBCOMMAND("register", &fn, maintenance_register),
2786
0
    OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
2787
0
    OPT_END(),
2788
0
  };
2789
2790
0
  argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
2791
0
           builtin_maintenance_usage, 0);
2792
0
  return fn(argc, argv, prefix);
2793
0
}