help: add help_unknown_ref()

When the user gives an unknown string to a command that expects to get a ref, we could be more helpful than just saying "that's not a ref" and die. Add helper function help_unknown_ref() to take care of displaying an error message along with a list of suggested refs the user might have meant. An interaction with "git merge" might go like this: $ git merge foo merge: foo - not something we can merge Did you mean one of these? origin/foo upstream/foo Signed-off-by: Vikrant Varma <vikrant.varma94@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Vikrant Varma committed May 4, 2013 at 05:34 UTC e56181060e1117d458b854c34e63206117dab8b6
2 files changed +55
help.c
+50
@@ -7,6 +7,7 @@
7 #include "string-list.h"
8 #include "column.h"
9 #include "version.h"
10 +#include "refs.h"
11
12 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
13 {
@@ -404,3 +405,52 @@ int cmd_version(int argc, const char **argv, const char *prefix)
405 printf("git version %s\n", git_version_string);
406 return 0;
407 }
408 +
409 +struct similar_ref_cb {
410 + const char *base_ref;
411 + struct string_list *similar_refs;
412 +};
413 +
414 +static int append_similar_ref(const char *refname, const unsigned char *sha1,
415 + int flags, void *cb_data)
416 +{
417 + struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
418 + char *branch = strrchr(refname, '/') + 1;
419 + /* A remote branch of the same name is deemed similar */
420 + if (!prefixcmp(refname, "refs/remotes/") &&
421 + !strcmp(branch, cb->base_ref))
422 + string_list_append(cb->similar_refs,
423 + refname + strlen("refs/remotes/"));
424 + return 0;
425 +}
426 +
427 +static struct string_list guess_refs(const char *ref)
428 +{
429 + struct similar_ref_cb ref_cb;
430 + struct string_list similar_refs = STRING_LIST_INIT_NODUP;
431 +
432 + ref_cb.base_ref = ref;
433 + ref_cb.similar_refs = &similar_refs;
434 + for_each_ref(append_similar_ref, &ref_cb);
435 + return similar_refs;
436 +}
437 +
438 +void help_unknown_ref(const char *ref, const char *cmd, const char *error)
439 +{
440 + int i;
441 + struct string_list suggested_refs = guess_refs(ref);
442 +
443 + fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
444 +
445 + if (suggested_refs.nr > 0) {
446 + fprintf_ln(stderr,
447 + Q_("\nDid you mean this?",
448 + "\nDid you mean one of these?",
449 + suggested_refs.nr));
450 + for (i = 0; i < suggested_refs.nr; i++)
451 + fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
452 + }
453 +
454 + string_list_clear(&suggested_refs, 0);
455 + exit(1);
456 +}
help.h
+5
@@ -27,4 +27,9 @@ extern void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
27 extern int is_in_cmdlist(struct cmdnames *cmds, const char *name);
28 extern void list_commands(unsigned int colopts, struct cmdnames *main_cmds, struct cmdnames *other_cmds);
29
30 +/*
31 + * call this to die(), when it is suspected that the user mistyped a
32 + * ref to the command, to give suggested "correct" refs.
33 + */
34 +extern void help_unknown_ref(const char *ref, const char *cmd, const char *error);
35 #endif /* HELP_H */