git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Scott R Parish <srp@srparish.net>
To: git@vger.kernel.org
Subject: [PATCH] "git help -a" should search all exec_paths and PATH
Date: Sun, 21 Oct 2007 14:48:46 -0700	[thread overview]
Message-ID: <20071021214846.GI16291@srparish.net> (raw)

Currently "git help -a" only searches in the highest priority exec_path,
meaning at worst, nothing is listed if the git commands are only available
from the PATH. It also makes git slightly less extensible.

To fix this, help.c is modified to search in all the exec_paths and PATH
for potential git commands. So that it has access to all the exec_paths,
exec_cmd.c now exposes the various paths. "current_exec_path" is renamed
as its name is misleading.

Signed-off-by: Scott R Parish <srp@srparish.net>
---
 exec_cmd.c |   26 +++++++++++---
 exec_cmd.h |    3 ++
 git.c      |    2 +-
 help.c     |  103 +++++++++++++++++++++++++++++++++++++++++++-----------------
 4 files changed, 98 insertions(+), 36 deletions(-)

diff --git a/exec_cmd.c b/exec_cmd.c
index 374ffc9..2c787a4 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -5,21 +5,35 @@
 
 extern char **environ;
 static const char *builtin_exec_path = GIT_EXEC_PATH;
-static const char *current_exec_path;
+static const char *argv_exec_path;
 
-void git_set_exec_path(const char *exec_path)
+void git_set_argv_exec_path(const char *exec_path)
 {
-	current_exec_path = exec_path;
+	argv_exec_path = exec_path;
 }
 
+const char *git_argv_exec_path(void)
+{
+	return argv_exec_path;
+}
+
+const char *git_builtin_exec_path(void)
+{
+	return builtin_exec_path;
+}
+
+const char *git_env_exec_path(void)
+{
+	return getenv(EXEC_PATH_ENVIRONMENT); 
+}
 
 /* Returns the highest-priority, location to look for git programs. */
 const char *git_exec_path(void)
 {
 	const char *env;
 
-	if (current_exec_path)
-		return current_exec_path;
+	if (argv_exec_path)
+		return argv_exec_path;
 
 	env = getenv(EXEC_PATH_ENVIRONMENT);
 	if (env && *env) {
@@ -34,7 +48,7 @@ int execv_git_cmd(const char **argv)
 {
 	char git_command[PATH_MAX + 1];
 	int i;
-	const char *paths[] = { current_exec_path,
+	const char *paths[] = { argv_exec_path,
 				getenv(EXEC_PATH_ENVIRONMENT),
 				builtin_exec_path,
 				"" };
diff --git a/exec_cmd.h b/exec_cmd.h
index 849a839..315fe83 100644
--- a/exec_cmd.h
+++ b/exec_cmd.h
@@ -3,6 +3,9 @@
 
 extern void git_set_exec_path(const char *exec_path);
 extern const char* git_exec_path(void);
+extern const char* git_argv_exec_path(void);
+extern const char* git_builtin_exec_path(void);
+extern const char* git_env_exec_path(void);
 extern int execv_git_cmd(const char **argv); /* NULL terminated */
 extern int execl_git_cmd(const char *cmd, ...);
 
diff --git a/git.c b/git.c
index 853e66c..b67fb17 100644
--- a/git.c
+++ b/git.c
@@ -51,7 +51,7 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)
 		if (!prefixcmp(cmd, "--exec-path")) {
 			cmd += 11;
 			if (*cmd == '=')
-				git_set_exec_path(cmd + 1);
+				git_set_argv_exec_path(cmd + 1);
 			else {
 				puts(git_exec_path());
 				exit(0);
diff --git a/help.c b/help.c
index b0d2dd4..85b2853 100644
--- a/help.c
+++ b/help.c
@@ -93,37 +93,27 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
 	}
 }
 
-static void list_commands(const char *exec_path, const char *pattern)
+static unsigned int list_commands_in_dir(const char *dir, const char *prefix)
 {
 	unsigned int longest = 0;
-	char path[PATH_MAX];
-	int dirlen;
-	DIR *dir = opendir(exec_path);
+	int start_dir = open(".", O_RDONLY, 0);
+	DIR *dirp = opendir(dir);
 	struct dirent *de;
 
-	if (!dir) {
-		fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
-		exit(1);
+	if (!dirp || chdir(dir)) {
+		fchdir(start_dir);
+		close(start_dir);
+		return 0;
 	}
 
-	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) {
+	while ((de = readdir(dirp)) != NULL) {
 		struct stat st;
 		int entlen;
-
-		if (prefixcmp(de->d_name, "git-"))
+			
+		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;
@@ -137,12 +127,67 @@ static void list_commands(const char *exec_path, const char *pattern)
 
 		add_cmdname(de->d_name + 4, entlen-4);
 	}
-	closedir(dir);
 
-	printf("git commands available in '%s'\n", exec_path);
-	printf("----------------------------");
-	mput_char('-', strlen(exec_path));
-	putchar('\n');
+	closedir(dirp);
+	fchdir(start_dir);
+	close(start_dir);
+
+	return longest;
+}
+
+static unsigned int list_commands_in_PATH(const char *prefix)
+{
+	unsigned int longest = 0;
+	unsigned int len;
+	const char *env_path = getenv("PATH");
+	char *paths, *path, *colon;
+       
+	if (!env_path)
+		return longest;
+
+	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);
+	return longest;
+}
+
+static void list_commands(const char *prefix)
+{
+	unsigned int longest = 0;
+	unsigned int len;
+	const char *paths[] = { git_argv_exec_path(),
+				git_env_exec_path(),
+				git_builtin_exec_path(),
+				"" };
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(paths); i++) {
+		if (!paths[i])
+			continue;
+
+		if (!*paths[i]) {
+			/* try PATH */
+			len = list_commands_in_PATH(prefix);
+			longest = MAX(longest, len);
+		}
+		else {
+			len = list_commands_in_dir(paths[i], prefix);
+			longest = MAX(longest, len);
+		}
+	}
+
+	printf("available git commands\n");
+	printf("----------------------\n");
 	pretty_print_string_list(cmdname, longest - 4);
 	putchar('\n');
 }
@@ -158,7 +203,7 @@ static void list_common_cmds_help(void)
 
 	puts("The most commonly used git commands are:");
 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
-		printf("   %s   ", common_cmds[i].name);
+		printf("   %s	", common_cmds[i].name);
 		mput_char(' ', longest - strlen(common_cmds[i].name));
 		puts(common_cmds[i].help);
 	}
@@ -210,7 +255,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);
 	}
 
-- 
1.5.3.4.209.g5d1ce-dirty

             reply	other threads:[~2007-10-21 21:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-21 21:48 Scott R Parish [this message]
2007-10-21 22:25 ` [PATCH] "git help -a" should search all exec_paths and PATH Johannes Schindelin
2007-10-22  0:54   ` Scott Parish
2007-10-22  5:30 ` Shawn O. Pearce
2007-10-22  6:32   ` Scott Parish
2007-10-22  6:39     ` Shawn O. Pearce
2007-10-22  6:06 ` Johannes Sixt

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=20071021214846.GI16291@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).