aboutsummaryrefslogtreecommitdiffstats
path: root/make_source_tarball.sh
blob: 6958b01a129190b24e39884ee02e436817ad8c9b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh
set -x
#
# Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# 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 3 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/>.

set -e

usage()
{
	echo "$0 <tag> <output directory> [-j N] [-T N]"
	echo "  -j N        use at most N download jobs to run in parallel;"
	echo "              the default is 4;"
	echo "  -T N        use at most N threads to compress the tarball;"
	echo "              the default is 1; set to 0 to use as many threads"
	echo "              as there are processor cores"

	exit 1
}

jobs=4
threads=1

get_option()
{
	option="$1"
	value="$2"

	if [ "${option}" = "-T" ] ; then
		threads="${value}"
	elif [ "${option}" = "-j" ] ; then
		jobs="${value}"
	else
		usage
	fi
}

get_revision()
{
    repo="$1"

    shift 1

    git -C "${repo}" --no-pager log --oneline --format='%H' $@
}

checkout_revision()
{
    repo="$1"
    given_revision="$2"

    # TODO: handle multiple remotes
    # Make sure to have the revision
    git -C "${repo}" fetch $(git -C "${repo}" remote) "${given_revision}"

    current_revision="$(get_revision ${repo} HEAD -1)"
    desired_revision="$(get_revision ${repo} ${given_revision})"

    if [ "${current_revision}" != "${desired_revision}" ] ; then
	git -C "${repo}" checkout "${given_revision}"
    fi
}

if [ $# -ne 2 -a $# -ne 4 -a $# -ne 6 ] ; then
	usage
elif [ $# -eq 4 ] ; then
	get_option "$3" "$4"
elif [ $# -eq 6 ] ; then
	if [ "$3" = "$5" ] ; then
		usage
	fi
	get_option "$3" "$4"
	get_option "$5" "$6"
fi

tag="$1"
outdir="$2"

replicant_dir="${outdir}/${tag}"
repo_branch="refs/tags/${tag}"

version="$(echo ${tag} | sed 's/replicant-//')"
major_version="$(echo ${version} | sed 's/-.*//')"
minor_version="$(echo ${version} | sed 's/.*-//')"

mkdir -p "${replicant_dir}"
cd "${replicant_dir}"
if [ ! -d .repo ] ; then
	yes | repo init \
		-u https://git.replicant.us/replicant/manifest.git \
		-b "${repo_branch}" \
		--depth=1 || rm -rf .repo/
fi

for repo in $(repo list | awk '{print $1}') ; do
     checkout_revision "${repo}" "${tag}"
done

# We have an issue with all the Replicant releases before Replicant 6.0-0001:
# All the manifests use git://, however over the time we moved several git
# repositories and wrote some apache redirect rules to handle that.
# Due to that, only http:// and https:// work fine.
# This should be fixed in all the replicant-<version>-dev branches, however
# we cannot use that here as we are making tarballs of former releases.
if [ $(echo ${major_version} | sed 's/\..*//') -lt 6 ] ; then
	sed 's#git://#https://#g' -i \
	    .repo/manifests/default.xml
fi

repo sync -j "${jobs}"



# We want the exact same source code used to build the images, so we need to
# remove all the modifications we did to enable fetching the source code
if [ $(echo $major_version | sed 's/\..*//') -lt 6 ] ; then
	git -C .repo/manifests reset --hard
fi

cd ..
tarball="${tag}.tar"
if [ ! -f "${tarball}" ] && [ ! -f "${tarball}.xz" ] ; then
	tar cf "${tarball}" "${replicant_dir}"	|| rm -f "${tarball}"
fi

# TODO: Make the tarball reproducible by removing metadata

xz -T "${threads}" -9e --verbose "${tarball}"