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" |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 14 | #include "cache.h" |
James Bowes | 44c637c | 2007-11-01 21:02:27 -0400 | [diff] [blame] | 15 | #include "parse-options.h" |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 16 | #include "run-command.h" |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 17 | #include "argv-array.h" |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 18 | |
| 19 | #define FAILED_RUN "failed to run %s" |
| 20 | |
James Bowes | 44c637c | 2007-11-01 21:02:27 -0400 | [diff] [blame] | 21 | static const char * const builtin_gc_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 22 | "git gc [options]", |
James Bowes | 44c637c | 2007-11-01 21:02:27 -0400 | [diff] [blame] | 23 | NULL |
| 24 | }; |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 25 | |
Linus Torvalds | 5675239 | 2007-05-24 11:41:39 -0700 | [diff] [blame] | 26 | static int pack_refs = 1; |
Johannes Schindelin | 1c192f3 | 2007-12-06 12:03:38 +0000 | [diff] [blame] | 27 | static int aggressive_window = 250; |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 28 | static int gc_auto_threshold = 6700; |
Junio C Hamano | 9706397 | 2008-03-23 00:04:48 -0700 | [diff] [blame] | 29 | static int gc_auto_pack_limit = 50; |
David Bryson | d3154b4 | 2008-09-30 13:28:58 -0700 | [diff] [blame] | 30 | static const char *prune_expire = "2.weeks.ago"; |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 31 | |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 32 | static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT; |
| 33 | static struct argv_array reflog = ARGV_ARRAY_INIT; |
| 34 | static struct argv_array repack = ARGV_ARRAY_INIT; |
| 35 | static struct argv_array prune = ARGV_ARRAY_INIT; |
| 36 | static struct argv_array rerere = ARGV_ARRAY_INIT; |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 37 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 38 | static int gc_config(const char *var, const char *value, void *cb) |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 39 | { |
| 40 | if (!strcmp(var, "gc.packrefs")) { |
Miklos Vajna | c5e5a2c | 2008-02-08 15:26:18 +0100 | [diff] [blame] | 41 | if (value && !strcmp(value, "notbare")) |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 42 | pack_refs = -1; |
| 43 | else |
| 44 | pack_refs = git_config_bool(var, value); |
| 45 | return 0; |
| 46 | } |
Theodore Tso | 0d7566a | 2007-05-09 15:48:39 -0400 | [diff] [blame] | 47 | if (!strcmp(var, "gc.aggressivewindow")) { |
| 48 | aggressive_window = git_config_int(var, value); |
| 49 | return 0; |
| 50 | } |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 51 | if (!strcmp(var, "gc.auto")) { |
| 52 | gc_auto_threshold = git_config_int(var, value); |
| 53 | return 0; |
| 54 | } |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 55 | if (!strcmp(var, "gc.autopacklimit")) { |
| 56 | gc_auto_pack_limit = git_config_int(var, value); |
| 57 | return 0; |
| 58 | } |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 59 | if (!strcmp(var, "gc.pruneexpire")) { |
David Bryson | d3154b4 | 2008-09-30 13:28:58 -0700 | [diff] [blame] | 60 | if (value && strcmp(value, "now")) { |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 61 | unsigned long now = approxidate("now"); |
| 62 | if (approxidate(value) >= now) |
Ævar Arnfjörð Bjarmason | fea6128 | 2011-02-22 23:42:24 +0000 | [diff] [blame] | 63 | return error(_("Invalid %s: '%s'"), var, value); |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 64 | } |
David Bryson | d3154b4 | 2008-09-30 13:28:58 -0700 | [diff] [blame] | 65 | return git_config_string(&prune_expire, var, value); |
Johannes Schindelin | 25ee973 | 2008-03-12 21:55:47 +0100 | [diff] [blame] | 66 | } |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 67 | return git_default_config(var, value, cb); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Junio C Hamano | a087cc9 | 2007-09-17 00:44:17 -0700 | [diff] [blame] | 70 | static int too_many_loose_objects(void) |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 71 | { |
| 72 | /* |
| 73 | * Quickly check if a "gc" is needed, by estimating how |
| 74 | * many loose objects there are. Because SHA-1 is evenly |
| 75 | * distributed, we can check only one and get a reasonable |
| 76 | * estimate. |
| 77 | */ |
| 78 | char path[PATH_MAX]; |
| 79 | const char *objdir = get_object_directory(); |
| 80 | DIR *dir; |
| 81 | struct dirent *ent; |
| 82 | int auto_threshold; |
| 83 | int num_loose = 0; |
| 84 | int needed = 0; |
| 85 | |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 86 | if (gc_auto_threshold <= 0) |
| 87 | return 0; |
| 88 | |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 89 | if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) { |
Ævar Arnfjörð Bjarmason | fea6128 | 2011-02-22 23:42:24 +0000 | [diff] [blame] | 90 | warning(_("insanely long object directory %.*s"), 50, objdir); |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | dir = opendir(path); |
| 94 | if (!dir) |
| 95 | return 0; |
| 96 | |
| 97 | auto_threshold = (gc_auto_threshold + 255) / 256; |
| 98 | while ((ent = readdir(dir)) != NULL) { |
| 99 | if (strspn(ent->d_name, "0123456789abcdef") != 38 || |
| 100 | ent->d_name[38] != '\0') |
| 101 | continue; |
| 102 | if (++num_loose > auto_threshold) { |
| 103 | needed = 1; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | closedir(dir); |
| 108 | return needed; |
| 109 | } |
| 110 | |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 111 | static int too_many_packs(void) |
| 112 | { |
| 113 | struct packed_git *p; |
| 114 | int cnt; |
| 115 | |
| 116 | if (gc_auto_pack_limit <= 0) |
| 117 | return 0; |
| 118 | |
| 119 | prepare_packed_git(); |
| 120 | for (cnt = 0, p = packed_git; p; p = p->next) { |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 121 | if (!p->pack_local) |
| 122 | continue; |
Brandon Casey | 01af249 | 2008-11-12 11:59:07 -0600 | [diff] [blame] | 123 | if (p->pack_keep) |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 124 | continue; |
| 125 | /* |
| 126 | * Perhaps check the size of the pack and count only |
| 127 | * very small ones here? |
| 128 | */ |
| 129 | cnt++; |
| 130 | } |
| 131 | return gc_auto_pack_limit <= cnt; |
| 132 | } |
| 133 | |
Jeff King | 7e52f56 | 2012-04-07 06:30:09 -0400 | [diff] [blame] | 134 | static void add_repack_all_option(void) |
| 135 | { |
| 136 | if (prune_expire && !strcmp(prune_expire, "now")) |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 137 | argv_array_push(&repack, "-a"); |
Jeff King | 7e52f56 | 2012-04-07 06:30:09 -0400 | [diff] [blame] | 138 | else { |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 139 | argv_array_push(&repack, "-A"); |
| 140 | if (prune_expire) |
| 141 | argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire); |
Jeff King | 7e52f56 | 2012-04-07 06:30:09 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Junio C Hamano | a087cc9 | 2007-09-17 00:44:17 -0700 | [diff] [blame] | 145 | static int need_to_gc(void) |
| 146 | { |
| 147 | /* |
Brandon Casey | b14d255 | 2008-03-19 16:53:20 -0500 | [diff] [blame] | 148 | * Setting gc.auto to 0 or negative can disable the |
| 149 | * automatic gc. |
Junio C Hamano | a087cc9 | 2007-09-17 00:44:17 -0700 | [diff] [blame] | 150 | */ |
Brandon Casey | b14d255 | 2008-03-19 16:53:20 -0500 | [diff] [blame] | 151 | if (gc_auto_threshold <= 0) |
Junio C Hamano | a087cc9 | 2007-09-17 00:44:17 -0700 | [diff] [blame] | 152 | return 0; |
| 153 | |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 154 | /* |
| 155 | * If there are too many loose objects, but not too many |
| 156 | * packs, we run "repack -d -l". If there are too many packs, |
| 157 | * we run "repack -A -d -l". Otherwise we tell the caller |
| 158 | * there is no need. |
| 159 | */ |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 160 | if (too_many_packs()) |
Jeff King | 7e52f56 | 2012-04-07 06:30:09 -0400 | [diff] [blame] | 161 | add_repack_all_option(); |
Junio C Hamano | 1781550 | 2007-09-17 00:55:13 -0700 | [diff] [blame] | 162 | else if (!too_many_loose_objects()) |
| 163 | return 0; |
Miklos Vajna | bde3054 | 2008-04-02 21:34:38 +0200 | [diff] [blame] | 164 | |
Stephan Beyer | ae98a00 | 2009-01-16 20:09:59 +0100 | [diff] [blame] | 165 | if (run_hook(NULL, "pre-auto-gc", NULL)) |
Miklos Vajna | bde3054 | 2008-04-02 21:34:38 +0200 | [diff] [blame] | 166 | return 0; |
Junio C Hamano | 95143f9 | 2007-09-17 00:48:39 -0700 | [diff] [blame] | 167 | return 1; |
Junio C Hamano | a087cc9 | 2007-09-17 00:44:17 -0700 | [diff] [blame] | 168 | } |
| 169 | |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 170 | int cmd_gc(int argc, const char **argv, const char *prefix) |
| 171 | { |
James Bowes | 44c637c | 2007-11-01 21:02:27 -0400 | [diff] [blame] | 172 | int aggressive = 0; |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 173 | int auto_gc = 0; |
Frank Lichtenheld | a0c14cb | 2008-02-29 22:53:39 +0100 | [diff] [blame] | 174 | int quiet = 0; |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 175 | |
James Bowes | 44c637c | 2007-11-01 21:02:27 -0400 | [diff] [blame] | 176 | struct option builtin_gc_options[] = { |
Jonathan Nieder | 8c83968 | 2010-11-08 13:54:48 -0600 | [diff] [blame] | 177 | OPT__QUIET(&quiet, "suppress progress reporting"), |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 178 | { OPTION_STRING, 0, "prune", &prune_expire, "date", |
| 179 | "prune unreferenced objects", |
| 180 | PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire }, |
James Bowes | 44c637c | 2007-11-01 21:02:27 -0400 | [diff] [blame] | 181 | OPT_BOOLEAN(0, "aggressive", &aggressive, "be more thorough (increased runtime)"), |
| 182 | OPT_BOOLEAN(0, "auto", &auto_gc, "enable auto-gc mode"), |
| 183 | OPT_END() |
| 184 | }; |
| 185 | |
Nguyễn Thái Ngọc Duy | 0c8151b | 2010-10-22 01:47:19 -0500 | [diff] [blame] | 186 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 187 | usage_with_options(builtin_gc_usage, builtin_gc_options); |
| 188 | |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 189 | argv_array_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL); |
| 190 | argv_array_pushl(&reflog, "reflog", "expire", "--all", NULL); |
| 191 | argv_array_pushl(&repack, "repack", "-d", "-l", NULL); |
| 192 | argv_array_pushl(&prune, "prune", "--expire", NULL ); |
| 193 | argv_array_pushl(&rerere, "rerere", "gc", NULL); |
| 194 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 195 | git_config(gc_config, NULL); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 196 | |
| 197 | if (pack_refs < 0) |
| 198 | pack_refs = !is_bare_repository(); |
| 199 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 200 | argc = parse_options(argc, argv, prefix, builtin_gc_options, |
| 201 | builtin_gc_usage, 0); |
James Bowes | 44c637c | 2007-11-01 21:02:27 -0400 | [diff] [blame] | 202 | if (argc > 0) |
| 203 | usage_with_options(builtin_gc_usage, builtin_gc_options); |
| 204 | |
| 205 | if (aggressive) { |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 206 | argv_array_push(&repack, "-f"); |
| 207 | argv_array_push(&repack, "--depth=250"); |
| 208 | if (aggressive_window > 0) |
| 209 | argv_array_pushf(&repack, "--window=%d", aggressive_window); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 210 | } |
Frank Lichtenheld | a0c14cb | 2008-02-29 22:53:39 +0100 | [diff] [blame] | 211 | if (quiet) |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 212 | argv_array_push(&repack, "-q"); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 213 | |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 214 | if (auto_gc) { |
| 215 | /* |
| 216 | * Auto-gc should be least intrusive as possible. |
| 217 | */ |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 218 | if (!need_to_gc()) |
| 219 | return 0; |
Ævar Arnfjörð Bjarmason | f6908ae | 2011-02-22 23:42:25 +0000 | [diff] [blame] | 220 | if (quiet) |
| 221 | fprintf(stderr, _("Auto packing the repository for optimum performance.\n")); |
| 222 | else |
| 223 | fprintf(stderr, |
| 224 | _("Auto packing the repository for optimum performance. You may also\n" |
| 225 | "run \"git gc\" manually. See " |
Andreas Schwab | daab4ee | 2011-06-19 10:03:26 +0200 | [diff] [blame] | 226 | "\"git help gc\" for more information.\n")); |
Brandon Casey | a37cce3 | 2008-05-09 23:01:56 -0500 | [diff] [blame] | 227 | } else |
Jeff King | 7e52f56 | 2012-04-07 06:30:09 -0400 | [diff] [blame] | 228 | add_repack_all_option(); |
Junio C Hamano | 2c3c439 | 2007-09-05 13:01:37 -0700 | [diff] [blame] | 229 | |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 230 | if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD)) |
| 231 | return error(FAILED_RUN, pack_refs_cmd.argv[0]); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 232 | |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 233 | if (run_command_v_opt(reflog.argv, RUN_GIT_CMD)) |
| 234 | return error(FAILED_RUN, reflog.argv[0]); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 235 | |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 236 | if (run_command_v_opt(repack.argv, RUN_GIT_CMD)) |
| 237 | return error(FAILED_RUN, repack.argv[0]); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 238 | |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 239 | if (prune_expire) { |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 240 | argv_array_push(&prune, prune_expire); |
Jeff King | bf0a59b | 2011-11-08 00:34:08 -0500 | [diff] [blame] | 241 | if (quiet) |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 242 | argv_array_push(&prune, "--no-progress"); |
| 243 | if (run_command_v_opt(prune.argv, RUN_GIT_CMD)) |
| 244 | return error(FAILED_RUN, prune.argv[0]); |
Johannes Schindelin | 58e9d9d | 2009-02-14 23:10:10 +0100 | [diff] [blame] | 245 | } |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 246 | |
Jeff King | 234587f | 2012-04-18 14:10:19 -0700 | [diff] [blame] | 247 | if (run_command_v_opt(rerere.argv, RUN_GIT_CMD)) |
| 248 | return error(FAILED_RUN, rerere.argv[0]); |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 249 | |
Junio C Hamano | a087cc9 | 2007-09-17 00:44:17 -0700 | [diff] [blame] | 250 | if (auto_gc && too_many_loose_objects()) |
Ævar Arnfjörð Bjarmason | fea6128 | 2011-02-22 23:42:24 +0000 | [diff] [blame] | 251 | warning(_("There are too many unreachable loose objects; " |
| 252 | "run 'git prune' to remove them.")); |
Junio C Hamano | a087cc9 | 2007-09-17 00:44:17 -0700 | [diff] [blame] | 253 | |
James Bowes | 6757ada | 2007-03-13 21:58:22 -0400 | [diff] [blame] | 254 | return 0; |
| 255 | } |