hgpaste / hgpaste.py

# -*- coding: utf-8 -*-
#
# paste mercurial extension
#
# Copyright (c) 2009, 2010 by Georg Brandl, Ronny Pfannschmidt.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""quickly paste a diff to a LodgeIt instance"""

import sys

from mercurial import commands, cmdutil, patch

def copy_to_clipboard(url):
    """Copy the url into the clipboard."""
    # try windows first
    try:
        import win32clipboard
    except ImportError:
        # then give pbcopy a try.  do that before gtk because
        # gtk might be installed on os x but nobody is interested
        # in the X11 clipboard there.
        from subprocess import Popen, PIPE
        for prog in 'pbcopy', 'xclip':
            try:
                client = Popen([prog], stdin=PIPE)
            except OSError:
                continue
            else:
                client.stdin.write(url)
                client.stdin.close()
                client.wait()
                break
        else:
            # keep gtk's import magic from fail
            from mercurial import demandimport
            demandimport.ignore.append('gobject._gobject')
            try:
                import pygtk
                pygtk.require('2.0')
                import gtk
                import gobject
            except ImportError:
                return
            gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(url)
            gobject.idle_add(gtk.main_quit)
            gtk.main()
    else:
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardText(url)
        win32clipboard.CloseClipboard()

def paste(ui, repo, *pats, **opts):
    """paste current working directory changes or a specified changeset

    Treat the all arguments as for the
    "hg diff" command and paste the resulting diff.

    The paste is done on http://paste.pocoo.org/ by default, but a
    different server with a compatible XMLRPC interface can be selected
    in the configuration (add a "server" value in a "paste" section).

    By default, the pasted text is also copied to the system clipboard;
    the -n option prevents that.
    """
    import xmlrpclib
    server = ui.config('paste', 'server', 'http://paste.pocoo.org/')
    if not server.endswith('/'):
        server += '/'
    proxy = xmlrpclib.ServerProxy(server + 'xmlrpc/', allow_none=True)

    opts['color'] = 'never'
    ui.pushbuffer()
    commands.diff(ui, repo, *pats, **opts)
    content = ui.popbuffer()
    if not content:
        ui.warn('Error: nothing to paste.\n')
        return

    rv = proxy.pastes.newPaste('diff', content, None, '', '', bool(opts.get('private')))
    if not rv:
        ui.warn('Could not create paste; server returned failure.\n')
    else:
        url = '%sshow/%s/' % (server, rv)
        ui.status(url + '\n')
        if not opts['nocopy']:
            copy_to_clipboard(url)
        if opts['browser']:
            import webbrowser
            webbrowser.open(url)

cmdtable = {
    '^paste': (
        paste,
        commands.table['^diff'][1] + [
            ('b', 'browser', None, 'open url in web browser'),
            ('n', 'nocopy', None, 'do not copy to clipboard'),
            ('', 'private', None, 'create a private paste'),
        ],
        commands.table['^diff'][2],
    )
}
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.