Add an option to quiet git-init.
[git/jrn.git] / builtin-gc.c
blob45025fba30c1fb594a696c26a7eac11862cfd8f9
1 /*
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
13 #include "cache.h"
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;
23 #define MAX_ADD 10
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"))
34 pack_refs = -1;
35 else
36 pack_refs = git_config_bool(var, value);
37 return 0;
39 if (!strcmp(var, "gc.aggressivewindow")) {
40 aggressive_window = git_config_int(var, value);
41 return 0;
43 return git_default_config(var, value);
46 static void append_option(const char **cmd, const char *opt, int max_length)
48 int i;
50 for (i = 0; cmd[i]; i++)
53 if (i + 2 >= max_length)
54 die("Too many options specified");
55 cmd[i++] = opt;
56 cmd[i] = NULL;
59 int cmd_gc(int argc, const char **argv, const char *prefix)
61 int i;
62 int prune = 0;
63 char buf[80];
65 git_config(gc_config);
67 if (pack_refs < 0)
68 pack_refs = !is_bare_repository();
70 for (i = 1; i < argc; i++) {
71 const char *arg = argv[i];
72 if (!strcmp(arg, "--prune")) {
73 prune = 1;
74 continue;
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);
82 continue;
84 /* perhaps other parameters later... */
85 break;
87 if (i != argc)
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]);
105 return 0;