Skip to content

Commit 566842f

Browse files
spearceJunio C Hamano
authored andcommitted
Fix lost-found to show commits only referenced by reflogs
Prior to 1.5.0 the git-lost-found utility was useful to locate commits that were not referenced by any ref. These were often amends, or resets, or tips of branches that had been deleted. Being able to locate a 'lost' commit and recover it by creating a new branch was a useful feature in those days. Unfortunately 1.5.0 added the reflogs to the reachability analysis performed by git-fsck, which means that most commits users would consider to be lost are still reachable through a reflog. So most (or all!) commits are reachable, and nothing gets output from git-lost-found. Now git-fsck can be told to ignore reflogs during its reachability analysis, making git-lost-found useful again to locate commits that are no longer referenced by a ref itself, but may still be referenced by a reflog. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent d72308e commit 566842f

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Documentation/git-fsck.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-fsck - Verifies the connectivity and validity of the objects in the database
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git-fsck' [--tags] [--root] [--unreachable] [--cache]
12+
'git-fsck' [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]
1313
[--full] [--strict] [<object>*]
1414

1515
DESCRIPTION
@@ -38,6 +38,12 @@ index file and all SHA1 references in .git/refs/* as heads.
3838
Consider any object recorded in the index also as a head node for
3939
an unreachability trace.
4040

41+
--no-reflogs::
42+
Do not consider commits that are referenced only by an
43+
entry in a reflog to be reachable. This option is meant
44+
only to search for commits that used to be in a ref, but
45+
now aren't, but are still in that corresponding reflog.
46+
4147
--full::
4248
Check not just objects in GIT_OBJECT_DIRECTORY
4349
($GIT_DIR/objects), but also the ones found in alternate

builtin-fsck.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
static int show_root;
1515
static int show_tags;
1616
static int show_unreachable;
17+
static int include_reflogs = 1;
1718
static int check_full;
1819
static int check_strict;
1920
static int keep_cache_objects;
@@ -517,7 +518,8 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f
517518
static void get_default_heads(void)
518519
{
519520
for_each_ref(fsck_handle_ref, NULL);
520-
for_each_reflog(fsck_handle_reflog, NULL);
521+
if (include_reflogs)
522+
for_each_reflog(fsck_handle_reflog, NULL);
521523

522524
/*
523525
* Not having any default heads isn't really fatal, but
@@ -616,6 +618,10 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
616618
keep_cache_objects = 1;
617619
continue;
618620
}
621+
if (!strcmp(arg, "--no-reflogs")) {
622+
include_reflogs = 0;
623+
continue;
624+
}
619625
if (!strcmp(arg, "--full")) {
620626
check_full = 1;
621627
continue;

git-lost-found.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fi
1212
laf="$GIT_DIR/lost-found"
1313
rm -fr "$laf" && mkdir -p "$laf/commit" "$laf/other" || exit
1414

15-
git fsck --full |
15+
git fsck --full --no-reflogs |
1616
while read dangling type sha1
1717
do
1818
case "$dangling" in

0 commit comments

Comments
 (0)