2014-01-25 45 views

回答

0

的FORMAIL命令(往往分布与procmail的,我认为)可能是用于提取电子邮件标题有用:相比特设有例如解析

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

一个区别sed或VonC答案中发布的短Perl脚本是它处理换行包头的行。

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

这对于日期,发件人和收件人行应该是不寻常的,但对于主题行是很常见的。

即使不是formail处理的另一个角落案例是根据RFC 2047进行编码的头字段,如果该行包含任何内容但不含纯US-ASCII,则这是必需的。

我建议您使用任何可用于您使用的语言的电子邮件/ MIME解析库。由于您在问题标题中提到了Python,下面是一个简短的Python示例,用于读取由标准输入由git format-patch创建的文件并打印它的一些标题:

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']) 
0

解析应该参与,因为这git apply无助:

git apply --summary 

正如你可以在任何t/t4100/t-apply-*.expect文件看,有没有日期或电子邮件的提。


话虽这么说,因为git format-patch产生的Unix邮箱格式,你可以使用工具躺在mailutilsparse such a file在C.
或(容易),用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); 
    }