git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] format-patch: make format.outputDirectory relative to worktree
@ 2019-12-24 12:37 Denton Liu
  2019-12-24 19:25 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Denton Liu @ 2019-12-24 12:37 UTC (permalink / raw)
  To: Git Mailing List

Currently, when format-patch outputs patches to the path specified by
the `format.outputDirectory` configuration variable, it is relative to
the current working directory. However, it would make more sense for the
path to be relative to the worktree if it exists so that patches will be
placed in one location even if a user were to change directories.

Rewrite the output directory logic for format-patch so that it will be
relative to the worktree of the directory. An escape hatch is provided
for if the previous behaviour is desired by prepending "./" to the
variable.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 Documentation/config/format.txt |  3 +++
 builtin/log.c                   | 20 ++++++++++++-----
 t/t4014-format-patch.sh         | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/Documentation/config/format.txt b/Documentation/config/format.txt
index 513fcd88d5..fc9d110d88 100644
--- a/Documentation/config/format.txt
+++ b/Documentation/config/format.txt
@@ -88,6 +88,9 @@ format.coverLetter::
 format.outputDirectory::
 	Set a custom directory to store the resulting files instead of the
 	current working directory. All directory components will be created.
+	The path specified will be relative to the worktree of the repository
+	unless the path begins with "./" or there is no worktree in which case
+	the path will be relative to current working directory.
 
 format.useAutoBase::
 	A boolean value which lets you enable the `--base=auto` option of
diff --git a/builtin/log.c b/builtin/log.c
index 8c664067ca..fe072c7309 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -885,7 +885,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 	if (!strcmp(var, "format.outputdirectory"))
-		return git_config_string(&config_output_directory, var, value);
+		return git_config_pathname(&config_output_directory, var, value);
 	if (!strcmp(var, "format.useautobase")) {
 		base_auto = git_config_bool(var, value);
 		return 0;
@@ -1841,13 +1841,21 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	if (rev.show_notes)
 		init_display_notes(&rev.notes_opt);
 
-	if (!output_directory && !use_stdout)
-		output_directory = config_output_directory;
+	if (!use_stdout) {
+		const char *outdir_prefix = NULL;
+		const char *outdir = config_output_directory;
 
-	if (!use_stdout)
-		output_directory = set_outdir(prefix, output_directory);
-	else
+		if (output_directory)
+			outdir = output_directory;
+
+		if (output_directory ||
+		    (config_output_directory && starts_with(config_output_directory, "./")))
+			outdir_prefix = prefix;
+
+		output_directory = set_outdir(outdir_prefix, outdir);
+	} else {
 		setup_pager();
+	}
 
 	if (output_directory) {
 		int saved;
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index a5b6302a1c..cc13cc4699 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -1813,6 +1813,45 @@ test_expect_success 'format-patch format.outputDirectory option' '
 	test_line_count = $count list
 '
 
+test_expect_success 'format-patch format.outputDirectory option relative to git dir' '
+	test_config format.outputDirectory patches &&
+	rm -fr patches &&
+	mkdir -p sub &&
+	(
+		cd sub &&
+		git format-patch master..side
+	) &&
+	count=$(git rev-list --count master..side) &&
+	ls patches >list &&
+	test_line_count = $count list
+'
+
+test_expect_success 'format-patch format.outputDirectory option with relative path' '
+	test_config format.outputDirectory ./patches &&
+	mkdir -p sub &&
+	(
+		cd sub &&
+		rm -fr patches &&
+		git format-patch master..side &&
+		count=$(git rev-list --count master..side) &&
+		ls patches >list &&
+		test_line_count = $count list
+	)
+'
+
+test_expect_success 'format-patch format.outputDirectory option absolute path' '
+	test_config format.outputDirectory "$PWD/patches" &&
+	rm -fr patches &&
+	mkdir -p sub &&
+	(
+		cd sub &&
+		git format-patch master..side
+	) &&
+	count=$(git rev-list --count master..side) &&
+	ls patches >list &&
+	test_line_count = $count list
+'
+
 test_expect_success 'format-patch -o overrides format.outputDirectory' '
 	test_config format.outputDirectory patches &&
 	rm -fr patches patchset &&
-- 
2.24.1.810.g65a2f617f4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] format-patch: make format.outputDirectory relative to worktree
  2019-12-24 12:37 [PATCH] format-patch: make format.outputDirectory relative to worktree Denton Liu
@ 2019-12-24 19:25 ` Junio C Hamano
  2019-12-24 22:05   ` Denton Liu
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2019-12-24 19:25 UTC (permalink / raw)
  To: Denton Liu; +Cc: Git Mailing List

Denton Liu <liu.denton@gmail.com> writes:

> Currently, when format-patch outputs patches to the path specified by
> the `format.outputDirectory` configuration variable, it is relative to
> the current working directory. However, it would make more sense for the
> path to be relative to the worktree if it exists so that patches will be
> placed in one location even if a user were to change directories.

Would it make more sense?  If a user is deep in the hierarchy, after
running

	$ git format-patch master..

the next thing the user would want to do is to go to the directory
and give them final review and edit the patches before sending out.
That would involve going up some levels depending on where s/he is,
in other words, the location the patches are found would be different
and this change makes it a lot more cumbersome to go there with a
relative path.  The only people who benefit from the change is those
who have their working trees at very shallow hierarchy so that it is
not cumbersome for them to give the full path to the top of them.

So I think this cuts both ways, and "it would make more sense" is
overselling.  Besides, those who can easily give the full path to
the top of the working tree (because they are short) can spell that
short path to outputDirectory configuration variable just once and
be done with it, no?

> Rewrite the output directory logic for format-patch so that it will be
> relative to the worktree of the directory. An escape hatch is provided
> for if the previous behaviour is desired by prepending "./" to the
> variable.

Anything that forces existing users to change their existing
settings is not an escape hatch.  It merely is a regression with a
possible workaround.

So I dunno.  I probably would have accepted a patch that *adds*
outputDirectory configuration that behaves the way you are proposing
in this patch if we did not have the variable yet in the system
(i.e. three or four years ago), but I am not sure if it is a good
idea to change it after these years.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] format-patch: make format.outputDirectory relative to worktree
  2019-12-24 19:25 ` Junio C Hamano
@ 2019-12-24 22:05   ` Denton Liu
  2019-12-25  2:26     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Denton Liu @ 2019-12-24 22:05 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git Mailing List

Hi Junio,

On Tue, Dec 24, 2019 at 11:25:18AM -0800, Junio C Hamano wrote:
> Denton Liu <liu.denton@gmail.com> writes:
> > Rewrite the output directory logic for format-patch so that it will be
> > relative to the worktree of the directory. An escape hatch is provided
> > for if the previous behaviour is desired by prepending "./" to the
> > variable.
> 
> Anything that forces existing users to change their existing
> settings is not an escape hatch.  It merely is a regression with a
> possible workaround.
> 
> So I dunno.  I probably would have accepted a patch that *adds*
> outputDirectory configuration that behaves the way you are proposing
> in this patch if we did not have the variable yet in the system
> (i.e. three or four years ago), but I am not sure if it is a good
> idea to change it after these years.

Perhaps instead of switching the default behaviour so that it's always
relative to the top of the worktree, we can add some sort of prefix to
indicate that we want this behaviour. Maybe `worktree:path/from/top` or
`^path/from/top` or something like this?

Thanks,

Denton

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] format-patch: make format.outputDirectory relative to worktree
  2019-12-24 22:05   ` Denton Liu
@ 2019-12-25  2:26     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2019-12-25  2:26 UTC (permalink / raw)
  To: Denton Liu; +Cc: Git Mailing List

Denton Liu <liu.denton@gmail.com> writes:

> ... Maybe `worktree:path/from/top` or
> `^path/from/top` or something like this?

I recall seeing a similar idea with a bit better syntax discussed
when we talked about path-type values in the configuration system,
where we use "~WORD~/" prefix to expand to Git specific special
places in the filesystem, just like "~USER/" prefix expands to the
home directory of the user.  E.g. "~gitdir~/config" may expand to
point at the per-repository configuration file in the current
repository, and "~worktree~/Makefile" may be the top-level Makefile
checked out in the current worktree.  This depends on the fact that
nobody sane has username ending with tilde (and if there is such a
user, tough luck for that user).  I'm tempted to credit Dscho for
the idea but it could have been by J6t.

Does anybody have a pointer to the discussion thread?

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-25  2:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-24 12:37 [PATCH] format-patch: make format.outputDirectory relative to worktree Denton Liu
2019-12-24 19:25 ` Junio C Hamano
2019-12-24 22:05   ` Denton Liu
2019-12-25  2:26     ` Junio C Hamano

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).