prune: --grace=time
[git/mingw/4msysgit/wingit-dll.git] / builtin-prune.c
blob7929af1aacd515bcc49bb79e9e3d8ee616397b4f
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "builtin.h"
6 #include "reachable.h"
8 static const char prune_usage[] = "git-prune [-n] [--grace=time]";
9 static int show_only;
10 static int prune_grace_period;
12 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
14 char buf[20];
15 const char *type;
17 if (show_only) {
18 if (sha1_object_info(sha1, buf, NULL))
19 type = "unknown";
20 else
21 type = buf;
22 printf("%s %s\n", sha1_to_hex(sha1), type);
23 return 0;
25 unlink(mkpath("%s/%s", path, filename));
26 rmdir(path);
27 return 0;
30 static int prune_dir(int i, char *path)
32 DIR *dir = opendir(path);
33 struct dirent *de;
35 if (!dir)
36 return 0;
38 while ((de = readdir(dir)) != NULL) {
39 char name[100];
40 unsigned char sha1[20];
41 int len = strlen(de->d_name);
42 struct stat st;
44 switch (len) {
45 case 2:
46 if (de->d_name[1] != '.')
47 break;
48 case 1:
49 if (de->d_name[0] != '.')
50 break;
51 continue;
52 case 38:
53 sprintf(name, "%02x", i);
54 memcpy(name+2, de->d_name, len+1);
55 if (get_sha1_hex(name, sha1) < 0)
56 break;
59 * Do we know about this object?
60 * It must have been reachable
62 if (lookup_object(sha1))
63 continue;
65 if (prune_grace_period > 0 &&
66 !stat(mkpath("%s/%s", path, de->d_name), &st) &&
67 st.st_mtime > prune_grace_period)
68 continue;
70 prune_object(path, de->d_name, sha1);
71 continue;
73 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
75 closedir(dir);
76 return 0;
79 static void prune_object_dir(const char *path)
81 int i;
82 for (i = 0; i < 256; i++) {
83 static char dir[4096];
84 sprintf(dir, "%s/%02x", path, i);
85 prune_dir(i, dir);
89 static int git_prune_config(const char *var, const char *value)
91 if (!strcmp(var, "gc.prunegrace")) {
92 if (!strcmp(value, "off"))
93 prune_grace_period = 0;
94 else
95 prune_grace_period = approxidate(value);
96 return 0;
98 return git_default_config(var, value);
101 int cmd_prune(int argc, const char **argv, const char *prefix)
103 int i;
104 struct rev_info revs;
105 prune_grace_period = time(NULL)-24*60*60;
107 git_config(git_prune_config);
109 for (i = 1; i < argc; i++) {
110 const char *arg = argv[i];
111 if (!strcmp(arg, "-n")) {
112 show_only = 1;
113 continue;
115 if (!strncmp(arg, "--grace=", 8)) {
116 if (!strcmp(arg+8, "off"))
117 prune_grace_period = 0;
118 else
119 prune_grace_period = approxidate(arg+8);
120 continue;
122 usage(prune_usage);
125 save_commit_buffer = 0;
126 init_revisions(&revs, prefix);
127 mark_reachable_objects(&revs, 1);
129 prune_object_dir(get_object_directory());
131 sync();
132 prune_packed_objects(show_only);
133 return 0;