summaryrefslogtreecommitdiff
path: root/pbs-absrepo-convert
blob: 97ef9369f7da81f89bea1902cd9d14cfc96084d1 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash -eu

export TMPDIR="`mktemp -d --tmpdir pbs-absrepo-convert.XXXXXXXXXX`"
cleanup() {
	msg "$(gettext "Removing temporary files...")"
	echo rm -rf "$TMPDIR"
}
trap cleanup EXIT

. /usr/bin/libremessages

abort() {
	echo # force a fresh line
	error "$(gettext "Aborting...")"
	cleanup
}

##
# Usage: collect-data MODE
# MODE is either 'correct' or 'fast'
#
# Assumptions:
#  - currently in a working copy of the repo
#  - git branch "master" exists and is untouched
#  - TMPDIR is set
#  - file(s) exist in "${TMPDIR}/missing-packages.in/"
# Effects:
#  - creates file "${TMPDIR}/commits"
#  - creates file "${TMPDIR}/find"
#  - creates file "${TMPDIR}/packages"
#  - creates file "${TMPDIR}/architectures"
##
collect-data() {
	msg "$(gettext "Collecting package data...")"
	[[ $# = 1 ]] || { usage; return 1; }
	local mode=$1

	case "$mode" in
		correct)
			git log --pretty=format:'%H' master > "${TMPDIR}/commits";;
		fast)
			echo master > "${TMPDIR}/commits";;
		*)
			usage; return 1;;
	esac

	# actual data collection ###############################################
	local count="$(wc -l < "${TMPDIR}/commits")"
	cat -n "${TMPDIR}/commits" | while read n commit; do
		printf '\rscanning commit %s (%d/%d)' "$commit" "$n" "$count" >> /dev/tty
		git ls-tree -rd --name-only "$commit"
	done | fgrep /repos/ | sort -u > "${TMPDIR}/find"
	echo # newline

	# extract some things ##################################################
	# packages
	{
		if [[ $mode = fast ]]; then
			cat "${TMPDIR}"/{packages,missing-packages}.in
		fi
		< "${TMPDIR}/find" sed -e 's|^\.||' -e 's|/.*||'
	} | sort -u > "${TMPDIR}/packages"
	# architectures
	{
	    echo master
	    < "${TMPDIR}/find" sed -e 's/.*-//' -e '/^any$/d'
	} | sort -u > "${TMPDIR}/architectures"
}

##
# Usage: convert-package SOURCE PACKAGE
# Assumptions:
#  - currently in a working copy of the repo
#  - TMPDIR is set
# Effects:
#  - creates git branch "packages/${SOURCE}/${PACKAGE}"
#  - creates file "${TMPDIR}/packages-${PACKAGE}.commits"
# Side effects:
#  - changes git working tree
#  - prints output of git commands
##
convert-package() {
	[[ $# = 2 ]] || { usage; return 1; }
	local source=$1
	local package=$2

	local branch="packages/${source}/${package}"
	local dir

	if git checkout "packages/${package}" &>/dev/null; then
		# special case (common; optimization)
		git checkout -b "$branch"
		git branch -D "packages/${package}"
		dir=trunk
	else
		# general case (uncommon)
		git checkout -b "$branch" orig
		dir="${package}/trunk"
	fi
	git filter-branch -f --prune-empty --subdirectory-filter "$dir" "$branch"

	git log --pretty=format:'%T %H' > "${TMPDIR}/packages-${package}.commits"
}

##
# Usage: convert-packages SOURCE
# Assumptions:
#  - meets assumptions of `convert-package`
#  - TMPDIR is set
#  - file "${TMPDIR}/packages" contains a newline-separated list of packages
# Effects:
#  - runs `convert-package` for every package listed in "${TMPDIR}/packages"
#  - prints status updates about what it is doing
##
convert-packages() {
	msg "$(gettext "Converting packages...")"
	[[ $# = 1 ]] || { usage; return 1; }
	local source=$1

	local count="$(wc -l < "${TMPDIR}/packages")"
	cat -n "${TMPDIR}/packages" | while read n package; do
		msg2 "$(gettext "(%d/%d) %s")" "$n" "$count" "$package"
		convert-package "$source" "$package"
	done
}

##
# Usage: convert-arch ARCH
# Assumptions:
#  - currently in a working copy of the repo
# Effects:
#  - creates git branch "${ARCH}"
#  - TMPDIR is set
#  - creates file "${TMPDIR}/missing-packages/${ARCH}"
# Side effects:
#  - changes git working tree
#  - prints output of git commands
##
convert-arch() {
	[[ $# = 1 ]] || { usage; return 1; }
	local arch=$1

	mkdir -p "${TMPDIR}/missing-packages"
	touch "${TMPDIR}/missing-packages/${arch}.tmp"

	git checkout orig
	git checkout -b "$arch"
	git filter-branch -f --tree-filter \
		"pbs-absrepo-convert--filterarch $arch" "$arch"

	sort -u \
	    < "${TMPDIR}/missing-packages/${arch}.tmp" \
	    > "${TMPDIR}/missing-packages/${arch}"
	rm -f "${TMPDIR}/missing-packages/${arch}.tmp"
}

##
# Usage: convert-arches
# Assumptions:
#  - meets assumptions of `convert-arch`
#  - file "${TMPDIR}/architectures" contains a newline-separated list of arches
#  - file "${TMPDIR}/packages-${PACKAGE}.commits" exists for every package
# Effects:
#  - runs `convert-arch` for every arch listed in "${TMPDIR}/architectures"
#  - prints status updates about what it is doing
##
convert-arches() {
	msg "$(gettext "Converting architectures...")"
	[[ $# = 0 ]] || { usage; return 1; }

	local count="$(wc -l < "${TMPDIR}/architectures")"
	cat -n "${TMPDIR}/architectures" | while read n arch; do
		msg2 "$(gettext "(%d/%d) %s")" "$n" "$count" "$arch"
		convert-arch "$arch"
	done
}

##
# Usage: copy-metatada SOURCEDIR SOURCE FILE
# Assumptions:
#  - TMPDIR is set
# Effects:
#  - creates "${TMPDIR}/${FILE}.in"
##
copy-metadata() {
	[[ $# = 3 ]] || { usage; return 1; }
	local sourcedir=$1
	local source=$2
	local file=$3

	if [[ -f "${sourcedir}/${source}.${file}" ]]; then
		cp "${sourcedir}/${source}.${file}" "${TMPDIR}/${file}.in"
	else
		touch "${TMPDIR}/${file}.in"
	fi
}

main() {
	trap abort EXIT
	[[ $# = 1 ]] || { usage; return 1; }
	local source=$1

	local cachedir="$(pbs-plumb-config get core.cachedir)"
	local sourcedir="$(pbs-plumb-config get core.sourcedir)"

	# init #################################################################
	copy-metadata "$sourcedir" "$source" missing-packages
	copy-metadata "$sourcedir" "$source" packages

	msg "$(gettext "Creating working copy...")"
	git clone "${cachedir}/${source}.git" "${TMPDIR}/repo"
	cd "${TMPDIR}/repo"
	git checkout -b orig
	collect-data fast
	git branch -D master

	# main #################################################################

	convert-packages "$source"
	convert-arches

	# save results #########################################################
	msg "$(gettext "Copying into source directory...")"
	[[ -d "$sourcedir" ]] || mkdir -p "$sourcedir"

	# copy git repo
	git clone --mirror "${TMPDIR}/repo" "${sourcedir}/${source}.new.git"
	if [[ -d "${sourcedir}/${source}.git" ]]; then
		mv "${sourcedir}/${source}"{,.old}.git
	fi
	mv "${sourcedir}/${source}"{.new,}.git
	if [[ ! -d "${sourcedir}/${source}.old.git" ]]; then
		rm -rf "${sourcedir}/${source}".old.git
	fi

	# copy other data
	cp -f "${TMPDIR}/packages" "${sourceir}/${source}.packages"
	cat "${TMPDIR}"/missing-packages/* > "${sourceir}/${source}.missing-packages"

	trap cleanup EXIT
}

main "$@"