use only the $PATH for exec'ing git commands
[gitweb.git] / help.c
diff --git a/help.c b/help.c
index b6674635a2e08197cc13aaa6acbe6290a303d6b3..34ac5db60188d3b5d6984fcfcbbd30db018a9b3f 100644 (file)
--- a/help.c
+++ b/help.c
@@ -31,12 +31,6 @@ static int term_columns(void)
        return 80;
 }
 
-static void oom(void)
-{
-       fprintf(stderr, "git: out of memory\n");
-       exit(1);
-}
-
 static inline void mput_char(char c, unsigned int num)
 {
        while(num--)
@@ -54,13 +48,9 @@ static void add_cmdname(const char *name, int len)
        struct cmdname *ent;
        if (cmdname_alloc <= cmdname_cnt) {
                cmdname_alloc = cmdname_alloc + 200;
-               cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
-               if (!cmdname)
-                       oom();
+               cmdname = xrealloc(cmdname, cmdname_alloc * sizeof(*cmdname));
        }
-       ent = malloc(sizeof(*ent) + len);
-       if (!ent)
-               oom();
+       ent = xmalloc(sizeof(*ent) + len);
        ent->len = len;
        memcpy(ent->name, name, len);
        ent->name[len] = 0;
@@ -103,49 +93,39 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
        }
 }
 
-static void list_commands(const char *exec_path, const char *pattern)
+static void list_commands(const char *exec_path)
 {
        unsigned int longest = 0;
-       char path[PATH_MAX];
-       int dirlen;
+       const char *prefix = "git-";
+       int prefix_len = strlen(prefix);
        DIR *dir = opendir(exec_path);
        struct dirent *de;
 
-       if (!dir) {
+       if (!dir || chdir(exec_path)) {
                fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
                exit(1);
        }
 
-       dirlen = strlen(exec_path);
-       if (PATH_MAX - 20 < dirlen) {
-               fprintf(stderr, "git: insanely long exec-path '%s'\n",
-                       exec_path);
-               exit(1);
-       }
-
-       memcpy(path, exec_path, dirlen);
-       path[dirlen++] = '/';
-
        while ((de = readdir(dir)) != NULL) {
                struct stat st;
                int entlen;
 
-               if (strncmp(de->d_name, "git-", 4))
+               if (prefixcmp(de->d_name, prefix))
                        continue;
-               strcpy(path+dirlen, de->d_name);
-               if (stat(path, &st) || /* stat, not lstat */
+
+               if (stat(de->d_name, &st) || /* stat, not lstat */
                    !S_ISREG(st.st_mode) ||
                    !(st.st_mode & S_IXUSR))
                        continue;
 
-               entlen = strlen(de->d_name);
+               entlen = strlen(de->d_name) - prefix_len;
                if (has_extension(de->d_name, ".exe"))
                        entlen -= 4;
 
                if (longest < entlen)
                        longest = entlen;
 
-               add_cmdname(de->d_name + 4, entlen-4);
+               add_cmdname(de->d_name + prefix_len, entlen);
        }
        closedir(dir);
 
@@ -153,11 +133,11 @@ static void list_commands(const char *exec_path, const char *pattern)
        printf("----------------------------");
        mput_char('-', strlen(exec_path));
        putchar('\n');
-       pretty_print_string_list(cmdname, longest - 4);
+       pretty_print_string_list(cmdname, longest);
        putchar('\n');
 }
 
-static void list_common_cmds_help(void)
+void list_common_cmds_help(void)
 {
        int i, longest = 0;
 
@@ -179,7 +159,7 @@ static void show_man_page(const char *git_cmd)
 {
        const char *page;
 
-       if (!strncmp(git_cmd, "git", 3))
+       if (!prefixcmp(git_cmd, "git"))
                page = git_cmd;
        else {
                int page_len = strlen(git_cmd) + 4;
@@ -195,8 +175,7 @@ static void show_man_page(const char *git_cmd)
 
 void help_unknown_cmd(const char *cmd)
 {
-       printf("git: '%s' is not a git-command\n\n", cmd);
-       list_common_cmds_help();
+       fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
        exit(1);
 }
 
@@ -214,14 +193,14 @@ int cmd_help(int argc, const char **argv, const char *prefix)
        if (!help_cmd) {
                printf("usage: %s\n\n", git_usage_string);
                list_common_cmds_help();
-               exit(1);
+               exit(0);
        }
 
        else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
                printf("usage: %s\n\n", git_usage_string);
                if(exec_path)
-                       list_commands(exec_path, "git-*");
-               exit(1);
+                       list_commands(exec_path);
+               exit(0);
        }
 
        else
@@ -229,5 +208,3 @@ int cmd_help(int argc, const char **argv, const char *prefix)
 
        return 0;
 }
-
-