mercurial/commands.py
changeset 209 63af1db35611
child 210 d2badbd7d1ad
equal deleted inserted replaced
208:0a37e9c8ad6c 209:63af1db35611
       
     1 import os, re
       
     2 from mercurial import fancyopts, ui, hg
       
     3 
       
     4 class UnknownCommand(Exception): pass
       
     5 
       
     6 def relpath(repo, args):
       
     7     if os.getcwd() != repo.root:
       
     8         p = os.getcwd()[len(repo.root) + 1: ]
       
     9         return [ os.path.join(p, x) for x in args ]
       
    10     return args
       
    11     
       
    12 def help(ui, args):
       
    13     ui.status("""\
       
    14  hg commands:
       
    15 
       
    16  add [files...]        add the given files in the next commit
       
    17  addremove             add all new files, delete all missing files
       
    18  annotate [files...]   show changeset number per file line
       
    19  branch <path>         create a branch of <path> in this directory
       
    20  checkout [changeset]  checkout the latest or given changeset
       
    21  commit                commit all changes to the repository
       
    22  diff [files...]       diff working directory (or selected files)
       
    23  dump <file> [rev]     dump the latest or given revision of a file
       
    24  dumpmanifest [rev]    dump the latest or given revision of the manifest
       
    25  export <rev>          dump the changeset header and diffs for a revision
       
    26  history               show changeset history
       
    27  init                  create a new repository in this directory
       
    28  log <file>            show revision history of a single file
       
    29  merge <path>          merge changes from <path> into local repository
       
    30  recover               rollback an interrupted transaction
       
    31  remove [files...]     remove the given files in the next commit
       
    32  serve                 export the repository via HTTP
       
    33  status                show new, missing, and changed files in working dir
       
    34  tags                  show current changeset tags
       
    35  undo                  undo the last transaction
       
    36 """)
       
    37 
       
    38 def init(ui, args):
       
    39     """create a repository"""
       
    40     hg.repository(ui, ".", create=1)
       
    41 
       
    42 def checkout(u, repo, args):
       
    43     node = repo.changelog.tip()
       
    44     if args:
       
    45         node = repo.lookup(args[0])
       
    46     repo.checkout(node)
       
    47 
       
    48 def annotate(u, repo, args, **ops):
       
    49     if not args:
       
    50         return
       
    51         
       
    52     def getnode(rev):
       
    53         return hg.short(repo.changelog.node(rev))
       
    54 
       
    55     def getname(rev):
       
    56         try:
       
    57             return bcache[rev]
       
    58         except KeyError:
       
    59             cl = repo.changelog.read(repo.changelog.node(rev))
       
    60             name = cl[1]
       
    61             f = name.find('@')
       
    62             if f >= 0:
       
    63                 name = name[:f]
       
    64             bcache[rev] = name
       
    65             return name
       
    66     
       
    67     bcache = {}
       
    68     opmap = [['user', getname], ['number', str], ['changeset', getnode]]
       
    69     if not ops['user'] and not ops['changeset']:
       
    70         ops['number'] = 1
       
    71 
       
    72     args = relpath(repo, args)
       
    73     node = repo.current
       
    74     if ops['revision']:
       
    75         node = repo.changelog.lookup(ops['revision'])
       
    76     change = repo.changelog.read(node)
       
    77     mmap = repo.manifest.read(change[0])
       
    78     maxuserlen = 0
       
    79     maxchangelen = 0
       
    80     for f in args:
       
    81         lines = repo.file(f).annotate(mmap[f])
       
    82         pieces = []
       
    83 
       
    84         for o, f in opmap:
       
    85             if ops[o]:
       
    86                 l = [ f(n) for n,t in lines ]
       
    87                 m = max(map(len, l))
       
    88                 pieces.append([ "%*s" % (m, x) for x in l])
       
    89 
       
    90         for p,l in zip(zip(*pieces), lines):
       
    91             u.write(" ".join(p) + ": " + l[1])
       
    92 
       
    93 table = {
       
    94     "init": (init, [], 'hg init'),
       
    95     "help": (help, [], 'hg init'),
       
    96     "checkout|co": (checkout, [], 'hg init'),
       
    97     "ann|annotate": (annotate,
       
    98                      [('r', 'revision', '', 'revision'),
       
    99                       ('u', 'user', None, 'show user'),
       
   100                       ('n', 'number', None, 'show revision number'),
       
   101                       ('c', 'changeset', None, 'show changeset')],
       
   102                      'hg annotate [-u] [-c] [-n] [-r id] [files]'),
       
   103     }
       
   104 
       
   105 norepo = "init branch help"
       
   106 
       
   107 def dispatch(args):
       
   108     options = {}
       
   109     opts = [('v', 'verbose', None, 'verbose'),
       
   110             ('d', 'debug', None, 'debug'),
       
   111             ('q', 'quiet', None, 'quiet'),
       
   112             ('y', 'noninteractive', None, 'run non-interactively'),
       
   113             ]
       
   114 
       
   115     args = fancyopts.fancyopts(args, opts, options,
       
   116                                'hg [options] <command> [options] [files]')
       
   117 
       
   118     if not args:
       
   119         cmd = "help"
       
   120     else:
       
   121         cmd, args = args[0], args[1:]
       
   122 
       
   123     u = ui.ui(options["verbose"], options["debug"], options["quiet"],
       
   124            not options["noninteractive"])
       
   125 
       
   126     i = None
       
   127     for e in table.keys():
       
   128         if re.match(e + "$", cmd):
       
   129             i = table[e]
       
   130 
       
   131     # deal with this internally later
       
   132     if not i: raise UnknownCommand(cmd)
       
   133 
       
   134     cmdoptions = {}
       
   135     args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2])
       
   136 
       
   137     if cmd not in norepo.split():
       
   138         repo = hg.repository(ui = u)
       
   139         d = lambda: i[0](u, repo, args, **cmdoptions)
       
   140     else:
       
   141         d = lambda: i[0](u, args, **cmdoptions)
       
   142 
       
   143     try:
       
   144         d()
       
   145     except KeyboardInterrupt:
       
   146         u.warn("interrupted!\n")