Code

detect close failure on just-written file handles
[git.git] / builtin-gc.c
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  */
13 #include "cache.h"
14 #include "run-command.h"
16 #define FAILED_RUN "failed to run %s"
18 static const char builtin_gc_usage[] = "git-gc [--prune] [--aggressive]";
20 static int pack_refs = 1;
21 static int aggressive_window = -1;
23 #define MAX_ADD 10
24 static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
25 static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
26 static const char *argv_repack[MAX_ADD] = {"repack", "-a", "-d", "-l", NULL};
27 static const char *argv_prune[] = {"prune", NULL};
28 static const char *argv_rerere[] = {"rerere", "gc", NULL};
30 static int gc_config(const char *var, const char *value)
31 {
32         if (!strcmp(var, "gc.packrefs")) {
33                 if (!strcmp(value, "notbare"))
34                         pack_refs = -1;
35                 else
36                         pack_refs = git_config_bool(var, value);
37                 return 0;
38         }
39         if (!strcmp(var, "gc.aggressivewindow")) {
40                 aggressive_window = git_config_int(var, value);
41                 return 0;
42         }
43         return git_default_config(var, value);
44 }
46 static void append_option(const char **cmd, const char *opt, int max_length)
47 {
48         int i;
50         for (i = 0; cmd[i]; i++)
51                 ;
53         if (i + 2 >= max_length)
54                 die("Too many options specified");
55         cmd[i++] = opt;
56         cmd[i] = NULL;
57 }
59 int cmd_gc(int argc, const char **argv, const char *prefix)
60 {
61         int i;
62         int prune = 0;
63         char buf[80];
65         git_config(gc_config);
67         if (pack_refs < 0)
68                 pack_refs = !is_bare_repository();
70         for (i = 1; i < argc; i++) {
71                 const char *arg = argv[i];
72                 if (!strcmp(arg, "--prune")) {
73                         prune = 1;
74                         continue;
75                 }
76                 if (!strcmp(arg, "--aggressive")) {
77                         append_option(argv_repack, "-f", MAX_ADD);
78                         if (aggressive_window > 0) {
79                                 sprintf(buf, "--window=%d", aggressive_window);
80                                 append_option(argv_repack, buf, MAX_ADD);
81                         }
82                         continue;
83                 }
84                 /* perhaps other parameters later... */
85                 break;
86         }
87         if (i != argc)
88                 usage(builtin_gc_usage);
90         if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
91                 return error(FAILED_RUN, argv_pack_refs[0]);
93         if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
94                 return error(FAILED_RUN, argv_reflog[0]);
96         if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
97                 return error(FAILED_RUN, argv_repack[0]);
99         if (prune && run_command_v_opt(argv_prune, RUN_GIT_CMD))
100                 return error(FAILED_RUN, argv_prune[0]);
102         if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
103                 return error(FAILED_RUN, argv_rerere[0]);
105         return 0;