Make gc a builtin.
Signed-off-by: James Bowes <jbowes@dangerouslyinc.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
James Bowes committed
Mar 13, 2007 at 21:58 UTC
6757ada403bf0eb0fb1fddcffbbeb74d91cbbb51
5 files changed
+82
-1
Makefile
+2
-1
@@ -177,7 +177,7 @@ BASIC_LDFLAGS =
177
SCRIPT_SH = \
178
git-bisect.sh git-checkout.sh \
179
git-clean.sh git-clone.sh git-commit.sh \
180
- git-fetch.sh git-gc.sh \
180
+ git-fetch.sh \
181
git-ls-remote.sh \
182
git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
183
git-pull.sh git-rebase.sh \
@@ -297,6 +297,7 @@ BUILTIN_OBJS = \
297
builtin-fmt-merge-msg.o \
298
builtin-for-each-ref.o \
299
builtin-fsck.o \
300
+ builtin-gc.o \
301
builtin-grep.o \
302
builtin-init-db.o \
303
builtin-log.o \
builtin-gc.c
new
+78
@@ -0,0 +1,78 @@
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
+
13
+#include "cache.h"
14
+#include "run-command.h"
15
+
16
+#define FAILED_RUN "failed to run %s"
17
+
18
+static const char builtin_gc_usage[] = "git-gc [--prune]";
19
+
20
+static int pack_refs = -1;
21
+
22
+static const char *argv_pack_refs[] = {"pack-refs", "--prune", NULL};
23
+static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
24
+static const char *argv_repack[] = {"repack", "-a", "-d", "-l", NULL};
25
+static const char *argv_prune[] = {"prune", NULL};
26
+static const char *argv_rerere[] = {"rerere", "gc", NULL};
27
+
28
+static int gc_config(const char *var, const char *value)
29
+{
30
+ if (!strcmp(var, "gc.packrefs")) {
31
+ if (!strcmp(value, "notbare"))
32
+ pack_refs = -1;
33
+ else
34
+ pack_refs = git_config_bool(var, value);
35
+ return 0;
36
+ }
37
+ return git_default_config(var, value);
38
+}
39
+
40
+int cmd_gc(int argc, const char **argv, const char *prefix)
41
+{
42
+ int i;
43
+ int prune = 0;
44
+
45
+ git_config(gc_config);
46
+
47
+ if (pack_refs < 0)
48
+ pack_refs = !is_bare_repository();
49
+
50
+ for (i = 1; i < argc; i++) {
51
+ const char *arg = argv[i];
52
+ if (!strcmp(arg, "--prune")) {
53
+ prune = 1;
54
+ continue;
55
+ }
56
+ /* perhaps other parameters later... */
57
+ break;
58
+ }
59
+ if (i != argc)
60
+ usage(builtin_gc_usage);
61
+
62
+ if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
63
+ return error(FAILED_RUN, argv_pack_refs[0]);
64
+
65
+ if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
66
+ return error(FAILED_RUN, argv_reflog[0]);
67
+
68
+ if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
69
+ return error(FAILED_RUN, argv_repack[0]);
70
+
71
+ if (prune && run_command_v_opt(argv_prune, RUN_GIT_CMD))
72
+ return error(FAILED_RUN, argv_prune[0]);
73
+
74
+ if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
75
+ return error(FAILED_RUN, argv_rerere[0]);
76
+
77
+ return 0;
78
+}
builtin.h
+1
@@ -37,6 +37,7 @@ extern int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
37
extern int cmd_for_each_ref(int argc, const char **argv, const char *prefix);
38
extern int cmd_format_patch(int argc, const char **argv, const char *prefix);
39
extern int cmd_fsck(int argc, const char **argv, const char *prefix);
40
+extern int cmd_gc(int argc, const char **argv, const char *prefix);
41
extern int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
42
extern int cmd_grep(int argc, const char **argv, const char *prefix);
43
extern int cmd_help(int argc, const char **argv, const char *prefix);
contrib/examples/git-gc.sh
renamed
git.c
+1
@@ -249,6 +249,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
249
{ "format-patch", cmd_format_patch, RUN_SETUP },
250
{ "fsck", cmd_fsck, RUN_SETUP },
251
{ "fsck-objects", cmd_fsck, RUN_SETUP },
252
+ { "gc", cmd_gc, RUN_SETUP },
253
{ "get-tar-commit-id", cmd_get_tar_commit_id },
254
{ "grep", cmd_grep, RUN_SETUP | USE_PAGER },
255
{ "help", cmd_help },