summaryrefslogtreecommitdiff
path: root/src/libregit
blob: 86fbef971860981bc5dbcaceb613d65fbc0b5f5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash

#   Copyright (c) 2012-2013 Pacman Development Team <pacman-dev@archlinux.org>
#   Copyright (c) 2012-2013 Luke Shumaker <lukeshu@sbcglobal.net>
#
#   This program 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 program 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 program.  If not, see <http://www.gnu.org/licenses/>.

. libremessages

cd_safe() {
	if ! cd "$1"; then
		error "$(gettext "Failed to change to directory %s")" "$1"
		plain "$(gettext "Aborting...")"
		exit 1
	fi
}

download_git() {
	if [[ ! -d "$dir/.git" ]] ; then
		msg2 "$(gettext "Cloning %s %s repo...")" "${repo}" "git"
		if ! git clone "$repo" "$dir"; then
			error "$(gettext "Failure while downloading %s %s repo")" "${repo}" "git"
			plain "$(gettext "Aborting...")"
			exit 1
		fi
	else
		cd_safe "$dir"
		# Make sure we are fetching the right repo
		if [[ "$repo" != "$(git config --get remote.origin.url)"  ]] ; then
			error "$(gettext "%s is not a clone of %s")" "$dir" "$repo"
			plain "$(gettext "Aborting...")"
			exit 1
		fi
		msg2 "$(gettext "Updating %s %s repo...")" "${repo}" "git"
		if ! git pull origin "$ref"; then
			# only warn on failure to allow offline builds
			warning "$(gettext "Failure while updating %s %s repo")" "${repo}" "git"
		fi
	fi
}

usage() {
	print 'Usage: %s repo ref dir' "${0##*/}"
	echo
	print "Clones or pulls from the git URL 'repo', and checks out the git ref"
	print "'ref' to the directory 'dir'."
	echo
	print "It does safety checks, figures out whether to clone or pull, and other"
	print "helpful things.  This exists because the same 'download_git' function"
	print "from makepkg was being copied again and again."
}

main() {
	[[ $# == 3 ]] || { usage >&2; return 1; }
	repo=$1
	ref=$2
	dir=$3

	[[ -d "${dir%/*}" ]] || mkdir -p "${dir%/*}"
	download_git
}

main "$@"