threads / discuss / 60177

Is there a way to get the "format-patch" formatted file name?

Subject: Is there a way to get the "format-patch" formatted file name?

## tl;dr

10 messages between Aug 31, 2023 and Sep 1, 2023.

replies: 9people: 3as markdown or json

Vít Ondruch· Aug 31, 2023, 09:49 UTC · lore
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
Oswald Buddenhagen· Aug 31, 2023, 11:04 UTC · re: Vít Ondruch · lore

Re: Is there a way to get the "format-patch" formatted file name?

On Thu, Aug 31, 2023 at 11:49:10AM +0200, Vít Ondruch wrote:
>$ git extract-filenames-from-patch 791.patch
>
>0001-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.

>P.S.: Please keep me in the CC
>
that's this list's policy anyway.
regards
Vít Ondruch· Aug 31, 2023, 12:13 UTC · re: Oswald Buddenhagen · lore

Re: Is there a way to get the "format-patch" formatted file name?

Dne 31. 08. 23 v 13:04 Oswald Buddenhagen napsal(a):
Show 7 quoted lines
> On Thu, Aug 31, 2023 at 11:49:10AM +0200, Vít Ondruch wrote:
>> $ git extract-filenames-from-patch 791.patch
>>
>> 0001-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

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
Oswald Buddenhagen· Aug 31, 2023, 12:46 UTC · re: Vít Ondruch · lore

Re: Is there a way to get the "format-patch" formatted file name?

On Thu, Aug 31, 2023 at 02:13:30PM +0200, Vít Ondruch wrote:
Show 15 quoted lines
>Dne 31. 08. 23 v 13:04 Oswald Buddenhagen napsal(a):
>> On Thu, Aug 31, 2023 at 11:49:10AM +0200, Vít Ondruch wrote:
>>> $ git extract-filenames-from-patch 791.patch
>>>
>>> 0001-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
>
>
>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.
>

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
Junio C Hamano· Aug 31, 2023, 17:19 UTC · re: Vít Ondruch · lore

Re: Is there a way to get the "format-patch" formatted file name?

Vít Ondruch <vondruch@redhat.com> writes:
Show 5 quoted lines
> My typical use case is to download patches from GH, e.g.:
>
> ~~~
>
> $ curl -OL https://github.com/rails/sprockets/pull/791.patch
...
Show 14 quoted 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
>
> ~~~
>
>
> 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.patch

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

Vít Ondruch· Sep 1, 2023, 08:56 UTC · re: Junio C Hamano · lore

Re: Is there a way to get the "format-patch" formatted file name?

Dne 31. 08. 23 v 19:19 Junio C Hamano napsal(a):
Show 28 quoted lines
> Vít Ondruch <vondruch@redhat.com> writes:
>
>> My typical use case is to download patches from GH, e.g.:
>>
>> ~~~
>>
>> $ curl -OL https://github.com/rails/sprockets/pull/791.patch
> ...
>> 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?
> 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.

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.

> But
> that is not a question/request we can answer here (this is not a
> help forum for GitHub users).
Right.
Show 13 quoted 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
>
> 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.

>    Once you
> get it in Git,
But I typically don't have a Git.
Show 7 quoted 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
Oswald Buddenhagen· Sep 1, 2023, 11:00 UTC · re: Vít Ondruch · lore

Re: Is there a way to get the "format-patch" formatted file name?

On Fri, Sep 01, 2023 at 10:56:28AM +0200, Vít Ondruch wrote:
>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
Junio C Hamano· Sep 1, 2023, 15:53 UTC · re: Oswald Buddenhagen · lore

Re: Is there a way to get the "format-patch" formatted file name?

Oswald Buddenhagen <oswald.buddenhagen@gmx.de> writes:
> 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

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

Junio C Hamano· Sep 1, 2023, 16:04 UTC · re: Junio C Hamano · lore

Re: Is there a way to get the "format-patch" formatted file name?

Junio C Hamano <gitster@pobox.com> writes:
> 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(+)
diff --git c/mailinfo.c w/mailinfo.c
index 931505363c..b0eb646f4e 100644
--- c/mailinfo.c
+++ w/mailinfo.c
@@ -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);
Vít Ondruch· Sep 1, 2023, 16:37 UTC · re: Junio C Hamano · lore

Re: Is there a way to get the "format-patch" formatted file name?

Dne 01. 09. 23 v 17:53 Junio C Hamano napsal(a):
Show 15 quoted lines
> Oswald Buddenhagen <oswald.buddenhagen@gmx.de> writes:
>
>> 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.

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

Show 7 quoted lines
>
>
> Having it in 'mailinfo' may still not be a good solution to the
> issue, given that Vit says
>
>>> 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

← back to recent threads