8 static const char prune_usage
[] = "git-prune [-n] [--grace=time]";
10 static int prune_grace_period
;
12 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
18 if (sha1_object_info(sha1
, buf
, NULL
))
22 printf("%s %s\n", sha1_to_hex(sha1
), type
);
25 unlink(mkpath("%s/%s", path
, filename
));
30 static int prune_dir(int i
, char *path
)
32 DIR *dir
= opendir(path
);
38 while ((de
= readdir(dir
)) != NULL
) {
40 unsigned char sha1
[20];
41 int len
= strlen(de
->d_name
);
46 if (de
->d_name
[1] != '.')
49 if (de
->d_name
[0] != '.')
53 sprintf(name
, "%02x", i
);
54 memcpy(name
+2, de
->d_name
, len
+1);
55 if (get_sha1_hex(name
, sha1
) < 0)
59 * Do we know about this object?
60 * It must have been reachable
62 if (lookup_object(sha1
))
65 if (prune_grace_period
> 0 &&
66 !stat(mkpath("%s/%s", path
, de
->d_name
), &st
) &&
67 st
.st_mtime
> prune_grace_period
)
70 prune_object(path
, de
->d_name
, sha1
);
73 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
79 static void prune_object_dir(const char *path
)
82 for (i
= 0; i
< 256; i
++) {
83 static char dir
[4096];
84 sprintf(dir
, "%s/%02x", path
, i
);
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;
95 prune_grace_period
= approxidate(value
);
98 return git_default_config(var
, value
);
101 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
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")) {
115 if (!strncmp(arg
, "--grace=", 8)) {
116 if (!strcmp(arg
+8, "off"))
117 prune_grace_period
= 0;
119 prune_grace_period
= approxidate(arg
+8);
125 save_commit_buffer
= 0;
126 init_revisions(&revs
, prefix
);
127 mark_reachable_objects(&revs
, 1);
129 prune_object_dir(get_object_directory());
132 prune_packed_objects(show_only
);