diff mbox series

[v3,2/2] format-patch: teach format.notes config option

Message ID df864c4adf4dcab5f959007f87b1c4d0eafecb52.1557513353.git.liu.denton@gmail.com (mailing list archive)
State New, archived
Headers show
Series format-patch: teach format.notes config option | expand

Commit Message

Denton Liu May 10, 2019, 6:37 p.m. UTC
In git-format-patch, notes can be appended with the `--notes` option.
However, this must be specified by the user on an
invocation-by-invocation basis. If a user is not careful, it's possible
that they may forget to include it and generate a patch series without
notes.

Teach git-format-patch the `format.notes` config option. Its value is a
notes ref that will be automatically appended. The special value of
"standard" can be used to specify the standard notes. This option is
overridable with the `--no-notes` option in case a user wishes not to
append notes.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 Documentation/config/format.txt    | 13 ++++++
 Documentation/git-format-patch.txt |  3 ++
 builtin/log.c                      | 18 +++++++-
 t/t4014-format-patch.sh            | 70 ++++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 1 deletion(-)

Comments

Junio C Hamano May 13, 2019, 2:44 a.m. UTC | #1
Denton Liu <liu.denton@gmail.com> writes:

> @@ -864,6 +866,20 @@ static int git_format_config(const char *var, const char *value, void *cb)
>  			from = NULL;
>  		return 0;
>  	}
> +	if (!strcmp(var, "format.notes")) {
> +		struct strbuf buf = STRBUF_INIT;
> +
> +		rev->show_notes = 1;
> +		if (!strcmp(value, "standard")) {
> +			rev->notes_opt.use_default_notes = 1;
> +		} else {
> +			strbuf_addstr(&buf, value);
> +			expand_notes_ref(&buf);
> +			string_list_append(&rev->notes_opt.extra_notes_refs,
> +					strbuf_detach(&buf, NULL));
> +		}
> +		return 0;
> +	}

Unlike the command line option parser, this does not seem to touch
rev->show_notes_given at all.  Intended?  I am wondering how well
this implementation meshes with what 66b2ed09 ("Fix "log" family not
to be too agressive about showing notes", 2010-01-20) wanted to do,
which 894a9d33 ("Support showing notes from more than one notes
tree", 2010-03-12) later extended.

> +test_expect_success 'format-patch notes output control' '
> +	git notes add -m "notes config message" HEAD &&
> +	test_when_finished git notes remove HEAD &&
> +
> +	git format-patch -1 --stdout >out &&
> +	! grep "notes config message" out &&
> +	git format-patch -1 --stdout --notes >out &&
> +	grep "notes config message" out &&
> +	git format-patch -1 --stdout --no-notes >out &&
> +	! grep "notes config message" out &&
> +	git format-patch -1 --stdout --notes --no-notes >out &&
> +	! grep "notes config message" out &&
> +	git format-patch -1 --stdout --no-notes --notes >out &&
> +	grep "notes config message" out &&
> +
> +	test_config format.notes standard &&

I think we tend to spell these things "default".

Alternatively, the format.notes configuration can be "bool or text",
and make the variable set to 'true' mean "show notes, using the
default ref".

> +	git format-patch -1 --stdout >out &&
> +	grep "notes config message" out &&
> +	git format-patch -1 --stdout --notes >out &&
> +	grep "notes config message" out &&
> +	git format-patch -1 --stdout --no-notes >out &&
> +	! grep "notes config message" out &&
> +	git format-patch -1 --stdout --notes --no-notes >out &&
> +	! grep "notes config message" out &&
> +	git format-patch -1 --stdout --no-notes --notes >out &&
> +	grep "notes config message" out
> +'

OK.

> +test_expect_success 'format-patch with multiple notes refs' '
> +	git notes --ref note1 add -m "this is note 1" HEAD &&
> +	test_when_finished git notes --ref note1 remove HEAD &&
> +	git notes --ref note2 add -m "this is note 2" HEAD &&
> +	test_when_finished git notes --ref note2 remove HEAD &&
> + ...
> +	git format-patch -1 --stdout --notes=note1 --notes=note2 >out &&
> +	grep "this is note 1" out &&
> +	grep "this is note 2" out &&

Do we promise the order in which these two lines appear in the output?

> +	test_config format.notes note1 &&
> +	git format-patch -1 --stdout >out &&
> +	grep "this is note 1" out &&
> +	! grep "this is note 2" out &&
> +	git format-patch -1 --stdout --no-notes >out &&
> +	! grep "this is note 1" out &&
> +	! grep "this is note 2" out &&
> +	git format-patch -1 --stdout --notes=note2 >out &&
> +	grep "this is note 1" out &&
> +	grep "this is note 2" out &&

So format.notes say note1 but the command line explicitly asks it
wants note from note2, but the command still gives from note1
anyway.

> +	git format-patch -1 --stdout --no-notes --notes=note2 >out &&
> +	! grep "this is note 1" out &&
> +	grep "this is note 2" out &&

And there is a way to work it around, i.e. clear everything
configured with --no-notes and then name the one you want from the
command line.

I am not sure if the above is consistent with how our options and
configurations interact in general.  Shouldn't the --notes=note2
alone in the earlier example cancel format.notes=note1 configured?

> +	git config --add format.notes note2 &&
> +	git format-patch -1 --stdout >out &&
> +	grep "this is note 1" out &&
> +	grep "this is note 2" out &&
> +	git format-patch -1 --stdout --no-notes >out &&
> +	! grep "this is note 1" out &&
> +	! grep "this is note 2" out
> +'
> +
>  echo "fatal: --name-only does not make sense" > expect.name-only
>  echo "fatal: --name-status does not make sense" > expect.name-status
>  echo "fatal: --check does not make sense" > expect.check
Denton Liu May 14, 2019, 5:01 p.m. UTC | #2
Hi Junio,

On Mon, May 13, 2019 at 11:44:21AM +0900, Junio C Hamano wrote:
> Denton Liu <liu.denton@gmail.com> writes:
> 
> > @@ -864,6 +866,20 @@ static int git_format_config(const char *var, const char *value, void *cb)
> >  			from = NULL;
> >  		return 0;
> >  	}
> > +	if (!strcmp(var, "format.notes")) {
> > +		struct strbuf buf = STRBUF_INIT;
> > +
> > +		rev->show_notes = 1;
> > +		if (!strcmp(value, "standard")) {
> > +			rev->notes_opt.use_default_notes = 1;
> > +		} else {
> > +			strbuf_addstr(&buf, value);
> > +			expand_notes_ref(&buf);
> > +			string_list_append(&rev->notes_opt.extra_notes_refs,
> > +					strbuf_detach(&buf, NULL));
> > +		}
> > +		return 0;
> > +	}
> 
> Unlike the command line option parser, this does not seem to touch
> rev->show_notes_given at all.  Intended?  I am wondering how well
> this implementation meshes with what 66b2ed09 ("Fix "log" family not
> to be too agressive about showing notes", 2010-01-20) wanted to do,
> which 894a9d33 ("Support showing notes from more than one notes
> tree", 2010-03-12) later extended.

This was intended but I'm not 100% sure that it's correct.

From what I could gleam from reading the code, `show_notes_given` is
only used by the `cmd_log_init` function, which is not called by
format-patch. As a result, I opted to not set that flag since it's not
really "given" in the sense that a user didn't explicitly pass in a
command-line option indicating they wanted notes.

> 
> > +test_expect_success 'format-patch notes output control' '
> > +	git notes add -m "notes config message" HEAD &&
> > +	test_when_finished git notes remove HEAD &&
> > +
> > +	git format-patch -1 --stdout >out &&
> > +	! grep "notes config message" out &&
> > +	git format-patch -1 --stdout --notes >out &&
> > +	grep "notes config message" out &&
> > +	git format-patch -1 --stdout --no-notes >out &&
> > +	! grep "notes config message" out &&
> > +	git format-patch -1 --stdout --notes --no-notes >out &&
> > +	! grep "notes config message" out &&
> > +	git format-patch -1 --stdout --no-notes --notes >out &&
> > +	grep "notes config message" out &&
> > +
> > +	test_config format.notes standard &&
> 
> I think we tend to spell these things "default".
> 
> Alternatively, the format.notes configuration can be "bool or text",
> and make the variable set to 'true' mean "show notes, using the
> default ref".

I think I'l go with this approach.

> 
> > +	git format-patch -1 --stdout >out &&
> > +	grep "notes config message" out &&
> > +	git format-patch -1 --stdout --notes >out &&
> > +	grep "notes config message" out &&
> > +	git format-patch -1 --stdout --no-notes >out &&
> > +	! grep "notes config message" out &&
> > +	git format-patch -1 --stdout --notes --no-notes >out &&
> > +	! grep "notes config message" out &&
> > +	git format-patch -1 --stdout --no-notes --notes >out &&
> > +	grep "notes config message" out
> > +'
> 
> OK.
> 
> > +test_expect_success 'format-patch with multiple notes refs' '
> > +	git notes --ref note1 add -m "this is note 1" HEAD &&
> > +	test_when_finished git notes --ref note1 remove HEAD &&
> > +	git notes --ref note2 add -m "this is note 2" HEAD &&
> > +	test_when_finished git notes --ref note2 remove HEAD &&
> > + ...
> > +	git format-patch -1 --stdout --notes=note1 --notes=note2 >out &&
> > +	grep "this is note 1" out &&
> > +	grep "this is note 2" out &&
> 
> Do we promise the order in which these two lines appear in the output?

According to the code, the order is stable. However, I just read through
the documentation and I realised that the ablility to provide multiple
notes refs is undocumented.

In a future reroll, I'll document the fact that --notes can be provided
multiple times.

> 
> > +	test_config format.notes note1 &&
> > +	git format-patch -1 --stdout >out &&
> > +	grep "this is note 1" out &&
> > +	! grep "this is note 2" out &&
> > +	git format-patch -1 --stdout --no-notes >out &&
> > +	! grep "this is note 1" out &&
> > +	! grep "this is note 2" out &&
> > +	git format-patch -1 --stdout --notes=note2 >out &&
> > +	grep "this is note 1" out &&
> > +	grep "this is note 2" out &&
> 
> So format.notes say note1 but the command line explicitly asks it
> wants note from note2, but the command still gives from note1
> anyway.
> 
> > +	git format-patch -1 --stdout --no-notes --notes=note2 >out &&
> > +	! grep "this is note 1" out &&
> > +	grep "this is note 2" out &&
> 
> And there is a way to work it around, i.e. clear everything
> configured with --no-notes and then name the one you want from the
> command line.
> 
> I am not sure if the above is consistent with how our options and
> configurations interact in general.  Shouldn't the --notes=note2
> alone in the earlier example cancel format.notes=note1 configured?

I borrowed this behaviour from how format.to behaves. In format-patch,
`--to` gives a recipient that is used _in addition_ to any format.to
variables. `--no-to` can override this. I made format.notes behave
similarly.

> 
> > +	git config --add format.notes note2 &&
> > +	git format-patch -1 --stdout >out &&
> > +	grep "this is note 1" out &&
> > +	grep "this is note 2" out &&
> > +	git format-patch -1 --stdout --no-notes >out &&
> > +	! grep "this is note 1" out &&
> > +	! grep "this is note 2" out
> > +'
> > +
> >  echo "fatal: --name-only does not make sense" > expect.name-only
> >  echo "fatal: --name-status does not make sense" > expect.name-status
> >  echo "fatal: --check does not make sense" > expect.check
diff mbox series

Patch

diff --git a/Documentation/config/format.txt b/Documentation/config/format.txt
index dc77941c48..e25f9cfc61 100644
--- a/Documentation/config/format.txt
+++ b/Documentation/config/format.txt
@@ -85,3 +85,16 @@  format.outputDirectory::
 format.useAutoBase::
 	A boolean value which lets you enable the `--base=auto` option of
 	format-patch by default.
+
+format.notes::
+	A ref which specifies where to get the notes (see
+	linkgit:git-notes[1]) that are appended for the commit after the
+	three-dash line.
++
+If the special value of "standard" is specified, then the standard notes
+ref is used (i.e. the notes ref used by `git notes` when no `--ref`
+argument is specified). If one wishes to use the ref
+`ref/notes/standard`, please use that literal instead.
++
+This configuration can be specified multiple times in order to allow
+multiple notes refs to be included.
diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
index 2c3971390e..9ce5b8aaee 100644
--- a/Documentation/git-format-patch.txt
+++ b/Documentation/git-format-patch.txt
@@ -275,6 +275,9 @@  these explanations after `format-patch` has run but before sending,
 keeping them as Git notes allows them to be maintained between versions
 of the patch series (but see the discussion of the `notes.rewrite`
 configuration options in linkgit:git-notes[1] to use this workflow).
++
+The default is `--no-notes`, unless the `format.notes` configuration is
+set.
 
 --[no-]signature=<signature>::
 	Add a signature to each message produced. Per RFC 3676 the signature
diff --git a/builtin/log.c b/builtin/log.c
index e43ee12fb1..9d26e0f248 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -779,6 +779,8 @@  enum {
 
 static int git_format_config(const char *var, const char *value, void *cb)
 {
+	struct rev_info *rev = cb;
+
 	if (!strcmp(var, "format.headers")) {
 		if (!value)
 			die(_("format.headers without value"));
@@ -864,6 +866,20 @@  static int git_format_config(const char *var, const char *value, void *cb)
 			from = NULL;
 		return 0;
 	}
+	if (!strcmp(var, "format.notes")) {
+		struct strbuf buf = STRBUF_INIT;
+
+		rev->show_notes = 1;
+		if (!strcmp(value, "standard")) {
+			rev->notes_opt.use_default_notes = 1;
+		} else {
+			strbuf_addstr(&buf, value);
+			expand_notes_ref(&buf);
+			string_list_append(&rev->notes_opt.extra_notes_refs,
+					strbuf_detach(&buf, NULL));
+		}
+		return 0;
+	}
 
 	return git_log_config(var, value, cb);
 }
@@ -1617,8 +1633,8 @@  int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	extra_to.strdup_strings = 1;
 	extra_cc.strdup_strings = 1;
 	init_log_defaults();
-	git_config(git_format_config, NULL);
 	repo_init_revisions(the_repository, &rev, prefix);
+	git_config(git_format_config, &rev);
 	rev.commit_format = CMIT_FMT_EMAIL;
 	rev.expand_tabs_in_log_default = 0;
 	rev.verbose_header = 1;
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index b6e2fdbc44..e0127282ba 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -738,6 +738,76 @@  test_expect_success 'format-patch --notes --signoff' '
 	sed "1,/^---$/d" out | grep "test message"
 '
 
+test_expect_success 'format-patch notes output control' '
+	git notes add -m "notes config message" HEAD &&
+	test_when_finished git notes remove HEAD &&
+
+	git format-patch -1 --stdout >out &&
+	! grep "notes config message" out &&
+	git format-patch -1 --stdout --notes >out &&
+	grep "notes config message" out &&
+	git format-patch -1 --stdout --no-notes >out &&
+	! grep "notes config message" out &&
+	git format-patch -1 --stdout --notes --no-notes >out &&
+	! grep "notes config message" out &&
+	git format-patch -1 --stdout --no-notes --notes >out &&
+	grep "notes config message" out &&
+
+	test_config format.notes standard &&
+	git format-patch -1 --stdout >out &&
+	grep "notes config message" out &&
+	git format-patch -1 --stdout --notes >out &&
+	grep "notes config message" out &&
+	git format-patch -1 --stdout --no-notes >out &&
+	! grep "notes config message" out &&
+	git format-patch -1 --stdout --notes --no-notes >out &&
+	! grep "notes config message" out &&
+	git format-patch -1 --stdout --no-notes --notes >out &&
+	grep "notes config message" out
+'
+
+test_expect_success 'format-patch with multiple notes refs' '
+	git notes --ref note1 add -m "this is note 1" HEAD &&
+	test_when_finished git notes --ref note1 remove HEAD &&
+	git notes --ref note2 add -m "this is note 2" HEAD &&
+	test_when_finished git notes --ref note2 remove HEAD &&
+
+	git format-patch -1 --stdout >out &&
+	! grep "this is note 1" out &&
+	! grep "this is note 2" out &&
+	git format-patch -1 --stdout --notes=note1 >out &&
+	grep "this is note 1" out &&
+	! grep "this is note 2" out &&
+	git format-patch -1 --stdout --notes=note2 >out &&
+	! grep "this is note 1" out &&
+	grep "this is note 2" out &&
+	git format-patch -1 --stdout --notes=note1 --notes=note2 >out &&
+	grep "this is note 1" out &&
+	grep "this is note 2" out &&
+
+	test_config format.notes note1 &&
+	git format-patch -1 --stdout >out &&
+	grep "this is note 1" out &&
+	! grep "this is note 2" out &&
+	git format-patch -1 --stdout --no-notes >out &&
+	! grep "this is note 1" out &&
+	! grep "this is note 2" out &&
+	git format-patch -1 --stdout --notes=note2 >out &&
+	grep "this is note 1" out &&
+	grep "this is note 2" out &&
+	git format-patch -1 --stdout --no-notes --notes=note2 >out &&
+	! grep "this is note 1" out &&
+	grep "this is note 2" out &&
+
+	git config --add format.notes note2 &&
+	git format-patch -1 --stdout >out &&
+	grep "this is note 1" out &&
+	grep "this is note 2" out &&
+	git format-patch -1 --stdout --no-notes >out &&
+	! grep "this is note 1" out &&
+	! grep "this is note 2" out
+'
+
 echo "fatal: --name-only does not make sense" > expect.name-only
 echo "fatal: --name-status does not make sense" > expect.name-status
 echo "fatal: --check does not make sense" > expect.check