Subject: [PATCH v2 21/21] revision: include repos/../HEAD in --all

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sat, 14 Dec 2013 17:55:07 +0700

This makes sure repack won't drop detached HEADS from $GIT_DIR/repos/

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 refs.c     | 21 +++++++++++++++++++++
 refs.h     |  1 +
 revision.c |  1 +
 3 files changed, 23 insertions(+)

diff --git a/refs.c b/refs.c
index 8ef1cb0..a9c8651 100644
--- a/refs.c
+++ b/refs.c
@@ -1740,6 +1740,27 @@ static int do_one_head_ref(const char *ref, const char *submodule,
 	return 0;
 }
 
+int repos_head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	DIR *dir = opendir(git_path("repos"));
+	int ret = 0;
+	struct dirent *d;
+
+	if (!dir)
+		return ret;
+
+	while (!ret && (d = readdir(dir)) != NULL) {
+		struct strbuf sb_ref = STRBUF_INIT;
+		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+			continue;
+		strbuf_addf(&sb_ref, "repos/%s/HEAD", d->d_name);
+		ret = do_one_head_ref(sb_ref.buf, submodule, fn, cb_data);
+		strbuf_release(&sb_ref);
+	}
+	closedir(dir);
+	return ret;
+}
+
 int head_ref(each_ref_fn fn, void *cb_data)
 {
 	return do_one_head_ref("HEAD", NULL, fn, cb_data);
diff --git a/refs.h b/refs.h
index 87a1a79..05686e4 100644
--- a/refs.h
+++ b/refs.h
@@ -70,6 +70,7 @@ extern int for_each_glob_ref(each_ref_fn, const char *pattern, void *);
 extern int for_each_glob_ref_in(each_ref_fn, const char *pattern, const char* prefix, void *);
 
 extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
+extern int repos_head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
 extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
 extern int for_each_ref_in_submodule(const char *submodule, const char *prefix,
 		each_ref_fn fn, void *cb_data);
diff --git a/revision.c b/revision.c
index 05d2d77..c4150ed 100644
--- a/revision.c
+++ b/revision.c
@@ -2004,6 +2004,7 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	if (!strcmp(arg, "--all")) {
 		handle_refs(submodule, revs, *flags, for_each_ref_submodule);
 		handle_refs(submodule, revs, *flags, head_ref_submodule);
+		handle_refs(submodule, revs, *flags, repos_head_ref_submodule);
 		clear_ref_exclusion(&revs->ref_excludes);
 	} else if (!strcmp(arg, "--branches")) {
 		handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
-- 
1.8.5.1.77.g42c48fa