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