#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 COPYRIGHT
##
## Copyright 2004 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------


#---------------------------------------------------------------------
##=item dl_get_git <url>
##
## Fetch the specified git url.
##
## This handler only supports tree downloads.
##
#---------------------------------------------------------------------
function dl_git_get () {
  dl_command_check git || return 254

  local target=$1
  local url_list=$2
  local hints=$3
  local dl_target=$4
  local dl_type=$5
  local url rc=0

  [[ $target ]] &&
  dl_connect || return 255

  for url in $url_list; do
    local URL GIT_ROOT GIT_DIRECTORY GIT_TAG
    url_crack "$url" "$hints"
    if [[ -z $GIT_DIRECTORY ]]
    then
      GIT_DIRECTORY=${target/.git/}
    fi
    if test -d $GIT_DIRECTORY; then
      message "${MESSAGE_COLOR}Running git pull...${DEFAULT_COLOR}"
      ( cd $GIT_DIRECTORY &&
      match=$(git branch | grep -F " $GIT_TAG"; true) &&
      if [[ $match ]]; then
        # no need to checkout a branch if we're already on it
        if [[ ${match:0:1} != '*' ]]; then
          echo git checkout $GIT_TAG &&
          git checkout $GIT_TAG
        else
          : # already on the correct branch
        fi
      else
        echo git checkout -b $GIT_TAG origin/$GIT_TAG &&
        git checkout -b $GIT_TAG origin/$GIT_TAG
      fi
      echo git pull &&
      git pull )
      rc=$?
      eval "$dl_target=\"$GIT_DIRECTORY\""
    else
      message "${MESSAGE_COLOR}Running git clone...${DEFAULT_COLOR}"
      echo git clone $GIT_ROOT $GIT_DIRECTORY
      git clone $GIT_ROOT $GIT_DIRECTORY &&
      cd $GIT_DIRECTORY &&
      if ! git branch | grep -qF " $GIT_TAG"; then
        echo git checkout -b $GIT_TAG origin/$GIT_TAG &&
        git checkout -b $GIT_TAG origin/$GIT_TAG
      fi
      rc=$?
      cd ../
      eval "$dl_target=\"$GIT_DIRECTORY\""
    fi
    [[ $rc == 0 ]] && break
  done
  dl_disconnect

  eval "$dl_type=\"tree\""
  return $rc
}

#---------------------------------------------------------------------
##=back
##
##=head1 LICENSE
##
## This software 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 software 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 software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------
