Question

is there any command to get email send to / from and date from the git patch file or do I have to read it from the parsing the patch file ?

Was it helpful?

Solution

The formail command (often distributed with procmail, I think) can be useful for extracting email headers:

formail -x Date < 0000-Some-commit.patch

One difference compared to ad-hoc parsing with e.g. sed or the short Perl script posted in VonC's answer is that it deals with line-wrapped header lines.

Subject: The line is so long that
  is has been wrapped.

This should be unusual for Date, From, and To lines but is common for Subject lines.

Another corner case that not even formail deals with is header fields encoded according to RFC 2047, which is necessary if the line contains anything but plain US-ASCII.

I suggest you use whatever email/MIME parsing library that's available for the language you use. Since you mention Python in the question title, here's a short Python example for reading a file created by git format-patch from stdin and printing some of its headers:

import email.header
import email.parser
import sys

def decode_header(s):
    return ' '.join(
        text.decode(charset) if charset else text
        for text, charset in email.header.decode_header(s))

message = email.parser.Parser().parse(sys.stdin)
print decode_header(message['From'])
print decode_header(message['Date'])
print decode_header(message['Subject'])

OTHER TIPS

Parsing should be involved, since this git apply wouldn't help:

git apply --summary

As you can see in any of the t/t4100/t-apply-*.expect files, there is no mention of date or emails.


That being said, since git format-patch produced Unix mailbox format, you can use tools lie mailutils to parse such a file in C.
Or (easier), with a perl script.

while (($line = <F>)) {

   # set variables in order
   chomp($line);
   if ($line =~ /^From /){
        $count++;
   }
   elsif ($line =~ /^Date:/){
        ($date_text,$date) = split(/:/,$line);
   }
   elsif ($line =~ /^From:/){
        ($from_text,$from) = split(/:/,$line);
   }
   elsif ($line =~ /^Subject:/){
        ($subject_text,$subject) = split(/:/,$line);
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top