From: Junio C Hamano <gitster@pobox.com>
To: Joel Teichroeb <joel@teichroeb.net>
Cc: Git Mailing List <git@vger.kernel.org>,
Thomas Gummerer <t.gummerer@gmail.com>,
Christian Couder <christian.couder@gmail.com>,
Eric Sunshine <sunshine@sunshineco.com>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: Re: [PATCH v4 2/5] stash: convert apply to builtin
Date: Thu, 29 Mar 2018 13:07:41 -0700 [thread overview]
Message-ID: <xmqqlgea63xu.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20180328222129.22192-3-joel@teichroeb.net> (Joel Teichroeb's message of "Wed, 28 Mar 2018 15:21:26 -0700")
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);
> +
> + 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?
> +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);
> + cd "$START_DIR"
> + git stash--helper apply "$@"
> + res=$?
> + cd_to_toplevel
> + return $res
> }
next prev parent reply other threads:[~2018-03-29 20:07 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-28 22:21 [PATCH v4 0/5] Convert some stash functionality to a builtin Joel Teichroeb
2018-03-28 22:21 ` [PATCH v4 1/5] stash: improve option parsing test coverage Joel Teichroeb
2018-03-29 10:10 ` Eric Sunshine
2018-03-29 19:39 ` Junio C Hamano
2018-03-28 22:21 ` [PATCH v4 2/5] stash: convert apply to builtin Joel Teichroeb
2018-03-29 20:07 ` Junio C Hamano [this message]
2018-03-31 17:04 ` Joel Teichroeb
2018-03-28 22:21 ` [PATCH v4 3/5] stash: convert drop and clear " Joel Teichroeb
2018-03-30 16:17 ` Junio C Hamano
2018-03-28 22:21 ` [PATCH v4 4/5] stash: convert branch " Joel Teichroeb
2018-03-28 22:21 ` [PATCH v4 5/5] stash: convert pop " Joel Teichroeb
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqlgea63xu.fsf@gitster-ct.c.googlers.com \
--to=gitster@pobox.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=joel@teichroeb.net \
--cc=sunshine@sunshineco.com \
--cc=t.gummerer@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).