aboutsummaryrefslogtreecommitdiff
path: root/libexec/build-fetch-git
blob: 5017f98dfa72d341c659d0402f52ffd86c937a03 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env build

build_fetch_git()
{
	build_fetch_git_uri="$(build-url --base "${1}")"
	build_fetch_git_uri="${build_fetch_git_uri#git://}"
	build_fetch_git_uri="${build_fetch_git_uri%%\?*}"
	build_fetch_git_tag=
	build_fetch_git_commit=
	build_fetch_git_var=
	for arg in $(build-url --arg "${1}"); do
		case "${arg}" in
		(archive=*) build_fetch_git_var="${arg##*=}";;
		(commit=*) build_fetch_git_commit="${arg##*=}";;
		(*)		 build_fetch_git_tag="${arg}";;
		esac
	done
	if [ -z "${build_fetch_git_var}" ]; then
		die "do not know how to store source from '${1}'"
	fi

	# We want to avoid avoid copying the repository and all of its history
	# in order to perform a single build, unfortunately git does not give a
	# clear-cut approach to checkout out a specific tag or the head of a
	# specific branch, instead we have to jump through some hoops.
	build_fetch_git_dir="${build_fetch_git_var%%.t*}"
	build_fetch_git_tmp="$(mktemp -d "${BUILDER_TMPDIR}/${build_fetch_git_dir}.XXXXXX")"
	cd "${build_fetch_git_tmp}"
	BUILD_FETCH_CLEAN="${BUILD_FETCH_CLEAN} ${build_fetch_git_tmp}"

	case "${build_fetch_git_uri}" in
	(*:[0-9]*)	build_fetch_git_uri="git://${build_fetch_git_uri}";;
	(*:*)	build_fetch_new_uri="${build_fetch_git_uri##*:}"
		build_fetch_new_uri="${build_fetch_new_uri#/}"
		build_fetch_git_uri="ssh://${build_fetch_git_uri%%:*}/${build_fetch_new_uri}"
		unset build_fetch_new_uri;;
	(*)		build_fetch_git_uri="git://${build_fetch_git_uri}";;
	esac

	if [ ! -z "${build_fetch_git_tag}" ]; then
		case "${build_fetch_git_tag}" in
		(tags/*)
			if ! git clone --depth 1 "${build_fetch_git_uri}" "${build_fetch_git_dir}"; then
				die "failed to clone git source at '${build_fetch_git_uri}'"
			fi

			if ! cd "${build_fetch_git_dir}"; then
				die "failed to change working directory to '${build_fetch_git_dir}'"
			fi

			if ! git fetch --tags --depth 1 "${build_fetch_git_uri}" "${build_fetch_git_tag}"; then
				die "failed to fetch git branch/tag '${build_fetch_git_tag}'"
			fi

			if ! git checkout "${build_fetch_git_tag}"; then
				die "failed to checkout git branch/tag '${build_fetch_git_tag}'"
			fi
			;;
		(*)
			if ! git clone --depth 1 --branch "${build_fetch_git_tag}" "${build_fetch_git_uri}" "${build_fetch_git_dir}"; then
				die "failed to clone git source at '${build_fetch_git_uri}'"
			fi
			;;
		esac
	elif [ ! -z "${build_fetch_git_commit}" ]; then
			if ! git clone "${build_fetch_git_uri}" "${build_fetch_git_dir}"; then
				die "failed to clone git source at '${build_fetch_git_uri}'"
			fi

			if ! cd "${build_fetch_git_dir}"; then
				die "failed to change working directory to '${build_fetch_git_dir}'"
			fi

			if ! git checkout "${build_fetch_git_commit}"; then
				die "failed to checkout git commit '${build_fetch_git_commit}'"
			fi
	else
		if ! git clone --depth 1 "${build_fetch_git_uri}" "${build_fetch_git_dir}"; then
			die "failed to clone git source at '${build_fetch_git_uri}'"
		fi
	fi

	cd "${build_fetch_git_tmp}"
	case "${build_fetch_git_var}" in
	(*tar.Z|*tz)	tar cZf "${build_fetch_git_var}" "${build_fetch_git_dir}";;
	(*tgz|*tar.gz)	tar czf "${build_fetch_git_var}" "${build_fetch_git_dir}";;
	(*tar.bz2)	tar cjf "${build_fetch_git_var}" "${build_fetch_git_dir}";;
	(*)		tar caf "${build_fetch_git_var}" "${build_fetch_git_dir}";;
	esac

	if [ ! -d "${BUILDER_SRCDIR}" ]; then
		mkdir -p "${BUILDER_SRCDIR}"
	fi

	mv "${build_fetch_git_var}" "${BUILDER_SRCDIR}/${build_fetch_git_var}"

	if [ -d "${build_fetch_git_tmp}" ]; then
		rm -rf "${build_fetch_git_tmp}"
	fi

	unset build_fetch_git_tmp
	unset build_fetch_git_var
	unset build_fetch_git_dir
	unset build_fetch_git_uri
	unset build_fetch_git_tag
}

if test "${BUILDER_CALL_STACK}" = '__main__'; then
	simple_usage 'fetch-git' '<url>' "$@"
	build_fetch_git "${1}"
fi

# vim: filetype=sh