pack-objects: match prune logic for discarding objects
A recent commit taught git-prune to keep non-recent objects that are reachable from recent ones. However, pack-objects, when loosening unreachable objects, tries to optimize out the write in the case that the object will be immediately pruned. It now gets this wrong, since its rule does not reflect the new prune code (and this can be seen by running t6501 with a strategically placed repack). Let's teach pack-objects similar logic. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Oct 15, 2014 at 18:42 UTC
abcb86553d3ec4afffa4e3963089dffe0559740e
4 files changed
+98
-40
builtin/pack-objects.c
+39
@@ -20,6 +20,8 @@
20
#include "streaming.h"
21
#include "thread-utils.h"
22
#include "pack-bitmap.h"
23
+#include "reachable.h"
24
+#include "sha1-array.h"
25
26
static const char *pack_usage[] = {
27
N_("git pack-objects --stdout [options...] [< ref-list | < object-list]"),
@@ -2407,6 +2409,15 @@ static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1)
2409
return 0;
2410
}
2411
2412
+/*
2413
+ * Store a list of sha1s that are should not be discarded
2414
+ * because they are either written too recently, or are
2415
+ * reachable from another object that was.
2416
+ *
2417
+ * This is filled by get_object_list.
2418
+ */
2419
+static struct sha1_array recent_objects;
2420
+
2421
static int loosened_object_can_be_discarded(const unsigned char *sha1,
2422
unsigned long mtime)
2423
{
@@ -2414,6 +2425,8 @@ static int loosened_object_can_be_discarded(const unsigned char *sha1,
2425
return 0;
2426
if (mtime > unpack_unreachable_expiration)
2427
return 0;
2428
+ if (sha1_array_lookup(&recent_objects, sha1) >= 0)
2429
+ return 0;
2430
return 1;
2431
}
2432
@@ -2470,6 +2483,19 @@ static int get_object_list_from_bitmap(struct rev_info *revs)
2483
return 0;
2484
}
2485
2486
+static void record_recent_object(struct object *obj,
2487
+ const struct name_path *path,
2488
+ const char *last,
2489
+ void *data)
2490
+{
2491
+ sha1_array_append(&recent_objects, obj->sha1);
2492
+}
2493
+
2494
+static void record_recent_commit(struct commit *commit, void *data)
2495
+{
2496
+ sha1_array_append(&recent_objects, commit->object.sha1);
2497
+}
2498
+
2499
static void get_object_list(int ac, const char **av)
2500
{
2501
struct rev_info revs;
@@ -2517,10 +2543,23 @@ static void get_object_list(int ac, const char **av)
2543
mark_edges_uninteresting(&revs, show_edge);
2544
traverse_commit_list(&revs, show_commit, show_object, NULL);
2545
2546
+ if (unpack_unreachable_expiration) {
2547
+ revs.ignore_missing_links = 1;
2548
+ if (add_unseen_recent_objects_to_traversal(&revs,
2549
+ unpack_unreachable_expiration))
2550
+ die("unable to add recent objects");
2551
+ if (prepare_revision_walk(&revs))
2552
+ die("revision walk setup failed");
2553
+ traverse_commit_list(&revs, record_recent_commit,
2554
+ record_recent_object, NULL);
2555
+ }
2556
+
2557
if (keep_unreachable)
2558
add_objects_in_unpacked_packs(&revs);
2559
if (unpack_unreachable)
2560
loosen_unused_packed_objects(&revs);
2561
+
2562
+ sha1_array_clear(&recent_objects);
2563
}
2564
2565
static int option_parse_index_version(const struct option *opt,
reachable.c
+2
-2
@@ -183,8 +183,8 @@ static int add_recent_packed(const unsigned char *sha1,
183
return 0;
184
}
185
186
-static int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
187
- unsigned long timestamp)
186
+int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
187
+ unsigned long timestamp)
188
{
189
struct recent_data data;
190
int r;
reachable.h
+2
@@ -2,6 +2,8 @@
2
#define REACHEABLE_H
3
4
struct progress;
5
+extern int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
6
+ unsigned long timestamp);
7
extern void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
8
unsigned long mark_recent, struct progress *);
9
t/t6501-freshen-objects.sh
+55
-38
@@ -39,50 +39,67 @@ commit () {
39
git commit -m "$1"
40
}
41
42
-test_expect_success 'disable reflogs' '
43
- git config core.logallrefupdates false &&
44
- rm -rf .git/logs
45
-'
42
+maybe_repack () {
43
+ if test -n "$repack"; then
44
+ git repack -ad
45
+ fi
46
+}
47
+
48
+for repack in '' true; do
49
+ title=${repack:+repack}
50
+ title=${title:-loose}
51
+
52
+ test_expect_success "make repo completely empty ($title)" '
53
+ rm -rf .git &&
54
+ git init
55
+ '
56
+
57
+ test_expect_success "disable reflogs ($title)" '
58
+ git config core.logallrefupdates false &&
59
+ rm -rf .git/logs
60
+ '
61
47
-test_expect_success 'setup basic history' '
48
- commit base
49
-'
62
+ test_expect_success "setup basic history ($title)" '
63
+ commit base
64
+ '
65
51
-test_expect_success 'create and abandon some objects' '
52
- git checkout -b experiment &&
53
- commit abandon &&
54
- git checkout master &&
55
- git branch -D experiment
56
-'
66
+ test_expect_success "create and abandon some objects ($title)" '
67
+ git checkout -b experiment &&
68
+ commit abandon &&
69
+ maybe_repack &&
70
+ git checkout master &&
71
+ git branch -D experiment
72
+ '
73
58
-test_expect_success 'simulate time passing' '
59
- find .git/objects -type f |
60
- xargs test-chmtime -v -86400
61
-'
74
+ test_expect_success "simulate time passing ($title)" '
75
+ find .git/objects -type f |
76
+ xargs test-chmtime -v -86400
77
+ '
78
63
-test_expect_success 'start writing new commit with old blob' '
64
- tree=$(
65
- GIT_INDEX_FILE=index.tmp &&
66
- export GIT_INDEX_FILE &&
67
- git read-tree HEAD &&
68
- add unrelated &&
69
- add abandon &&
70
- git write-tree
71
- )
72
-'
79
+ test_expect_success "start writing new commit with old blob ($title)" '
80
+ tree=$(
81
+ GIT_INDEX_FILE=index.tmp &&
82
+ export GIT_INDEX_FILE &&
83
+ git read-tree HEAD &&
84
+ add unrelated &&
85
+ add abandon &&
86
+ git write-tree
87
+ )
88
+ '
89
74
-test_expect_success 'simultaneous gc' '
75
- git gc --prune=12.hours.ago
76
-'
90
+ test_expect_success "simultaneous gc ($title)" '
91
+ git gc --prune=12.hours.ago
92
+ '
93
78
-test_expect_success 'finish writing out commit' '
79
- commit=$(echo foo | git commit-tree -p HEAD $tree) &&
80
- git update-ref HEAD $commit
81
-'
94
+ test_expect_success "finish writing out commit ($title)" '
95
+ commit=$(echo foo | git commit-tree -p HEAD $tree) &&
96
+ git update-ref HEAD $commit
97
+ '
98
83
-# "abandon" blob should have been rescued by reference from new tree
84
-test_expect_success 'repository passes fsck' '
85
- git fsck
86
-'
99
+ # "abandon" blob should have been rescued by reference from new tree
100
+ test_expect_success "repository passes fsck ($title)" '
101
+ git fsck
102
+ '
103
+done
104
105
test_done