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

Re: [PATCH v3 06/10] diff-lib: define diff_get_merge_base()

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 17, 2020, 17:16 UTC
Message-ID
<xmqqd02ks4qk.fsf@gitster.c.googlers.com>
In-Reply-To
<6aac57ca022963fb41d93905e41dff36dccd5969.1600328335.git.liu.denton@gmail.com>
Denton Liu <liu.denton@gmail.com> writes:
Show 13 quoted lines
> +void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
> +{
> +	int i;
> +	struct commit *mb_child[2] = {0};
> +	struct commit_list *merge_bases;
> +
> +	for (i = 0; i < revs->pending.nr; i++) {
> +		struct object *obj = revs->pending.objects[i].item;
> +		if (obj->flags)
> +			die(_("--merge-base does not work with ranges"));
> +		if (obj->type != OBJ_COMMIT)
> +			die(_("--merge-base only works with commits"));
> +	}

This is the first use of die() in this file, that is designed to keep a set of reusable library functions so that the caller(s) can do their own die(). They may want to become

	return error(_(...));
The same comment applies to all die()s the patch adds.
Show 5 quoted lines
> +	/*
> +	 * This check must go after the for loop above because A...B
> +	 * ranges produce three pending commits, resulting in a
> +	 * misleading error message.
> +	 */

Should "git diff --merge-base A...B" be forbidden, or does it just ask the same thing twice and is not a die-worthy offence?

> +	if (revs->pending.nr > ARRAY_SIZE(mb_child))
> +		die(_("--merge-base does not work with more than two commits"));

Compare with hardcoded '2' in the condition. The requirement for mb_child[] is that it can hold at least 2 (changes in the future may find it convenient if its size gets increased to 3 to hold NULL sentinel to signal end-of-list, for example). Comparison with ARRAY_SIZE() may be appropriate in different situations but not here where the code knows it wants to reject more than two no matter how big mb_child[] is.

Show 7 quoted lines
> +	for (i = 0; i < revs->pending.nr; i++)
> +		mb_child[i] = lookup_commit_reference(the_repository, &revs->pending.objects[i].item->oid);
> +	if (revs->pending.nr < ARRAY_SIZE(mb_child)) {
> +		struct object_id oid;
> +
> +		if (revs->pending.nr != 1)
> +			BUG("unexpected revs->pending.nr: %d", revs->pending.nr);

This is an obviously impossible condition as we will not take more than 2.

Show 15 quoted lines
> +		if (get_oid("HEAD", &oid))
> +			die(_("unable to get HEAD"));
> +		mb_child[1] = lookup_commit_reference(the_repository, &oid);
> +	}
> +
> +	merge_bases = repo_get_merge_bases(the_repository, mb_child[0], mb_child[1]);
> +	if (!merge_bases)
> +		die(_("no merge base found"));
> +	if (merge_bases->next)
> +		die(_("multiple merge bases found"));
> +
> +	oidcpy(mb, &merge_bases->item->object.oid);
> +
> +	free_commit_list(merge_bases);
> +}
OK.
Show 12 quoted lines
>  int run_diff_index(struct rev_info *revs, unsigned int option)
>  {
>  	struct object_array_entry *ent;
> diff --git a/diff.h b/diff.h
> index 0cc874f2d5..ae2bb7001a 100644
> --- a/diff.h
> +++ b/diff.h
> @@ -580,6 +580,8 @@ void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc);
>   */
>  const char *diff_aligned_abbrev(const struct object_id *sha1, int);
>  
> +void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb);

Without an actual caller, we cannot judge if this interface is designed right, but we'll see soon in the next steps ;-)

Looking good so far.
Thanks.
> +
>  /* do not report anything on removed paths */
>  #define DIFF_SILENT_ON_REMOVED 01
>  /* report racily-clean paths as modified */
Previous: Denton LiuNext: Denton Liu
Message 42 of 58 in “builtin/diff: learn --merge-base”
  1. 0/4 builtin/diff: learn --merge-baseDenton Liu, Sep 5, 2020
  2. 1/4 t4068: remove unnecessary >tmpDenton Liu, Sep 5, 2020
  3. 3/4 builtin/diff: parse --no-index using parse_options()Denton Liu, Sep 5, 2020
  4. 2/4 git-diff.txt: backtick quote command textDenton Liu, Sep 5, 2020
  5. 4/4 builtin/diff: learn --merge-baseDenton Liu, Sep 5, 2020
  6. Junio C HamanoSep 6, 2020
  7. 0/4 builtin/diff: learn --merge-baseDenton Liu, Sep 10, 2020
  8. 1/4 t4068: remove unnecessary >tmpDenton Liu, Sep 10, 2020
  9. 2/4 git-diff.txt: backtick quote command textDenton Liu, Sep 10, 2020
  10. 3/4 builtin/diff: parse --no-index using parse_options()Denton Liu, Sep 10, 2020
  11. Junio C HamanoSep 10, 2020
  12. Denton LiuSep 13, 2020
  13. Junio C HamanoSep 13, 2020
  14. 4/4 builtin/diff: learn --merge-baseDenton Liu, Sep 10, 2020
  15. 00/10 builtin/diff: learn --merge-baseDenton Liu, Sep 17, 2020
  16. 01/10 t4068: remove unnecessary >tmpDenton Liu, Sep 17, 2020
  17. 02/10 git-diff-index.txt: make --cached description a proper sentenceDenton Liu, Sep 17, 2020
  18. 10/10 contrib/completion: complete `git diff --merge-base`Denton Liu, Sep 17, 2020
  19. 09/10 builtin/diff-tree: learn --merge-baseDenton Liu, Sep 17, 2020
  20. Junio C HamanoSep 17, 2020
  21. Denton LiuSep 18, 2020
  22. Junio C HamanoSep 18, 2020
  23. Denton LiuSep 20, 2020
  24. Junio C HamanoSep 21, 2020
  25. Denton LiuSep 21, 2020
  26. Junio C HamanoSep 21, 2020
  27. Junio C HamanoSep 21, 2020
  28. Denton LiuSep 21, 2020
  29. Junio C HamanoSep 21, 2020
  30. Denton LiuSep 23, 2020
  31. Junio C HamanoSep 25, 2020
  32. Denton LiuSep 26, 2020
  33. 08/10 builtin/diff-index: learn --merge-baseDenton Liu, Sep 17, 2020
  34. Junio C HamanoSep 17, 2020
  35. Jeff KingSep 17, 2020
  36. Junio C HamanoSep 18, 2020
  37. Jeff KingSep 18, 2020
  38. 05/10 diff-lib: accept option flags in run_diff_index()Denton Liu, Sep 17, 2020
  39. Junio C HamanoSep 17, 2020
  40. 04/10 contrib/completion: extract common diff/difftool optionsDenton Liu, Sep 17, 2020
  41. 06/10 diff-lib: define diff_get_merge_base()Denton Liu, Sep 17, 2020
  42. Junio C HamanoSep 17, 2020
  43. Denton LiuSep 18, 2020
  44. Junio C HamanoSep 19, 2020
  45. 03/10 git-diff.txt: backtick quote command textDenton Liu, Sep 17, 2020
  46. 07/10 t4068: add --merge-base testsDenton Liu, Sep 17, 2020
  47. 00/10 builtin/diff: learn --merge-baseDenton Liu, Sep 20, 2020
  48. 01/10 t4068: remove unnecessary >tmpDenton Liu, Sep 20, 2020
  49. 02/10 git-diff-index.txt: make --cached description a proper sentenceDenton Liu, Sep 20, 2020
  50. 06/10 diff-lib: define diff_get_merge_base()Denton Liu, Sep 20, 2020
  51. 05/10 diff-lib: accept option flags in run_diff_index()Denton Liu, Sep 20, 2020
  52. 03/10 git-diff.txt: backtick quote command textDenton Liu, Sep 20, 2020
  53. 07/10 t4068: add --merge-base testsDenton Liu, Sep 20, 2020
  54. 08/10 builtin/diff-index: learn --merge-baseDenton Liu, Sep 20, 2020
  55. Martin ÅgrenSep 29, 2020
  56. 04/10 contrib/completion: extract common diff/difftool optionsDenton Liu, Sep 20, 2020
  57. 09/10 builtin/diff-tree: learn --merge-baseDenton Liu, Sep 20, 2020
  58. 10/10 contrib/completion: complete `git diff --merge-base`Denton Liu, Sep 20, 2020

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.