7 #include "parse-options.h"
9 static const char * const prune_usage
[] = {
10 "git-prune [-n] [--expire <time>] [--] [<head>...]",
14 static unsigned long expire
;
16 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
18 const char *fullpath
= mkpath("%s/%s", path
, filename
);
21 if (lstat(fullpath
, &st
))
22 return error("Could not stat '%s'", fullpath
);
23 if (st
.st_mtime
> expire
)
27 enum object_type type
= sha1_object_info(sha1
, NULL
);
28 printf("%s %s\n", sha1_to_hex(sha1
),
29 (type
> 0) ? typename(type
) : "unknown");
35 static int prune_dir(int i
, char *path
)
37 DIR *dir
= opendir(path
);
43 while ((de
= readdir(dir
)) != NULL
) {
45 unsigned char sha1
[20];
46 int len
= strlen(de
->d_name
);
50 if (de
->d_name
[1] != '.')
53 if (de
->d_name
[0] != '.')
57 sprintf(name
, "%02x", i
);
58 memcpy(name
+2, de
->d_name
, len
+1);
59 if (get_sha1_hex(name
, sha1
) < 0)
63 * Do we know about this object?
64 * It must have been reachable
66 if (lookup_object(sha1
))
69 prune_object(path
, de
->d_name
, sha1
);
72 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
80 static void prune_object_dir(const char *path
)
83 for (i
= 0; i
< 256; i
++) {
84 static char dir
[4096];
85 sprintf(dir
, "%s/%02x", path
, i
);
91 * Write errors (particularly out of space) can result in
92 * failed temporary packs (and more rarely indexes and other
93 * files begining with "tmp_") accumulating in the
96 static void remove_temporary_files(void)
100 char* dirname
=get_object_directory();
102 dir
= opendir(dirname
);
104 fprintf(stderr
, "Unable to open object directory %s\n",
108 while ((de
= readdir(dir
)) != NULL
) {
109 if (!prefixcmp(de
->d_name
, "tmp_")) {
111 int c
= snprintf(name
, PATH_MAX
, "%s/%s",
112 dirname
, de
->d_name
);
113 if (c
< 0 || c
>= PATH_MAX
)
117 if (stat(name
, &st
) != 0 || st
.st_mtime
>= expire
)
120 printf("Removing stale temporary file %s\n", name
);
128 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
130 struct rev_info revs
;
131 const struct option options
[] = {
132 OPT_BOOLEAN('n', NULL
, &show_only
,
133 "do not remove, show only"),
134 OPT_DATE(0, "expire", &expire
,
135 "expire objects older than <time>"),
139 save_commit_buffer
= 0;
140 init_revisions(&revs
, prefix
);
142 argc
= parse_options(argc
, argv
, options
, prune_usage
, 0);
144 die ("unrecognized argument: %s", name
);
145 mark_reachable_objects(&revs
, 1);
146 prune_object_dir(get_object_directory());
149 prune_packed_objects(show_only
);
150 remove_temporary_files();