<?xml version="1.0"?>
<!-- ======================================================================
     ANT Macrodef Build Script

     - Runs various GitHub tasks

     Author: Newtriks <simon@newtriks.com>
     Date: 6th March 2010

     This script is based on code written by:
     
     Mark Lowe <melowe@gmail.com>
	 Tom Robinson <http://tlrobinson.net>
	 
	 Scripts ripped from:
		
	 <http://tlrobinson.net/blog/2008/11/13/ant-tasks-for-git/>
	 and Comment by Timo on the above URL
     
	====================================================================== -->
<project>
	<!-- = = = = = = = = = = = = = = = = =
	     macrodef: git
	     = = = = = = = = = = = = = = = = = -->
	<macrodef name="git">
		<attribute name = "command" />
		<attribute name = "dir" default = "" />
		<element name = "args" optional = "true" />
		<sequential>
				<echo file="GIT_COMMAND_LOG" message="git @{command} &#xa;"
					append="yes" />
				<exec executable = "git" dir = "@{dir}" logerror="true" >
					<arg value = "@{command}" />
					<args/>
				</exec>
		</sequential>
	</macrodef>

	<macrodef name = "git-init">
		<attribute name = "repository" />
		<sequential>
			<git command = "init">
				<args>
					<arg value = "@{repository}" />
				</args>
			</git>
		</sequential>
	</macrodef>
    <macrodef name = "git-export">
            <attribute name = "revision" />
            <attribute name = "destinationDirectory" />
            <sequential>
              <exec executable = "${buildsupport.basedir}/gitant/git-export.sh"  logerror="true" >
					<arg value = "@{revision}" />
					<arg value = "@{destinationDirectory}" />
				</exec>
            </sequential>
        </macrodef>

	<macrodef name = "git-add">
		<attribute name = "path" />
		<attribute name = "target" />
		<sequential>
			<git command = "add" dir="@{path}">
				<args>
					<arg value = "@{target}" /> 
				</args>
			</git>
		</sequential>
	</macrodef>

	<macrodef name = "git-commit">
		<attribute name = "path" />
		<attribute name = "message" />
		<sequential>
			<git command="commit" dir="@{path}">
				<args>
					<arg value= "@{message}" />
				</args>
			</git>
		</sequential>
	</macrodef>

	<macrodef name = "git-clone-pull">
		<attribute name = "repository" />
		<attribute name = "dest" />
		<sequential>
			<git command = "clone">
				<args>
					<arg value = "@{repository}" />
					<arg value = "@{dest}" />
				</args>
			</git>
			<git command = "pull" dir = "@{dest}" />
		</sequential>
	</macrodef>

	<macrodef name = "git-remote-add">
		<attribute name = "path" />
		<attribute name = "branch" />
		<attribute name = "repository" />
		<sequential>
			<echo file="DUMP" message="branch: @{branch} repos: @{repository}"/>
			<git command = "remote"  dir="@{path}">
				<args>
					<arg value = "add" />
					<arg value = "@{branch}" />
					<arg value = "@{repository}" />
				</args>
			</git>
		</sequential>
	</macrodef>

	<macrodef name = "git-push">
		<attribute name = "path" />
		<attribute name = "branch" />
		<attribute name = "head" />
		<sequential>
			<git command = "push" dir="@{path}">
				<args>
					<arg value = "@{branch}" />
					<arg value = "@{head}" />
				</args>
			</git>
		</sequential>
	</macrodef>

	<macrodef name="test">
		<attribute name = "path" default="hello" />
		<sequential>
			<fail message="fail">
				<condition>
					<not>
						<contains string="@{path}" substring="hello" />
					</not>
				</condition>
			</fail>
		</sequential>
	</macrodef>

	<macrodef name="git-revision">
		<attribute name="path" />
		<attribute name="output" />
		<sequential>
			<exec executable="git" outputproperty="head" dir = "@{path}">
				<arg value="rev-parse" />
				<arg value="HEAD" />
			</exec>
			<echo file="GIT_REVISION_LOG" message="Found revision: ${head}"/>
			<exec executable="git" outputproperty="dirty">
				<arg value="diff" />
				<arg value="--shortstatt" />
			</exec>
			<condition property="@{output}" value="${head}" else="${head} (dirty)">
				<equals arg1="${dirty}" arg2="" />
			</condition>
		</sequential>
	</macrodef>

</project>
