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 cmdnames {
47 } main_cmds, other_cmds;
49 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
51 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
54 memcpy(ent->name, name, len);
57 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
58 cmds->names[cmds->cnt++] = ent;
61 static int cmdname_compare(const void *a_, const void *b_)
63 struct cmdname *a = *(struct cmdname **)a_;
64 struct cmdname *b = *(struct cmdname **)b_;
65 return strcmp(a->name, b->name);
68 static void uniq(struct cmdnames *cmds)
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];
82 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
88 while (ci < cmds->cnt && ei < excludes->cnt) {
89 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
91 cmds->names[cj++] = cmds->names[ci++];
98 while (ci < cmds->cnt)
99 cmds->names[cj++] = cmds->names[ci++];
104 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
107 int space = longest + 1; /* min 1 SP between words */
108 int max_cols = term_columns() - 1; /* don't print *on* the edge */
111 if (space < max_cols)
112 cols = max_cols / space;
113 rows = (cmds->cnt + cols - 1) / cols;
115 for (i = 0; i < rows; i++) {
118 for (j = 0; j < cols; j++) {
119 int n = j * rows + i;
123 if (j == cols-1 || n + rows >= cmds->cnt)
125 printf("%-*s", size, cmds->names[n]->name);
131 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
134 unsigned int longest = 0;
135 const char *prefix = "git-";
136 int prefix_len = strlen(prefix);
137 DIR *dir = opendir(path);
140 if (!dir || chdir(path))
143 while ((de = readdir(dir)) != NULL) {
147 if (prefixcmp(de->d_name, prefix))
150 if (stat(de->d_name, &st) || /* stat, not lstat */
151 !S_ISREG(st.st_mode) ||
152 !(st.st_mode & S_IXUSR))
155 entlen = strlen(de->d_name) - prefix_len;
156 if (has_extension(de->d_name, ".exe"))
159 if (longest < entlen)
162 add_cmdname(cmds, de->d_name + prefix_len, entlen);
169 static void list_commands(void)
171 unsigned int longest = 0;
173 const char *env_path = getenv("PATH");
174 char *paths, *path, *colon;
175 const char *exec_path = git_exec_path();
178 longest = list_commands_in_dir(&main_cmds, exec_path);
181 fprintf(stderr, "PATH not set\n");
185 path = paths = xstrdup(env_path);
187 if ((colon = strchr(path, ':')))
190 len = list_commands_in_dir(&other_cmds, path);
200 qsort(main_cmds.names, main_cmds.cnt,
201 sizeof(*main_cmds.names), cmdname_compare);
204 qsort(other_cmds.names, other_cmds.cnt,
205 sizeof(*other_cmds.names), cmdname_compare);
207 exclude_cmds(&other_cmds, &main_cmds);
210 printf("available git commands in '%s'\n", exec_path);
211 printf("----------------------------");
212 mput_char('-', strlen(exec_path));
214 pretty_print_string_list(&main_cmds, longest);
218 if (other_cmds.cnt) {
219 printf("git commands available from elsewhere on your $PATH\n");
220 printf("---------------------------------------------------\n");
221 pretty_print_string_list(&other_cmds, longest);
226 void list_common_cmds_help(void)
230 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
231 if (longest < strlen(common_cmds[i].name))
232 longest = strlen(common_cmds[i].name);
235 puts("The most commonly used git commands are:");
236 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
237 printf(" %s ", common_cmds[i].name);
238 mput_char(' ', longest - strlen(common_cmds[i].name));
239 puts(common_cmds[i].help);
241 puts("(use 'git help -a' to get a list of all installed git commands)");
244 static void show_man_page(const char *git_cmd)
248 if (!prefixcmp(git_cmd, "git"))
251 int page_len = strlen(git_cmd) + 4;
252 char *p = xmalloc(page_len + 1);
254 strcpy(p + 4, git_cmd);
259 execlp("man", "man", page, NULL);
262 void help_unknown_cmd(const char *cmd)
264 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
268 int cmd_version(int argc, const char **argv, const char *prefix)
270 printf("git version %s\n", git_version_string);
274 int cmd_help(int argc, const char **argv, const char *prefix)
276 const char *help_cmd = argc > 1 ? argv[1] : NULL;
279 printf("usage: %s\n\n", git_usage_string);
280 list_common_cmds_help();
284 else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
285 printf("usage: %s\n\n", git_usage_string);
291 show_man_page(help_cmd);