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