##// END OF EJS Templates
help: merge section about uisetup() and extsetup()...
Yuya Nishihara -
r40632:252396a6 default
parent child Browse files
Show More
@@ -1,321 +1,314 b''
1 1 Extensions allow the creation of new features and using them directly from
2 2 the main hg command line as if they were built-in commands. The extensions
3 3 have full access to the *internal* API.
4 4
5 5 Use of Mercurial's internal API very likely makes your code subject to
6 6 Mercurial's license. Before going any further, read the License page.
7 7
8 8 There are NO guarantees that third-party code calling into Mercurial's
9 9 internals won't break from release to release. If you do use Mercurial's API
10 10 for published third-party code, we expect you to test your code before each
11 11 major Mercurial release. This will prevent various bug reports from your users
12 12 when they upgrade their copy of Mercurial.
13 13
14 14 File Layout
15 15 ===========
16 16
17 17 Extensions are usually written as simple python modules. Larger ones are
18 18 better split into multiple modules of a single package (see the convert
19 19 extension). The package root module gives its name to the extension and
20 20 implements the ``cmdtable`` and optional callbacks described below.
21 21
22 22 Command table
23 23 =============
24 24
25 25 To write your own extension, your python module can provide an optional dict
26 26 named ``cmdtable`` with entries describing each command. A command should be
27 27 registered to the ``cmdtable`` by ``@command`` decorator.
28 28
29 29 Example using ``@command`` decorator (requires Mercurial 1.9)::
30 30
31 31 from mercurial import cmdutil
32 32 from mercurial.i18n import _
33 33
34 34 cmdtable = {}
35 35 command = cmdutil.command(cmdtable)
36 36
37 37 @command('print-parents',
38 38 [('s', 'short', None, _('print short form')),
39 39 ('l', 'long', None, _('print long form'))],
40 40 _('[options] node'))
41 41 def printparents(ui, repo, node, **opts):
42 42 ...
43 43
44 44 The cmdtable dictionary
45 45 -----------------------
46 46
47 47 The ``cmdtable`` dictionary uses as key the new command names, and, as value,
48 48 a tuple containing:
49 49
50 50 1. the function to be called when the command is used.
51 51 2. a list of options the command can take.
52 52 3. a command line synopsis for the command (the function docstring is used for
53 53 the full help).
54 54
55 55 List of options
56 56 ---------------
57 57
58 58 All the command flag options are documented in the mercurial/fancyopts.py
59 59 sources.
60 60
61 61 The options list is a list of tuples containing:
62 62
63 63 1. the short option letter, or ``''`` if no short option is available
64 64 (for example, ``o`` for a ``-o`` option).
65 65 2. the long option name (for example, ``option`` for a ``--option`` option).
66 66 3. a default value for the option.
67 67 4. a help string for the option (it's possible to omit the "hg newcommand"
68 68 part and only the options and parameter substring is needed).
69 69
70 70 Command function signatures
71 71 ---------------------------
72 72
73 73 Functions that implement new commands always receive a ``ui`` and usually
74 74 a ``repo`` parameter. The rest of parameters are taken from the command line
75 75 items that don't start with a dash and are passed in the same order they were
76 76 written. If no default value is given in the parameter list they are required.
77 77
78 78 If there is no repo to be associated with the command and consequently no
79 79 ``repo`` passed, then ``norepo=True`` should be passed to the ``@command``
80 80 decorator::
81 81
82 82 @command('mycommand', [], norepo=True)
83 83 def mycommand(ui, **opts):
84 84 ...
85 85
86 86 For examples of ``norepo``, see the convert extension.
87 87
88 88 Command function docstrings
89 89 ===========================
90 90
91 91 The docstring of your function is used as the main help text, shown by
92 92 ``hg help mycommand``. The docstring should be formatted using a simple
93 93 subset of reStructuredText markup. The supported constructs include:
94 94
95 95 Paragraphs::
96 96
97 97 This is a paragraph.
98 98
99 99 Paragraphs are separated
100 100 by blank lines.
101 101
102 102 A verbatim block is introduced with a double colon followed by an indented
103 103 block. The double colon is turned into a single colon on display::
104 104
105 105 Some text::
106 106
107 107 verbatim
108 108 text
109 109 !!
110 110
111 111 We have field lists::
112 112
113 113 :key1: value1
114 114 :key2: value2
115 115
116 116 Bullet lists::
117 117
118 118 - foo
119 119 - bar
120 120
121 121 Enumerated lists::
122 122
123 123 1. foo
124 124 2. bar
125 125
126 126 Inline markup::
127 127
128 128 ``*bold*``, ``monospace``, :hg:`command`
129 129
130 130 Mark Mercurial commands with ``:hg:`` to make a nice link to the corresponding
131 131 documentation. We'll expand the support if new constructs can be parsed
132 132 without too much trouble.
133 133
134 134 Communicating with the user
135 135 ===========================
136 136
137 137 Besides the ``ui`` methods, like ``ui.write(*msg)`` or
138 138 ``ui.prompt(msg, default="y")``, an extension can add help text for each
139 139 of its commands and the extension itself.
140 140
141 141 The module docstring will be used as help string when ``hg help extensionname``
142 142 is used and, similarly, the help string for a command and the docstring
143 143 belonging to the function that's wrapped by the command will be shown when
144 144 ``hg help command`` is invoked.
145 145
146 146 Setup Callbacks
147 147 ===============
148 148
149 149 Extensions are loaded in phases. All extensions are processed in a given phase
150 150 before the next phase begins. In the first phase, all extension modules are
151 151 loaded and registered with Mercurial. This means that you can find all enabled
152 152 extensions with ``extensions.find`` in the following phases.
153 153
154 ui setup
155 --------
154 Extension setup
155 ---------------
156 156
157 Extensions can implement an optional callback named ``uisetup``. ``uisetup``
158 is called when the extension is first loaded and receives a ui object::
157 There are two callbacks to be called when extensions are loaded, named
158 ``uisetup`` and ``extsetup``. ``uisetup`` is called first for each extension,
159 then ``extsetup`` is called. This means ``extsetup`` can be useful in case
160 one extension optionally depends on another extension.
161
162 Both ``uisetup`` and ``extsetup`` receive a ui object::
159 163
160 164 def uisetup(ui):
161 165 # ...
162 166
163 Extension setup
164 ---------------
165
166 Extensions can implement an optional callback named ``extsetup``. It is
167 called after all the extension are loaded, and can be useful in case one
168 extension optionally depends on another extension. Signature::
169
170 def extsetup():
167 def extsetup(ui):
171 168 # ...
172 169
173 Mercurial version 8e6019b16a7d and later (that is post-1.3.1) will pass
174 a ``ui``` argument to ``extsetup``::
175
176 def extsetup(ui):
177 # ...
170 In Mercurial 1.3.1 or earlier, ``extsetup`` takes no argument.
178 171
179 172 Command table setup
180 173 -------------------
181 174
182 175 After ``extsetup``, the ``cmdtable`` is copied into the global command table
183 176 in Mercurial.
184 177
185 178 Repository setup
186 179 ----------------
187 180
188 181 Extensions can implement an optional callback named ``reposetup``. It is
189 182 called after the main Mercurial repository initialization, and can be used
190 183 to setup any local state the extension might need.
191 184
192 185 As other command functions it receives an ``ui`` object and a ``repo`` object
193 186 (no additional parameters for this, though)::
194 187
195 188 def reposetup(ui, repo):
196 189 #do initialization here.
197 190
198 191 It is important to take into account that the ``ui`` object that is received
199 192 by the ``reposetup`` function is not the same as the one received by the
200 193 ``uisetup`` and ``extsetup`` functions. This is particularly important when
201 194 setting up hooks as described in the following section, since not all hooks
202 195 use the same ``ui`` object and hence different hooks must be configured in
203 196 different setup functions.
204 197
205 198 Wrapping methods on the ui and repo classes
206 199 -------------------------------------------
207 200
208 201 Because extensions can be loaded *per repository*, you should avoid using
209 202 ``extensions.wrapfunction()`` on methods of the ``ui`` and ``repo`` objects.
210 203 Instead, create a subclass of the specific class of the instance passed into
211 204 the ``*setup()`` hook; e.g. use ``ui.__class__`` as the base class, then
212 205 reassign your new class to ``ui.__class__`` again. Mercurial will then use
213 206 your updated ``ui`` or ``repo`` instance only for repositories where your
214 207 extension is enabled (or copies thereof, reusing your new class).
215 208
216 209 For example::
217 210
218 211 def uisetup(ui):
219 212 class echologui(ui.__class__):
220 213 def log(self, service, *msg, **opts):
221 214 if msg:
222 215 self.write('%s: %s\n' % (service, msg[0] % msg[1:]))
223 216 super(echologui, self).log(service, *msg, **opts)
224 217
225 218 ui.__class__ = echologui
226 219
227 220 Configuring Hooks
228 221 =================
229 222
230 223 Some extensions must use hooks to do their work. These required hooks can
231 224 be configured manually by the user by modifying the ``[hook]`` section of
232 225 their hgrc, but they can also be configured automatically by calling the
233 226 ``ui.setconfig('hooks', ...)`` function in one of the setup functions
234 227 described above.
235 228
236 229 The main difference between manually modifying the hooks section in the hgrc
237 230 and using ``ui.setconfig()`` is that when using ``ui.setconfig()`` you have
238 231 access to the actual hook function object, which you can pass directly to
239 232 ``ui.setconfig()``, while when you use the hooks section of the hgrc file
240 233 you must refer to the hook function by using the
241 234 ``python:modulename.functioname`` idiom (e.g. ``python:hgext.notify.hook``).
242 235
243 236 For example::
244 237
245 238 # Define hooks -- note that the actual function name it irrelevant.
246 239 def preupdatehook(ui, repo, **kwargs):
247 240 ui.write("Pre-update hook triggered\n")
248 241
249 242 def updatehook(ui, repo, **kwargs):
250 243 ui.write("Update hook triggered\n")
251 244
252 245 def uisetup(ui):
253 246 # When pre-<cmd> and post-<cmd> hooks are configured by means of
254 247 # the ui.setconfig() function, you must use the ui object passed
255 248 # to uisetup or extsetup.
256 249 ui.setconfig("hooks", "pre-update.myextension", preupdatehook)
257 250
258 251 def reposetup(ui, repo):
259 252 # Repository-specific hooks can be configured here. These include
260 253 # the update hook.
261 254 ui.setconfig("hooks", "update.myextension", updatehook)
262 255
263 256 Note how different hooks may need to be configured in different setup
264 257 functions. In the example you can see that the ``update`` hook must be
265 258 configured in the ``reposetup`` function, while the ``pre-update`` hook
266 259 must be configured on the ``uisetup`` or the ``extsetup`` functions.
267 260
268 261 Marking compatible versions
269 262 ===========================
270 263
271 264 Every extension should use the ``testedwith`` variable to specify Mercurial
272 265 releases it's known to be compatible with. This helps us and users diagnose
273 266 where problems are coming from::
274 267
275 268 testedwith = '2.0 2.0.1 2.1 2.1.1 2.1.2'
276 269
277 270 Do not use the ``internal`` marker in third-party extensions; we will
278 271 immediately drop all bug reports mentioning your extension if we catch you
279 272 doing this.
280 273
281 274 Similarly, an extension can use the ``buglink`` variable to specify how users
282 275 should report issues with the extension. This link will be included in the
283 276 error message if the extension produces errors::
284 277
285 278 buglink = 'https://bitbucket.org/USER/REPO/issues'
286 279
287 280 Wrap up: what belongs where?
288 281 ============================
289 282
290 283 You will find here a list of most common tasks, based on setups from the
291 284 extensions included in Mercurial core.
292 285
293 286 uisetup
294 287 -------
295 288
296 289 * Changes to ``ui.__class__`` . The ``ui`` object that will be used to run
297 290 the command has not yet been created. Changes made here will affect ``ui``
298 291 objects created after this, and in particular the ``ui`` that will be passed
299 292 to ``runcommand``
300 293 * Command wraps (``extensions.wrapcommand``)
301 294 * Changes that need to be visible by other extensions: because initialization
302 295 occurs in phases (all extensions run ``uisetup``, then all run ``extsetup``),
303 296 a change made here will be visible by other extensions during ``extsetup``.
304 297 * Monkeypatches or function wraps (``extensions.wrapfunction``) of ``dispatch``
305 298 module members
306 299 * Setup of ``pre-*`` and ``post-*`` hooks
307 300 * ``pushkey`` setup
308 301
309 302 extsetup
310 303 --------
311 304
312 305 * Changes depending on the status of other extensions. (``if extensions.find('mq')``)
313 306 * Add a global option to all commands
314 307 * Extend revsets
315 308
316 309 reposetup
317 310 ---------
318 311
319 312 * All hooks but ``pre-*`` and ``post-*``
320 313 * Modify configuration variables
321 314 * Changes to ``repo.__class__``, ``repo.dirstate.__class__``
General Comments 0
You need to be logged in to leave comments. Login now