override previous merge with mpm.
authorVadim Gelfer <vadim.gelfer@gmail.com>
Wed, 16 Aug 2006 10:52:42 -0700
changeset 2922 773c5b82d052
parent 2921 addb58e3b41c (diff)
parent 2920 ef8ee4477019 (current diff)
child 2923 cd47230a4eb9
override previous merge with mpm. merge at ef8ee4477019 was bad.
hgext/mq.py
mercurial/commands.py
mercurial/patch.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -400,39 +400,15 @@ class queue:
         '''Apply patchfile  to the working directory.
         patchfile: file name of patch'''
         try:
-            pp = util.find_in_path('gpatch', os.environ.get('PATH', ''), 'patch')
-            f = os.popen("%s -d %s -p1 --no-backup-if-mismatch < %s" %
-                         (pp, util.shellquote(repo.root), util.shellquote(patchfile)))
-        except:
-            self.ui.warn("patch failed, unable to continue (try -v)\n")
-            return (None, [], False)
-        files = []
-        fuzz = False
-        for l in f:
-            l = l.rstrip('\r\n');
-            if self.ui.verbose:
-                self.ui.warn(l + "\n")
-            if l[:14] == 'patching file ':
-                pf = os.path.normpath(util.parse_patch_output(l))
-                if pf not in files:
-                    files.append(pf)
-                printed_file = False
-                file_str = l
-            elif l.find('with fuzz') >= 0:
-                if not printed_file:
-                    self.ui.warn(file_str + '\n')
-                    printed_file = True
-                self.ui.warn(l + '\n')
-                fuzz = True
-            elif l.find('saving rejects to file') >= 0:
-                self.ui.warn(l + '\n')
-            elif l.find('FAILED') >= 0:
-                if not printed_file:
-                    self.ui.warn(file_str + '\n')
-                    printed_file = True
-                self.ui.warn(l + '\n')
+            (files, fuzz) = patch.patch(patchfile, self.ui, strip=1,
+                                        cwd=repo.root)
+        except Exception, inst:
+            self.ui.note(str(inst) + '\n')
+            if not self.ui.verbose:
+                self.ui.warn("patch failed, unable to continue (try -v)\n")
+            return (False, [], False)
 
-        return (not f.close(), files, fuzz)
+        return (True, files.keys(), fuzz)
 
     def apply(self, repo, series, list=False, update_status=True,
               strict=False, patchdir=None, merge=None, wlock=None):
@@ -506,21 +482,28 @@ class queue:
         tr.close()
         return (err, n)
 
-    def delete(self, repo, patch, force=False):
-        patch = self.lookup(patch, strict=True)
-        info = self.isapplied(patch)
-        if info:
-            raise util.Abort(_("cannot delete applied patch %s") % patch)
-        if patch not in self.series:
-            raise util.Abort(_("patch %s not in series file") % patch)
-        if force:
+    def delete(self, repo, patches, keep=False):
+        realpatches = []
+        for patch in patches:
+            patch = self.lookup(patch, strict=True)
+            info = self.isapplied(patch)
+            if info:
+                raise util.Abort(_("cannot delete applied patch %s") % patch)
+            if patch not in self.series:
+                raise util.Abort(_("patch %s not in series file") % patch)
+            realpatches.append(patch)
+
+        if not keep:
             r = self.qrepo()
             if r:
-                r.remove([patch], True)
+                r.remove(realpatches, True)
             else:
                 os.unlink(self.join(patch))
-        i = self.find_series(patch)
-        del self.full_series[i]
+
+        indices = [self.find_series(p) for p in realpatches]
+        indices.sort()
+        for i in indices[-1::-1]:
+            del self.full_series[i]
         self.parse_series()
         self.series_dirty = 1
 
@@ -1300,13 +1283,13 @@ class queue:
         if qrepo:
             qrepo.add(added)
 
-def delete(ui, repo, patch, **opts):
-    """remove a patch from the series file
+def delete(ui, repo, patch, *patches, **opts):
+    """remove patches from queue
 
-    The patch must not be applied.
-    With -f, deletes the patch file as well as the series entry."""
+    The patches must not be applied.
+    With -k, the patch files are preserved in the patch directory."""
     q = repo.mq
-    q.delete(repo, patch, force=opts.get('force'))
+    q.delete(repo, (patch,) + patches, keep=opts.get('keep'))
     q.save_dirty()
     return 0
 
@@ -1464,7 +1447,7 @@ def fold(ui, repo, *files, **opts):
     applied to the current patch in the order given. If all the
     patches apply successfully, the current patch will be refreshed
     with the new cumulative patch, and the folded patches will
-    be deleted. With -f/--force, the folded patch files will
+    be deleted. With -k/--keep, the folded patch files will not
     be removed afterwards.
 
     The header for each folded patch will be concatenated with
@@ -1514,7 +1497,7 @@ def fold(ui, repo, *files, **opts):
     q.refresh(repo, msg=message)
 
     for patch in patches:
-        q.delete(repo, patch, force=opts['force'])
+        q.delete(repo, patch, keep=opts['keep'])
 
     q.save_dirty()
 
@@ -1903,14 +1886,14 @@ cmdtable = {
          commands.table["^commit|ci"][1],
          'hg qcommit [OPTION]... [FILE]...'),
     "^qdiff": (diff, [], 'hg qdiff [FILE]...'),
-    "qdelete":
+    "qdelete|qremove|qrm":
         (delete,
-         [('f', 'force', None, _('delete patch file'))],
-          'hg qdelete [-f] PATCH'),
+         [('k', 'keep', None, _('keep patch file'))],
+          'hg qdelete [-k] PATCH'),
     'qfold':
         (fold,
          [('e', 'edit', None, _('edit patch header')),
-          ('f', 'force', None, _('delete folded patch files')),
+          ('k', 'keep', None, _('keep folded patch files')),
           ('m', 'message', '', _('set patch header to <text>')),
           ('l', 'logfile', '', _('set patch header to contents of <file>'))],
          'hg qfold [-e] [-m <text>] [-l <file] PATCH...'),
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -183,49 +183,59 @@ def walkchangerevs(ui, repo, pats, opts)
                 fncache[rev] = matches
                 wanted[rev] = 1
 
-    def iterate():
-        class followfilter:
-            def __init__(self, onlyfirst=False):
-                self.startrev = -1
-                self.roots = []
-                self.onlyfirst = onlyfirst
-
-            def match(self, rev):
-                def realparents(rev):
-                    if self.onlyfirst:
-                        return repo.changelog.parentrevs(rev)[0:1]
-                    else:
-                        return filter(lambda x: x != -1, repo.changelog.parentrevs(rev))
-
-                if self.startrev == -1:
-                    self.startrev = rev
+    class followfilter:
+        def __init__(self, onlyfirst=False):
+            self.startrev = -1
+            self.roots = []
+            self.onlyfirst = onlyfirst
+
+        def match(self, rev):
+            def realparents(rev):
+                if self.onlyfirst:
+                    return repo.changelog.parentrevs(rev)[0:1]
+                else:
+                    return filter(lambda x: x != -1, repo.changelog.parentrevs(rev))
+
+            if self.startrev == -1:
+                self.startrev = rev
+                return True
+
+            if rev > self.startrev:
+                # forward: all descendants
+                if not self.roots:
+                    self.roots.append(self.startrev)
+                for parent in realparents(rev):
+                    if parent in self.roots:
+                        self.roots.append(rev)
+                        return True
+            else:
+                # backwards: all parents
+                if not self.roots:
+                    self.roots.extend(realparents(self.startrev))
+                if rev in self.roots:
+                    self.roots.remove(rev)
+                    self.roots.extend(realparents(rev))
                     return True
 
-                if rev > self.startrev:
-                    # forward: all descendants
-                    if not self.roots:
-                        self.roots.append(self.startrev)
-                    for parent in realparents(rev):
-                        if parent in self.roots:
-                            self.roots.append(rev)
-                            return True
-                else:
-                    # backwards: all parents
-                    if not self.roots:
-                        self.roots.extend(realparents(self.startrev))
-                    if rev in self.roots:
-                        self.roots.remove(rev)
-                        self.roots.extend(realparents(rev))
-                        return True
-
-                return False
-
+            return False
+
+    # it might be worthwhile to do this in the iterator if the rev range
+    # is descending and the prune args are all within that range
+    for rev in opts.get('prune', ()):
+        rev = repo.changelog.rev(repo.lookup(rev))
+        ff = followfilter()
+        stop = min(revs[0], revs[-1])
+        for x in range(rev, stop-1, -1):
+            if ff.match(x) and wanted.has_key(x):
+                del wanted[x]
+
+    def iterate():
         if follow and not files:
             ff = followfilter(onlyfirst=opts.get('follow_first'))
             def want(rev):
-                if rev not in wanted:
-                    return False
-                return ff.match(rev)
+                if ff.match(rev) and rev in wanted:
+                    return True
+                return False
         else:
             def want(rev):
                 return rev in wanted
@@ -1672,7 +1682,7 @@ def import_(ui, repo, patch1, *patches, 
                 message = None
             ui.debug(_('message:\n%s\n') % message)
 
-            files = patch.patch(strip, tmpname, ui, cwd=repo.root)
+            files, fuzz = patch.patch(tmpname, ui, strip=strip, cwd=repo.root)
             removes = []
             if len(files) > 0:
                 cfiles = files.keys()
@@ -1960,9 +1970,29 @@ def merge(ui, repo, node=None, force=Non
     requested revision. Files that changed between either parent are
     marked as changed for the next commit and a commit must be
     performed before any further updates are allowed.
+
+    If no revision is specified, the working directory's parent is a
+    head revision, and the repository contains exactly one other head,
+    the other head is merged with by default.  Otherwise, an explicit
+    revision to merge with must be provided.
     """
 
-    node = _lookup(repo, node, branch)
+    if node:
+        node = _lookup(repo, node, branch)
+    else:
+        heads = repo.heads()
+        if len(heads) > 2:
+            raise util.Abort(_('repo has %d heads - '
+                               'please merge with an explicit rev') %
+                             len(heads))
+        if len(heads) == 1:
+            raise util.Abort(_('there is nothing to merge - '
+                               'use "hg update" instead'))
+        parent = repo.dirstate.parents()[0]
+        if parent not in heads:
+            raise util.Abort(_('working dir not at a head rev - '
+                               'use "hg update" or merge with an explicit rev'))
+        node = parent == heads[0] and heads[-1] or heads[0]
     return hg.merge(repo, node, force=force)
 
 def outgoing(ui, repo, dest=None, **opts):
@@ -2868,6 +2898,7 @@ table = {
           ('a', 'text', None, _('treat all files as text')),
           ('p', 'show-function', None,
            _('show which function each change is in')),
+          ('g', 'git', None, _('use git extended diff format')),
           ('w', 'ignore-all-space', None,
            _('ignore white space when comparing lines')),
           ('b', 'ignore-space-change', None,
@@ -2967,6 +2998,7 @@ table = {
           ('', 'style', '', _('display using template map file')),
           ('m', 'only-merges', None, _('show only merges')),
           ('p', 'patch', None, _('show patch')),
+          ('P', 'prune', [], _('do not display revision or any of its ancestors')),
           ('', 'template', '', _('display with template')),
           ('I', 'include', [], _('include names matching the given patterns')),
           ('X', 'exclude', [], _('exclude names matching the given patterns'))],
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -215,13 +215,14 @@ def dogitpatch(patchname, gitpatches):
     tmpfp.close()
     return patchname
 
-def patch(strip, patchname, ui, cwd=None):
+def patch(patchname, ui, strip=1, cwd=None):
     """apply the patch <patchname> to the working directory.
     a list of patched files is returned"""
 
     (dopatch, gitpatches) = readgitpatch(patchname)
 
     files = {}
+    fuzz = False
     if dopatch:
         if dopatch == 'filter':
             patchname = dogitpatch(patchname, gitpatches)
@@ -237,10 +238,25 @@ def patch(strip, patchname, ui, cwd=None
 
         for line in fp:
             line = line.rstrip()
-            ui.status("%s\n" % line)
+            ui.note(line + '\n')
             if line.startswith('patching file '):
                 pf = util.parse_patch_output(line)
+                printed_file = False
                 files.setdefault(pf, (None, None))
+            elif line.find('with fuzz') >= 0:
+                fuzz = True
+                if not printed_file:
+                    ui.warn(pf + '\n')
+                    printed_file = True
+                ui.warn(line + '\n')
+            elif line.find('saving rejects to file') >= 0:
+                ui.warn(line + '\n')
+            elif line.find('FAILED') >= 0:
+                if not printed_file:
+                    ui.warn(pf + '\n')
+                    printed_file = True
+                ui.warn(line + '\n')
+            
         code = fp.close()
         if code:
             raise util.Abort(_("patch command failed: %s") %
@@ -249,11 +265,13 @@ def patch(strip, patchname, ui, cwd=None
     for gp in gitpatches:
         files[gp.path] = (gp.op, gp)
 
-    return files
+    return (files, fuzz)
 
 def diffopts(ui, opts={}):
     return mdiff.diffopts(
         text=opts.get('text'),
+        git=(opts.get('git') or
+                  ui.configbool('diff', 'git', None)),
         showfunc=(opts.get('show_function') or
                   ui.configbool('diff', 'showfunc', None)),
         ignorews=(opts.get('ignore_all_space') or
@@ -310,6 +328,9 @@ def diff(repo, node1=None, node2=None, f
             return _date2
         def read(f):
             return repo.file(f).read(mmap2[f])
+        def renamed(f):
+            src = repo.file(f).renamed(mmap2[f])
+            return src and src[0] or None
     else:
         tz = util.makedate()[1]
         _date2 = util.datestr()
@@ -321,6 +342,8 @@ def diff(repo, node1=None, node2=None, f
                 return _date2
         def read(f):
             return repo.wread(f)
+        def renamed(f):
+            return repo.dirstate.copies.get(f)
 
     if repo.ui.quiet:
         r = None
@@ -328,16 +351,65 @@ def diff(repo, node1=None, node2=None, f
         hexfunc = repo.ui.verbose and hex or short
         r = [hexfunc(node) for node in [node1, node2] if node]
 
+    if opts.git:
+        copied = {}
+        for f in added:
+            src = renamed(f)
+            if src:
+                copied[f] = src
+        srcs = [x[1] for x in copied.items()]
+
     all = modified + added + removed
     all.sort()
     for f in all:
         to = None
         tn = None
+        dodiff = True
         if f in mmap:
             to = repo.file(f).read(mmap[f])
         if f not in removed:
             tn = read(f)
-        fp.write(mdiff.unidiff(to, date1, tn, date2(f), f, r, opts=opts))
+        if opts.git:
+            def gitmode(x):
+                return x and '100755' or '100644'
+            def addmodehdr(header, omode, nmode):
+                if omode != nmode:
+                    header.append('old mode %s\n' % omode)
+                    header.append('new mode %s\n' % nmode)
+
+            a, b = f, f
+            header = []
+            if f in added:
+                if node2:
+                    mode = gitmode(mmap2.execf(f))
+                else:
+                    mode = gitmode(util.is_exec(repo.wjoin(f), None))
+                if f in copied:
+                    a = copied[f]
+                    omode = gitmode(mmap.execf(a))
+                    addmodehdr(header, omode, mode)
+                    op = a in removed and 'rename' or 'copy'
+                    header.append('%s from %s\n' % (op, a))
+                    header.append('%s to %s\n' % (op, f))
+                    to = repo.file(a).read(mmap[a])
+                else:
+                    header.append('new file mode %s\n' % mode)
+            elif f in removed:
+                if f in srcs:
+                    dodiff = False
+                else:
+                    mode = gitmode(mmap.execf(f))
+                    header.append('deleted file mode %s\n' % mode)
+            else:
+                omode = gitmode(mmap.execf(f))
+                nmode = gitmode(util.is_exec(repo.wjoin(f), mmap.execf(f)))
+                addmodehdr(header, omode, nmode)
+            r = None
+            if dodiff:
+                header.insert(0, 'diff --git a/%s b/%s\n' % (a, b))
+                fp.write(''.join(header))
+        if dodiff:
+            fp.write(mdiff.unidiff(to, date1, tn, date2(f), f, r, opts=opts))
 
 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
            opts=None):