Skip to content

Commit d3ba675

Browse files
author
Junio C Hamano
committed
Merge branch 'ml/trace'
* ml/trace: test-lib: unset GIT_TRACE GIT_TRACE: fix a mixed declarations and code warning GIT_TRACE: show which built-in/external commands are executed
2 parents a72f937 + 1d0361e commit d3ba675

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

Documentation/git.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,13 @@ git Diffs
615615
gitlink:git-diff-files[1];
616616
gitlink:git-diff-tree[1]
617617

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+
618625
Discussion[[Discussion]]
619626
------------------------
620627
include::README[]

exec_cmd.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "exec_cmd.h"
3+
#include "quote.h"
34
#define MAX_ARGS 32
45

56
extern char **environ;
@@ -96,9 +97,27 @@ int execv_git_cmd(const char **argv)
9697
tmp = argv[0];
9798
argv[0] = git_command;
9899

100+
if (getenv("GIT_TRACE")) {
101+
const char **p = argv;
102+
fputs("trace: exec:", stderr);
103+
while (*p) {
104+
fputc(' ', stderr);
105+
sq_quote_print(stderr, *p);
106+
++p;
107+
}
108+
putc('\n', stderr);
109+
fflush(stderr);
110+
}
111+
99112
/* execve() can only ever return if it fails */
100113
execve(git_command, (char **)argv, environ);
101114

115+
if (getenv("GIT_TRACE")) {
116+
fprintf(stderr, "trace: exec failed: %s\n",
117+
strerror(errno));
118+
fflush(stderr);
119+
}
120+
102121
argv[0] = tmp;
103122
}
104123
return -1;

git.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "git-compat-util.h"
1212
#include "exec_cmd.h"
1313
#include "cache.h"
14+
#include "quote.h"
1415

1516
#include "builtin.h"
1617

@@ -120,6 +121,18 @@ static int handle_alias(int *argcp, const char ***argv)
120121
if (!strcmp(alias_command, new_argv[0]))
121122
die("recursive alias: %s", alias_command);
122123

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+
123136
/* insert after command name */
124137
if (*argcp > 1) {
125138
new_argv = realloc(new_argv, sizeof(char*) *
@@ -203,6 +216,18 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
203216
struct cmd_struct *p = commands+i;
204217
if (strcmp(p->cmd, cmd))
205218
continue;
219+
220+
if (getenv("GIT_TRACE")) {
221+
int i;
222+
fprintf(stderr, "trace: built-in: git");
223+
for (i = 0; i < argc; ++i) {
224+
fputc(' ', stderr);
225+
sq_quote_print(stderr, argv[i]);
226+
}
227+
putc('\n', stderr);
228+
fflush(stderr);
229+
}
230+
206231
exit(p->fn(argc, argv, envp));
207232
}
208233
}

quote.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ size_t sq_quote_buf(char *dst, size_t n, const char *src)
4545
return len;
4646
}
4747

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+
4865
char *sq_quote(const char *src)
4966
{
5067
char *buf;

quote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
extern char *sq_quote(const char *src);
32+
extern void sq_quote_print(FILE *stream, const char *src);
3233
extern size_t sq_quote_buf(char *dst, size_t n, const char *src);
3334

3435
/* This unwraps what sq_quote() produces in place, but returns

t/test-lib.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ unset GIT_DIR
2828
unset GIT_EXTERNAL_DIFF
2929
unset GIT_INDEX_FILE
3030
unset GIT_OBJECT_DIRECTORY
31+
unset GIT_TRACE
3132
unset SHA1_FILE_DIRECTORIES
3233
unset SHA1_FILE_DIRECTORY
3334
export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME

0 commit comments

Comments
 (0)