<s> import operator <EOL> import subprocess <EOL> import datetime <EOL> from email . utils import parsedate_tz <EOL> def call ( seq ) : <EOL> if isinstance ( seq , basestring ) : <EOL> seq = seq . split ( ) <EOL> return subprocess . Popen ( seq , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> def _parse_branch_info ( output ) : <EOL> info = { } <EOL> commit_msg = None <EOL> for thing in output . splitlines ( ) : <EOL> if commit_msg is not None : <EOL> commit_msg += thing + '<STR_LIT:\n>' <EOL> continue <EOL> if thing . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> if thing == '<STR_LIT>' : <EOL> commit_msg = '<STR_LIT>' <EOL> continue <EOL> label , stuff = thing . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> info [ label . strip ( ) ] = stuff . strip ( ) <EOL> info [ '<STR_LIT>' ] = commit_msg <EOL> return info <EOL> def branch_info ( name ) : <EOL> output = call ( '<STR_LIT>' % name ) <EOL> info = _parse_branch_info ( output ) <EOL> info [ '<STR_LIT:name>' ] = name <EOL> info [ '<STR_LIT:date>' ] = info [ '<STR_LIT>' ] <EOL> info [ '<STR_LIT>' ] = parsedate_tz ( info [ '<STR_LIT:date>' ] ) <EOL> info [ '<STR_LIT>' ] = datetime . datetime ( * info [ '<STR_LIT>' ] [ : <NUM_LIT:7> ] ) <EOL> age = datetime . datetime . now ( ) - info [ '<STR_LIT>' ] <EOL> info [ '<STR_LIT>' ] = age <EOL> return info <EOL> def find ( search ) : <EOL> refs = call ( '<STR_LIT>' ) <EOL> branches = [ ] <EOL> for ref in refs . splitlines ( ) : <EOL> try : <EOL> branch_name = ref . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> continue <EOL> if search and search . lower ( ) not in branch_name . lower ( ) : <EOL> continue <EOL> branches . append ( branch_info ( branch_name ) ) <EOL> return branches <EOL> def get_merged_branches ( ) : <EOL> merged = [ ] <EOL> for line in call ( '<STR_LIT>' ) . splitlines ( ) : <EOL> line = line . strip ( ) <EOL> if not line . startswith ( '<STR_LIT:*>' ) : <EOL> merged . append ( line ) <EOL> return merged <EOL> def checkout ( branch_name ) : <EOL> call ( '<STR_LIT>' % branch_name ) <EOL> def print_list ( branches , merged ) : <EOL> for each in sorted ( branches , key = operator . itemgetter ( '<STR_LIT>' ) ) : <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> is_merged = '<STR_LIT>' <EOL> if each [ '<STR_LIT:name>' ] in merged and each [ '<STR_LIT:name>' ] != '<STR_LIT>' : <EOL> is_merged = "<STR_LIT>" <EOL> print each [ '<STR_LIT:name>' ] , is_merged <EOL> print "<STR_LIT:\t>" , each [ '<STR_LIT:date>' ] <EOL> print "<STR_LIT:\t>" , each [ '<STR_LIT>' ] , "<STR_LIT>" <EOL> print "<STR_LIT:\t>" , each [ '<STR_LIT>' ] . strip ( ) <EOL> print <EOL> def run ( searchstring ) : <EOL> branches_ = find ( searchstring ) <EOL> if branches_ : <EOL> merged = get_merged_branches ( ) <EOL> print "<STR_LIT>" <EOL> print_list ( branches_ , merged ) <EOL> if len ( branches_ ) == <NUM_LIT:1> : <EOL> branch_name = branches_ [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] <EOL> if len ( branch_name ) > <NUM_LIT:50> : <EOL> branch_name = branch_name [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> check_it_out = raw_input ( <EOL> "<STR_LIT>" % branch_name <EOL> ) <EOL> if check_it_out . lower ( ) . strip ( ) != '<STR_LIT:n>' : <EOL> checkout ( branches_ [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] ) <EOL> elif searchstring : <EOL> print "<STR_LIT>" % searchstring <EOL> else : <EOL> print "<STR_LIT>" </s>
