[prev in list] [next in list] [prev in thread] [next in thread]
List: git
Subject: Re: [PATCH v4 2/5] stash: convert apply to builtin
From: Joel Teichroeb <joel () teichroeb ! net>
Date: 2018-03-31 17:04:32
Message-ID: CA+CzEk9vLRMGSMmdpEe6mm9f4f8+Cs+ZBb3sTS0TeZDbWKemcw () mail ! gmail ! com
[Download RAW message or body]
On Thu, Mar 29, 2018 at 1:07 PM, Junio C Hamano <gitster@pobox.com> wrote:
> 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.
>> +
>> + 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!
>
>> +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!
>> + cd "$START_DIR"
>> + git stash--helper apply "$@"
>> + res=$?
>> + cd_to_toplevel
>> + return $res
>> }
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic