"""\
Various useful plugins for working with bzr.
"""

import bzrlib


__version__ = '0.12.0'


version_info = tuple(int(n) for n in __version__.split('.'))


def check_bzrlib_version(desired):
    """Check that bzrlib is compatible.

    If version is < bzrtools version, assume incompatible.
    If version == bzrtools version, assume completely compatible
    If version == bzrtools version + 1, assume compatible, with deprecations
    Otherwise, assume incompatible.
    """
    desired_plus = (desired[0], desired[1]+1)
    bzrlib_version = bzrlib.version_info[:2]
    if bzrlib_version == desired:
        return
    try:
        from bzrlib.trace import warning
    except ImportError:
        # get the message out any way we can
        from warnings import warn as warning
    if bzrlib_version < desired:
        warning('Installed bzr version %s is too old to be used with bzrtools'
                ' %s.' % (bzrlib.__version__, __version__))
        # Not using BzrNewError, because it may not exist.
        raise Exception, ('Version mismatch', version_info)
    else:
        warning('Bzrtools is not up to date with installed bzr version %s.'
                ' \nThere should be a newer version available, e.g. %i.%i.' 
                % (bzrlib.__version__, bzrlib_version[0], bzrlib_version[1]))
        if bzrlib_version != desired_plus:
            raise Exception, 'Version mismatch'


check_bzrlib_version(version_info[:2])


import rspush
from errors import CommandError, NoPyBaz
from patchsource import BzrPatchSource
from shelf import Shelf
from switch import cmd_switch
import sys
import os.path

import bzrlib.builtins
import bzrlib.branch
import bzrlib.commands
from bzrlib.commands import get_cmd_object
from bzrlib.errors import BzrCommandError
from bzrlib.help import command_usage
import bzrlib.ignores
from bzrlib.option import Option
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), 
                                                 "external")))

bzrlib.ignores.add_runtime_ignores(['./.shelf'])


class cmd_clean_tree(bzrlib.commands.Command):
    """Remove unwanted files from working tree.

    By default, only unknown files, not ignored files, are deleted.  Versioned
    files are never deleted.

    Another class is 'detritus', which includes files emitted by bzr during
    normal operations and selftests.  (The value of these files decreases with
    time.)

    If no options are specified, unknown files are deleted.  Otherwise, option
    flags are respected, and may be combined.

    To check what clean-tree will do, use --dry-run.
    """
    takes_options = [Option('ignored', help='delete all ignored files.'), 
                     Option('detritus', help='delete conflict files, merge'
                            ' backups, and failed selftest dirs.'), 
                     Option('unknown', 
                            help='delete files unknown to bzr.  (default)'),
                     Option('dry-run', help='show files to delete instead of'
                            ' deleting them.')]
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False):
        from clean_tree import clean_tree
        if not (unknown or ignored or detritus):
            unknown = True
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus, 
                   dry_run=dry_run)


class cmd_graph_ancestry(bzrlib.commands.Command):
    """Produce ancestry graphs using dot.
    
    Output format is detected according to file extension.  Some of the more
    common output formats are html, png, gif, svg, ps.  An extension of '.dot'
    will cause a dot graph file to be produced.  HTML output has mouseovers
    that show the commit message.

    Branches are labeled r?, where ? is the revno.  If they have no revno,
    with the last 5 characters of their revision identifier are used instead.

    The value starting with d is "(maximum) distance from the null revision".
    
    If --merge-branch is specified, the two branches are compared and a merge
    base is selected.
    
    Legend:
    white    normal revision
    yellow   THIS  history
    red      OTHER history
    orange   COMMON history
    blue     COMMON non-history ancestor
    green    Merge base (COMMON ancestor farthest from the null revision)
    dotted   Ghost revision (missing from branch storage)

    Ancestry is usually collapsed by skipping revisions with a single parent
    and descendant.  The number of skipped revisions is shown on the arrow.
    This feature can be disabled with --no-collapse.

    By default, revisions are ordered by distance from root, but they can be
    clustered instead using --cluster.

    If available, rsvg is used to antialias PNG and JPEG output, but this can
    be disabled with --no-antialias.
    """
    takes_args = ['branch', 'file']
    takes_options = [Option('no-collapse', help="Do not skip simple nodes"), 
                     Option('no-antialias',
                     help="Do not use rsvg to produce antialiased output"), 
                     Option('merge-branch', type=str, 
                     help="Use this branch to calcuate a merge base"), 
                     Option('cluster', help="Use clustered output.")]
    def run(self, branch, file, no_collapse=False, no_antialias=False,
        merge_branch=None, cluster=False):
        import graph
        if cluster:
            ranking = "cluster"
        else:
            ranking = "forced"
        graph.write_ancestry_file(branch, file, not no_collapse, 
                                  not no_antialias, merge_branch, ranking)


class cmd_fetch_ghosts(bzrlib.commands.Command):
    """Attempt to retrieve ghosts from another branch.
    If the other branch is not supplied, the last-pulled branch is used.
    """
    aliases = ['fetch-missing']
    takes_args = ['branch?']
    takes_options = [Option('no-fix')]
    def run(self, branch=None, no_fix=False):
        from fetch_ghosts import fetch_ghosts
        fetch_ghosts(branch, no_fix)

strip_help="""Strip the smallest prefix containing num leading slashes  from \
each file name found in the patch file."""
Option.OPTIONS['bzrdiff'] = Option('bzrdiff',type=None,
                                help="""Handle extra bzr tags""")


class cmd_patch(bzrlib.commands.Command):
    """Apply a named patch to the current tree.
    """
    takes_args = ['filename?']
    takes_options = [Option('strip', type=int, help=strip_help)]
    def run(self, filename=None, strip=-1, bzrdiff=0):
        from patch import patch
        from bzrlib.workingtree import WorkingTree
        wt = WorkingTree.open_containing('.')[0]
        if strip == -1:
            if bzrdiff: strip = 0
            else:       strip = 0

        return patch(wt, filename, strip, legacy= not bzrdiff)


class cmd_shelve(bzrlib.commands.Command):
    """Temporarily set aside some changes from the current tree.

    Shelve allows you to temporarily put changes you've made "on the shelf",
    ie. out of the way, until a later time when you can bring them back from
    the shelf with the 'unshelve' command.

    Shelve is intended to help separate several sets of text changes that have
    been inappropriately mingled.  If you just want to get rid of all changes
    (text and otherwise) and you don't need to restore them later, use revert.
    If you want to shelve all text changes at once, use shelve --all.

    By default shelve asks you what you want to shelve, press '?' at the
    prompt to get help. To shelve everything run shelve --all.

    If filenames are specified, only the changes to those files will be
    shelved, other files will be left untouched.

    If a revision is specified, changes since that revision will be shelved.

    You can put multiple items on the shelf. Normally each time you run
    unshelve the most recently shelved changes will be reinstated. However,
    you can also unshelve changes in a different order by explicitly
    specifiying which changes to unshelve. This works best when the changes
    don't depend on each other.

    While you have patches on the shelf you can view and manipulate them with
    the 'shelf' command. Run 'bzr shelf -h' for more info.
    """

    takes_args = ['file*']
    takes_options = ['message', 'revision',
            Option('all', help='Shelve all changes without prompting'), 
            Option('no-color', help='Never display changes in color')]

    def run(self, all=False, file_list=None, message=None, revision=None,
            no_color=False):
        if revision is not None and revision:
            if len(revision) == 1:
                revision = revision[0]
            else:
                raise CommandError("shelve only accepts a single revision "
                                  "parameter.")

        source = BzrPatchSource(revision, file_list)
        s = Shelf(source.base)
        s.shelve(source, all, message, no_color)
        return 0


# The following classes are only used as subcommands for 'shelf', they're
# not to be registered directly with bzr.

class cmd_shelf_list(bzrlib.commands.Command):
    """List the patches on the current shelf."""
    aliases = ['list', 'ls']
    def run(self):
        self.shelf.list()


class cmd_shelf_delete(bzrlib.commands.Command):
    """Delete the patch from the current shelf."""
    aliases = ['delete', 'del']
    takes_args = ['patch']
    def run(self, patch):
        self.shelf.delete(patch)


class cmd_shelf_switch(bzrlib.commands.Command):
    """Switch to the other shelf, create it if necessary."""
    aliases = ['switch']
    takes_args = ['othershelf']
    def run(self, othershelf):
        s = Shelf(self.shelf.base, othershelf)
        s.make_default()


class cmd_shelf_show(bzrlib.commands.Command):
    """Show the contents of the specified or topmost patch."""
    aliases = ['show', 'cat', 'display']
    takes_args = ['patch?']
    def run(self, patch=None):
        self.shelf.display(patch)


class cmd_shelf_upgrade(bzrlib.commands.Command):
    """Upgrade old format shelves."""
    aliases = ['upgrade']
    def run(self):
        self.shelf.upgrade()


class cmd_shelf(bzrlib.commands.Command):
    """Perform various operations on your shelved patches. See also shelve."""
    takes_args = ['subcommand', 'args*']

    subcommands = [cmd_shelf_list, cmd_shelf_delete, cmd_shelf_switch,
        cmd_shelf_show, cmd_shelf_upgrade]

    def run(self, subcommand, args_list):
        import sys

        if args_list is None:
            args_list = []
        cmd = self._get_cmd_object(subcommand)
        source = BzrPatchSource()
        s = Shelf(source.base)
        cmd.shelf = s
        return cmd.run_argv_aliases(args_list)

    def _get_cmd_object(self, cmd_name):
        for cmd_class in self.subcommands:
            for alias in cmd_class.aliases:
                if alias == cmd_name:
                    return cmd_class()
        raise CommandError("Unknown shelf subcommand '%s'" % cmd_name)

    def help(self):
        text = ["%s\n\nSubcommands:\n" % self.__doc__]

        for cmd_class in self.subcommands:
            text.extend(self.sub_help(cmd_class) + ['\n'])

        return ''.join(text)

    def sub_help(self, cmd_class):
        text = []
        cmd_obj = cmd_class()
        indent = 2 * ' '

        usage = command_usage(cmd_obj)
        usage = usage.replace('bzr shelf-', '')
        text.append('%s%s\n' % (indent, usage))

        text.append('%s%s\n' % (2 * indent, cmd_class.__doc__))

        # Somewhat copied from bzrlib.help.help_on_command_options
        option_help = []
        for option_name, option in sorted(cmd_obj.options().items()):
            if option_name == 'help':
                continue
            option_help.append('%s--%s' % (3 * indent, option_name))
            if option.type is not None:
                option_help.append(' %s' % option.argname.upper())
            if option.short_name():
                option_help.append(', -%s' % option.short_name())
            option_help.append('%s%s\n' % (2 * indent, option.help))

        if len(option_help) > 0:
            text.append('%soptions:\n' % (2 * indent))
            text.extend(option_help)

        return text


class cmd_unshelve(bzrlib.commands.Command):
    """Restore shelved changes.

    By default the most recently shelved changes are restored. However if you
    specify a patch by name those changes will be restored instead.

    See 'shelve' for more information.
    """
    takes_options = [
            Option('all', help='Unshelve all changes without prompting'),
            Option('force', help='Force unshelving even if errors occur'),
            Option('no-color', help='Never display changes in color')
        ]
    takes_args = ['patch?']
    def run(self, patch=None, all=False, force=False, no_color=False):
        source = BzrPatchSource()
        s = Shelf(source.base)
        s.unshelve(source, patch, all, force, no_color)
        return 0


class cmd_shell(bzrlib.commands.Command):
    """Begin an interactive shell tailored for bzr.
    Bzr commands can be used without typing bzr first, and will be run natively
    when possible.  Tab completion is tailored for bzr.  The shell prompt shows
    the branch nick, revno, and path.

    If it encounters any moderately complicated shell command, it will punt to
    the system shell.

    Example:
    $ bzr shell
    bzr bzrtools:287/> status
    modified:
      __init__.py
    bzr bzrtools:287/> status --[TAB][TAB]
    --all        --help       --revision   --show-ids
    bzr bzrtools:287/> status --
    """
    def run(self):
        import shell
        return shell.run_shell()


class cmd_branch_history(bzrlib.commands.Command):
    """\
    Display the development history of a branch.

    Each different committer or branch nick is considered a different line of
    development.  Committers are treated as the same if they have the same
    name, or if they have the same email address.
    """
    takes_args = ["branch?"]
    def run(self, branch=None):
        from branchhistory import branch_history 
        return branch_history(branch)


class cmd_zap(bzrlib.commands.Command):
    """\
    Remove a lightweight checkout, if it can be done safely.

    This command will remove a lightweight checkout without losing data.  That
    means it only removes lightweight checkouts, and only if they have no
    uncommitted changes.

    If --branch is specified, the branch will be deleted too, but only if the
    the branch has no new commits (relative to its parent).
    """
    takes_options = [Option("branch", help="Remove associtated branch from"
                                           " repository")]
    takes_args = ["checkout"]
    def run(self, checkout, branch=False):
        from zap import zap
        return zap(checkout, remove_branch=branch)


class cmd_cbranch(bzrlib.commands.Command):
    """
    Create a new checkout, associated with a new repository branch.
    
    When you cbranch, bzr looks up the repository associated with your current
    directory in locations.conf.  It creates a new branch in that repository
    with the same name and relative path as the checkout you request.

    The locations.conf parameter is "cbranch_root".  So if you want 
    cbranch operations in /home/jrandom/bigproject to produce branches in 
    /home/jrandom/bigproject/repository, you'd add this:

    [/home/jrandom/bigproject]
    cbranch_root = /home/jrandom/bigproject/repository

    Note that if "/home/jrandom/bigproject/repository" isn't a repository,
    standalone branches will be produced.  Standalone branches will also
    be produced if the source branch is in 0.7 format (or earlier).
    """
    takes_options = [Option("lightweight", 
                            help="Create a lightweight checkout"), 'revision']
    takes_args = ["source", "target?"]
    def run(self, source, target=None, lightweight=False, revision=None):
        from cbranch import cbranch
        return cbranch(source, target, lightweight=lightweight, 
                       revision=revision)


class cmd_branches(bzrlib.commands.Command):
    """Scan a location for branches"""
    takes_args = ["location?"]
    def run(self, location=None):
        from branches import branches
        return branches(location)


class cmd_multi_pull(bzrlib.commands.Command):
    """Pull all the branches under a location, e.g. a repository.
    
    Both branches present in the directory and the branches of checkouts are
    pulled.
    """
    takes_args = ["location?"]
    def run(self, location=None):
        from bzrlib.branch import Branch
        from bzrlib.transport import get_transport
        from bzrtools import iter_branch_tree
        if location is None:
            location = '.'
        t = get_transport(location)
        if not t.listable():
            print "Can't list this type of location."
            return 3
        for branch, wt in iter_branch_tree(t):
            if wt is None:
                pullable = branch
            else:
                pullable = wt
            parent = branch.get_parent()
            if parent is None:
                continue
            if wt is not None:
                base = wt.basedir
            else:
                base = branch.base
            if base.startswith(t.base):
                relpath = base[len(t.base):].rstrip('/')
            else:
                relpath = base
            print "Pulling %s from %s" % (relpath, parent)
            try:
                pullable.pull(Branch.open(parent))
            except Exception, e:
                print e


class cmd_branch_mark(bzrlib.commands.Command):
    """
    Add, view or list branch markers <EXPERIMENTAL>

    To add a mark, do 'bzr branch-mark MARK'.
    To list marks, do 'bzr branch-mark' (this lists all marks for the branch's
    repository).
    To delete a mark, do 'bzr branch-mark --delete MARK'

    These marks can be used to track a branch's status.
    """
    takes_args = ['mark?', 'branch?']
    takes_options = [Option('delete', help='Delete this mark')]
    def run(self, mark=None, branch=None, delete=False):
        from branch_mark import branch_mark
        branch_mark(mark, branch, delete)


class cmd_import(bzrlib.commands.Command):
    """Import sources from a tarball
    
    This command will import a tarball into a bzr tree, replacing any versioned
    files already present.  If a directory is specified, it is used as the
    target.  If the directory does not exist, or is not versioned, it is
    created.

    Tarballs may be gzip or bzip2 compressed.  This is autodetected.

    If the tarball has a single root directory, that directory is stripped
    when extracting the tarball.
    """
    
    takes_args = ['source', 'tree?']
    def run(self, source, tree=None):
        from upstream_import import do_import
        do_import(source, tree)


class cmd_cdiff(bzrlib.commands.Command):
    """A color version of bzr's diff"""
    takes_args = property(lambda x: get_cmd_object('diff').takes_args)
    takes_options = property(lambda x: get_cmd_object('diff').takes_options)
    def run(*args, **kwargs):
        from colordiff import colordiff
        colordiff(*args, **kwargs)


class cmd_baz_import(bzrlib.commands.Command):
    """Import an Arch or Baz archive into a bzr repository.

    This command should be used on local archives (or mirrors) only.  It is
    quite slow on remote archives.
    
    reuse_history allows you to specify any previous imports you 
    have done of different archives, which this archive has branches
    tagged from. This will dramatically reduce the time to convert 
    the archive as it will not have to convert the history already
    converted in that other branch.

    If you specify prefixes, only branches whose names start with that prefix
    will be imported.  Skipped branches will be listed, so you can import any
    branches you missed by accident.  Here's an example of doing a partial
    import from thelove@canonical.com:
    bzr baz-import thelove thelove@canonical.com --prefixes dists:talloc-except
    """
    takes_args = ['to_root_dir', 'from_archive', 'reuse_history*']
    takes_options = ['verbose', Option('prefixes', type=str,
                     help="Prefixes of branches to import, colon-separated")]

    def run(self, to_root_dir, from_archive, verbose=False,
            reuse_history_list=[], prefixes=None):
        from errors import NoPyBaz
        try:
            import baz_import
            baz_import.baz_import(to_root_dir, from_archive,
                                  verbose, reuse_history_list, prefixes)
        except NoPyBaz:
            print "This command is disabled.  Please install PyBaz."


class cmd_baz_import_branch(bzrlib.commands.Command):
    """Import an Arch or Baz branch into a bzr branch."""
    takes_args = ['to_location', 'from_branch?', 'reuse_history*']
    takes_options = ['verbose', Option('max-count', type=int)]

    def run(self, to_location, from_branch=None, fast=False, max_count=None,
            verbose=False, dry_run=False, reuse_history_list=[]):
        from errors import NoPyBaz
        try:
            import baz_import
            baz_import.baz_import_branch(to_location, from_branch, fast, 
                                         max_count, verbose, dry_run,
                                         reuse_history_list)
        except NoPyBaz:
            print "This command is disabled.  Please install PyBaz."


commands = [
            cmd_baz_import,
            cmd_baz_import_branch,
            cmd_branches,
            cmd_branch_history,
            cmd_branch_mark,
            cmd_cbranch,  
            cmd_cdiff,
            cmd_clean_tree,
            cmd_fetch_ghosts,
            cmd_graph_ancestry,
            cmd_import,
            cmd_multi_pull,
            cmd_patch,
            cmd_shelf, 
            cmd_shell,
            cmd_shelve, 
            cmd_switch,
            cmd_unshelve, 
            cmd_zap,            
            ]

commands.append(rspush.cmd_rspush)

if hasattr(bzrlib.commands, 'register_command'):
    for command in commands:
        bzrlib.commands.register_command(command)


def test_suite():
    from bzrlib.tests.TestUtil import TestLoader
    import tests
    from doctest import DocTestSuite, ELLIPSIS
    from unittest import TestSuite
    import tests.clean_tree
    import upstream_import
    import zap
    import tests.blackbox
    import tests.shelf_tests
    result = TestSuite()
    result.addTest(DocTestSuite(bzrtools, optionflags=ELLIPSIS))
    result.addTest(tests.clean_tree.test_suite())
    try:
        import baz_import
        result.addTest(DocTestSuite(baz_import))
    except NoPyBaz:
        pass
    result.addTest(tests.test_suite())
    result.addTest(TestLoader().loadTestsFromModule(tests.shelf_tests))
    result.addTest(tests.blackbox.test_suite())
    result.addTest(upstream_import.test_suite())
    result.addTest(zap.test_suite())
    return result
