]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/prune-packed.c
sha1_file: let for_each_file_in_obj_subdir() handle subdir names
[thirdparty/git.git] / builtin / prune-packed.c
CommitLineData
25f38f06 1#include "builtin.h"
2396ec85 2#include "cache.h"
b5d72f0a 3#include "progress.h"
7cfe0c98 4#include "parse-options.h"
2396ec85 5
7cfe0c98 6static const char * const prune_packed_usage[] = {
9c9b4f2f 7 N_("git prune-packed [-n | --dry-run] [-q | --quiet]"),
7cfe0c98
SB
8 NULL
9};
51890a64 10
dc6a0757 11static struct progress *progress;
b5d72f0a 12
0d3b7296 13static int prune_subdir(int nr, const char *path, void *data)
2396ec85 14{
0d3b7296
JK
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
76c1d9a0 22static int prune_object(const struct object_id *oid, const char *path,
0d3b7296
JK
23 void *data)
24{
25 int *opts = data;
2396ec85 26
76c1d9a0 27 if (!has_sha1_pack(oid->hash))
0d3b7296 28 return 0;
c235d960 29
0d3b7296
JK
30 if (*opts & PRUNE_PACKED_DRY_RUN)
31 printf("rm -f %s\n", path);
32 else
33 unlink_or_warn(path);
34 return 0;
2396ec85
LT
35}
36
b60daf05 37void prune_packed_objects(int opts)
2396ec85 38{
af0b4a3b 39 if (opts & PRUNE_PACKED_VERBOSE)
754dbc43 40 progress = start_progress_delay(_("Removing duplicate objects"),
b5d72f0a
SP
41 256, 95, 2);
42
0d3b7296
JK
43 for_each_loose_file_in_objdir(get_object_directory(),
44 prune_object, NULL, prune_subdir, &opts);
2396ec85 45
0d3b7296
JK
46 /* Ensure we show 100% before finishing progress */
47 display_progress(progress, 256);
4d4fcc54 48 stop_progress(&progress);
2396ec85
LT
49}
50
25f38f06 51int cmd_prune_packed(int argc, const char **argv, const char *prefix)
2396ec85 52{
af0b4a3b 53 int opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;
7cfe0c98 54 const struct option prune_packed_options[] = {
af0b4a3b
NTND
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),
7cfe0c98
SB
59 OPT_END()
60 };
2396ec85 61
7cfe0c98
SB
62 argc = parse_options(argc, argv, prefix, prune_packed_options,
63 prune_packed_usage, 0);
2396ec85 64
b60daf05 65 prune_packed_objects(opts);
2396ec85
LT
66 return 0;
67}