You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
1.9 KiB
87 lines
1.9 KiB
19 years ago
|
#include "builtin.h"
|
||
20 years ago
|
#include "cache.h"
|
||
17 years ago
|
#include "progress.h"
|
||
16 years ago
|
#include "parse-options.h"
|
||
20 years ago
|
|
||
16 years ago
|
static const char * const prune_packed_usage[] = {
|
||
|
"git prune-packed [-n|--dry-run] [-q|--quiet]",
|
||
|
NULL
|
||
|
};
|
||
20 years ago
|
|
||
18 years ago
|
#define DRY_RUN 01
|
||
|
#define VERBOSE 02
|
||
|
|
||
17 years ago
|
static struct progress *progress;
|
||
17 years ago
|
|
||
18 years ago
|
static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
|
||
20 years ago
|
{
|
||
|
struct dirent *de;
|
||
|
char hex[40];
|
||
|
|
||
|
sprintf(hex, "%02x", i);
|
||
|
while ((de = readdir(dir)) != NULL) {
|
||
|
unsigned char sha1[20];
|
||
|
if (strlen(de->d_name) != 38)
|
||
|
continue;
|
||
|
memcpy(hex+2, de->d_name, 38);
|
||
|
if (get_sha1_hex(hex, sha1))
|
||
|
continue;
|
||
16 years ago
|
if (!has_sha1_pack(sha1))
|
||
20 years ago
|
continue;
|
||
|
memcpy(pathname + len, de->d_name, 38);
|
||
18 years ago
|
if (opts & DRY_RUN)
|
||
20 years ago
|
printf("rm -f %s\n", pathname);
|
||
16 years ago
|
else
|
||
|
unlink_or_warn(pathname);
|
||
17 years ago
|
display_progress(progress, i + 1);
|
||
20 years ago
|
}
|
||
20 years ago
|
pathname[len] = 0;
|
||
20 years ago
|
rmdir(pathname);
|
||
20 years ago
|
}
|
||
|
|
||
18 years ago
|
void prune_packed_objects(int opts)
|
||
20 years ago
|
{
|
||
|
int i;
|
||
|
static char pathname[PATH_MAX];
|
||
|
const char *dir = get_object_directory();
|
||
|
int len = strlen(dir);
|
||
|
|
||
17 years ago
|
if (opts == VERBOSE)
|
||
17 years ago
|
progress = start_progress_delay("Removing duplicate objects",
|
||
17 years ago
|
256, 95, 2);
|
||
|
|
||
20 years ago
|
if (len > PATH_MAX - 42)
|
||
|
die("impossible object directory");
|
||
|
memcpy(pathname, dir, len);
|
||
|
if (len && pathname[len-1] != '/')
|
||
|
pathname[len++] = '/';
|
||
|
for (i = 0; i < 256; i++) {
|
||
|
DIR *d;
|
||
|
|
||
16 years ago
|
display_progress(progress, i + 1);
|
||
20 years ago
|
sprintf(pathname + len, "%02x/", i);
|
||
|
d = opendir(pathname);
|
||
|
if (!d)
|
||
20 years ago
|
continue;
|
||
18 years ago
|
prune_dir(i, d, pathname, len + 3, opts);
|
||
20 years ago
|
closedir(d);
|
||
|
}
|
||
17 years ago
|
stop_progress(&progress);
|
||
20 years ago
|
}
|
||
|
|
||
19 years ago
|
int cmd_prune_packed(int argc, const char **argv, const char *prefix)
|
||
20 years ago
|
{
|
||
15 years ago
|
int opts = isatty(2) ? VERBOSE : 0;
|
||
16 years ago
|
const struct option prune_packed_options[] = {
|
||
|
OPT_BIT('n', "dry-run", &opts, "dry run", DRY_RUN),
|
||
|
OPT_NEGBIT('q', "quiet", &opts, "be quiet", VERBOSE),
|
||
|
OPT_END()
|
||
|
};
|
||
20 years ago
|
|
||
16 years ago
|
argc = parse_options(argc, argv, prefix, prune_packed_options,
|
||
|
prune_packed_usage, 0);
|
||
20 years ago
|
|
||
18 years ago
|
prune_packed_objects(opts);
|
||
20 years ago
|
return 0;
|
||
|
}
|