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_tmp_object(char *path
, const char *filename
)
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
)
26 printf("Removing stale temporary file %s\n", fullpath
);
32 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
34 const char *fullpath
= mkpath("%s/%s", path
, filename
);
37 if (lstat(fullpath
, &st
))
38 return error("Could not stat '%s'", fullpath
);
39 if (st
.st_mtime
> expire
)
43 enum object_type type
= sha1_object_info(sha1
, NULL
);
44 printf("%s %s\n", sha1_to_hex(sha1
),
45 (type
> 0) ? typename(type
) : "unknown");
51 static int prune_dir(int i
, char *path
)
53 DIR *dir
= opendir(path
);
59 while ((de
= readdir(dir
)) != NULL
) {
61 unsigned char sha1
[20];
62 int len
= strlen(de
->d_name
);
66 if (de
->d_name
[1] != '.')
69 if (de
->d_name
[0] != '.')
73 if (prefixcmp(de
->d_name
, "tmp_obj_"))
75 prune_tmp_object(path
, de
->d_name
);
78 sprintf(name
, "%02x", i
);
79 memcpy(name
+2, de
->d_name
, len
+1);
80 if (get_sha1_hex(name
, sha1
) < 0)
84 * Do we know about this object?
85 * It must have been reachable
87 if (lookup_object(sha1
))
90 prune_object(path
, de
->d_name
, sha1
);
93 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
101 static void prune_object_dir(const char *path
)
104 for (i
= 0; i
< 256; i
++) {
105 static char dir
[4096];
106 sprintf(dir
, "%s/%02x", path
, i
);
112 * Write errors (particularly out of space) can result in
113 * failed temporary packs (and more rarely indexes and other
114 * files begining with "tmp_") accumulating in the
117 static void remove_temporary_files(void)
121 char* dirname
=get_object_directory();
123 dir
= opendir(dirname
);
125 fprintf(stderr
, "Unable to open object directory %s\n",
129 while ((de
= readdir(dir
)) != NULL
)
130 if (!prefixcmp(de
->d_name
, "tmp_"))
131 prune_tmp_object(dirname
, de
->d_name
);
135 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
137 struct rev_info revs
;
138 const struct option options
[] = {
139 OPT_BOOLEAN('n', NULL
, &show_only
,
140 "do not remove, show only"),
141 OPT_DATE(0, "expire", &expire
,
142 "expire objects older than <time>"),
146 save_commit_buffer
= 0;
147 init_revisions(&revs
, prefix
);
149 argc
= parse_options(argc
, argv
, options
, prune_usage
, 0);
151 unsigned char sha1
[20];
152 const char *name
= *argv
++;
154 if (!get_sha1(name
, sha1
)) {
155 struct object
*object
= parse_object(sha1
);
157 die("bad object: %s", name
);
158 add_pending_object(&revs
, object
, "");
161 die("unrecognized argument: %s", name
);
163 mark_reachable_objects(&revs
, 1);
164 prune_object_dir(get_object_directory());
166 prune_packed_objects(show_only
);
167 remove_temporary_files();