Exemple #1
0
def _normalize(names, default, root, cwd):
    pats = []
    for kind, name in [_patsplit(p, default) for p in names]:
        if kind in ('glob', 'relpath'):
            name = util.canonpath(root, cwd, name)
        elif kind in ('relglob', 'path'):
            name = util.normpath(name)

        pats.append((kind, name))
    return pats
Exemple #2
0
def _normalize(names, default, root, cwd, auditor):
    pats = []
    for kind, name in [_patsplit(p, default) for p in names]:
        if kind in ('glob', 'relpath'):
            name = util.canonpath(root, cwd, name, auditor)
        elif kind in ('relglob', 'path'):
            name = util.normpath(name)

        pats.append((kind, name))
    return pats
Exemple #3
0
def applydiff(ui, fp, changed, strip=1, sourcefile=None, reverse=False,
              rejmerge=None, updatedir=None):
    """reads a patch from fp and tries to apply it.  The dict 'changed' is
       filled in with all of the filenames changed by the patch.  Returns 0
       for a clean patch, -1 if any rejects were found and 1 if there was
       any fuzz."""

    rejects = 0
    err = 0
    current_file = None
    gitpatches = None

    def closefile():
        if not current_file:
            return 0
        current_file.close()
        if rejmerge:
            rejmerge(current_file)
        return len(current_file.rej)

    for state, values in iterhunks(ui, fp, sourcefile):
        if state == 'hunk':
            if not current_file:
                continue
            current_hunk = values
            ret = current_file.apply(current_hunk, reverse)
            if ret >= 0:
                changed.setdefault(current_file.fname, (None, None))
                if ret > 0:
                    err = 1
        elif state == 'file':
            rejects += closefile()
            afile, bfile, first_hunk = values
            try:
                if sourcefile:
                    current_file = patchfile(ui, sourcefile)
                else:
                    current_file, missing = selectfile(afile, bfile, first_hunk,
                                            strip, reverse)
                    current_file = patchfile(ui, current_file, missing)
            except PatchError, err:
                ui.warn(str(err) + '\n')
                current_file, current_hunk = None, None
                rejects += 1
                continue
        elif state == 'git':
            gitpatches = values
            cwd = os.getcwd()
            for gp in gitpatches:
                if gp.op in ('COPY', 'RENAME'):
                    src, dst = [util.canonpath(cwd, cwd, x)
                                for x in [gp.oldpath, gp.path]]
                    copyfile(src, dst)
                changed[gp.path] = (gp.op, gp)
Exemple #4
0
def copyfile(src, dst, basedir):
    abssrc, absdst = [util.canonpath(basedir, basedir, x) for x in [src, dst]]
    if os.path.exists(absdst):
        raise util.Abort(_("cannot create %s: destination already exists") % dst)

    dstdir = os.path.dirname(absdst)
    if dstdir and not os.path.isdir(dstdir):
        try:
            os.makedirs(dstdir)
        except IOError:
            raise util.Abort(_("cannot create %s: unable to create destination directory") % dst)

    util.copyfile(abssrc, absdst)
Exemple #5
0
    def copyfile(abssrc, relsrc, otarget, exact):
        abstarget = util.canonpath(repo.root, cwd, otarget)
        reltarget = repo.pathto(abstarget, cwd)
        target = repo.wjoin(abstarget)
        src = repo.wjoin(abssrc)
        state = repo.dirstate[abstarget]

        # check for collisions
        prevsrc = targets.get(abstarget)
        if prevsrc is not None:
            ui.warn(
                _('%s: not overwriting - %s collides with %s\n') %
                (reltarget, repo.pathto(abssrc, cwd), repo.pathto(
                    prevsrc, cwd)))
            return

        # check for overwrites
        exists = os.path.lexists(target)
        if not after and exists or after and state in 'mn':
            if not opts['force']:
                ui.warn(_('%s: not overwriting - file exists\n') % reltarget)
                return

        if after:
            if not exists:
                if rename:
                    ui.warn(
                        _('%s: not recording move - %s does not exist\n') %
                        (relsrc, reltarget))
                else:
                    ui.warn(
                        _('%s: not recording copy - %s does not exist\n') %
                        (relsrc, reltarget))
                return
        elif not dryrun:
            try:
                if exists:
                    os.unlink(target)
                targetdir = os.path.dirname(target) or '.'
                if not os.path.isdir(targetdir):
                    os.makedirs(targetdir)
                util.copyfile(src, target)
            except IOError, inst:
                if inst.errno == errno.ENOENT:
                    ui.warn(_('%s: deleted in working copy\n') % relsrc)
                else:
                    ui.warn(
                        _('%s: cannot copy - %s\n') % (relsrc, inst.strerror))
                    return True  # report a failure
Exemple #6
0
def copyfile(src, dst, basedir):
    abssrc, absdst = [util.canonpath(basedir, basedir, x) for x in [src, dst]]
    if os.path.exists(absdst):
        raise util.Abort(_("cannot create %s: destination already exists") %
                         dst)

    dstdir = os.path.dirname(absdst)
    if dstdir and not os.path.isdir(dstdir):
        try:
            os.makedirs(dstdir)
        except IOError:
            raise util.Abort(
                _("cannot create %s: unable to create destination directory")
                % dst)

    util.copyfile(abssrc, absdst)
Exemple #7
0
    def copyfile(abssrc, relsrc, otarget, exact):
        abstarget = util.canonpath(repo.root, cwd, otarget)
        reltarget = repo.pathto(abstarget, cwd)
        target = repo.wjoin(abstarget)
        src = repo.wjoin(abssrc)
        state = repo.dirstate[abstarget]

        # check for collisions
        prevsrc = targets.get(abstarget)
        if prevsrc is not None:
            ui.warn(_('%s: not overwriting - %s collides with %s\n') %
                    (reltarget, repo.pathto(abssrc, cwd),
                     repo.pathto(prevsrc, cwd)))
            return

        # check for overwrites
        exists = os.path.lexists(target)
        if not after and exists or after and state in 'mn':
            if not opts['force']:
                ui.warn(_('%s: not overwriting - file exists\n') %
                        reltarget)
                return

        if after:
            if not exists:
                if rename:
                    ui.warn(_('%s: not recording move - %s does not exist\n') %
                            (relsrc, reltarget))
                else:
                    ui.warn(_('%s: not recording copy - %s does not exist\n') %
                            (relsrc, reltarget))
                return
        elif not dryrun:
            try:
                if exists:
                    os.unlink(target)
                targetdir = os.path.dirname(target) or '.'
                if not os.path.isdir(targetdir):
                    os.makedirs(targetdir)
                util.copyfile(src, target)
            except IOError, inst:
                if inst.errno == errno.ENOENT:
                    ui.warn(_('%s: deleted in working copy\n') % relsrc)
                else:
                    ui.warn(_('%s: cannot copy - %s\n') %
                            (relsrc, inst.strerror))
                    return True # report a failure
Exemple #8
0
 def targetpathfn(pat, dest, srcs):
     if os.path.isdir(pat):
         abspfx = util.canonpath(repo.root, cwd, pat)
         abspfx = util.localpath(abspfx)
         if destdirexists:
             striplen = len(os.path.split(abspfx)[0])
         else:
             striplen = len(abspfx)
         if striplen:
             striplen += len(os.sep)
         res = lambda p: os.path.join(dest, util.localpath(p)[striplen:])
     elif destdirexists:
         res = lambda p: os.path.join(dest,
                                      os.path.basename(util.localpath(p)))
     else:
         res = lambda p: dest
     return res
Exemple #9
0
 def targetpathfn(pat, dest, srcs):
     if os.path.isdir(pat):
         abspfx = util.canonpath(repo.root, cwd, pat)
         abspfx = util.localpath(abspfx)
         if destdirexists:
             striplen = len(os.path.split(abspfx)[0])
         else:
             striplen = len(abspfx)
         if striplen:
             striplen += len(os.sep)
         res = lambda p: os.path.join(dest, util.localpath(p)[striplen:])
     elif destdirexists:
         res = lambda p: os.path.join(dest,
                                      os.path.basename(util.localpath(p)))
     else:
         res = lambda p: dest
     return res
Exemple #10
0
def _normalize(names, default, root, cwd, auditor):
    pats = []
    for kind, name in [_patsplit(p, default) for p in names]:
        if kind in ('glob', 'relpath'):
            name = util.canonpath(root, cwd, name, auditor)
        elif kind in ('relglob', 'path'):
            name = util.normpath(name)
        elif kind in ('listfile', 'listfile0'):
            delimiter = kind == 'listfile0' and '\0' or '\n'
            try:
                files = open(name, 'r').read().split(delimiter)
                files = [f for f in files if f]
            except EnvironmentError:
                raise util.Abort(_("unable to read file list (%s)") % name)
            pats += _normalize(files, default, root, cwd, auditor)
            continue

        pats.append((kind, name))
    return pats
Exemple #11
0
def _normalize(names, default, root, cwd, auditor):
    pats = []
    for kind, name in [_patsplit(p, default) for p in names]:
        if kind in ('glob', 'relpath'):
            name = util.canonpath(root, cwd, name, auditor)
        elif kind in ('relglob', 'path'):
            name = util.normpath(name)
        elif kind in ('listfile', 'listfile0'):
            delimiter = kind == 'listfile0' and '\0' or '\n'
            try:
                files = open(name, 'r').read().split(delimiter)
                files = [f for f in files if f]
            except EnvironmentError:
                raise util.Abort(_("unable to read file list (%s)") % name)
            pats += _normalize(files, default, root, cwd, auditor)
            continue

        pats.append((kind, name))
    return pats
Exemple #12
0
    def targetpathafterfn(pat, dest, srcs):
        if util.patkind(pat, None)[0]:
            # a mercurial pattern
            res = lambda p: os.path.join(dest,
                                         os.path.basename(util.localpath(p)))
        else:
            abspfx = util.canonpath(repo.root, cwd, pat)
            if len(abspfx) < len(srcs[0][0]):
                # A directory. Either the target path contains the last
                # component of the source path or it does not.
                def evalpath(striplen):
                    score = 0
                    for s in srcs:
                        t = os.path.join(dest, util.localpath(s[0])[striplen:])
                        if os.path.exists(t):
                            score += 1
                    return score

                abspfx = util.localpath(abspfx)
                striplen = len(abspfx)
                if striplen:
                    striplen += len(os.sep)
                if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
                    score = evalpath(striplen)
                    striplen1 = len(os.path.split(abspfx)[0])
                    if striplen1:
                        striplen1 += len(os.sep)
                    if evalpath(striplen1) > score:
                        striplen = striplen1
                res = lambda p: os.path.join(dest,
                                             util.localpath(p)[striplen:])
            else:
                # a file
                if destdirexists:
                    res = lambda p: os.path.join(
                        dest, os.path.basename(util.localpath(p)))
                else:
                    res = lambda p: dest
        return res
Exemple #13
0
    def targetpathafterfn(pat, dest, srcs):
        if matchmod.patkind(pat):
            # a mercurial pattern
            res = lambda p: os.path.join(dest,
                                         os.path.basename(util.localpath(p)))
        else:
            abspfx = util.canonpath(repo.root, cwd, pat)
            if len(abspfx) < len(srcs[0][0]):
                # A directory. Either the target path contains the last
                # component of the source path or it does not.
                def evalpath(striplen):
                    score = 0
                    for s in srcs:
                        t = os.path.join(dest, util.localpath(s[0])[striplen:])
                        if os.path.lexists(t):
                            score += 1
                    return score

                abspfx = util.localpath(abspfx)
                striplen = len(abspfx)
                if striplen:
                    striplen += len(os.sep)
                if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
                    score = evalpath(striplen)
                    striplen1 = len(os.path.split(abspfx)[0])
                    if striplen1:
                        striplen1 += len(os.sep)
                    if evalpath(striplen1) > score:
                        striplen = striplen1
                res = lambda p: os.path.join(dest,
                                             util.localpath(p)[striplen:])
            else:
                # a file
                if destdirexists:
                    res = lambda p: os.path.join(dest,
                                        os.path.basename(util.localpath(p)))
                else:
                    res = lambda p: dest
        return res
Exemple #14
0
def applydiff(ui,
              fp,
              changed,
              strip=1,
              sourcefile=None,
              reverse=False,
              rejmerge=None,
              updatedir=None):
    """reads a patch from fp and tries to apply it.  The dict 'changed' is
       filled in with all of the filenames changed by the patch.  Returns 0
       for a clean patch, -1 if any rejects were found and 1 if there was
       any fuzz."""

    rejects = 0
    err = 0
    current_file = None
    gitpatches = None

    def closefile():
        if not current_file:
            return 0
        current_file.close()
        if rejmerge:
            rejmerge(current_file)
        return len(current_file.rej)

    for state, values in iterhunks(ui, fp, sourcefile):
        if state == 'hunk':
            if not current_file:
                continue
            current_hunk = values
            ret = current_file.apply(current_hunk, reverse)
            if ret >= 0:
                changed.setdefault(current_file.fname, (None, None))
                if ret > 0:
                    err = 1
        elif state == 'file':
            rejects += closefile()
            afile, bfile, first_hunk = values
            try:
                if sourcefile:
                    current_file = patchfile(ui, sourcefile)
                else:
                    current_file, missing = selectfile(afile, bfile,
                                                       first_hunk, strip,
                                                       reverse)
                    current_file = patchfile(ui, current_file, missing)
            except PatchError, err:
                ui.warn(str(err) + '\n')
                current_file, current_hunk = None, None
                rejects += 1
                continue
        elif state == 'git':
            gitpatches = values
            cwd = os.getcwd()
            for gp in gitpatches:
                if gp.op in ('COPY', 'RENAME'):
                    src, dst = [
                        util.canonpath(cwd, cwd, x)
                        for x in [gp.oldpath, gp.path]
                    ]
                    copyfile(src, dst)
                changed[gp.path] = (gp.op, gp)