From 575ba9d69d5dfd07d95343fe946a5991c4cb27d6 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Sun, 25 Jun 2006 15:56:18 +0200 Subject: [PATCH 1/3] GIT_TRACE: show which built-in/external commands are executed With the environment variable GIT_TRACE set git will show - alias expansion - built-in command execution - external command execution on stderr. Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- Documentation/git.txt | 7 +++++++ exec_cmd.c | 19 +++++++++++++++++++ git.c | 25 +++++++++++++++++++++++++ quote.c | 17 +++++++++++++++++ quote.h | 1 + 5 files changed, 69 insertions(+) diff --git a/Documentation/git.txt b/Documentation/git.txt index 51f20c6e67..4b140b8216 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -615,6 +615,13 @@ git Diffs gitlink:git-diff-files[1]; gitlink:git-diff-tree[1] +other +~~~~~ +'GIT_TRACE':: + If this variable is set git will print `trace:` messages on + stderr telling about alias expansion, built-in command + execution and external command execution. + Discussion[[Discussion]] ------------------------ include::README[] diff --git a/exec_cmd.c b/exec_cmd.c index c1539d12ce..f2133ec93f 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -1,5 +1,6 @@ #include "cache.h" #include "exec_cmd.h" +#include "quote.h" #define MAX_ARGS 32 extern char **environ; @@ -96,9 +97,27 @@ int execv_git_cmd(const char **argv) tmp = argv[0]; argv[0] = git_command; + if (getenv("GIT_TRACE")) { + fputs("trace: exec:", stderr); + const char **p = argv; + while (*p) { + fputc(' ', stderr); + sq_quote_print(stderr, *p); + ++p; + } + putc('\n', stderr); + fflush(stderr); + } + /* execve() can only ever return if it fails */ execve(git_command, (char **)argv, environ); + if (getenv("GIT_TRACE")) { + fprintf(stderr, "trace: exec failed: %s\n", + strerror(errno)); + fflush(stderr); + } + argv[0] = tmp; } return -1; diff --git a/git.c b/git.c index 256730112e..27989da77c 100644 --- a/git.c +++ b/git.c @@ -11,6 +11,7 @@ #include "git-compat-util.h" #include "exec_cmd.h" #include "cache.h" +#include "quote.h" #include "builtin.h" @@ -120,6 +121,18 @@ static int handle_alias(int *argcp, const char ***argv) if (!strcmp(alias_command, new_argv[0])) die("recursive alias: %s", alias_command); + if (getenv("GIT_TRACE")) { + int i; + fprintf(stderr, "trace: alias expansion: %s =>", + alias_command); + for (i = 0; i < count; ++i) { + fputc(' ', stderr); + sq_quote_print(stderr, new_argv[i]); + } + fputc('\n', stderr); + fflush(stderr); + } + /* insert after command name */ if (*argcp > 1) { new_argv = realloc(new_argv, sizeof(char*) * @@ -202,6 +215,18 @@ static void handle_internal_command(int argc, const char **argv, char **envp) struct cmd_struct *p = commands+i; if (strcmp(p->cmd, cmd)) continue; + + if (getenv("GIT_TRACE")) { + int i; + fprintf(stderr, "trace: built-in: git"); + for (i = 0; i < argc; ++i) { + fputc(' ', stderr); + sq_quote_print(stderr, argv[i]); + } + putc('\n', stderr); + fflush(stderr); + } + exit(p->fn(argc, argv, envp)); } } diff --git a/quote.c b/quote.c index 1910d000a5..e220dcc280 100644 --- a/quote.c +++ b/quote.c @@ -45,6 +45,23 @@ size_t sq_quote_buf(char *dst, size_t n, const char *src) return len; } +void sq_quote_print(FILE *stream, const char *src) +{ + char c; + + fputc('\'', stream); + while ((c = *src++)) { + if (need_bs_quote(c)) { + fputs("'\\", stream); + fputc(c, stream); + fputc('\'', stream); + } else { + fputc(c, stream); + } + } + fputc('\'', stream); +} + char *sq_quote(const char *src) { char *buf; diff --git a/quote.h b/quote.h index c1ab3788e6..fc5481e78a 100644 --- a/quote.h +++ b/quote.h @@ -29,6 +29,7 @@ */ extern char *sq_quote(const char *src); +extern void sq_quote_print(FILE *stream, const char *src); extern size_t sq_quote_buf(char *dst, size_t n, const char *src); /* This unwraps what sq_quote() produces in place, but returns From e82e058d3ad1d50fbb99fac8630ad7b511ca4abe Mon Sep 17 00:00:00 2001 From: Timo Hirvonen Date: Wed, 28 Jun 2006 12:15:00 +0300 Subject: [PATCH 2/3] GIT_TRACE: fix a mixed declarations and code warning Signed-off-by: Timo Hirvonen Signed-off-by: Junio C Hamano --- exec_cmd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec_cmd.c b/exec_cmd.c index f2133ec93f..62f51fcd6e 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -98,8 +98,8 @@ int execv_git_cmd(const char **argv) argv[0] = git_command; if (getenv("GIT_TRACE")) { - fputs("trace: exec:", stderr); const char **p = argv; + fputs("trace: exec:", stderr); while (*p) { fputc(' ', stderr); sq_quote_print(stderr, *p); From 1d0361e806086f6076c221ff5bb40b73643e96c3 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 12 Jul 2006 23:09:00 -0700 Subject: [PATCH 3/3] test-lib: unset GIT_TRACE Signed-off-by: Junio C Hamano --- t/test-lib.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/test-lib.sh b/t/test-lib.sh index 05f6e79560..c3d3984b33 100755 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -26,6 +26,7 @@ unset GIT_DIR unset GIT_EXTERNAL_DIFF unset GIT_INDEX_FILE unset GIT_OBJECT_DIRECTORY +unset GIT_TRACE unset SHA1_FILE_DIRECTORIES unset SHA1_FILE_DIRECTORY export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME