From: Scott R Parish <srp@srparish.net>
To: git@vger.kernel.org
Cc: Scott R Parish <srp@srparish.net>
Subject: [PATCH 6/7] walk PATH to generate list of commands for "help -a"
Date: Wed, 24 Oct 2007 20:37:16 -0700 [thread overview]
Message-ID: <1193283437-1706-6-git-send-email-srp@srparish.net> (raw)
In-Reply-To: <1193283437-1706-5-git-send-email-srp@srparish.net>
Signed-off-by: Scott R Parish <srp@srparish.net>
---
help.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 52 insertions(+), 17 deletions(-)
diff --git a/help.c b/help.c
index 906f8f6..3f8b4aa 100644
--- a/help.c
+++ b/help.c
@@ -64,6 +64,19 @@ static int cmdname_compare(const void *a_, const void *b_)
return strcmp(a->name, b->name);
}
+static void uniq(struct cmdname **cmdname)
+{
+ int i, j;
+
+ for (i = j = 1; i < cmdname_cnt; i++) {
+ if (strcmp(cmdname[i]->name, cmdname[i-1]->name)) {
+ cmdname[j++] = cmdname[i];
+ }
+ }
+
+ cmdname_cnt = j;
+}
+
static void pretty_print_string_list(struct cmdname **cmdname, int longest)
{
int cols = 1, rows;
@@ -71,12 +84,13 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
int max_cols = term_columns() - 1; /* don't print *on* the edge */
int i, j;
+ qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
+ uniq(cmdname);
+
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 < rows; i++) {
printf(" ");
@@ -93,19 +107,17 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
}
}
-static void list_commands(const char *exec_path, const char *prefix)
+static unsigned int list_commands_in_dir(const char *dir, const char *prefix)
{
unsigned int longest = 0;
int prefix_len = strlen(prefix);
- DIR *dir = opendir(exec_path);
+ DIR *dirp = opendir(dir);
struct dirent *de;
- if (!dir || chdir(exec_path)) {
- fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
- exit(1);
- }
+ if (!dirp || chdir(dir))
+ return 0;
- while ((de = readdir(dir)) != NULL) {
+ while ((de = readdir(dirp)) != NULL) {
struct stat st;
int entlen;
@@ -126,12 +138,37 @@ static void list_commands(const char *exec_path, const char *prefix)
add_cmdname(de->d_name + prefix_len, entlen);
}
- closedir(dir);
+ closedir(dirp);
- printf("git commands available in '%s'\n", exec_path);
- printf("----------------------------");
- mput_char('-', strlen(exec_path));
- putchar('\n');
+ return longest;
+}
+
+static void list_commands(const char *prefix)
+{
+ unsigned int longest = 0;
+ unsigned int len;
+ const char *env_path = getenv("PATH");
+ char *paths, *path, *colon;
+
+ if (!env_path) {
+ fprintf(stderr, "PATH not set\n");
+ exit(1);
+ }
+
+ path = paths = xstrdup(env_path);
+ while ((char *)1 != path) {
+ if ((colon = strchr(path, ':')))
+ *colon = 0;
+
+ len = list_commands_in_dir(path, prefix);
+ longest = MAX(longest, len);
+
+ path = colon + 1;
+ }
+ free(paths);
+
+ printf("available git commands\n");
+ printf("----------------------\n");
pretty_print_string_list(cmdname, longest);
putchar('\n');
}
@@ -188,7 +225,6 @@ int cmd_version(int argc, const char **argv, const char *prefix)
int cmd_help(int argc, const char **argv, const char *prefix)
{
const char *help_cmd = argc > 1 ? argv[1] : NULL;
- const char *exec_path = git_exec_path();
if (!help_cmd) {
printf("usage: %s\n\n", git_usage_string);
@@ -198,8 +234,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
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-");
+ list_commands("git-");
exit(0);
}
--
gitgui.0.8.4.11176.gd9205-dirty
next prev parent reply other threads:[~2007-10-25 3:37 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-25 3:37 [PATCH 1/7] "git" calls help_unknown_cmd(""); "git help" and "git help -a" return 0 Scott R Parish
2007-10-25 3:37 ` [PATCH 2/7] s/pattern/prefix/ in help's list_commands Scott R Parish
2007-10-25 3:37 ` [PATCH 3/7] "current_exec_path" is a misleading name, use "argv_exec_path" Signed-off-by: Scott R Parish <srp@srparish.net> Scott R Parish
2007-10-25 3:37 ` [PATCH 4/7] use only the PATH for exec'ing git commands Scott R Parish
2007-10-25 3:37 ` [PATCH 5/7] chdir() into list_commands() dir instead of building paths for stat() Scott R Parish
2007-10-25 3:37 ` Scott R Parish [this message]
2007-10-25 3:37 ` [PATCH 7/7] shell should call setup_path() instead of manually setting up its path Scott R Parish
2007-10-25 4:42 ` [PATCH 6/7] walk PATH to generate list of commands for "help -a" Junio C Hamano
2007-10-25 5:07 ` Scott Parish
2007-10-25 5:33 ` Junio C Hamano
2007-10-25 7:07 ` Scott Parish
2007-10-25 4:41 ` [PATCH 2/7] s/pattern/prefix/ in help's list_commands Junio C Hamano
2007-10-25 4:53 ` Scott Parish
2007-10-25 6:30 ` [PATCH 2/7] remove unused/unneeded "pattern" argument of list_commands Scott R Parish
2007-10-25 6:32 ` [PATCH 5/7] chdir() into list_commands() dir instead of building paths for stat() Scott R Parish
2007-10-25 4:40 ` [PATCH 1/7] "git" calls help_unknown_cmd(""); "git help" and "git help -a" return 0 Junio C Hamano
2007-10-25 4:52 ` Scott Parish
2007-10-26 23:03 ` Junio C Hamano
2007-10-27 7:16 ` Scott Parish
-- strict thread matches above, loose matches on Subject: below --
2007-10-27 8:36 [PATCH 1/7] "git" returns 1; " Scott R Parish
2007-10-27 8:36 ` [PATCH 2/7] remove unused/unneeded "pattern" argument of list_commands Scott R Parish
2007-10-27 8:36 ` [PATCH 3/7] "current_exec_path" is a misleading name, use "argv_exec_path" Scott R Parish
2007-10-27 8:36 ` [PATCH 4/7] list_commands(): simplify code by using chdir() Scott R Parish
2007-10-27 8:36 ` [PATCH 5/7] use only the $PATH for exec'ing git commands Scott R Parish
2007-10-27 8:36 ` [PATCH 6/7] walk $PATH to generate list of commands for "help -a" Scott R Parish
2007-10-28 6:18 ` Junio C Hamano
2007-10-28 9:45 ` Scott Parish
2007-10-28 10:07 ` Junio C Hamano
2007-10-28 11:15 ` Scott Parish
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1193283437-1706-6-git-send-email-srp@srparish.net \
--to=srp@srparish.net \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).