view util.py @ 69:63ece4ea25c9

hg_delta_editor: register copies only if files are unchanged between source and dest Handle copies of items from revision X into revision Y where X is not the parent of Y. This cannot happen in Mercurial because copies always happen between parents and children. A file copy is recorded if: 1- Source and destination revs are in the same branch. 2- The file is unchanged (content, type, removal) through all revisions between destination and source, not including source and destination. Directory copies are registered only if the previous rules apply on all copied items. [1] is there because file copies across branches are meaningless in Mercurial world. We could have tried to remap the source rev to a similar one in the correct branch, but anyway the intent is wrong. [2] is more questionable but I think it's better this way for we live in a non-perfect svn world. In theory, 99% of copies out there should come from the direct parent. But the direct parent is a fuzzy notion when you can have a working directory composed of different directory at different revisions. So we assume that stuff copied from past revisions exactly matching the content of the direct parent revision is really copied from the parent revision. The alternative would be to discard the copy, which would always happen unless people kept updating the working directory after every commit (see tests).
author Patrick Mezard <pmezard@gmail.com>
date Wed, 05 Nov 2008 13:37:08 +0100
parents b3c7b844b782
children 1da7aafdd323
line wrap: on
line source

import os
import pickle
import shutil

from mercurial import node

svn_subcommands = { }

def register_subcommand(name):
    def inner(fn):
        svn_subcommands[name] = fn
        return fn
    return inner


def wipe_all_files(hg_wc_path):
    files = [f for f in os.listdir(hg_wc_path) if f != '.hg']
    for f in files:
        f = os.path.join(hg_wc_path, f)
        if os.path.isdir(f):
            shutil.rmtree(f)
        else:
            os.remove(f)

REVMAP_FILE_VERSION = 1
def parse_revmap(revmap_filename):
    revmap = {}
    f = open(revmap_filename)
    try:
        # Remove compat code after March of 2009. That should be more than long
        # enough.
        revmap = pickle.load(f)
        f.close()
        f = open(revmap_filename, 'w')
        f.write('1\n')
        for key, value in sorted(revmap.items()):
            f.write('%s %s %s\n' % (str(key[0]), node.hex(value), key[1] or ''))
        f.close()
    except:
        f.close()
        f = open(revmap_filename)
        ver = int(f.readline())
        if ver == 1:
            for l in f:
                revnum, node_hash, branch = l.split(' ', 2)
                if branch == '\n':
                    branch = None
                else:
                    branch = branch[:-1]
                revmap[int(revnum), branch] = node.bin(node_hash)
            f.close()
        else:
            print ('Your revmap was made by a newer version of hgsubversion.'
                   ' Please upgrade.')
            raise NotImplementedError
    return revmap


class PrefixMatch(object):
    def __init__(self, prefix):
        self.p = prefix
    
    def files(self):
        return []
    
    def __call__(self, fn):
        return fn.startswith(self.p)