2006-07-06 17:16:22 +00:00
|
|
|
#include "cache.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "revision.h"
|
|
|
|
#include "builtin.h"
|
2007-01-06 10:16:17 +00:00
|
|
|
#include "reachable.h"
|
2008-03-23 20:50:29 +00:00
|
|
|
#include "parse-options.h"
|
2009-01-10 12:07:50 +00:00
|
|
|
#include "dir.h"
|
2006-07-06 17:16:22 +00:00
|
|
|
|
2008-03-23 20:50:29 +00:00
|
|
|
static const char * const prune_usage[] = {
|
2008-09-29 16:49:52 +00:00
|
|
|
"git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
|
2008-03-23 20:50:29 +00:00
|
|
|
NULL
|
|
|
|
};
|
2006-08-15 17:23:48 +00:00
|
|
|
static int show_only;
|
2008-09-29 16:49:52 +00:00
|
|
|
static int verbose;
|
2007-11-29 20:59:55 +00:00
|
|
|
static unsigned long expire;
|
2006-07-06 17:16:22 +00:00
|
|
|
|
2008-09-22 23:34:26 +00:00
|
|
|
static int prune_tmp_object(const char *path, const char *filename)
|
2008-07-24 22:41:12 +00:00
|
|
|
{
|
|
|
|
const char *fullpath = mkpath("%s/%s", path, filename);
|
|
|
|
if (expire) {
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(fullpath, &st))
|
|
|
|
return error("Could not stat '%s'", fullpath);
|
|
|
|
if (st.st_mtime > expire)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
printf("Removing stale temporary file %s\n", fullpath);
|
|
|
|
if (!show_only)
|
2009-04-29 21:22:56 +00:00
|
|
|
unlink_or_warn(fullpath);
|
2008-07-24 22:41:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-06 17:16:22 +00:00
|
|
|
static int prune_object(char *path, const char *filename, const unsigned char *sha1)
|
|
|
|
{
|
2007-11-29 20:59:55 +00:00
|
|
|
const char *fullpath = mkpath("%s/%s", path, filename);
|
|
|
|
if (expire) {
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(fullpath, &st))
|
|
|
|
return error("Could not stat '%s'", fullpath);
|
|
|
|
if (st.st_mtime > expire)
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-29 16:49:52 +00:00
|
|
|
if (show_only || verbose) {
|
2007-02-26 19:55:59 +00:00
|
|
|
enum object_type type = sha1_object_info(sha1, NULL);
|
|
|
|
printf("%s %s\n", sha1_to_hex(sha1),
|
|
|
|
(type > 0) ? typename(type) : "unknown");
|
2008-09-29 16:49:52 +00:00
|
|
|
}
|
|
|
|
if (!show_only)
|
2009-04-29 21:22:56 +00:00
|
|
|
unlink_or_warn(fullpath);
|
2006-07-06 17:16:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prune_dir(int i, char *path)
|
|
|
|
{
|
|
|
|
DIR *dir = opendir(path);
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((de = readdir(dir)) != NULL) {
|
|
|
|
char name[100];
|
|
|
|
unsigned char sha1[20];
|
|
|
|
|
2009-01-10 12:07:50 +00:00
|
|
|
if (is_dot_or_dotdot(de->d_name))
|
2006-07-06 17:16:22 +00:00
|
|
|
continue;
|
2009-01-10 12:07:50 +00:00
|
|
|
if (strlen(de->d_name) == 38) {
|
2006-07-06 17:16:22 +00:00
|
|
|
sprintf(name, "%02x", i);
|
2009-01-10 12:07:50 +00:00
|
|
|
memcpy(name+2, de->d_name, 39);
|
2006-07-06 17:16:22 +00:00
|
|
|
if (get_sha1_hex(name, sha1) < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do we know about this object?
|
|
|
|
* It must have been reachable
|
|
|
|
*/
|
|
|
|
if (lookup_object(sha1))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
prune_object(path, de->d_name, sha1);
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-05 18:01:50 +00:00
|
|
|
if (!prefixcmp(de->d_name, "tmp_obj_")) {
|
|
|
|
prune_tmp_object(path, de->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-06 17:16:22 +00:00
|
|
|
fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
|
|
|
|
}
|
2007-03-21 03:32:13 +00:00
|
|
|
if (!show_only)
|
|
|
|
rmdir(path);
|
2006-07-06 17:16:22 +00:00
|
|
|
closedir(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prune_object_dir(const char *path)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
static char dir[4096];
|
|
|
|
sprintf(dir, "%s/%02x", path, i);
|
|
|
|
prune_dir(i, dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-07 02:55:14 +00:00
|
|
|
/*
|
|
|
|
* Write errors (particularly out of space) can result in
|
|
|
|
* failed temporary packs (and more rarely indexes and other
|
2008-09-22 23:34:26 +00:00
|
|
|
* files begining with "tmp_") accumulating in the object
|
|
|
|
* and the pack directories.
|
2008-02-07 02:55:14 +00:00
|
|
|
*/
|
2008-09-22 23:34:26 +00:00
|
|
|
static void remove_temporary_files(const char *path)
|
2008-02-07 02:55:14 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *de;
|
|
|
|
|
2008-09-22 23:34:26 +00:00
|
|
|
dir = opendir(path);
|
2008-02-07 02:55:14 +00:00
|
|
|
if (!dir) {
|
2008-09-22 23:34:26 +00:00
|
|
|
fprintf(stderr, "Unable to open directory %s\n", path);
|
2008-02-07 02:55:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-07-24 22:41:12 +00:00
|
|
|
while ((de = readdir(dir)) != NULL)
|
|
|
|
if (!prefixcmp(de->d_name, "tmp_"))
|
2008-09-22 23:34:26 +00:00
|
|
|
prune_tmp_object(path, de->d_name);
|
2008-02-07 02:55:14 +00:00
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:44:25 +00:00
|
|
|
int cmd_prune(int argc, const char **argv, const char *prefix)
|
2006-07-06 17:16:22 +00:00
|
|
|
{
|
2007-01-06 10:16:10 +00:00
|
|
|
struct rev_info revs;
|
2008-03-23 20:50:29 +00:00
|
|
|
const struct option options[] = {
|
|
|
|
OPT_BOOLEAN('n', NULL, &show_only,
|
|
|
|
"do not remove, show only"),
|
2008-09-29 16:49:52 +00:00
|
|
|
OPT_BOOLEAN('v', NULL, &verbose,
|
|
|
|
"report pruned objects"),
|
2008-03-23 20:50:29 +00:00
|
|
|
OPT_DATE(0, "expire", &expire,
|
|
|
|
"expire objects older than <time>"),
|
|
|
|
OPT_END()
|
|
|
|
};
|
2008-09-22 23:34:26 +00:00
|
|
|
char *s;
|
2006-07-06 17:16:22 +00:00
|
|
|
|
2007-01-05 21:31:43 +00:00
|
|
|
save_commit_buffer = 0;
|
2009-01-23 09:07:46 +00:00
|
|
|
read_replace_refs = 0;
|
2006-07-29 05:44:25 +00:00
|
|
|
init_revisions(&revs, prefix);
|
2006-07-06 17:16:22 +00:00
|
|
|
|
2009-05-23 18:53:12 +00:00
|
|
|
argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
|
2008-03-25 06:20:51 +00:00
|
|
|
while (argc--) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
const char *name = *argv++;
|
|
|
|
|
|
|
|
if (!get_sha1(name, sha1)) {
|
|
|
|
struct object *object = parse_object(sha1);
|
|
|
|
if (!object)
|
|
|
|
die("bad object: %s", name);
|
|
|
|
add_pending_object(&revs, object, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
die("unrecognized argument: %s", name);
|
|
|
|
}
|
2008-03-23 20:50:29 +00:00
|
|
|
mark_reachable_objects(&revs, 1);
|
2006-07-06 17:16:22 +00:00
|
|
|
prune_object_dir(get_object_directory());
|
|
|
|
|
2006-10-22 23:01:23 +00:00
|
|
|
prune_packed_objects(show_only);
|
2008-09-22 23:34:26 +00:00
|
|
|
remove_temporary_files(get_object_directory());
|
|
|
|
s = xstrdup(mkpath("%s/pack", get_object_directory()));
|
|
|
|
remove_temporary_files(s);
|
|
|
|
free(s);
|
2006-07-06 17:16:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|