git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 2/3] advice: revamp advise API

From
Heba Waly <heba.waly@gmail.com>
Date
Feb 24, 2020, 23:49 UTC
Message-ID
<CACg5j25XQLC1bYxm4244UQddL5AVENg-0XwJQFvLpdQ-n1hVqg@mail.gmail.com>
In-Reply-To
<xmqqftezod3n.fsf@gitster-ct.c.googlers.com>
On Tue, Feb 25, 2020 at 11:05 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 19 quoted lines
>
> "Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > -static void vadvise(const char *advice, va_list params)
> > +static const char *advice_config_keys[] = {
> > +     [FETCH_SHOW_FORCED_UPDATES]              = "fetchShowForcedUpdates",
> > +     [PUSH_UPDATE_REJECTED]                   = "pushUpdateRejected",
> > +     /* make this an alias for backward compatibility */
> > +     [PUSH_UPDATE_REJECTED_ALIAS]             = "pushNonFastForward",
> > +...
> > +     [CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]  = "checkoutAmbiguousRemoteBranchName",
> > +     [NESTED_TAG]                             = "nestedTag",
> > +     [SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
> > +};
>
> Terminate the last entry of the array with a trailing comma ',', so
> that the next person who adds one new advise key to the table at the
> end has to just add only one line without changing any existing lines.
>
Sure, thank you for the explanation, that makes sense.
Show 7 quoted lines
> As you are using the designated initializers for this array, we are
> free to order the lines in any way that makes most sense to us and
> do not have to order the lines in numerical order.  In what order
> are these lines sorted right now?  I am tempted to suggest that we
> should sort alphabetically on the values, i.e. run the contents of
> the table through "LC_ALL=C sort -k2,2 -t=".
>

Currently it's sorted in the same order that was originally used for advice_config[] and the global variables, which I assume is the order in which every config variable was added to the code. Sorting alphabetically will be neater of course.

Show 13 quoted lines
> > +
> > +static const char turn_off_instructions[] =
> > +N_("\n"
> > +   "Disable this message with \"git config %s false\"");
> > +
> > +static void vadvise(const char *advice, va_list params,
> > +                 int display_instructions, char *key)
>
> It may be just me, but I feel uneasy when I see va_list in the
> middle of the parameter list.  As it is a mechanism to allow us
> handle "the remainder of the arguments", it logically makes more
> sense to have it as the last parameter.
>
I don't mind it either way, so yeah no problem, I'll change that.
Show 11 quoted lines
> >  {
> >       struct strbuf buf = STRBUF_INIT;
> >       const char *cp, *np;
> >
> >       strbuf_vaddf(&buf, advice, params);
> >
> > +     if(display_instructions)
> > +             strbuf_addf(&buf, turn_off_instructions, key);
>
> Style.  We always have one SP between a syntactic keyword like "if"
> and open parenthesis.
Noted.
Show 21 quoted lines
>
> > +
> >       for (cp = buf.buf; *cp; cp = np) {
> >               np = strchrnul(cp, '\n');
> >               fprintf(stderr, _("%shint: %.*s%s\n"),
> > @@ -119,8 +161,42 @@ void advise(const char *advice, ...)
> >  {
> >       va_list params;
> >       va_start(params, advice);
> > -     vadvise(advice, params);
> > +     vadvise(advice, params, 0, "");
> > +     va_end(params);
> > +}
> > +
> > +static int get_config_value(enum advice_type type)
> > +{
> > +     int value = 1;
> > +     char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
>
> Have a blank line between the decl and the first statement, i.e. here.
>
Right, I missed this one.
Show 10 quoted lines
> > +     git_config_get_bool(key, &value);
> > +     free(key);
> > +     return value;
> > +}
> > +
> > +int advice_enabled(enum advice_type type)
> > +{
> > +     switch(type) {
>
> Style.
Noted.
Show 17 quoted lines
>
> > +     case PUSH_UPDATE_REJECTED:
> > +             return get_config_value(PUSH_UPDATE_REJECTED) &&
> > +                    get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
> > +     default:
> > +             return get_config_value(type);
> > +     }
> > +}
> > +
> > +void advise_if_enabled(enum advice_type type, const char *advice, ...)
> > +{
> > +     char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> > +     va_list params;
> > +
> > +     if(!advice_enabled(type))
>
> Stile.
Takes time changing the style I've been using for years, sorry.
Show 43 quoted lines
>
> > +             return;
> > +
> > +     va_start(params, advice);
> > +     vadvise(advice, params, 1, key);
> >       va_end(params);
> > +     free(key);
> >  }
> >
> >  int git_default_advice_config(const char *var, const char *value)
> > @@ -159,8 +235,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
> >  {
> >       int i;
> >
> > -     for (i = 0; i < ARRAY_SIZE(advice_config); i++)
> > -             list_config_item(list, prefix, advice_config[i].name);
> > +     for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
> > +             list_config_item(list, prefix, advice_config_keys[i]);
> >  }
> >
> >  int error_resolve_conflict(const char *me)
> > diff --git a/advice.h b/advice.h
> > index b706780614d..61a7ee82827 100644
> > --- a/advice.h
> > +++ b/advice.h
> > @@ -32,9 +32,61 @@ extern int advice_checkout_ambiguous_remote_branch_name;
> >  extern int advice_nested_tag;
> >  extern int advice_submodule_alternate_error_strategy_die;
> >
> > +/**
> > + To add a new advice, you need to:
> > + - Define an advice_type.
> > + - Add a new entry to advice_config_keys list.
> > + - Add the new config variable to Documentation/config/advice.txt.
> > + - Call advise_if_enabled to print your advice.
> > + */
>
>     /*
>      * Our multi-line comments should look
>      * more like this (multiple style violations
>      * in this patch).
>      */
>
Got it.
Show 15 quoted lines
> > +enum advice_type {
> > +     FETCH_SHOW_FORCED_UPDATES = 0,
> > +     PUSH_UPDATE_REJECTED = 1,
> > +     PUSH_UPDATE_REJECTED_ALIAS = 2,
> > +     PUSH_NON_FF_CURRENT = 3,
>
> Do we need to spell out the values, or is it sufficient to let the
> compiler automatically count up?  Does any code depend on the exact
> numeric value of the advice type, or at least at the source code
> level the only thing we care about them is that they are distinct?
>
> I'd really want to get rid of these exact value assignments---they
> are source of unnecessary conflicts when two or more topics want to
> add new advice types of their own.
>
Yes, we can get rid of them.
> I also suggest that these are sorted alphabetically.

As we'll get rid of the value assignments, so sorting alphabetically should not introduce problems when adding new advice types, will do.

Show 43 quoted lines
> > + ...
> > +     SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
> > +};
>
> > +++ b/t/t0018-advice.sh
> > @@ -0,0 +1,28 @@
> > +#!/bin/sh
> > +
> > +test_description='Test advise_if_enabled functionality'
> > +
> > +. ./test-lib.sh
> > +
> > +cat > expected <<EOF
> > +hint: This is a piece of advice
> > +hint: Disable this message with "git config advice.nestedTag false"
> > +EOF
> > +test_expect_success 'advise should be printed when config variable is unset' '
> > +     test-tool advise "This is a piece of advice" 2>actual &&
> > +     test_i18ncmp expected actual
> > +'
>
>  - Prepare the expected output inside test_expect_success block that
>    uses it.
>
>  - There should be no SP between a redirection operator and the
>    filename.
>
>  - Here-doc that does not use parameter expansion should use a
>    quoted EOF marker.
>
>  - The file that gets compared with "actual" is by convention called
>    "expect", not "expected".
>
> i.e.
>
> test_expect_success 'advise should be printed when config variable is unset' '
>         cat >expect <<-\EOF &&
>         hint: ...
>         hint: ...
>         EOF
>         test-tool advise "This is a piece of advice" 2>actual &&
>         test_i18ncmp expected actual
> '
Noted.
Will send an updated version soon.

Thank you, Heba

Previous: Heba WalyNext: Emily Shaffer
Message 43 of 76 in “advice: refactor advise API”
  1. advice: refactor advise APIHeba Waly via GitGitGadget, Feb 10, 2020
  2. Derrick StoleeFeb 10, 2020
  3. Junio C HamanoFeb 10, 2020
  4. Taylor BlauFeb 10, 2020
  5. Emily ShafferFeb 10, 2020
  6. Heba WalyFeb 11, 2020
  7. Taylor BlauFeb 12, 2020
  8. Heba WalyFeb 10, 2020
  9. Derrick StoleeFeb 11, 2020
  10. Jeff KingFeb 10, 2020
  11. Emily ShafferFeb 10, 2020
  12. Heba WalyFeb 11, 2020
  13. Jeff KingFeb 11, 2020
  14. Jeff KingFeb 11, 2020
  15. Heba WalyFeb 11, 2020
  16. Junio C HamanoFeb 10, 2020
  17. Heba WalyFeb 11, 2020
  18. Junio C HamanoFeb 11, 2020
  19. 0/2 [RFC][Outreachy] advice: refactor advise APIHeba Waly via GitGitGadget, Feb 16, 2020
  20. 2/2 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 16, 2020
  21. 1/2 advice: refactor advise APIHeba Waly via GitGitGadget, Feb 16, 2020
  22. Junio C HamanoFeb 17, 2020
  23. Heba WalyFeb 17, 2020
  24. Heba WalyFeb 19, 2020
  25. Junio C HamanoFeb 17, 2020
  26. 0/2 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 19, 2020
  27. 2/2 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 19, 2020
  28. Emily ShafferFeb 20, 2020
  29. Heba WalyFeb 21, 2020
  30. 1/2 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 19, 2020
  31. Emily ShafferFeb 20, 2020
  32. Heba WalyFeb 21, 2020
  33. 0/3 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 24, 2020
  34. 1/3 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 24, 2020
  35. Emily ShafferFeb 24, 2020
  36. 3/3 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 24, 2020
  37. Junio C HamanoFeb 24, 2020
  38. Emily ShafferFeb 24, 2020
  39. 2/3 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 24, 2020
  40. Junio C HamanoFeb 24, 2020
  41. Eric SunshineFeb 24, 2020
  42. Heba WalyFeb 24, 2020
  43. Heba WalyFeb 24, 2020
  44. Emily ShafferFeb 24, 2020
  45. 0/3 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 25, 2020
  46. 1/3 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 25, 2020
  47. 2/3 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 25, 2020
  48. Junio C HamanoFeb 25, 2020
  49. Emily ShafferFeb 25, 2020
  50. Junio C HamanoFeb 25, 2020
  51. Junio C HamanoFeb 25, 2020
  52. Heba WalyFeb 25, 2020
  53. Junio C HamanoFeb 25, 2020
  54. Heba WalyFeb 26, 2020
  55. Junio C HamanoFeb 26, 2020
  56. Heba WalyFeb 26, 2020
  57. Junio C HamanoFeb 26, 2020
  58. Jonathan TanFeb 26, 2020
  59. 3/3 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 25, 2020
  60. Junio C HamanoFeb 25, 2020
  61. 0/4 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 27, 2020
  62. 1/4 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 27, 2020
  63. 2/4 advice: change "setupStreamFailure" to "setUpstreamFailure"Heba Waly via GitGitGadget, Feb 27, 2020
  64. Junio C HamanoFeb 27, 2020
  65. 3/4 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 27, 2020
  66. Junio C HamanoFeb 27, 2020
  67. Heba WalyFeb 29, 2020
  68. 4/4 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 27, 2020
  69. 0/4 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Mar 2, 2020
  70. 1/4 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Mar 2, 2020
  71. 2/4 advice: change "setupStreamFailure" to "setUpstreamFailure"Heba Waly via GitGitGadget, Mar 2, 2020
  72. 4/4 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Mar 2, 2020
  73. 3/4 advice: revamp advise APIHeba Waly via GitGitGadget, Mar 2, 2020
  74. Junio C HamanoMar 2, 2020
  75. Junio C HamanoMar 3, 2020
  76. Heba WalyMar 4, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.