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
)) {
36 fprintf(stderr
, "git: out of memory\n");
40 static inline void mput_char(char c
, unsigned int num
)
46 static struct cmdname
{
50 static int cmdname_alloc
, cmdname_cnt
;
52 static void add_cmdname(const char *name
, int len
)
55 if (cmdname_alloc
<= cmdname_cnt
) {
56 cmdname_alloc
= cmdname_alloc
+ 200;
57 cmdname
= realloc(cmdname
, cmdname_alloc
* sizeof(*cmdname
));
61 ent
= malloc(sizeof(*ent
) + len
);
65 memcpy(ent
->name
, name
, len
);
67 cmdname
[cmdname_cnt
++] = ent
;
70 static int cmdname_compare(const void *a_
, const void *b_
)
72 struct cmdname
*a
= *(struct cmdname
**)a_
;
73 struct cmdname
*b
= *(struct cmdname
**)b_
;
74 return strcmp(a
->name
, b
->name
);
77 static void pretty_print_string_list(struct cmdname
**cmdname
, int longest
)
80 int space
= longest
+ 1; /* min 1 SP between words */
81 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
85 cols
= max_cols
/ space
;
86 rows
= (cmdname_cnt
+ cols
- 1) / cols
;
88 qsort(cmdname
, cmdname_cnt
, sizeof(*cmdname
), cmdname_compare
);
90 for (i
= 0; i
< rows
; i
++) {
93 for (j
= 0; j
< cols
; j
++) {
98 if (j
== cols
-1 || n
+ rows
>= cmdname_cnt
)
100 printf("%-*s", size
, cmdname
[n
]->name
);
106 static void list_commands(const char *exec_path
, const char *pattern
)
108 unsigned int longest
= 0;
111 DIR *dir
= opendir(exec_path
);
115 fprintf(stderr
, "git: '%s': %s\n", exec_path
, strerror(errno
));
119 dirlen
= strlen(exec_path
);
120 if (PATH_MAX
- 20 < dirlen
) {
121 fprintf(stderr
, "git: insanely long exec-path '%s'\n",
126 memcpy(path
, exec_path
, dirlen
);
127 path
[dirlen
++] = '/';
129 while ((de
= readdir(dir
)) != NULL
) {
133 if (strncmp(de
->d_name
, "git-", 4))
135 strcpy(path
+dirlen
, de
->d_name
);
136 if (stat(path
, &st
) || /* stat, not lstat */
137 !S_ISREG(st
.st_mode
) ||
138 !(st
.st_mode
& S_IXUSR
))
141 entlen
= strlen(de
->d_name
);
142 if (has_extension(de
->d_name
, ".exe"))
145 if (longest
< entlen
)
148 add_cmdname(de
->d_name
+ 4, entlen
-4);
152 printf("git commands available in '%s'\n", exec_path
);
153 printf("----------------------------");
154 mput_char('-', strlen(exec_path
));
156 pretty_print_string_list(cmdname
, longest
- 4);
160 static void list_common_cmds_help(void)
164 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
165 if (longest
< strlen(common_cmds
[i
].name
))
166 longest
= strlen(common_cmds
[i
].name
);
169 puts("The most commonly used git commands are:");
170 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
171 printf(" %s ", common_cmds
[i
].name
);
172 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
173 puts(common_cmds
[i
].help
);
175 puts("(use 'git help -a' to get a list of all installed git commands)");
178 static void show_man_page(const char *git_cmd
)
182 if (!strncmp(git_cmd
, "git", 3))
185 int page_len
= strlen(git_cmd
) + 4;
186 char *p
= xmalloc(page_len
+ 1);
188 strcpy(p
+ 4, git_cmd
);
193 execlp("man", "man", page
, NULL
);
196 void help_unknown_cmd(const char *cmd
)
198 printf("git: '%s' is not a git-command\n\n", cmd
);
199 list_common_cmds_help();
203 int cmd_version(int argc
, const char **argv
, const char *prefix
)
205 printf("git version %s\n", git_version_string
);
209 int cmd_help(int argc
, const char **argv
, const char *prefix
)
211 const char *help_cmd
= argc
> 1 ? argv
[1] : NULL
;
212 const char *exec_path
= git_exec_path();
215 printf("usage: %s\n\n", git_usage_string
);
216 list_common_cmds_help();
220 else if (!strcmp(help_cmd
, "--all") || !strcmp(help_cmd
, "-a")) {
221 printf("usage: %s\n\n", git_usage_string
);
223 list_commands(exec_path
, "git-*");
228 show_man_page(help_cmd
);