Skip to content

Commit 9535ce7

Browse files
jeffhostetlergitster
authored andcommitted
pack-objects: add list-objects filtering
Teach pack-objects to use the filtering provided by the traverse_commit_list_filtered() interface to omit unwanted objects from the resulting packfile. Filtering requires the use of the "--stdout" option. Add t5317 test. In the future, we will introduce a "partial clone" mechanism wherein an object in a repo, obtained from a remote, may reference a missing object that can be dynamically fetched from that remote once needed. This "partial clone" mechanism will have a way, sometimes slow, of determining if a missing link is one of the links expected to be produced by this mechanism. This patch introduces handling of missing objects to help debugging and development of the "partial clone" mechanism, and once the mechanism is implemented, for a power user to perform operations that are missing-object aware without incurring the cost of checking if a missing link is expected. Signed-off-by: Jeff Hostetler <[email protected]> Reviewed-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent caf3827 commit 9535ce7

File tree

3 files changed

+456
-2
lines changed

3 files changed

+456
-2
lines changed

Documentation/git-pack-objects.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ SYNOPSIS
1212
'git pack-objects' [-q | --progress | --all-progress] [--all-progress-implied]
1313
[--no-reuse-delta] [--delta-base-offset] [--non-empty]
1414
[--local] [--incremental] [--window=<n>] [--depth=<n>]
15-
[--revs [--unpacked | --all]] [--stdout | base-name]
15+
[--revs [--unpacked | --all]]
16+
[--stdout [--filter=<filter-spec>] | base-name]
1617
[--shallow] [--keep-true-parents] < object-list
1718

1819

@@ -236,6 +237,22 @@ So does `git bundle` (see linkgit:git-bundle[1]) when it creates a bundle.
236237
With this option, parents that are hidden by grafts are packed
237238
nevertheless.
238239

240+
--filter=<filter-spec>::
241+
Requires `--stdout`. Omits certain objects (usually blobs) from
242+
the resulting packfile. See linkgit:git-rev-list[1] for valid
243+
`<filter-spec>` forms.
244+
245+
--missing=<missing-action>::
246+
A debug option to help with future "partial clone" development.
247+
This option specifies how missing objects are handled.
248+
+
249+
The form '--missing=error' requests that pack-objects stop with an error if
250+
a missing object is encountered. This is the default action.
251+
+
252+
The form '--missing=allow-any' will allow object traversal to continue
253+
if a missing object is encountered. Missing objects will silently be
254+
omitted from the results.
255+
239256
SEE ALSO
240257
--------
241258
linkgit:git-rev-list[1]

builtin/pack-objects.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "diff.h"
1616
#include "revision.h"
1717
#include "list-objects.h"
18+
#include "list-objects-filter.h"
19+
#include "list-objects-filter-options.h"
1820
#include "pack-objects.h"
1921
#include "progress.h"
2022
#include "refs.h"
@@ -79,6 +81,15 @@ static unsigned long cache_max_small_delta_size = 1000;
7981

8082
static unsigned long window_memory_limit = 0;
8183

84+
static struct list_objects_filter_options filter_options;
85+
86+
enum missing_action {
87+
MA_ERROR = 0, /* fail if any missing objects are encountered */
88+
MA_ALLOW_ANY, /* silently allow ALL missing objects */
89+
};
90+
static enum missing_action arg_missing_action;
91+
static show_object_fn fn_show_object;
92+
8293
/*
8394
* stats
8495
*/
@@ -2552,6 +2563,42 @@ static void show_object(struct object *obj, const char *name, void *data)
25522563
obj->flags |= OBJECT_ADDED;
25532564
}
25542565

2566+
static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
2567+
{
2568+
assert(arg_missing_action == MA_ALLOW_ANY);
2569+
2570+
/*
2571+
* Quietly ignore ALL missing objects. This avoids problems with
2572+
* staging them now and getting an odd error later.
2573+
*/
2574+
if (!has_object_file(&obj->oid))
2575+
return;
2576+
2577+
show_object(obj, name, data);
2578+
}
2579+
2580+
static int option_parse_missing_action(const struct option *opt,
2581+
const char *arg, int unset)
2582+
{
2583+
assert(arg);
2584+
assert(!unset);
2585+
2586+
if (!strcmp(arg, "error")) {
2587+
arg_missing_action = MA_ERROR;
2588+
fn_show_object = show_object;
2589+
return 0;
2590+
}
2591+
2592+
if (!strcmp(arg, "allow-any")) {
2593+
arg_missing_action = MA_ALLOW_ANY;
2594+
fn_show_object = show_object__ma_allow_any;
2595+
return 0;
2596+
}
2597+
2598+
die(_("invalid value for --missing"));
2599+
return 0;
2600+
}
2601+
25552602
static void show_edge(struct commit *commit)
25562603
{
25572604
add_preferred_base(commit->object.oid.hash);
@@ -2816,7 +2863,12 @@ static void get_object_list(int ac, const char **av)
28162863
if (prepare_revision_walk(&revs))
28172864
die("revision walk setup failed");
28182865
mark_edges_uninteresting(&revs, show_edge);
2819-
traverse_commit_list(&revs, show_commit, show_object, NULL);
2866+
2867+
if (!fn_show_object)
2868+
fn_show_object = show_object;
2869+
traverse_commit_list_filtered(&filter_options, &revs,
2870+
show_commit, fn_show_object, NULL,
2871+
NULL);
28202872

28212873
if (unpack_unreachable_expiration) {
28222874
revs.ignore_missing_links = 1;
@@ -2952,6 +3004,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
29523004
N_("use a bitmap index if available to speed up counting objects")),
29533005
OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index,
29543006
N_("write a bitmap index together with the pack index")),
3007+
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
3008+
{ OPTION_CALLBACK, 0, "missing", NULL, N_("action"),
3009+
N_("handling for missing objects"), PARSE_OPT_NONEG,
3010+
option_parse_missing_action },
29553011
OPT_END(),
29563012
};
29573013

@@ -3028,6 +3084,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
30283084
if (!rev_list_all || !rev_list_reflog || !rev_list_index)
30293085
unpack_unreachable_expiration = 0;
30303086

3087+
if (filter_options.choice) {
3088+
if (!pack_to_stdout)
3089+
die("cannot use --filter without --stdout.");
3090+
use_bitmap_index = 0;
3091+
}
3092+
30313093
/*
30323094
* "soft" reasons not to use bitmaps - for on-disk repack by default we want
30333095
*

0 commit comments

Comments
 (0)