Crash when creating patch
Dear git-team,
I was trying to create a patch file of a commit to another directory. However, git crashed like that:
[josi@localhost typescript]$ git format-patch -1 236a9 --output=/home/josi/tmp/patches/txt-6117.patch 0001-Txture-Core-Implemented-whitelisting-for-groovy-scri.patch free(): invalid pointer
Aborted (core dumped)
While I'm not entirely sure I interpreted the man page correctly, I think the crash shouldn't happen anyway.
I'm using git 2.28.0, but my colleague is experiencing the same issue with git 2.25.1.
Please find attached the output of git bugreport and some more debug files that were collected by Fedora's ABRT. Please let me know if you need any other output of ABRT.
Best regards,
johannes
Johannes Postler
DevOps & Consulting
Txture - The Cloud Transformation Platform
Grabenweg 68
A-6020 Innsbruck
[email protected]
www.txture.io
Thank you for filling out a Git bug report!
Please answer the following questions to help us understand your issue.
What did you do before the bug happened? (Steps to reproduce your issue)
I tried to create a patch file of a commit in another directory using the following command:
[josi@localhost typescript]$ git format-patch -1 236a9 --output=/home/josi/tmp/patches/txt-6117.patch
What did you expect to happen? (Expected behavior) I expected a patch file at the location specified in --output
What happened instead? (Actual behavior)
git crashed:
0001-Txture-Core-Implemented-whitelisting-for-groovy-scri.patch free(): invalid pointer
Aborted (core dumped)
What's different between what you expected and what actually happened? I didn't expect the crash :D
Anything else you want to add:
I do have a full core dump recorded by ABRT (Fedora). Please let me know at [email protected] if you need thaThe following is the backtrace according to ABRT:
{ "signal": 6
, "executable": "/usr/bin/git"
, "stacktrace":
[ { "crash_thread": true
, "frames":
[ { "address": 139721002875845
, "build_id": "2602cd2de6dc672a5ec4378ace0b4a68ea5b13ea"
, "build_id_offset": 252869
, "function_name": "raise"
, "file_name": "/lib64/libc.so.6"
}
, { "address": 139721002780836
, "build_id": "2602cd2de6dc672a5ec4378ace0b4a68ea5b13ea"
, "build_id_offset": 157860
, "function_name": "abort"
, "file_name": "/lib64/libc.so.6"
}
, { "address": 139721003147559
, "build_id": "2602cd2de6dc672a5ec4378ace0b4a68ea5b13ea"
, "build_id_offset": 524583
, "function_name": "__libc_message"
, "file_name": "/lib64/libc.so.6"
}
, { "address": 139721003179548
, "build_id": "2602cd2de6dc672a5ec4378ace0b4a68ea5b13ea"
, "build_id_offset": 556572
, "function_name": ".annobin_top_check.start"
, "file_name": "/lib64/libc.so.6"
}
, { "address": 139721003186412
, "build_id": "2602cd2de6dc672a5ec4378ace0b4a68ea5b13ea"
, "build_id_offset": 563436
, "function_name": "_int_free"
, "file_name": "/lib64/libc.so.6"
}
, { "address": 139721003104931
, "build_id": "2602cd2de6dc672a5ec4378ace0b4a68ea5b13ea"
, "build_id_offset": 481955
, "function_name": "fclose@@GLIBC_2.2.5"
, "file_name": "/lib64/libc.so.6"
}
, { "address": 94420595460270
, "build_id": "ba2b8ad417de884d751cd7503b17e9ce357b2dcf"
, "build_id_offset": 538798
, "function_name": "cmd_format_patch"
, "file_name": "/usr/bin/git"
}
, { "address": 94420595060875
, "build_id": "ba2b8ad417de884d751cd7503b17e9ce357b2dcf"
, "build_id_offset": 139403
, "function_name": "handle_builtin.lto_priv.0"
, "file_name": "/usr/bin/git"
}
, { "address": 94420595041051
, "build_id": "ba2b8ad417de884d751cd7503b17e9ce357b2dcf"
, "build_id_offset": 119579
, "function_name": "main"
, "file_name": "/usr/bin/git"
} ]
} ]
}Please review the rest of the bug report below.
You can delete any lines you don't wish to share.
[System Info]
git version:
git version 2.28.0
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 5.8.16-300.fc33.x86_64 #1 SMP Mon Oct 19 13:18:33 UTC 2020 x86_64 compiler info: gnuc: 10.2
libc info: glibc: 2.32
$SHELL (typically, interactive shell): /bin/bash
[Enabled Hooks]
applypatch-msg
pre-applypatch
post-applypatch
pre-commit
pre-merge-commit
prepare-commit-msg
commit-msg
post-commit
pre-rebase
post-checkout
post-merge
pre-push
pre-receive
update
post-receive
post-update
push-to-checkout
pre-auto-gc
post-rewrite
sendemail-validate
Quoted 5 lines · Johannes Postler
[josi@localhost typescript]$ git format-patch -1 236a9 --output=/home/josi/tmp/patches/txt-6117.patch 0001-Txture-Core-Implemented-whitelisting-for-groovy-scri.patch
free(): invalid pointer
Aborted (core dumped)
Thanks for the report. I was able to reproduce the problem here.
The issue is that "--output" was never supposed to work with format-patch. But a subtle change in the option parsing a while back caused it to be respected. And as you noticed, the documentation mistakenly mentions the option, since format-patch includes the standard diff-options text.
So one obvious fix would be to forbid it and adjust the documentation. But because of the way the option parsers interact, it's surprisingly hard to do so cleanly. It's actually easier to just make it do something useful (i.e., behave like --stdout but sent to a file). So I did that instead.
[1/3]: format-patch: refactor output selection [2/3]: format-patch: tie file-opening logic to output_directory [3/3]: format-patch: support --output option
builtin/log.c | 37 ++++++++++++++++++++++--------------- t/t4014-format-patch.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 15 deletions(-)
-Peff
PATCH 1/3 format-patch: refactor output selection
The --stdout and --output-directory options are mutually exclusive, but it's hard to tell from reading the code. We have three separate conditionals that check for use_stdout, and it's only after we've set up the output_directory fully that we check whether the user also specified --stdout.
Instead, let's check the exclusion explicitly first, then have a single conditional that handles stdout versus an output directory. This is slightly easier to follow now, and also will keep things sane when we add another output mode in a future patch.
--- builtin/log.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
builtin/log.c +9 -9
@@ -1942,20 +1942,20 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)if (rev.show_notes)load_display_notes (&rev.notes_opt); -if (!output_directory && !use_stdout) - output_directory = config_output_directory; +if (use_stdout + !!output_directory >1 ) +die (_ ("specify only one of --stdout, --output, and --output-directory" )); -if (!use_stdout) - output_directory =set_outdir (prefix, output_directory); -else +if (use_stdout) {setup_pager (); - -if (output_directory) { + }else {int saved; + +if (!output_directory) + output_directory = config_output_directory; + output_directory =set_outdir (prefix, output_directory); +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.
Signature · 1 line
2.29.2.559.g8ec94df761
Re: [PATCH 1/3] format-patch: refactor output selection
Quoted 15 lines · Jeff King
The --stdout and --output-directory options are mutually exclusive, but it's hard to tell from reading the code. We have three separate conditionals that check for
use_stdout, and it's only after we've set up theoutput_directoryfully that we check whether the user also specified --stdout.Instead, let's check the exclusion explicitly first, then have a single conditional that handles stdout versus an output directory. This is slightly easier to follow now, and also will keep things sane when we add another output mode in a future patch.
Signed-off-by Jeff King <[email protected]>---
builtin/log.c int cmd_format_patch(int argc, const char **argv, const char *prefix) · line 1943
Quoted 2 lines
builtin/log.c +2
-0@@ -1942,20 +1942,20 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) +if (use_stdout + !!output_directory >1 ) +die (_ ("specify only one of --stdout, --output, and --output-directory" ));
Is mention of --output intentional here? The commit message only talks about --stdout and --output-directory.
It's subjective, but "mutually exclusive" sounds a bit more consistent with other similar error messages elsewhere:
--stdout, --output, and --output-directory are mutually exclusive
Re: [PATCH 1/3] format-patch: refactor output selection
Quoted 7 lines · Eric Sunshine
builtin/log.c int cmd_format_patch(int argc, const char **argv, const char *prefix) · line 1943
Quoted 4 lines
builtin/log.c +2
-0@@ -1942,20 +1942,20 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) +if (use_stdout + !!output_directory >1 ) +die (_ ("specify only one of --stdout, --output, and --output-directory" ));Is mention of --output intentional here? The commit message only talks about --stdout and --output-directory.
Whoops, thanks. I wrote this line after adding the new feature, but forgot to revise it when I rebased.
Quoted 4 lines
It's subjective, but "mutually exclusive" sounds a bit more consistent with other similar error messages elsewhere:
--stdout, --output, and --output-directory are mutually exclusive
Yeah, that reads better. I remember I reworded it a few times to try to get it not-awkward, but I'm not sure how I failed to come up with that quite obvious wording. ;)
-Peff
PATCH 2/3 format-patch: tie file-opening logic to output_directory
In reply to Jeff KingIn format-patch we're either outputting to stdout or to individual files in an output directory (which maybe just "./"). Our logic for whether to open a new file for each patch is checked with "!use_stdout", but it is equally correct to check for a non-NULL output_directory.
The distinction will matter when we add a new single-stream output in a future patch, when only one of the three methods will want individual files. Let's swap the logic here in preparation.
--- builtin/log.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
builtin/log.c +6 -6
@@ -1153,7 +1153,7 @@ static void get_notes_args(struct strvec *arg, struct rev_info *rev) } } -static void make_cover_letter(struct rev_info *rev, int use_stdout, +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,
@@ -1173,7 +1173,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout, committer =git_committer_info (0 ); -if (!use_stdout && +if (use_separate_file &&open_next_file (NULL , rev->numbered_files ?NULL :"cover-letter" , rev, quiet))die (_ ("failed to create cover-letter file" ));
@@ -2117,7 +2117,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)if (cover_letter) {if (thread)gen_message_id (&rev,"cover" ); -make_cover_letter (&rev, use_stdout, +make_cover_letter (&rev, !!output_directory, origin, nr, list, branch_name, quiet);print_bases (&bases, rev.diffopt.file);print_signature (rev.diffopt.file);
@@ -2172,7 +2172,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)gen_message_id (&rev,oid_to_hex (&commit->object.oid)); } -if (!use_stdout && +if (output_directory &&open_next_file (rev.numbered_files ?NULL : commit,NULL , &rev, quiet))die (_ ("failed to create output files" )); shown =log_tree_commit (&rev, commit);
@@ -2185,7 +2185,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)* the log; when using one file per patch, we do * not want the extra blank line. */ -if (!use_stdout) +if (output_directory) rev.shown_one =0 ;if (shown) {print_bases (&bases, rev.diffopt.file);
@@ -2196,7 +2196,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)else print_signature (rev.diffopt.file); } -if (!use_stdout) +if (output_directory)fclose (rev.diffopt.file); }stop_progress (&progress);
Signature · 1 line
2.29.2.559.g8ec94df761
Re: [PATCH 2/3] format-patch: tie file-opening logic to output_directory
Quoted 2 lines · Jeff King
In format-patch we're either outputting to stdout or to individual files in an output directory (which maybe just "./"). Our logic for whether to
s/maybe/may be/
Quoted 8 lines
open a new file for each patch is checked with "!use_stdout", but it is equally correct to check for a non-NULL
output_directory.The distinction will matter when we add a new single-stream output in a future patch, when only one of the three methods will want individual files. Let's swap the logic here in preparation.
Signed-off-by Jeff King <[email protected]>
PATCH 3/3 format-patch: support --output option
In reply to Jeff KingWe've never intended to support diff's --output option in format-patch. And until baa4adc66a (parse-options: disable option abbreviation with PARSE_OPT_KEEP_UNKNOWN, 2019-01-27), it was impossible to trigger. We first parse the format-patch options before handing the remainder off to setup_revisions(). Before that commit, we'd accept "--output=foo" as an abbreviation for "--output-directory=foo". But afterwards, we don't check abbreviations, and --output gets passed to the diff code.
This results in nonsense behavior and bugs. The diff code will have opened a filehandle at rev.diffopt.file, but we'll overwrite that with our own handles that we open for each individual patch file. So the --output file will always just be empty. But worse, the diff code also sets rev.diffopt.close_file, so log_tree_commit() will close the filehandle itself. And then the main loop in cmd_format_patch() will try to close it again, resulting in a double-free.
The simplest solution would be to just disallow --output with format-patch, as nobody ever intended it to work. However, we have accidentally documented it (because format-patch includes diff-options). And it does work with "git log", which writes the whole output to the specified file. It's easy enough to make that work for format-patch,
too: it's really the same as --stdout, but pointed at a specific file.
We can detect the use of the --output option by the "close_file" flag (note that we can't use rev.diffopt.file, since the diff setup will otherwise set it to stdout). So we just need to unset that flag, but don't have to do anything else. Our situation is otherwise exactly like --stdout (note that we don't fclose() the file, but nor does the stdout case; exiting the program takes care of that for us).
--- builtin/log.c | 9 ++++++++- t/t4014-format-patch.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-)
builtin/log.c +8 -1
@@ -1942,11 +1942,18 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)if (rev.show_notes)load_display_notes (&rev.notes_opt); -if (use_stdout + !!output_directory >1 ) +if (use_stdout + rev.diffopt.close_file + !!output_directory >1 )die (_ ("specify only one of --stdout, --output, and --output-directory" ));if (use_stdout) {setup_pager (); + }else if (rev.diffopt.close_file) { +/* +* The diff code parsed --output; it has already opened the +* file, but but we must instruct it not to close after each +* diff. +*/ + rev.diffopt.close_file =0 ; }else {int saved;
t/t4014-format-patch.sh +33 -0
@@ -1919,6 +1919,39 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' ' test_path_is_dir patchset ' +test_expect_success 'format-patch forbids multiple outputs' ' + rm -fr outfile outdir && + test_must_fail \ + git format-patch --stdout --output=outfile && + test_must_fail \ + git format-patch --stdout --output-directory=outdir && + test_must_fail \ + git format-patch --output=outfile --output-directory=outdir +' + +test_expect_success 'configured outdir does not conflict with output options' ' + rm -fr outfile outdir && + test_config format.outputDirectory outdir && + git format-patch --stdout && + test_path_is_missing outdir && + git format-patch --output=outfile && + test_path_is_missing outdir +' + +test_expect_success 'format-patch --output' ' + rm -fr outfile && + git format-patch -3 --stdout HEAD >expect && + git format-patch -3 --output=outfile HEAD && + test_cmp expect outfile +' + +test_expect_success 'format-patch --cover-letter --output' ' + rm -fr outfile && + git format-patch --cover-letter -3 --stdout HEAD >expect && + git format-patch --cover-letter -3 --output=outfile HEAD && + test_cmp expect outfile +' + test_expect_success 'format-patch --base' ' git checkout patchid &&
Signature · 1 line
2.29.2.559.g8ec94df761
Re: [PATCH 3/3] format-patch: support --output option
Quoted 12 lines · Jeff King
[...]
The simplest solution would be to just disallow --output with format-patch, as nobody ever intended it to work. However, we have accidentally documented it (because format-patch includes diff-options). And it does work with "git log", which writes the whole output to the specified file. It's easy enough to make that work for format-patch,
too: it's really the same as --stdout, but pointed at a specific file.[...]Signed-off-by Jeff King <[email protected]>---
builtin/log.c int cmd_format_patch(int argc, const char **argv, const char *prefix) · line 1949
Quoted 8 lines
builtin/log.c +7
-0@@ -1942,11 +1942,18 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) + }else if (rev.diffopt.close_file) { +/* +* The diff code parsed --output; it has already opened the +* file, but but we must instruct it not to close after each +* diff. +*/ + rev.diffopt.close_file =0 ; }else {
The commit message's justification for supporting --output seems reasonable. However, my knee-jerk reaction to the implementation was that it feels overly magical and a bit too hacky. I can see the logic in it but it also leaves a bad taste when the implementation has to "undo" a side-effect of some other piece of code, which makes it feel unplanned and fragile. The question which popped into my mind immediately was "why not handle --output explicitly via builtin_format_patch_options[] along with other first-class options?". This review comment may or may not be actionable; it's just expressing surprise and a bit of nose-wrinkling I experienced.
t/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1927
Quoted 11 lines
t/t4014-format-patch.sh +9
-0@@ -1919,6 +1919,39 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' ' +test_expect_success 'format-patch forbids multiple outputs' ' + rm -fr outfile outdir && + test_must_fail \ + git format-patch --stdout --output=outfile && + test_must_fail \ + git format-patch --stdout --output-directory=outdir && + test_must_fail \ + git format-patch --output=outfile --output-directory=outdir +'
I would have expected to see this test added in patch [1/3], and then extended by this patch with the addition of the --output case(s).
Re: [PATCH 3/3] format-patch: support --output option
Quoted 13 lines · Eric Sunshine
builtin/log.c int cmd_format_patch(int argc, const char **argv, const char *prefix) · line 1949
Quoted 10 lines
builtin/log.c +7
-0@@ -1942,11 +1942,18 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) + }else if (rev.diffopt.close_file) { +/* +* The diff code parsed --output; it has already opened the +* file, but but we must instruct it not to close after each +* diff. +*/ + rev.diffopt.close_file =0 ; }else {The commit message's justification for supporting --output seems reasonable. However, my knee-jerk reaction to the implementation was
Quoted 8 lines
that it feels overly magical and a bit too hacky. I can see the logic in it but it also leaves a bad taste when the implementation has to "undo" a side-effect of some other piece of code, which makes it feel unplanned and fragile. The question which popped into my mind immediately was "why not handle --output explicitly via builtin_format_patch_options[] along with other first-class options?". This review comment may or may not be actionable; it's just expressing surprise and a bit of nose-wrinkling I experienced.
I agree it's a bit magical. I didn't really consider writing a new "--output" option for format-patch, because I came at it from the angle of "let's unbreak the existing diff --output option". Which isn't necessarily a defense, but just an explanation.
FWIW, that unsetting of rev.diffopt.close_file is exactly how git-log does it. So while I agree it's a bit ugly, this is the intended way for --output to be used across multiple diffs, and with log_tree_commit(). I'd prefer to go this way for now, and if somebody wants to make it less ugly, they can clean up all of the callers in one go.
Amusingly, 6ea57703f6 (log: prepare log/log-tree to reuse the diffopt.close_file attribute, 2016-06-22) which introduced this code foresaw the format-patch bug here.
-Peff
Re: [PATCH 3/3] format-patch: support --output option
Quoted 8 lines · Jeff King
Quoted 6 lines · Eric Sunshine
The commit message's justification for supporting --output seems reasonable. However, my knee-jerk reaction to the implementation was that it feels overly magical and a bit too hacky. I can see the logic in it but it also leaves a bad taste when the implementation has to "undo" a side-effect of some other piece of code, which makes it feel unplanned and fragile. [...]
Quoted 5 lines
FWIW, that unsetting of rev.diffopt.close_file is exactly how git-log does it. So while I agree it's a bit ugly, this is the intended way for --output to be used across multiple diffs, and with
log_tree_commit(). I'd prefer to go this way for now, and if somebody wants to make it less ugly, they can clean up all of the callers in one go.
If you intend to re-roll, it might make sense to include this explanation in the commit message since existing precedent helps sell the ugliness (bad taste), making for a less negative knee-jerk reaction.
Re: [PATCH 3/3] format-patch: support --output option
In reply to Jeff Kingt/t4014-format-patch.sh test_expect_success 'format-patch -o overrides format.outputDirectory' ' · line 1953
Quoted 13 lines · Jeff King
t/t4014-format-patch.sh +13
-0+test_expect_success 'format-patch --output' ' + rm -fr outfile && + git format-patch -3 --stdout HEAD >expect && + git format-patch -3 --output=outfile HEAD && + test_cmp expect outfile +' + +test_expect_success 'format-patch --cover-letter --output' ' + rm -fr outfile && + git format-patch --cover-letter -3 --stdout HEAD >expect && + git format-patch --cover-letter -3 --output=outfile HEAD && + test_cmp expect outfile +'
It is pleasing to see an obvious and clear demonstration that "--output=X" is equivalent to "--stdout >X".
PATCH v2 1/1 format-patch --output
In reply to Jeff KingQuoted 12 lines · Jeff King
The issue is that "--output" was never supposed to work with format-patch. But a subtle change in the option parsing a while back caused it to be respected. And as you noticed, the documentation mistakenly mentions the option, since format-patch includes the standard diff-options text.
So one obvious fix would be to forbid it and adjust the documentation. But because of the way the option parsers interact, it's surprisingly hard to do so cleanly. It's actually easier to just make it do something useful (i.e., behave like --stdout but sent to a file). So I did that instead.
Quoted 3 lines
[1/3]: format-patch: refactor output selection [2/3]: format-patch: tie file-opening logic to output_directory [3/3]: format-patch: support --output option
Here's a re-roll taking into account the comments from Eric. The only thing I didn't do is rewrite --output as a format-patch option. As I said in the thread, I'd rather keep parity with how git-log works here (though I don't mind if somebody wants to do further clean up on top).
[1/3]: format-patch: refactor output selection [2/3]: format-patch: tie file-opening logic to output_directory [3/3]: format-patch: support --output option
builtin/log.c | 37 ++++++++++++++++++++++--------------- t/t4014-format-patch.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 15 deletions(-)
Range diff from v1:
1: 49e8b54549 ! 1: 9206d6852b format-patch: refactor output selection
@@ Commit message
slightly easier to follow now, and also will keep things sane when we
add another output mode in a future patch.
+ We'll add a few tests as well, covering the mutual exclusion and the
+ fact that we are not confused by a configured output directory.
+
Signed-off-by: Jeff King <[email protected]>
## builtin/log.c ##
@@ builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre
- if (!output_directory && !use_stdout)
- output_directory = config_output_directory;
+ if (use_stdout + !!output_directory > 1)
-+ die(_("specify only one of --stdout, --output, and --output-directory"));
++ die(_("--stdout and --output-directory are mutually exclusive"));
- if (!use_stdout)
- output_directory = set_outdir(prefix, output_directory);
@@ builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre
/*
* We consider <outdir> as 'outside of gitdir', therefore avoid
* applying adjust_shared_perm in s-c-l-d.
+
+ ## t/t4014-format-patch.sh ##
+@@ t/t4014-format-patch.sh: test_expect_success 'format-patch -o overrides format.outputDirectory' '
+ test_path_is_dir patchset
+ '
+
++test_expect_success 'format-patch forbids multiple outputs' '
++ rm -fr outdir &&
++ test_must_fail \
++ git format-patch --stdout --output-directory=outdir
++'
++
++test_expect_success 'configured outdir does not conflict with output options' '
++ rm -fr outfile outdir &&
++ test_config format.outputDirectory outdir &&
++ git format-patch --stdout &&
++ test_path_is_missing outdir
++'
++
+ test_expect_success 'format-patch --base' '
+ git checkout patchid &&
+ 2: 884c06861d ! 2: 9dc30924b2 format-patch: tie file-opening logic to output_directory
@@ Commit message
format-patch: tie file-opening logic to output_directory
In format-patch we're either outputting to stdout or to individual files
- in an output directory (which maybe just "./"). Our logic for whether to
- open a new file for each patch is checked with "!use_stdout", but it is
- equally correct to check for a non-NULL output_directory.
+ in an output directory (which may be just "./"). Our logic for whether
+ to open a new file for each patch is checked with "!use_stdout", but it
+ is equally correct to check for a non-NULL output_directory.
The distinction will matter when we add a new single-stream output in a future patch, when only one of the three methods will want individual
3: 8befceb150 ! 3: 2b0fab9b50 format-patch: support --output option
@@ builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre
load_display_notes(&rev.notes_opt);
- if (use_stdout + !!output_directory > 1)
+- die(_("--stdout and --output-directory are mutually exclusive"));
+ if (use_stdout + rev.diffopt.close_file + !!output_directory > 1)
- die(_("specify only one of --stdout, --output, and --output-directory"));
++ die(_("--stdout, --output, and --output-directory are mutually exclusive"));
if (use_stdout) {
setup_pager();
@@ builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre
## t/t4014-format-patch.sh ##
@@ t/t4014-format-patch.sh: test_expect_success 'format-patch -o overrides format.outputDirectory' '
- test_path_is_dir patchset
- '
-
-+test_expect_success 'format-patch forbids multiple outputs' '
-+ rm -fr outfile outdir &&
+ test_expect_success 'format-patch forbids multiple outputs' '
+ rm -fr outdir &&
+ test_must_fail \
+- git format-patch --stdout --output-directory=outdir
++ git format-patch --stdout --output-directory=outdir &&
+ test_must_fail \
+ git format-patch --stdout --output=outfile &&
+ test_must_fail \
-+ git format-patch --stdout --output-directory=outdir &&
-+ test_must_fail \
+ git format-patch --output=outfile --output-directory=outdir
-+'
-+
-+test_expect_success 'configured outdir does not conflict with output options' '
-+ rm -fr outfile outdir &&
-+ test_config format.outputDirectory outdir &&
-+ git format-patch --stdout &&
+ '
+
+ test_expect_success 'configured outdir does not conflict with output options' '
+ rm -fr outfile outdir &&
+ test_config format.outputDirectory outdir &&
+ git format-patch --stdout &&
+ test_path_is_missing outdir &&
+ git format-patch --output=outfile &&
-+ test_path_is_missing outdir
-+'
-+
+ test_path_is_missing outdir
+ '
+
+test_expect_success 'format-patch --output' '
+ rm -fr outfile &&
+ git format-patch -3 --stdout HEAD >expect &&PATCH v2 1/3 format-patch: refactor output selection
The --stdout and --output-directory options are mutually exclusive, but it's hard to tell from reading the code. We have three separate conditionals that check for use_stdout, and it's only after we've set up the output_directory fully that we check whether the user also specified --stdout.
Instead, let's check the exclusion explicitly first, then have a single conditional that handles stdout versus an output directory. This is slightly easier to follow now, and also will keep things sane when we add another output mode in a future patch.
We'll add a few tests as well, covering the mutual exclusion and the fact that we are not confused by a configured output directory.
--- builtin/log.c | 18 +++++++++--------- t/t4014-format-patch.sh | 13 +++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-)
builtin/log.c +9 -9
@@ -1942,20 +1942,20 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)if (rev.show_notes)load_display_notes (&rev.notes_opt); -if (!output_directory && !use_stdout) - output_directory = config_output_directory; +if (use_stdout + !!output_directory >1 ) +die (_ ("--stdout and --output-directory are mutually exclusive" )); -if (!use_stdout) - output_directory =set_outdir (prefix, output_directory); -else +if (use_stdout) {setup_pager (); - -if (output_directory) { + }else {int saved; + +if (!output_directory) + output_directory = config_output_directory; + output_directory =set_outdir (prefix, output_directory); +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.
t/t4014-format-patch.sh +13 -0
@@ -1919,6 +1919,19 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' ' test_path_is_dir patchset ' +test_expect_success 'format-patch forbids multiple outputs' ' + rm -fr outdir && + test_must_fail \ + git format-patch --stdout --output-directory=outdir +' + +test_expect_success 'configured outdir does not conflict with output options' ' + rm -fr outdir && + test_config format.outputDirectory outdir && + git format-patch --stdout && + test_path_is_missing outdir +' + test_expect_success 'format-patch --base' ' git checkout patchid &&
Signature · 1 line
2.29.2.559.g8ec94df761
PATCH v2 2/3 format-patch: tie file-opening logic to output_directory
In reply to Jeff KingIn format-patch we're either outputting to stdout or to individual files in an output directory (which may be just "./"). Our logic for whether to open a new file for each patch is checked with "!use_stdout", but it is equally correct to check for a non-NULL output_directory.
The distinction will matter when we add a new single-stream output in a future patch, when only one of the three methods will want individual files. Let's swap the logic here in preparation.
--- builtin/log.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
builtin/log.c +6 -6
@@ -1153,7 +1153,7 @@ static void get_notes_args(struct strvec *arg, struct rev_info *rev) } } -static void make_cover_letter(struct rev_info *rev, int use_stdout, +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,
@@ -1173,7 +1173,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout, committer =git_committer_info (0 ); -if (!use_stdout && +if (use_separate_file &&open_next_file (NULL , rev->numbered_files ?NULL :"cover-letter" , rev, quiet))die (_ ("failed to create cover-letter file" ));
@@ -2117,7 +2117,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)if (cover_letter) {if (thread)gen_message_id (&rev,"cover" ); -make_cover_letter (&rev, use_stdout, +make_cover_letter (&rev, !!output_directory, origin, nr, list, branch_name, quiet);print_bases (&bases, rev.diffopt.file);print_signature (rev.diffopt.file);
@@ -2172,7 +2172,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)gen_message_id (&rev,oid_to_hex (&commit->object.oid)); } -if (!use_stdout && +if (output_directory &&open_next_file (rev.numbered_files ?NULL : commit,NULL , &rev, quiet))die (_ ("failed to create output files" )); shown =log_tree_commit (&rev, commit);
@@ -2185,7 +2185,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)* the log; when using one file per patch, we do * not want the extra blank line. */ -if (!use_stdout) +if (output_directory) rev.shown_one =0 ;if (shown) {print_bases (&bases, rev.diffopt.file);
@@ -2196,7 +2196,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)else print_signature (rev.diffopt.file); } -if (!use_stdout) +if (output_directory)fclose (rev.diffopt.file); }stop_progress (&progress);
Signature · 1 line
2.29.2.559.g8ec94df761
PATCH v2 3/3 format-patch: support --output option
In reply to Jeff KingWe've never intended to support diff's --output option in format-patch. And until baa4adc66a (parse-options: disable option abbreviation with PARSE_OPT_KEEP_UNKNOWN, 2019-01-27), it was impossible to trigger. We first parse the format-patch options before handing the remainder off to setup_revisions(). Before that commit, we'd accept "--output=foo" as an abbreviation for "--output-directory=foo". But afterwards, we don't check abbreviations, and --output gets passed to the diff code.
This results in nonsense behavior and bugs. The diff code will have opened a filehandle at rev.diffopt.file, but we'll overwrite that with our own handles that we open for each individual patch file. So the --output file will always just be empty. But worse, the diff code also sets rev.diffopt.close_file, so log_tree_commit() will close the filehandle itself. And then the main loop in cmd_format_patch() will try to close it again, resulting in a double-free.
The simplest solution would be to just disallow --output with format-patch, as nobody ever intended it to work. However, we have accidentally documented it (because format-patch includes diff-options). And it does work with "git log", which writes the whole output to the specified file. It's easy enough to make that work for format-patch,
too: it's really the same as --stdout, but pointed at a specific file.
We can detect the use of the --output option by the "close_file" flag (note that we can't use rev.diffopt.file, since the diff setup will otherwise set it to stdout). So we just need to unset that flag, but don't have to do anything else. Our situation is otherwise exactly like --stdout (note that we don't fclose() the file, but nor does the stdout case; exiting the program takes care of that for us).
--- builtin/log.c | 11 +++++++++-- t/t4014-format-patch.sh | 26 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-)
builtin/log.c +9 -2
@@ -1942,11 +1942,18 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)if (rev.show_notes)load_display_notes (&rev.notes_opt); -if (use_stdout + !!output_directory >1 ) -die (_ ("--stdout and --output-directory are mutually exclusive" )); +if (use_stdout + rev.diffopt.close_file + !!output_directory >1 ) +die (_ ("--stdout, --output, and --output-directory are mutually exclusive" ));if (use_stdout) {setup_pager (); + }else if (rev.diffopt.close_file) { +/* +* The diff code parsed --output; it has already opened the +* file, but but we must instruct it not to close after each +* diff. +*/ + rev.diffopt.close_file =0 ; }else {int saved;
t/t4014-format-patch.sh +23 -3
@@ -1920,18 +1920,38 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' ' ' test_expect_success 'format-patch forbids multiple outputs' ' - rm -fr outdir && + rm -fr outfile outdir && test_must_fail \ - git format-patch --stdout --output-directory=outdir + git format-patch --stdout --output-directory=outdir && + test_must_fail \ + git format-patch --stdout --output=outfile && + test_must_fail \ + git format-patch --output=outfile --output-directory=outdir ' test_expect_success 'configured outdir does not conflict with output options' ' - rm -fr outdir && + rm -fr outfile outdir && test_config format.outputDirectory outdir && git format-patch --stdout && + test_path_is_missing outdir && + git format-patch --output=outfile && test_path_is_missing outdir ' +test_expect_success 'format-patch --output' ' + rm -fr outfile && + git format-patch -3 --stdout HEAD >expect && + git format-patch -3 --output=outfile HEAD && + test_cmp expect outfile +' + +test_expect_success 'format-patch --cover-letter --output' ' + rm -fr outfile && + git format-patch --cover-letter -3 --stdout HEAD >expect && + git format-patch --cover-letter -3 --output=outfile HEAD && + test_cmp expect outfile +' + test_expect_success 'format-patch --base' ' git checkout patchid &&
Signature · 1 line
2.29.2.559.g8ec94df761
Re: [PATCH v2] format-patch --output
In reply to Jeff KingThanks a lot everyone for fixing this so quickly! Please let me know if there is anything else I can do. Best,
johannes
Johannes Postler
DevOps & Consulting
Txture - The Cloud Transformation Platform
Grabenweg 68
A-6020 Innsbruck
[email protected]
www.txture.io
Johannes Postler
DevOps & Consulting
Txture - The Cloud Transformation Platform
Grabenweg 68
A-6020 Innsbruck
[email protected]
www.txture.io
Follow us:
Quoted 150 lines · Jeff King
Quoted 12 lines · Jeff King
The issue is that "--output" was never supposed to work with format-patch. But a subtle change in the option parsing a while back caused it to be respected. And as you noticed, the documentation mistakenly mentions the option, since format-patch includes the standard diff-options text.
So one obvious fix would be to forbid it and adjust the documentation. But because of the way the option parsers interact, it's surprisingly hard to do so cleanly. It's actually easier to just make it do something useful (i.e., behave like --stdout but sent to a file). So I did that instead.
Quoted 3 lines
[1/3]: format-patch: refactor output selection [2/3]: format-patch: tie file-opening logic to output_directory [3/3]: format-patch: support --output optionHere's a re-roll taking into account the comments from Eric. The only thing I didn't do is rewrite --output as a format-patch option. As I said in the thread, I'd rather keep parity with how git-log works here (though I don't mind if somebody wants to do further clean up on top).
[1/3]: format-patch: refactor output selection [2/3]: format-patch: tie file-opening logic to output_directory [3/3]: format-patch: support --output optionbuiltin/log.c | 37 ++++++++++++++++++++++--------------- t/t4014-format-patch.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 15 deletions(-)Range diff from v1:
1:
49e8b54549! 1:9206d6852bformat-patch: refactor output selection
@@ Commit message
slightly easier to follow now, and also will keep things sane when we add another output mode in a future patch.+ We'll add a few tests as well, covering the mutual exclusion and the
+ fact that we are not confused by a configured output directory.
+Signed-off-by Jeff King <[email protected]>##
builtin/log.c##
@@builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre
- if (!output_directory && !use_stdout)
-output_directory=config_output_directory;
+ if (use_stdout+ !!output_directory > 1)
-+die(_("specify only one of --stdout, --output, and --output-directory"));
++die(_("--stdout and --output-directory are mutually exclusive"));- if (!use_stdout)
-output_directory=set_outdir(prefix, output_directory);
@@builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre
/*
* We consider <outdir> as 'outside of gitdir', therefore avoid
* applyingadjust_shared_permin s-c-l-d.
+
+ ##t/t4014-format-patch.sh##
+@@t/t4014-format-patch.sh:test_expect_success'format-patch -o overrides format.outputDirectory' '
+test_path_is_dirpatchset
+ '
+
++test_expect_success 'format-patch forbids multiple outputs' '
++ rm -fr outdir &&
++test_must_fail\
++ git format-patch --stdout --output-directory=outdir
++'
++
++test_expect_success 'configured outdir does not conflict with output options' '
++ rm -fr outfile outdir &&
++test_configformat.outputDirectory outdir &&
++ git format-patch --stdout &&
++test_path_is_missingoutdir
++'
++
+test_expect_success'format-patch --base' '
+ git checkout patchid &&
+
2:884c06861d! 2:9dc30924b2format-patch: tie file-opening logic tooutput_directory
@@ Commit message
format-patch: tie file-opening logic tooutput_directoryIn format-patch we're either outputting to stdout or to individual files
- in an output directory (which maybe just "./"). Our logic for whether to
- open a new file for each patch is checked with "!use_stdout", but it is
- equally correct to check for a non-NULLoutput_directory.
+ in an output directory (which may be just "./"). Our logic for whether
+ to open a new file for each patch is checked with "!use_stdout", but it
+ is equally correct to check for a non-NULLoutput_directory.The distinction will matter when we add a new single-stream output in a future patch, when only one of the three methods will want individual
3:8befceb150! 3:2b0fab9b50format-patch: support --output option
@@builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre
load_display_notes(&rev.notes_opt);- if (
use_stdout+ !!output_directory > 1)
+-die(_("--stdout and --output-directory are mutually exclusive"));
+ if (use_stdout+ rev.diffopt.close_file + !!output_directory > 1)
-die(_("specify only one of --stdout, --output, and --output-directory"));
++die(_("--stdout, --output, and --output-directory are mutually exclusive"));if (
use_stdout) {
setup_pager();
@@builtin/log.c: int cmd_format_patch(int argc, const char **argv, const char *pre##
t/t4014-format-patch.sh##
@@t/t4014-format-patch.sh:test_expect_success'format-patch -o overrides format.outputDirectory' '
-test_path_is_dirpatchset
- '
-
-+test_expect_success 'format-patch forbids multiple outputs' '
-+ rm -fr outfile outdir &&
+test_expect_success'format-patch forbids multiple outputs' '
+ rm -fr outdir &&
+test_must_fail\
+- git format-patch --stdout --output-directory=outdir
++ git format-patch --stdout --output-directory=outdir &&
+test_must_fail\
+ git format-patch --stdout --output=outfile &&
+test_must_fail\
-+ git format-patch --stdout --output-directory=outdir &&
-+test_must_fail\
+ git format-patch --output=outfile --output-directory=outdir
-+'
-+
-+test_expect_success 'configured outdir does not conflict with output options' '
-+ rm -fr outfile outdir &&
-+test_configformat.outputDirectory outdir &&
-+ git format-patch --stdout &&
+ '
+
+test_expect_success'configured outdir does not conflict with output options' '
+ rm -fr outfile outdir &&
+test_configformat.outputDirectory outdir &&
+ git format-patch --stdout &&
+test_path_is_missingoutdir &&
+ git format-patch --output=outfile &&
-+test_path_is_missingoutdir
-+'
-+
+test_path_is_missingoutdir
+ '
+
+test_expect_success 'format-patch --output' '
+ rm -fr outfile &&
+ git format-patch -3 --stdout HEAD >expect &&