<s> """<STR_LIT>""" <EOL> from difflib import SequenceMatcher <EOL> import rfc822 <EOL> import subprocess <EOL> import time <EOL> from lib . dulwich . objects import ( <EOL> Blob , <EOL> Commit , <EOL> ) <EOL> def write_commit_patch ( f , commit , contents , progress , version = None ) : <EOL> """<STR_LIT>""" <EOL> ( num , total ) = progress <EOL> f . write ( "<STR_LIT>" % ( commit . id , time . ctime ( commit . commit_time ) ) ) <EOL> f . write ( "<STR_LIT>" % commit . author ) <EOL> f . write ( "<STR_LIT>" % time . strftime ( "<STR_LIT>" ) ) <EOL> f . write ( "<STR_LIT>" % ( num , total , commit . message ) ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> try : <EOL> p = subprocess . Popen ( [ "<STR_LIT>" ] , stdout = subprocess . PIPE , <EOL> stdin = subprocess . PIPE ) <EOL> except OSError , e : <EOL> pass <EOL> else : <EOL> ( diffstat , _ ) = p . communicate ( contents ) <EOL> f . write ( diffstat ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> f . write ( contents ) <EOL> f . write ( "<STR_LIT>" ) <EOL> if version is None : <EOL> from dulwich import __version__ as dulwich_version <EOL> f . write ( "<STR_LIT>" % dulwich_version ) <EOL> else : <EOL> f . write ( "<STR_LIT>" % version ) <EOL> def get_summary ( commit ) : <EOL> """<STR_LIT>""" <EOL> return commit . message . splitlines ( ) [ <NUM_LIT:0> ] . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:->" ) <EOL> def unified_diff ( a , b , fromfile = '<STR_LIT>' , tofile = '<STR_LIT>' , n = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> started = False <EOL> for group in SequenceMatcher ( None , a , b ) . get_grouped_opcodes ( n ) : <EOL> if not started : <EOL> yield '<STR_LIT>' % fromfile <EOL> yield '<STR_LIT>' % tofile <EOL> started = True <EOL> i1 , i2 , j1 , j2 = group [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , group [ - <NUM_LIT:1> ] [ <NUM_LIT:2> ] , group [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] , group [ - <NUM_LIT:1> ] [ <NUM_LIT:4> ] <EOL> yield "<STR_LIT>" % ( i1 + <NUM_LIT:1> , i2 - i1 , j1 + <NUM_LIT:1> , j2 - j1 ) <EOL> for tag , i1 , i2 , j1 , j2 in group : <EOL> if tag == '<STR_LIT>' : <EOL> for line in a [ i1 : i2 ] : <EOL> yield '<STR_LIT:U+0020>' + line <EOL> continue <EOL> if tag == '<STR_LIT:replace>' or tag == '<STR_LIT>' : <EOL> for line in a [ i1 : i2 ] : <EOL> if not line [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> line += '<STR_LIT>' <EOL> yield '<STR_LIT:->' + line <EOL> if tag == '<STR_LIT:replace>' or tag == '<STR_LIT>' : <EOL> for line in b [ j1 : j2 ] : <EOL> if not line [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> line += '<STR_LIT>' <EOL> yield '<STR_LIT:+>' + line <EOL> def write_blob_diff ( f , ( old_path , old_mode , old_blob ) , <EOL> ( new_path , new_mode , new_blob ) ) : <EOL> """<STR_LIT>""" <EOL> def blob_id ( blob ) : <EOL> if blob is None : <EOL> return "<STR_LIT:0>" * <NUM_LIT:7> <EOL> else : <EOL> return blob . id [ : <NUM_LIT:7> ] <EOL> def lines ( blob ) : <EOL> if blob is not None : <EOL> return blob . data . splitlines ( True ) <EOL> else : <EOL> return [ ] <EOL> if old_path is None : <EOL> old_path = "<STR_LIT>" <EOL> else : <EOL> old_path = "<STR_LIT>" % old_path <EOL> if new_path is None : <EOL> new_path = "<STR_LIT>" <EOL> else : <EOL> new_path = "<STR_LIT>" % new_path <EOL> f . write ( "<STR_LIT>" % ( old_path , new_path ) ) <EOL> if old_mode != new_mode : <EOL> if new_mode is not None : <EOL> if old_mode is not None : <EOL> f . write ( "<STR_LIT>" % old_mode ) <EOL> f . write ( "<STR_LIT>" % new_mode ) <EOL> else : <EOL> f . write ( "<STR_LIT>" % old_mode ) <EOL> f . write ( "<STR_LIT>" % ( blob_id ( old_blob ) , blob_id ( new_blob ) ) ) <EOL> if new_mode is not None : <EOL> f . write ( "<STR_LIT>" % new_mode ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> old_contents = lines ( old_blob ) <EOL> new_contents = lines ( new_blob ) <EOL> f . writelines ( unified_diff ( old_contents , new_contents , <EOL> old_path , new_path ) ) <EOL> def write_tree_diff ( f , store , old_tree , new_tree ) : <EOL> """<STR_LIT>""" <EOL> changes = store . tree_changes ( old_tree , new_tree ) <EOL> for ( oldpath , newpath ) , ( oldmode , newmode ) , ( oldsha , newsha ) in changes : <EOL> if oldsha is None : <EOL> old_blob = Blob . from_string ( "<STR_LIT>" ) <EOL> else : <EOL> old_blob = store [ oldsha ] <EOL> if newsha is None : <EOL> new_blob = Blob . from_string ( "<STR_LIT>" ) <EOL> else : <EOL> new_blob = store [ newsha ] <EOL> write_blob_diff ( f , ( oldpath , oldmode , old_blob ) , <EOL> ( newpath , newmode , new_blob ) ) <EOL> def git_am_patch_split ( f ) : <EOL> """<STR_LIT>""" <EOL> msg = rfc822 . Message ( f ) <EOL> c = Commit ( ) <EOL> c . author = msg [ "<STR_LIT>" ] <EOL> c . committer = msg [ "<STR_LIT>" ] <EOL> try : <EOL> patch_tag_start = msg [ "<STR_LIT>" ] . index ( "<STR_LIT>" ) <EOL> except ValueError : <EOL> subject = msg [ "<STR_LIT>" ] <EOL> else : <EOL> close = msg [ "<STR_LIT>" ] . index ( "<STR_LIT>" , patch_tag_start ) <EOL> subject = msg [ "<STR_LIT>" ] [ close + <NUM_LIT:2> : ] <EOL> c . message = subject . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) + "<STR_LIT:\n>" <EOL> first = True <EOL> for l in f : <EOL> if l == "<STR_LIT>" : <EOL> break <EOL> if first : <EOL> if l . startswith ( "<STR_LIT>" ) : <EOL> c . author = l [ len ( "<STR_LIT>" ) : ] . rstrip ( ) <EOL> else : <EOL> c . message += "<STR_LIT:\n>" + l <EOL> first = False <EOL> else : <EOL> c . message += l <EOL> diff = "<STR_LIT>" <EOL> for l in f : <EOL> if l == "<STR_LIT>" : <EOL> break <EOL> diff += l <EOL> try : <EOL> version = f . next ( ) . rstrip ( "<STR_LIT:\n>" ) <EOL> except StopIteration : <EOL> version = None <EOL> return c , diff , version </s>
