]> git.ipfire.org Git - thirdparty/git.git/blame - help.c
Merge branch 'jk/patch-ids-no-merges'
[thirdparty/git.git] / help.c
CommitLineData
70827b15
LT
1#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
8af84dad 4#include "levenshtein.h"
940208a7 5#include "help.h"
6612b9e4 6#include "common-cmds.h"
dbfae689
NTND
7#include "string-list.h"
8#include "column.h"
816fb46b 9#include "version.h"
e5618106 10#include "refs.h"
64949984 11
940208a7 12void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 13{
96ffc06f
JK
14 struct cmdname *ent;
15 FLEX_ALLOC_MEM(ent, name, name, len);
70827b15 16 ent->len = len;
1eb05690
SP
17
18 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
19 cmds->names[cmds->cnt++] = ent;
70827b15
LT
20}
21
8af84dad
JS
22static void clean_cmdnames(struct cmdnames *cmds)
23{
24 int i;
25 for (i = 0; i < cmds->cnt; ++i)
26 free(cmds->names[i]);
27 free(cmds->names);
28 cmds->cnt = 0;
29 cmds->alloc = 0;
30}
31
70827b15
LT
32static int cmdname_compare(const void *a_, const void *b_)
33{
34 struct cmdname *a = *(struct cmdname **)a_;
35 struct cmdname *b = *(struct cmdname **)b_;
36 return strcmp(a->name, b->name);
37}
38
1eb05690
SP
39static void uniq(struct cmdnames *cmds)
40{
41 int i, j;
42
43 if (!cmds->cnt)
44 return;
45
4a15758f
JK
46 for (i = j = 1; i < cmds->cnt; i++) {
47 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
48 free(cmds->names[i]);
49 else
1eb05690 50 cmds->names[j++] = cmds->names[i];
4a15758f 51 }
1eb05690
SP
52
53 cmds->cnt = j;
54}
55
940208a7 56void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
f3fa1838 57{
1eb05690
SP
58 int ci, cj, ei;
59 int cmp;
60
61 ci = cj = ei = 0;
62 while (ci < cmds->cnt && ei < excludes->cnt) {
63 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
64 if (cmp < 0)
65 cmds->names[cj++] = cmds->names[ci++];
6a17f583
JH
66 else if (cmp == 0) {
67 ei++;
68 free(cmds->names[ci++]);
69 } else if (cmp > 0)
1eb05690
SP
70 ei++;
71 }
72
73 while (ci < cmds->cnt)
74 cmds->names[cj++] = cmds->names[ci++];
75
76 cmds->cnt = cj;
77}
78
d10cb3df 79static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
70827b15 80{
dbfae689
NTND
81 struct string_list list = STRING_LIST_INIT_NODUP;
82 struct column_options copts;
83 int i;
70827b15 84
dbfae689
NTND
85 for (i = 0; i < cmds->cnt; i++)
86 string_list_append(&list, cmds->names[i]->name);
87 /*
88 * always enable column display, we only consult column.*
89 * about layout strategy and stuff
90 */
91 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
92 memset(&copts, 0, sizeof(copts));
93 copts.indent = " ";
94 copts.padding = 2;
95 print_columns(&list, colopts, &copts);
96 string_list_clear(&list, 0);
70827b15
LT
97}
98
cc3b7a97
JS
99static int is_executable(const char *name)
100{
101 struct stat st;
102
103 if (stat(name, &st) || /* stat, not lstat */
104 !S_ISREG(st.st_mode))
105 return 0;
106
f66450ae 107#if defined(GIT_WINDOWS_NATIVE)
0d30ad71 108{ /* cannot trust the executable bit, peek into the file instead */
cc3b7a97
JS
109 char buf[3] = { 0 };
110 int n;
111 int fd = open(name, O_RDONLY);
112 st.st_mode &= ~S_IXUSR;
113 if (fd >= 0) {
114 n = read(fd, buf, 2);
115 if (n == 2)
116 /* DOS executables start with "MZ" */
117 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
118 st.st_mode |= S_IXUSR;
119 close(fd);
120 }
0d30ad71 121}
cc3b7a97
JS
122#endif
123 return st.st_mode & S_IXUSR;
124}
125
e321180e 126static void list_commands_in_dir(struct cmdnames *cmds,
940208a7
MV
127 const char *path,
128 const char *prefix)
70827b15 129{
1eb05690 130 DIR *dir = opendir(path);
70827b15 131 struct dirent *de;
f5d600e2
JS
132 struct strbuf buf = STRBUF_INIT;
133 int len;
70827b15 134
f5d600e2 135 if (!dir)
e321180e 136 return;
940208a7
MV
137 if (!prefix)
138 prefix = "git-";
70827b15 139
f5d600e2
JS
140 strbuf_addf(&buf, "%s/", path);
141 len = buf.len;
142
70827b15 143 while ((de = readdir(dir)) != NULL) {
de8118e1 144 const char *ent;
26936bfd 145 size_t entlen;
70827b15 146
de8118e1 147 if (!skip_prefix(de->d_name, prefix, &ent))
70827b15 148 continue;
0966003c 149
f5d600e2
JS
150 strbuf_setlen(&buf, len);
151 strbuf_addstr(&buf, de->d_name);
152 if (!is_executable(buf.buf))
70827b15
LT
153 continue;
154
de8118e1 155 entlen = strlen(ent);
6e409473 156 strip_suffix(ent, ".exe", &entlen);
70827b15 157
de8118e1 158 add_cmdname(cmds, ent, entlen);
70827b15
LT
159 }
160 closedir(dir);
f5d600e2 161 strbuf_release(&buf);
1eb05690
SP
162}
163
e321180e 164void load_command_list(const char *prefix,
940208a7
MV
165 struct cmdnames *main_cmds,
166 struct cmdnames *other_cmds)
1eb05690 167{
1eb05690 168 const char *env_path = getenv("PATH");
1eb05690
SP
169 const char *exec_path = git_exec_path();
170
1f08e5ce 171 if (exec_path) {
e321180e 172 list_commands_in_dir(main_cmds, exec_path, prefix);
1f08e5ce
AR
173 qsort(main_cmds->names, main_cmds->cnt,
174 sizeof(*main_cmds->names), cmdname_compare);
175 uniq(main_cmds);
1eb05690
SP
176 }
177
1f08e5ce
AR
178 if (env_path) {
179 char *paths, *path, *colon;
180 path = paths = xstrdup(env_path);
181 while (1) {
182 if ((colon = strchr(path, PATH_SEP)))
183 *colon = 0;
746c221a
PB
184 if (!exec_path || strcmp(path, exec_path))
185 list_commands_in_dir(other_cmds, path, prefix);
1eb05690 186
1f08e5ce
AR
187 if (!colon)
188 break;
189 path = colon + 1;
190 }
191 free(paths);
1eb05690 192
1f08e5ce
AR
193 qsort(other_cmds->names, other_cmds->cnt,
194 sizeof(*other_cmds->names), cmdname_compare);
195 uniq(other_cmds);
196 }
940208a7 197 exclude_cmds(other_cmds, main_cmds);
2156435f
JK
198}
199
f4ed0af6 200void list_commands(unsigned int colopts,
dbfae689 201 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
2156435f 202{
940208a7 203 if (main_cmds->cnt) {
61c5d431 204 const char *exec_path = git_exec_path();
4470ef94 205 printf_ln(_("available git commands in '%s'"), exec_path);
1eb05690 206 putchar('\n');
d10cb3df 207 pretty_print_cmdnames(main_cmds, colopts);
1eb05690
SP
208 putchar('\n');
209 }
210
940208a7 211 if (other_cmds->cnt) {
4470ef94 212 printf_ln(_("git commands available from elsewhere on your $PATH"));
940208a7 213 putchar('\n');
d10cb3df 214 pretty_print_cmdnames(other_cmds, colopts);
1eb05690
SP
215 putchar('\n');
216 }
70827b15
LT
217}
218
22414770
SG
219static int cmd_group_cmp(const void *elem1, const void *elem2)
220{
221 const struct cmdname_help *e1 = elem1;
222 const struct cmdname_help *e2 = elem2;
223
224 if (e1->group < e2->group)
225 return -1;
226 if (e1->group > e2->group)
227 return 1;
228 return strcmp(e1->name, e2->name);
229}
230
1542d4cd
JH
231void list_common_cmds_help(void)
232{
233 int i, longest = 0;
22414770 234 int current_grp = -1;
1542d4cd
JH
235
236 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
237 if (longest < strlen(common_cmds[i].name))
238 longest = strlen(common_cmds[i].name);
239 }
240
22414770
SG
241 qsort(common_cmds, ARRAY_SIZE(common_cmds),
242 sizeof(common_cmds[0]), cmd_group_cmp);
243
244 puts(_("These are common Git commands used in various situations:"));
245
1542d4cd 246 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
22414770
SG
247 if (common_cmds[i].group != current_grp) {
248 printf("\n%s\n", _(common_cmd_groups[common_cmds[i].group]));
249 current_grp = common_cmds[i].group;
250 }
251
1542d4cd
JH
252 printf(" %s ", common_cmds[i].name);
253 mput_char(' ', longest - strlen(common_cmds[i].name));
254 puts(_(common_cmds[i].help));
255 }
256}
257
940208a7 258int is_in_cmdlist(struct cmdnames *c, const char *s)
2156435f
JK
259{
260 int i;
261 for (i = 0; i < c->cnt; i++)
262 if (!strcmp(s, c->names[i]->name))
263 return 1;
264 return 0;
265}
266
f0e90716 267static int autocorrect;
746c221a 268static struct cmdnames aliases;
f0e90716
AR
269
270static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
271{
ae021d87
JK
272 const char *p;
273
f0e90716
AR
274 if (!strcmp(var, "help.autocorrect"))
275 autocorrect = git_config_int(var,value);
746c221a 276 /* Also use aliases for command lookup */
ae021d87
JK
277 if (skip_prefix(var, "alias.", &p))
278 add_cmdname(&aliases, p, strlen(p));
f0e90716
AR
279
280 return git_default_config(var, value, cb);
281}
282
8af84dad 283static int levenshtein_compare(const void *p1, const void *p2)
822a7d50 284{
8af84dad
JS
285 const struct cmdname *const *c1 = p1, *const *c2 = p2;
286 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
287 int l1 = (*c1)->len;
288 int l2 = (*c2)->len;
289 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
290}
291
746c221a
PB
292static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
293{
294 int i;
295 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
296
297 for (i = 0; i < old->cnt; i++)
298 cmds->names[cmds->cnt++] = old->names[i];
299 free(old->names);
300 old->cnt = 0;
301 old->names = NULL;
302}
303
06500a02 304/* An empirically derived magic number */
6612b9e4
EFL
305#define SIMILARITY_FLOOR 7
306#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
06500a02 307
823e0ded
MS
308static const char bad_interpreter_advice[] =
309 N_("'%s' appears to be a git command, but we were not\n"
310 "able to execute it. Maybe git-%s is broken?");
311
8af84dad 312const char *help_unknown_cmd(const char *cmd)
822a7d50 313{
8af84dad
JS
314 int i, n, best_similarity = 0;
315 struct cmdnames main_cmds, other_cmds;
316
317 memset(&main_cmds, 0, sizeof(main_cmds));
0b74f5dc 318 memset(&other_cmds, 0, sizeof(other_cmds));
746c221a 319 memset(&aliases, 0, sizeof(aliases));
8af84dad 320
f0e90716
AR
321 git_config(git_unknown_cmd_config, NULL);
322
8af84dad
JS
323 load_command_list("git-", &main_cmds, &other_cmds);
324
746c221a
PB
325 add_cmd_list(&main_cmds, &aliases);
326 add_cmd_list(&main_cmds, &other_cmds);
327 qsort(main_cmds.names, main_cmds.cnt,
d333ac17 328 sizeof(*main_cmds.names), cmdname_compare);
746c221a 329 uniq(&main_cmds);
8af84dad 330
6612b9e4
EFL
331 /* This abuses cmdname->len for levenshtein distance */
332 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
333 int cmp = 0; /* avoid compiler stupidity */
334 const char *candidate = main_cmds.names[i]->name;
335
823e0ded
MS
336 /*
337 * An exact match means we have the command, but
338 * for some reason exec'ing it gave us ENOENT; probably
339 * it's a bad interpreter in the #! line.
340 */
341 if (!strcmp(candidate, cmd))
342 die(_(bad_interpreter_advice), cmd, cmd);
343
6612b9e4
EFL
344 /* Does the candidate appear in common_cmds list? */
345 while (n < ARRAY_SIZE(common_cmds) &&
346 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
347 n++;
348 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
349 /* Yes, this is one of the common commands */
350 n++; /* use the entry from common_cmds[] */
59556548 351 if (starts_with(candidate, cmd)) {
6612b9e4
EFL
352 /* Give prefix match a very good score */
353 main_cmds.names[i]->len = 0;
354 continue;
355 }
356 }
357
8af84dad 358 main_cmds.names[i]->len =
c41494f8 359 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
6612b9e4 360 }
8af84dad
JS
361
362 qsort(main_cmds.names, main_cmds.cnt,
363 sizeof(*main_cmds.names), levenshtein_compare);
364
365 if (!main_cmds.cnt)
9665627d 366 die(_("Uh oh. Your system reports no Git commands at all."));
8af84dad 367
6612b9e4
EFL
368 /* skip and count prefix matches */
369 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
370 ; /* still counting */
371
372 if (main_cmds.cnt <= n) {
373 /* prefix matches with everything? that is too ambiguous */
374 best_similarity = SIMILARITY_FLOOR + 1;
375 } else {
376 /* count all the most similar ones */
377 for (best_similarity = main_cmds.names[n++]->len;
378 (n < main_cmds.cnt &&
379 best_similarity == main_cmds.names[n]->len);
380 n++)
381 ; /* still counting */
382 }
06500a02 383 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
8af84dad
JS
384 const char *assumed = main_cmds.names[0]->name;
385 main_cmds.names[0] = NULL;
386 clean_cmdnames(&main_cmds);
9665627d
NTND
387 fprintf_ln(stderr,
388 _("WARNING: You called a Git command named '%s', "
389 "which does not exist.\n"
390 "Continuing under the assumption that you meant '%s'"),
8af84dad 391 cmd, assumed);
f0e90716 392 if (autocorrect > 0) {
9665627d 393 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
f0e90716 394 (float)autocorrect/10.0);
2024d317 395 sleep_millisec(autocorrect * 100);
f0e90716 396 }
8af84dad
JS
397 return assumed;
398 }
399
9665627d 400 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
8af84dad 401
06500a02 402 if (SIMILAR_ENOUGH(best_similarity)) {
9665627d
NTND
403 fprintf_ln(stderr,
404 Q_("\nDid you mean this?",
405 "\nDid you mean one of these?",
406 n));
8af84dad
JS
407
408 for (i = 0; i < n; i++)
409 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
410 }
411
822a7d50
RJ
412 exit(1);
413}
414
a633fca0 415int cmd_version(int argc, const char **argv, const char *prefix)
70827b15 416{
f2de0b97
DA
417 /*
418 * The format of this string should be kept stable for compatibility
419 * with external projects that rely on the output of "git version".
420 */
70827b15 421 printf("git version %s\n", git_version_string);
6b9c38e1
JK
422 while (*++argv) {
423 if (!strcmp(*argv, "--build-options")) {
424 printf("sizeof-long: %d\n", (int)sizeof(long));
425 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
426 }
427 }
70827b15
LT
428 return 0;
429}
e5618106
VV
430
431struct similar_ref_cb {
432 const char *base_ref;
433 struct string_list *similar_refs;
434};
435
91d6e94e 436static int append_similar_ref(const char *refname, const struct object_id *oid,
e5618106
VV
437 int flags, void *cb_data)
438{
439 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
440 char *branch = strrchr(refname, '/') + 1;
95b567c7
JK
441 const char *remote;
442
e5618106 443 /* A remote branch of the same name is deemed similar */
95b567c7 444 if (skip_prefix(refname, "refs/remotes/", &remote) &&
e5618106 445 !strcmp(branch, cb->base_ref))
95b567c7 446 string_list_append(cb->similar_refs, remote);
e5618106
VV
447 return 0;
448}
449
450static struct string_list guess_refs(const char *ref)
451{
452 struct similar_ref_cb ref_cb;
453 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
454
455 ref_cb.base_ref = ref;
456 ref_cb.similar_refs = &similar_refs;
457 for_each_ref(append_similar_ref, &ref_cb);
458 return similar_refs;
459}
460
461void help_unknown_ref(const char *ref, const char *cmd, const char *error)
462{
463 int i;
464 struct string_list suggested_refs = guess_refs(ref);
465
466 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
467
468 if (suggested_refs.nr > 0) {
469 fprintf_ln(stderr,
470 Q_("\nDid you mean this?",
471 "\nDid you mean one of these?",
472 suggested_refs.nr));
473 for (i = 0; i < suggested_refs.nr; i++)
474 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
475 }
476
477 string_list_clear(&suggested_refs, 0);
478 exit(1);
479}