################################################################################
#
# git.cygclass - functions for building packages from GIT checkouts
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2015 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport 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 3 of the License, or
# (at your option) any later version.
#
# cygport 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 cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/git.cygclass
#  SYNOPSIS
#  GIT_URI="..."
#  [GIT_BRANCH="..."]
#  [GIT_REV="..."]
#  [GIT_TAG="..."]
#  inherit git
#  DESCRIPTION
#  Git is a distributed version control system.  Originally developed for the
#  Linux kernel, it is widely used by a number of projects, including GNOME,
#  GStreamer, X.Org, and Xfce.
#
#  This cygclass creates source tarballs from Git checkouts.
#  NOTE
#  This cygclass sets a special SRC_URI for the tarball it creates.  If
#  additional sources are required, be sure to _add_ to SRC_URI rather than
#  outright setting it.
#  REQUIRES
#  git
#****

#****v* git.cygclass/GIT_URI
#  DESCRIPTION
#  Address of Git repository from which to clone.  Must be defined before
#  inherit()ing git.cygclass.
#****
if ! defined GIT_URI
then
	error "GIT_URI must be defined"
fi

GIT_URI=${GIT_URI%/}

if ! defined GIT_MODULE
then
	GIT_MODULE=${GIT_URI##*/}
	GIT_MODULE=${GIT_MODULE%.git}
fi

git_tarball="${GIT_MODULE}-${PV}.tar.bz2"

SRC_URI="${git_tarball} "
SRC_DIR="${GIT_MODULE}${GIT_SUBDIR+/}${GIT_SUBDIR}"

git_fetch() {
	local _depth

	check_prog_req git

	if ! defined GIT_TAG && ! defined GIT_BRANCH && ! defined GIT_REV
	then
		# fetching master/HEAD, so we don't need any history
		_depth="--depth 1"
	fi

	# T likely doesn't exist at this point, so create it first
	mkdir -p ${T}
	cd ${T}
	verbose git clone ${_depth} --no-checkout ${GIT_URI} ${GIT_MODULE} || error "git clone failed"
	cd ${T}/${GIT_MODULE}

#****v* git.cygclass/GIT_BRANCH
#  DESCRIPTION
#  Branch from which to clone.  If undefined, the 'master' branch is used.
#****
#****v* git.cygclass/GIT_REV
#  DESCRIPTION
#  Specific revision to clone, in the form of a SHA1 hash (a 40-digit hexadecimal)
#  or an abbreviation thereof (the first 7 hexadecimal digits are usually
#  sufficent).  If undefined, the latest revision in the given GIT_BRANCH is used.
#****
#****v* git.cygclass/GIT_TAG
#  DESCRIPTION
#  Tag to clone, essentially a legible shorthand for a given commit.
#****

	if defined GIT_TAG
	then
		verbose git checkout tags/${GIT_TAG} || error "git checkout failed"
	elif defined GIT_BRANCH
	then
		verbose git checkout remotes/origin/${GIT_BRANCH} || error "git checkout failed"
	elif defined GIT_REV
	then
		verbose git checkout ${GIT_REV} || error "git checkout failed"
	else
		verbose git checkout master || error "git checkout failed"
	fi

	if [ -f .gitmodules ]
	then
		git submodule init
		git submodule update
	fi

	cd ${T}/
	tar ${_tar_bz2_flag}cf ${top}/${git_tarball} --exclude-vcs ${GIT_MODULE}/${GIT_SUBDIR}
}

readonly -f git_fetch
