Fix a crash in ls-remote when refspec expands into nothing
[git/git-bigfiles.git] / builtin-gc.c
blob939748261041049f31d62935ec08f062bdfa6e79
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 "builtin.h"
14 #include "cache.h"
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;
24 #define MAX_ADD 10
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"))
35 pack_refs = -1;
36 else
37 pack_refs = git_config_bool(var, value);
38 return 0;
40 if (!strcmp(var, "gc.aggressivewindow")) {
41 aggressive_window = git_config_int(var, value);
42 return 0;
44 return git_default_config(var, value);
47 static void append_option(const char **cmd, const char *opt, int max_length)
49 int i;
51 for (i = 0; cmd[i]; i++)
54 if (i + 2 >= max_length)
55 die("Too many options specified");
56 cmd[i++] = opt;
57 cmd[i] = NULL;
60 int cmd_gc(int argc, const char **argv, const char *prefix)
62 int i;
63 int prune = 0;
64 char buf[80];
66 git_config(gc_config);
68 if (pack_refs < 0)
69 pack_refs = !is_bare_repository();
71 for (i = 1; i < argc; i++) {
72 const char *arg = argv[i];
73 if (!strcmp(arg, "--prune")) {
74 prune = 1;
75 continue;
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);
83 continue;
85 /* perhaps other parameters later... */
86 break;
88 if (i != argc)
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]);
106 return 0;