commands.py 115 KB
Newer Older
mpm@selenic.com's avatar
mpm@selenic.com committed
1 2
# commands.py - command processing for mercurial
#
3
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
mpm@selenic.com's avatar
mpm@selenic.com committed
4 5 6 7
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

8
import demandimport; demandimport.enable()
9
from node import *
Matt Mackall's avatar
Matt Mackall committed
10
from i18n import _
11 12
import bisect, os, re, sys, urllib, shlex, stat
import ui, hg, util, revlog, bundlerepo, extensions
13
import difflib, patch, time, help, mdiff, tempfile
14
import errno, version, socket
15
import archival, changegroup, cmdutil, hgweb.server, sshserver
16

mpm@selenic.com's avatar
mpm@selenic.com committed
17
# Commands start here, listed alphabetically
18

19
def add(ui, repo, *pats, **opts):
20 21 22 23
    """add the specified files on the next commit

    Schedule files to be version controlled and added to the repository.

24 25
    The files will be added to the repository at the next commit. To
    undo an add before that, see hg revert.
26

27
    If no names are given, add all files in the repository.
28 29
    """

30
    names = []
31
    for src, abs, rel, exact in cmdutil.walk(repo, pats, opts):
32
        if exact:
33 34
            if ui.verbose:
                ui.status(_('adding %s\n') % rel)
35 36
            names.append(abs)
        elif repo.dirstate.state(abs) == '?':
37
            ui.status(_('adding %s\n') % rel)
38
            names.append(abs)
39
    if not opts.get('dry_run'):
Vadim Gelfer's avatar
Vadim Gelfer committed
40
        repo.add(names)
41

42
def addremove(ui, repo, *pats, **opts):
43
    """add all new files, delete all missing files
44 45 46 47 48

    Add all new files and remove all missing files from the repository.

    New files are ignored if they match any of the patterns in .hgignore. As
    with add, these changes take effect at the next commit.
49 50 51 52 53 54

    Use the -s option to detect renamed files.  With a parameter > 0,
    this compares every removed file with every added file and records
    those similar enough as renames.  This option takes a percentage
    between 0 (disabled) and 100 (files must be identical) as its
    parameter.  Detecting renamed files this way can be expensive.
55
    """
56 57 58 59
    sim = float(opts.get('similarity') or 0)
    if sim < 0 or sim > 100:
        raise util.Abort(_('similarity must be between 0 and 100'))
    return cmdutil.addremove(repo, pats, opts, similarity=sim/100.)
60

61
def annotate(ui, repo, *pats, **opts):
62 63 64 65 66 67 68 69 70 71 72
    """show changeset information per file line

    List changes in files, showing the revision id responsible for each line

    This command is useful to discover who did a change or when a change took
    place.

    Without the -a option, annotate will avoid processing files it
    detects as binary. With -a, annotate will generate an annotation
    anyway, probably with undesirable results.
    """
73
    getdate = util.cachefunc(lambda x: util.datestr(x.date()))
74

75
    if not pats:
76
        raise util.Abort(_('at least one file name or pattern required'))
77

78 79 80
    opmap = [['user', lambda x: ui.shortuser(x.user())],
             ['number', lambda x: str(x.rev())],
             ['changeset', lambda x: short(x.node())],
81 82 83
             ['date', getdate], ['follow', lambda x: x.path()]]
    if (not opts['user'] and not opts['changeset'] and not opts['date']
        and not opts['follow']):
84
        opts['number'] = 1
85

Brendan Cully's avatar
Brendan Cully committed
86
    ctx = repo.changectx(opts['rev'])
87

88 89
    for src, abs, rel, exact in cmdutil.walk(repo, pats, opts,
                                             node=ctx.node()):
90 91
        fctx = ctx.filectx(abs)
        if not opts['text'] and util.binary(fctx.data()):
92
            ui.write(_("%s: binary file\n") % ((pats and rel) or abs))
93 94
            continue

95
        lines = fctx.annotate(follow=opts.get('follow'))
96 97 98
        pieces = []

        for o, f in opmap:
99
            if opts[o]:
100
                l = [f(n) for n, dummy in lines]
mpm@selenic.com's avatar
mpm@selenic.com committed
101 102 103
                if l:
                    m = max(map(len, l))
                    pieces.append(["%*s" % (m, x) for x in l])
104

mpm@selenic.com's avatar
mpm@selenic.com committed
105 106 107
        if pieces:
            for p, l in zip(zip(*pieces), lines):
                ui.write("%s: %s" % (" ".join(p), l[1]))
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
def archive(ui, repo, dest, **opts):
    '''create unversioned archive of a repository revision

    By default, the revision used is the parent of the working
    directory; use "-r" to specify a different revision.

    To specify the type of archive to create, use "-t".  Valid
    types are:

    "files" (default): a directory full of files
    "tar": tar archive, uncompressed
    "tbz2": tar archive, compressed using bzip2
    "tgz": tar archive, compressed using gzip
    "uzip": zip archive, uncompressed
    "zip": zip archive, compressed using deflate

    The exact name of the destination archive or directory is given
    using a format string; see "hg help export" for details.

    Each member added to an archive file has a directory prefix
    prepended.  Use "-p" to specify a format string for the prefix.
    The default is the basename of the archive, with suffixes removed.
    '''

Brendan Cully's avatar
Brendan Cully committed
133
    node = repo.changectx(opts['rev']).node()
Vadim Gelfer's avatar
Vadim Gelfer committed
134
    dest = cmdutil.make_filename(repo, dest, node)
135 136
    if os.path.realpath(dest) == repo.root:
        raise util.Abort(_('repository root cannot be destination'))
137
    dummy, matchfn, dummy = cmdutil.matchpats(repo, [], opts)
138 139 140 141 142 143 144
    kind = opts.get('type') or 'files'
    prefix = opts['prefix']
    if dest == '-':
        if kind == 'files':
            raise util.Abort(_('cannot archive plain files to stdout'))
        dest = sys.stdout
        if not prefix: prefix = os.path.basename(repo.root) + '-%h'
Vadim Gelfer's avatar
Vadim Gelfer committed
145
    prefix = cmdutil.make_filename(repo, prefix, node)
146 147
    archival.archive(repo, dest, node, kind, not opts['no_decode'],
                     matchfn, prefix)
148

Daniel Holth's avatar
Daniel Holth committed
149
def backout(ui, repo, node=None, rev=None, **opts):
Vadim Gelfer's avatar
Vadim Gelfer committed
150 151
    '''reverse effect of earlier changeset

Vadim Gelfer's avatar
Vadim Gelfer committed
152 153
    Commit the backed out changes as a new changeset.  The new
    changeset is a child of the backed out changeset.
Vadim Gelfer's avatar
Vadim Gelfer committed
154 155

    If you back out a changeset other than the tip, a new head is
Vadim Gelfer's avatar
Vadim Gelfer committed
156 157 158 159 160 161 162 163 164 165
    created.  This head is the parent of the working directory.  If
    you back out an old changeset, your working directory will appear
    old after the backout.  You should merge the backout changeset
    with another head.

    The --merge option remembers the parent of the working directory
    before starting the backout, then merges the new head with that
    changeset afterwards.  This saves you from doing the merge by
    hand.  The result of this merge is not committed, as for a normal
    merge.'''
Daniel Holth's avatar
Daniel Holth committed
166 167 168 169 170
    if rev and node:
        raise util.Abort(_("please specify just one revision"))

    if not rev:
        rev = node
Vadim Gelfer's avatar
Vadim Gelfer committed
171

172 173 174
    if not rev:
        raise util.Abort(_("please specify a revision to backout"))

175
    cmdutil.bail_if_changed(repo)
Vadim Gelfer's avatar
Vadim Gelfer committed
176 177 178 179
    op1, op2 = repo.dirstate.parents()
    if op2 != nullid:
        raise util.Abort(_('outstanding uncommitted merge'))
    node = repo.lookup(rev)
180 181
    p1, p2 = repo.changelog.parents(node)
    if p1 == nullid:
Vadim Gelfer's avatar
Vadim Gelfer committed
182 183
        raise util.Abort(_('cannot back out a change with no parents'))
    if p2 != nullid:
184 185 186 187 188
        if not opts['parent']:
            raise util.Abort(_('cannot back out a merge changeset without '
                               '--parent'))
        p = repo.lookup(opts['parent'])
        if p not in (p1, p2):
189
            raise util.Abort(_('%s is not a parent of %s') %
190
                             (short(p), short(node)))
191 192 193 194 195
        parent = p
    else:
        if opts['parent']:
            raise util.Abort(_('cannot use --parent on non-merge changeset'))
        parent = p1
196
    hg.clean(repo, node, show_stats=False)
Vadim Gelfer's avatar
Vadim Gelfer committed
197
    revert_opts = opts.copy()
198
    revert_opts['date'] = None
199
    revert_opts['all'] = True
Vadim Gelfer's avatar
Vadim Gelfer committed
200 201 202 203
    revert_opts['rev'] = hex(parent)
    revert(ui, repo, **revert_opts)
    commit_opts = opts.copy()
    commit_opts['addremove'] = False
204
    if not commit_opts['message'] and not commit_opts['logfile']:
205
        commit_opts['message'] = _("Backed out changeset %s") % (short(node))
206
        commit_opts['force_editor'] = True
Vadim Gelfer's avatar
Vadim Gelfer committed
207 208 209 210 211
    commit(ui, repo, **commit_opts)
    def nice(node):
        return '%d:%s' % (repo.changelog.rev(node), short(node))
    ui.status(_('changeset %s backs out changeset %s\n') %
              (nice(repo.changelog.tip()), nice(node)))
212 213 214
    if op1 != node:
        if opts['merge']:
            ui.status(_('merging with changeset %s\n') % nice(op1))
215
            hg.merge(repo, hex(op1))
216 217 218
        else:
            ui.status(_('the backout changeset is a new head - '
                        'do not forget to merge\n'))
219 220
            ui.status(_('(use "backout --merge" '
                        'if you want to auto-merge)\n'))
Vadim Gelfer's avatar
Vadim Gelfer committed
221

222
def branch(ui, repo, label=None, **opts):
223 224
    """set or show the current branch name

225 226 227
    With no argument, show the current branch name. With one argument,
    set the working directory branch name (the branch does not exist in
    the repository until the next commit).
228 229 230

    Unless --force is specified, branch will not let you set a
    branch name that shadows an existing branch.
231 232
    """

233
    if label:
234 235 236 237
        if not opts.get('force') and label in repo.branchtags():
            if label not in [p.branch() for p in repo.workingctx().parents()]:
                raise util.Abort(_('a branch of the same name already exists'
                                   ' (use --force to override)'))
238
        repo.dirstate.setbranch(util.fromlocal(label))
239
        ui.status(_('marked working directory as branch %s\n') % label)
240
    else:
241
        ui.write("%s\n" % util.tolocal(repo.dirstate.branch()))
242

243
def branches(ui, repo, active=False):
244 245
    """list repository named branches

246 247 248 249
    List the repository's named branches, indicating which ones are
    inactive.  If active is specified, only show active branches.

    A branch is considered active if it contains unmerged heads.
250 251
    """
    b = repo.branchtags()
252 253
    heads = dict.fromkeys(repo.heads(), 1)
    l = [((n in heads), repo.changelog.rev(n), n, t) for t, n in b.items()]
254
    l.sort()
255 256 257 258 259 260
    l.reverse()
    for ishead, r, n, t in l:
        if active and not ishead:
            # If we're only displaying active branches, abort the loop on
            # encountering the first inactive head
            break
261
        else:
262 263 264 265 266 267 268 269 270
            hexfunc = ui.debugflag and hex or short
            if ui.quiet:
                ui.write("%s\n" % t)
            else:
                spaces = " " * (30 - util.locallen(t))
                # The code only gets here if inactive branches are being
                # displayed or the branch is active.
                isinactive = ((not ishead) and " (inactive)") or ''
                ui.write("%s%s %s:%s%s\n" % (t, spaces, r, hexfunc(n), isinactive))
271

272
def bundle(ui, repo, fname, dest=None, **opts):
273 274
    """create a changegroup file

275 276
    Generate a compressed changegroup file collecting changesets not
    found in the other repository.
277

278 279
    If no destination repository is specified the destination is assumed
    to have all the nodes specified by one or more --base parameters.
280

281 282 283 284
    The bundle file can then be transferred using conventional means and
    applied to another repository with the unbundle or pull command.
    This is useful when direct push and pull are not available or when
    exporting an entire repository is undesirable.
285

286 287
    Applying bundles preserves all changeset contents including
    permissions, copy/rename information, and revision history.
288
    """
289 290 291 292 293 294 295 296
    revs = opts.get('rev') or None
    if revs:
        revs = [repo.lookup(rev) for rev in revs]
    base = opts.get('base')
    if base:
        if dest:
            raise util.Abort(_("--base is incompatible with specifiying "
                               "a destination"))
297 298 299
        base = [repo.lookup(rev) for rev in base]
        # create the right base
        # XXX: nodesbetween / changegroup* should be "fixed" instead
300
        o = []
Matt Mackall's avatar
Matt Mackall committed
301
        has = {nullid: None}
302
        for n in base:
Matt Mackall's avatar
Matt Mackall committed
303
            has.update(repo.changelog.reachable(n))
304
        if revs:
305
            visit = list(revs)
306
        else:
307
            visit = repo.changelog.heads()
Matt Mackall's avatar
Matt Mackall committed
308
        seen = {}
309 310
        while visit:
            n = visit.pop(0)
Matt Mackall's avatar
Matt Mackall committed
311
            parents = [p for p in repo.changelog.parents(n) if p not in has]
312 313 314
            if len(parents) == 0:
                o.insert(0, n)
            else:
315 316
                for p in parents:
                    if p not in seen:
Matt Mackall's avatar
Matt Mackall committed
317
                        seen[p] = 1
318
                        visit.append(p)
319
    else:
320
        cmdutil.setremoteconfig(ui, opts)
Matt Mackall's avatar
Matt Mackall committed
321 322
        dest, revs = cmdutil.parseurl(
            ui.expandpath(dest or 'default-push', dest or 'default'), revs)
323 324 325 326 327 328 329
        other = hg.repository(ui, dest)
        o = repo.findoutgoing(other, force=opts['force'])

    if revs:
        cg = repo.changegroupsubset(o, revs, 'bundle')
    else:
        cg = repo.changegroup(o, 'bundle')
330
    changegroup.writebundle(cg, fname, "HG10BZ")
331

332
def cat(ui, repo, file1, *pats, **opts):
333
    """output the current or given revision of files
334 335

    Print the specified files as they were at the given revision.
336 337
    If no revision is given, the parent of the working directory is used,
    or tip if no revision is checked out.
338 339 340 341 342 343 344 345 346

    Output may be to a file, in which case the name of the file is
    given using a format string.  The formatting rules are the same as
    for the export command, with the following additions:

    %s   basename of file being printed
    %d   dirname of file being printed, or '.' if in repo root
    %p   root-relative path name of file being printed
    """
Brendan Cully's avatar
Brendan Cully committed
347
    ctx = repo.changectx(opts['rev'])
Matt Mackall's avatar
Matt Mackall committed
348
    err = 1
349 350
    for src, abs, rel, exact in cmdutil.walk(repo, (file1,) + pats, opts,
                                             ctx.node()):
Vadim Gelfer's avatar
Vadim Gelfer committed
351
        fp = cmdutil.make_file(repo, opts['output'], ctx.node(), pathname=abs)
352
        fp.write(ctx.filectx(abs).data())
Matt Mackall's avatar
Matt Mackall committed
353 354
        err = 0
    return err
mpm@selenic.com's avatar
mpm@selenic.com committed
355

356
def clone(ui, source, dest=None, **opts):
357 358 359 360 361 362 363 364 365 366 367
    """make a copy of an existing repository

    Create a copy of an existing repository in a new directory.

    If no destination directory name is specified, it defaults to the
    basename of the source.

    The location of the source is added to the new repository's
    .hg/hgrc file, as the default to be used for future pulls.

    For efficiency, hardlinks are used for cloning whenever the source
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    and destination are on the same filesystem (note this applies only
    to the repository data, not to the checked out files).  Some
    filesystems, such as AFS, implement hardlinking incorrectly, but
    do not report errors.  In these cases, use the --pull option to
    avoid hardlinking.

    You can safely clone repositories and checked out files using full
    hardlinks with

      $ cp -al REPO REPOCLONE

    which is the fastest way to clone. However, the operation is not
    atomic (making sure REPO is not modified during the operation is
    up to you) and you have to make sure your editor breaks hardlinks
    (Emacs and most Linux Kernel tools do so).
383

384 385 386
    If you use the -r option to clone up to a specific revision, no
    subsequent revisions will be present in the cloned repository.
    This option implies --pull, even on local repositories.
387 388

    See pull for valid source format details.
389 390

    It is possible to specify an ssh:// URL as the destination, but no
391 392 393
    .hg/hgrc and working directory will be created on the remote side.
    Look at the help text for the pull command for important details
    about ssh:// URLs.
394
    """
395
    cmdutil.setremoteconfig(ui, opts)
Matt Mackall's avatar
Matt Mackall committed
396
    hg.clone(ui, source, dest,
397
             pull=opts['pull'],
398
             stream=opts['uncompressed'],
399 400
             rev=opts['rev'],
             update=not opts['noupdate'])
mpm@selenic.com's avatar
mpm@selenic.com committed
401

402
def commit(ui, repo, *pats, **opts):
403 404 405 406 407
    """commit the specified files or all outstanding changes

    Commit changes to the given files into the repository.

    If a list of files is omitted, all changes reported by "hg status"
408
    will be committed.
409

410 411
    If no commit message is specified, the editor configured in your hgrc
    or in the EDITOR environment variable is started to enter a message.
412
    """
413
    message = cmdutil.logmessage(opts)
414

mpm@selenic.com's avatar
mpm@selenic.com committed
415
    if opts['addremove']:
416
        cmdutil.addremove(repo, pats, opts)
417
    fns, match, anypats = cmdutil.matchpats(repo, pats, opts)
418
    if pats:
419 420
        status = repo.status(files=fns, match=match)
        modified, added, removed, deleted, unknown = status[:5]
421
        files = modified + added + removed
422
        slist = None
423
        for f in fns:
424 425
            if f == '.':
                continue
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
            if f not in files:
                rf = repo.wjoin(f)
                try:
                    mode = os.lstat(rf)[stat.ST_MODE]
                except OSError:
                    raise util.Abort(_("file %s not found!") % rf)
                if stat.S_ISDIR(mode):
                    name = f + '/'
                    if slist is None:
                        slist = list(files)
                        slist.sort()
                    i = bisect.bisect(slist, name)
                    if i >= len(slist) or not slist[i].startswith(name):
                        raise util.Abort(_("no match under directory %s!")
                                         % rf)
441
                elif not (stat.S_ISREG(mode) or stat.S_ISLNK(mode)):
442 443
                    raise util.Abort(_("can't commit %s: "
                                       "unsupported file type!") % rf)
444 445
                elif repo.dirstate.state(f) == '?':
                    raise util.Abort(_("file %s not tracked!") % rf)
446 447
    else:
        files = []
448
    try:
449
        repo.commit(files, message, opts['user'], opts['date'], match,
450
                    force_editor=opts.get('force_editor'))
451 452
    except ValueError, inst:
        raise util.Abort(str(inst))
453

454 455
def docopy(ui, repo, pats, opts, wlock):
    # called with the repo lock held
456 457 458
    #
    # hgsep => pathname that uses "/" to separate directories
    # ossep => pathname that uses os.sep to separate directories
459 460 461
    cwd = repo.getcwd()
    errors = 0
    copied = []
462
    targets = {}
463

464 465 466
    # abs: hgsep
    # rel: ossep
    # return: hgsep
467
    def okaytocopy(abs, rel, exact):
468
        reasons = {'?': _('is not managed'),
469
                   'r': _('has been marked for remove')}
Robin Farine's avatar
Robin Farine committed
470 471
        state = repo.dirstate.state(abs)
        reason = reasons.get(state)
472
        if reason:
473 474 475
            if exact:
                ui.warn(_('%s: not copying - file %s\n') % (rel, reason))
        else:
Robin Farine's avatar
Robin Farine committed
476 477 478 479 480
            if state == 'a':
                origsrc = repo.dirstate.copied(abs)
                if origsrc is not None:
                    return origsrc
            return abs
481

482 483 484
    # origsrc: hgsep
    # abssrc: hgsep
    # relsrc: ossep
485 486 487
    # otarget: ossep
    def copy(origsrc, abssrc, relsrc, otarget, exact):
        abstarget = util.canonpath(repo.root, cwd, otarget)
488
        reltarget = repo.pathto(abstarget, cwd)
489
        prevsrc = targets.get(abstarget)
490 491
        src = repo.wjoin(abssrc)
        target = repo.wjoin(abstarget)
492 493
        if prevsrc is not None:
            ui.warn(_('%s: not overwriting - %s collides with %s\n') %
494 495
                    (reltarget, repo.pathto(abssrc, cwd),
                     repo.pathto(prevsrc, cwd)))
496
            return
497
        if (not opts['after'] and os.path.exists(target) or
498
            opts['after'] and repo.dirstate.state(abstarget) not in '?ar'):
Robin Farine's avatar
Robin Farine committed
499
            if not opts['force']:
500 501 502
                ui.warn(_('%s: not overwriting - file exists\n') %
                        reltarget)
                return
503
            if not opts['after'] and not opts.get('dry_run'):
504
                os.unlink(target)
Robin Farine's avatar
Robin Farine committed
505
        if opts['after']:
506
            if not os.path.exists(target):
Robin Farine's avatar
Robin Farine committed
507 508
                return
        else:
509
            targetdir = os.path.dirname(target) or '.'
510
            if not os.path.isdir(targetdir) and not opts.get('dry_run'):
511
                os.makedirs(targetdir)
512
            try:
513
                restore = repo.dirstate.state(abstarget) == 'r'
514
                if restore and not opts.get('dry_run'):
515 516
                    repo.undelete([abstarget], wlock)
                try:
517
                    if not opts.get('dry_run'):
518
                        util.copyfile(src, target)
519 520 521
                    restore = False
                finally:
                    if restore:
522
                        repo.remove([abstarget], wlock=wlock)
523 524
            except IOError, inst:
                if inst.errno == errno.ENOENT:
525
                    ui.warn(_('%s: deleted in working copy\n') % relsrc)
526
                else:
527 528 529 530
                    ui.warn(_('%s: cannot copy - %s\n') %
                            (relsrc, inst.strerror))
                    errors += 1
                    return
Robin Farine's avatar
Robin Farine committed
531 532
        if ui.verbose or not exact:
            ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
533
        targets[abstarget] = abssrc
534 535 536 537 538 539 540 541 542
        if abstarget != origsrc:
            if repo.dirstate.state(origsrc) == 'a':
                ui.warn(_("%s was marked for addition. "
                          "%s will not be committed as a copy.\n")
                        % (repo.pathto(origsrc, cwd), reltarget))
                if abstarget not in repo.dirstate and not opts.get('dry_run'):
                    repo.add([abstarget], wlock)
            elif not opts.get('dry_run'):
                repo.copy(origsrc, abstarget, wlock)
543 544
        copied.append((abssrc, relsrc, exact))

545 546 547 548
    # pat: ossep
    # dest ossep
    # srcs: list of (hgsep, hgsep, ossep, bool)
    # return: function that takes hgsep and returns ossep
Robin Farine's avatar
Robin Farine committed
549 550
    def targetpathfn(pat, dest, srcs):
        if os.path.isdir(pat):
551
            abspfx = util.canonpath(repo.root, cwd, pat)
552
            abspfx = util.localpath(abspfx)
Robin Farine's avatar
Robin Farine committed
553
            if destdirexists:
554
                striplen = len(os.path.split(abspfx)[0])
Robin Farine's avatar
Robin Farine committed
555
            else:
556
                striplen = len(abspfx)
Robin Farine's avatar
Robin Farine committed
557 558
            if striplen:
                striplen += len(os.sep)
559
            res = lambda p: os.path.join(dest, util.localpath(p)[striplen:])
Robin Farine's avatar
Robin Farine committed
560
        elif destdirexists:
561 562
            res = lambda p: os.path.join(dest,
                                         os.path.basename(util.localpath(p)))
Robin Farine's avatar
Robin Farine committed
563 564 565 566
        else:
            res = lambda p: dest
        return res

567 568 569 570
    # pat: ossep
    # dest ossep
    # srcs: list of (hgsep, hgsep, ossep, bool)
    # return: function that takes hgsep and returns ossep
Robin Farine's avatar
Robin Farine committed
571 572 573
    def targetpathafterfn(pat, dest, srcs):
        if util.patkind(pat, None)[0]:
            # a mercurial pattern
574 575
            res = lambda p: os.path.join(dest,
                                         os.path.basename(util.localpath(p)))
Robin Farine's avatar
Robin Farine committed
576
        else:
577 578 579 580 581 582 583
            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:
584
                        t = os.path.join(dest, util.localpath(s[0])[striplen:])
585 586 587 588
                        if os.path.exists(t):
                            score += 1
                    return score

589
                abspfx = util.localpath(abspfx)
590 591 592 593 594 595 596 597 598 599
                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
600 601
                res = lambda p: os.path.join(dest,
                                             util.localpath(p)[striplen:])
Robin Farine's avatar
Robin Farine committed
602
            else:
603 604
                # a file
                if destdirexists:
605 606
                    res = lambda p: os.path.join(dest,
                                        os.path.basename(util.localpath(p)))
607 608
                else:
                    res = lambda p: dest
Robin Farine's avatar
Robin Farine committed
609 610 611
        return res


612
    pats = util.expand_glob(pats)
613 614 615 616 617 618
    if not pats:
        raise util.Abort(_('no source or destination specified'))
    if len(pats) == 1:
        raise util.Abort(_('no destination specified'))
    dest = pats.pop()
    destdirexists = os.path.isdir(dest)
Robin Farine's avatar
Robin Farine committed
619
    if (len(pats) > 1 or util.patkind(pats[0], None)[0]) and not destdirexists:
620 621
        raise util.Abort(_('with multiple sources, destination must be an '
                         'existing directory'))
Robin Farine's avatar
Robin Farine committed
622 623 624 625 626
    if opts['after']:
        tfn = targetpathafterfn
    else:
        tfn = targetpathfn
    copylist = []
627
    for pat in pats:
Robin Farine's avatar
Robin Farine committed
628
        srcs = []
629 630
        for tag, abssrc, relsrc, exact in cmdutil.walk(repo, [pat], opts,
                                                       globbed=True):
Robin Farine's avatar
Robin Farine committed
631 632 633
            origsrc = okaytocopy(abssrc, relsrc, exact)
            if origsrc:
                srcs.append((origsrc, abssrc, relsrc, exact))
Robin Farine's avatar
Robin Farine committed
634 635 636 637 638 639
        if not srcs:
            continue
        copylist.append((tfn(pat, dest, srcs), srcs))
    if not copylist:
        raise util.Abort(_('no files to copy'))

Vadim Gelfer's avatar
Vadim Gelfer committed
640 641 642
    for targetpath, srcs in copylist:
        for origsrc, abssrc, relsrc, exact in srcs:
            copy(origsrc, abssrc, relsrc, targetpath(abssrc), exact)
643 644

    if errors:
645
        ui.warn(_('(consider using --after)\n'))
646
    return errors, copied
Bryan O'Sullivan's avatar
Bryan O'Sullivan committed
647 648

def copy(ui, repo, *pats, **opts):
649 650 651 652 653 654 655 656 657 658
    """mark files as copied for the next commit

    Mark dest as having copies of source files.  If dest is a
    directory, copies are put in that directory.  If dest is a file,
    there can only be one source.

    By default, this command copies the contents of files as they
    stand in the working directory.  If invoked with --after, the
    operation is recorded, but no copying is performed.

659 660
    This command takes effect in the next commit. To undo a copy
    before that, see hg revert.
661
    """
662 663
    wlock = repo.wlock(0)
    errs, copied = docopy(ui, repo, pats, opts, wlock)
664
    return errs
mpm@selenic.com's avatar
mpm@selenic.com committed
665

mpm@selenic.com's avatar
mpm@selenic.com committed
666 667
def debugancestor(ui, index, rev1, rev2):
    """find the ancestor revision of two revisions in a given index"""
668
    r = revlog.revlog(util.opener(os.getcwd(), audit=False), index)
mpm@selenic.com's avatar
mpm@selenic.com committed
669 670 671
    a = r.ancestor(r.lookup(rev1), r.lookup(rev2))
    ui.write("%d:%s\n" % (r.rev(a), hex(a)))

672
def debugcomplete(ui, cmd='', **opts):
Benoit Boissinot's avatar
Benoit Boissinot committed
673
    """returns the completion list associated with the given command"""
674 675 676 677 678

    if opts['options']:
        options = []
        otables = [globalopts]
        if cmd:
679
            aliases, entry = cmdutil.findcmd(ui, cmd)
680 681 682 683 684 685 686 687 688
            otables.append(entry[1])
        for t in otables:
            for o in t:
                if o[0]:
                    options.append('-%s' % o[0])
                options.append('--%s' % o[1])
        ui.write("%s\n" % "\n".join(options))
        return

689
    clist = cmdutil.findpossible(ui, cmd).keys()
Benoit Boissinot's avatar
Benoit Boissinot committed
690
    clist.sort()
691
    ui.write("%s\n" % "\n".join(clist))
Benoit Boissinot's avatar
Benoit Boissinot committed
692

693
def debugrebuildstate(ui, repo, rev=""):
694
    """rebuild the dirstate as it would look like for the given revision"""
695
    if rev == "":
696
        rev = repo.changelog.tip()
697 698
    ctx = repo.changectx(rev)
    files = ctx.manifest()
699
    wlock = repo.wlock()
700
    repo.dirstate.rebuild(rev, files)
701

702 703
def debugcheckstate(ui, repo):
    """validate the correctness of the current dirstate"""
704
    parent1, parent2 = repo.dirstate.parents()
Matt Mackall's avatar
Matt Mackall committed
705
    dc = repo.dirstate
706 707
    m1 = repo.changectx(parent1).manifest()
    m2 = repo.changectx(parent2).manifest()
708 709 710 711
    errors = 0
    for f in dc:
        state = repo.dirstate.state(f)
        if state in "nr" and f not in m1:
712
            ui.warn(_("%s in state %s, but not in manifest1\n") % (f, state))
713 714
            errors += 1
        if state in "a" and f in m1:
715
            ui.warn(_("%s in state %s, but also in manifest1\n") % (f, state))
716 717
            errors += 1
        if state in "m" and f not in m1 and f not in m2:
718
            ui.warn(_("%s in state %s, but not in either manifest\n") %
719
                    (f, state))
720 721 722 723
            errors += 1
    for f in m1:
        state = repo.dirstate.state(f)
        if state not in "nrm":
724
            ui.warn(_("%s in manifest1, but listed as state %s") % (f, state))
725 726
            errors += 1
    if errors:
727 728
        error = _(".hg/dirstate inconsistent with current parent's manifest")
        raise util.Abort(error)
729

730
def showconfig(ui, repo, *values, **opts):
731 732 733 734 735 736 737 738 739 740
    """show combined config settings from all hgrc files

    With no args, print names and values of all config items.

    With one arg of the form section.name, print just the value of
    that config item.

    With multiple args, print names and values of all config items
    with matching section names."""

741
    untrusted = bool(opts.get('untrusted'))
742 743 744
    if values:
        if len([v for v in values if '.' in v]) > 1:
            raise util.Abort(_('only one config item permitted'))
745
    for section, name, value in ui.walkconfig(untrusted=untrusted):
746 747 748 749 750 751 752 753 754
        sectname = section + '.' + name
        if values:
            for v in values:
                if v == section:
                    ui.write('%s=%s\n' % (sectname, value))
                elif v == sectname:
                    ui.write(value, '\n')
        else:
            ui.write('%s=%s\n' % (sectname, value))
Bryan O'Sullivan's avatar
Bryan O'Sullivan committed
755

Matt Mackall's avatar
Matt Mackall committed
756
def debugsetparents(ui, repo, rev1, rev2=None):
TK Soh's avatar
TK Soh committed
757
    """manually set the parents of the current working directory
Matt Mackall's avatar
Matt Mackall committed
758 759 760 761 762 763 764 765

    This is useful for writing repository conversion tools, but should
    be used with care.
    """

    if not rev2:
        rev2 = hex(nullid)

766 767 768 769 770
    wlock = repo.wlock()
    try:
        repo.dirstate.setparents(repo.lookup(rev1), repo.lookup(rev2))
    finally:
        wlock.release()
Matt Mackall's avatar
Matt Mackall committed
771

772 773
def debugstate(ui, repo):
    """show the contents of the current dirstate"""
Matt Mackall's avatar
Matt Mackall committed
774 775
    dc = repo.dirstate
    for file_ in dc:
776 777 778 779 780 781 782
        if dc[file_][3] == -1:
            # Pad or slice to locale representation
            locale_len = len(time.strftime("%x %X", time.localtime(0)))
            timestr = 'unset'
            timestr = timestr[:locale_len] + ' '*(locale_len - len(timestr))
        else:
            timestr = time.strftime("%x %X", time.localtime(dc[file_][3]))
783 784
        ui.write("%c %3o %10d %s %s\n"
                 % (dc[file_][0], dc[file_][1] & 0777, dc[file_][2],
785
                    timestr, file_))
786 787
    for f in repo.dirstate.copies():
        ui.write(_("copy: %s -> %s\n") % (repo.dirstate.copied(f), f))
788

789
def debugdata(ui, file_, rev):
790 791
    """dump the contents of a data file revision"""
    r = revlog.revlog(util.opener(os.getcwd(), audit=False), file_[:-2] + ".i")
792 793 794
    try:
        ui.write(r.revision(r.lookup(rev)))
    except KeyError:
795
        raise util.Abort(_('invalid revision identifier %s') % rev)
796

Matt Mackall's avatar
Matt Mackall committed
797
def debugdate(ui, date, range=None, **opts):
Matt Mackall's avatar
Matt Mackall committed
798
    """parse and display a date"""
Matt Mackall's avatar
Matt Mackall committed
799 800 801 802
    if opts["extended"]:
        d = util.parsedate(date, util.extendeddateformats)
    else:
        d = util.parsedate(date)
Matt Mackall's avatar
Matt Mackall committed
803 804
    ui.write("internal: %s %s\n" % d)
    ui.write("standard: %s\n" % util.datestr(d))
Matt Mackall's avatar
Matt Mackall committed
805 806 807
    if range:
        m = util.matchdate(range)
        ui.write("match: %s\n" % m(d[0]))
Matt Mackall's avatar
Matt Mackall committed
808