comparison mercurial/cmdutil.py @ 2873:4ec58b157265

refactor text diff/patch code. rename commands.dodiff to patch.diff. rename commands.doexport to patch.export. move some functions from commands to new mercurial.cmdutil module. turn list of diff options into mdiff.diffopts class. patch.diff and patch.export now has clean api for call from 3rd party python code.
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Sat, 12 Aug 2006 16:13:27 -0700
parents
children cf98cd70d2c4
comparison
equal deleted inserted replaced
2872:5dd6631c8238 2873:4ec58b157265
1 # commands.py - command processing for mercurial
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 #
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
7
8 from demandload import demandload
9 from node import *
10 from i18n import gettext as _
11 demandload(globals(), 'os sys')
12
13 def make_filename(repo, pat, node,
14 total=None, seqno=None, revwidth=None, pathname=None):
15 node_expander = {
16 'H': lambda: hex(node),
17 'R': lambda: str(repo.changelog.rev(node)),
18 'h': lambda: short(node),
19 }
20 expander = {
21 '%': lambda: '%',
22 'b': lambda: os.path.basename(repo.root),
23 }
24
25 try:
26 if node:
27 expander.update(node_expander)
28 if node and revwidth is not None:
29 expander['r'] = (lambda:
30 str(repo.changelog.rev(node)).zfill(revwidth))
31 if total is not None:
32 expander['N'] = lambda: str(total)
33 if seqno is not None:
34 expander['n'] = lambda: str(seqno)
35 if total is not None and seqno is not None:
36 expander['n'] = lambda:str(seqno).zfill(len(str(total)))
37 if pathname is not None:
38 expander['s'] = lambda: os.path.basename(pathname)
39 expander['d'] = lambda: os.path.dirname(pathname) or '.'
40 expander['p'] = lambda: pathname
41
42 newname = []
43 patlen = len(pat)
44 i = 0
45 while i < patlen:
46 c = pat[i]
47 if c == '%':
48 i += 1
49 c = pat[i]
50 c = expander[c]()
51 newname.append(c)
52 i += 1
53 return ''.join(newname)
54 except KeyError, inst:
55 raise util.Abort(_("invalid format spec '%%%s' in output file name"),
56 inst.args[0])
57
58 def make_file(repo, pat, node=None,
59 total=None, seqno=None, revwidth=None, mode='wb', pathname=None):
60 if not pat or pat == '-':
61 return 'w' in mode and sys.stdout or sys.stdin
62 if hasattr(pat, 'write') and 'w' in mode:
63 return pat
64 if hasattr(pat, 'read') and 'r' in mode:
65 return pat
66 return open(make_filename(repo, pat, node, total, seqno, revwidth,
67 pathname),
68 mode)