def format_patchsets(to_export, revs, output=u'patches'):
 outs = []
 errs = []
 cur_rev = to_export[0]
 cur_master_idx = revs.index(cur_rev)
 patches_to_export = [[cur_rev]]
 patchset_idx = 0
 for (idx, rev) in enumerate(to_export[1:]):
  try:
   master_idx = revs[cur_master_idx:].index(rev)
   master_idx += cur_master_idx
  except ValueError:
   master_idx = revs.index(rev)
  if (master_idx == (cur_master_idx + 1)):
   patches_to_export[patchset_idx].append(rev)
   cur_master_idx += 1
   continue
  else:
   patches_to_export.append([rev])
   cur_master_idx = master_idx
   patchset_idx += 1
 status = 0
 for patchset in patches_to_export:
  (stat, out, err) = export_patchset(patchset[0], patchset[(-1)], output=u'patches', n=(len(patchset) > 1), thread=True, patch_with_stat=True)
  outs.append(out)
  if err:
   errs.append(err)
  status = max(stat, status)
 return (status, u'\n'.join(outs), u'\n'.join(errs))

def format_patchsets ( to_export , revs , output = u'patches' ) : outs = [ ] errs = [ ] cur_rev = to_export [ 0 ] cur_master_idx = revs . index ( cur_rev ) patches_to_export = [ [ cur_rev ] ] patchset_idx = 0 for ( idx , rev ) in enumerate ( to_export [ 1 : ] ) : try : master_idx = revs [ cur_master_idx : ] . index ( rev ) master_idx += cur_master_idx except ValueError : master_idx = revs . index ( rev ) if ( master_idx == ( cur_master_idx + 1 ) ) : patches_to_export [ patchset_idx ] . append ( rev ) cur_master_idx += 1 continue else : patches_to_export . append ( [ rev ] ) cur_master_idx = master_idx patchset_idx += 1 status = 0 for patchset in patches_to_export : ( stat , out , err ) = export_patchset ( patchset [ 0 ] , patchset [ ( - 1 ) ] , output = u'patches' , n = ( len ( patchset ) > 1 ) , thread = True , patch_with_stat = True ) outs . append ( out ) if err : errs . append ( err ) status = max ( stat , status ) return ( status , u'\n' . join ( outs ) , u'\n' . join ( errs ) )
group contiguous revision selection into patchsets exists to handle multi-selection .