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 <matled@gmx.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Matthias Lederhofer committed
Jun 25, 2006 at 15:56 UTC
575ba9d69d5dfd07d95343fe946a5991c4cb27d6
5 files changed
+69
Documentation/git.txt
+7
@@ -615,6 +615,13 @@ git Diffs
615
gitlink:git-diff-files[1];
616
gitlink:git-diff-tree[1]
617
618
+other
619
+~~~~~
620
+'GIT_TRACE'::
621
+ If this variable is set git will print `trace:` messages on
622
+ stderr telling about alias expansion, built-in command
623
+ execution and external command execution.
624
+
625
Discussion[[Discussion]]
626
------------------------
627
include::README[]
exec_cmd.c
+19
@@ -1,5 +1,6 @@
1
#include "cache.h"
2
#include "exec_cmd.h"
3
+#include "quote.h"
4
#define MAX_ARGS 32
5
6
extern char **environ;
@@ -96,9 +97,27 @@ int execv_git_cmd(const char **argv)
97
tmp = argv[0];
98
argv[0] = git_command;
99
100
+ if (getenv("GIT_TRACE")) {
101
+ fputs("trace: exec:", stderr);
102
+ const char **p = argv;
103
+ while (*p) {
104
+ fputc(' ', stderr);
105
+ sq_quote_print(stderr, *p);
106
+ ++p;
107
+ }
108
+ putc('\n', stderr);
109
+ fflush(stderr);
110
+ }
111
+
112
/* execve() can only ever return if it fails */
113
execve(git_command, (char **)argv, environ);
114
115
+ if (getenv("GIT_TRACE")) {
116
+ fprintf(stderr, "trace: exec failed: %s\n",
117
+ strerror(errno));
118
+ fflush(stderr);
119
+ }
120
+
121
argv[0] = tmp;
122
}
123
return -1;
git.c
+25
@@ -11,6 +11,7 @@
11
#include "git-compat-util.h"
12
#include "exec_cmd.h"
13
#include "cache.h"
14
+#include "quote.h"
15
16
#include "builtin.h"
17
@@ -120,6 +121,18 @@ static int handle_alias(int *argcp, const char ***argv)
121
if (!strcmp(alias_command, new_argv[0]))
122
die("recursive alias: %s", alias_command);
123
124
+ if (getenv("GIT_TRACE")) {
125
+ int i;
126
+ fprintf(stderr, "trace: alias expansion: %s =>",
127
+ alias_command);
128
+ for (i = 0; i < count; ++i) {
129
+ fputc(' ', stderr);
130
+ sq_quote_print(stderr, new_argv[i]);
131
+ }
132
+ fputc('\n', stderr);
133
+ fflush(stderr);
134
+ }
135
+
136
/* insert after command name */
137
if (*argcp > 1) {
138
new_argv = realloc(new_argv, sizeof(char*) *
@@ -202,6 +215,18 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
215
struct cmd_struct *p = commands+i;
216
if (strcmp(p->cmd, cmd))
217
continue;
218
+
219
+ if (getenv("GIT_TRACE")) {
220
+ int i;
221
+ fprintf(stderr, "trace: built-in: git");
222
+ for (i = 0; i < argc; ++i) {
223
+ fputc(' ', stderr);
224
+ sq_quote_print(stderr, argv[i]);
225
+ }
226
+ putc('\n', stderr);
227
+ fflush(stderr);
228
+ }
229
+
230
exit(p->fn(argc, argv, envp));
231
}
232
}
quote.c
+17
@@ -45,6 +45,23 @@ size_t sq_quote_buf(char *dst, size_t n, const char *src)
45
return len;
46
}
47
48
+void sq_quote_print(FILE *stream, const char *src)
49
+{
50
+ char c;
51
+
52
+ fputc('\'', stream);
53
+ while ((c = *src++)) {
54
+ if (need_bs_quote(c)) {
55
+ fputs("'\\", stream);
56
+ fputc(c, stream);
57
+ fputc('\'', stream);
58
+ } else {
59
+ fputc(c, stream);
60
+ }
61
+ }
62
+ fputc('\'', stream);
63
+}
64
+
65
char *sq_quote(const char *src)
66
{
67
char *buf;
quote.h
+1
@@ -29,6 +29,7 @@
29
*/
30
31
extern char *sq_quote(const char *src);
32
+extern void sq_quote_print(FILE *stream, const char *src);
33
extern size_t sq_quote_buf(char *dst, size_t n, const char *src);
34
35
/* This unwraps what sq_quote() produces in place, but returns