Subject: [PATCH v4 27/27] count-objects: report unused files in $GIT_DIR/repos/...

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sat, 1 Mar 2014 19:13:03 +0700

In linked checkouts, borrowed parts like config is taken from
$GIT_COMMON_DIR. $GIT_DIR/config is never used. Report them as
garbage.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/count-objects.c | 37 ++++++++++++++++++++++++++++++++++++-
 path.c                  |  4 ++++
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/builtin/count-objects.c b/builtin/count-objects.c
index a7f70cb..725cd5f 100644
--- a/builtin/count-objects.c
+++ b/builtin/count-objects.c
@@ -78,6 +78,39 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
 	}
 }
 
+static void report_linked_checkout_garbage(void)
+{
+	/*
+	 * must be more or less in sync with * path.c:update_common_dir().
+	 *
+	 * "logs" is let slip because logs/HEAD is in $GIT_DIR but the
+	 * remaining in $GIT_COMMON_DIR. Probably not worth traversing
+	 * the entire "logs" directory for that.
+	 *
+	 * The same "gc.pid" for because it's a temporary file.
+	 */
+	const char *list[] = {
+		"branches", "hooks", "info", "lost-found", "modules",
+		"objects", "refs", "remotes", "rr-cache", "svn",
+		"config", "packed-refs", "shallow", NULL
+	};
+	struct strbuf sb = STRBUF_INIT;
+	const char **p;
+	int len;
+
+	if (!file_exists(git_path("commondir")))
+		return;
+	strbuf_addf(&sb, "%s/", get_git_dir());
+	len = sb.len;
+	for (p = list; *p; p++) {
+		strbuf_setlen(&sb, len);
+		strbuf_addstr(&sb, *p);
+		if (file_exists(sb.buf))
+			report_garbage("unused in linked checkout", sb.buf);
+	}
+	strbuf_release(&sb);
+}
+
 static char const * const count_objects_usage[] = {
 	N_("git count-objects [-v] [-H | --human-readable]"),
 	NULL
@@ -102,8 +135,10 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
 	/* we do not take arguments other than flags for now */
 	if (argc)
 		usage_with_options(count_objects_usage, opts);
-	if (verbose)
+	if (verbose) {
 		report_garbage = real_report_garbage;
+		report_linked_checkout_garbage();
+	}
 	memcpy(path, objdir, len);
 	if (len && objdir[len-1] != '/')
 		path[len++] = '/';
diff --git a/path.c b/path.c
index 47383ff..2e6035d 100644
--- a/path.c
+++ b/path.c
@@ -92,6 +92,10 @@ static void replace_dir(struct strbuf *buf, int len, const char *newdir)
 
 static void update_common_dir(struct strbuf *buf, int git_dir_len)
 {
+	/*
+	 * Remember to report_linked_checkout_garbage()
+	 * builtin/count-objects.c
+	 */
 	const char *common_dir_list[] = {
 		"branches", "hooks", "info", "logs", "lost-found", "modules",
 		"objects", "refs", "remotes", "repos", "rr-cache", "svn",
-- 
1.9.0.40.gaa8c3ea