4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
10 #include <sys/ioctl.h>
12 /* most GUI terminals set COLUMNS (although some don't export it) */
13 static int term_columns(void)
15 char *col_string
= getenv("COLUMNS");
18 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
24 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
34 static inline void mput_char(char c
, unsigned int num
)
40 static struct cmdname
{
44 static int cmdname_alloc
, cmdname_cnt
;
46 static void add_cmdname(const char *name
, int len
)
49 if (cmdname_alloc
<= cmdname_cnt
) {
50 cmdname_alloc
= cmdname_alloc
+ 200;
51 cmdname
= xrealloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
53 ent
= xmalloc(sizeof(*ent
) + len
);
55 memcpy(ent
->name
, name
, len
);
57 cmdname
[cmdname_cnt
++] = ent
;
60 static int cmdname_compare(const void *a_
, const void *b_
)
62 struct cmdname
*a
= *(struct cmdname
**)a_
;
63 struct cmdname
*b
= *(struct cmdname
**)b_
;
64 return strcmp(a
->name
, b
->name
);
67 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
70 int space
= longest
+ 1; /* min 1 SP between words */
71 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
75 cols
= max_cols
/ space
;
76 rows
= (cmdname_cnt
+ cols
- 1) / cols
;
78 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
80 for (i
= 0; i
< rows
; i
++) {
83 for (j
= 0; j
< cols
; j
++) {
88 if (j
== cols
-1 || n
+ rows
>= cmdname_cnt
)
90 printf("%-*s", size
, cmdname
[n
]->name
);
96 static void list_commands(const char *exec_path
)
98 unsigned int longest
= 0;
99 const char *prefix
= "git-";
100 int prefix_len
= strlen(prefix
);
101 DIR *dir
= opendir(exec_path
);
104 if (!dir
|| chdir(exec_path
)) {
105 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
109 while ((de
= readdir(dir
)) != NULL
) {
113 if (prefixcmp(de
->d_name
, prefix
))
116 if (stat(de
->d_name
, &st
) || /* stat, not lstat */
117 !S_ISREG(st
.st_mode
) ||
118 !(st
.st_mode
& S_IXUSR
))
121 entlen
= strlen(de
->d_name
) - prefix_len
;
122 if (has_extension(de
->d_name
, ".exe"))
125 if (longest
< entlen
)
128 add_cmdname(de
->d_name
+ prefix_len
, entlen
);
132 printf("git commands available in '%s'\n", exec_path
);
133 printf("----------------------------");
134 mput_char('-', strlen(exec_path
));
136 pretty_print_string_list(cmdname
, longest
);
140 void list_common_cmds_help(void)
144 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
145 if (longest
< strlen(common_cmds
[i
].name
))
146 longest
= strlen(common_cmds
[i
].name
);
149 puts("The most commonly used git commands are:");
150 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
151 printf(" %s ", common_cmds
[i
].name
);
152 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
153 puts(common_cmds
[i
].help
);
155 puts("(use 'git help -a' to get a list of all installed git commands)");
158 static void show_man_page(const char *git_cmd
)
162 if (!prefixcmp(git_cmd
, "git"))
165 int page_len
= strlen(git_cmd
) + 4;
166 char *p
= xmalloc(page_len
+ 1);
168 strcpy(p
+ 4, git_cmd
);
173 execlp("man", "man", page
, NULL
);
176 void help_unknown_cmd(const char *cmd
)
178 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
182 int cmd_version(int argc
, const char **argv
, const char *prefix
)
184 printf("git version %s\n", git_version_string
);
188 int cmd_help(int argc
, const char **argv
, const char *prefix
)
190 const char *help_cmd
= argc
> 1 ? argv
[1] : NULL
;
191 const char *exec_path
= git_exec_path();
194 printf("usage: %s\n\n", git_usage_string
);
195 list_common_cmds_help();
199 else if (!strcmp(help_cmd
, "--all") || !strcmp(help_cmd
, "-a")) {
200 printf("usage: %s\n\n", git_usage_string
);
202 list_commands(exec_path
);
207 show_man_page(help_cmd
);