1#include "builtin.h"
2#include "cache.h"
3#include "progress.h"
4#include "parse-options.h"
5
6static const char * const prune_packed_usage[] = {
7 N_("git prune-packed [-n | --dry-run] [-q | --quiet]"),
8 NULL
9};
10
11static struct progress *progress;
12
13static int prune_subdir(unsigned int nr, const char *path, void *data)
14{
15 int *opts = data;
16 display_progress(progress, nr + 1);
17 if (!(*opts & PRUNE_PACKED_DRY_RUN))
18 rmdir(path);
19 return 0;
20}
21
22static int prune_object(const struct object_id *oid, const char *path,
23 void *data)
24{
25 int *opts = data;
26
27 if (!has_sha1_pack(oid->hash))
28 return 0;
29
30 if (*opts & PRUNE_PACKED_DRY_RUN)
31 printf("rm -f %s\n", path);
32 else
33 unlink_or_warn(path);
34 return 0;
35}
36
37void prune_packed_objects(int opts)
38{
39 if (opts & PRUNE_PACKED_VERBOSE)
40 progress = start_delayed_progress(_("Removing duplicate objects"), 256);
41
42 for_each_loose_file_in_objdir(get_object_directory(),
43 prune_object, NULL, prune_subdir, &opts);
44
45 /* Ensure we show 100% before finishing progress */
46 display_progress(progress, 256);
47 stop_progress(&progress);
48}
49
50int cmd_prune_packed(int argc, const char **argv, const char *prefix)
51{
52 int opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;
53 const struct option prune_packed_options[] = {
54 OPT_BIT('n', "dry-run", &opts, N_("dry run"),
55 PRUNE_PACKED_DRY_RUN),
56 OPT_NEGBIT('q', "quiet", &opts, N_("be quiet"),
57 PRUNE_PACKED_VERBOSE),
58 OPT_END()
59 };
60
61 argc = parse_options(argc, argv, prefix, prune_packed_options,
62 prune_packed_usage, 0);
63
64 prune_packed_objects(opts);
65 return 0;
66}