Subject: [PATCH 04/20] path.c: add is_git_path_shared()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 3 Feb 2016 16:35:34 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 cache.h |  2 ++
 path.c  | 27 ++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/cache.h b/cache.h
index ec71181..c75d13f 100644
--- a/cache.h
+++ b/cache.h
@@ -748,6 +748,8 @@ extern int check_repository_format(void);
 extern const char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
 extern const char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
 extern const char *git_common_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
+extern int is_git_path_shared(const char *path);
+
 
 extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
 	__attribute__((format (printf, 3, 4)));
diff --git a/path.c b/path.c
index 08ff346..9e350e7 100644
--- a/path.c
+++ b/path.c
@@ -335,15 +335,18 @@ static int check_common(const char *unmatched, void *value, void *baton)
 	return 0;
 }
 
-static void update_common_dir(struct strbuf *buf, int git_dir_len,
-			      const char *common_dir)
+static int update_common_dir(struct strbuf *buf, int git_dir_len,
+			     const char *common_dir)
 {
 	char *base = buf->buf + git_dir_len;
 	init_common_trie();
 	if (!common_dir)
 		common_dir = get_git_common_dir();
-	if (trie_find(&common_trie, base, check_common, NULL) > 0)
+	if (trie_find(&common_trie, base, check_common, NULL) > 0) {
 		replace_dir(buf, git_dir_len, common_dir);
+		return 1;
+	}
+	return 0;
 }
 
 void report_linked_checkout_garbage(void)
@@ -368,7 +371,7 @@ void report_linked_checkout_garbage(void)
 	strbuf_release(&sb);
 }
 
-static void adjust_git_path(struct strbuf *buf, int git_dir_len)
+static int adjust_git_path(struct strbuf *buf, int git_dir_len)
 {
 	const char *base = buf->buf + git_dir_len;
 	if (git_graft_env && is_dir_file(base, "info", "grafts"))
@@ -380,7 +383,10 @@ static void adjust_git_path(struct strbuf *buf, int git_dir_len)
 	else if (git_db_env && dir_prefix(base, "objects"))
 		replace_dir(buf, git_dir_len + 7, get_object_directory());
 	else if (git_common_dir_env)
-		update_common_dir(buf, git_dir_len, NULL);
+		return update_common_dir(buf, git_dir_len, NULL);
+	else
+		return 0;
+	return 1;
 }
 
 static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
@@ -532,6 +538,17 @@ void strbuf_git_common_path(struct strbuf *sb, const char *fmt, ...)
 	va_end(args);
 }
 
+int is_git_path_shared(const char *path)
+{
+	struct strbuf buf = STRBUF_INIT;
+	int ret;
+
+	strbuf_addf(&buf, ".git/%s", path);
+	ret = adjust_git_path(&buf, 5);
+	strbuf_release(&buf);
+	return ret;
+}
+
 int validate_headref(const char *path)
 {
 	struct stat st;
-- 
2.7.0.377.g4cd97dd