]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-gc.c
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;
26 static const char *argv_pack_refs
[] = {"pack-refs", "--all", "--prune", NULL
};
27 static const char *argv_reflog
[] = {"reflog", "expire", "--all", NULL
};
28 static const char *argv_repack
[MAX_ADD
] = {"repack", "-a", "-d", "-l", NULL
};
29 static const char *argv_prune
[] = {"prune", NULL
};
30 static const char *argv_rerere
[] = {"rerere", "gc", NULL
};
32 static const char *argv_repack_auto
[] = {"repack", "-d", "-l", NULL
};
34 static int gc_config(const char *var
, const char *value
)
36 if (!strcmp(var
, "gc.packrefs")) {
37 if (!strcmp(value
, "notbare"))
40 pack_refs
= git_config_bool(var
, value
);
43 if (!strcmp(var
, "gc.aggressivewindow")) {
44 aggressive_window
= git_config_int(var
, value
);
47 if (!strcmp(var
, "gc.auto")) {
48 gc_auto_threshold
= git_config_int(var
, value
);
51 return git_default_config(var
, value
);
54 static void append_option(const char **cmd
, const char *opt
, int max_length
)
58 for (i
= 0; cmd
[i
]; i
++)
61 if (i
+ 2 >= max_length
)
62 die("Too many options specified");
67 static int too_many_loose_objects(void)
70 * Quickly check if a "gc" is needed, by estimating how
71 * many loose objects there are. Because SHA-1 is evenly
72 * distributed, we can check only one and get a reasonable
76 const char *objdir
= get_object_directory();
83 if (sizeof(path
) <= snprintf(path
, sizeof(path
), "%s/17", objdir
)) {
84 warning("insanely long object directory %.*s", 50, objdir
);
91 auto_threshold
= (gc_auto_threshold
+ 255) / 256;
92 while ((ent
= readdir(dir
)) != NULL
) {
93 if (strspn(ent
->d_name
, "0123456789abcdef") != 38 ||
94 ent
->d_name
[38] != '\0')
96 if (++num_loose
> auto_threshold
) {
105 static int need_to_gc(void)
108 * Setting gc.auto to 0 or negative can disable the
111 if (gc_auto_threshold
<= 0)
114 return too_many_loose_objects();
117 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
124 git_config(gc_config
);
127 pack_refs
= !is_bare_repository();
129 for (i
= 1; i
< argc
; i
++) {
130 const char *arg
= argv
[i
];
131 if (!strcmp(arg
, "--prune")) {
135 if (!strcmp(arg
, "--aggressive")) {
136 append_option(argv_repack
, "-f", MAX_ADD
);
137 if (aggressive_window
> 0) {
138 sprintf(buf
, "--window=%d", aggressive_window
);
139 append_option(argv_repack
, buf
, MAX_ADD
);
143 if (!strcmp(arg
, "--auto")) {
150 usage(builtin_gc_usage
);
154 * Auto-gc should be least intrusive as possible.
157 for (i
= 0; i
< ARRAY_SIZE(argv_repack_auto
); i
++)
158 argv_repack
[i
] = argv_repack_auto
[i
];
163 if (pack_refs
&& run_command_v_opt(argv_pack_refs
, RUN_GIT_CMD
))
164 return error(FAILED_RUN
, argv_pack_refs
[0]);
166 if (run_command_v_opt(argv_reflog
, RUN_GIT_CMD
))
167 return error(FAILED_RUN
, argv_reflog
[0]);
169 if (run_command_v_opt(argv_repack
, RUN_GIT_CMD
))
170 return error(FAILED_RUN
, argv_repack
[0]);
172 if (prune
&& run_command_v_opt(argv_prune
, RUN_GIT_CMD
))
173 return error(FAILED_RUN
, argv_prune
[0]);
175 if (run_command_v_opt(argv_rerere
, RUN_GIT_CMD
))
176 return error(FAILED_RUN
, argv_rerere
[0]);
178 if (auto_gc
&& too_many_loose_objects())
179 warning("There are too many unreachable loose objects; "
180 "run 'git prune' to remove them.");