Git 2.13.7
[git.git] / help.c
blob9d2811efdabb55bd8ddd89e76a6d461801f7eced
1 #include "cache.h"
2 #include "builtin.h"
3 #include "exec_cmd.h"
4 #include "run-command.h"
5 #include "levenshtein.h"
6 #include "help.h"
7 #include "common-cmds.h"
8 #include "string-list.h"
9 #include "column.h"
10 #include "version.h"
11 #include "refs.h"
13 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
15 struct cmdname *ent;
16 FLEX_ALLOC_MEM(ent, name, name, len);
17 ent->len = len;
19 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
20 cmds->names[cmds->cnt++] = ent;
23 static void clean_cmdnames(struct cmdnames *cmds)
25 int i;
26 for (i = 0; i < cmds->cnt; ++i)
27 free(cmds->names[i]);
28 free(cmds->names);
29 cmds->cnt = 0;
30 cmds->alloc = 0;
33 static int cmdname_compare(const void *a_, const void *b_)
35 struct cmdname *a = *(struct cmdname **)a_;
36 struct cmdname *b = *(struct cmdname **)b_;
37 return strcmp(a->name, b->name);
40 static void uniq(struct cmdnames *cmds)
42 int i, j;
44 if (!cmds->cnt)
45 return;
47 for (i = j = 1; i < cmds->cnt; i++) {
48 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
49 free(cmds->names[i]);
50 else
51 cmds->names[j++] = cmds->names[i];
54 cmds->cnt = j;
57 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
59 int ci, cj, ei;
60 int cmp;
62 ci = cj = ei = 0;
63 while (ci < cmds->cnt && ei < excludes->cnt) {
64 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
65 if (cmp < 0)
66 cmds->names[cj++] = cmds->names[ci++];
67 else if (cmp == 0) {
68 ei++;
69 free(cmds->names[ci++]);
70 } else if (cmp > 0)
71 ei++;
74 while (ci < cmds->cnt)
75 cmds->names[cj++] = cmds->names[ci++];
77 cmds->cnt = cj;
80 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
82 struct string_list list = STRING_LIST_INIT_NODUP;
83 struct column_options copts;
84 int i;
86 for (i = 0; i < cmds->cnt; i++)
87 string_list_append(&list, cmds->names[i]->name);
89 * always enable column display, we only consult column.*
90 * about layout strategy and stuff
92 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
93 memset(&copts, 0, sizeof(copts));
94 copts.indent = " ";
95 copts.padding = 2;
96 print_columns(&list, colopts, &copts);
97 string_list_clear(&list, 0);
100 static void list_commands_in_dir(struct cmdnames *cmds,
101 const char *path,
102 const char *prefix)
104 DIR *dir = opendir(path);
105 struct dirent *de;
106 struct strbuf buf = STRBUF_INIT;
107 int len;
109 if (!dir)
110 return;
111 if (!prefix)
112 prefix = "git-";
114 strbuf_addf(&buf, "%s/", path);
115 len = buf.len;
117 while ((de = readdir(dir)) != NULL) {
118 const char *ent;
119 size_t entlen;
121 if (!skip_prefix(de->d_name, prefix, &ent))
122 continue;
124 strbuf_setlen(&buf, len);
125 strbuf_addstr(&buf, de->d_name);
126 if (!is_executable(buf.buf))
127 continue;
129 entlen = strlen(ent);
130 strip_suffix(ent, ".exe", &entlen);
132 add_cmdname(cmds, ent, entlen);
134 closedir(dir);
135 strbuf_release(&buf);
138 void load_command_list(const char *prefix,
139 struct cmdnames *main_cmds,
140 struct cmdnames *other_cmds)
142 const char *env_path = getenv("PATH");
143 const char *exec_path = git_exec_path();
145 if (exec_path) {
146 list_commands_in_dir(main_cmds, exec_path, prefix);
147 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
148 uniq(main_cmds);
151 if (env_path) {
152 char *paths, *path, *colon;
153 path = paths = xstrdup(env_path);
154 while (1) {
155 if ((colon = strchr(path, PATH_SEP)))
156 *colon = 0;
157 if (!exec_path || strcmp(path, exec_path))
158 list_commands_in_dir(other_cmds, path, prefix);
160 if (!colon)
161 break;
162 path = colon + 1;
164 free(paths);
166 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
167 uniq(other_cmds);
169 exclude_cmds(other_cmds, main_cmds);
172 void list_commands(unsigned int colopts,
173 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
175 if (main_cmds->cnt) {
176 const char *exec_path = git_exec_path();
177 printf_ln(_("available git commands in '%s'"), exec_path);
178 putchar('\n');
179 pretty_print_cmdnames(main_cmds, colopts);
180 putchar('\n');
183 if (other_cmds->cnt) {
184 printf_ln(_("git commands available from elsewhere on your $PATH"));
185 putchar('\n');
186 pretty_print_cmdnames(other_cmds, colopts);
187 putchar('\n');
191 static int cmd_group_cmp(const void *elem1, const void *elem2)
193 const struct cmdname_help *e1 = elem1;
194 const struct cmdname_help *e2 = elem2;
196 if (e1->group < e2->group)
197 return -1;
198 if (e1->group > e2->group)
199 return 1;
200 return strcmp(e1->name, e2->name);
203 void list_common_cmds_help(void)
205 int i, longest = 0;
206 int current_grp = -1;
208 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
209 if (longest < strlen(common_cmds[i].name))
210 longest = strlen(common_cmds[i].name);
213 QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
215 puts(_("These are common Git commands used in various situations:"));
217 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
218 if (common_cmds[i].group != current_grp) {
219 printf("\n%s\n", _(common_cmd_groups[common_cmds[i].group]));
220 current_grp = common_cmds[i].group;
223 printf(" %s ", common_cmds[i].name);
224 mput_char(' ', longest - strlen(common_cmds[i].name));
225 puts(_(common_cmds[i].help));
229 int is_in_cmdlist(struct cmdnames *c, const char *s)
231 int i;
232 for (i = 0; i < c->cnt; i++)
233 if (!strcmp(s, c->names[i]->name))
234 return 1;
235 return 0;
238 static int autocorrect;
239 static struct cmdnames aliases;
241 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
243 const char *p;
245 if (!strcmp(var, "help.autocorrect"))
246 autocorrect = git_config_int(var,value);
247 /* Also use aliases for command lookup */
248 if (skip_prefix(var, "alias.", &p))
249 add_cmdname(&aliases, p, strlen(p));
251 return git_default_config(var, value, cb);
254 static int levenshtein_compare(const void *p1, const void *p2)
256 const struct cmdname *const *c1 = p1, *const *c2 = p2;
257 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
258 int l1 = (*c1)->len;
259 int l2 = (*c2)->len;
260 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
263 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
265 int i;
266 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
268 for (i = 0; i < old->cnt; i++)
269 cmds->names[cmds->cnt++] = old->names[i];
270 free(old->names);
271 old->cnt = 0;
272 old->names = NULL;
275 /* An empirically derived magic number */
276 #define SIMILARITY_FLOOR 7
277 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
279 static const char bad_interpreter_advice[] =
280 N_("'%s' appears to be a git command, but we were not\n"
281 "able to execute it. Maybe git-%s is broken?");
283 const char *help_unknown_cmd(const char *cmd)
285 int i, n, best_similarity = 0;
286 struct cmdnames main_cmds, other_cmds;
288 memset(&main_cmds, 0, sizeof(main_cmds));
289 memset(&other_cmds, 0, sizeof(other_cmds));
290 memset(&aliases, 0, sizeof(aliases));
292 read_early_config(git_unknown_cmd_config, NULL);
294 load_command_list("git-", &main_cmds, &other_cmds);
296 add_cmd_list(&main_cmds, &aliases);
297 add_cmd_list(&main_cmds, &other_cmds);
298 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
299 uniq(&main_cmds);
301 /* This abuses cmdname->len for levenshtein distance */
302 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
303 int cmp = 0; /* avoid compiler stupidity */
304 const char *candidate = main_cmds.names[i]->name;
307 * An exact match means we have the command, but
308 * for some reason exec'ing it gave us ENOENT; probably
309 * it's a bad interpreter in the #! line.
311 if (!strcmp(candidate, cmd))
312 die(_(bad_interpreter_advice), cmd, cmd);
314 /* Does the candidate appear in common_cmds list? */
315 while (n < ARRAY_SIZE(common_cmds) &&
316 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
317 n++;
318 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
319 /* Yes, this is one of the common commands */
320 n++; /* use the entry from common_cmds[] */
321 if (starts_with(candidate, cmd)) {
322 /* Give prefix match a very good score */
323 main_cmds.names[i]->len = 0;
324 continue;
328 main_cmds.names[i]->len =
329 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
332 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
334 if (!main_cmds.cnt)
335 die(_("Uh oh. Your system reports no Git commands at all."));
337 /* skip and count prefix matches */
338 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
339 ; /* still counting */
341 if (main_cmds.cnt <= n) {
342 /* prefix matches with everything? that is too ambiguous */
343 best_similarity = SIMILARITY_FLOOR + 1;
344 } else {
345 /* count all the most similar ones */
346 for (best_similarity = main_cmds.names[n++]->len;
347 (n < main_cmds.cnt &&
348 best_similarity == main_cmds.names[n]->len);
349 n++)
350 ; /* still counting */
352 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
353 const char *assumed = main_cmds.names[0]->name;
354 main_cmds.names[0] = NULL;
355 clean_cmdnames(&main_cmds);
356 fprintf_ln(stderr,
357 _("WARNING: You called a Git command named '%s', "
358 "which does not exist."),
359 cmd);
360 if (autocorrect < 0)
361 fprintf_ln(stderr,
362 _("Continuing under the assumption that "
363 "you meant '%s'."),
364 assumed);
365 else {
366 fprintf_ln(stderr,
367 _("Continuing in %0.1f seconds, "
368 "assuming that you meant '%s'."),
369 (float)autocorrect/10.0, assumed);
370 sleep_millisec(autocorrect * 100);
372 return assumed;
375 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
377 if (SIMILAR_ENOUGH(best_similarity)) {
378 fprintf_ln(stderr,
379 Q_("\nThe most similar command is",
380 "\nThe most similar commands are",
381 n));
383 for (i = 0; i < n; i++)
384 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
387 exit(1);
390 int cmd_version(int argc, const char **argv, const char *prefix)
393 * The format of this string should be kept stable for compatibility
394 * with external projects that rely on the output of "git version".
396 printf("git version %s\n", git_version_string);
397 while (*++argv) {
398 if (!strcmp(*argv, "--build-options")) {
399 printf("sizeof-long: %d\n", (int)sizeof(long));
400 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
403 return 0;
406 struct similar_ref_cb {
407 const char *base_ref;
408 struct string_list *similar_refs;
411 static int append_similar_ref(const char *refname, const struct object_id *oid,
412 int flags, void *cb_data)
414 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
415 char *branch = strrchr(refname, '/') + 1;
416 const char *remote;
418 /* A remote branch of the same name is deemed similar */
419 if (skip_prefix(refname, "refs/remotes/", &remote) &&
420 !strcmp(branch, cb->base_ref))
421 string_list_append(cb->similar_refs, remote);
422 return 0;
425 static struct string_list guess_refs(const char *ref)
427 struct similar_ref_cb ref_cb;
428 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
430 ref_cb.base_ref = ref;
431 ref_cb.similar_refs = &similar_refs;
432 for_each_ref(append_similar_ref, &ref_cb);
433 return similar_refs;
436 void help_unknown_ref(const char *ref, const char *cmd, const char *error)
438 int i;
439 struct string_list suggested_refs = guess_refs(ref);
441 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
443 if (suggested_refs.nr > 0) {
444 fprintf_ln(stderr,
445 Q_("\nDid you mean this?",
446 "\nDid you mean one of these?",
447 suggested_refs.nr));
448 for (i = 0; i < suggested_refs.nr; i++)
449 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
452 string_list_clear(&suggested_refs, 0);
453 exit(1);