4 #include "run-command.h"
5 #include "levenshtein.h"
7 #include "common-cmds.h"
8 #include "string-list.h"
13 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
16 FLEX_ALLOC_MEM(ent
, name
, name
, 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
)
26 for (i
= 0; i
< cmds
->cnt
; ++i
)
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
)
47 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
48 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
51 cmds
->names
[j
++] = cmds
->names
[i
];
57 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
63 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
64 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
66 cmds
->names
[cj
++] = cmds
->names
[ci
++];
69 free(cmds
->names
[ci
++]);
74 while (ci
< cmds
->cnt
)
75 cmds
->names
[cj
++] = cmds
->names
[ci
++];
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
;
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
));
96 print_columns(&list
, colopts
, &copts
);
97 string_list_clear(&list
, 0);
100 static void list_commands_in_dir(struct cmdnames
*cmds
,
104 DIR *dir
= opendir(path
);
106 struct strbuf buf
= STRBUF_INIT
;
114 strbuf_addf(&buf
, "%s/", path
);
117 while ((de
= readdir(dir
)) != NULL
) {
121 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
124 strbuf_setlen(&buf
, len
);
125 strbuf_addstr(&buf
, de
->d_name
);
126 if (!is_executable(buf
.buf
))
129 entlen
= strlen(ent
);
130 strip_suffix(ent
, ".exe", &entlen
);
132 add_cmdname(cmds
, ent
, entlen
);
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();
146 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
147 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
152 char *paths
, *path
, *colon
;
153 path
= paths
= xstrdup(env_path
);
155 if ((colon
= strchr(path
, PATH_SEP
)))
157 if (!exec_path
|| strcmp(path
, exec_path
))
158 list_commands_in_dir(other_cmds
, path
, prefix
);
166 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
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
);
179 pretty_print_cmdnames(main_cmds
, colopts
);
183 if (other_cmds
->cnt
) {
184 printf_ln(_("git commands available from elsewhere on your $PATH"));
186 pretty_print_cmdnames(other_cmds
, colopts
);
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
)
198 if (e1
->group
> e2
->group
)
200 return strcmp(e1
->name
, e2
->name
);
203 void list_common_cmds_help(void)
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
)
232 for (i
= 0; i
< c
->cnt
; i
++)
233 if (!strcmp(s
, c
->names
[i
]->name
))
238 static int autocorrect
;
239 static struct cmdnames aliases
;
241 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
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
;
260 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
263 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
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_AND_NULL(old
->names
);
274 /* An empirically derived magic number */
275 #define SIMILARITY_FLOOR 7
276 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
278 static const char bad_interpreter_advice
[] =
279 N_("'%s' appears to be a git command, but we were not\n"
280 "able to execute it. Maybe git-%s is broken?");
282 const char *help_unknown_cmd(const char *cmd
)
284 int i
, n
, best_similarity
= 0;
285 struct cmdnames main_cmds
, other_cmds
;
287 memset(&main_cmds
, 0, sizeof(main_cmds
));
288 memset(&other_cmds
, 0, sizeof(other_cmds
));
289 memset(&aliases
, 0, sizeof(aliases
));
291 git_config(git_unknown_cmd_config
, NULL
);
293 load_command_list("git-", &main_cmds
, &other_cmds
);
295 add_cmd_list(&main_cmds
, &aliases
);
296 add_cmd_list(&main_cmds
, &other_cmds
);
297 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
300 /* This abuses cmdname->len for levenshtein distance */
301 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
302 int cmp
= 0; /* avoid compiler stupidity */
303 const char *candidate
= main_cmds
.names
[i
]->name
;
306 * An exact match means we have the command, but
307 * for some reason exec'ing it gave us ENOENT; probably
308 * it's a bad interpreter in the #! line.
310 if (!strcmp(candidate
, cmd
))
311 die(_(bad_interpreter_advice
), cmd
, cmd
);
313 /* Does the candidate appear in common_cmds list? */
314 while (n
< ARRAY_SIZE(common_cmds
) &&
315 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
317 if ((n
< ARRAY_SIZE(common_cmds
)) && !cmp
) {
318 /* Yes, this is one of the common commands */
319 n
++; /* use the entry from common_cmds[] */
320 if (starts_with(candidate
, cmd
)) {
321 /* Give prefix match a very good score */
322 main_cmds
.names
[i
]->len
= 0;
327 main_cmds
.names
[i
]->len
=
328 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
331 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
334 die(_("Uh oh. Your system reports no Git commands at all."));
336 /* skip and count prefix matches */
337 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
338 ; /* still counting */
340 if (main_cmds
.cnt
<= n
) {
341 /* prefix matches with everything? that is too ambiguous */
342 best_similarity
= SIMILARITY_FLOOR
+ 1;
344 /* count all the most similar ones */
345 for (best_similarity
= main_cmds
.names
[n
++]->len
;
346 (n
< main_cmds
.cnt
&&
347 best_similarity
== main_cmds
.names
[n
]->len
);
349 ; /* still counting */
351 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
352 const char *assumed
= main_cmds
.names
[0]->name
;
353 main_cmds
.names
[0] = NULL
;
354 clean_cmdnames(&main_cmds
);
356 _("WARNING: You called a Git command named '%s', "
357 "which does not exist.\n"
358 "Continuing under the assumption that you meant '%s'"),
360 if (autocorrect
> 0) {
361 fprintf_ln(stderr
, _("in %0.1f seconds automatically..."),
362 (float)autocorrect
/10.0);
363 sleep_millisec(autocorrect
* 100);
368 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
370 if (SIMILAR_ENOUGH(best_similarity
)) {
372 Q_("\nThe most similar command is",
373 "\nThe most similar commands are",
376 for (i
= 0; i
< n
; i
++)
377 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
383 int cmd_version(int argc
, const char **argv
, const char *prefix
)
386 * The format of this string should be kept stable for compatibility
387 * with external projects that rely on the output of "git version".
389 printf("git version %s\n", git_version_string
);
391 if (!strcmp(*argv
, "--build-options")) {
392 printf("sizeof-long: %d\n", (int)sizeof(long));
393 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
399 struct similar_ref_cb
{
400 const char *base_ref
;
401 struct string_list
*similar_refs
;
404 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
405 int flags
, void *cb_data
)
407 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
408 char *branch
= strrchr(refname
, '/') + 1;
411 /* A remote branch of the same name is deemed similar */
412 if (skip_prefix(refname
, "refs/remotes/", &remote
) &&
413 !strcmp(branch
, cb
->base_ref
))
414 string_list_append(cb
->similar_refs
, remote
);
418 static struct string_list
guess_refs(const char *ref
)
420 struct similar_ref_cb ref_cb
;
421 struct string_list similar_refs
= STRING_LIST_INIT_NODUP
;
423 ref_cb
.base_ref
= ref
;
424 ref_cb
.similar_refs
= &similar_refs
;
425 for_each_ref(append_similar_ref
, &ref_cb
);
429 void help_unknown_ref(const char *ref
, const char *cmd
, const char *error
)
432 struct string_list suggested_refs
= guess_refs(ref
);
434 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
436 if (suggested_refs
.nr
> 0) {
438 Q_("\nDid you mean this?",
439 "\nDid you mean one of these?",
441 for (i
= 0; i
< suggested_refs
.nr
; i
++)
442 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
445 string_list_clear(&suggested_refs
, 0);