2 * git gc builtin command
4 * Cleanup unreachable files and optimize the repository.
6 * Copyright (c) 2007 James Bowes
8 * Based on git-gc.sh, which is
10 * Copyright (c) 2006 Shawn O. Pearce
14 #include "run-command.h"
16 #define FAILED_RUN "failed to run %s"
18 static const char builtin_gc_usage[] = "git-gc [--prune] [--aggressive]";
20 static int pack_refs = 1;
21 static int aggressive_window = -1;
24 static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
25 static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
26 static const char *argv_repack[MAX_ADD] = {"repack", "-a", "-d", "-l", NULL};
27 static const char *argv_prune[] = {"prune", NULL};
28 static const char *argv_rerere[] = {"rerere", "gc", NULL};
30 static int gc_config(const char *var, const char *value)
32 if (!strcmp(var, "gc.packrefs")) {
33 if (!strcmp(value, "notbare"))
36 pack_refs = git_config_bool(var, value);
39 if (!strcmp(var, "gc.aggressivewindow")) {
40 aggressive_window = git_config_int(var, value);
43 return git_default_config(var, value);
46 static void append_option(const char **cmd, const char *opt, int max_length)
50 for (i = 0; cmd[i]; i++)
53 if (i + 2 >= max_length)
54 die("Too many options specified");
59 int cmd_gc(int argc, const char **argv, const char *prefix)
65 git_config(gc_config);
68 pack_refs = !is_bare_repository();
70 for (i = 1; i < argc; i++) {
71 const char *arg = argv[i];
72 if (!strcmp(arg, "--prune")) {
76 if (!strcmp(arg, "--aggressive")) {
77 append_option(argv_repack, "-f", MAX_ADD);
78 if (aggressive_window > 0) {
79 sprintf(buf, "--window=%d", aggressive_window);
80 append_option(argv_repack, buf, MAX_ADD);
84 /* perhaps other parameters later... */
88 usage(builtin_gc_usage);
90 if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
91 return error(FAILED_RUN, argv_pack_refs[0]);
93 if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
94 return error(FAILED_RUN, argv_reflog[0]);
96 if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
97 return error(FAILED_RUN, argv_repack[0]);
99 if (prune && run_command_v_opt(argv_prune, RUN_GIT_CMD))
100 return error(FAILED_RUN, argv_prune[0]);
102 if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
103 return error(FAILED_RUN, argv_rerere[0]);