# [PATCH] fsck --lost-found: show found commits human readably

4 messages from 2008-03-26 to 2008-03-28. Participants: Junio C Hamano, Johannes Schindelin.
Thread: https://gitlist.dev/t/12872

## Junio C Hamano, 2008-03-26 06:45

Subject: [PATCH] fsck --lost-found: show found commits human readably
Message-ID: <7vbq52gezo.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vbq52gezo.fsf%40gitster.siamese.dyndns.org

```
An earlier commit fc8b5f0 (Deprecate git-lost-found, 2007-11-08) declared
"lost-found" deprecated, because "fsck" learned "--lost-found" option that
drops the found objects in $GIT_DIR/lost-found.

But the output from the lost-found program has been much more informative
than the plain vanilla "git fsck" (or "git fsck --lost-found") output.  In
that sense, forcing users to use "fsck --lost-found" when they want to use
"lost-found" is a regression.

This patch slightly enhances the output from "fsck --lost-found" to add
oneline description at the end of the usual "dangling <type> <sha-1>"
message for commit objects it found.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---

 * Is it just me, or were we very sloppy during 1.5.4 cycle that we are
   getting hit by regressions left and right today from that period?

   This looks bigger than it really is because most of it is about
   moving a deeply nested part of another function into a separate
   function.

   This is _not_ a 1.5.5 material, as we are not removing git-lost-found
   yet.  We might however want to revert fc8b5f0, though, until an
   enhancement along this line is in the mainline.

 builtin-fsck.c |   94 ++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 64 insertions(+), 30 deletions(-)

diff --git a/builtin-fsck.c b/builtin-fsck.c
index 78a6e1f..b57cc78 100644
--- a/builtin-fsck.c
+++ b/builtin-fsck.c
@@ -145,6 +145,68 @@ static void check_reachable_object(struct object *obj)
 	}
 }
 
+static void dangling_object(struct object *obj)
+{
+	char *filename;
+	FILE *f;
+	enum object_type type;
+	unsigned long size;
+	char *buf = NULL;
+
+	if (!write_lost_and_found)
+		goto report_and_exit;
+
+	filename = git_path("lost-found/%s/%s",
+			    obj->type == OBJ_COMMIT ? "commit" : "other",
+			    sha1_to_hex(obj->sha1));
+
+	if (safe_create_leading_directories(filename)) {
+		error("Could not create lost-found");
+		return;
+	}
+	if (!(f = fopen(filename, "w")))
+		die("Could not open %s", filename);
+	if (obj->type == OBJ_BLOB || obj->type == OBJ_COMMIT)
+		buf = read_sha1_file(obj->sha1, &type, &size);
+
+	if (obj->type == OBJ_BLOB) {
+		if (buf) {
+			fwrite(buf, size, 1, f);
+			free(buf);
+		}
+	} else
+		fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
+	fclose(f);
+
+	if (obj->type == OBJ_COMMIT) {
+		struct strbuf sb = STRBUF_INIT;
+		struct commit *commit = lookup_commit(obj->sha1);
+		int reported = 0;
+
+		if (!commit->buffer)
+			commit->buffer = buf;
+		if (commit->buffer) {
+			parse_commit(commit);
+			pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb,
+					    0, NULL, NULL, 0, 0);
+			printf("dangling commit %s (%s)\n",
+			       sha1_to_hex(obj->sha1), sb.buf);
+			reported = 1;
+		}
+		strbuf_release(&sb);
+		free(commit->buffer);
+		if (buf && commit->buffer != buf)
+			free(buf);
+		commit->buffer = NULL;
+		if (reported)
+			return;
+	}
+
+ report_and_exit:
+	printf("dangling %s %s\n", typename(obj->type),
+	       sha1_to_hex(obj->sha1));
+}
+
 /*
  * Check a single unreachable object
  */
@@ -180,36 +242,8 @@ static void check_unreachable_object(struct object *obj)
 	 * deleted a branch by mistake, this is a prime candidate to
 	 * start looking at, for example.
 	 */
-	if (!obj->used) {
-		printf("dangling %s %s\n", typename(obj->type),
-		       sha1_to_hex(obj->sha1));
-		if (write_lost_and_found) {
-			char *filename = git_path("lost-found/%s/%s",
-				obj->type == OBJ_COMMIT ? "commit" : "other",
-				sha1_to_hex(obj->sha1));
-			FILE *f;
-
-			if (safe_create_leading_directories(filename)) {
-				error("Could not create lost-found");
-				return;
-			}
-			if (!(f = fopen(filename, "w")))
-				die("Could not open %s", filename);
-			if (obj->type == OBJ_BLOB) {
-				enum object_type type;
-				unsigned long size;
-				char *buf = read_sha1_file(obj->sha1,
-						&type, &size);
-				if (buf) {
-					fwrite(buf, size, 1, f);
-					free(buf);
-				}
-			} else
-				fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
-			fclose(f);
-		}
-		return;
-	}
+	if (!obj->used)
+		dangling_object(obj);
 
 	/*
 	 * Otherwise? It's there, it's unreachable, and some other unreachable

```

## Johannes Schindelin, 2008-03-28 14:05

Subject: Re: [PATCH] fsck --lost-found: show found commits human readably
Message-ID: <alpine.LSU.1.00.0803281504591.18259@racer.site>
URL: https://gitlist.dev/e/alpine.LSU.1.00.0803281504591.18259%40racer.site
In-Reply-To: <7vbq52gezo.fsf@gitster.siamese.dyndns.org>

```
Hi,

I split your patch into two logical entities.  Will send as a reply.

Ciao,
Dscho

```

## Johannes Schindelin, 2008-03-28 14:05

Subject: [PATCH 1/2] fsck --lost-found: refactor handling of dangling objects
Message-ID: <alpine.LSU.1.00.0803281505330.18259@racer.site>
URL: https://gitlist.dev/e/alpine.LSU.1.00.0803281505330.18259%40racer.site
In-Reply-To: <alpine.LSU.1.00.0803281504591.18259@racer.site>

```

From: Junio C Hamano <gitster@pobox.com>

This moves a deeply nested part of the function
check_unreachable_object() into a separate function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin-fsck.c |   65 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/builtin-fsck.c b/builtin-fsck.c
index 78a6e1f..f01263a 100644
--- a/builtin-fsck.c
+++ b/builtin-fsck.c
@@ -145,6 +145,39 @@ static void check_reachable_object(struct object *obj)
 	}
 }
 
+static void dangling_object(struct object *obj)
+{
+	char *filename;
+	FILE *f;
+	printf("dangling %s %s\n", typename(obj->type),
+			sha1_to_hex(obj->sha1));
+	if (!write_lost_and_found)
+		return;
+
+	filename = git_path("lost-found/%s/%s",
+			    obj->type == OBJ_COMMIT ? "commit" : "other",
+			    sha1_to_hex(obj->sha1));
+
+	if (safe_create_leading_directories(filename)) {
+		error("Could not create lost-found");
+		return;
+	}
+	if (!(f = fopen(filename, "w")))
+		die("Could not open %s", filename);
+	if (obj->type == OBJ_BLOB) {
+		enum object_type type;
+		unsigned long size;
+		char *buf = read_sha1_file(obj->sha1,
+				&type, &size);
+		if (buf) {
+			fwrite(buf, size, 1, f);
+			free(buf);
+		}
+	} else
+		fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
+	fclose(f);
+}
+
 /*
  * Check a single unreachable object
  */
@@ -180,36 +213,8 @@ static void check_unreachable_object(struct object *obj)
 	 * deleted a branch by mistake, this is a prime candidate to
 	 * start looking at, for example.
 	 */
-	if (!obj->used) {
-		printf("dangling %s %s\n", typename(obj->type),
-		       sha1_to_hex(obj->sha1));
-		if (write_lost_and_found) {
-			char *filename = git_path("lost-found/%s/%s",
-				obj->type == OBJ_COMMIT ? "commit" : "other",
-				sha1_to_hex(obj->sha1));
-			FILE *f;
-
-			if (safe_create_leading_directories(filename)) {
-				error("Could not create lost-found");
-				return;
-			}
-			if (!(f = fopen(filename, "w")))
-				die("Could not open %s", filename);
-			if (obj->type == OBJ_BLOB) {
-				enum object_type type;
-				unsigned long size;
-				char *buf = read_sha1_file(obj->sha1,
-						&type, &size);
-				if (buf) {
-					fwrite(buf, size, 1, f);
-					free(buf);
-				}
-			} else
-				fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
-			fclose(f);
-		}
-		return;
-	}
+	if (!obj->used)
+		dangling_object(obj);
 
 	/*
 	 * Otherwise? It's there, it's unreachable, and some other unreachable
-- 
1.5.5.rc2.186.gbac51

```

## Johannes Schindelin, 2008-03-28 14:08

Subject: [PATCH 2/2] fsck --lost-found: show found commits human readably
Message-ID: <alpine.LSU.1.00.0803281505510.18259@racer.site>
URL: https://gitlist.dev/e/alpine.LSU.1.00.0803281505510.18259%40racer.site
In-Reply-To: <alpine.LSU.1.00.0803281504591.18259@racer.site>

```
From: Junio C Hamano <gitster@pobox.com>

An earlier commit fc8b5f0 (Deprecate git-lost-found, 2007-11-08) declared
"lost-found" deprecated, because "fsck" learned "--lost-found" option that
drops the found objects in $GIT_DIR/lost-found.

But the output from the lost-found program has been much more informative
than the plain vanilla "git fsck" (or "git fsck --lost-found") output.  In
that sense, forcing users to use "fsck --lost-found" when they want to use
"lost-found" is a regression.

This patch slightly enhances the output from "fsck --lost-found" to add
oneline description at the end of the usual "dangling <type> <sha-1>"
message for commit objects it found.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---

	As for the deprecation of lost-found, I think it is correct, and 
	should not be reverted.  We _do_ have way too many commands, and 
	lost-found logically _belongs_ into fsck.  The proper fix is to 
	fix fsck --lost-found.

	Now, I did not yet look closely at the output of 
	git-lost-found.sh, as I already switched to "master" in the hope 
	that 1.5.5-rc2 will be almost identical to 1.5.5, and I fully 
	intend to push for a quick release (after the pending "fetch" 
	resolutions).

	After 1.5.5, I will tend to lost-found.

 builtin-fsck.c |   43 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/builtin-fsck.c b/builtin-fsck.c
index f01263a..b57cc78 100644
--- a/builtin-fsck.c
+++ b/builtin-fsck.c
@@ -149,10 +149,12 @@ static void dangling_object(struct object *obj)
 {
 	char *filename;
 	FILE *f;
-	printf("dangling %s %s\n", typename(obj->type),
-			sha1_to_hex(obj->sha1));
+	enum object_type type;
+	unsigned long size;
+	char *buf = NULL;
+
 	if (!write_lost_and_found)
-		return;
+		goto report_and_exit;
 
 	filename = git_path("lost-found/%s/%s",
 			    obj->type == OBJ_COMMIT ? "commit" : "other",
@@ -164,11 +166,10 @@ static void dangling_object(struct object *obj)
 	}
 	if (!(f = fopen(filename, "w")))
 		die("Could not open %s", filename);
+	if (obj->type == OBJ_BLOB || obj->type == OBJ_COMMIT)
+		buf = read_sha1_file(obj->sha1, &type, &size);
+
 	if (obj->type == OBJ_BLOB) {
-		enum object_type type;
-		unsigned long size;
-		char *buf = read_sha1_file(obj->sha1,
-				&type, &size);
 		if (buf) {
 			fwrite(buf, size, 1, f);
 			free(buf);
@@ -176,6 +177,34 @@ static void dangling_object(struct object *obj)
 	} else
 		fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
 	fclose(f);
+
+	if (obj->type == OBJ_COMMIT) {
+		struct strbuf sb = STRBUF_INIT;
+		struct commit *commit = lookup_commit(obj->sha1);
+		int reported = 0;
+
+		if (!commit->buffer)
+			commit->buffer = buf;
+		if (commit->buffer) {
+			parse_commit(commit);
+			pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb,
+					    0, NULL, NULL, 0, 0);
+			printf("dangling commit %s (%s)\n",
+			       sha1_to_hex(obj->sha1), sb.buf);
+			reported = 1;
+		}
+		strbuf_release(&sb);
+		free(commit->buffer);
+		if (buf && commit->buffer != buf)
+			free(buf);
+		commit->buffer = NULL;
+		if (reported)
+			return;
+	}
+
+ report_and_exit:
+	printf("dangling %s %s\n", typename(obj->type),
+	       sha1_to_hex(obj->sha1));
 }
 
 /*
-- 
1.5.5.rc2.186.gbac51

```
