Attachment 'qup.py'

Download

   1 # qup.py - move MQ patches to top of unapplied part of series
   2 #
   3 # Copyright 2008 Andrei Vermel <andrei.vermel@gmail.com>
   4 #
   5 # This software may be used and distributed according to the terms
   6 # of the GNU General Public License, incorporated herein by reference.
   7 
   8 from mercurial.i18n import _
   9 from mercurial.node import *
  10 from mercurial import commands, cmdutil, hg, node, util
  11 import os, tempfile
  12 
  13 def qup(ui, repo, *patches, **opts):
  14     '''move MQ patches to top of unapplied part of series.
  15 
  16 Patches can be specified by unambiguous start substrings or indices.'''
  17 
  18     try:
  19         fh = open(os.path.join(repo.path, 'patches.queue'))
  20         queue = fh.read().rstrip()
  21         if not queue:
  22             qpath = os.path.join(repo.path, 'patches')
  23         else:
  24             qpath = os.path.join(repo.path, 'patches-' + queue)
  25     except IOError:
  26         qpath = os.path.join(repo.path, 'patches')
  27 
  28     try:
  29         series = file(os.path.join(qpath, 'series'), 'r')
  30     except:
  31         raise util.Abort(_('patch queue not found'))
  32 
  33     q = repo.mq
  34 
  35     if not patches:
  36         raise util.Abort(_('no patches specified'))
  37 
  38     old_num_patches = len(patches)
  39     patches = [q.lookup(patch) for patch in patches]
  40     patches_dict = dict.fromkeys(patches)
  41     if len(patches_dict.keys()) != old_num_patches:
  42         raise util.Abort(_(('same patch specified multiple times, %s') % patches))
  43 
  44     series_dict = dict.fromkeys(q.series)
  45     for p in patches:
  46         if p not in series_dict:    
  47             raise util.Abort(_(('patch %s is not in series') % p))
  48    
  49     applied=[a.name for a in q.applied]
  50     for a in applied:
  51         if a in patches_dict:
  52             raise util.Abort(_(('patch %s is applied already, can\'t move') % a))
  53 
  54     pending_applied = False
  55     if applied:
  56         top_applied = applied[-1]
  57         pending_applied = True
  58    
  59     tokeep = [] 
  60     tofront = {}
  61     toback = []
  62     for line in series:
  63         if line.strip().startswith('#'):
  64             toback.append(line)
  65         else:
  66             spl = line.split('#')
  67             firstword=''
  68             if spl:
  69                 firstword = spl[0].strip()
  70             if pending_applied:
  71                 tokeep.append(line)
  72                 if firstword == top_applied:
  73                     pending_applied = False
  74             else:
  75                 if firstword in patches_dict:
  76                     tofront[firstword]=line
  77                 else:
  78                     toback.append(line)
  79 
  80     if pending_applied:
  81         raise util.Abort(_('top applied patch not found in the series?'))
  82     if not tofront:
  83         raise util.Abort(_('none of specified patches found in the series'))
  84 
  85     outfd, outname = tempfile.mkstemp(dir=repo.path+'/patches')
  86     outfile = os.fdopen(outfd, 'w')
  87     outfile.writelines(tokeep)
  88     for patch in patches:
  89         outfile.write(tofront[patch].rstrip('\n')+'\n')
  90     outfile.writelines(toback)
  91     outfile.close()
  92     series.close()
  93     os.remove(series.name)
  94     os.rename(outname, series.name)
  95 
  96 cmdtable = {
  97     'qup':
  98         (qup, [],
  99         _('hg qup PATCH...')),
 100 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2010-08-16 08:14:12, 3.0 KB) [[attachment:qup.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.