Marc Simpson avatar Marc Simpson committed bbf7304

Initial commit of persona extension (with wrapper for clone)

Comments (0)

Files changed (2)

+syntax: glob
+*.pyc
+#!/usr/bin/env python
+
+'''persona
+
+This extension provides an easy way to set ui.username on a
+repository by repository basis.
+
+At its simplest,
+
+    hg persona -n "Firstname Lastname <firstname@domain.com>"
+
+You can configure individual personas in your ~/.hgrc as follows:
+
+    [persona]
+    home        = Firstname Lastname <firstname@home.domain>
+    work        = Firstname Lastname <firstname.lastname@work.domain>
+
+which allows for quick switching:
+
+    hg persona -n personal
+    hg persona -n work
+
+To list known personas,
+
+    hg persona
+
+You can also specify a persona on clone:
+
+    hg clone --persona work src dest
+'''
+
+from mercurial.i18n import _
+from mercurial import hg, ui, cmdutil, config, util
+from mercurial import extensions, commands, localrepo
+from mercurial import error
+import os, shutil
+
+cmdtable = {}
+command = cmdutil.command(cmdtable)
+
+@command('persona',
+        [('n', 'name', '', _('repository local username'), _('PERSONA'))],
+        _('hg persona [-n PERSONA]'))
+def persona(ui, repo, *args, **opts):
+    '''Specify or list repository-local persona(s).'''
+    if args:
+        raise util.Abort(_("Use the -n switch to specify a new persona."))
+    if opts["name"]:
+        set_persona(ui, repo, opts["name"])
+    else:
+        show_personas(ui)
+
+def show_personas(ui):
+    personas = ui.configitems("persona")
+    ui.write(_("Known personas:\n"))
+    for persona in personas:
+        p, username = persona[0].ljust(12), persona[1]
+        ui.write("  %s %s\n" % (p, username))
+    ui.write(_("Current username:\n"))
+    ui.write("  %s\n" % ui.config("ui", "username"))
+
+def set_persona(ui, repo, name):
+    hgrc = repo.path + os.sep + "hgrc"
+    cfg = config.config(None)
+    try:
+        cfg.read(hgrc)
+    except IOError:
+        pass
+
+    username = lookup_persona(ui, name)
+    ui.write(_("~ Setting username to '%s'\n" % username))
+
+    cfg.set("ui", "username", username)
+    _writecfg(cfg, hgrc)
+
+def lookup_persona(ui, name):
+    persona = ui.config("persona", name)
+    if not persona:
+        ui.write(_("Persona not found; treating argument as a username.\n"))
+        persona = name
+    return persona
+
+# XXX will re-order sections and remove comments from your repository's hgrc
+def _writecfg(cfg, hgrc):
+    first = True
+    try:
+        shutil.copy(hgrc, hgrc + "_bak")
+    except IOError:
+        pass
+    fp = open(hgrc, "w")
+    for s in cfg.sections():
+        if not first:
+            fp.write("\n")
+        first = False
+        fp.write("[%s]\n" % s)
+        for i in cfg.items(s):
+            fp.write("%s = %s\n" % (i[0], i[1]))
+
+def clone_wrapper(orig, ui, repo, *args, **opts):
+    res = orig(ui, repo, *args, **opts)
+    if args:
+        try:
+            target = localrepo.localrepository(ui, args[0])
+        except error.RepoError:
+            raise util.Abort(_("Can't write persona (is this a local repo?)"))
+        if opts["persona"]:
+            persona(ui, target, name=opts["persona"])
+
+def uisetup(ui):
+    entry = extensions.wrapcommand(commands.table, "clone", clone_wrapper)
+    entry[1].append(('', 'persona', '', _("Clone with persona"), _("PERSONA")))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.