Differences between revisions 1 and 27 (spanning 26 versions)
Revision 1 as of 2005-09-13 19:00:35
Size: 2448
Editor: ChrisMason
Comment:
Revision 27 as of 2007-01-07 16:38:28
Size: 5163
Editor: TKSoh
Comment: remove extra formatting character
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Mercurial features an extension mechanism for adding new commands. It allows you to create new features and use them directly from the main hg command line.  The contrib directory includes an extension to mimic some git commands under Mercurial. This is named hgit, and will be used as an example here. Mercurial features an extension mechanism for adding new commands. It allows you to create new features and use them directly from the main hg command line.
Line 3: Line 3:
To load an extension, you add it to your .hgrc file. You can either specify an absolute path: {{{ == Bundled extensions ==

The following extensions are distributed with Mercurial.

|| ''Name'' || ''Description'' ||
|| '''acl''' || Hooks to control commit access to parts of a tree. ||
|| '''bisect''' || Extension for binary searching in O(log2(n)) for the changeset introducing a (mis)feature (see UsingBisect for more details). ||
|| '''bugzilla''' || Update Bugzilla bugs when changesets mention them. ||
|| '''churn''' || Graph amount of code changed per author over time. ||
|| '''extdiff''' || Extension for using an external program to diff repository (or selected files). Available since 0.9.1. ||
|| '''fetch''' || Convenience wrapper for pulling and merging. ||
|| '''gpg''' || Extension for signing and checking signatures. ||
|| '''hgk''' || Start {{{hgk}}}, a gitk-like graphical repository browser bundled with Mercurial - see UsingHgk.||
|| '''mq''' || Mercurial Queue management extension - see MqExtension. ||
|| '''notify''' || Template-driven email notifications. - see NotifyExtension. ||
|| '''patchbomb''' || Extension providing the {{{hg email}}} command for sending a collection of Mercurial changesets as a series of patch emails. ||
|| '''win32text''' || Extension for line ending conversion filters for the Windows platform. ||

== Other Extensions ==

|| ''Name'' || ''Source'' || ''Description'' ||
|| '''forest''' || [http://www.terminus.org/hg/hgforest repository] || Extension that provides commands to help working with trees composed of many Mercurial repositories. [http://www.selenic.com/pipermail/mercurial/2006-July/009336.html thread] ||
|| '''transplant''' || [http://hg.kublai.com/mercurial/transplant repository] || Cherry-picking, rebasing and changeset rewriting - see TransplantExtension. ||

== Enabling an extension ==

To load an extension, you add it to the "extensions" section of your [http://www.selenic.com/mercurial/hgrc.5.html .hgrc] file.

Mercurial will scan the default python library path for a file named {{{hgk.py}}} if you set {{{hgk}}} empty:

{{{
Line 5: Line 35:
hgit=/usr/local/lib/hgit hgk=
Line 8: Line 38:
Mercurial can also scan the default python library path for a file named 'hgit': {{{ Extensions are usually located in the hgext directory, in which case you can load them like:

{{{
Line 10: Line 42:
hgit= hgext.patchbomb=
Line 13: Line 45:
hg help will now show the new commands provided by the hgit extension.

To write your own extension, your python module needs to provide a dict with entries describing each command, and a callback named reposetup. reposetup is called after the main Mercurial repository initialization, and can be used to setup any local state the extension might need. Below is an example extension to help demonstrate how things work:
You can also specify an absolute path:
Line 18: Line 48:
[extensions]
hgk=/usr/local/lib/hgk.py
}}}

Extensions can often be configured further in an extension specific section in the same configuration file.


== Writing your own extension ==

To write your own extension, your python module can provide an optional dict named `cmdtable` with entries describing each command, and an optional callback named `reposetup`.
The `reposetup` callback is called after the main Mercurial repository initialization, and can be used to setup any local state the extension might need. Below is an example extension to help demonstrate how things work:

{{{
#!python
Line 40: Line 84:
 # hg.hex will return the full sha1   # hg.hex will return the full sha1
Line 47: Line 91:
    "print-parents": (print_parents,      "print-parents": (print_parents,
Line 55: Line 99:
def reposetup(ui, repo):
    pass
    # extension specific setup can go here
Line 59: Line 100:

If `cmdtable` or `reposetup` is not present, your extension will still work. This means that an extension can work "silently", without making new functionality directly visible through the command line interface.

== Where to put extensions in the source tree ==

As of a change shortly after the 0.7 release, the recommended location for installing extensions in the source tree is the `hgext` directory. If you put a file in there called `foo.py`, you will need to refer to it in the `hgrc` file as a qualified package name, `hgext.foo`.

The contents of the `hgext` directory will be installed by the top-level `setup.py` script along with the rest of Mercurial.
----
CategoryExtension

Mercurial features an extension mechanism for adding new commands. It allows you to create new features and use them directly from the main hg command line.

Bundled extensions

The following extensions are distributed with Mercurial.

Name

Description

acl

Hooks to control commit access to parts of a tree.

bisect

Extension for binary searching in O(log2(n)) for the changeset introducing a (mis)feature (see UsingBisect for more details).

bugzilla

Update Bugzilla bugs when changesets mention them.

churn

Graph amount of code changed per author over time.

extdiff

Extension for using an external program to diff repository (or selected files). Available since 0.9.1.

fetch

Convenience wrapper for pulling and merging.

gpg

Extension for signing and checking signatures.

hgk

Start hgk, a gitk-like graphical repository browser bundled with Mercurial - see UsingHgk.

mq

Mercurial Queue management extension - see MqExtension.

notify

Template-driven email notifications. - see NotifyExtension.

patchbomb

Extension providing the hg email command for sending a collection of Mercurial changesets as a series of patch emails.

win32text

Extension for line ending conversion filters for the Windows platform.

Other Extensions

Name

Source

Description

forest

[http://www.terminus.org/hg/hgforest repository]

Extension that provides commands to help working with trees composed of many Mercurial repositories. [http://www.selenic.com/pipermail/mercurial/2006-July/009336.html thread]

transplant

[http://hg.kublai.com/mercurial/transplant repository]

Cherry-picking, rebasing and changeset rewriting - see TransplantExtension.

Enabling an extension

To load an extension, you add it to the "extensions" section of your [http://www.selenic.com/mercurial/hgrc.5.html .hgrc] file.

Mercurial will scan the default python library path for a file named hgk.py if you set hgk empty:

[extensions]
hgk=

Extensions are usually located in the hgext directory, in which case you can load them like:

[extensions]
hgext.patchbomb=

You can also specify an absolute path:

[extensions]
hgk=/usr/local/lib/hgk.py

Extensions can often be configured further in an extension specific section in the same configuration file.

Writing your own extension

To write your own extension, your python module can provide an optional dict named cmdtable with entries describing each command, and an optional callback named reposetup. The reposetup callback is called after the main Mercurial repository initialization, and can be used to setup any local state the extension might need. Below is an example extension to help demonstrate how things work:

   1 #!/usr/bin/env python
   2 
   3 from mercurial import hg
   4 
   5 # every command must take a ui and and repo as arguments.
   6 # opts is a dict where you can find other command line flags
   7 #
   8 # Other parameters are taken in order from items on the command line that
   9 # don't start with a dash.  If no default value is given in the parameter list,
  10 # they are required.
  11 def print_parents(ui, repo, node, **opts):
  12     # The doc string below will show up in hg help
  13     """Print parent information"""
  14 
  15     # repo.lookup can lookup based on tags, an sha1, or a revision number
  16     node = repo.lookup(node)
  17     parents = repo.changelog.parents(node)
  18 
  19     if opts['short']:
  20         # hg.short will return a smaller portion of the sha1
  21         print "short %s %s" % (hg.short(parents[0]), hg.short(parents[1]))
  22     elif opts['long']:
  23         # hg.hex will return the full sha1
  24         print "long %s %s" % (hg.hex(parents[0]), hg.hex(parents[1]))
  25     else:
  26         print "default %s %s" % (hg.short(parents[0]), hg.short(parents[1]))
  27 
  28 cmdtable = {
  29     # cmd name        function call
  30     "print-parents": (print_parents,
  31                      # see mercurial/fancyopts.py for all of the command
  32                      # flag options.
  33                      [('s', 'short', None, 'print short form'),
  34                       ('l', 'long', None, 'print long form')],
  35                      "hg print-parents [options] node")
  36 }

If cmdtable or reposetup is not present, your extension will still work. This means that an extension can work "silently", without making new functionality directly visible through the command line interface.

Where to put extensions in the source tree

As of a change shortly after the 0.7 release, the recommended location for installing extensions in the source tree is the hgext directory. If you put a file in there called foo.py, you will need to refer to it in the hgrc file as a qualified package name, hgext.foo.

The contents of the hgext directory will be installed by the top-level setup.py script along with the rest of Mercurial.


CategoryExtension

WritingExtensions (last edited 2020-07-29 10:00:07 by aayjaychan)