mercurial/cmdutil.py
branchstable
changeset 24845 8133494accf1
parent 24843 21b33f0460e0
child 24857 d8505bfe4825
equal deleted inserted replaced
24844:6c6aee6d395b 24845:8133494accf1
    57     return newchunks
    57     return newchunks
    58 
    58 
    59 def dorecord(ui, repo, commitfunc, cmdsuggest, backupall,
    59 def dorecord(ui, repo, commitfunc, cmdsuggest, backupall,
    60             filterfn, *pats, **opts):
    60             filterfn, *pats, **opts):
    61     import merge as mergemod
    61     import merge as mergemod
       
    62     hunkclasses = (crecordmod.uihunk, patch.recordhunk)
       
    63     ishunk = lambda x: isinstance(x, hunkclasses)
    62 
    64 
    63     if not ui.interactive():
    65     if not ui.interactive():
    64         raise util.Abort(_('running non-interactively, use %s instead') %
    66         raise util.Abort(_('running non-interactively, use %s instead') %
    65                          cmdsuggest)
    67                          cmdsuggest)
    66 
    68 
   100         try:
   102         try:
   101             chunks = filterfn(ui, originalchunks)
   103             chunks = filterfn(ui, originalchunks)
   102         except patch.PatchError, err:
   104         except patch.PatchError, err:
   103             raise util.Abort(_('error parsing patch: %s') % err)
   105             raise util.Abort(_('error parsing patch: %s') % err)
   104 
   106 
       
   107         # We need to keep a backup of files that have been newly added and
       
   108         # modified during the recording process because there is a previous
       
   109         # version without the edit in the workdir
       
   110         newlyaddedandmodifiedfiles = set()
       
   111         for chunk in chunks:
       
   112             if ishunk(chunk) and chunk.header.isnewfile() and chunk not in \
       
   113                 originalchunks:
       
   114                 newlyaddedandmodifiedfiles.add(chunk.header.filename())
   105         contenders = set()
   115         contenders = set()
   106         for h in chunks:
   116         for h in chunks:
   107             try:
   117             try:
   108                 contenders.update(set(h.files()))
   118                 contenders.update(set(h.files()))
   109             except AttributeError:
   119             except AttributeError:
   120         # 2. backup changed files, so we can restore them in the end
   130         # 2. backup changed files, so we can restore them in the end
   121 
   131 
   122         if backupall:
   132         if backupall:
   123             tobackup = changed
   133             tobackup = changed
   124         else:
   134         else:
   125             tobackup = [f for f in newfiles if f in modified]
   135             tobackup = [f for f in newfiles if f in modified or f in \
   126 
   136                     newlyaddedandmodifiedfiles]
   127         backups = {}
   137         backups = {}
   128         if tobackup:
   138         if tobackup:
   129             backupdir = repo.join('record-backups')
   139             backupdir = repo.join('record-backups')
   130             try:
   140             try:
   131                 os.mkdir(backupdir)
   141                 os.mkdir(backupdir)
   149                 if fname in backups:
   159                 if fname in backups:
   150                     c.write(fp)
   160                     c.write(fp)
   151             dopatch = fp.tell()
   161             dopatch = fp.tell()
   152             fp.seek(0)
   162             fp.seek(0)
   153 
   163 
       
   164             [os.unlink(c) for c in newlyaddedandmodifiedfiles]
   154             # 3a. apply filtered patch to clean repo  (clean)
   165             # 3a. apply filtered patch to clean repo  (clean)
   155             if backups:
   166             if backups:
   156                 # Equivalent to hg.revert
   167                 # Equivalent to hg.revert
   157                 choices = lambda key: key in backups
   168                 choices = lambda key: key in backups
   158                 mergemod.update(repo, repo.dirstate.p1(),
   169                 mergemod.update(repo, repo.dirstate.p1(),