Source

hgopenurl / hgopenurl / __init__.py

'''quickly show the web address for a repository

hgopenurl provides two commands: ``showurl`` and ``openurl``. ``showurl``
writes the web address to standard output. ``openurl`` opens the repository
web site in your web browser using the Python webbrowser module.
'''

import os
import re
import sys
import webbrowser

from mercurial import hg
from mercurial import node
from mercurial import util

from mercurial import demandimport

demandimport.ignore.append('ic')

def getpath(ui, repo, filename, path, rev):
    if path is None:
        path = 'default'

    path = repo.ui.expandpath(path)

    # disabled for now
    if False and rev and rev in repo:
        rev = node.short(repo[rev].node())

    gcode = re.match(
        '^https?://([-a-z0-9]+).googlecode.com/hg/?$',
        path, re.IGNORECASE)
    bb = re.match(
        '^((ssh://(hg@)?)|https?://)bitbucket.org/'
        '([-a-z0-9]+)/([-a-z0-9]+)(/\.hg/patches)?/?$',
        path, re.IGNORECASE
    )
    fallbackhttp = re.match('^https?://', path)
    fallbackssh = re.match('^ssh://', path)

    if gcode:
        ui.debug('this is a Google Code repository\n')
        repoid = gcode.group(1)
        if filename:
            url = 'https://code.google.com/p/%s/source/browse/%s' % (
                repoid, filename,
            )
            if rev:
                url += '?rev=%s' % rev
        elif rev:
            url = 'https://code.google.com/p/%s/source/detail?r=%s' % (
                repoid, rev,
            )
        else:
            url = 'https://code.google.com/p/%s/source/list' % repoid
    elif bb:
        ui.debug('this is a BitBucket repository\n')
        repoid = '%s/%s' % bb.groups()[3:5]
        if filename:
            url = 'https://bitbucket.org/%s/src/%s/%s' % (
                repoid, rev or 'default', filename,
            )
            if rev:
                url += '?rev=%s' % rev
        elif rev:
            url = 'https://bitbucket.org/%s/changeset/%s' % (repoid, rev)
        else:
            url = 'https://bitbucket.org/%s/changesets' % repoid
    elif fallbackhttp:
        ui.note('assuming standard hgweb repository')
        url = path.rstrip('/')
        if filename:
            url = '/file/%s/%s' % (
                rev or 'default', filename,
            )
        elif rev:
            url += '/rev/%s' % rev
    elif fallbackssh:
        raise util.Abort("don't know how to convert regular SSH repositories "
                         "into an web address")
    else:
        raise util.Abort("unsupported URL schema for path %r" % path)

    return url
    
def showurl(ui, repo, filename=None, path=None, rev=None):
    '''print the web URL of the repository.

    If the -r/--rev argument is given, show the address of the changelog for
    that revision. Otherwise, the address of the repository changelog is shown.
    '''
    ui.write('%s\n' % getpath(ui, repo, filename, path, rev))

def openurl(ui, repo, filename=None, path=None, rev=None):
    '''open the URL of the repository in a web browser.

    If the -r/--rev argument is given, open the address of the changelog for
    that revision. Otherwise, the address of the repository changelog is opened.
    '''

    url = getpath(ui, repo, filename, path, rev)

    ui.note('opening %r\n' % url)
    webbrowser.open(url)

def uisetup(ui):
    pass

def reposetup(ui, repo):
    pass

def extsetup(ui):
    pass

cmdtable = {
    "showurl|su":
        (showurl,
         [
          ('r', 'rev', '', 'revision to link to', 'REV'),
         ],
         'hg showurl [-r REV] [FILE [DEST]]',
         ),
    "openurl|ou":
        (openurl,
         [
          ('r', 'rev', '', 'revision to link to', 'REV'),
         ],
         'hg openurl [-r REV] [FILE [DEST]]',
         ),
}