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