Is there a way to get the "format-patch" formatted file name?
Hi,
My typical use case is to download patches from GH, e.g.:
~~~
$ curl -OL https://github.com/rails/sprockets/pull/791.patch % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 12717 0 12717 0 0 21765 0 --:--:-- --:--:-- --:--:-- 58603
~~~
The problem with this is that I end up with the "791.patch" file, while I'd like have a file with similar name as if I have used the git command:
~~~
$ git format-patch -1 6554b6d 0001-Fix-Minitest-constant-name-in-tests.patch
~~~
So I wonder, is there a way to get such file name?
The basic use case which would be enough for me would be to obtain the string:
~~~
$ head -4 791.patch | tail -1 Subject: [PATCH] Fix Minitest constant name in tests ~~~
and then use something like:
~~~
$ echo "Fix Minitest constant name in tests" | git sanitize-string-into-filename
Fix-Minitest-constant-name-in-tests
~~~
Of course I can imagine something more fancy such as:
~~~
$ git extract-filenames-from-patch 791.patch
0001-Fix-Minitest-constant-name-in-tests.patch
~~~
IOW, I'd like if the fmt_output_subject method (if that is the right one):
https://github.com/git/git/blob/6e8611e90a629e38da1e7c0e1f986bc6ec23a330/log-tree.c#L388
was more user exposed.
Of course I could put some script like this together myself, but I think that Git already has all the pluming.
Thank you in advance
Vít
P.S.: Please keep me in the CC, because I am not subscribed to this ML
Quoted 4 lines · Vít Ondruch
$ git extract-filenames-from-patch 791.patch0001-Fix-Minitest-constant-name-in-tests.patch
git log --pretty=format:%f does that.
of course you need to apply the patch first for that to work, which is a bit inefficient and arguably inelegant if you don't intend to do that anyway.
Quoted 2 lines
P.S.: Please keep me in the CC
that's this list's policy anyway.
regards
Dne 31. 08. 23 v 13:04 Oswald Buddenhagen napsal(a):
Quoted 7 lines · Vít Ondruch
Quoted 4 lines
$ git extract-filenames-from-patch 791.patch0001-Fix-Minitest-constant-name-in-tests.patch
git log --pretty=format:%fdoes that.
of course you need to apply the patch first for that to work
That (applying the patch) is the problem actually. I am looking for functionality like this in the context of Fedora packaging, where we work with source tarballs. Therefore I don't typically have an repository around.
And if I had a repository, I could possibly use git format-patch.
Vít
Quoted 4 lines · Vít Ondruch
Dne 31. 08. 23 v 13:04 Oswald Buddenhagen napsal(a):
Quoted 3 lines · Vít Ondruch
Quoted 2 lines
$ git extract-filenames-from-patch 791.patch
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
0001-Fix-Minitest-constant-name-in-tests.patch
git log --pretty=format:%fdoes that.
of course you need to apply the patch first for that to workThat (applying the patch) is the problem actually. I am looking for functionality like this in the context of Fedora packaging, where we work with source tarballs. Therefore I don't typically have an repository around.
well, you can just git init and add and commit the entire tree.
less inefficient would be creating a dummy repo, and cutting down the patch to be empty before applying it.
of course these are ludicrous suggestions in practice, and just replicating the cleanup function with a few regexes in a perl one-liner should do.
regards
Quoted 5 lines · Vít Ondruch
My typical use case is to download patches from GH, e.g.:
~~~
$ curl -OL https://github.com/rails/sprockets/pull/791.patch
...
Quoted 7 lines
The problem with this is that I end up with the "791.patch" file, while I'd like have a file with similar name as if I have used the git command:
~~~
Quoted 7 lines
$ git format-patch -1 6554b6d 0001-Fix-Minitest-constant-name-in-tests.patch~~~
So I wonder, is there a way to get such file name?
Do you mean: GitHub should let me run this command instead
$ curl -OL https://github.com/rails/sprockets/pull/0001-Fix-Minitest-constant-name-in-tests.patch
It may be nice for them to give a more meaningful name to their pull request (not just the output file name) than just an integer. But that is not a question/request we can answer here (this is not a help forum for GitHub users).
Something along the lines of
sed -ne '/^Subject: /{
s/^Subject: *\[PATCH[^]]*\] *//;
s/[^a-zA-Z0-9]/-/g;
s/--*/-/g;
s/$/\.patch/;
p;
q;
}' 791.patchshould be doable, but I am not sure what the benefit is. Once you get it in Git, you'd park it on a branch with a useful name and we can forget about "791", so the "The files we get from GitHub are named in a way that makes it hard to identify them" does not sound like a Git issue, at least to me.
Dne 31. 08. 23 v 19:19 Junio C Hamano napsal(a):
Quoted 21 lines
Quoted 5 lines · Vít Ondruch
My typical use case is to download patches from GH, e.g.:
~~~
$ curl -OL https://github.com/rails/sprockets/pull/791.patch...
Quoted 13 lines
The problem with this is that I end up with the "791.patch" file, while I'd like have a file with similar name as if I have used the git command:
~~~
$ git format-patch -1 6554b6d 0001-Fix-Minitest-constant-name-in-tests.patch~~~
Quoted 7 lines
Quoted 1 line
So I wonder, is there a way to get such file name?
Do you mean: GitHub should let me run this command instead
$ curl -OL https://github.com/rails/sprockets/pull/0001-Fix-Minitest-constant-name-in-tests.patchIt may be nice for them to give a more meaningful name to their pull request (not just the output file name) than just an integer.
This URL would be hard to compile and it could potentially lead me to the same question ;) But maybe in combination with -J option, GH (but also all other forges) could probably do something about it.
Quoted 3 lines
But
that is not a question/request we can answer here (this is not a help forum for GitHub users).
Right.
Quoted 12 lines
Something along the lines of
sed -ne '/^Subject: /{ s/^Subject: *\[PATCH[^]]*\] *//; s/[^a-zA-Z0-9]/-/g; s/--*/-/g; s/$/\.patch/; p; q; }' 791.patch
Quoted 1 line
should be doable, but I am not sure what the benefit is.
If this was provided as a some convenient script, then I would not need to know what the sanitation of the subject is done to get the file name. However, so far I was mostly fine with manually replacing spaces by dashes, so what is sanitized is not really that important to me. I just though that if Git has already some opinion about it and it is already implemented, it would be nice to reuse the functionality.
Quoted 2 lines
Once you
get it in Git,
But I typically don't have a Git.
Quoted 7 lines
you'd park it on a branch with a useful name and we
can forget about "791", so the "The files we get from GitHub are named in a way that makes it hard to identify them" does not sound like a Git issue, at least to me.
It does not seems I have convinced you (and that is fine). I'll probably look around. patchutils project could be probably good candidate for such script. I'll try to ask there. Maybe I'll also try to explore if GH would be open to provide some better filename, leveraging the -J curl option.
Thx for you help! Appreciate that.
Vít
Quoted 2 lines · Vít Ondruch
If this was provided as a some convenient script,
well, that's the key here: to be actually useful, it needs to be integrated into a highly specialized tool. the poor discoverability of that in conjunction with its somewhat minor utility is probably a sufficient argument for not going that route (beyond personal tool collections). github & co. providing a sensible content-disposition otoh is a (somewhat) well-known, generic solution to this problem.
but i wouldn't be opposed to for example git-mailinfo learning to understand the --pretty argument, if the implementation doesn't turn out to be completely out of proportion.
regards
Quoted 3 lines · Oswald Buddenhagen
but i wouldn't be opposed to for example git-mailinfo learning to understand the --pretty argument, if the implementation doesn't turn out to be completely out of proportion.
Excellent suggestion. I agree that 'mailinfo' would be the closest place we have for such a new feature. It's "info" output (i.e. what comes out to the standard output of the command) is designed to be extensible, and I vaguely recall that we indeed have added new field(s) during its lifetime with existing users already.
We can just invent a new label (e.g. "Filesystem-safe-subject:"), pass the subject string to pretty.c:format_sanitized_subject() and emit the result next to the existing "Subject:" with that label, and we can even do so unconditionally without breaking anybody.
Having it in 'mailinfo' may still not be a good solution to the issue, given that Vit says
Quoted 1 line
Quoted 1 line
But I typically don't have a Git.
though.
On a related not-so-distant tangent, we probably should redo the support for --message-id to emit it as an extra entry to the "info" output, instead of contaminating the "message" output. The option was added only to support "git am --message-id", and as long as the calling "am" and "mailinfo" are updated in sync to use the "info" output to carry the Message-Id: information, we should be able to do such a clean-up without changing the externally visible behaviour.
Quoted 4 lines · Junio C Hamano
We can just invent a new label (e.g. "Filesystem-safe-subject:"), pass the subject string to pretty.c:format_sanitized_subject() and emit the result next to the existing "Subject:" with that label, and we can even do so unconditionally without breaking anybody.
Such a change would be quite small and simple.
mailinfo.c | 6 ++++++ 1 file changed, 6 insertions(+)
mailinfo.c +6 -0
@@ -5,6 +5,7 @@#include "utf8.h" #include "strbuf.h" #include "mailinfo.h" +#include "pretty.h" static void cleanup_space (struct strbuf *sb) {
@@ -1166,11 +1167,16 @@ static void handle_info(struct mailinfo *mi) }if (!strcmp (header[i],"Subject" )) { +struct strbuf san =STRBUF_INIT ; +if (!mi->keep_subject) {cleanup_subject (mi, hdr);cleanup_space (hdr); }output_header_lines (mi->output,"Subject" , hdr); +format_sanitized_subject (&san, hdr->buf, hdr->len); +output_header_lines (mi->output,"Filesystem-Safe-Subject" , &san); +strbuf_release (&san); }else if (!strcmp (header[i],"From" )) {cleanup_space (hdr);handle_from (mi, hdr);
Dne 01. 09. 23 v 17:53 Junio C Hamano napsal(a):
Quoted 11 lines
Quoted 3 lines · Oswald Buddenhagen
but i wouldn't be opposed to for example git-mailinfo learning to understand the --pretty argument, if the implementation doesn't turn out to be completely out of proportion.
Excellent suggestion. I agree that 'mailinfo' would be the closest place we have for such a new feature. It's "info" output (i.e. what comes out to the standard output of the command) is designed to be extensible, and I vaguely recall that we indeed have added new field(s) during its lifetime with existing users already.
Quoted 4 lines
We can just invent a new label (e.g. "Filesystem-safe-subject:"), pass the subject string to pretty.c:format_sanitized_subject() and emit the result next to the existing "Subject:" with that label, and we can even do so unconditionally without breaking anybody.
'mailinfo' is new to me. I am not really sure what is the proper use of this tool, but it sound as a neat idea. I could certainly extract the string from the 'mailinfo' output and it works just above the .patch file, which is great.
Quoted 7 lines
Having it in 'mailinfo' may still not be a good solution to the issue, given that Vit says
Quoted 1 line
Quoted 1 line
But I typically don't have a Git.
though.
Well, this should have been "I typically don't have a Git *repository*", because I certainly have a Git. Otherwise I would not ask for this functionality.
Just to explain, I would need to expand the sources, which I typically don't do. I'd need initialize the repository and commit everything, apply the patch, etc. In that case, I'd rater keep manually changing the patch names :)
Vít