Subject: [PATCH 01/34] builtins: print setup info if repo is found

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 27 Oct 2010 21:49:04 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 It's not perfect. "setup: " lines won't always appear. That can be
 done once setup code is cleaned up a bit.

 cache.h |    1 +
 git.c   |   36 ++++++++++++++++++++++++++++++++++++
 trace.c |   11 +++++++++++
 3 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/cache.h b/cache.h
index 33decd9..d5ccf40 100644
--- a/cache.h
+++ b/cache.h
@@ -1058,6 +1058,7 @@ extern void *alloc_object_node(void);
 extern void alloc_report(void);
 
 /* trace.c */
+int has_trace_fd();
 __attribute__((format (printf, 1, 2)))
 extern void trace_printf(const char *format, ...);
 __attribute__((format (printf, 2, 3)))
diff --git a/git.c b/git.c
index 50a1401..22ade18 100644
--- a/git.c
+++ b/git.c
@@ -244,6 +244,29 @@ struct cmd_struct {
 	int option;
 };
 
+static const char *quote_crnl(const char *path)
+{
+	static char new_path[PATH_MAX];
+	const char *p2 = path;
+	char *p1 = new_path;
+
+	if (!path)
+		return NULL;
+
+	while (*p2) {
+		switch (*p2) {
+		case '\\': *p1++ = '\\'; *p1++ = '\\'; break;
+		case '\n': *p1++ = '\\'; *p1++ = 'n'; break;
+		case '\r': *p1++ = '\\'; *p1++ = 'r'; break;
+		default:
+			*p1++ = *p2;
+		}
+		p2++;
+	}
+	*p1 = '\0';
+	return new_path;
+}
+
 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 {
 	int status, help;
@@ -264,6 +287,19 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 			use_pager = check_pager_config(p->cmd);
 		if (use_pager == -1 && p->option & USE_PAGER)
 			use_pager = 1;
+
+		if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
+		    startup_info->have_repository && /* get_git_dir() may set up repo, avoid that */
+		    has_trace_fd()) {
+			char cwd[PATH_MAX];
+			if (!getcwd(cwd, PATH_MAX))
+				die("Unable to get current working directory");
+
+			trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir()));
+			trace_printf("setup: worktree: %s\n", quote_crnl(get_git_work_tree()));
+			trace_printf("setup: cwd: %s\n", quote_crnl(cwd));
+			trace_printf("setup: prefix: %s\n", quote_crnl(prefix));
+		}
 	}
 	commit_pager_choice();
 
diff --git a/trace.c b/trace.c
index 1e560cb..fbf7de0 100644
--- a/trace.c
+++ b/trace.c
@@ -29,6 +29,17 @@ void do_nothing(size_t unused)
 {
 }
 
+int has_trace_fd()
+{
+	char *trace = getenv("GIT_TRACE");
+
+	if (!trace || !strcmp(trace, "") ||
+	    !strcmp(trace, "0") || !strcasecmp(trace, "false"))
+		return 0;
+
+	return 1;
+}
+
 /* Get a trace file descriptor from GIT_TRACE env variable. */
 static int get_trace_fd(int *need_close)
 {
-- 
1.7.0.2.445.gcbdb3