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

Re: [PATCH v4 2/5] stash: convert apply to builtin

From
Joel Teichroeb <joel@teichroeb.net>
Date
Mar 31, 2018, 17:04 UTC
Message-ID
<CA+CzEk9vLRMGSMmdpEe6mm9f4f8+Cs+ZBb3sTS0TeZDbWKemcw@mail.gmail.com>
In-Reply-To
<xmqqlgea63xu.fsf@gitster-ct.c.googlers.com>
On Thu, Mar 29, 2018 at 1:07 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 60 quoted lines
> Joel Teichroeb <joel@teichroeb.net> writes:
>
>> +static int get_stash_info(struct stash_info *info, int argc, const char **argv)
>> +{
>
> So, this roughly corresponds to parse_flags_and_rev function, it seems.
>
>> +     struct strbuf w_commit_rev = STRBUF_INIT;
>> +     struct strbuf b_commit_rev = STRBUF_INIT;
>> +     struct strbuf w_tree_rev = STRBUF_INIT;
>> +     struct strbuf b_tree_rev = STRBUF_INIT;
>> +     struct strbuf i_tree_rev = STRBUF_INIT;
>> +     struct strbuf u_tree_rev = STRBUF_INIT;
>> +     struct strbuf symbolic = STRBUF_INIT;
>> +     struct strbuf out = STRBUF_INIT;
>> +     int ret;
>> +     const char *revision;
>> +     const char *commit = NULL;
>> +     char *end_of_rev;
>> +     info->is_stash_ref = 0;
>> +
>> +     if (argc > 1) {
>> +             int i;
>> +             struct strbuf refs_msg = STRBUF_INIT;
>> +             for (i = 0; i < argc; ++i)
>> +                     strbuf_addf(&refs_msg, " '%s'", argv[i]);
>> +
>> +             fprintf_ln(stderr, _("Too many revisions specified:%s"), refs_msg.buf);
>> +             strbuf_release(&refs_msg);
>> +
>> +             return -1;
>> +     }
>> +
>> +     if (argc == 1)
>> +             commit = argv[0];
>> +
>> +     strbuf_init(&info->revision, 0);
>> +     if (commit == NULL) {
>> +             if (have_stash()) {
>> +                     free_stash_info(info);
>> +                     return error(_("No stash entries found."));
>> +             }
>> +
>> +             strbuf_addf(&info->revision, "%s@{0}", ref_stash);
>> +     } else if (strspn(commit, "0123456789") == strlen(commit)) {
>> +             strbuf_addf(&info->revision, "%s@{%s}", ref_stash, commit);
>> +     } else {
>> +             strbuf_addstr(&info->revision, commit);
>> +     }
>> +
>> +     revision = info->revision.buf;
>> +     strbuf_addstr(&w_commit_rev, revision);
>> +     ret = !get_oid(w_commit_rev.buf, &info->w_commit);
>> +     strbuf_release(&w_commit_rev);
>
> Use of strbuf w_commit_rev looks completely pointless here.  Am I
> mistaken to say that the above three lines are equivalent to:
>
>         ret = !get_oid(revision, &info->w_commit);
>

Right, it was refactored to this in a previous version, but I didn't quite think it through.

Show 48 quoted lines
>> +
>> +     if (!ret) {
>> +             error(_("%s is not a valid reference"), revision);
>> +             free_stash_info(info);
>> +             return -1;
>> +     }
>> +
>> +     strbuf_addf(&b_commit_rev, "%s^1", revision);
>> +     strbuf_addf(&w_tree_rev, "%s:", revision);
>> +     strbuf_addf(&b_tree_rev, "%s^1:", revision);
>> +     strbuf_addf(&i_tree_rev, "%s^2:", revision);
>> +
>> +     ret = !get_oid(b_commit_rev.buf, &info->b_commit) &&
>> +             !get_oid(w_tree_rev.buf, &info->w_tree) &&
>> +             !get_oid(b_tree_rev.buf, &info->b_tree) &&
>> +             !get_oid(i_tree_rev.buf, &info->i_tree);
>> +
>> +     strbuf_release(&b_commit_rev);
>> +     strbuf_release(&w_tree_rev);
>> +     strbuf_release(&b_tree_rev);
>> +     strbuf_release(&i_tree_rev);
>
> For the same reason, these strbuf's look pretty much pointless.  I
> wonder if a private helper
>
>         static int grab_oid(struct oid *oid, const char *fmt, const char *rev)
>         {
>                 struct strbuf buf = STRBUF_INIT;
>                 int ret;
>
>                 strbuf_addf(&buf, fmt, rev);
>                 ret = get_oid(buf, oid);
>                 strbuf_release(&buf);
>                 return ret;
>         }
>
> would help here?  Then you wouldn't be writing something like the
> above, and instead you'd grab four object names like so:
>
>         if (grab_oid(&info->b_commit, "%s^1", revision) ||
>             grab_oid(&info->w_tree, "%s:", revision) ||
>             grab_oid(&info->b_tree, "%s&1:", revision) ||
>             grab_oid(&info->i_tree, "%s&2:", revision)) {
>                 ... we found an error ...
>                 return -1;
>         }
>
> which would be a lot easier to follow, no?

Very much agreed! I felt like that part of the code was the weakest part of my patch before. I'm very happy to have it cleaned up like this!

Show 26 quoted lines
>
>> +int cmd_stash__helper(int argc, const char **argv, const char *prefix)
>> +{
>> +     int result = 0;
>> +     pid_t pid = getpid();
>> +     const char *index_file;
>> +
>> +     struct option options[] = {
>> +             OPT_END()
>> +     };
>> +
>> +     git_config(git_default_config, NULL);
>> +
>> +     argc = parse_options(argc, argv, prefix, options, git_stash_helper_usage,
>> +             PARSE_OPT_KEEP_UNKNOWN|PARSE_OPT_KEEP_DASHDASH);
>> +
>> +     index_file = get_index_file();
>> +     xsnprintf(stash_index_path, PATH_MAX, "%s.stash.%"PRIuMAX, index_file, (uintmax_t)pid);
>
> Wouldn't it make more sense to get rid of PATH_MAX and hold it in a
> strbuf instead?  I.e.
>
>     static struct strbuf stash_index_path = STRBUF_INIT;
>     ...
>     strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, (uintmax_t)pid);
>
That makes it a lot cleaner, thanks!
Show 6 quoted lines
>> +     cd "$START_DIR"
>> +     git stash--helper apply "$@"
>> +     res=$?
>> +     cd_to_toplevel
>> +     return $res
>>  }
Previous: Junio C HamanoNext: Joel Teichroeb
Message 9 of 11 in “Convert some stash functionality to a builtin”
  1. 0/5 Convert some stash functionality to a builtinJoel Teichroeb, Mar 28, 2018
  2. 1/5 stash: improve option parsing test coverageJoel Teichroeb, Mar 28, 2018
  3. Eric SunshineMar 29, 2018
  4. Junio C HamanoMar 29, 2018
  5. 3/5 stash: convert drop and clear to builtinJoel Teichroeb, Mar 28, 2018
  6. Junio C HamanoMar 30, 2018
  7. 2/5 stash: convert apply to builtinJoel Teichroeb, Mar 28, 2018
  8. Junio C HamanoMar 29, 2018
  9. Joel TeichroebMar 31, 2018
  10. 5/5 stash: convert pop to builtinJoel Teichroeb, Mar 28, 2018
  11. 4/5 stash: convert branch to builtinJoel Teichroeb, Mar 28, 2018

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.