]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/prune-packed.c
Merge branch 'ma/split-symref-update-fix'
[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"
150e3001 5#include "packfile.h"
2396ec85 6
7cfe0c98 7static const char * const prune_packed_usage[] = {
9c9b4f2f 8 N_("git prune-packed [-n | --dry-run] [-q | --quiet]"),
7cfe0c98
SB
9 NULL
10};
51890a64 11
dc6a0757 12static struct progress *progress;
b5d72f0a 13
70c49050 14static int prune_subdir(unsigned int nr, const char *path, void *data)
2396ec85 15{
0d3b7296
JK
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
76c1d9a0 23static int prune_object(const struct object_id *oid, const char *path,
0d3b7296
JK
24 void *data)
25{
26 int *opts = data;
2396ec85 27
76c1d9a0 28 if (!has_sha1_pack(oid->hash))
0d3b7296 29 return 0;
c235d960 30
0d3b7296
JK
31 if (*opts & PRUNE_PACKED_DRY_RUN)
32 printf("rm -f %s\n", path);
33 else
34 unlink_or_warn(path);
35 return 0;
2396ec85
LT
36}
37
b60daf05 38void prune_packed_objects(int opts)
2396ec85 39{
af0b4a3b 40 if (opts & PRUNE_PACKED_VERBOSE)
8aade107 41 progress = start_delayed_progress(_("Removing duplicate objects"), 256);
b5d72f0a 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}