Skip to content

Commit 7e52f56

Browse files
peffgitster
authored andcommitted
gc: do not explode objects which will be immediately pruned
When we pack everything into one big pack with "git repack -Ad", any unreferenced objects in to-be-deleted packs are exploded into loose objects, with the intent that they will be examined and possibly cleaned up by the next run of "git prune". Since the exploded objects will receive the mtime of the pack from which they come, if the source pack is old, those loose objects will end up pruned immediately. In that case, it is much more efficient to skip the exploding step entirely for these objects. This patch teaches pack-objects to receive the expiration information and avoid writing these objects out. It also teaches "git gc" to pass the value of gc.pruneexpire to repack (which in turn learns to pass it along to pack-objects) so that this optimization happens automatically during "git gc" and "git gc --auto". Signed-off-by: Jeff King <[email protected]> Acked-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e8dde3e commit 7e52f56

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

builtin/gc.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ static int too_many_packs(void)
144144
return gc_auto_pack_limit <= cnt;
145145
}
146146

147+
static void add_repack_all_option(void)
148+
{
149+
if (prune_expire && !strcmp(prune_expire, "now"))
150+
append_option(argv_repack, "-a", MAX_ADD);
151+
else {
152+
append_option(argv_repack, "-A", MAX_ADD);
153+
if (prune_expire) {
154+
append_option(argv_repack, "--unpack-unreachable", MAX_ADD);
155+
append_option(argv_repack, prune_expire, MAX_ADD);
156+
}
157+
}
158+
}
159+
147160
static int need_to_gc(void)
148161
{
149162
/*
@@ -160,10 +173,7 @@ static int need_to_gc(void)
160173
* there is no need.
161174
*/
162175
if (too_many_packs())
163-
append_option(argv_repack,
164-
prune_expire && !strcmp(prune_expire, "now") ?
165-
"-a" : "-A",
166-
MAX_ADD);
176+
add_repack_all_option();
167177
else if (!too_many_loose_objects())
168178
return 0;
169179

@@ -227,10 +237,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
227237
"run \"git gc\" manually. See "
228238
"\"git help gc\" for more information.\n"));
229239
} else
230-
append_option(argv_repack,
231-
prune_expire && !strcmp(prune_expire, "now")
232-
? "-a" : "-A",
233-
MAX_ADD);
240+
add_repack_all_option();
234241

235242
if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
236243
return error(FAILED_RUN, argv_pack_refs[0]);

builtin/pack-objects.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static uint32_t nr_objects, nr_alloc, nr_result, nr_written;
6363
static int non_empty;
6464
static int reuse_delta = 1, reuse_object = 1;
6565
static int keep_unreachable, unpack_unreachable, include_tag;
66+
static unsigned long unpack_unreachable_expiration;
6667
static int local;
6768
static int incremental;
6869
static int ignore_packed_keep;
@@ -2249,6 +2250,10 @@ static void loosen_unused_packed_objects(struct rev_info *revs)
22492250
if (!p->pack_local || p->pack_keep)
22502251
continue;
22512252

2253+
if (unpack_unreachable_expiration &&
2254+
p->mtime < unpack_unreachable_expiration)
2255+
continue;
2256+
22522257
if (open_pack_index(p))
22532258
die("cannot open pack index");
22542259

@@ -2315,6 +2320,21 @@ static int option_parse_index_version(const struct option *opt,
23152320
return 0;
23162321
}
23172322

2323+
static int option_parse_unpack_unreachable(const struct option *opt,
2324+
const char *arg, int unset)
2325+
{
2326+
if (unset) {
2327+
unpack_unreachable = 0;
2328+
unpack_unreachable_expiration = 0;
2329+
}
2330+
else {
2331+
unpack_unreachable = 1;
2332+
if (arg)
2333+
unpack_unreachable_expiration = approxidate(arg);
2334+
}
2335+
return 0;
2336+
}
2337+
23182338
static int option_parse_ulong(const struct option *opt,
23192339
const char *arg, int unset)
23202340
{
@@ -2392,8 +2412,9 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
23922412
"include tag objects that refer to objects to be packed"),
23932413
OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
23942414
"keep unreachable objects"),
2395-
OPT_BOOL(0, "unpack-unreachable", &unpack_unreachable,
2396-
"unpack unreachable objects"),
2415+
{ OPTION_CALLBACK, 0, "unpack-unreachable", NULL, "time",
2416+
"unpack unreachable objects newer than <time>",
2417+
PARSE_OPT_OPTARG, option_parse_unpack_unreachable },
23972418
OPT_BOOL(0, "thin", &thin,
23982419
"create thin packs"),
23992420
OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep,

git-repack.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ F pass --no-reuse-object to git-pack-objects
1515
n do not run git-update-server-info
1616
q,quiet be quiet
1717
l pass --local to git-pack-objects
18+
unpack-unreachable= with -A, do not loosen objects older than this
1819
Packing constraints
1920
window= size of the window used for delta compression
2021
window-memory= same as the above, but limit memory size instead of entries count
@@ -33,6 +34,8 @@ do
3334
-a) all_into_one=t ;;
3435
-A) all_into_one=t
3536
unpack_unreachable=--unpack-unreachable ;;
37+
--unpack-unreachable)
38+
unpack_unreachable="--unpack-unreachable=$2"; shift ;;
3639
-d) remove_redundant=t ;;
3740
-q) GIT_QUIET=t ;;
3841
-f) no_reuse=--no-reuse-delta ;;
@@ -76,7 +79,12 @@ case ",$all_into_one," in
7679
if test -n "$existing" -a -n "$unpack_unreachable" -a \
7780
-n "$remove_redundant"
7881
then
79-
args="$args $unpack_unreachable"
82+
# This may have arbitrary user arguments, so we
83+
# have to protect it against whitespace splitting
84+
# when it gets run as "pack-objects $args" later.
85+
# Fortunately, we know it's an approxidate, so we
86+
# can just use dots instead.
87+
args="$args $(echo "$unpack_unreachable" | tr ' ' .)"
8088
fi
8189
fi
8290
;;

t/t7701-repack-unpack-unreachable.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,18 @@ test_expect_success 'unpacked objects receive timestamp of pack file' '
9595
compare_mtimes < mtimes
9696
'
9797

98+
test_expect_success 'do not bother loosening old objects' '
99+
obj1=$(echo one | git hash-object -w --stdin) &&
100+
obj2=$(echo two | git hash-object -w --stdin) &&
101+
pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) &&
102+
pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) &&
103+
git prune-packed &&
104+
git cat-file -p $obj1 &&
105+
git cat-file -p $obj2 &&
106+
test-chmtime =-86400 .git/objects/pack/pack-$pack2.pack &&
107+
git repack -A -d --unpack-unreachable=1.hour.ago &&
108+
git cat-file -p $obj1 &&
109+
test_must_fail git cat-file -p $obj2
110+
'
111+
98112
test_done

0 commit comments

Comments
 (0)