1#include "builtin.h"
2#include "cache.h"
3#include "progress.h"
4#include "parse-options.h"
5#include "packfile.h"
6#include "object-store.h"
78
static const char * const prune_packed_usage[] = {
9N_("git prune-packed [-n | --dry-run] [-q | --quiet]"),
10NULL
11};
1213
static struct progress *progress;
1415
static int prune_subdir(unsigned int nr, const char *path, void *data)
16{
17int *opts = data;
18display_progress(progress, nr + 1);
19if (!(*opts & PRUNE_PACKED_DRY_RUN))
20rmdir(path);
21return 0;
22}
2324
static int prune_object(const struct object_id *oid, const char *path,
25void *data)
26{
27int *opts = data;
2829
if (!has_object_pack(oid))
30return 0;
3132
if (*opts & PRUNE_PACKED_DRY_RUN)
33printf("rm -f %s\n", path);
34else
35unlink_or_warn(path);
36return 0;
37}
3839
void prune_packed_objects(int opts)
40{
41if (opts & PRUNE_PACKED_VERBOSE)
42progress = start_delayed_progress(_("Removing duplicate objects"), 256);
4344
for_each_loose_file_in_objdir(get_object_directory(),
45prune_object, NULL, prune_subdir, &opts);
4647
/* Ensure we show 100% before finishing progress */
48display_progress(progress, 256);
49stop_progress(&progress);
50}
5152
int cmd_prune_packed(int argc, const char **argv, const char *prefix)
53{
54int opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;
55const struct option prune_packed_options[] = {
56OPT_BIT('n', "dry-run", &opts, N_("dry run"),
57PRUNE_PACKED_DRY_RUN),
58OPT_NEGBIT('q', "quiet", &opts, N_("be quiet"),
59PRUNE_PACKED_VERBOSE),
60OPT_END()
61};
6263
argc = parse_options(argc, argv, prefix, prune_packed_options,
64prune_packed_usage, 0);
6566
prune_packed_objects(opts);
67return 0;
68}