{"thread":{"id":"41795","subject":"[PATCH] sha1_name.c: add an option to abort on ambiguous refs","startedAt":"2016-03-23T13:30:40Z","lastAt":"2016-06-28T17:56:38Z","messageCount":4,"participants":["Nguyễn Thái Ngọc Duy","Johannes Schindelin","Duy Nguyen"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"281579","messageId":"1458739840-15855-1-git-send-email-pclouds@gmail.com","threadId":"41795","inReplyTo":null,"subject":"[PATCH] sha1_name.c: add an option to abort on ambiguous refs","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2016-03-23T13:30:40Z","receivedAt":"2016-03-23T13:30:40Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"There are cases when a warning on ambiguous refs may go unnoticed\n(e.g. git-log filling up the whole screen). There are also cases when\npeople want to catch ambiguity early (e.g. it happens deep in some\nscript). In either case, aborting the program would accomplish it.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/config.txt |  3 ++-\n config.c                 |  5 ++++-\n sha1_name.c              | 10 ++++++++--\n 3 files changed, 14 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 2cd6bdd..4172f59 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -522,7 +522,8 @@ core.sharedRepository::\n \n core.warnAmbiguousRefs::\n \tIf true, Git will warn you if the ref name you passed it is ambiguous\n-\tand might match multiple refs in the repository. True by default.\n+\tand might match multiple refs in the repository. If set to \"fatal\",\n+\tthe program will abort on ambiguous refs. True by default.\n \n core.compression::\n \tAn integer -1..9, indicating a default compression level.\ndiff --git a/config.c b/config.c\nindex 9ba40bc..79f1ea5 100644\n--- a/config.c\n+++ b/config.c\n@@ -738,7 +738,10 @@ static int git_default_core_config(const char *var, const char *value)\n \t}\n \n \tif (!strcmp(var, \"core.warnambiguousrefs\")) {\n-\t\twarn_ambiguous_refs = git_config_bool(var, value);\n+\t\tif (!strcasecmp(value, \"fatal\"))\n+\t\t\twarn_ambiguous_refs = 2;\n+\t\telse\n+\t\t\twarn_ambiguous_refs = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n \ndiff --git a/sha1_name.c b/sha1_name.c\nindex 3acf221..a0f0ab9 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -480,6 +480,8 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,\n \t\t\t\twarning(warn_msg, len, str);\n \t\t\t\tif (advice_object_name_warning)\n \t\t\t\t\tfprintf(stderr, \"%s\\n\", _(object_name_msg));\n+\t\t\t\tif (warn_ambiguous_refs > 1)\n+\t\t\t\t\tdie(_(\"cannot continue with ambiguous refs\"));\n \t\t\t}\n \t\t\tfree(real_ref);\n \t\t}\n@@ -537,8 +539,12 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,\n \n \tif (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&\n \t    (refs_found > 1 ||\n-\t     !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))\n-\t\twarning(warn_msg, len, str);\n+\t     !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY))) {\n+\t\tif (warn_ambiguous_refs > 1)\n+\t\t\tdie(warn_msg, len, str);\n+\t\telse\n+\t\t\twarning(warn_msg, len, str);\n+\t}\n \n \tif (reflog_len) {\n \t\tint nth, i;\n-- \n2.8.0.rc0.210.gd302cd2\n"},{"id":"281584","messageId":"alpine.DEB.2.20.1603231642580.4690@virtualbox","threadId":"41795","inReplyTo":"1458739840-15855-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] sha1_name.c: add an option to abort on ambiguous refs","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2016-03-23T15:45:42Z","receivedAt":"2016-03-23T15:45:42Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Duy,\n\nOn Wed, 23 Mar 2016, Nguyễn Thái Ngọc Duy wrote:\n\n> There are cases when a warning on ambiguous refs may go unnoticed\n> (e.g. git-log filling up the whole screen). There are also cases when\n> people want to catch ambiguity early (e.g. it happens deep in some\n> script). In either case, aborting the program would accomplish it.\n\nWhenever I see a die() in code outside of builtin/*.c, I cringe. I do that\nbecause it was *exactly* something like that that caused a serious\nregression in the builtin am so that we had to resort back to spawning\nseparate processes *just so* that we could catch error conditions and\nrun certain code in that case.\n\nMaybe there would be a way to do what you want to do that does not fly in\nthe face of libification? Maybe some strbuf with an atexit() that\naccumulates fatal errors that might be hidden and that are then written at\nthe end of the program (colorfully, if isatty(2))?\n\nCiao,\nDscho"},{"id":"281616","messageId":"CACsJy8D7B0wRiBqz6G3vrpLKUEGaoBvzf_a97R1LVU0UrKQ2rw@mail.gmail.com","threadId":"41795","inReplyTo":"alpine.DEB.2.20.1603231642580.4690@virtualbox","subject":"Re: [PATCH] sha1_name.c: add an option to abort on ambiguous refs","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-03-23T23:11:00Z","receivedAt":"2016-03-23T23:11:00Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Mar 23, 2016 at 10:45 PM, Johannes Schindelin\n<Johannes.Schindelin@gmx.de> wrote:\n> Hi Duy,\n>\n> On Wed, 23 Mar 2016, Nguyễn Thái Ngọc Duy wrote:\n>\n>> There are cases when a warning on ambiguous refs may go unnoticed\n>> (e.g. git-log filling up the whole screen). There are also cases when\n>> people want to catch ambiguity early (e.g. it happens deep in some\n>> script). In either case, aborting the program would accomplish it.\n>\n> Whenever I see a die() in code outside of builtin/*.c, I cringe. I do that\n> because it was *exactly* something like that that caused a serious\n> regression in the builtin am so that we had to resort back to spawning\n> separate processes *just so* that we could catch error conditions and\n> run certain code in that case.\n>\n> Maybe there would be a way to do what you want to do that does not fly in\n> the face of libification?\n\nSorry I got nothing better.\n\n> Maybe some strbuf with an atexit() that\n> accumulates fatal errors that might be hidden and that are then written at\n> the end of the program (colorfully, if isatty(2))?\n\nThat sounds hacky. If you don't want do die() deep inside then the\ncallers have to handle it, but get_sha1() is spread everywhere. Plus I\ndo not want to program to succeed when there is ambiguation because\nthe ref git chooses may not be what I want. I want it to abort (and\nalmost made a patch that die() unconditionally).\n-- \nDuy\n"},{"id":"290404","messageId":"CACsJy8CdXxJ6ydZ944sGZetDwpb8TTgNBtv3PLS=4dLT4mbXmw@mail.gmail.com","threadId":"41795","inReplyTo":"1458739840-15855-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] sha1_name.c: add an option to abort on ambiguous refs","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-28T17:55:52Z","receivedAt":"2016-06-28T17:56:38Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Bringing this up again after I've seen another person accidentally\ncreate a refs/heads/origin/some-branch and get really confused because\n\"git push\" reports up-to-date but the remote branch is not updated. I\ndon't know how he got into that situation, but I hope we should be\nable to catch it and suggest a way out.\n\nOn Wed, Mar 23, 2016 at 2:30 PM, Nguyễn Thái Ngọc Duy <pclouds@gmail.com> wrote:\n> There are cases when a warning on ambiguous refs may go unnoticed\n> (e.g. git-log filling up the whole screen). There are also cases when\n> people want to catch ambiguity early (e.g. it happens deep in some\n> script). In either case, aborting the program would accomplish it.\n>\n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n> ---\n>  Documentation/config.txt |  3 ++-\n>  config.c                 |  5 ++++-\n>  sha1_name.c              | 10 ++++++++--\n>  3 files changed, 14 insertions(+), 4 deletions(-)\n>\n> diff --git a/Documentation/config.txt b/Documentation/config.txt\n> index 2cd6bdd..4172f59 100644\n> --- a/Documentation/config.txt\n> +++ b/Documentation/config.txt\n> @@ -522,7 +522,8 @@ core.sharedRepository::\n>\n>  core.warnAmbiguousRefs::\n>         If true, Git will warn you if the ref name you passed it is ambiguous\n> -       and might match multiple refs in the repository. True by default.\n> +       and might match multiple refs in the repository. If set to \"fatal\",\n> +       the program will abort on ambiguous refs. True by default.\n>\n>  core.compression::\n>         An integer -1..9, indicating a default compression level.\n> diff --git a/config.c b/config.c\n> index 9ba40bc..79f1ea5 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -738,7 +738,10 @@ static int git_default_core_config(const char *var, const char *value)\n>         }\n>\n>         if (!strcmp(var, \"core.warnambiguousrefs\")) {\n> -               warn_ambiguous_refs = git_config_bool(var, value);\n> +               if (!strcasecmp(value, \"fatal\"))\n> +                       warn_ambiguous_refs = 2;\n> +               else\n> +                       warn_ambiguous_refs = git_config_bool(var, value);\n>                 return 0;\n>         }\n>\n> diff --git a/sha1_name.c b/sha1_name.c\n> index 3acf221..a0f0ab9 100644\n> --- a/sha1_name.c\n> +++ b/sha1_name.c\n> @@ -480,6 +480,8 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,\n>                                 warning(warn_msg, len, str);\n>                                 if (advice_object_name_warning)\n>                                         fprintf(stderr, \"%s\\n\", _(object_name_msg));\n> +                               if (warn_ambiguous_refs > 1)\n> +                                       die(_(\"cannot continue with ambiguous refs\"));\n>                         }\n>                         free(real_ref);\n>                 }\n> @@ -537,8 +539,12 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1,\n>\n>         if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&\n>             (refs_found > 1 ||\n> -            !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))\n> -               warning(warn_msg, len, str);\n> +            !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY))) {\n> +               if (warn_ambiguous_refs > 1)\n> +                       die(warn_msg, len, str);\n> +               else\n> +                       warning(warn_msg, len, str);\n> +       }\n>\n>         if (reflog_len) {\n>                 int nth, i;\n> --\n> 2.8.0.rc0.210.gd302cd2\n>\n\n\n\n-- \nDuy\n"}]}