@matheusbsbs / matheus-bagliano / commits / 2c3c439947
Fork 0

Implement git gc --auto

This implements a new option "git gc --auto". When gc.auto is set to a positive value, and the object database has accumulated roughly that many number of loose objects, this runs a lightweight version of "git gc". The primary difference from the full "git gc" is that it does not pass "-a" option to "git repack", which means we do not try to repack _everything_, but only repack incrementally. We still do "git prune-packed". The default threshold is arbitrarily set by yours truly to: - not trigger it for fully unpacked git v0.99 history; - do trigger it for fully unpacked git v1.0.0 history; - not trigger it for incremental update to git v1.0.0 starting from fully packed git v0.99 history. This patch does not add invocation of the "auto repacking". It is left to key Porcelain commands that could produce tons of loose objects to add a call to "git gc --auto" after they are done their work. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 5, 2007 at 13:01 UTC 2c3c4399477533329579ca6b84824ef0b125914f
1 file changed +63 -1
builtin-gc.c
+63 -1
@@ -20,6 +20,7 @@ static const char builtin_gc_usage[] = "git-gc [--prune] [--aggressive]";
20
21 static int pack_refs = 1;
22 static int aggressive_window = -1;
23 +static int gc_auto_threshold = 6700;
24
25 #define MAX_ADD 10
26 static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
@@ -28,6 +29,8 @@ 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};
31
32 +static const char *argv_repack_auto[] = {"repack", "-d", "-l", NULL};
33 +
34 static int gc_config(const char *var, const char *value)
35 {
36 if (!strcmp(var, "gc.packrefs")) {
@@ -41,6 +44,10 @@ static int gc_config(const char *var, const char *value)
44 aggressive_window = git_config_int(var, value);
45 return 0;
46 }
47 + if (!strcmp(var, "gc.auto")) {
48 + gc_auto_threshold = git_config_int(var, value);
49 + return 0;
50 + }
51 return git_default_config(var, value);
52 }
53
@@ -57,10 +64,49 @@ static void append_option(const char **cmd, const char *opt, int max_length)
64 cmd[i] = NULL;
65 }
66
67 +static int need_to_gc(void)
68 +{
69 + /*
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
73 + * estimate.
74 + */
75 + char path[PATH_MAX];
76 + const char *objdir = get_object_directory();
77 + DIR *dir;
78 + struct dirent *ent;
79 + int auto_threshold;
80 + int num_loose = 0;
81 + int needed = 0;
82 +
83 + if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
84 + warning("insanely long object directory %.*s", 50, objdir);
85 + return 0;
86 + }
87 + dir = opendir(path);
88 + if (!dir)
89 + return 0;
90 +
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')
95 + continue;
96 + if (++num_loose > auto_threshold) {
97 + needed = 1;
98 + break;
99 + }
100 + }
101 + closedir(dir);
102 + return needed;
103 +}
104 +
105 int cmd_gc(int argc, const char **argv, const char *prefix)
106 {
107 int i;
108 int prune = 0;
109 + int auto_gc = 0;
110 char buf[80];
111
112 git_config(gc_config);
@@ -82,12 +128,28 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
128 }
129 continue;
130 }
85 - /* perhaps other parameters later... */
131 + if (!strcmp(arg, "--auto")) {
132 + if (gc_auto_threshold <= 0)
133 + return 0;
134 + auto_gc = 1;
135 + continue;
136 + }
137 break;
138 }
139 if (i != argc)
140 usage(builtin_gc_usage);
141
142 + if (auto_gc) {
143 + /*
144 + * Auto-gc should be least intrusive as possible.
145 + */
146 + prune = 0;
147 + for (i = 0; i < ARRAY_SIZE(argv_repack_auto); i++)
148 + argv_repack[i] = argv_repack_auto[i];
149 + if (!need_to_gc())
150 + return 0;
151 + }
152 +
153 if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
154 return error(FAILED_RUN, argv_pack_refs[0]);
155