summaryrefslogtreecommitdiff
path: root/pbs-convert-absgit
blob: 22539d318e600ca4ad668752b468e7a9770050a6 (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
#!/bin/bash -euE

cmd="${0##*/}"
export TMPDIR="`mktemp -d --tmpdir ${cmd}.XXXXXXXXXX`"
cleanup() {
	msg "$(gettext "Removing temporary files...")"
	echo rm -rf "$TMPDIR"
}
trap cleanup EXIT

. libremessages

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

##
# Usage: trailing-newline FILE
#
# Adds a trailing newline to ${FILE} if there isn't one.
#
# Useful because different versions of git are inconsistent about
# when there is one on output.
###
trailing-newline() {
	local file=$1
	if [[ "`sed -n '$p' "$file"|wc -l`" = 0 ]]; then
		echo >> "$file"
	fi
}

##
# Usage: collect-data
#
# Assumptions:
#  - currently in the working repo
# Effected by:
#  - file ../missing-packages
# Effects:
#  - creates file "../find"
#  - creates file "../packages"
#  - creates file "../architectures"
# Side effects:
#  - creates file "${TMPDIR}/commits"
##
collect-data() {
	local mode=fast # 'correct' or 'fast
	# 'fast' may omit some packages that have been deleted.

	msg "$(gettext "Collecting package data...")"

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

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

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

##
# Usage: convert-package PACKAGE
#
# Assumptions:
#  - currently in the working repo
# Effects:
#  - creates git branch "pkgs/${PACKAGE}"
#  - creates file "../pkg-${PACKAGE}.commits"
# Side effects:
#  - changes git working tree
#  - prints output of git commands
##
convert-package() {
	local package=$1

	local obranch="pkgs/${package}"
	local ibranch
	local dir
	if git checkout "packages/${package}" &>/dev/null; then
		# special case (common; optimization)
		ibranch="packages/${package}"
		dir=trunk
	else
		# general case (uncommon)
		ibranch=master
		dir="${package}/trunk"
	fi

	git rewrite-branch --svn "$ibranch" "$obranch" \
		--prune-empty --subdirectory-filter "$dir"

	if [[ $ibranch != master ]]; then
		git branch -D "$ibranch"
	fi

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

##
# Usage: convert-packages
# Assumptions:
#  - currently in the working repo
#  - file "../packages" contains a newline-separated list of packages
# Effects:
#  - runs `convert-package` for every package listed in "../packages"
#  - prints status updates about what it is doing
##
convert-packages() {
	msg "$(gettext "Converting packages...")"
	local count="$(wc -l < ../packages)"
	cat -n ../packages | while read n package; do
		msg2 "$(gettext "(%d/%d) %s")" "$n" "$count" "$package"
		convert-package "$package"
	done
}

##
# Usage: convert-arch ARCH
# Assumptions:
#  - currently in the working repo
#  - file "../packages-${PACKAGE}.commits" exists for every package
# Effects:
#  - creates git branch "${ARCH}"
#  - creates file "${TMPDIR}/missing-packages/${ARCH}"
# Side effects:
#  - changes git working tree
#  - prints output of git commands
##
convert-arch() {
	local arch=$1

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

	git rewrite-branch master "$arch" \
		--tree-filter "${cmd}--filterarch $arch"

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

##
# Usage: convert-arches
# Assumptions:
#  - currently in the working repo
#  - file "../architectures" contains a newline-separated list of arches
# 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...")"
	local count="$(wc -l < "../architectures")"
	cat -n "../architectures" | while read n arch; do
		msg2 "$(gettext "(%d/%d) %s")" "$n" "$count" "$arch"
		convert-arch "$arch"
	done
}

usage() {
	echo "Usage: ${0##*/} SOURCE"
	echo "Convert the absgit format SOURCE to pbs format"
	echo
	echo "SOURCE must be configured in /etc/libretools.d/pbs-convert.conf"
}

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

	local cache="$(pbs-plumb-config get core.cachedir)/${source}.git"
	local workdir="$(pbs-plumb-config get core.rewritedir)/${source}"
	local sourcedir="$(pbs-plumb-config get core.sourcedir)"

	# init ###################################d##############################
	if [[ ! -d "${workdir}" ]]; then
		msg "$(gettext "Creating working copy...")"
		git clone "$cache" "${workdir}/repo"
	fi
	cd "${workdir}/repo"

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

	collect-data fast
	convert-packages
	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 "../packages" "${sourceir}/${source}.packages"
	cat "${TMPDIR}"/missing-packages/* > "${sourceir}/${source}.missing-packages"

	trap cleanup EXIT
}

main "$@"