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;
23 static int gc_auto_threshold
= 6700;
24 static int gc_auto_pack_limit
= 20;
27 static const char *argv_pack_refs
[] = {"pack-refs", "--all", "--prune", NULL
};
28 static const char *argv_reflog
[] = {"reflog", "expire", "--all", NULL
};
29 static const char *argv_repack
[MAX_ADD
] = {"repack", "-a", "-d", "-l", NULL
};
30 static const char *argv_prune
[] = {"prune", NULL
};
31 static const char *argv_rerere
[] = {"rerere", "gc", NULL
};
33 static int gc_config(const char *var
, const char *value
)
35 if (!strcmp(var
, "gc.packrefs")) {
36 if (!strcmp(value
, "notbare"))
39 pack_refs
= git_config_bool(var
, value
);
42 if (!strcmp(var
, "gc.aggressivewindow")) {
43 aggressive_window
= git_config_int(var
, value
);
46 if (!strcmp(var
, "gc.auto")) {
47 gc_auto_threshold
= git_config_int(var
, value
);
50 if (!strcmp(var
, "gc.autopacklimit")) {
51 gc_auto_pack_limit
= git_config_int(var
, value
);
54 return git_default_config(var
, value
);
57 static void append_option(const char **cmd
, const char *opt
, int max_length
)
61 for (i
= 0; cmd
[i
]; i
++)
64 if (i
+ 2 >= max_length
)
65 die("Too many options specified");
70 static int too_many_loose_objects(void)
73 * Quickly check if a "gc" is needed, by estimating how
74 * many loose objects there are. Because SHA-1 is evenly
75 * distributed, we can check only one and get a reasonable
79 const char *objdir
= get_object_directory();
86 if (gc_auto_threshold
<= 0)
89 if (sizeof(path
) <= snprintf(path
, sizeof(path
), "%s/17", objdir
)) {
90 warning("insanely long object directory %.*s", 50, objdir
);
97 auto_threshold
= (gc_auto_threshold
+ 255) / 256;
98 while ((ent
= readdir(dir
)) != NULL
) {
99 if (strspn(ent
->d_name
, "0123456789abcdef") != 38 ||
100 ent
->d_name
[38] != '\0')
102 if (++num_loose
> auto_threshold
) {
111 static int too_many_packs(void)
113 struct packed_git
*p
;
116 if (gc_auto_pack_limit
<= 0)
119 prepare_packed_git();
120 for (cnt
= 0, p
= packed_git
; p
; p
= p
->next
) {
127 len
= strlen(p
->pack_name
);
128 if (PATH_MAX
<= len
+ 1)
129 continue; /* oops, give up */
130 memcpy(path
, p
->pack_name
, len
-5);
131 memcpy(path
+ len
- 5, ".keep", 6);
132 keep
= access(p
->pack_name
, F_OK
) && (errno
== ENOENT
);
136 * Perhaps check the size of the pack and count only
137 * very small ones here?
141 return gc_auto_pack_limit
<= cnt
;
144 static int need_to_gc(void)
149 * Setting gc.auto and gc.autopacklimit to 0 or negative can
150 * disable the automatic gc.
152 if (gc_auto_threshold
<= 0 && gc_auto_pack_limit
<= 0)
156 * If there are too many loose objects, but not too many
157 * packs, we run "repack -d -l". If there are too many packs,
158 * we run "repack -A -d -l". Otherwise we tell the caller
161 argv_repack
[ac
++] = "repack";
162 if (too_many_packs())
163 argv_repack
[ac
++] = "-A";
164 else if (!too_many_loose_objects())
166 argv_repack
[ac
++] = "-d";
167 argv_repack
[ac
++] = "-l";
168 argv_repack
[ac
++] = NULL
;
172 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
179 git_config(gc_config
);
182 pack_refs
= !is_bare_repository();
184 for (i
= 1; i
< argc
; i
++) {
185 const char *arg
= argv
[i
];
186 if (!strcmp(arg
, "--prune")) {
190 if (!strcmp(arg
, "--aggressive")) {
191 append_option(argv_repack
, "-f", MAX_ADD
);
192 if (aggressive_window
> 0) {
193 sprintf(buf
, "--window=%d", aggressive_window
);
194 append_option(argv_repack
, buf
, MAX_ADD
);
198 if (!strcmp(arg
, "--auto")) {
205 usage(builtin_gc_usage
);
209 * Auto-gc should be least intrusive as possible.
216 if (pack_refs
&& run_command_v_opt(argv_pack_refs
, RUN_GIT_CMD
))
217 return error(FAILED_RUN
, argv_pack_refs
[0]);
219 if (run_command_v_opt(argv_reflog
, RUN_GIT_CMD
))
220 return error(FAILED_RUN
, argv_reflog
[0]);
222 if (run_command_v_opt(argv_repack
, RUN_GIT_CMD
))
223 return error(FAILED_RUN
, argv_repack
[0]);
225 if (prune
&& run_command_v_opt(argv_prune
, RUN_GIT_CMD
))
226 return error(FAILED_RUN
, argv_prune
[0]);
228 if (run_command_v_opt(argv_rerere
, RUN_GIT_CMD
))
229 return error(FAILED_RUN
, argv_rerere
[0]);
231 if (auto_gc
&& too_many_loose_objects())
232 warning("There are too many unreachable loose objects; "
233 "run 'git prune' to remove them.");