# [PATCH v5 1/2] format-patch: create leading components of output directory

4 messages from 2019-10-21 to 2019-10-23. Participants: Bert Wesarg, Junio C Hamano.
Thread: https://gitlist.dev/t/52077

## Bert Wesarg, 2019-10-21 10:25

Subject: [PATCH v5 1/2] format-patch: create leading components of output directory
Message-ID: <ac3cc3df4452355dc41c58fe94961c37ca09ebec.1571653441.git.bert.wesarg@googlemail.com>
URL: https://gitlist.dev/e/ac3cc3df4452355dc41c58fe94961c37ca09ebec.1571653441.git.bert.wesarg%40googlemail.com

```
'git format-patch -o <outdir>' did an equivalent of 'mkdir <outdir>'
not 'mkdir -p <outdir>', which is being corrected.

Avoid the usage of 'adjust_shared_perm' on the leading directories which
may have security implications. Achieved by temporarily disabling of
'config.sharedRepository' like 'git init' does.

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>

---
Changes in v2:
 * squashed and base new tests on 'dl/format-patch-doc-test-cleanup'

Changes in v3:
 * avoid applying adjust_shared_perm

Changes in v4:
 * based on dl/format-patch-doc-test-cleanup and adopt it

Changes in v5:
 * make tests self-contained

Cc: Denton Liu <liu.denton@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>
Cc: SZEDER Gábor <szeder.dev@gmail.com>
---
 Documentation/config/format.txt    |  2 +-
 Documentation/git-format-patch.txt |  3 ++-
 builtin/log.c                      | 16 ++++++++++++++++
 t/t4014-format-patch.sh            | 26 ++++++++++++++++++++++++++
 4 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/Documentation/config/format.txt b/Documentation/config/format.txt
index cb629fa769..40cad9278f 100644
--- a/Documentation/config/format.txt
+++ b/Documentation/config/format.txt
@@ -81,7 +81,7 @@ format.coverLetter::
 
 format.outputDirectory::
 	Set a custom directory to store the resulting files instead of the
-	current working directory.
+	current working directory. All directory components will be created.
 
 format.useAutoBase::
 	A boolean value which lets you enable the `--base=auto` option of
diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
index 0ac56f4b70..2035d4d5d5 100644
--- a/Documentation/git-format-patch.txt
+++ b/Documentation/git-format-patch.txt
@@ -66,7 +66,8 @@ they are created in the current working directory. The default path
 can be set with the `format.outputDirectory` configuration option.
 The `-o` option takes precedence over `format.outputDirectory`.
 To store patches in the current working directory even when
-`format.outputDirectory` points elsewhere, use `-o .`.
+`format.outputDirectory` points elsewhere, use `-o .`. All directory
+components will be created.
 
 By default, the subject of a single patch is "[PATCH] " followed by
 the concatenation of lines from the commit message up to the first blank
diff --git a/builtin/log.c b/builtin/log.c
index 44b10b3415..8d08632858 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1765,10 +1765,26 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		setup_pager();
 
 	if (output_directory) {
+		int saved;
 		if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
 			rev.diffopt.use_color = GIT_COLOR_NEVER;
 		if (use_stdout)
 			die(_("standard output, or directory, which one?"));
+		/*
+		 * We consider <outdir> as 'outside of gitdir', therefore avoid
+		 * applying adjust_shared_perm in s-c-l-d.
+		 */
+		saved = get_shared_repository();
+		set_shared_repository(0);
+		switch (safe_create_leading_directories_const(output_directory)) {
+		case SCLD_OK:
+		case SCLD_EXISTS:
+			break;
+		default:
+			die(_("could not create leading directories "
+			      "of '%s'"), output_directory);
+		}
+		set_shared_repository(saved);
 		if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
 			die_errno(_("could not create directory '%s'"),
 				  output_directory);
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index 72b09896cf..3aab25da76 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -1606,6 +1606,32 @@ test_expect_success 'From line has expected format' '
 	test_cmp from filtered
 '
 
+test_expect_success 'format-patch -o with no leading directories' '
+	rm -fr patches &&
+	git format-patch -o patches master..side &&
+	count=$(git rev-list --count master..side) &&
+	ls patches >list &&
+	test_line_count = $count list
+'
+
+test_expect_success 'format-patch -o with leading existing directories' '
+	rm -rf existing-dir &&
+	mkdir existing-dir &&
+	git format-patch -o existing-dir/patches master..side &&
+	count=$(git rev-list --count master..side) &&
+	ls existing-dir/patches >list &&
+	test_line_count = $count list
+'
+
+test_expect_success 'format-patch -o with leading non-existing directories' '
+	rm -rf non-existing-dir &&
+	git format-patch -o non-existing-dir/patches master..side &&
+	count=$(git rev-list --count master..side) &&
+	test_path_is_dir non-existing-dir
+	ls non-existing-dir/patches >list &&
+	test_line_count = $count list
+'
+
 test_expect_success 'format-patch format.outputDirectory option' '
 	test_config format.outputDirectory patches &&
 	rm -fr patches &&
-- 
2.23.0.13.g28bc381d7c


```

## Bert Wesarg, 2019-10-21 10:25

Subject: [PATCH v5 2/2] format-patch: configure a command to generate the output directory name
Message-ID: <d461ae79c23ded9cc570ba1856cbae4ca227a711.1571653441.git.bert.wesarg@googlemail.com>
URL: https://gitlist.dev/e/d461ae79c23ded9cc570ba1856cbae4ca227a711.1571653441.git.bert.wesarg%40googlemail.com
In-Reply-To: <ac3cc3df4452355dc41c58fe94961c37ca09ebec.1571653441.git.bert.wesarg@googlemail.com>

```
The 'format.outputDirectory' configuration is only able to store constant
directory names. Though some may use

   $ git format-patch -o $(createdir) …

to name the directory dynamically. Provide a new configuration to be able
to store such a command too.

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>

---
Changes in v2:
 * rephrase motivation

Changes in v3:
 * remove RFC

Changes in v4:
 * based on dl/format-patch-doc-test-cleanup and adopt it

Changes in v5:
 * none

Cc: Alexander Kuleshov <kuleshovmail@gmail.com>
Cc: Eric Sunshine <sunshine@sunshineco.com>
Cc: Denton Liu <liu.denton@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>
---
 Documentation/config/format.txt    |  5 +++++
 Documentation/git-format-patch.txt |  6 +++++-
 builtin/log.c                      | 24 +++++++++++++++++++++++-
 t/t4014-format-patch.sh            | 26 ++++++++++++++++++++++++++
 4 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/Documentation/config/format.txt b/Documentation/config/format.txt
index 40cad9278f..420188a1c6 100644
--- a/Documentation/config/format.txt
+++ b/Documentation/config/format.txt
@@ -83,6 +83,11 @@ format.outputDirectory::
 	Set a custom directory to store the resulting files instead of the
 	current working directory. All directory components will be created.
 
+format.outputDirectoryCmd::
+	The command which is used to name a custom directory to store the
+	resulting files instead of the current working directory. All directory
+	components will be created.
+
 format.useAutoBase::
 	A boolean value which lets you enable the `--base=auto` option of
 	format-patch by default.
diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
index 2035d4d5d5..4936b9f91d 100644
--- a/Documentation/git-format-patch.txt
+++ b/Documentation/git-format-patch.txt
@@ -67,7 +67,11 @@ can be set with the `format.outputDirectory` configuration option.
 The `-o` option takes precedence over `format.outputDirectory`.
 To store patches in the current working directory even when
 `format.outputDirectory` points elsewhere, use `-o .`. All directory
-components will be created.
+components will be created. The 'format.outputDirectoryCmd' configuration can
+be used to name a command to produce the directory name programmatically. The
+command should produce the name to its standard output. The
+`format.outputDirectory` configuration takes precedence over
+`format.outputDirectoryCmd`.
 
 By default, the subject of a single patch is "[PATCH] " followed by
 the concatenation of lines from the commit message up to the first blank
diff --git a/builtin/log.c b/builtin/log.c
index 8d08632858..3eb507c02f 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -774,6 +774,7 @@ static const char *signature = git_version_string;
 static const char *signature_file;
 static int config_cover_letter;
 static const char *config_output_directory;
+static const char *config_output_directory_cmd;
 
 enum {
 	COVER_UNSET,
@@ -856,6 +857,8 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	}
 	if (!strcmp(var, "format.outputdirectory"))
 		return git_config_string(&config_output_directory, var, value);
+	if (!strcmp(var, "format.outputdirectorycmd"))
+		return git_config_string(&config_output_directory_cmd, var, value);
 	if (!strcmp(var, "format.useautobase")) {
 		base_auto = git_config_bool(var, value);
 		return 0;
@@ -1756,8 +1759,27 @@ 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)
+	if (!output_directory && !use_stdout) {
+		// outputDirectoryCmd can be preceeded by outputDirectory
+		if (!config_output_directory && config_output_directory_cmd) {
+			struct child_process cp = CHILD_PROCESS_INIT;
+			const char *argv[1];
+			struct strbuf buf = STRBUF_INIT;
+			int rc;
+
+			argv[0] = config_output_directory_cmd;
+			cp.argv = argv;
+			cp.use_shell = 1;
+			rc = capture_command(&cp, &buf, PATH_MAX);
+			if (rc)
+				die(_("outputDirectoryCmd command failed: "
+				      "'%s'"), config_output_directory_cmd);
+			strbuf_setlen(&buf, strcspn(buf.buf, "\r\n"));
+			config_output_directory = strbuf_detach(&buf, NULL);
+		}
+
 		output_directory = config_output_directory;
+	}
 
 	if (!use_stdout)
 		output_directory = set_outdir(prefix, output_directory);
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index 3aab25da76..725706ded5 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -1649,6 +1649,32 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' '
 	test_path_is_dir patchset
 '
 
+test_expect_success 'format-patch format.outputDirectoryCmd option' '
+	test_config format.outputDirectoryCmd "echo patches" &&
+	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 overrides format.outputDirectoryCmd' '
+	test_config format.outputDirectoryCmd "echo patches" &&
+	test_config format.outputDirectory patchset &&
+	rm -fr patches patchset &&
+	git format-patch master..side &&
+	test_path_is_missing patches &&
+	test_path_is_dir patchset
+'
+
+test_expect_success 'format-patch -o overrides format.outputDirectoryCmd' '
+	test_config format.outputDirectoryCmd "echo patches" &&
+	rm -fr patches patchset &&
+	git format-patch -o patchset master..side &&
+	test_path_is_missing patches &&
+	test_path_is_dir patchset
+'
+
 test_expect_success 'format-patch --base' '
 	git checkout patchid &&
 
-- 
2.23.0.13.g28bc381d7c


```

## Bert Wesarg, 2019-10-21 13:20

Subject: Re: [PATCH v5 1/2] format-patch: create leading components of output directory
Message-ID: <CAKPyHN3QQuX4Z8-sySU3DbQYj1mfTqGjzgX8RFw_H6UFA07ohw@mail.gmail.com>
URL: https://gitlist.dev/e/CAKPyHN3QQuX4Z8-sySU3DbQYj1mfTqGjzgX8RFw_H6UFA07ohw%40mail.gmail.com
In-Reply-To: <ac3cc3df4452355dc41c58fe94961c37ca09ebec.1571653441.git.bert.wesarg@googlemail.com>

```
Please ignore this. Will rebase on 2.24-rc0 and will only include the
test changes.

Bert

On Mon, Oct 21, 2019 at 12:25 PM Bert Wesarg <bert.wesarg@googlemail.com> wrote:
>
> 'git format-patch -o <outdir>' did an equivalent of 'mkdir <outdir>'
> not 'mkdir -p <outdir>', which is being corrected.
>
> Avoid the usage of 'adjust_shared_perm' on the leading directories which
> may have security implications. Achieved by temporarily disabling of
> 'config.sharedRepository' like 'git init' does.
>
> Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
>
> ---
> Changes in v2:
>  * squashed and base new tests on 'dl/format-patch-doc-test-cleanup'
>
> Changes in v3:
>  * avoid applying adjust_shared_perm
>
> Changes in v4:
>  * based on dl/format-patch-doc-test-cleanup and adopt it
>
> Changes in v5:
>  * make tests self-contained
>
> Cc: Denton Liu <liu.denton@gmail.com>
> Cc: Junio C Hamano <gitster@pobox.com>
> Cc: SZEDER Gábor <szeder.dev@gmail.com>
> ---
>  Documentation/config/format.txt    |  2 +-
>  Documentation/git-format-patch.txt |  3 ++-
>  builtin/log.c                      | 16 ++++++++++++++++
>  t/t4014-format-patch.sh            | 26 ++++++++++++++++++++++++++
>  4 files changed, 45 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/config/format.txt b/Documentation/config/format.txt
> index cb629fa769..40cad9278f 100644
> --- a/Documentation/config/format.txt
> +++ b/Documentation/config/format.txt
> @@ -81,7 +81,7 @@ format.coverLetter::
>
>  format.outputDirectory::
>         Set a custom directory to store the resulting files instead of the
> -       current working directory.
> +       current working directory. All directory components will be created.
>
>  format.useAutoBase::
>         A boolean value which lets you enable the `--base=auto` option of
> diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt
> index 0ac56f4b70..2035d4d5d5 100644
> --- a/Documentation/git-format-patch.txt
> +++ b/Documentation/git-format-patch.txt
> @@ -66,7 +66,8 @@ they are created in the current working directory. The default path
>  can be set with the `format.outputDirectory` configuration option.
>  The `-o` option takes precedence over `format.outputDirectory`.
>  To store patches in the current working directory even when
> -`format.outputDirectory` points elsewhere, use `-o .`.
> +`format.outputDirectory` points elsewhere, use `-o .`. All directory
> +components will be created.
>
>  By default, the subject of a single patch is "[PATCH] " followed by
>  the concatenation of lines from the commit message up to the first blank
> diff --git a/builtin/log.c b/builtin/log.c
> index 44b10b3415..8d08632858 100644
> --- a/builtin/log.c
> +++ b/builtin/log.c
> @@ -1765,10 +1765,26 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
>                 setup_pager();
>
>         if (output_directory) {
> +               int saved;
>                 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
>                         rev.diffopt.use_color = GIT_COLOR_NEVER;
>                 if (use_stdout)
>                         die(_("standard output, or directory, which one?"));
> +               /*
> +                * We consider <outdir> as 'outside of gitdir', therefore avoid
> +                * applying adjust_shared_perm in s-c-l-d.
> +                */
> +               saved = get_shared_repository();
> +               set_shared_repository(0);
> +               switch (safe_create_leading_directories_const(output_directory)) {
> +               case SCLD_OK:
> +               case SCLD_EXISTS:
> +                       break;
> +               default:
> +                       die(_("could not create leading directories "
> +                             "of '%s'"), output_directory);
> +               }
> +               set_shared_repository(saved);
>                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
>                         die_errno(_("could not create directory '%s'"),
>                                   output_directory);
> diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
> index 72b09896cf..3aab25da76 100755
> --- a/t/t4014-format-patch.sh
> +++ b/t/t4014-format-patch.sh
> @@ -1606,6 +1606,32 @@ test_expect_success 'From line has expected format' '
>         test_cmp from filtered
>  '
>
> +test_expect_success 'format-patch -o with no leading directories' '
> +       rm -fr patches &&
> +       git format-patch -o patches master..side &&
> +       count=$(git rev-list --count master..side) &&
> +       ls patches >list &&
> +       test_line_count = $count list
> +'
> +
> +test_expect_success 'format-patch -o with leading existing directories' '
> +       rm -rf existing-dir &&
> +       mkdir existing-dir &&
> +       git format-patch -o existing-dir/patches master..side &&
> +       count=$(git rev-list --count master..side) &&
> +       ls existing-dir/patches >list &&
> +       test_line_count = $count list
> +'
> +
> +test_expect_success 'format-patch -o with leading non-existing directories' '
> +       rm -rf non-existing-dir &&
> +       git format-patch -o non-existing-dir/patches master..side &&
> +       count=$(git rev-list --count master..side) &&
> +       test_path_is_dir non-existing-dir
> +       ls non-existing-dir/patches >list &&
> +       test_line_count = $count list
> +'
> +
>  test_expect_success 'format-patch format.outputDirectory option' '
>         test_config format.outputDirectory patches &&
>         rm -fr patches &&
> --
> 2.23.0.13.g28bc381d7c
>

```

## Junio C Hamano, 2019-10-23 02:08

Subject: Re: [PATCH v5 1/2] format-patch: create leading components of output directory
Message-ID: <xmqqh840jjsj.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqh840jjsj.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <CAKPyHN3QQuX4Z8-sySU3DbQYj1mfTqGjzgX8RFw_H6UFA07ohw@mail.gmail.com>

```
Bert Wesarg <bert.wesarg@googlemail.com> writes:

> Please ignore this. Will rebase on 2.24-rc0 and will only include the
> test changes.

Thanks.

```
