Skip to content

Commit ea32776

Browse files
committed
Merge branch 'jk/fsck-doc'
"git fsck --connectivity-only" omits computation necessary to sift the objects that are not reachable from any of the refs into unreachable and dangling. This is now enabled when dangling objects are requested (which is done by default, but can be overridden with the "--no-dangling" option). * jk/fsck-doc: fsck: always compute USED flags for unreachable objects doc/fsck: clarify --connectivity-only behavior
2 parents 88255bb + 8d8c2a5 commit ea32776

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

Documentation/git-fsck.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,17 @@ index file, all SHA-1 references in `refs` namespace, and all reflogs
6262
with --no-full.
6363

6464
--connectivity-only::
65-
Check only the connectivity of tags, commits and tree objects. By
66-
avoiding to unpack blobs, this speeds up the operation, at the
67-
expense of missing corrupt objects or other problematic issues.
65+
Check only the connectivity of reachable objects, making sure
66+
that any objects referenced by a reachable tag, commit, or tree
67+
is present. This speeds up the operation by avoiding reading
68+
blobs entirely (though it does still check that referenced blobs
69+
exist). This will detect corruption in commits and trees, but
70+
not do any semantic checks (e.g., for format errors). Corruption
71+
in blob objects will not be detected at all.
72+
+
73+
Unreachable tags, commits, and trees will also be accessed to find the
74+
tips of dangling segments of history. Use `--no-dangling` if you don't
75+
care about this output and want to speed it up further.
6876

6977
--strict::
7078
Enable more strict checking, namely to catch a file mode

builtin/fsck.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,48 @@ static int mark_used(struct object *obj, int type, void *data, struct fsck_optio
235235
return 0;
236236
}
237237

238+
static void mark_unreachable_referents(const struct object_id *oid)
239+
{
240+
struct fsck_options options = FSCK_OPTIONS_DEFAULT;
241+
struct object *obj = lookup_object(the_repository, oid->hash);
242+
243+
if (!obj || !(obj->flags & HAS_OBJ))
244+
return; /* not part of our original set */
245+
if (obj->flags & REACHABLE)
246+
return; /* reachable objects already traversed */
247+
248+
/*
249+
* Avoid passing OBJ_NONE to fsck_walk, which will parse the object
250+
* (and we want to avoid parsing blobs).
251+
*/
252+
if (obj->type == OBJ_NONE) {
253+
enum object_type type = oid_object_info(the_repository,
254+
&obj->oid, NULL);
255+
if (type > 0)
256+
object_as_type(the_repository, obj, type, 0);
257+
}
258+
259+
options.walk = mark_used;
260+
fsck_walk(obj, NULL, &options);
261+
}
262+
263+
static int mark_loose_unreachable_referents(const struct object_id *oid,
264+
const char *path,
265+
void *data)
266+
{
267+
mark_unreachable_referents(oid);
268+
return 0;
269+
}
270+
271+
static int mark_packed_unreachable_referents(const struct object_id *oid,
272+
struct packed_git *pack,
273+
uint32_t pos,
274+
void *data)
275+
{
276+
mark_unreachable_referents(oid);
277+
return 0;
278+
}
279+
238280
/*
239281
* Check a single reachable object
240282
*/
@@ -347,6 +389,26 @@ static void check_connectivity(void)
347389
/* Traverse the pending reachable objects */
348390
traverse_reachable();
349391

392+
/*
393+
* With --connectivity-only, we won't have actually opened and marked
394+
* unreachable objects with USED. Do that now to make --dangling, etc
395+
* accurate.
396+
*/
397+
if (connectivity_only && (show_dangling || write_lost_and_found)) {
398+
/*
399+
* Even though we already have a "struct object" for each of
400+
* these in memory, we must not iterate over the internal
401+
* object hash as we do below. Our loop would potentially
402+
* resize the hash, making our iteration invalid.
403+
*
404+
* Instead, we'll just go back to the source list of objects,
405+
* and ignore any that weren't present in our earlier
406+
* traversal.
407+
*/
408+
for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
409+
for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
410+
}
411+
350412
/* Look up all the requirements, warn about missing objects.. */
351413
max = get_max_object_index();
352414
if (verbose)

t/t1450-fsck.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ test_expect_success 'fsck detects truncated loose object' '
740740
# for each of type, we have one version which is referenced by another object
741741
# (and so while unreachable, not dangling), and another variant which really is
742742
# dangling.
743-
test_expect_success 'fsck notices dangling objects' '
743+
test_expect_success 'create dangling-object repository' '
744744
git init dangling &&
745745
(
746746
cd dangling &&
@@ -751,19 +751,34 @@ test_expect_success 'fsck notices dangling objects' '
751751
commit=$(git commit-tree $tree) &&
752752
dcommit=$(git commit-tree -p $commit $tree) &&
753753
754-
cat >expect <<-EOF &&
754+
cat >expect <<-EOF
755755
dangling blob $dblob
756756
dangling commit $dcommit
757757
dangling tree $dtree
758758
EOF
759+
)
760+
'
759761

762+
test_expect_success 'fsck notices dangling objects' '
763+
(
764+
cd dangling &&
760765
git fsck >actual &&
761766
# the output order is non-deterministic, as it comes from a hash
762767
sort <actual >actual.sorted &&
763768
test_i18ncmp expect actual.sorted
764769
)
765770
'
766771

772+
test_expect_success 'fsck --connectivity-only notices dangling objects' '
773+
(
774+
cd dangling &&
775+
git fsck --connectivity-only >actual &&
776+
# the output order is non-deterministic, as it comes from a hash
777+
sort <actual >actual.sorted &&
778+
test_i18ncmp expect actual.sorted
779+
)
780+
'
781+
767782
test_expect_success 'fsck $name notices bogus $name' '
768783
test_must_fail git fsck bogus &&
769784
test_must_fail git fsck $ZERO_OID

0 commit comments

Comments
 (0)