2006-07-06 19:16:22 +02:00
|
|
|
#include "cache.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "revision.h"
|
|
|
|
#include "builtin.h"
|
2007-01-06 11:16:17 +01:00
|
|
|
#include "reachable.h"
|
2008-03-23 21:50:29 +01:00
|
|
|
#include "parse-options.h"
|
2011-11-05 13:00:08 +01:00
|
|
|
#include "progress.h"
|
2009-01-10 13:07:50 +01:00
|
|
|
#include "dir.h"
|
2006-07-06 19:16:22 +02:00
|
|
|
|
2008-03-23 21:50:29 +01:00
|
|
|
static const char * const prune_usage[] = {
|
2012-08-20 14:32:32 +02:00
|
|
|
N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"),
|
2008-03-23 21:50:29 +01:00
|
|
|
NULL
|
|
|
|
};
|
2006-08-15 19:23:48 +02:00
|
|
|
static int show_only;
|
2008-09-29 18:49:52 +02:00
|
|
|
static int verbose;
|
2007-11-29 21:59:55 +01:00
|
|
|
static unsigned long expire;
|
2011-11-08 06:34:08 +01:00
|
|
|
static int show_progress = -1;
|
2006-07-06 19:16:22 +02:00
|
|
|
|
2013-12-18 00:22:31 +01:00
|
|
|
static int prune_tmp_file(const char *fullpath)
|
2008-07-25 00:41:12 +02:00
|
|
|
{
|
2010-02-27 04:50:02 +01:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(fullpath, &st))
|
|
|
|
return error("Could not stat '%s'", fullpath);
|
|
|
|
if (st.st_mtime > expire)
|
|
|
|
return 0;
|
2012-08-07 07:01:49 +02:00
|
|
|
if (show_only || verbose)
|
|
|
|
printf("Removing stale temporary file %s\n", fullpath);
|
2008-07-25 00:41:12 +02:00
|
|
|
if (!show_only)
|
2009-04-29 23:22:56 +02:00
|
|
|
unlink_or_warn(fullpath);
|
2008-07-25 00:41:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-18 00:22:31 +01:00
|
|
|
static int prune_object(const char *fullpath, const unsigned char *sha1)
|
2006-07-06 19:16:22 +02:00
|
|
|
{
|
2010-02-27 04:50:02 +01:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(fullpath, &st))
|
|
|
|
return error("Could not stat '%s'", fullpath);
|
|
|
|
if (st.st_mtime > expire)
|
|
|
|
return 0;
|
2008-09-29 18:49:52 +02:00
|
|
|
if (show_only || verbose) {
|
2007-02-26 20:55:59 +01: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 18:49:52 +02:00
|
|
|
}
|
|
|
|
if (!show_only)
|
2009-04-29 23:22:56 +02:00
|
|
|
unlink_or_warn(fullpath);
|
2006-07-06 19:16:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-18 00:22:31 +01:00
|
|
|
static int prune_dir(int i, struct strbuf *path)
|
2006-07-06 19:16:22 +02:00
|
|
|
{
|
2013-12-18 00:22:31 +01:00
|
|
|
size_t baselen = path->len;
|
|
|
|
DIR *dir = opendir(path->buf);
|
2006-07-06 19:16:22 +02:00
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((de = readdir(dir)) != NULL) {
|
|
|
|
char name[100];
|
|
|
|
unsigned char sha1[20];
|
|
|
|
|
2009-01-10 13:07:50 +01:00
|
|
|
if (is_dot_or_dotdot(de->d_name))
|
2006-07-06 19:16:22 +02:00
|
|
|
continue;
|
2009-01-10 13:07:50 +01:00
|
|
|
if (strlen(de->d_name) == 38) {
|
2006-07-06 19:16:22 +02:00
|
|
|
sprintf(name, "%02x", i);
|
2009-01-10 13:07:50 +01:00
|
|
|
memcpy(name+2, de->d_name, 39);
|
2006-07-06 19:16:22 +02: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;
|
|
|
|
|
2013-12-18 00:22:31 +01:00
|
|
|
strbuf_addf(path, "/%s", de->d_name);
|
|
|
|
prune_object(path->buf, sha1);
|
|
|
|
strbuf_setlen(path, baselen);
|
2006-07-06 19:16:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(de->d_name, "tmp_obj_")) {
|
2013-12-18 00:22:31 +01:00
|
|
|
strbuf_addf(path, "/%s", de->d_name);
|
|
|
|
prune_tmp_file(path->buf);
|
|
|
|
strbuf_setlen(path, baselen);
|
2008-08-05 20:01:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-18 00:22:31 +01:00
|
|
|
fprintf(stderr, "bad sha1 file: %s/%s\n", path->buf, de->d_name);
|
2006-07-06 19:16:22 +02:00
|
|
|
}
|
2012-03-06 10:18:41 +01:00
|
|
|
closedir(dir);
|
2007-03-21 04:32:13 +01:00
|
|
|
if (!show_only)
|
2013-12-18 00:22:31 +01:00
|
|
|
rmdir(path->buf);
|
2006-07-06 19:16:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void prune_object_dir(const char *path)
|
|
|
|
{
|
2013-12-18 00:22:31 +01:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
size_t baselen;
|
2006-07-06 19:16:22 +02:00
|
|
|
int i;
|
2013-12-18 00:22:31 +01:00
|
|
|
|
|
|
|
strbuf_addstr(&buf, path);
|
|
|
|
strbuf_addch(&buf, '/');
|
|
|
|
baselen = buf.len;
|
|
|
|
|
2006-07-06 19:16:22 +02:00
|
|
|
for (i = 0; i < 256; i++) {
|
2013-12-18 00:22:31 +01:00
|
|
|
strbuf_addf(&buf, "%02x", i);
|
|
|
|
prune_dir(i, &buf);
|
|
|
|
strbuf_setlen(&buf, baselen);
|
2006-07-06 19:16:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-07 03:55:14 +01:00
|
|
|
/*
|
|
|
|
* Write errors (particularly out of space) can result in
|
|
|
|
* failed temporary packs (and more rarely indexes and other
|
2010-02-04 06:23:18 +01:00
|
|
|
* files beginning with "tmp_") accumulating in the object
|
2008-09-23 01:34:26 +02:00
|
|
|
* and the pack directories.
|
2008-02-07 03:55:14 +01:00
|
|
|
*/
|
2008-09-23 01:34:26 +02:00
|
|
|
static void remove_temporary_files(const char *path)
|
2008-02-07 03:55:14 +01:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *de;
|
|
|
|
|
2008-09-23 01:34:26 +02:00
|
|
|
dir = opendir(path);
|
2008-02-07 03:55:14 +01:00
|
|
|
if (!dir) {
|
2008-09-23 01:34:26 +02:00
|
|
|
fprintf(stderr, "Unable to open directory %s\n", path);
|
2008-02-07 03:55:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-07-25 00:41:12 +02:00
|
|
|
while ((de = readdir(dir)) != NULL)
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(de->d_name, "tmp_"))
|
2013-12-18 00:22:31 +01:00
|
|
|
prune_tmp_file(mkpath("%s/%s", path, de->d_name));
|
2008-02-07 03:55:14 +01:00
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2006-07-29 07:44:25 +02:00
|
|
|
int cmd_prune(int argc, const char **argv, const char *prefix)
|
2006-07-06 19:16:22 +02:00
|
|
|
{
|
2007-01-06 11:16:10 +01:00
|
|
|
struct rev_info revs;
|
2011-11-08 06:34:08 +01:00
|
|
|
struct progress *progress = NULL;
|
2008-03-23 21:50:29 +01:00
|
|
|
const struct option options[] = {
|
2012-08-20 14:32:32 +02:00
|
|
|
OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
|
|
|
|
OPT__VERBOSE(&verbose, N_("report pruned objects")),
|
|
|
|
OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
|
2013-04-25 20:13:49 +02:00
|
|
|
OPT_EXPIRY_DATE(0, "expire", &expire,
|
|
|
|
N_("expire objects older than <time>")),
|
2008-03-23 21:50:29 +01:00
|
|
|
OPT_END()
|
|
|
|
};
|
2008-09-23 01:34:26 +02:00
|
|
|
char *s;
|
2006-07-06 19:16:22 +02:00
|
|
|
|
2010-02-27 04:50:02 +01:00
|
|
|
expire = ULONG_MAX;
|
2007-01-05 22:31:43 +01:00
|
|
|
save_commit_buffer = 0;
|
2009-01-23 10:07:46 +01:00
|
|
|
read_replace_refs = 0;
|
2006-07-29 07:44:25 +02:00
|
|
|
init_revisions(&revs, prefix);
|
2006-07-06 19:16:22 +02:00
|
|
|
|
2009-05-23 20:53:12 +02:00
|
|
|
argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
|
2008-03-25 07:20:51 +01:00
|
|
|
while (argc--) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
const char *name = *argv++;
|
|
|
|
|
|
|
|
if (!get_sha1(name, sha1)) {
|
2013-03-17 09:23:31 +01:00
|
|
|
struct object *object = parse_object_or_die(sha1, name);
|
2008-03-25 07:20:51 +01:00
|
|
|
add_pending_object(&revs, object, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
die("unrecognized argument: %s", name);
|
|
|
|
}
|
2011-11-08 06:34:08 +01:00
|
|
|
|
|
|
|
if (show_progress == -1)
|
|
|
|
show_progress = isatty(2);
|
|
|
|
if (show_progress)
|
2014-02-21 13:50:18 +01:00
|
|
|
progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
|
2011-11-08 06:34:08 +01:00
|
|
|
|
2011-11-05 13:00:08 +01:00
|
|
|
mark_reachable_objects(&revs, 1, progress);
|
|
|
|
stop_progress(&progress);
|
2006-07-06 19:16:22 +02:00
|
|
|
prune_object_dir(get_object_directory());
|
|
|
|
|
2013-05-27 13:18:47 +02:00
|
|
|
prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
|
2008-09-23 01:34:26 +02:00
|
|
|
remove_temporary_files(get_object_directory());
|
2012-09-04 19:31:14 +02:00
|
|
|
s = mkpathdup("%s/pack", get_object_directory());
|
2008-09-23 01:34:26 +02:00
|
|
|
remove_temporary_files(s);
|
|
|
|
free(s);
|
2013-12-05 14:02:54 +01:00
|
|
|
|
|
|
|
if (is_repository_shallow())
|
|
|
|
prune_shallow(show_only);
|
|
|
|
|
2006-07-06 19:16:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|