combine-diff: better hunk splitting.
[gitweb.git] / git.c
diff --git a/git.c b/git.c
index 0b10b6e781fdf76a3822b15cfa304352aee812cc..4616df6e62de5fb2f4a22ebb04fb87664579bb84 100644 (file)
--- a/git.c
+++ b/git.c
@@ -8,6 +8,9 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
+#include <sys/ioctl.h>
+#include "git-compat-util.h"
+#include "exec_cmd.h"
 
 #ifndef PATH_MAX
 # define PATH_MAX 4096
@@ -25,6 +28,16 @@ static int term_columns(void)
        if (col_string && (n_cols = atoi(col_string)) > 0)
                return n_cols;
 
+#ifdef TIOCGWINSZ
+       {
+               struct winsize ws;
+               if (!ioctl(1, TIOCGWINSZ, &ws)) {
+                       if (ws.ws_col)
+                               return ws.ws_col;
+               }
+       }
+#endif
+
        return 80;
 }
 
@@ -73,25 +86,28 @@ static int cmdname_compare(const void *a_, const void *b_)
 
 static void pretty_print_string_list(struct cmdname **cmdname, int longest)
 {
-       int cols = 1;
+       int cols = 1, rows;
        int space = longest + 1; /* min 1 SP between words */
        int max_cols = term_columns() - 1; /* don't print *on* the edge */
-       int i;
+       int i, j;
 
        if (space < max_cols)
                cols = max_cols / space;
+       rows = (cmdname_cnt + cols - 1) / cols;
 
        qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
 
-       for (i = 0; i < cmdname_cnt; ) {
-               int c;
+       for (i = 0; i < rows; i++) {
                printf("  ");
 
-               for (c = cols; c && i < cmdname_cnt; i++) {
-                       printf("%s", cmdname[i]->name);
-
-                       if (--c)
-                               mput_char(' ', space - cmdname[i]->len);
+               for (j = 0; j < cols; j++) {
+                       int n = j * rows + i;
+                       int size = space;
+                       if (n >= cmdname_cnt)
+                               break;
+                       if (j == cols-1 || n + rows >= cmdname_cnt)
+                               size = 1;
+                       printf("%-*s", size, cmdname[n]->name);
                }
                putchar('\n');
        }
@@ -152,10 +168,10 @@ static void list_commands(const char *exec_path, const char *pattern)
 }
 
 #ifdef __GNUC__
-static void usage(const char *exec_path, const char *fmt, ...)
+static void cmd_usage(const char *exec_path, const char *fmt, ...)
        __attribute__((__format__(__printf__, 2, 3), __noreturn__));
 #endif
-static void usage(const char *exec_path, const char *fmt, ...)
+static void cmd_usage(const char *exec_path, const char *fmt, ...)
 {
        if (fmt) {
                va_list ap;
@@ -188,7 +204,6 @@ static void prepend_to_path(const char *dir, int len)
        path_len = len + strlen(old_path) + 1;
 
        path = malloc(path_len + 1);
-       path[path_len + 1] = '\0';
 
        memcpy(path, dir, len);
        path[len] = ':';
@@ -219,17 +234,19 @@ int main(int argc, char **argv, char **envp)
 {
        char git_command[PATH_MAX + 1];
        char wd[PATH_MAX + 1];
-       int i, len, show_help = 0;
-       char *exec_path = getenv("GIT_EXEC_PATH");
+       int i, show_help = 0;
+       const char *exec_path;
 
        getcwd(wd, PATH_MAX);
 
-       if (!exec_path)
-               exec_path = GIT_EXEC_PATH;
-
        for (i = 1; i < argc; i++) {
                char *arg = argv[i];
 
+               if (!strcmp(arg, "help")) {
+                       show_help = 1;
+                       continue;
+               }
+
                if (strncmp(arg, "--", 2))
                        break;
 
@@ -237,10 +254,11 @@ int main(int argc, char **argv, char **envp)
 
                if (!strncmp(arg, "exec-path", 9)) {
                        arg += 9;
-                       if (*arg == '=')
+                       if (*arg == '=') {
                                exec_path = arg + 1;
-                       else {
-                               puts(exec_path);
+                               git_set_exec_path(exec_path);
+                       } else {
+                               puts(git_exec_path());
                                exit(0);
                        }
                }
@@ -251,48 +269,26 @@ int main(int argc, char **argv, char **envp)
                else if (!strcmp(arg, "help"))
                        show_help = 1;
                else if (!show_help)
-                       usage(NULL, NULL);
+                       cmd_usage(NULL, NULL);
        }
 
        if (i >= argc || show_help) {
                if (i >= argc)
-                       usage(exec_path, NULL);
+                       cmd_usage(git_exec_path(), NULL);
 
                show_man_page(argv[i]);
        }
 
-       if (*exec_path != '/') {
-               if (!getcwd(git_command, sizeof(git_command))) {
-                       fprintf(stderr,
-                               "git: cannot determine current directory");
-                       exit(1);
-               }
-               len = strlen(git_command);
-
-               /* Trivial cleanup */
-               while (!strncmp(exec_path, "./", 2)) {
-                       exec_path += 2;
-                       while (*exec_path == '/')
-                               exec_path++;
-               }
-               snprintf(git_command + len, sizeof(git_command) - len,
-                        "/%s", exec_path);
-       }
-       else
-               strcpy(git_command, exec_path);
-       len = strlen(git_command);
-       prepend_to_path(git_command, len);
+       exec_path = git_exec_path();
+       prepend_to_path(exec_path, strlen(exec_path));
 
-       strncat(&git_command[len], "/git-", sizeof(git_command) - len);
-       len += 5;
-       strncat(&git_command[len], argv[i], sizeof(git_command) - len);
+       execv_git_cmd(argv + i);
 
-       if (access(git_command, X_OK))
-               usage(exec_path, "'%s' is not a git-command", argv[i]);
+       if (errno == ENOENT)
+               cmd_usage(exec_path, "'%s' is not a git-command", argv[i]);
 
-       /* execve() can only ever return if it fails */
-       execve(git_command, &argv[i], envp);
-       printf("Failed to run command '%s': %s\n", git_command, strerror(errno));
+       fprintf(stderr, "Failed to run command '%s': %s\n",
+               git_command, strerror(errno));
 
        return 1;
 }