git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [RFC PATCH 1/2] add-patch: compare object id instead of literal string
Date: Mon, 29 Jan 2024 10:27:10 -0800	[thread overview]
Message-ID: <xmqqmssohu69.fsf@gitster.g> (raw)
In-Reply-To: <20240128181202.986753-3-shyamthakkar001@gmail.com> (Ghanshyam Thakkar's message of "Sun, 28 Jan 2024 23:41:22 +0530")

Ghanshyam Thakkar <shyamthakkar001@gmail.com> writes:

> Add a new function reveq(), which takes repository struct and two revision
> strings as arguments and returns 0 if the revisions point to the same
> object. Passing a rev which does not point to an object is considered
> undefined behavior as the underlying function memcmp() will be called
> with NULL hash strings.

I didn't dug into the patch or the codepath it touches, but
I wonder if it has something (possibly historical) to do with the
fact that these two behave quite differently:

    $ git checkout HEAD
    $ git checkout HEAD^0

With the former, you will stay on your current branch, while with
the latter you detach at the commit at the tip of your current
branch.  Granted that "git checkout -p" is not about moving HEAD but
checking out some files to the worktree from a given tree-ish, anytime
I see code that does strcmp() with a fixed "HEAD" string, that is
one consideration I'd look for.

> Should the return values of repo_get_oid() be checked in reveq()? As
> reveq() is not a global function and is only used in run_add_p(), the
> validity of revisions is already checked beforehand by builtin/checkout.c
> and builtin/reset.c before the call to run_add_p().

If this were to become a public function (even if it somehow turns
out that it is a bad idea to move away from an explicit comparison
with "HEAD", introducing such a function might be useful---I dunno),
it probably makes sense not to burden its potential callers with too
many assumption.  But doesn't the fact that the immediate callers
you are introducing already checked the validity of the revisions
tell us something?  Would it result in us not needing this new
helper function at all, if we rearranged the code that already
checks the validity so that the actual object names are collected?
Then it would become the matter of running oideq() directly on these
object names, instead of calling a new helper function that
(re)converts from strings to object names and compares them.

> +// Check if two revisions point to the same object. Passing a rev which does not
> +// point to an object is undefined behavior.

Style:

    /*
     * Our multi-line comments look
     * like this.
     */

> +static inline int reveq(struct repository *r, const char *rev1,
> +			const char *rev2)
> +{
> +	struct object_id oid_rev1, oid_rev2;
> +	repo_get_oid(r, rev1, &oid_rev1);
> +	repo_get_oid(r, rev2, &oid_rev2);
> +
> +	return !oideq(&oid_rev1, &oid_rev2);

Horribly confusing.  If oideq() says "yes, they are the same" by
returning 0, then any helper function derived from it to ansewr "are
X and Y the same?" should return 0 when it wants to say "yes, they
are the same" to help developers keep their sanity.

> +}
> +
>  static int parse_range(const char **p,
>  		       unsigned long *offset, unsigned long *count)
>  {
> @@ -1730,28 +1743,25 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
>  		s.mode = &patch_mode_stash;
>  	else if (mode == ADD_P_RESET) {
>  		/*
> -		 * NEEDSWORK: Instead of comparing to the literal "HEAD",
> -		 * compare the commit objects instead so that other ways of
> -		 * saying the same thing (such as "@") are also handled
> -		 * appropriately.
> -		 *
> -		 * This applies to the cases below too.
> +		 * The literal string comparison to HEAD below is kept
> +		 * to handle unborn HEAD.
>  		 */

So, does this change solve the NEEDSWORK comment?  On an unborn
HEAD, this would still not allow you to say "@".  Only "HEAD" is
supported.

Not that I necessarily agree with the original "NEEDSWORK" comment
(I think it is perfectly fine for this or any other codepaths not to
take "@" as "HEAD"), but if that desire still stands here, should
the resulting comment still mention it with a NEEDSWORK label?

Besides ...

> diff --git a/t/t2016-checkout-patch.sh b/t/t2016-checkout-patch.sh
> index 747eb5563e..431f34fa9c 100755
> --- a/t/t2016-checkout-patch.sh
> +++ b/t/t2016-checkout-patch.sh
> @@ -12,6 +12,7 @@ test_expect_success 'setup' '
>  	git commit -m initial &&
>  	test_tick &&
>  	test_commit second dir/foo head &&
> +	git branch newbranch &&
>  	set_and_save_state bar bar_work bar_index &&
>  	save_head
>  '
> +# Note: 'newbranch' points to the same commit as HEAD. And it is technically
> +# allowed to name a branch '@' as of now, however in below test '@'
> +# represents the shortcut for HEAD.
> +for opt in "HEAD" "@" "newbranch"
> +do
> +	test_expect_success "git checkout -p $opt with NO staged changes: abort" '
> +		set_and_save_state dir/foo work head &&
> +		test_write_lines n y n | git checkout -p $opt >output &&
> +		verify_saved_state bar &&
> +		verify_saved_state dir/foo &&
> +		test_grep "Discard" output
> +	'

I think this change in behaviour, especially for "newbranch" that
used to use the "_nothead" variants of directions and messages, is
way too confusing.  Users may consider "HEAD" and "@" the same and
may want them to behave the same way, but the user, when explicitly
naming "newbranch", means they want to "check contents out of that
OTHER thing named 'newbranch', not the current branch"; it may or
may not happen to be pointing at the same commit as HEAD, but if
the user meant to say "check contents out of the current commit,
(partially) reverting the local changes I have", the user would have
said HEAD.  After all, the user may not even be immediately aware
that "newbranch" happens to point at the same commit as HEAD.

So, after thinking about it a bit more, I do not think I agree with
the NEEDSWORK comment.  I can buy "@", but not an arbitrary revision
name that happens to point at the same commit as HEAD.  In other
words, I may be persuaded to thinking into it is a good idea to add:

    static inline int user_means_HEAD(const char *a)
    {
	return !strcmp(a, "HEAD") || !strcmp(a, "@");
    }

and replace "!strcmp(rev, "HEAD")" with "user_means_HEAD(rev)", but
I would not go any further than that.

Thanks.





  parent reply	other threads:[~2024-01-29 18:27 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-28 18:11 [GSOC][RFC PATCH 0/2] add-patch: compare object id Ghanshyam Thakkar
2024-01-28 18:11 ` [RFC PATCH 1/2] add-patch: compare object id instead of literal string Ghanshyam Thakkar
2024-01-29 11:48   ` Patrick Steinhardt
2024-01-30  6:39     ` Ghanshyam Thakkar
2024-01-30 16:42       ` Junio C Hamano
2024-01-29 18:27   ` Junio C Hamano [this message]
2024-01-29 18:58     ` Junio C Hamano
2024-01-30  5:35     ` Ghanshyam Thakkar
2024-01-28 18:11 ` [RFC PATCH 2/2] checkout: remove HEAD special case Ghanshyam Thakkar
2024-01-29 11:48   ` Patrick Steinhardt
2024-02-02 15:03 ` [PATCH v2 0/2] add-patch: Support '@' as a synonym for 'HEAD' Ghanshyam Thakkar
2024-02-02 15:03   ` [PATCH v2 1/2] add-patch: remove non-relevant NEEDSWORK comment Ghanshyam Thakkar
2024-02-02 15:03   ` [PATCH v2 2/2] add-patch: classify '@' as a synonym for 'HEAD' Ghanshyam Thakkar
2024-02-02 17:08     ` Junio C Hamano
2024-02-02 17:43       ` Junio C Hamano
2024-02-02 17:53         ` Ghanshyam Thakkar
2024-02-02 17:51       ` Ghanshyam Thakkar
2024-02-02 17:31   ` [PATCH v2 0/2] add-patch: Support " Ghanshyam Thakkar
2024-02-03 11:25   ` [PATCH v3 0/2] add-patch: " Ghanshyam Thakkar
2024-02-06 22:50     ` [PATCH v4 0/3] '@' as a synonym for 'HEAD' in patch mode Ghanshyam Thakkar
2024-02-11 20:20       ` [PATCH v5 0/2] add-patch: classify '@' as a synonym for 'HEAD' Ghanshyam Thakkar
2024-02-13  0:05         ` [PATCH v6 " Ghanshyam Thakkar
2024-02-14 11:06           ` Phillip Wood
2024-02-13  0:05         ` [PATCH v6 1/2] " Ghanshyam Thakkar
2024-02-13  0:05         ` [PATCH v6 2/2] add -p tests: remove PERL prerequisites Ghanshyam Thakkar
2024-02-11 20:20       ` [PATCH v5 1/2] add-patch: classify '@' as a synonym for 'HEAD' Ghanshyam Thakkar
2024-02-12 21:45         ` Junio C Hamano
2024-02-11 20:20       ` [PATCH v5 2/2] add -p tests: remove PERL prerequisites Ghanshyam Thakkar
2024-02-06 22:50     ` [PATCH v4 1/3] add-patch: remove unnecessary NEEDSWORK comment Ghanshyam Thakkar
2024-02-07 10:51       ` Phillip Wood
2024-02-06 22:50     ` [PATCH v4 2/3] add-patch: classify '@' as a synonym for 'HEAD' Ghanshyam Thakkar
2024-02-07  1:05       ` Junio C Hamano
2024-02-07 10:38         ` Phillip Wood
2024-02-09 15:57         ` Ghanshyam Thakkar
2024-02-06 22:50     ` [PATCH v4 3/3] add -p tests: remove Perl prerequisite Ghanshyam Thakkar
2024-02-07 10:50       ` Phillip Wood
2024-02-07 13:51         ` Phillip Wood
2024-02-07 16:02           ` Junio C Hamano
2024-02-07 16:58           ` Eric Sunshine
2024-02-03 11:25   ` [PATCH v3 1/2] add-patch: remove unnecessary NEEDSWORK comment Ghanshyam Thakkar
2024-02-03 11:25   ` [PATCH v3 2/2] add-patch: classify '@' as a synonym for 'HEAD' Ghanshyam Thakkar
2024-02-03 22:33     ` Junio C Hamano
2024-02-05 15:14       ` Ghanshyam Thakkar
2024-02-05 16:37     ` Phillip Wood
2024-02-05 20:38       ` Ghanshyam Thakkar
2024-02-05 23:07       ` Junio C Hamano

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=xmqqmssohu69.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=shyamthakkar001@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).