{"thread":{"id":"5298","subject":"[RFC/PATCH] Trace into a file if GIT_TRACE can interpreted as a filename.","startedAt":"2006-08-16T20:58:46Z","lastAt":"2006-08-17T06:14:43Z","messageCount":3,"participants":["Christian Couder","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"25428","messageId":"20060816225846.ae424b46.chriscool@tuxfamily.org","threadId":"5298","inReplyTo":null,"subject":"[RFC/PATCH] Trace into a file if GIT_TRACE can interpreted as a filename.","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2006-08-16T20:58:46Z","receivedAt":"2006-08-16T20:58:46Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This RFC/patch is a first step to trace the git commands used when running\nthe test suite.\n\nThanks,\nChristian.\n\n-- 8< --\n[RFC/PATCH] Trace into a file if GIT_TRACE can interpreted as a filename.\n\nIf GIT_TRACE is set to something different than \"1\" or\n\"true\" then we suppose it's a filemane and try to trace\ninto it instead of stderr.\nSo this change is not compatible with the previous\nbehavior, because stderr will not be used anymore if\nGIT_TRACE is set to something other than \"1\" or \"true\".\nNo file locking is done. This maybe a problem.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n\n---\n Makefile      |    3 ++-\n cache.h       |    4 ++++\n exec_cmd.c    |   19 ++++++++++---------\n git.c         |   26 ++++++++++++++------------\n t/test-lib.sh |    3 +++\n trace.c       |   43 +++++++++++++++++++++++++++++++++++++++++++\n 6 files changed, 76 insertions(+), 22 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex c6b62d9..8ce78b3 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -253,7 +253,8 @@ LIB_OBJS = \\\n \tserver-info.o setup.o sha1_file.o sha1_name.o strbuf.o \\\n \ttag.o tree.o usage.o config.o environment.o ctype.o copy.o \\\n \tfetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \\\n-\talloc.o merge-file.o path-list.o unpack-trees.o help.o $(DIFF_OBJS)\n+\talloc.o merge-file.o path-list.o unpack-trees.o help.o trace.o \\\n+\t$(DIFF_OBJS)\n\n BUILTIN_OBJS = \\\n \tbuiltin-add.o \\\ndiff --git a/cache.h b/cache.h\nindex 913be6a..0efcf50 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -409,4 +409,8 @@ extern struct commit *alloc_commit_node(\n extern struct tag *alloc_tag_node(void);\n extern void alloc_report(void);\n \n+/* trace.c */\n+extern FILE *start_trace(void);\n+extern void stop_trace(void);\n+  \n #endif /* CACHE_H */\ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex 62f51fc..ddb45a3 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -43,6 +43,7 @@ int execv_git_cmd(const char **argv)\n \t\tint rc;\n \t\tconst char *exec_dir = paths[i];\n \t\tconst char *tmp;\n+\t\tFILE *trace_file;\n \n \t\tif (!exec_dir || !*exec_dir) continue;\n \n@@ -97,25 +98,25 @@ int execv_git_cmd(const char **argv)\n \t\ttmp = argv[0];\n \t\targv[0] = git_command;\n \n-\t\tif (getenv(\"GIT_TRACE\")) {\n+\t\tif ((trace_file = start_trace())) {\n \t\t\tconst char **p = argv;\n-\t\t\tfputs(\"trace: exec:\", stderr);\n+\t\t\tfputs(\"trace: exec:\", trace_file);\n \t\t\twhile (*p) {\n-\t\t\t\tfputc(' ', stderr);\n-\t\t\t\tsq_quote_print(stderr, *p);\n+\t\t\t\tfputc(' ', trace_file);\n+\t\t\t\tsq_quote_print(trace_file, *p);\n \t\t\t\t++p;\n \t\t\t}\n-\t\t\tputc('\\n', stderr);\n-\t\t\tfflush(stderr);\n+\t\t\tputc('\\n', trace_file);\n+\t\t\tstop_trace();\n \t\t}\n \n \t\t/* execve() can only ever return if it fails */\n \t\texecve(git_command, (char **)argv, environ);\n \n-\t\tif (getenv(\"GIT_TRACE\")) {\n-\t\t\tfprintf(stderr, \"trace: exec failed: %s\\n\",\n+\t\tif ((trace_file = start_trace())) {\n+\t\t\tfprintf(trace_file, \"trace: exec failed: %s\\n\",\n \t\t\t\tstrerror(errno));\n-\t\t\tfflush(stderr);\n+\t\t\tstop_trace();\n \t\t}\n \n \t\targv[0] = tmp;\ndiff --git a/git.c b/git.c\nindex 96e596b..39b78e2 100644\n--- a/git.c\n+++ b/git.c\n@@ -161,6 +161,7 @@ static int handle_alias(int *argcp, cons\n \tconst char *subdir;\n \tint count, option_count;\n \tconst char** new_argv;\n+\tFILE *trace_file;\n \n \tsubdir = setup_git_directory_gently(&nongit);\n \n@@ -179,16 +180,16 @@ static int handle_alias(int *argcp, cons\n \t\tif (!strcmp(alias_command, new_argv[0]))\n \t\t\tdie(\"recursive alias: %s\", alias_command);\n \n-\t\tif (getenv(\"GIT_TRACE\")) {\n+\t\tif ((trace_file = start_trace())) {\n \t\t\tint i;\n-\t\t\tfprintf(stderr, \"trace: alias expansion: %s =>\",\n+\t\t\tfprintf(trace_file, \"trace: alias expansion: %s =>\",\n \t\t\t\talias_command);\n \t\t\tfor (i = 0; i < count; ++i) {\n-\t\t\t\tfputc(' ', stderr);\n-\t\t\t\tsq_quote_print(stderr, new_argv[i]);\n+\t\t\t\tfputc(' ', trace_file);\n+\t\t\t\tsq_quote_print(trace_file, new_argv[i]);\n \t\t\t}\n-\t\t\tfputc('\\n', stderr);\n-\t\t\tfflush(stderr);\n+\t\t\tfputc('\\n', trace_file);\n+\t\t\tstop_trace();\n \t\t}\n \n \t\tnew_argv = realloc(new_argv, sizeof(char*) *\n@@ -282,6 +283,7 @@ static void handle_internal_command(int \n \tfor (i = 0; i < ARRAY_SIZE(commands); i++) {\n \t\tstruct cmd_struct *p = commands+i;\n \t\tconst char *prefix;\n+\t\tFILE * trace_file;\n \t\tif (strcmp(p->cmd, cmd))\n \t\t\tcontinue;\n \n@@ -290,15 +292,15 @@ static void handle_internal_command(int \n \t\t\tprefix = setup_git_directory();\n \t\tif (p->option & USE_PAGER)\n \t\t\tsetup_pager();\n-\t\tif (getenv(\"GIT_TRACE\")) {\n+\t\tif ((trace_file = start_trace())) {\n \t\t\tint i;\n-\t\t\tfprintf(stderr, \"trace: built-in: git\");\n+\t\t\tfprintf(trace_file, \"trace: built-in: git\");\n \t\t\tfor (i = 0; i < argc; ++i) {\n-\t\t\t\tfputc(' ', stderr);\n-\t\t\t\tsq_quote_print(stderr, argv[i]);\n+\t\t\t\tfputc(' ', trace_file);\n+\t\t\t\tsq_quote_print(trace_file, argv[i]);\n \t\t\t}\n-\t\t\tputc('\\n', stderr);\n-\t\t\tfflush(stderr);\n+\t\t\tputc('\\n', trace_file);\n+\t\t\tstop_trace();\n \t\t}\n \n \t\texit(p->fn(argc, argv, prefix));\ndiff --git a/t/test-lib.sh b/t/test-lib.sh\nindex b6d119a..5afa5f0 100755\n--- a/t/test-lib.sh\n+++ b/t/test-lib.sh\n@@ -35,6 +35,9 @@ export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME\n export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME\n export EDITOR VISUAL\n \n+# Uncomment the following line to trace the git commands into a file.\n+# export GIT_TRACE=`pwd`/git_trace.log\n+\n # Each test should start with something like this, after copyright notices:\n #\n # test_description='Description of this test...\ndiff --git a/trace.c b/trace.c\nnew file mode 100644\nindex 0000000..b1a14e0\n--- /dev/null\n+++ b/trace.c\n@@ -0,0 +1,43 @@\n+/*\n+ * GIT - The information manager from hell\n+ *\n+ * Copyright (C) Christian Couder, 2006\n+ *\n+ */\n+#include \"cache.h\"\n+\n+static FILE *trace_file = NULL;\n+\n+FILE *start_trace()\n+{\n+\tchar * trace = getenv(\"GIT_TRACE\");\n+\n+\tif (trace && (!strcmp(trace,\"1\") || !strcasecmp(trace,\"true\")))\n+\t\ttrace_file = stderr;\n+\telse if (trace && strcmp(trace, \"\") &&\n+\t\t strcmp(trace, \"0\") && strcasecmp(trace, \"false\")) {\n+\t\t/* We suppose we have a filename. */\n+\t\ttrace_file = fopen(trace, \"a\");\n+\t\tif (!trace_file) {\n+\t\t\tfprintf(stderr, \"Error: %s\\n\"\n+\t\t\t\t\"when opening trace file: %s\\n\"\n+\t\t\t\t\"Tracing on stderr instead.\\n\",\n+\t\t\t\tstrerror(errno), trace);\n+\t\t\ttrace_file = stderr;\n+\t\t}\n+\t}\n+\n+\treturn trace_file;\n+}\n+\n+void stop_trace()\n+{\n+\tif (trace_file) {\n+\t\tif (trace_file == stderr)\n+\t\t\tfflush(stderr);\n+\t\telse\n+\t\t\tfclose(trace_file);\n+\t\ttrace_file = NULL;\n+\t}\n+}\n+\n-- \n1.4.2.rc3.gcc6ea\n"},{"id":"25430","messageId":"7vhd0c2xbz.fsf@assigned-by-dhcp.cox.net","threadId":"5298","inReplyTo":"20060816225846.ae424b46.chriscool@tuxfamily.org","subject":"Re: [RFC/PATCH] Trace into a file if GIT_TRACE can interpreted as a filename.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-08-16T21:18:56Z","receivedAt":"2006-08-16T21:18:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christian Couder <chriscool@tuxfamily.org> writes:\n\n> So this change is not compatible with the previous\n> behavior, because stderr will not be used anymore if\n> GIT_TRACE is set to something other than \"1\" or \"true\".\n> No file locking is done. This maybe a problem.\n\nI wonder if we can pass an already opened file descriptor to\nGIT_TRACE.  E.g.\n\n\tGIT_TRACE=9 make test 9>/var/tmp/trace.log\n\nand make the trace code to emit the message with one write(2) to\nmake sure messages from processes do not get mixed up in the\nmiddle.\n"},{"id":"25447","messageId":"200608170814.44461.chriscool@tuxfamily.org","threadId":"5298","inReplyTo":"7vhd0c2xbz.fsf@assigned-by-dhcp.cox.net","subject":"Re: [RFC/PATCH] Trace into a file if GIT_TRACE can interpreted as a filename.","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2006-08-17T06:14:43Z","receivedAt":"2006-08-17T06:14:43Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Junio C Hamano writes:\n> \n> I wonder if we can pass an already opened file descriptor to\n> GIT_TRACE.  E.g.\n> \n> \tGIT_TRACE=9 make test 9>/var/tmp/trace.log\n> \n> and make the trace code to emit the message with one write(2) to\n> make sure messages from processes do not get mixed up in the\n> middle.\n \nI will see if I can come up with something.\n\nThanks again,\nChristian. \n"}]}