summaryrefslogtreecommitdiff
path: root/script/update/project/repo
blob: c0cec1b6155f4e471e3614003f0d305382a1528b (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
#!/usr/bin/env sh
# SPDX-License-Identifier: GPL-3.0-only
# SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>

. "include/err.sh"
. "include/git.sh"
. "include/option.sh"

eval "$(setvars "" name rev loc url bkup_url depend)"
tmp_dir="${PWD}/tmp/gitclone"

main()
{
	[ $# -gt 0 ] || fail "no argument given"

	[ -z "${1+x}" ] && fail 'main(): name not set'
	name=${1}

	scan_config "${name}" "config/git" "fail"
	verify_config

	clone_project
	[ "${depend}" = "" ] || for d in ${depend} ; do
		./update project repo ${d} || \
		  fail "Cannot fetch dependency, ${d}, for project, ${name}"
	done

	rm -Rf "${tmp_dir}" || fail "cannot remove tmpdir, ${tmp_dir}"
}

verify_config()
{
	[ -z "${rev+x}" ] && fail 'verify_config: rev not set'
	[ -z "${loc+x}" ] && fail 'verify_config: loc not set'
	[ -z "${url+x}" ] && fail 'verify_config: url not set'
}

clone_project()
{
	rm -Rf "${tmp_dir}" || fail "clone_project: cannot remove old tmpdir"
	mkdir -p "${tmp_dir%/*}" || fail "clone_project: can't mkdir"

	git clone ${url} "${tmp_dir}" || git clone ${bkup_url} "${tmp_dir}" || \
	    fail "clone_project: could not download ${name}"
	git_reset_rev "${tmp_dir}" "${rev}" "fail" || \
	    fail "clone_project ${loc}/: cannot reset <- ${rev}"
	git_am_patches "${tmp_dir}" "${PWD}/config/${name}/patches" "fail" || \
	    fail "clone_project ${loc}/: cannot apply patches"

	[ ! -d "${loc}" ] || \
		rm -Rf "${loc}" || \
		    fail "clone_project: Can't remove directory '${loc}'"
	[ "${loc}" = "${loc%/*}" ] || mkdir -p ${loc%/*} || \
	    fail "clone_project: cannot make directory for ${name}"
	mv "${tmp_dir}" "${loc}" || \
	    fail "clone_project: could not copy temp file to destination"
}

fail()
{
	for x in "${loc}" "${tmp_dir}"; do
		[ -z "${x}" ] || [ ! -d "${x}" ] || rm -Rf "${loc}" || :
	done
	usage
	err "${1}"
}

usage()
{
	cat <<- EOF
	Usage: ./update project repo [name]

	Options:
	name: Module name as specified in files under config/git/
	EOF
}

main $@