@matheusbsbs / matheus-bagliano / commits / 9460fd48b5
Fork 0

Lib-ify prune-packed

In builtin.h, there exists the distinctly lib-ish function prune_packed_objects(). This function can currently only be called by built-in commands but, unlike all of the other functions in the header, it does not make sense to impose this restriction as the functionality can be logically reused in libgit. Extract this function into prune-packed.c so that related definitions can exist clearly in their own header file. While we're at it, clean up #includes that are unused. This patch is best viewed with --color-moved. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Mar 23, 2020 at 21:07 UTC 9460fd48b5f3a9e37f692d7637328d7fe18962ea
7 files changed +56 -48
Makefile
+1
@@ -952,6 +952,7 @@ LIB_OBJS += progress.o
952 LIB_OBJS += promisor-remote.o
953 LIB_OBJS += prompt.o
954 LIB_OBJS += protocol.o
955 +LIB_OBJS += prune-packed.o
956 LIB_OBJS += quote.o
957 LIB_OBJS += range-diff.o
958 LIB_OBJS += reachable.o
builtin.h
-5
@@ -97,11 +97,6 @@
97 extern const char git_usage_string[];
98 extern const char git_more_info_string[];
99
100 -#define PRUNE_PACKED_DRY_RUN 01
101 -#define PRUNE_PACKED_VERBOSE 02
102 -
103 -void prune_packed_objects(int);
104 -
100 /**
101 * If a built-in has DELAY_PAGER_CONFIG set, the built-in should call this early
102 * when it wishes to respect the `pager.foo`-config. The `cmd` is the name of
builtin/prune-packed.c
+1 -43
@@ -1,54 +1,12 @@
1 #include "builtin.h"
2 -#include "cache.h"
3 -#include "progress.h"
2 #include "parse-options.h"
5 -#include "packfile.h"
6 -#include "object-store.h"
3 +#include "prune-packed.h"
4
5 static const char * const prune_packed_usage[] = {
6 N_("git prune-packed [-n | --dry-run] [-q | --quiet]"),
7 NULL
8 };
9
13 -static struct progress *progress;
14 -
15 -static int prune_subdir(unsigned int nr, const char *path, void *data)
16 -{
17 - int *opts = data;
18 - display_progress(progress, nr + 1);
19 - if (!(*opts & PRUNE_PACKED_DRY_RUN))
20 - rmdir(path);
21 - return 0;
22 -}
23 -
24 -static int prune_object(const struct object_id *oid, const char *path,
25 - void *data)
26 -{
27 - int *opts = data;
28 -
29 - if (!has_object_pack(oid))
30 - return 0;
31 -
32 - if (*opts & PRUNE_PACKED_DRY_RUN)
33 - printf("rm -f %s\n", path);
34 - else
35 - unlink_or_warn(path);
36 - return 0;
37 -}
38 -
39 -void prune_packed_objects(int opts)
40 -{
41 - if (opts & PRUNE_PACKED_VERBOSE)
42 - progress = start_delayed_progress(_("Removing duplicate objects"), 256);
43 -
44 - for_each_loose_file_in_objdir(get_object_directory(),
45 - prune_object, NULL, prune_subdir, &opts);
46 -
47 - /* Ensure we show 100% before finishing progress */
48 - display_progress(progress, 256);
49 - stop_progress(&progress);
50 -}
51 -
10 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
11 {
12 int opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;
builtin/prune.c
+1
@@ -6,6 +6,7 @@
6 #include "reachable.h"
7 #include "parse-options.h"
8 #include "progress.h"
9 +#include "prune-packed.h"
10 #include "object-store.h"
11
12 static const char * const prune_usage[] = {
builtin/repack.c
+1
@@ -10,6 +10,7 @@
10 #include "argv-array.h"
11 #include "midx.h"
12 #include "packfile.h"
13 +#include "prune-packed.h"
14 #include "object-store.h"
15 #include "promisor-remote.h"
16
prune-packed.c new
+43
@@ -0,0 +1,43 @@
1 +#include "object-store.h"
2 +#include "packfile.h"
3 +#include "progress.h"
4 +#include "prune-packed.h"
5 +
6 +static struct progress *progress;
7 +
8 +static int prune_subdir(unsigned int nr, const char *path, void *data)
9 +{
10 + int *opts = data;
11 + display_progress(progress, nr + 1);
12 + if (!(*opts & PRUNE_PACKED_DRY_RUN))
13 + rmdir(path);
14 + return 0;
15 +}
16 +
17 +static int prune_object(const struct object_id *oid, const char *path,
18 + void *data)
19 +{
20 + int *opts = data;
21 +
22 + if (!has_object_pack(oid))
23 + return 0;
24 +
25 + if (*opts & PRUNE_PACKED_DRY_RUN)
26 + printf("rm -f %s\n", path);
27 + else
28 + unlink_or_warn(path);
29 + return 0;
30 +}
31 +
32 +void prune_packed_objects(int opts)
33 +{
34 + if (opts & PRUNE_PACKED_VERBOSE)
35 + progress = start_delayed_progress(_("Removing duplicate objects"), 256);
36 +
37 + for_each_loose_file_in_objdir(get_object_directory(),
38 + prune_object, NULL, prune_subdir, &opts);
39 +
40 + /* Ensure we show 100% before finishing progress */
41 + display_progress(progress, 256);
42 + stop_progress(&progress);
43 +}
prune-packed.h new
+9
@@ -0,0 +1,9 @@
1 +#ifndef PRUNE_PACKED_H
2 +#define PRUNE_PACKED_H
3 +
4 +#define PRUNE_PACKED_DRY_RUN 01
5 +#define PRUNE_PACKED_VERBOSE 02
6 +
7 +void prune_packed_objects(int);
8 +
9 +#endif