Merge branch 'master' of git://repo.or.cz/git/git-p4
[git/git.git] / builtin-gc.c
CommitLineData
6757ada4
JB
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
baffc0e7 13#include "builtin.h"
6757ada4
JB
14#include "cache.h"
15#include "run-command.h"
16
17#define FAILED_RUN "failed to run %s"
18
0d7566a5 19static const char builtin_gc_usage[] = "git-gc [--prune] [--aggressive]";
6757ada4 20
56752391 21static int pack_refs = 1;
0d7566a5 22static int aggressive_window = -1;
6757ada4 23
0d7566a5 24#define MAX_ADD 10
56752391 25static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
6757ada4 26static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
0d7566a5 27static const char *argv_repack[MAX_ADD] = {"repack", "-a", "-d", "-l", NULL};
6757ada4
JB
28static const char *argv_prune[] = {"prune", NULL};
29static const char *argv_rerere[] = {"rerere", "gc", NULL};
30
31static int gc_config(const char *var, const char *value)
32{
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;
39 }
0d7566a5
TT
40 if (!strcmp(var, "gc.aggressivewindow")) {
41 aggressive_window = git_config_int(var, value);
42 return 0;
43 }
6757ada4
JB
44 return git_default_config(var, value);
45}
46
0d7566a5
TT
47static void append_option(const char **cmd, const char *opt, int max_length)
48{
49 int i;
50
51 for (i = 0; cmd[i]; i++)
52 ;
53
54 if (i + 2 >= max_length)
55 die("Too many options specified");
56 cmd[i++] = opt;
57 cmd[i] = NULL;
58}
59
6757ada4
JB
60int cmd_gc(int argc, const char **argv, const char *prefix)
61{
62 int i;
63 int prune = 0;
0d7566a5 64 char buf[80];
6757ada4
JB
65
66 git_config(gc_config);
67
68 if (pack_refs < 0)
69 pack_refs = !is_bare_repository();
70
71 for (i = 1; i < argc; i++) {
72 const char *arg = argv[i];
73 if (!strcmp(arg, "--prune")) {
74 prune = 1;
75 continue;
76 }
0d7566a5
TT
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);
82 }
83 continue;
84 }
6757ada4
JB
85 /* perhaps other parameters later... */
86 break;
87 }
88 if (i != argc)
89 usage(builtin_gc_usage);
90
91 if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
92 return error(FAILED_RUN, argv_pack_refs[0]);
93
94 if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
95 return error(FAILED_RUN, argv_reflog[0]);
96
97 if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
98 return error(FAILED_RUN, argv_repack[0]);
99
100 if (prune && run_command_v_opt(argv_prune, RUN_GIT_CMD))
101 return error(FAILED_RUN, argv_prune[0]);
102
103 if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
104 return error(FAILED_RUN, argv_rerere[0]);
105
106 return 0;
107}