]> git.ipfire.org Git - thirdparty/git.git/blame - help.c
tree-diff: convert diff_root_tree_sha1 to struct object_id
[thirdparty/git.git] / help.c
CommitLineData
70827b15
LT
1#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
38124a40 4#include "run-command.h"
8af84dad 5#include "levenshtein.h"
940208a7 6#include "help.h"
6612b9e4 7#include "common-cmds.h"
dbfae689
NTND
8#include "string-list.h"
9#include "column.h"
816fb46b 10#include "version.h"
e5618106 11#include "refs.h"
64949984 12
940208a7 13void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 14{
96ffc06f
JK
15 struct cmdname *ent;
16 FLEX_ALLOC_MEM(ent, name, name, len);
70827b15 17 ent->len = len;
1eb05690
SP
18
19 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
20 cmds->names[cmds->cnt++] = ent;
70827b15
LT
21}
22
8af84dad
JS
23static void clean_cmdnames(struct cmdnames *cmds)
24{
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;
31}
32
70827b15
LT
33static int cmdname_compare(const void *a_, const void *b_)
34{
35 struct cmdname *a = *(struct cmdname **)a_;
36 struct cmdname *b = *(struct cmdname **)b_;
37 return strcmp(a->name, b->name);
38}
39
1eb05690
SP
40static void uniq(struct cmdnames *cmds)
41{
42 int i, j;
43
44 if (!cmds->cnt)
45 return;
46
4a15758f
JK
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
1eb05690 51 cmds->names[j++] = cmds->names[i];
4a15758f 52 }
1eb05690
SP
53
54 cmds->cnt = j;
55}
56
940208a7 57void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
f3fa1838 58{
1eb05690
SP
59 int ci, cj, ei;
60 int cmp;
61
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++];
6a17f583
JH
67 else if (cmp == 0) {
68 ei++;
69 free(cmds->names[ci++]);
70 } else if (cmp > 0)
1eb05690
SP
71 ei++;
72 }
73
74 while (ci < cmds->cnt)
75 cmds->names[cj++] = cmds->names[ci++];
76
77 cmds->cnt = cj;
78}
79
d10cb3df 80static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
70827b15 81{
dbfae689
NTND
82 struct string_list list = STRING_LIST_INIT_NODUP;
83 struct column_options copts;
84 int i;
70827b15 85
dbfae689
NTND
86 for (i = 0; i < cmds->cnt; i++)
87 string_list_append(&list, cmds->names[i]->name);
88 /*
89 * always enable column display, we only consult column.*
90 * about layout strategy and stuff
91 */
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);
70827b15
LT
98}
99
e321180e 100static void list_commands_in_dir(struct cmdnames *cmds,
940208a7
MV
101 const char *path,
102 const char *prefix)
70827b15 103{
1eb05690 104 DIR *dir = opendir(path);
70827b15 105 struct dirent *de;
f5d600e2
JS
106 struct strbuf buf = STRBUF_INIT;
107 int len;
70827b15 108
f5d600e2 109 if (!dir)
e321180e 110 return;
940208a7
MV
111 if (!prefix)
112 prefix = "git-";
70827b15 113
f5d600e2
JS
114 strbuf_addf(&buf, "%s/", path);
115 len = buf.len;
116
70827b15 117 while ((de = readdir(dir)) != NULL) {
de8118e1 118 const char *ent;
26936bfd 119 size_t entlen;
70827b15 120
de8118e1 121 if (!skip_prefix(de->d_name, prefix, &ent))
70827b15 122 continue;
0966003c 123
f5d600e2
JS
124 strbuf_setlen(&buf, len);
125 strbuf_addstr(&buf, de->d_name);
126 if (!is_executable(buf.buf))
70827b15
LT
127 continue;
128
de8118e1 129 entlen = strlen(ent);
6e409473 130 strip_suffix(ent, ".exe", &entlen);
70827b15 131
de8118e1 132 add_cmdname(cmds, ent, entlen);
70827b15
LT
133 }
134 closedir(dir);
f5d600e2 135 strbuf_release(&buf);
1eb05690
SP
136}
137
e321180e 138void load_command_list(const char *prefix,
940208a7
MV
139 struct cmdnames *main_cmds,
140 struct cmdnames *other_cmds)
1eb05690 141{
1eb05690 142 const char *env_path = getenv("PATH");
1eb05690
SP
143 const char *exec_path = git_exec_path();
144
1f08e5ce 145 if (exec_path) {
e321180e 146 list_commands_in_dir(main_cmds, exec_path, prefix);
9ed0d8d6 147 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
1f08e5ce 148 uniq(main_cmds);
1eb05690
SP
149 }
150
1f08e5ce
AR
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;
746c221a
PB
157 if (!exec_path || strcmp(path, exec_path))
158 list_commands_in_dir(other_cmds, path, prefix);
1eb05690 159
1f08e5ce
AR
160 if (!colon)
161 break;
162 path = colon + 1;
163 }
164 free(paths);
1eb05690 165
9ed0d8d6 166 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
1f08e5ce
AR
167 uniq(other_cmds);
168 }
940208a7 169 exclude_cmds(other_cmds, main_cmds);
2156435f
JK
170}
171
f4ed0af6 172void list_commands(unsigned int colopts,
dbfae689 173 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
2156435f 174{
940208a7 175 if (main_cmds->cnt) {
61c5d431 176 const char *exec_path = git_exec_path();
4470ef94 177 printf_ln(_("available git commands in '%s'"), exec_path);
1eb05690 178 putchar('\n');
d10cb3df 179 pretty_print_cmdnames(main_cmds, colopts);
1eb05690
SP
180 putchar('\n');
181 }
182
940208a7 183 if (other_cmds->cnt) {
4470ef94 184 printf_ln(_("git commands available from elsewhere on your $PATH"));
940208a7 185 putchar('\n');
d10cb3df 186 pretty_print_cmdnames(other_cmds, colopts);
1eb05690
SP
187 putchar('\n');
188 }
70827b15
LT
189}
190
22414770
SG
191static int cmd_group_cmp(const void *elem1, const void *elem2)
192{
193 const struct cmdname_help *e1 = elem1;
194 const struct cmdname_help *e2 = elem2;
195
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);
201}
202
1542d4cd
JH
203void list_common_cmds_help(void)
204{
205 int i, longest = 0;
22414770 206 int current_grp = -1;
1542d4cd
JH
207
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);
211 }
212
9ed0d8d6 213 QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
22414770
SG
214
215 puts(_("These are common Git commands used in various situations:"));
216
1542d4cd 217 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
22414770
SG
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;
221 }
222
1542d4cd
JH
223 printf(" %s ", common_cmds[i].name);
224 mput_char(' ', longest - strlen(common_cmds[i].name));
225 puts(_(common_cmds[i].help));
226 }
227}
228
940208a7 229int is_in_cmdlist(struct cmdnames *c, const char *s)
2156435f
JK
230{
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;
236}
237
f0e90716 238static int autocorrect;
746c221a 239static struct cmdnames aliases;
f0e90716
AR
240
241static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
242{
ae021d87
JK
243 const char *p;
244
f0e90716
AR
245 if (!strcmp(var, "help.autocorrect"))
246 autocorrect = git_config_int(var,value);
746c221a 247 /* Also use aliases for command lookup */
ae021d87
JK
248 if (skip_prefix(var, "alias.", &p))
249 add_cmdname(&aliases, p, strlen(p));
f0e90716
AR
250
251 return git_default_config(var, value, cb);
252}
253
8af84dad 254static int levenshtein_compare(const void *p1, const void *p2)
822a7d50 255{
8af84dad
JS
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);
261}
262
746c221a
PB
263static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
264{
265 int i;
266 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
267
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;
273}
274
06500a02 275/* An empirically derived magic number */
6612b9e4
EFL
276#define SIMILARITY_FLOOR 7
277#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
06500a02 278
823e0ded
MS
279static 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?");
282
8af84dad 283const char *help_unknown_cmd(const char *cmd)
822a7d50 284{
8af84dad
JS
285 int i, n, best_similarity = 0;
286 struct cmdnames main_cmds, other_cmds;
287
288 memset(&main_cmds, 0, sizeof(main_cmds));
0b74f5dc 289 memset(&other_cmds, 0, sizeof(other_cmds));
746c221a 290 memset(&aliases, 0, sizeof(aliases));
8af84dad 291
f0e90716
AR
292 git_config(git_unknown_cmd_config, NULL);
293
8af84dad
JS
294 load_command_list("git-", &main_cmds, &other_cmds);
295
746c221a
PB
296 add_cmd_list(&main_cmds, &aliases);
297 add_cmd_list(&main_cmds, &other_cmds);
9ed0d8d6 298 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
746c221a 299 uniq(&main_cmds);
8af84dad 300
6612b9e4
EFL
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;
305
823e0ded
MS
306 /*
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.
310 */
311 if (!strcmp(candidate, cmd))
312 die(_(bad_interpreter_advice), cmd, cmd);
313
6612b9e4
EFL
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[] */
59556548 321 if (starts_with(candidate, cmd)) {
6612b9e4
EFL
322 /* Give prefix match a very good score */
323 main_cmds.names[i]->len = 0;
324 continue;
325 }
326 }
327
8af84dad 328 main_cmds.names[i]->len =
c41494f8 329 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
6612b9e4 330 }
8af84dad 331
9ed0d8d6 332 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
8af84dad
JS
333
334 if (!main_cmds.cnt)
9665627d 335 die(_("Uh oh. Your system reports no Git commands at all."));
8af84dad 336
6612b9e4
EFL
337 /* skip and count prefix matches */
338 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
339 ; /* still counting */
340
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 */
351 }
06500a02 352 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
8af84dad
JS
353 const char *assumed = main_cmds.names[0]->name;
354 main_cmds.names[0] = NULL;
355 clean_cmdnames(&main_cmds);
9665627d
NTND
356 fprintf_ln(stderr,
357 _("WARNING: You called a Git command named '%s', "
358 "which does not exist.\n"
359 "Continuing under the assumption that you meant '%s'"),
8af84dad 360 cmd, assumed);
f0e90716 361 if (autocorrect > 0) {
9665627d 362 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
f0e90716 363 (float)autocorrect/10.0);
2024d317 364 sleep_millisec(autocorrect * 100);
f0e90716 365 }
8af84dad
JS
366 return assumed;
367 }
368
9665627d 369 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
8af84dad 370
06500a02 371 if (SIMILAR_ENOUGH(best_similarity)) {
9665627d 372 fprintf_ln(stderr,
6c486862
JNA
373 Q_("\nThe most similar command is",
374 "\nThe most similar commands are",
9665627d 375 n));
8af84dad
JS
376
377 for (i = 0; i < n; i++)
378 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
379 }
380
822a7d50
RJ
381 exit(1);
382}
383
a633fca0 384int cmd_version(int argc, const char **argv, const char *prefix)
70827b15 385{
f2de0b97
DA
386 /*
387 * The format of this string should be kept stable for compatibility
388 * with external projects that rely on the output of "git version".
389 */
70827b15 390 printf("git version %s\n", git_version_string);
6b9c38e1
JK
391 while (*++argv) {
392 if (!strcmp(*argv, "--build-options")) {
393 printf("sizeof-long: %d\n", (int)sizeof(long));
394 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
395 }
396 }
70827b15
LT
397 return 0;
398}
e5618106
VV
399
400struct similar_ref_cb {
401 const char *base_ref;
402 struct string_list *similar_refs;
403};
404
91d6e94e 405static int append_similar_ref(const char *refname, const struct object_id *oid,
e5618106
VV
406 int flags, void *cb_data)
407{
408 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
409 char *branch = strrchr(refname, '/') + 1;
95b567c7
JK
410 const char *remote;
411
e5618106 412 /* A remote branch of the same name is deemed similar */
95b567c7 413 if (skip_prefix(refname, "refs/remotes/", &remote) &&
e5618106 414 !strcmp(branch, cb->base_ref))
95b567c7 415 string_list_append(cb->similar_refs, remote);
e5618106
VV
416 return 0;
417}
418
419static struct string_list guess_refs(const char *ref)
420{
421 struct similar_ref_cb ref_cb;
422 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
423
424 ref_cb.base_ref = ref;
425 ref_cb.similar_refs = &similar_refs;
426 for_each_ref(append_similar_ref, &ref_cb);
427 return similar_refs;
428}
429
430void help_unknown_ref(const char *ref, const char *cmd, const char *error)
431{
432 int i;
433 struct string_list suggested_refs = guess_refs(ref);
434
435 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
436
437 if (suggested_refs.nr > 0) {
438 fprintf_ln(stderr,
439 Q_("\nDid you mean this?",
440 "\nDid you mean one of these?",
441 suggested_refs.nr));
442 for (i = 0; i < suggested_refs.nr; i++)
443 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
444 }
445
446 string_list_clear(&suggested_refs, 0);
447 exit(1);
448}