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
15 #include "run-command.h"
17 #define FAILED_RUN "failed to run %s"
19 static const char builtin_gc_usage
[] = "git-gc [--prune] [--aggressive]";
21 static int pack_refs
= 1;
22 static int aggressive_window
= -1;
25 static const char *argv_pack_refs
[] = {"pack-refs", "--all", "--prune", NULL
};
26 static const char *argv_reflog
[] = {"reflog", "expire", "--all", NULL
};
27 static const char *argv_repack
[MAX_ADD
] = {"repack", "-a", "-d", "-l", NULL
};
28 static const char *argv_prune
[] = {"prune", NULL
};
29 static const char *argv_rerere
[] = {"rerere", "gc", NULL
};
31 static int gc_config(const char *var
, const char *value
)
33 if (!strcmp(var
, "gc.packrefs")) {
34 if (!strcmp(value
, "notbare"))
37 pack_refs
= git_config_bool(var
, value
);
40 if (!strcmp(var
, "gc.aggressivewindow")) {
41 aggressive_window
= git_config_int(var
, value
);
44 return git_default_config(var
, value
);
47 static void append_option(const char **cmd
, const char *opt
, int max_length
)
51 for (i
= 0; cmd
[i
]; i
++)
54 if (i
+ 2 >= max_length
)
55 die("Too many options specified");
60 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
66 git_config(gc_config
);
69 pack_refs
= !is_bare_repository();
71 for (i
= 1; i
< argc
; i
++) {
72 const char *arg
= argv
[i
];
73 if (!strcmp(arg
, "--prune")) {
77 if (!strcmp(arg
, "--aggressive")) {
78 append_option(argv_repack
, "-f", MAX_ADD
);
79 if (aggressive_window
> 0) {
80 sprintf(buf
, "--window=%d", aggressive_window
);
81 append_option(argv_repack
, buf
, MAX_ADD
);
85 /* perhaps other parameters later... */
89 usage(builtin_gc_usage
);
91 if (pack_refs
&& run_command_v_opt(argv_pack_refs
, RUN_GIT_CMD
))
92 return error(FAILED_RUN
, argv_pack_refs
[0]);
94 if (run_command_v_opt(argv_reflog
, RUN_GIT_CMD
))
95 return error(FAILED_RUN
, argv_reflog
[0]);
97 if (run_command_v_opt(argv_repack
, RUN_GIT_CMD
))
98 return error(FAILED_RUN
, argv_repack
[0]);
100 if (prune
&& run_command_v_opt(argv_prune
, RUN_GIT_CMD
))
101 return error(FAILED_RUN
, argv_prune
[0]);
103 if (run_command_v_opt(argv_rerere
, RUN_GIT_CMD
))
104 return error(FAILED_RUN
, argv_rerere
[0]);