Make the default abbrev length configurable
[gitweb.git] / trace.c
diff --git a/trace.c b/trace.c
index 91548a56eceb56cfcb0521fc0313afe51dff0353..35d388dce44a4a8e3d6053dfd6abcb652771818a 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -37,7 +37,7 @@ static int get_trace_fd(int *need_close)
                return STDERR_FILENO;
        if (strlen(trace) == 1 && isdigit(*trace))
                return atoi(trace);
-       if (*trace == '/') {
+       if (is_absolute_path(trace)) {
                int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
                if (fd == -1) {
                        fprintf(stderr,
@@ -50,7 +50,7 @@ static int get_trace_fd(int *need_close)
                return fd;
        }
 
-       fprintf(stderr, "What does '%s' for GIT_TRACE mean?\n", trace);
+       fprintf(stderr, "What does '%s' for GIT_TRACE mean?\n", trace);
        fprintf(stderr, "If you want to trace into a file, "
                "then please set GIT_TRACE to an absolute pathname "
                "(starting with /).\n");
@@ -64,7 +64,7 @@ static const char err_msg[] = "Could not trace into fd given by "
 
 void trace_printf(const char *fmt, ...)
 {
-       char buf[8192];
+       struct strbuf buf;
        va_list ap;
        int fd, len, need_close = 0;
 
@@ -72,45 +72,107 @@ void trace_printf(const char *fmt, ...)
        if (!fd)
                return;
 
+       set_try_to_free_routine(NULL);  /* is never reset */
+       strbuf_init(&buf, 64);
        va_start(ap, fmt);
-       len = vsnprintf(buf, sizeof(buf), fmt, ap);
+       len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
        va_end(ap);
-       if (len >= sizeof(buf))
-               die("unreasonnable trace length");
-       write_or_whine_pipe(fd, buf, len, err_msg);
+       if (len >= strbuf_avail(&buf)) {
+               strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
+               va_start(ap, fmt);
+               len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
+               va_end(ap);
+               if (len >= strbuf_avail(&buf))
+                       die("broken vsnprintf");
+       }
+       strbuf_setlen(&buf, len);
+
+       write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
+       strbuf_release(&buf);
 
        if (need_close)
                close(fd);
 }
 
-void trace_argv_printf(const char **argv, int count, const char *fmt, ...)
+void trace_argv_printf(const char **argv, const char *fmt, ...)
 {
-       char buf[8192];
+       struct strbuf buf;
        va_list ap;
-       char *argv_str;
-       size_t argv_len;
        int fd, len, need_close = 0;
 
        fd = get_trace_fd(&need_close);
        if (!fd)
                return;
 
+       set_try_to_free_routine(NULL);  /* is never reset */
+       strbuf_init(&buf, 64);
        va_start(ap, fmt);
-       len = vsnprintf(buf, sizeof(buf), fmt, ap);
+       len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
        va_end(ap);
-       if (len >= sizeof(buf))
-               die("unreasonnable trace length");
-
-       /* Get the argv string. */
-       argv_str = sq_quote_argv(argv, count);
-       argv_len = strlen(argv_str);
-
-       write_or_whine_pipe(fd, buf, len, err_msg);
-       write_or_whine_pipe(fd, argv_str, argv_len, err_msg);
-       write_or_whine_pipe(fd, "\n", 1, err_msg);
+       if (len >= strbuf_avail(&buf)) {
+               strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
+               va_start(ap, fmt);
+               len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
+               va_end(ap);
+               if (len >= strbuf_avail(&buf))
+                       die("broken vsnprintf");
+       }
+       strbuf_setlen(&buf, len);
 
-       free(argv_str);
+       sq_quote_argv(&buf, argv, 0);
+       strbuf_addch(&buf, '\n');
+       write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
+       strbuf_release(&buf);
 
        if (need_close)
                close(fd);
 }
+
+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;
+}
+
+/* FIXME: move prefix to startup_info struct and get rid of this arg */
+void trace_repo_setup(const char *prefix)
+{
+       const char *git_work_tree;
+       char cwd[PATH_MAX];
+       char *trace = getenv("GIT_TRACE");
+
+       if (!trace || !strcmp(trace, "") ||
+           !strcmp(trace, "0") || !strcasecmp(trace, "false"))
+               return;
+
+       if (!getcwd(cwd, PATH_MAX))
+               die("Unable to get current working directory");
+
+       if (!(git_work_tree = get_git_work_tree()))
+               git_work_tree = "(null)";
+
+       if (!prefix)
+               prefix = "(null)";
+
+       trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir()));
+       trace_printf("setup: worktree: %s\n", quote_crnl(git_work_tree));
+       trace_printf("setup: cwd: %s\n", quote_crnl(cwd));
+       trace_printf("setup: prefix: %s\n", quote_crnl(prefix));
+}