blob: 1de9c0d589cd9b615b1b20d60e06b9601d08fe8f [file] [log] [blame]
Linus Torvalds70827b12006-04-21 10:27:34 -07001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Linus Torvalds70827b12006-04-21 10:27:34 -07003#include "builtin.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07004#include "exec-cmd.h"
Brandon Williams38124a42017-04-25 16:46:59 -07005#include "run-command.h"
Johannes Schindelin8af84da2008-08-31 15:50:23 +02006#include "levenshtein.h"
Miklos Vajna940208a2008-07-30 01:16:58 +02007#include "help.h"
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +02008#include "command-list.h"
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +07009#include "string-list.h"
10#include "column.h"
Jeff King816fb462012-06-02 14:51:42 -040011#include "version.h"
Vikrant Varmae5618102013-05-04 05:34:19 +053012#include "refs.h"
Jeff Kingb48cbfc2017-05-30 01:17:42 -040013#include "parse-options.h"
Christian Couder64949982008-03-07 08:46:28 +010014
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +020015struct category_description {
16 uint32_t category;
17 const char *desc;
18};
19static uint32_t common_mask =
20 CAT_init | CAT_worktree | CAT_info |
21 CAT_history | CAT_remote;
22static struct category_description common_categories[] = {
23 { CAT_init, N_("start a working area (see also: git help tutorial)") },
24 { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
25 { CAT_info, N_("examine the history and state (see also: git help revisions)") },
26 { CAT_history, N_("grow, mark and tweak your common history") },
27 { CAT_remote, N_("collaborate (see also: git help workflows)") },
28 { 0, NULL }
29};
Nguyễn Thái Ngọc Duy63eae832018-05-20 20:40:01 +020030static struct category_description main_categories[] = {
31 { CAT_mainporcelain, N_("Main Porcelain Commands") },
32 { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") },
33 { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") },
34 { CAT_foreignscminterface, N_("Interacting with Others") },
35 { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") },
36 { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") },
Elijah Newren96c0caf2019-11-05 17:07:26 +000037 { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") },
Nguyễn Thái Ngọc Duy63eae832018-05-20 20:40:01 +020038 { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
39 { 0, NULL }
40};
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +020041
Nguyễn Thái Ngọc Duy1b81d8c2018-05-20 20:40:02 +020042static const char *drop_prefix(const char *name, uint32_t category)
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +020043{
44 const char *new_name;
45
46 if (skip_prefix(name, "git-", &new_name))
47 return new_name;
Nguyễn Thái Ngọc Duy1b81d8c2018-05-20 20:40:02 +020048 if (category == CAT_guide && skip_prefix(name, "git", &new_name))
49 return new_name;
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +020050 return name;
51
52}
53
54static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
55{
56 int i, nr = 0;
57 struct cmdname_help *cmds;
58
59 if (ARRAY_SIZE(command_list) == 0)
60 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
61
62 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
63
64 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
65 const struct cmdname_help *cmd = command_list + i;
66
67 if (!(cmd->category & mask))
68 continue;
69
70 cmds[nr] = *cmd;
Nguyễn Thái Ngọc Duy1b81d8c2018-05-20 20:40:02 +020071 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +020072
73 nr++;
74 }
75 cmds[nr].name = NULL;
76 *p_cmds = cmds;
77}
78
79static void print_command_list(const struct cmdname_help *cmds,
80 uint32_t mask, int longest)
81{
82 int i;
83
84 for (i = 0; cmds[i].name; i++) {
85 if (cmds[i].category & mask) {
Johannes Schindelin1c4b9852018-12-11 06:58:11 -080086 size_t len = strlen(cmds[i].name);
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +020087 printf(" %s ", cmds[i].name);
Nguyễn Thái Ngọc Duy6195a762019-01-31 16:23:49 +070088 if (longest > len)
89 mput_char(' ', longest - len);
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +020090 puts(_(cmds[i].help));
91 }
92 }
93}
94
95static int cmd_name_cmp(const void *elem1, const void *elem2)
96{
97 const struct cmdname_help *e1 = elem1;
98 const struct cmdname_help *e2 = elem2;
99
100 return strcmp(e1->name, e2->name);
101}
102
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +0200103static void print_cmd_by_category(const struct category_description *catdesc,
104 int *longest_p)
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +0200105{
106 struct cmdname_help *cmds;
107 int longest = 0;
108 int i, nr = 0;
109 uint32_t mask = 0;
110
111 for (i = 0; catdesc[i].desc; i++)
112 mask |= catdesc[i].category;
113
114 extract_cmds(&cmds, mask);
115
116 for (i = 0; cmds[i].name; i++, nr++) {
117 if (longest < strlen(cmds[i].name))
118 longest = strlen(cmds[i].name);
119 }
120 QSORT(cmds, nr, cmd_name_cmp);
121
122 for (i = 0; catdesc[i].desc; i++) {
123 uint32_t mask = catdesc[i].category;
124 const char *desc = catdesc[i].desc;
125
126 printf("\n%s\n", _(desc));
127 print_command_list(cmds, mask, longest);
128 }
129 free(cmds);
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +0200130 if (longest_p)
131 *longest_p = longest;
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +0200132}
133
Miklos Vajna940208a2008-07-30 01:16:58 +0200134void add_cmdname(struct cmdnames *cmds, const char *name, int len)
Linus Torvalds70827b12006-04-21 10:27:34 -0700135{
Jeff King96ffc062016-02-22 17:44:32 -0500136 struct cmdname *ent;
137 FLEX_ALLOC_MEM(ent, name, name, len);
Linus Torvalds70827b12006-04-21 10:27:34 -0700138 ent->len = len;
Scott R Parish1eb05692007-10-28 20:30:52 -0700139
140 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
141 cmds->names[cmds->cnt++] = ent;
Linus Torvalds70827b12006-04-21 10:27:34 -0700142}
143
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200144static void clean_cmdnames(struct cmdnames *cmds)
145{
146 int i;
147 for (i = 0; i < cmds->cnt; ++i)
148 free(cmds->names[i]);
149 free(cmds->names);
150 cmds->cnt = 0;
151 cmds->alloc = 0;
152}
153
Linus Torvalds70827b12006-04-21 10:27:34 -0700154static int cmdname_compare(const void *a_, const void *b_)
155{
156 struct cmdname *a = *(struct cmdname **)a_;
157 struct cmdname *b = *(struct cmdname **)b_;
158 return strcmp(a->name, b->name);
159}
160
Scott R Parish1eb05692007-10-28 20:30:52 -0700161static void uniq(struct cmdnames *cmds)
162{
163 int i, j;
164
165 if (!cmds->cnt)
166 return;
167
Jeff King4a157582012-07-26 00:16:19 +0800168 for (i = j = 1; i < cmds->cnt; i++) {
169 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
170 free(cmds->names[i]);
171 else
Scott R Parish1eb05692007-10-28 20:30:52 -0700172 cmds->names[j++] = cmds->names[i];
Jeff King4a157582012-07-26 00:16:19 +0800173 }
Scott R Parish1eb05692007-10-28 20:30:52 -0700174
175 cmds->cnt = j;
176}
177
Miklos Vajna940208a2008-07-30 01:16:58 +0200178void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
Junio C Hamanof3fa1832007-11-08 15:35:32 -0800179{
Scott R Parish1eb05692007-10-28 20:30:52 -0700180 int ci, cj, ei;
181 int cmp;
182
183 ci = cj = ei = 0;
184 while (ci < cmds->cnt && ei < excludes->cnt) {
185 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
186 if (cmp < 0)
187 cmds->names[cj++] = cmds->names[ci++];
Junio C Hamano6a17f582012-07-25 11:01:12 -0700188 else if (cmp == 0) {
189 ei++;
190 free(cmds->names[ci++]);
191 } else if (cmp > 0)
Scott R Parish1eb05692007-10-28 20:30:52 -0700192 ei++;
193 }
194
195 while (ci < cmds->cnt)
196 cmds->names[cj++] = cmds->names[ci++];
197
198 cmds->cnt = cj;
199}
200
Ralf Thielowd10cb3d2014-02-28 20:27:29 +0100201static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
Linus Torvalds70827b12006-04-21 10:27:34 -0700202{
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +0700203 struct string_list list = STRING_LIST_INIT_NODUP;
204 struct column_options copts;
205 int i;
Linus Torvalds70827b12006-04-21 10:27:34 -0700206
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +0700207 for (i = 0; i < cmds->cnt; i++)
208 string_list_append(&list, cmds->names[i]->name);
209 /*
210 * always enable column display, we only consult column.*
211 * about layout strategy and stuff
212 */
213 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
214 memset(&copts, 0, sizeof(copts));
215 copts.indent = " ";
216 copts.padding = 2;
217 print_columns(&list, colopts, &copts);
218 string_list_clear(&list, 0);
Linus Torvalds70827b12006-04-21 10:27:34 -0700219}
220
Alex Riesene3211802008-08-28 19:15:33 +0200221static void list_commands_in_dir(struct cmdnames *cmds,
Miklos Vajna940208a2008-07-30 01:16:58 +0200222 const char *path,
223 const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -0700224{
Scott R Parish1eb05692007-10-28 20:30:52 -0700225 DIR *dir = opendir(path);
Linus Torvalds70827b12006-04-21 10:27:34 -0700226 struct dirent *de;
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200227 struct strbuf buf = STRBUF_INIT;
228 int len;
Linus Torvalds70827b12006-04-21 10:27:34 -0700229
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200230 if (!dir)
Alex Riesene3211802008-08-28 19:15:33 +0200231 return;
Miklos Vajna940208a2008-07-30 01:16:58 +0200232 if (!prefix)
233 prefix = "git-";
Linus Torvalds70827b12006-04-21 10:27:34 -0700234
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200235 strbuf_addf(&buf, "%s/", path);
236 len = buf.len;
237
Linus Torvalds70827b12006-04-21 10:27:34 -0700238 while ((de = readdir(dir)) != NULL) {
Jeff Kingde8118e2014-06-18 15:57:17 -0400239 const char *ent;
Jeff King26936bf2014-06-30 12:58:51 -0400240 size_t entlen;
Linus Torvalds70827b12006-04-21 10:27:34 -0700241
Jeff Kingde8118e2014-06-18 15:57:17 -0400242 if (!skip_prefix(de->d_name, prefix, &ent))
Linus Torvalds70827b12006-04-21 10:27:34 -0700243 continue;
Scott R Parish09660032007-10-27 01:36:52 -0700244
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200245 strbuf_setlen(&buf, len);
246 strbuf_addstr(&buf, de->d_name);
247 if (!is_executable(buf.buf))
Linus Torvalds70827b12006-04-21 10:27:34 -0700248 continue;
249
Jeff Kingde8118e2014-06-18 15:57:17 -0400250 entlen = strlen(ent);
Junio C Hamano6e409472014-07-16 11:25:59 -0700251 strip_suffix(ent, ".exe", &entlen);
Linus Torvalds70827b12006-04-21 10:27:34 -0700252
Jeff Kingde8118e2014-06-18 15:57:17 -0400253 add_cmdname(cmds, ent, entlen);
Linus Torvalds70827b12006-04-21 10:27:34 -0700254 }
255 closedir(dir);
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200256 strbuf_release(&buf);
Scott R Parish1eb05692007-10-28 20:30:52 -0700257}
258
Alex Riesene3211802008-08-28 19:15:33 +0200259void load_command_list(const char *prefix,
Miklos Vajna940208a2008-07-30 01:16:58 +0200260 struct cmdnames *main_cmds,
261 struct cmdnames *other_cmds)
Scott R Parish1eb05692007-10-28 20:30:52 -0700262{
Scott R Parish1eb05692007-10-28 20:30:52 -0700263 const char *env_path = getenv("PATH");
Scott R Parish1eb05692007-10-28 20:30:52 -0700264 const char *exec_path = git_exec_path();
265
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200266 if (exec_path) {
Alex Riesene3211802008-08-28 19:15:33 +0200267 list_commands_in_dir(main_cmds, exec_path, prefix);
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200268 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200269 uniq(main_cmds);
Scott R Parish1eb05692007-10-28 20:30:52 -0700270 }
271
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200272 if (env_path) {
273 char *paths, *path, *colon;
274 path = paths = xstrdup(env_path);
275 while (1) {
276 if ((colon = strchr(path, PATH_SEP)))
277 *colon = 0;
Pieter de Bie746c2212008-09-10 17:54:28 +0200278 if (!exec_path || strcmp(path, exec_path))
279 list_commands_in_dir(other_cmds, path, prefix);
Scott R Parish1eb05692007-10-28 20:30:52 -0700280
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200281 if (!colon)
282 break;
283 path = colon + 1;
284 }
285 free(paths);
286
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200287 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200288 uniq(other_cmds);
Scott R Parish1eb05692007-10-28 20:30:52 -0700289 }
Miklos Vajna940208a2008-07-30 01:16:58 +0200290 exclude_cmds(other_cmds, main_cmds);
Jeff King21564352008-02-24 17:17:37 -0500291}
292
Junio C Hamanof4ed0af2012-05-03 15:13:31 -0700293void list_commands(unsigned int colopts,
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +0700294 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
Jeff King21564352008-02-24 17:17:37 -0500295{
Miklos Vajna940208a2008-07-30 01:16:58 +0200296 if (main_cmds->cnt) {
Alex Riesen61c5d432008-08-28 19:19:42 +0200297 const char *exec_path = git_exec_path();
Nguyễn Thái Ngọc Duy4470ef92012-04-25 18:21:53 +0700298 printf_ln(_("available git commands in '%s'"), exec_path);
Scott R Parish1eb05692007-10-28 20:30:52 -0700299 putchar('\n');
Ralf Thielowd10cb3d2014-02-28 20:27:29 +0100300 pretty_print_cmdnames(main_cmds, colopts);
Scott R Parish1eb05692007-10-28 20:30:52 -0700301 putchar('\n');
302 }
303
Miklos Vajna940208a2008-07-30 01:16:58 +0200304 if (other_cmds->cnt) {
Nguyễn Thái Ngọc Duy4470ef92012-04-25 18:21:53 +0700305 printf_ln(_("git commands available from elsewhere on your $PATH"));
Miklos Vajna940208a2008-07-30 01:16:58 +0200306 putchar('\n');
Ralf Thielowd10cb3d2014-02-28 20:27:29 +0100307 pretty_print_cmdnames(other_cmds, colopts);
Scott R Parish1eb05692007-10-28 20:30:52 -0700308 putchar('\n');
309 }
Linus Torvalds70827b12006-04-21 10:27:34 -0700310}
311
Junio C Hamano1542d4c2013-01-18 22:35:04 -0800312void list_common_cmds_help(void)
313{
Sébastien Guimmara22414772015-05-21 19:39:22 +0200314 puts(_("These are common Git commands used in various situations:"));
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +0200315 print_cmd_by_category(common_categories, NULL);
Junio C Hamano1542d4c2013-01-18 22:35:04 -0800316}
Alex Riesenf0e90712008-08-31 15:54:58 +0200317
Nguyễn Thái Ngọc Duy6bb2dc02018-05-20 20:39:59 +0200318void list_all_main_cmds(struct string_list *list)
319{
320 struct cmdnames main_cmds, other_cmds;
321 int i;
322
323 memset(&main_cmds, 0, sizeof(main_cmds));
324 memset(&other_cmds, 0, sizeof(other_cmds));
325 load_command_list("git-", &main_cmds, &other_cmds);
326
327 for (i = 0; i < main_cmds.cnt; i++)
328 string_list_append(list, main_cmds.names[i]->name);
329
330 clean_cmdnames(&main_cmds);
331 clean_cmdnames(&other_cmds);
332}
333
334void list_all_other_cmds(struct string_list *list)
335{
336 struct cmdnames main_cmds, other_cmds;
337 int i;
338
339 memset(&main_cmds, 0, sizeof(main_cmds));
340 memset(&other_cmds, 0, sizeof(other_cmds));
341 load_command_list("git-", &main_cmds, &other_cmds);
342
343 for (i = 0; i < other_cmds.cnt; i++)
344 string_list_append(list, other_cmds.names[i]->name);
345
346 clean_cmdnames(&main_cmds);
347 clean_cmdnames(&other_cmds);
348}
349
Nguyễn Thái Ngọc Duy3c777762018-05-20 20:40:00 +0200350void list_cmds_by_category(struct string_list *list,
351 const char *cat)
352{
353 int i, n = ARRAY_SIZE(command_list);
354 uint32_t cat_id = 0;
355
356 for (i = 0; category_names[i]; i++) {
357 if (!strcmp(cat, category_names[i])) {
358 cat_id = 1UL << i;
359 break;
Jeff Kingae021d82014-06-18 15:47:50 -0400360 }
Alex Riesenf0e90712008-08-31 15:54:58 +0200361 }
Nguyễn Thái Ngọc Duy3c777762018-05-20 20:40:00 +0200362 if (!cat_id)
363 die(_("unsupported command listing type '%s'"), cat);
364
365 for (i = 0; i < n; i++) {
366 struct cmdname_help *cmd = command_list + i;
367
Nguyễn Thái Ngọc Duy1b81d8c2018-05-20 20:40:02 +0200368 if (!(cmd->category & cat_id))
369 continue;
370 string_list_append(list, drop_prefix(cmd->name, cmd->category));
Nguyễn Thái Ngọc Duy3c777762018-05-20 20:40:00 +0200371 }
372}
373
Nguyễn Thái Ngọc Duy6532f372018-05-20 20:40:09 +0200374void list_cmds_by_config(struct string_list *list)
375{
376 const char *cmd_list;
377
Nguyễn Thái Ngọc Duy6532f372018-05-20 20:40:09 +0200378 if (git_config_get_string_const("completion.commands", &cmd_list))
379 return;
380
381 string_list_sort(list);
382 string_list_remove_duplicates(list, 0);
383
384 while (*cmd_list) {
385 struct strbuf sb = STRBUF_INIT;
386 const char *p = strchrnul(cmd_list, ' ');
387
388 strbuf_add(&sb, cmd_list, p - cmd_list);
Jeff King057ab542019-03-20 14:03:28 -0400389 if (sb.buf[0] == '-')
390 string_list_remove(list, sb.buf + 1, 0);
Nguyễn Thái Ngọc Duy6532f372018-05-20 20:40:09 +0200391 else
392 string_list_insert(list, sb.buf);
393 strbuf_release(&sb);
394 while (*p == ' ')
395 p++;
396 cmd_list = p;
397 }
398}
399
Nguyễn Thái Ngọc Duy1b81d8c2018-05-20 20:40:02 +0200400void list_common_guides_help(void)
401{
402 struct category_description catdesc[] = {
403 { CAT_guide, N_("The common Git guides are:") },
404 { 0, NULL }
405 };
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +0200406 print_cmd_by_category(catdesc, NULL);
Nguyễn Thái Ngọc Duy1b81d8c2018-05-20 20:40:02 +0200407 putchar('\n');
408}
409
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +0200410static int get_alias(const char *var, const char *value, void *data)
411{
412 struct string_list *list = data;
413
414 if (skip_prefix(var, "alias.", &var))
415 string_list_append(list, var)->util = xstrdup(value);
416
417 return 0;
418}
419
Nguyễn Thái Ngọc Duy63eae832018-05-20 20:40:01 +0200420void list_all_cmds_help(void)
421{
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +0200422 struct string_list others = STRING_LIST_INIT_DUP;
423 struct string_list alias_list = STRING_LIST_INIT_DUP;
424 struct cmdname_help *aliases;
425 int i, longest;
426
427 printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
428 print_cmd_by_category(main_categories, &longest);
429
430 list_all_other_cmds(&others);
431 if (others.nr)
432 printf("\n%s\n", _("External commands"));
433 for (i = 0; i < others.nr; i++)
434 printf(" %s\n", others.items[i].string);
435 string_list_clear(&others, 0);
436
437 git_config(get_alias, &alias_list);
438 string_list_sort(&alias_list);
Johannes Schindelin1c4b9852018-12-11 06:58:11 -0800439
440 for (i = 0; i < alias_list.nr; i++) {
441 size_t len = strlen(alias_list.items[i].string);
442 if (longest < len)
443 longest = len;
444 }
445
Nguyễn Thái Ngọc Duy26c7d062018-09-29 08:08:14 +0200446 if (alias_list.nr) {
447 printf("\n%s\n", _("Command aliases"));
448 ALLOC_ARRAY(aliases, alias_list.nr + 1);
449 for (i = 0; i < alias_list.nr; i++) {
450 aliases[i].name = alias_list.items[i].string;
451 aliases[i].help = alias_list.items[i].util;
452 aliases[i].category = 1;
453 }
454 aliases[alias_list.nr].name = NULL;
455 print_command_list(aliases, 1, longest);
456 free(aliases);
457 }
458 string_list_clear(&alias_list, 1);
Alex Riesenf0e90712008-08-31 15:54:58 +0200459}
Pieter de Bie746c2212008-09-10 17:54:28 +0200460
461int is_in_cmdlist(struct cmdnames *c, const char *s)
462{
Alex Riesenf0e90712008-08-31 15:54:58 +0200463 int i;
464 for (i = 0; i < c->cnt; i++)
465 if (!strcmp(s, c->names[i]->name))
466 return 1;
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200467 return 0;
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100468}
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200469
470static int autocorrect;
471static struct cmdnames aliases;
472
473static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
474{
Jeff Kingae021d82014-06-18 15:47:50 -0400475 const char *p;
476
Linus Torvalds70827b12006-04-21 10:27:34 -0700477 if (!strcmp(var, "help.autocorrect"))
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100478 autocorrect = git_config_int(var,value);
479 /* Also use aliases for command lookup */
Jeff Kingae021d82014-06-18 15:47:50 -0400480 if (skip_prefix(var, "alias.", &p))
481 add_cmdname(&aliases, p, strlen(p));
Linus Torvalds70827b12006-04-21 10:27:34 -0700482
483 return git_default_config(var, value, cb);
484}
485
Christian Couder5d6491c2007-12-02 06:07:55 +0100486static int levenshtein_compare(const void *p1, const void *p2)
Linus Torvalds70827b12006-04-21 10:27:34 -0700487{
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200488 const struct cmdname *const *c1 = p1, *const *c2 = p2;
489 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
490 int l1 = (*c1)->len;
491 int l2 = (*c2)->len;
492 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
493}
494
Pieter de Bie746c2212008-09-10 17:54:28 +0200495static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
496{
497 int i;
498 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
499
500 for (i = 0; i < old->cnt; i++)
501 cmds->names[cmds->cnt++] = old->names[i];
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +0000502 FREE_AND_NULL(old->names);
Pieter de Bie746c2212008-09-10 17:54:28 +0200503 old->cnt = 0;
Pieter de Bie746c2212008-09-10 17:54:28 +0200504}
505
Johannes Sixt06500a02009-12-15 08:57:18 +0100506/* An empirically derived magic number */
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100507#define SIMILARITY_FLOOR 7
508#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
Johannes Sixt06500a02009-12-15 08:57:18 +0100509
Michael Schubert823e0de2011-07-08 12:08:49 +0200510static const char bad_interpreter_advice[] =
511 N_("'%s' appears to be a git command, but we were not\n"
512 "able to execute it. Maybe git-%s is broken?");
513
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200514const char *help_unknown_cmd(const char *cmd)
515{
516 int i, n, best_similarity = 0;
517 struct cmdnames main_cmds, other_cmds;
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +0200518 struct cmdname_help *common_cmds;
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200519
520 memset(&main_cmds, 0, sizeof(main_cmds));
Johan Herland0b74f5d2009-08-11 12:10:21 +0200521 memset(&other_cmds, 0, sizeof(other_cmds));
Pieter de Bie746c2212008-09-10 17:54:28 +0200522 memset(&aliases, 0, sizeof(aliases));
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200523
Johannes Schindelin659fef12017-06-14 13:35:50 +0200524 read_early_config(git_unknown_cmd_config, NULL);
Alex Riesenf0e90712008-08-31 15:54:58 +0200525
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200526 load_command_list("git-", &main_cmds, &other_cmds);
527
Pieter de Bie746c2212008-09-10 17:54:28 +0200528 add_cmd_list(&main_cmds, &aliases);
529 add_cmd_list(&main_cmds, &other_cmds);
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200530 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
Pieter de Bie746c2212008-09-10 17:54:28 +0200531 uniq(&main_cmds);
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200532
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +0200533 extract_cmds(&common_cmds, common_mask);
534
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100535 /* This abuses cmdname->len for levenshtein distance */
536 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
537 int cmp = 0; /* avoid compiler stupidity */
538 const char *candidate = main_cmds.names[i]->name;
539
Michael Schubert823e0de2011-07-08 12:08:49 +0200540 /*
541 * An exact match means we have the command, but
542 * for some reason exec'ing it gave us ENOENT; probably
543 * it's a bad interpreter in the #! line.
544 */
545 if (!strcmp(candidate, cmd))
546 die(_(bad_interpreter_advice), cmd, cmd);
547
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100548 /* Does the candidate appear in common_cmds list? */
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +0200549 while (common_cmds[n].name &&
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100550 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
551 n++;
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +0200552 if (common_cmds[n].name && !cmp) {
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100553 /* Yes, this is one of the common commands */
554 n++; /* use the entry from common_cmds[] */
Christian Couder59556542013-11-30 21:55:40 +0100555 if (starts_with(candidate, cmd)) {
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100556 /* Give prefix match a very good score */
557 main_cmds.names[i]->len = 0;
558 continue;
559 }
560 }
561
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200562 main_cmds.names[i]->len =
Matthieu Moyc41494f2012-05-27 18:02:58 +0200563 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100564 }
Nguyễn Thái Ngọc Duycfb22a02018-05-10 10:46:42 +0200565 FREE_AND_NULL(common_cmds);
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200566
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200567 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200568
569 if (!main_cmds.cnt)
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700570 die(_("Uh oh. Your system reports no Git commands at all."));
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200571
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100572 /* skip and count prefix matches */
573 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
574 ; /* still counting */
575
576 if (main_cmds.cnt <= n) {
577 /* prefix matches with everything? that is too ambiguous */
578 best_similarity = SIMILARITY_FLOOR + 1;
579 } else {
580 /* count all the most similar ones */
581 for (best_similarity = main_cmds.names[n++]->len;
582 (n < main_cmds.cnt &&
583 best_similarity == main_cmds.names[n]->len);
584 n++)
585 ; /* still counting */
586 }
Johannes Sixt06500a02009-12-15 08:57:18 +0100587 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200588 const char *assumed = main_cmds.names[0]->name;
589 main_cmds.names[0] = NULL;
590 clean_cmdnames(&main_cmds);
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700591 fprintf_ln(stderr,
592 _("WARNING: You called a Git command named '%s', "
Marc Branchaud968b1fe2017-06-21 09:57:38 -0400593 "which does not exist."),
594 cmd);
595 if (autocorrect < 0)
596 fprintf_ln(stderr,
597 _("Continuing under the assumption that "
598 "you meant '%s'."),
599 assumed);
600 else {
601 fprintf_ln(stderr,
602 _("Continuing in %0.1f seconds, "
603 "assuming that you meant '%s'."),
604 (float)autocorrect/10.0, assumed);
Johannes Sixt2024d312015-06-05 21:45:05 +0200605 sleep_millisec(autocorrect * 100);
Alex Riesenf0e90712008-08-31 15:54:58 +0200606 }
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200607 return assumed;
608 }
609
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700610 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200611
Johannes Sixt06500a02009-12-15 08:57:18 +0100612 if (SIMILAR_ENOUGH(best_similarity)) {
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700613 fprintf_ln(stderr,
Jean-Noel Avila6c486862017-05-11 14:06:32 +0200614 Q_("\nThe most similar command is",
615 "\nThe most similar commands are",
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700616 n));
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200617
618 for (i = 0; i < n; i++)
619 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
620 }
621
Linus Torvalds70827b12006-04-21 10:27:34 -0700622 exit(1);
623}
624
Emily Shaffer617d5712020-04-16 14:18:05 -0700625void get_version_info(struct strbuf *buf, int show_build_options)
626{
627 /*
628 * The format of this string should be kept stable for compatibility
629 * with external projects that rely on the output of "git version".
630 *
631 * Always show the version, even if other options are given.
632 */
633 strbuf_addf(buf, "git version %s\n", git_version_string);
634
635 if (show_build_options) {
636 strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
637 if (git_built_from_commit_string[0])
638 strbuf_addf(buf, "built from commit: %s\n",
639 git_built_from_commit_string);
640 else
641 strbuf_addstr(buf, "no commit associated with this build\n");
642 strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
643 strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
644 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
645 }
646}
647
Linus Torvalds70827b12006-04-21 10:27:34 -0700648int cmd_version(int argc, const char **argv, const char *prefix)
649{
Emily Shaffer617d5712020-04-16 14:18:05 -0700650 struct strbuf buf = STRBUF_INIT;
Jeff Kingb48cbfc2017-05-30 01:17:42 -0400651 int build_options = 0;
652 const char * const usage[] = {
653 N_("git version [<options>]"),
654 NULL
655 };
656 struct option options[] = {
657 OPT_BOOL(0, "build-options", &build_options,
658 "also print build options"),
659 OPT_END()
660 };
661
662 argc = parse_options(argc, argv, prefix, options, usage, 0);
663
Emily Shaffer617d5712020-04-16 14:18:05 -0700664 get_version_info(&buf, build_options);
665 printf("%s", buf.buf);
Jeff Kingb48cbfc2017-05-30 01:17:42 -0400666
Emily Shaffer617d5712020-04-16 14:18:05 -0700667 strbuf_release(&buf);
668
Linus Torvalds70827b12006-04-21 10:27:34 -0700669 return 0;
670}
Vikrant Varmae5618102013-05-04 05:34:19 +0530671
672struct similar_ref_cb {
673 const char *base_ref;
674 struct string_list *similar_refs;
675};
676
Michael Haggerty91d6e942015-05-25 18:38:54 +0000677static int append_similar_ref(const char *refname, const struct object_id *oid,
Vikrant Varmae5618102013-05-04 05:34:19 +0530678 int flags, void *cb_data)
679{
680 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
681 char *branch = strrchr(refname, '/') + 1;
Jeff King95b567c2014-06-18 15:48:29 -0400682
Vikrant Varmae5618102013-05-04 05:34:19 +0530683 /* A remote branch of the same name is deemed similar */
Jeff King2ed2e192019-05-14 08:05:05 -0400684 if (starts_with(refname, "refs/remotes/") &&
Vikrant Varmae5618102013-05-04 05:34:19 +0530685 !strcmp(branch, cb->base_ref))
Jeff King2ed2e192019-05-14 08:05:05 -0400686 string_list_append_nodup(cb->similar_refs,
687 shorten_unambiguous_ref(refname, 1));
Vikrant Varmae5618102013-05-04 05:34:19 +0530688 return 0;
689}
690
691static struct string_list guess_refs(const char *ref)
692{
693 struct similar_ref_cb ref_cb;
Jeff King8ed51b02019-05-14 08:04:31 -0400694 struct string_list similar_refs = STRING_LIST_INIT_DUP;
Vikrant Varmae5618102013-05-04 05:34:19 +0530695
696 ref_cb.base_ref = ref;
697 ref_cb.similar_refs = &similar_refs;
698 for_each_ref(append_similar_ref, &ref_cb);
699 return similar_refs;
700}
701
René Scharfe80e36582019-08-29 21:13:16 +0200702NORETURN void help_unknown_ref(const char *ref, const char *cmd,
703 const char *error)
Vikrant Varmae5618102013-05-04 05:34:19 +0530704{
705 int i;
706 struct string_list suggested_refs = guess_refs(ref);
707
708 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
709
710 if (suggested_refs.nr > 0) {
711 fprintf_ln(stderr,
712 Q_("\nDid you mean this?",
713 "\nDid you mean one of these?",
714 suggested_refs.nr));
715 for (i = 0; i < suggested_refs.nr; i++)
716 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
717 }
718
719 string_list_clear(&suggested_refs, 0);
720 exit(1);
721}