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

Re: [RFC PATCH 1/2] add-patch: compare object id instead of literal string

From
Ghanshyam Thakkar <shyamthakkar001@gmail.com>
Date
Jan 30, 2024, 06:39 UTC
Message-ID
<CYRU26F9KCDF.2XI7VRT7N04OC@gmail.com>
In-Reply-To
<ZbeQmv_KcChtrPqJ@tanuki>
On Mon Jan 29, 2024 at 5:18 PM IST, Patrick Steinhardt wrote:
Show 6 quoted lines
> On Sun, Jan 28, 2024 at 11:41:22PM +0530, Ghanshyam Thakkar wrote:
>
> We typically start commit messages with an explanation of what the
> actual problem is that the commit is trying to solve. This helps to set
> the stage for any reviewers so that they know why you're doing changes
> in the first place.
I will keep that in mind for future patches.
Show 30 quoted lines
> > 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.
> > 
> > Subsequently, replace literal string comparison to HEAD in run_add_p()
> > with reveq() to handle more ways of saying HEAD (such as '@' or '$branch'
> > where $branch points to same commit as HEAD). This addresses the
> > NEEDSWORK comment in run_add_p().
> > 
> > However, in ADD_P_RESET mode keep string comparison in logical OR with
> > reveq() to handle unborn HEAD.
> > 
> > As for the behavior change, with this patch applied if the given
> > revision points to the same object as HEAD, the patch mode will be set to
> > patch_mode_(reset,checkout,worktree)_head instead of
> > patch_mode_(...)_nothead. That is equivalent of not setting -R flag in
> > diff-index, which would have been otherwise set before this patch.
> > However, when given same set of inputs, the actual outcome is same as
> > before this patch. Therefore, this does not affect any automated scripts.
>
> So this is the closest to an actual description of what your goal is.
> But it doesn't say why that is a good idea, it only explains the change
> in behaviour.
>
> I think the best thing to do would be to give a sequence of Git commands
> that demonstrate the problem that you are trying to solve. This would
> help the reader gain a high-level understanding of what you propose to
> change.

Yeah, my original motive was to support '@' as a shorthand for HEAD. But, since '@' can also be used as branch name, I thought of comparing object ids instead of string comparison in accordance with the NEEDSWORK comment. However, as Junio pointed out, treating a branch name revision that points to same commit as HEAD, as HEAD would just cause confusion.

Show 51 quoted lines
> > Also, add testcases to check the similarity of result between different
> > ways of saying HEAD.
> > 
> > Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
> > ---
> > 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().
> > 
> >  add-patch.c               | 28 +++++++++++++++-------
> >  t/t2016-checkout-patch.sh | 50 +++++++++++++++++++++++----------------
> >  t/t2071-restore-patch.sh  | 21 ++++++++++------
> >  t/t7105-reset-patch.sh    | 14 +++++++++++
> >  4 files changed, 77 insertions(+), 36 deletions(-)
> > 
> > diff --git a/add-patch.c b/add-patch.c
> > index 79eda168eb..01eb71d90e 100644
> > --- a/add-patch.c
> > +++ b/add-patch.c
> > @@ -14,6 +14,7 @@
> >  #include "color.h"
> >  #include "compat/terminal.h"
> >  #include "prompt.h"
> > +#include "hash.h"
> >  
> >  enum prompt_mode_type {
> >  	PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_ADDITION, PROMPT_HUNK,
> > @@ -316,6 +317,18 @@ static void setup_child_process(struct add_p_state *s,
> >  		     INDEX_ENVIRONMENT "=%s", s->s.r->index_file);
> >  }
> >  
> > +// Check if two revisions point to the same object. Passing a rev which does not
> > +// point to an object is undefined behavior.
>
> We only use `/* */`-style comments in the Git codebase.
>
> > +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);
> > +}
>
> I don't think it's a good idea to allow for undefined behaviour here.
> While more tedious for the caller, I think it's preferable to handle the
> case correctly where revisions don't resolve, e.g. by returning `-1` in
> case either of the revisions does not resolve.
Will update it. 
Thanks.
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 4 of 46 in “add-patch: compare object id”
  1. Ghanshyam ThakkarJan 28, 2024
  2. 1/2 add-patch: compare object id instead of literal stringGhanshyam Thakkar, Jan 28, 2024
  3. Patrick SteinhardtJan 29, 2024
  4. Ghanshyam ThakkarJan 30, 2024
  5. Junio C HamanoJan 30, 2024
  6. Junio C HamanoJan 29, 2024
  7. Junio C HamanoJan 29, 2024
  8. Ghanshyam ThakkarJan 30, 2024
  9. 2/2 checkout: remove HEAD special caseGhanshyam Thakkar, Jan 28, 2024
  10. Patrick SteinhardtJan 29, 2024
  11. 0/2 add-patch: Support '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 2, 2024
  12. 1/2 add-patch: remove non-relevant NEEDSWORK commentGhanshyam Thakkar, Feb 2, 2024
  13. 2/2 add-patch: classify '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 2, 2024
  14. Junio C HamanoFeb 2, 2024
  15. Junio C HamanoFeb 2, 2024
  16. Ghanshyam ThakkarFeb 2, 2024
  17. Ghanshyam ThakkarFeb 2, 2024
  18. Ghanshyam ThakkarFeb 2, 2024
  19. 0/2 add-patch: '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 3, 2024
  20. 0/3 '@' as a synonym for 'HEAD' in patch modeGhanshyam Thakkar, Feb 6, 2024
  21. 0/2 add-patch: classify '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 11, 2024
  22. 0/2 add-patch: classify '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 13, 2024
  23. Phillip WoodFeb 14, 2024
  24. 1/2 add-patch: classify '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 13, 2024
  25. 2/2 add -p tests: remove PERL prerequisitesGhanshyam Thakkar, Feb 13, 2024
  26. 1/2 add-patch: classify '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 11, 2024
  27. Junio C HamanoFeb 12, 2024
  28. 2/2 add -p tests: remove PERL prerequisitesGhanshyam Thakkar, Feb 11, 2024
  29. 1/3 add-patch: remove unnecessary NEEDSWORK commentGhanshyam Thakkar, Feb 6, 2024
  30. Phillip WoodFeb 7, 2024
  31. 2/3 add-patch: classify '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 6, 2024
  32. Junio C HamanoFeb 7, 2024
  33. Phillip WoodFeb 7, 2024
  34. Ghanshyam ThakkarFeb 9, 2024
  35. 3/3 add -p tests: remove Perl prerequisiteGhanshyam Thakkar, Feb 6, 2024
  36. Phillip WoodFeb 7, 2024
  37. Phillip WoodFeb 7, 2024
  38. Junio C HamanoFeb 7, 2024
  39. Eric SunshineFeb 7, 2024
  40. 1/2 add-patch: remove unnecessary NEEDSWORK commentGhanshyam Thakkar, Feb 3, 2024
  41. 2/2 add-patch: classify '@' as a synonym for 'HEAD'Ghanshyam Thakkar, Feb 3, 2024
  42. Junio C HamanoFeb 3, 2024
  43. Ghanshyam ThakkarFeb 5, 2024
  44. Phillip WoodFeb 5, 2024
  45. Ghanshyam ThakkarFeb 5, 2024
  46. Junio C HamanoFeb 5, 2024

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.