4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
11 /* most GUI terminals set COLUMNS (although some don't export it) */
12 static int term_columns(void)
14 char *col_string
= getenv("COLUMNS");
17 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
23 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
33 static inline void mput_char(char c
, unsigned int num
)
39 static struct cmdnames
{
46 } main_cmds
, other_cmds
;
48 static void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
50 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
);
53 memcpy(ent
->name
, name
, len
);
56 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
57 cmds
->names
[cmds
->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 uniq(struct cmdnames
*cmds
)
74 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
75 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
76 cmds
->names
[j
++] = cmds
->names
[i
];
81 static void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
87 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
88 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
90 cmds
->names
[cj
++] = cmds
->names
[ci
++];
97 while (ci
< cmds
->cnt
)
98 cmds
->names
[cj
++] = cmds
->names
[ci
++];
103 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
106 int space
= longest
+ 1; /* min 1 SP between words */
107 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
110 if (space
< max_cols
)
111 cols
= max_cols
/ space
;
112 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
114 for (i
= 0; i
< rows
; i
++) {
117 for (j
= 0; j
< cols
; j
++) {
118 int n
= j
* rows
+ i
;
122 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
124 printf("%-*s", size
, cmds
->names
[n
]->name
);
130 static unsigned int list_commands_in_dir(struct cmdnames
*cmds
,
133 unsigned int longest
= 0;
134 const char *prefix
= "git-";
135 int prefix_len
= strlen(prefix
);
136 DIR *dir
= opendir(path
);
139 if (!dir
|| chdir(path
))
142 while ((de
= readdir(dir
)) != NULL
) {
146 if (prefixcmp(de
->d_name
, prefix
))
149 if (stat(de
->d_name
, &st
) || /* stat, not lstat */
150 !S_ISREG(st
.st_mode
) ||
151 !(st
.st_mode
& S_IXUSR
))
154 entlen
= strlen(de
->d_name
) - prefix_len
;
155 if (has_extension(de
->d_name
, ".exe"))
158 if (longest
< entlen
)
161 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
168 static void list_commands(void)
170 unsigned int longest
= 0;
172 const char *env_path
= getenv("PATH");
173 char *paths
, *path
, *colon
;
174 const char *exec_path
= git_exec_path();
177 longest
= list_commands_in_dir(&main_cmds
, exec_path
);
180 fprintf(stderr
, "PATH not set\n");
184 path
= paths
= xstrdup(env_path
);
186 if ((colon
= strchr(path
, ':')))
189 len
= list_commands_in_dir(&other_cmds
, path
);
199 qsort(main_cmds
.names
, main_cmds
.cnt
,
200 sizeof(*main_cmds
.names
), cmdname_compare
);
203 qsort(other_cmds
.names
, other_cmds
.cnt
,
204 sizeof(*other_cmds
.names
), cmdname_compare
);
206 exclude_cmds(&other_cmds
, &main_cmds
);
209 printf("available git commands in '%s'\n", exec_path
);
210 printf("----------------------------");
211 mput_char('-', strlen(exec_path
));
213 pretty_print_string_list(&main_cmds
, longest
);
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
);
225 void list_common_cmds_help(void)
229 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
230 if (longest
< strlen(common_cmds
[i
].name
))
231 longest
= strlen(common_cmds
[i
].name
);
234 puts("The most commonly used git commands are:");
235 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
236 printf(" %s ", common_cmds
[i
].name
);
237 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
238 puts(common_cmds
[i
].help
);
242 static const char *cmd_to_page(const char *git_cmd
)
246 else if (!prefixcmp(git_cmd
, "git"))
249 int page_len
= strlen(git_cmd
) + 4;
250 char *p
= xmalloc(page_len
+ 1);
252 strcpy(p
+ 4, git_cmd
);
258 static void setup_man_path(void)
260 struct strbuf new_path
;
261 const char *old_path
= getenv("MANPATH");
263 strbuf_init(&new_path
, 0);
265 /* We should always put ':' after our path. If there is no
266 * old_path, the ':' at the end will let 'man' to try
267 * system-wide paths after ours to find the manual page. If
268 * there is old_path, we need ':' as delimiter. */
269 strbuf_addstr(&new_path
, GIT_MAN_PATH
);
270 strbuf_addch(&new_path
, ':');
272 strbuf_addstr(&new_path
, old_path
);
274 setenv("MANPATH", new_path
.buf
, 1);
276 strbuf_release(&new_path
);
279 static void show_man_page(const char *git_cmd
)
281 const char *page
= cmd_to_page(git_cmd
);
283 execlp("man", "man", page
, NULL
);
286 static void show_info_page(const char *git_cmd
)
288 const char *page
= cmd_to_page(git_cmd
);
289 setenv("INFOPATH", GIT_INFO_PATH
, 1);
290 execlp("info", "info", "gitman", page
, NULL
);
293 static void show_html_page(const char *git_cmd
)
295 const char *page
= cmd_to_page(git_cmd
);
296 execl_git_cmd("browse-help", page
, NULL
);
299 void help_unknown_cmd(const char *cmd
)
301 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
305 int cmd_version(int argc
, const char **argv
, const char *prefix
)
307 printf("git version %s\n", git_version_string
);
311 int cmd_help(int argc
, const char **argv
, const char *prefix
)
313 const char *help_cmd
= argv
[1];
316 printf("usage: %s\n\n", git_usage_string
);
317 list_common_cmds_help();
321 if (!strcmp(help_cmd
, "--all") || !strcmp(help_cmd
, "-a")) {
322 printf("usage: %s\n\n", git_usage_string
);
326 else if (!strcmp(help_cmd
, "--web") || !strcmp(help_cmd
, "-w")) {
327 show_html_page(argc
> 2 ? argv
[2] : NULL
);
330 else if (!strcmp(help_cmd
, "--info") || !strcmp(help_cmd
, "-i")) {
331 show_info_page(argc
> 2 ? argv
[2] : NULL
);
335 show_man_page(help_cmd
);