# -*- coding: utf-8 -*-
# Copyright (C) Tim Niemueller [www.niemueller.de]
# Copyright (C) 2007 Edgewall Software
# based on hgtools.py
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://bitten.edgewall.org/wiki/License.

"""Recipe commands for git."""

import logging

log = logging.getLogger('bitten.build.gittools')

__docformat__ = 'restructuredtext en'

def clone(ctxt, url, dir_='.'):
    """pull and update the local working copy from the git repository.
    
    :param ctxt: the build context
    :type ctxt: `Context`
    :param url: the url of the repository to clone
    :param dir\_: the name of a local subdirectory containing the working copy
    """
    args = ['clone', url, dir_]

    from bitten.build import shtools
    returncode = shtools.execute(ctxt, file_='git', args=args)
    if returncode != 0:
        ctxt.error('git clone failed (%s)' % returncode)

def pull(ctxt, revision, remote='origin', dir_='.'):
    """pull and update the local working copy from the git repository.
    
    :param ctxt: the build context
    :type ctxt: `Context`
    :param remote: the remote repository to pull from
    :param dir\_: the name of a local subdirectory containing the working copy
    """
    args = ['pull', '-u', remote, revision]

    from bitten.build import shtools
    returncode = shtools.execute(ctxt, file_='git', dir_=dir_, args=args)
    if returncode != 0:
        ctxt.error('git pull failed (%s)' % returncode)

def checkout(ctxt, branch, dir_='.'):
    """checkout specific branch
    
    :param ctxt: the build context
    :type ctxt: `Context`
    :param branch: branch to checkout
    :param dir\_: the name of a local subdirectory containing the working copy
    """
    args = ['checkout', branch]

    from bitten.build import shtools
    returncode = shtools.execute(ctxt, file_='git', dir_=dir_, args=args)
    if returncode != 0:
        ctxt.error('git branch failed (%s)' % returncode)

def reset(ctxt, revision, dir_='.'):
    """reset git tree to given revision/commit
    
    :param ctxt: the build context
    :type ctxt: `Context`
    :param revision: the revision/commit to reset to
    :param dir\_: the name of a local subdirectory containing the working copy
    """
    args = ['reset', revision]

    from bitten.build import shtools
    returncode = shtools.execute(ctxt, file_='git', dir_=dir_, args=args)
    if returncode != 0:
        ctxt.error('git reset failed (%s)' % returncode)

