Kiln » TortoiseHg » TortoiseHg
Clone URL:  
hglib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# hglib.py - Mercurial API wrappers for TortoiseHg # # Copyright 2007 Steve Borho <steve@borho.org> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. import os import sys import traceback import shlib import time from mercurial import hg, ui, util, extensions, commands, hook from i18n import _ import paths from mercurial.error import RepoError, ParseError, LookupError from mercurial.error import UnknownCommand, AmbiguousCommand from mercurial import dispatch, encoding, util _encoding = encoding.encoding _encodingmode = encoding.encodingmode _fallbackencoding = encoding.fallbackencoding try: # post 1.1.2 from mercurial import util hgversion = util.version() except AttributeError: # <= 1.1.2 from mercurial import version hgversion = version.get_version() def toutf(s): """ Convert a string to UTF-8 encoding Based on mercurial.util.tolocal() """ for e in ('utf-8', _encoding): try: return s.decode(e, 'strict').encode('utf-8') except UnicodeDecodeError: pass return s.decode(_fallbackencoding, 'replace').encode('utf-8') def fromutf(s): """ Convert UTF-8 encoded string to local. It's primarily used on strings converted to UTF-8 by toutf(). """ try: return s.decode('utf-8').encode(_encoding) except UnicodeDecodeError: pass except UnicodeEncodeError: pass return s.decode('utf-8').encode(_fallbackencoding) _tabwidth = None def gettabwidth(ui): global _tabwidth if _tabwidth is not None: return _tabwidth tabwidth = ui.config('tortoisehg', 'tabwidth') try: tabwidth = int(tabwidth) if tabwidth < 1 or tabwidth > 16: tabwidth = 0 except (ValueError, TypeError): tabwidth = 0 _tabwidth = tabwidth return tabwidth _maxdiff = None def getmaxdiffsize(ui): global _maxdiff if _maxdiff is not None: return _maxdiff maxdiff = ui.config('tortoisehg', 'maxdiff') try: maxdiff = int(maxdiff) if maxdiff < 1: maxdiff = sys.maxint except (ValueError, TypeError): maxdiff = 1024 # 1MB by default _maxdiff = maxdiff * 1024 return _maxdiff def diffexpand(line): 'Expand tabs in a line of diff/patch text' if _tabwidth is None: gettabwidth(ui.ui()) if not _tabwidth or len(line) < 2: return line return line[0] + line[1:].expandtabs(_tabwidth) def uiwrite(u, args): ''' write args if there are buffers returns True if the caller shall handle writing ''' if u._buffers: ui.ui.write(u, *args) return False return True def invalidaterepo(repo): repo.invalidate() repo.dirstate.invalidate() if 'mq' in repo.__dict__: #do not create if it does not exist repo.mq.invalidate() def canonpaths(list): 'Get canonical paths (relative to root) for list of files' # This is a horrible hack. Please remove this when HG acquires a # decent case-folding solution. canonpats = [] cwd = os.getcwd() root = paths.find_root(cwd) for f in list: try: canonpats.append(util.canonpath(root, cwd, f)) except util.Abort: # Attempt to resolve case folding conflicts. fu = f.upper() cwdu = cwd.upper() if fu.startswith(cwdu): canonpats.append(util.canonpath(root, cwd, f[len(cwd+os.sep):])) else: # May already be canonical canonpats.append(f) return canonpats def hgcmd_toq(path, q, *args): ''' Run an hg command in a background thread, pipe all output to a Queue object. Assumes command is completely noninteractive. ''' class Qui(ui.ui): def __init__(self, src=None): super(Qui, self).__init__(src) self.setconfig('ui', 'interactive', 'off') def write(self, *args): if uiwrite(self, args): for a in args: q.put(str(a)) u = Qui() for k, v in u.configitems('defaults'): u.setconfig('defaults', k, '') return dispatch._dispatch(u, list(args)) def displaytime(date): return util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2') def utctime(date): return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(date[0]))