コード例 #1
0
def _narrow(
    ui,
    repo,
    remote,
    commoninc,
    oldincludes,
    oldexcludes,
    newincludes,
    newexcludes,
    force,
):
    oldmatch = narrowspec.match(repo.root, oldincludes, oldexcludes)
    newmatch = narrowspec.match(repo.root, newincludes, newexcludes)

    # This is essentially doing "hg outgoing" to find all local-only
    # commits. We will then check that the local-only commits don't
    # have any changes to files that will be untracked.
    unfi = repo.unfiltered()
    outgoing = discovery.findcommonoutgoing(unfi, remote, commoninc=commoninc)
    ui.status(_(b'looking for local changes to affected paths\n'))
    localnodes = []
    for n in itertools.chain(outgoing.missing, outgoing.excluded):
        if any(oldmatch(f) and not newmatch(f) for f in unfi[n].files()):
            localnodes.append(n)
    revstostrip = unfi.revs(b'descendants(%ln)', localnodes)
    hiddenrevs = repoview.filterrevs(repo, b'visible')
    visibletostrip = list(
        repo.changelog.node(r) for r in (revstostrip - hiddenrevs))
    if visibletostrip:
        ui.status(
            _(b'The following changeset(s) or their ancestors have '
              b'local changes not on the remote:\n'))
        maxnodes = 10
        if ui.verbose or len(visibletostrip) <= maxnodes:
            for n in visibletostrip:
                ui.status(b'%s\n' % short(n))
        else:
            for n in visibletostrip[:maxnodes]:
                ui.status(b'%s\n' % short(n))
            ui.status(
                _(b'...and %d more, use --verbose to list all\n') %
                (len(visibletostrip) - maxnodes))
        if not force:
            raise error.StateError(
                _(b'local changes found'),
                hint=_(b'use --force-delete-local-changes to ignore'),
            )

    with ui.uninterruptible():
        if revstostrip:
            tostrip = [unfi.changelog.node(r) for r in revstostrip]
            if repo[b'.'].node() in tostrip:
                # stripping working copy, so move to a different commit first
                urev = max(
                    repo.revs(
                        b'(::%n) - %ln + null',
                        repo[b'.'].node(),
                        visibletostrip,
                    ))
                hg.clean(repo, urev)
            overrides = {(b'devel', b'strip-obsmarkers'): False}
            with ui.configoverride(overrides, b'narrow'):
                repair.strip(ui, unfi, tostrip, topic=b'narrow')

        todelete = []
        for f, f2, size in repo.store.datafiles():
            if f.startswith(b'data/'):
                file = f[5:-2]
                if not newmatch(file):
                    todelete.append(f)
            elif f.startswith(b'meta/'):
                dir = f[5:-13]
                dirs = sorted(pathutil.dirs({dir})) + [dir]
                include = True
                for d in dirs:
                    visit = newmatch.visitdir(d)
                    if not visit:
                        include = False
                        break
                    if visit == b'all':
                        break
                if not include:
                    todelete.append(f)

        repo.destroying()

        with repo.transaction(b'narrowing'):
            # Update narrowspec before removing revlogs, so repo won't be
            # corrupt in case of crash
            repo.setnarrowpats(newincludes, newexcludes)

            for f in todelete:
                ui.status(_(b'deleting %s\n') % f)
                util.unlinkpath(repo.svfs.join(f))
                repo.store.markremoved(f)

            narrowspec.updateworkingcopy(repo, assumeclean=True)
            narrowspec.copytoworkingcopy(repo)

        repo.destroyed()
コード例 #2
0
    def write(self, transaction, link, p1, p2, added, removed, match=None):
        # We're not (for now, anyway) going to audit filenames, so we
        # can ignore added and removed.

        # TODO what does this match argument get used for? hopefully
        # just narrow?
        assert not match or isinstance(match, matchmod.alwaysmatcher)

        touched_dirs = pathutil.dirs(list(self._pending_changes))
        trees = {
            b'': self._tree,
        }
        # path: treebuilder
        builders = {
            b'': self._repo.TreeBuilder(self._tree),
        }
        # get a TreeBuilder for every tree in the touched_dirs set
        for d in sorted(touched_dirs, key=lambda x: (len(x), x)):
            if d == b'':
                # loaded root tree above
                continue
            comps = d.split(b'/')
            full = b''
            for part in comps:
                parent = trees[full]
                try:
                    parent_tree_id = parent[pycompat.fsdecode(part)].id
                    new = self._repo[parent_tree_id]
                except KeyError:
                    # new directory
                    new = None
                full += b'/' + part
                if new is not None:
                    # existing directory
                    trees[full] = new
                    builders[full] = self._repo.TreeBuilder(new)
                else:
                    # new directory, use an empty dict to easily
                    # generate KeyError as any nested new dirs get
                    # created.
                    trees[full] = {}
                    builders[full] = self._repo.TreeBuilder()
        for f, info in self._pending_changes.items():
            if b'/' not in f:
                dirname = b''
                basename = f
            else:
                dirname, basename = f.rsplit(b'/', 1)
                dirname = b'/' + dirname
            if info is None:
                builders[dirname].remove(pycompat.fsdecode(basename))
            else:
                n, fl = info
                mode = {
                    b'': pygit2.GIT_FILEMODE_BLOB,
                    b'x': pygit2.GIT_FILEMODE_BLOB_EXECUTABLE,
                    b'l': pygit2.GIT_FILEMODE_LINK,
                }[fl]
                builders[dirname].insert(
                    pycompat.fsdecode(basename), gitutil.togitnode(n), mode
                )
        # This visits the buffered TreeBuilders in deepest-first
        # order, bubbling up the edits.
        for b in sorted(builders, key=len, reverse=True):
            if b == b'':
                break
            cb = builders[b]
            dn, bn = b.rsplit(b'/', 1)
            builders[dn].insert(
                pycompat.fsdecode(bn), cb.write(), pygit2.GIT_FILEMODE_TREE
            )
        return builders[b''].write().raw
コード例 #3
0
def uncommit(ui, repo, *pats, **opts):
    """uncommit part or all of a local changeset

    This command undoes the effect of a local commit, returning the affected
    files to their uncommitted state. This means that files modified or
    deleted in the changeset will be left unchanged, and so will remain
    modified in the working directory.

    If no files are specified, the commit will be pruned, unless --keep is
    given.
    """
    opts = pycompat.byteskwargs(opts)

    cmdutil.checknotesize(ui, opts)
    cmdutil.resolvecommitoptions(ui, opts)

    with repo.wlock(), repo.lock():

        st = repo.status()
        m, a, r, d = st.modified, st.added, st.removed, st.deleted
        isdirtypath = any(set(m + a + r + d) & set(pats))
        allowdirtywcopy = opts[
            b'allow_dirty_working_copy'] or repo.ui.configbool(
                b'experimental', b'uncommitondirtywdir')
        if not allowdirtywcopy and (not pats or isdirtypath):
            cmdutil.bailifchanged(
                repo,
                hint=_(b'requires --allow-dirty-working-copy to uncommit'),
            )
        old = repo[b'.']
        rewriteutil.precheck(repo, [old.rev()], b'uncommit')
        if len(old.parents()) > 1:
            raise error.Abort(_(b"cannot uncommit merge changeset"))

        match = scmutil.match(old, pats, opts)

        # Check all explicitly given files; abort if there's a problem.
        if match.files():
            s = old.status(old.p1(), match, listclean=True)
            eligible = set(s.added) | set(s.modified) | set(s.removed)

            badfiles = set(match.files()) - eligible

            # Naming a parent directory of an eligible file is OK, even
            # if not everything tracked in that directory can be
            # uncommitted.
            if badfiles:
                badfiles -= {f for f in pathutil.dirs(eligible)}

            for f in sorted(badfiles):
                if f in s.clean:
                    hint = _(
                        b"file was not changed in working directory parent")
                elif repo.wvfs.exists(f):
                    hint = _(b"file was untracked in working directory parent")
                else:
                    hint = _(b"file does not exist")

                raise error.Abort(
                    _(b'cannot uncommit "%s"') % scmutil.getuipathfn(repo)(f),
                    hint=hint,
                )

        with repo.transaction(b'uncommit'):
            if not (opts[b'message'] or opts[b'logfile']):
                opts[b'message'] = old.description()
            message = cmdutil.logmessage(ui, opts)

            keepcommit = pats
            if not keepcommit:
                if opts.get(b'keep') is not None:
                    keepcommit = opts.get(b'keep')
                else:
                    keepcommit = ui.configbool(b'experimental',
                                               b'uncommit.keep')
            newid = _commitfiltered(
                repo,
                old,
                match,
                keepcommit,
                message=message,
                user=opts.get(b'user'),
                date=opts.get(b'date'),
            )
            if newid is None:
                ui.status(_(b"nothing to uncommit\n"))
                return 1

            mapping = {}
            if newid != old.p1().node():
                # Move local changes on filtered changeset
                mapping[old.node()] = (newid, )
            else:
                # Fully removed the old commit
                mapping[old.node()] = ()

            with repo.dirstate.parentchange():
                scmutil.movedirstate(repo, repo[newid], match)

            scmutil.cleanupnodes(repo, mapping, b'uncommit', fixphase=True)
コード例 #4
0
 def _dirs(self):
     return pathutil.dirs(self)