builtin-help: make some internal functions available to other builtins
Make load_command_list() capable of filtering for a given prefix and loading into a pair of "struct cmdnames" supplied by the caller. Make the static add_cmdname(), exclude_cmds() and is_in_cmdlist() functions non-static. Make list_commands() accept a custom title, and work from a pair of "struct cmdnames" supplied by the caller. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Miklos Vajna committed
Jul 30, 2008 at 01:16 UTC
940208a771066229bc6a486f6a058e332b71cfe4
3 files changed
+63
-38
Makefile
+1
@@ -355,6 +355,7 @@ LIB_H += git-compat-util.h
355
LIB_H += graph.h
356
LIB_H += grep.h
357
LIB_H += hash.h
358
+LIB_H += help.h
359
LIB_H += list-objects.h
360
LIB_H += ll-merge.h
361
LIB_H += log-tree.h
help.c
+39
-38
@@ -9,6 +9,7 @@
9
#include "common-cmds.h"
10
#include "parse-options.h"
11
#include "run-command.h"
12
+#include "help.h"
13
14
static struct man_viewer_list {
15
struct man_viewer_list *next;
@@ -300,18 +301,11 @@ static inline void mput_char(char c, unsigned int num)
301
putchar(c);
302
}
303
303
-static struct cmdnames {
304
- int alloc;
305
- int cnt;
306
- struct cmdname {
307
- size_t len;
308
- char name[1];
309
- } **names;
310
-} main_cmds, other_cmds;
304
+struct cmdnames main_cmds, other_cmds;
305
312
-static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
306
+void add_cmdname(struct cmdnames *cmds, const char *name, int len)
307
{
314
- struct cmdname *ent = xmalloc(sizeof(*ent) + len);
308
+ struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
309
310
ent->len = len;
311
memcpy(ent->name, name, len);
@@ -342,7 +336,7 @@ static void uniq(struct cmdnames *cmds)
336
cmds->cnt = j;
337
}
338
345
-static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
339
+void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
340
{
341
int ci, cj, ei;
342
int cmp;
@@ -418,11 +412,11 @@ static int is_executable(const char *name)
412
}
413
414
static unsigned int list_commands_in_dir(struct cmdnames *cmds,
421
- const char *path)
415
+ const char *path,
416
+ const char *prefix)
417
{
418
unsigned int longest = 0;
424
- const char *prefix = "git-";
425
- int prefix_len = strlen(prefix);
419
+ int prefix_len;
420
DIR *dir = opendir(path);
421
struct dirent *de;
422
struct strbuf buf = STRBUF_INIT;
@@ -430,6 +424,9 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
424
425
if (!dir)
426
return 0;
427
+ if (!prefix)
428
+ prefix = "git-";
429
+ prefix_len = strlen(prefix);
430
431
strbuf_addf(&buf, "%s/", path);
432
len = buf.len;
@@ -460,7 +457,9 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
457
return longest;
458
}
459
463
-static unsigned int load_command_list(void)
460
+unsigned int load_command_list(const char *prefix,
461
+ struct cmdnames *main_cmds,
462
+ struct cmdnames *other_cmds)
463
{
464
unsigned int longest = 0;
465
unsigned int len;
@@ -469,7 +468,7 @@ static unsigned int load_command_list(void)
468
const char *exec_path = git_exec_path();
469
470
if (exec_path)
472
- longest = list_commands_in_dir(&main_cmds, exec_path);
471
+ longest = list_commands_in_dir(main_cmds, exec_path, prefix);
472
473
if (!env_path) {
474
fprintf(stderr, "PATH not set\n");
@@ -481,7 +480,7 @@ static unsigned int load_command_list(void)
480
if ((colon = strchr(path, PATH_SEP)))
481
*colon = 0;
482
484
- len = list_commands_in_dir(&other_cmds, path);
483
+ len = list_commands_in_dir(other_cmds, path, prefix);
484
if (len > longest)
485
longest = len;
486
@@ -491,36 +490,38 @@ static unsigned int load_command_list(void)
490
}
491
free(paths);
492
494
- qsort(main_cmds.names, main_cmds.cnt,
495
- sizeof(*main_cmds.names), cmdname_compare);
496
- uniq(&main_cmds);
493
+ qsort(main_cmds->names, main_cmds->cnt,
494
+ sizeof(*main_cmds->names), cmdname_compare);
495
+ uniq(main_cmds);
496
498
- qsort(other_cmds.names, other_cmds.cnt,
499
- sizeof(*other_cmds.names), cmdname_compare);
500
- uniq(&other_cmds);
501
- exclude_cmds(&other_cmds, &main_cmds);
497
+ qsort(other_cmds->names, other_cmds->cnt,
498
+ sizeof(*other_cmds->names), cmdname_compare);
499
+ uniq(other_cmds);
500
+ exclude_cmds(other_cmds, main_cmds);
501
502
return longest;
503
}
504
506
-static void list_commands(void)
505
+void list_commands(const char *title, unsigned int longest,
506
+ struct cmdnames *main_cmds, struct cmdnames *other_cmds)
507
{
508
- unsigned int longest = load_command_list();
508
const char *exec_path = git_exec_path();
509
511
- if (main_cmds.cnt) {
512
- printf("available git commands in '%s'\n", exec_path);
513
- printf("----------------------------");
514
- mput_char('-', strlen(exec_path));
510
+ if (main_cmds->cnt) {
511
+ printf("available %s in '%s'\n", title, exec_path);
512
+ printf("----------------");
513
+ mput_char('-', strlen(title) + strlen(exec_path));
514
putchar('\n');
516
- pretty_print_string_list(&main_cmds, longest);
515
+ pretty_print_string_list(main_cmds, longest);
516
putchar('\n');
517
}
518
520
- if (other_cmds.cnt) {
521
- printf("git commands available from elsewhere on your $PATH\n");
522
- printf("---------------------------------------------------\n");
523
- pretty_print_string_list(&other_cmds, longest);
519
+ if (other_cmds->cnt) {
520
+ printf("%s available from elsewhere on your $PATH\n", title);
521
+ printf("---------------------------------------");
522
+ mput_char('-', strlen(title));
523
+ putchar('\n');
524
+ pretty_print_string_list(other_cmds, longest);
525
putchar('\n');
526
}
527
}
@@ -542,7 +543,7 @@ void list_common_cmds_help(void)
543
}
544
}
545
545
-static int is_in_cmdlist(struct cmdnames *c, const char *s)
546
+int is_in_cmdlist(struct cmdnames *c, const char *s)
547
{
548
int i;
549
for (i = 0; i < c->cnt; i++)
@@ -553,7 +554,6 @@ static int is_in_cmdlist(struct cmdnames *c, const char *s)
554
555
static int is_git_command(const char *s)
556
{
556
- load_command_list();
557
return is_in_cmdlist(&main_cmds, s) ||
558
is_in_cmdlist(&other_cmds, s);
559
}
@@ -698,8 +698,9 @@ int cmd_help(int argc, const char **argv, const char *prefix)
698
builtin_help_usage, 0);
699
700
if (show_all) {
701
+ unsigned int longest = load_command_list("git-", &main_cmds, &other_cmds);
702
printf("usage: %s\n\n", git_usage_string);
702
- list_commands();
703
+ list_commands("git commands", longest, &main_cmds, &other_cmds);
704
printf("%s\n", git_more_info_string);
705
return 0;
706
}
help.h
new
+23
@@ -0,0 +1,23 @@
1
+#ifndef HELP_H
2
+#define HELP_H
3
+
4
+struct cmdnames {
5
+ int alloc;
6
+ int cnt;
7
+ struct cmdname {
8
+ size_t len;
9
+ char name[FLEX_ARRAY];
10
+ } **names;
11
+};
12
+
13
+unsigned int load_command_list(const char *prefix,
14
+ struct cmdnames *main_cmds,
15
+ struct cmdnames *other_cmds);
16
+void add_cmdname(struct cmdnames *cmds, const char *name, int len);
17
+/* Here we require that excludes is a sorted list. */
18
+void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
19
+int is_in_cmdlist(struct cmdnames *c, const char *s);
20
+void list_commands(const char *title, unsigned int longest,
21
+ struct cmdnames *main_cmds, struct cmdnames *other_cmds);
22
+
23
+#endif /* HELP_H */