diff mbox series

[GSOC,RFC] format-patch: pass --right-only to range-diff

Message ID pull.898.git.1615100240295.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series [GSOC,RFC] format-patch: pass --right-only to range-diff | expand

Commit Message

ZheNing Hu March 7, 2021, 6:57 a.m. UTC
From: ZheNing Hu <adlternative@gmail.com>

In https://lore.kernel.org/git/YBx5rmVsg1LJhSKN@nand.local/,
Taylor Blau proposing 'git format-patch --cover-letter
--range-diff' may mistakenly place upstream commit in the
range-diff output. Teach `format-patch` pass `--right-only`
to range-diff, maybe can avoid this kind of mistake.

Because `git rebase --apply` will internally call `git
format-patch -k --stdout --full-index --cherry-pick --right-only
--src-prefix=a/ --dst-prefix=b/ --no-renames --no-cover-letter
--pretty=mboxrd --topo-order --no-base`, In order to avoid the
ambiguity of `--right-only`, users can call `format-patch
--range-diff --only-right` instead of `--right-only` to only emit
output related to the second range.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
    [GSOC][RFC] format-patch: pass --right-only to range-diff
    
    Transfer parameters are relatively simple. But because I can' t
    reproduce the situation, There's no big difference between adding
    ---only-right and not adding ---only-right. So I haven't written the
    test files for the time being.
    
    I may need to ask reviewers opinion first.
    
    this want to fix #876 Thanks.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-898%2Fadlternative%2Fformat-patch-range-diff-right-only-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-898/adlternative/format-patch-range-diff-right-only-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/898

 Documentation/git-format-patch.txt |  5 ++++-
 builtin/log.c                      | 15 ++++++++++-----
 2 files changed, 14 insertions(+), 6 deletions(-)


base-commit: be7935ed8bff19f481b033d0d242c5d5f239ed50

Comments

Taylor Blau March 8, 2021, 8:18 p.m. UTC | #1
On Sun, Mar 07, 2021 at 06:57:19AM +0000, ZheNing Hu via GitGitGadget wrote:
> From: ZheNing Hu <adlternative@gmail.com>
>
> In https://lore.kernel.org/git/YBx5rmVsg1LJhSKN@nand.local/,
> Taylor Blau proposing 'git format-patch --cover-letter
> --range-diff' may mistakenly place upstream commit in the
> range-diff output. Teach `format-patch` pass `--right-only`
> to range-diff, maybe can avoid this kind of mistake.
>
> Because `git rebase --apply` will internally call `git
> format-patch -k --stdout --full-index --cherry-pick --right-only
> --src-prefix=a/ --dst-prefix=b/ --no-renames --no-cover-letter
> --pretty=mboxrd --topo-order --no-base`, In order to avoid the
> ambiguity of `--right-only`, users can call `format-patch
> --range-diff --only-right` instead of `--right-only` to only emit
> output related to the second range.

A couple of times this new option is called "--right-only" and other
times it is "--only-right". This comment makes me think that you meant
to call it "--only-right", but...

> Signed-off-by: ZheNing Hu <adlternative@gmail.com>
> ---
>     [GSOC][RFC] format-patch: pass --right-only to range-diff
>
>     Transfer parameters are relatively simple. But because I can' t
>     reproduce the situation, There's no big difference between adding
>     ---only-right and not adding ---only-right. So I haven't written the
>     test files for the time being.

Note that I think (and Johannes--cc'd--could confirm) that what you want
is '--left-only' to discard rebased changes from the upstream branch:

In either case, this should help you reproduce it (and would be a good
starting point for adding a test case). Here, we're using '--left-only'
to discard changes from upstream (here, the 'main' branch) after
rebasing:

    #!/bin/sh

    set -e

    rm -fr repo
    git init repo
    cd repo

    git branch -M main

    echo "base" >base
    git add base
    git commit -m "base"

    git checkout -b my-feature
    echo "feature" >feature
    git add feature
    git commit -m "feature"

    base="$(git rev-parse main)"
    old="$(git rev-parse my-feature)"

    git checkout main
    echo "other" >>base
    git add base
    git commit -m "new"

    git checkout my-feature
    git rebase $base --onto main

    tip="$(git rev-parse my-feature)"

    git range-diff $base $old $tip
    git range-diff --left-only $base $old $tip

>     I may need to ask reviewers opinion first.
>
>     this want to fix #876 Thanks.
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-898%2Fadlternative%2Fformat-patch-range-diff-right-only-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-898/adlternative/format-patch-range-diff-right-only-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/898
>
>  Documentation/git-format-patch.txt |  5 ++++-
>  builtin/log.c                      | 15 ++++++++++-----
>  2 files changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
> index 3e49bf221087..5dae34c3090b 100644
> --- a/Documentation/git-format-patch.txt
> +++ b/Documentation/git-format-patch.txt
> @@ -27,7 +27,7 @@ SYNOPSIS
>  		   [--[no-]encode-email-headers]
>  		   [--no-notes | --notes[=<ref>]]
>  		   [--interdiff=<previous>]
> -		   [--range-diff=<previous> [--creation-factor=<percent>]]
> +		   [--range-diff=<previous> [--creation-factor=<percent>] [--right-only]]
>  		   [--filename-max-length=<n>]
>  		   [--progress]
>  		   [<common diff options>]
> @@ -301,6 +301,9 @@ material (this may change in the future).
>  	creation/deletion cost fudge factor. See linkgit:git-range-diff[1])
>  	for details.
>
> +--only-right:
> +	Used with `--range-diff`, only emit output related to the second range.
> +

s/--only-right/--right-only?

>  --notes[=<ref>]::
>  --no-notes::
>  	Append the notes (see linkgit:git-notes[1]) for the commit
> diff --git a/builtin/log.c b/builtin/log.c
> index f67b67d80ed1..5d2f39fd19a7 100644
> --- a/builtin/log.c
> +++ b/builtin/log.c
> @@ -1153,7 +1153,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
>  			      struct commit *origin,
>  			      int nr, struct commit **list,
>  			      const char *branch_name,
> -			      int quiet)
> +			      int quiet, int right_only)
>  {
>  	const char *committer;
>  	struct shortlog log;
> @@ -1228,7 +1228,8 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
>  			.creation_factor = rev->creation_factor,
>  			.dual_color = 1,
>  			.diffopt = &opts,
> -			.other_arg = &other_arg
> +			.other_arg = &other_arg,
> +			.right_only = right_only
>  		};
>
>  		diff_setup(&opts);
> @@ -1732,7 +1733,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>  	struct strbuf rdiff2 = STRBUF_INIT;
>  	struct strbuf rdiff_title = STRBUF_INIT;
>  	int creation_factor = -1;
> -
> +	int right_only = 0;

Nit: the newline between creation_factor's declaration and
builtin_format_patch_options's is good to keep.

>  	const struct option builtin_format_patch_options[] = {
>  		OPT_CALLBACK_F('n', "numbered", &numbered, NULL,
>  			    N_("use [PATCH n/m] even with a single patch"),
> @@ -1814,6 +1815,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>  			     parse_opt_object_name),
>  		OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
>  			   N_("show changes against <refspec> in cover letter or single patch")),
> +		OPT_BOOL(0, "only-right", &right_only,
> +			 N_("only emit output related to the second range")),
>  		OPT_INTEGER(0, "creation-factor", &creation_factor,
>  			    N_("percentage by which creation is weighted")),
>  		OPT_END()
> @@ -2087,7 +2090,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>  		creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
>  	else if (!rdiff_prev)
>  		die(_("--creation-factor requires --range-diff"));
> -
> +	if (right_only && !rdiff_prev)
> +		die(_("--right_only requires --range-diff"));

These may be good to combine into one big conditional that dies whenever
an option requiring --range-diff is set, but --range-diff is not:

  if (!rdiff_prev) {
    if (creation_factor)
      die(_("--creation-factor requires --range-diff"));
    if (right_only)
      die(_("--right-only requires --range-diff"));
  }

Thanks,
Taylor
ZheNing Hu March 9, 2021, 7:28 a.m. UTC | #2
Taylor Blau <ttaylorr@github.com> 于2021年3月9日周二 上午4:18写道:
>
> On Sun, Mar 07, 2021 at 06:57:19AM +0000, ZheNing Hu via GitGitGadget wrote:
> > From: ZheNing Hu <adlternative@gmail.com>
> >
> > In https://lore.kernel.org/git/YBx5rmVsg1LJhSKN@nand.local/,
> > Taylor Blau proposing 'git format-patch --cover-letter
> > --range-diff' may mistakenly place upstream commit in the
> > range-diff output. Teach `format-patch` pass `--right-only`
> > to range-diff, maybe can avoid this kind of mistake.
> >
> > Because `git rebase --apply` will internally call `git
> > format-patch -k --stdout --full-index --cherry-pick --right-only
> > --src-prefix=a/ --dst-prefix=b/ --no-renames --no-cover-letter
> > --pretty=mboxrd --topo-order --no-base`, In order to avoid the
> > ambiguity of `--right-only`, users can call `format-patch
> > --range-diff --only-right` instead of `--right-only` to only emit
> > output related to the second range.
>
> A couple of times this new option is called "--right-only" and other
> times it is "--only-right". This comment makes me think that you meant
> to call it "--only-right", but...
>
> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>
> > ---
> >     [GSOC][RFC] format-patch: pass --right-only to range-diff
> >
> >     Transfer parameters are relatively simple. But because I can' t
> >     reproduce the situation, There's no big difference between adding
> >     ---only-right and not adding ---only-right. So I haven't written the
> >     test files for the time being.
>
> Note that I think (and Johannes--cc'd--could confirm) that what you want
> is '--left-only' to discard rebased changes from the upstream branch:
>
> In either case, this should help you reproduce it (and would be a good
> starting point for adding a test case). Here, we're using '--left-only'
> to discard changes from upstream (here, the 'main' branch) after
> rebasing:
>
>     #!/bin/sh
>
>     set -e
>
>     rm -fr repo
>     git init repo
>     cd repo
>
>     git branch -M main
>
>     echo "base" >base
>     git add base
>     git commit -m "base"
>
>     git checkout -b my-feature
>     echo "feature" >feature
>     git add feature
>     git commit -m "feature"
>
>     base="$(git rev-parse main)"
>     old="$(git rev-parse my-feature)"
>
>     git checkout main
>     echo "other" >>base
>     git add base
>     git commit -m "new"
>
>     git checkout my-feature
>     git rebase $base --onto main
>
>     tip="$(git rev-parse my-feature)"
>
>     git range-diff $base $old $tip
>     git range-diff --left-only $base $old $tip
>
Thanks, I have seen an obvious phenomenon from
your shell script, and I wrote test code based on it.

> >     I may need to ask reviewers opinion first.
> >
> >     this want to fix #876 Thanks.
> >
> > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-898%2Fadlternative%2Fformat-patch-range-diff-right-only-v1
> > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-898/adlternative/format-patch-range-diff-right-only-v1
> > Pull-Request: https://github.com/gitgitgadget/git/pull/898
> >
> >  Documentation/git-format-patch.txt |  5 ++++-
> >  builtin/log.c                      | 15 ++++++++++-----
> >  2 files changed, 14 insertions(+), 6 deletions(-)
> >
> > diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
> > index 3e49bf221087..5dae34c3090b 100644
> > --- a/Documentation/git-format-patch.txt
> > +++ b/Documentation/git-format-patch.txt
> > @@ -27,7 +27,7 @@ SYNOPSIS
> >                  [--[no-]encode-email-headers]
> >                  [--no-notes | --notes[=<ref>]]
> >                  [--interdiff=<previous>]
> > -                [--range-diff=<previous> [--creation-factor=<percent>]]
> > +                [--range-diff=<previous> [--creation-factor=<percent>] [--right-only]]
> >                  [--filename-max-length=<n>]
> >                  [--progress]
> >                  [<common diff options>]
> > @@ -301,6 +301,9 @@ material (this may change in the future).
> >       creation/deletion cost fudge factor. See linkgit:git-range-diff[1])
> >       for details.
> >
> > +--only-right:
> > +     Used with `--range-diff`, only emit output related to the second range.
> > +
>
> s/--only-right/--right-only?
Now it's all "--left-only".
>
> >  --notes[=<ref>]::
> >  --no-notes::
> >       Append the notes (see linkgit:git-notes[1]) for the commit
> > diff --git a/builtin/log.c b/builtin/log.c
> > index f67b67d80ed1..5d2f39fd19a7 100644
> > --- a/builtin/log.c
> > +++ b/builtin/log.c
> > @@ -1153,7 +1153,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
> >                             struct commit *origin,
> >                             int nr, struct commit **list,
> >                             const char *branch_name,
> > -                           int quiet)
> > +                           int quiet, int right_only)
> >  {
> >       const char *committer;
> >       struct shortlog log;
> > @@ -1228,7 +1228,8 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
> >                       .creation_factor = rev->creation_factor,
> >                       .dual_color = 1,
> >                       .diffopt = &opts,
> > -                     .other_arg = &other_arg
> > +                     .other_arg = &other_arg,
> > +                     .right_only = right_only
> >               };
> >
> >               diff_setup(&opts);
> > @@ -1732,7 +1733,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
> >       struct strbuf rdiff2 = STRBUF_INIT;
> >       struct strbuf rdiff_title = STRBUF_INIT;
> >       int creation_factor = -1;
> > -
> > +     int right_only = 0;
>
> Nit: the newline between creation_factor's declaration and
> builtin_format_patch_options's is good to keep.
>
> >       const struct option builtin_format_patch_options[] = {
> >               OPT_CALLBACK_F('n', "numbered", &numbered, NULL,
> >                           N_("use [PATCH n/m] even with a single patch"),
> > @@ -1814,6 +1815,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
> >                            parse_opt_object_name),
> >               OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
> >                          N_("show changes against <refspec> in cover letter or single patch")),
> > +             OPT_BOOL(0, "only-right", &right_only,
> > +                      N_("only emit output related to the second range")),
> >               OPT_INTEGER(0, "creation-factor", &creation_factor,
> >                           N_("percentage by which creation is weighted")),
> >               OPT_END()
> > @@ -2087,7 +2090,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
> >               creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
> >       else if (!rdiff_prev)
> >               die(_("--creation-factor requires --range-diff"));
> > -
> > +     if (right_only && !rdiff_prev)
> > +             die(_("--right_only requires --range-diff"));
>
> These may be good to combine into one big conditional that dies whenever
> an option requiring --range-diff is set, but --range-diff is not:
>
Good tip.
>   if (!rdiff_prev) {
>     if (creation_factor)
>       die(_("--creation-factor requires --range-diff"));
>     if (right_only)
>       die(_("--right-only requires --range-diff"));
>   }
>
> Thanks,
> Taylor

Thanks,
ZheNing Hu
Junio C Hamano March 12, 2021, 9:55 p.m. UTC | #3
Taylor Blau <ttaylorr@github.com> writes:

> Note that I think (and Johannes--cc'd--could confirm) that what you want
> is '--left-only' to discard rebased changes from the upstream branch:

Does that mean range-diff got --left-only and --right-only
backwards?  IOW, doesn't

$ git log --left-only A...B

show what is shown in "git log B..A", and if so, shouldn't

$ git range-diff --left-only A...B

which is a synonym for

$ git range-diff B..A A..B

also give commits in the B..A range?

Puzzled....
Junio C Hamano March 12, 2021, 10:09 p.m. UTC | #4
Junio C Hamano <gitster@pobox.com> writes:

> Taylor Blau <ttaylorr@github.com> writes:
>
>> Note that I think (and Johannes--cc'd--could confirm) that what you want
>> is '--left-only' to discard rebased changes from the upstream branch:
>
> Does that mean range-diff got --left-only and --right-only
> backwards?  IOW, doesn't
>
> $ git log --left-only A...B
>
> show what is shown in "git log B..A", and if so, shouldn't
>
> $ git range-diff --left-only A...B
>
> which is a synonym for
>
> $ git range-diff B..A A..B
>
> also give commits in the B..A range?
>
> Puzzled....

Ah, it was only that "range-diff --help" uses confusing
double-negative phrasing.  I.e.

    --left-only::
            Suppress commits that are missing from the first specified range
            (or the "left range" when using the `<rev1>...<rev2>` format).

If you "suppress those that are missing from X", you are effectively
including only those that appear in X, so two conclusions I draw are

 - "range-diff" does not get "--left/right-only" backwards;

 - its documentation should be improved.

Perhaps

    --left-only::
	Include only commits that are in the first range (or the
	"left range" when using the `<rev1>...<rev2>` format).

wouldn't have made me puzzled.

Thanks.
diff mbox series

Patch

diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
index 3e49bf221087..5dae34c3090b 100644
--- a/Documentation/git-format-patch.txt
+++ b/Documentation/git-format-patch.txt
@@ -27,7 +27,7 @@  SYNOPSIS
 		   [--[no-]encode-email-headers]
 		   [--no-notes | --notes[=<ref>]]
 		   [--interdiff=<previous>]
-		   [--range-diff=<previous> [--creation-factor=<percent>]]
+		   [--range-diff=<previous> [--creation-factor=<percent>] [--right-only]]
 		   [--filename-max-length=<n>]
 		   [--progress]
 		   [<common diff options>]
@@ -301,6 +301,9 @@  material (this may change in the future).
 	creation/deletion cost fudge factor. See linkgit:git-range-diff[1])
 	for details.
 
+--only-right:
+	Used with `--range-diff`, only emit output related to the second range.
+
 --notes[=<ref>]::
 --no-notes::
 	Append the notes (see linkgit:git-notes[1]) for the commit
diff --git a/builtin/log.c b/builtin/log.c
index f67b67d80ed1..5d2f39fd19a7 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1153,7 +1153,7 @@  static void make_cover_letter(struct rev_info *rev, int use_separate_file,
 			      struct commit *origin,
 			      int nr, struct commit **list,
 			      const char *branch_name,
-			      int quiet)
+			      int quiet, int right_only)
 {
 	const char *committer;
 	struct shortlog log;
@@ -1228,7 +1228,8 @@  static void make_cover_letter(struct rev_info *rev, int use_separate_file,
 			.creation_factor = rev->creation_factor,
 			.dual_color = 1,
 			.diffopt = &opts,
-			.other_arg = &other_arg
+			.other_arg = &other_arg,
+			.right_only = right_only
 		};
 
 		diff_setup(&opts);
@@ -1732,7 +1733,7 @@  int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	struct strbuf rdiff2 = STRBUF_INIT;
 	struct strbuf rdiff_title = STRBUF_INIT;
 	int creation_factor = -1;
-
+	int right_only = 0;
 	const struct option builtin_format_patch_options[] = {
 		OPT_CALLBACK_F('n', "numbered", &numbered, NULL,
 			    N_("use [PATCH n/m] even with a single patch"),
@@ -1814,6 +1815,8 @@  int cmd_format_patch(int argc, const char **argv, const char *prefix)
 			     parse_opt_object_name),
 		OPT_STRING(0, "range-diff", &rdiff_prev, N_("refspec"),
 			   N_("show changes against <refspec> in cover letter or single patch")),
+		OPT_BOOL(0, "only-right", &right_only,
+			 N_("only emit output related to the second range")),
 		OPT_INTEGER(0, "creation-factor", &creation_factor,
 			    N_("percentage by which creation is weighted")),
 		OPT_END()
@@ -2087,7 +2090,8 @@  int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
 	else if (!rdiff_prev)
 		die(_("--creation-factor requires --range-diff"));
-
+	if (right_only && !rdiff_prev)
+		die(_("--right_only requires --range-diff"));
 	if (rdiff_prev) {
 		if (!cover_letter && total != 1)
 			die(_("--range-diff requires --cover-letter or single patch"));
@@ -2134,7 +2138,8 @@  int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		if (thread)
 			gen_message_id(&rev, "cover");
 		make_cover_letter(&rev, !!output_directory,
-				  origin, nr, list, branch_name, quiet);
+				  origin, nr, list, branch_name, quiet,
+					right_only);
 		print_bases(&bases, rev.diffopt.file);
 		print_signature(rev.diffopt.file);
 		total++;