include $PATH in generating list of commands for "help -a"
Git had previously been using the $PATH for scripts--a previous patch moved exec'ed commands to also use the $PATH. For consistency "help -a" should also list commands in the $PATH. The main commands are still listed from the git_exec_path(), but the $PATH is walked and other git commands (probably extensions) are listed. Signed-off-by: Scott R Parish <srp@srparish.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Scott R Parish committed
Oct 28, 2007 at 20:30 UTC
1eb056905a2956ca9fabd2edcce05c0a29ce64b1
1 file changed
+120
-37
help.c
+120
-37
@@ -37,24 +37,25 @@ static inline void mput_char(char c, unsigned int num)
37
putchar(c);
38
}
39
40
-static struct cmdname {
41
- size_t len;
42
- char name[1];
43
-} **cmdname;
44
-static int cmdname_alloc, cmdname_cnt;
45
-
46
-static void add_cmdname(const char *name, int len)
40
+static struct cmdnames {
41
+ int alloc;
42
+ int cnt;
43
+ struct cmdname {
44
+ size_t len;
45
+ char name[1];
46
+ } **names;
47
+} main_cmds, other_cmds;
48
+
49
+static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
50
{
48
- struct cmdname *ent;
49
- if (cmdname_alloc <= cmdname_cnt) {
50
- cmdname_alloc = cmdname_alloc + 200;
51
- cmdname = xrealloc(cmdname, cmdname_alloc * sizeof(*cmdname));
52
- }
53
- ent = xmalloc(sizeof(*ent) + len);
51
+ struct cmdname *ent = xmalloc(sizeof(*ent) + len);
52
+
53
ent->len = len;
54
memcpy(ent->name, name, len);
55
ent->name[len] = 0;
57
- cmdname[cmdname_cnt++] = ent;
56
+
57
+ ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
58
+ cmds->names[cmds->cnt++] = ent;
59
}
60
61
static int cmdname_compare(const void *a_, const void *b_)
@@ -64,7 +65,42 @@ static int cmdname_compare(const void *a_, const void *b_)
65
return strcmp(a->name, b->name);
66
}
67
67
-static void pretty_print_string_list(struct cmdname **cmdname, int longest)
68
+static void uniq(struct cmdnames *cmds)
69
+{
70
+ int i, j;
71
+
72
+ if (!cmds->cnt)
73
+ return;
74
+
75
+ for (i = j = 1; i < cmds->cnt; i++)
76
+ if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
77
+ cmds->names[j++] = cmds->names[i];
78
+
79
+ cmds->cnt = j;
80
+}
81
+
82
+static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) {
83
+ int ci, cj, ei;
84
+ int cmp;
85
+
86
+ ci = cj = ei = 0;
87
+ while (ci < cmds->cnt && ei < excludes->cnt) {
88
+ cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
89
+ if (cmp < 0)
90
+ cmds->names[cj++] = cmds->names[ci++];
91
+ else if (cmp == 0)
92
+ ci++, ei++;
93
+ else if (cmp > 0)
94
+ ei++;
95
+ }
96
+
97
+ while (ci < cmds->cnt)
98
+ cmds->names[cj++] = cmds->names[ci++];
99
+
100
+ cmds->cnt = cj;
101
+}
102
+
103
+static void pretty_print_string_list(struct cmdnames *cmds, int longest)
104
{
105
int cols = 1, rows;
106
int space = longest + 1; /* min 1 SP between words */
@@ -73,9 +109,7 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
109
110
if (space < max_cols)
111
cols = max_cols / space;
76
- rows = (cmdname_cnt + cols - 1) / cols;
77
-
78
- qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
112
+ rows = (cmds->cnt + cols - 1) / cols;
113
114
for (i = 0; i < rows; i++) {
115
printf(" ");
@@ -83,28 +117,27 @@ static void pretty_print_string_list(struct cmdname **cmdname, int longest)
117
for (j = 0; j < cols; j++) {
118
int n = j * rows + i;
119
int size = space;
86
- if (n >= cmdname_cnt)
120
+ if (n >= cmds->cnt)
121
break;
88
- if (j == cols-1 || n + rows >= cmdname_cnt)
122
+ if (j == cols-1 || n + rows >= cmds->cnt)
123
size = 1;
90
- printf("%-*s", size, cmdname[n]->name);
124
+ printf("%-*s", size, cmds->names[n]->name);
125
}
126
putchar('\n');
127
}
128
}
129
96
-static void list_commands(const char *exec_path)
130
+static unsigned int list_commands_in_dir(struct cmdnames *cmds,
131
+ const char *path)
132
{
133
unsigned int longest = 0;
134
const char *prefix = "git-";
135
int prefix_len = strlen(prefix);
101
- DIR *dir = opendir(exec_path);
136
+ DIR *dir = opendir(path);
137
struct dirent *de;
138
104
- if (!dir || chdir(exec_path)) {
105
- fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
106
- exit(1);
107
- }
139
+ if (!dir || chdir(path))
140
+ return 0;
141
142
while ((de = readdir(dir)) != NULL) {
143
struct stat st;
@@ -125,16 +158,68 @@ static void list_commands(const char *exec_path)
158
if (longest < entlen)
159
longest = entlen;
160
128
- add_cmdname(de->d_name + prefix_len, entlen);
161
+ add_cmdname(cmds, de->d_name + prefix_len, entlen);
162
}
163
closedir(dir);
164
132
- printf("git commands available in '%s'\n", exec_path);
133
- printf("----------------------------");
134
- mput_char('-', strlen(exec_path));
135
- putchar('\n');
136
- pretty_print_string_list(cmdname, longest);
137
- putchar('\n');
165
+ return longest;
166
+}
167
+
168
+static void list_commands(void)
169
+{
170
+ unsigned int longest = 0;
171
+ unsigned int len;
172
+ const char *env_path = getenv("PATH");
173
+ char *paths, *path, *colon;
174
+ const char *exec_path = git_exec_path();
175
+
176
+ if (exec_path)
177
+ longest = list_commands_in_dir(&main_cmds, exec_path);
178
+
179
+ if (!env_path) {
180
+ fprintf(stderr, "PATH not set\n");
181
+ exit(1);
182
+ }
183
+
184
+ path = paths = xstrdup(env_path);
185
+ while (1) {
186
+ if ((colon = strchr(path, ':')))
187
+ *colon = 0;
188
+
189
+ len = list_commands_in_dir(&other_cmds, path);
190
+ if (len > longest)
191
+ longest = len;
192
+
193
+ if (!colon)
194
+ break;
195
+ path = colon + 1;
196
+ }
197
+ free(paths);
198
+
199
+ qsort(main_cmds.names, main_cmds.cnt,
200
+ sizeof(*main_cmds.names), cmdname_compare);
201
+ uniq(&main_cmds);
202
+
203
+ qsort(other_cmds.names, other_cmds.cnt,
204
+ sizeof(*other_cmds.names), cmdname_compare);
205
+ uniq(&other_cmds);
206
+ exclude_cmds(&other_cmds, &main_cmds);
207
+
208
+ if (main_cmds.cnt) {
209
+ printf("available git commands in '%s'\n", exec_path);
210
+ printf("----------------------------");
211
+ mput_char('-', strlen(exec_path));
212
+ putchar('\n');
213
+ pretty_print_string_list(&main_cmds, longest);
214
+ putchar('\n');
215
+ }
216
+
217
+ if (other_cmds.cnt) {
218
+ printf("git commands available from elsewhere on your $PATH\n");
219
+ printf("---------------------------------------------------\n");
220
+ pretty_print_string_list(&other_cmds, longest);
221
+ putchar('\n');
222
+ }
223
}
224
225
void list_common_cmds_help(void)
@@ -188,7 +273,6 @@ int cmd_version(int argc, const char **argv, const char *prefix)
273
int cmd_help(int argc, const char **argv, const char *prefix)
274
{
275
const char *help_cmd = argc > 1 ? argv[1] : NULL;
191
- const char *exec_path = git_exec_path();
276
277
if (!help_cmd) {
278
printf("usage: %s\n\n", git_usage_string);
@@ -198,8 +282,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
282
283
else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
284
printf("usage: %s\n\n", git_usage_string);
201
- if(exec_path)
202
- list_commands(exec_path);
285
+ list_commands();
286
exit(0);
287
}
288