db-move 4.12 KB
Newer Older
Aaron Griffin's avatar
Aaron Griffin committed
1
2
#!/bin/bash

3
4
. "$(dirname "$(readlink -e "$0")")/config"
. "$(dirname "$(readlink -e "$0")")/db-functions"
5

Eli Schwartz's avatar
Eli Schwartz committed
6
if (( $# < 3 )); then
7
	msg "usage: %s <repo-from> <repo-to> <pkgname|pkgbase> ..." "${0##*/}"
8
	exit 1
Pierre Schmitz's avatar
Pierre Schmitz committed
9
fi
Aaron Griffin's avatar
Aaron Griffin committed
10

11
args=("${@}")
12
13
14
15
repo_from="${args[0]}"
repo_to="${args[1]}"
ftppath_from="${FTP_BASE}/${repo_from}/os/"
ftppath_to="${FTP_BASE}/${repo_to}/os/"
Aaron Griffin's avatar
Aaron Griffin committed
16

17
18
19
20
21
check_leapfrog=false
if in_array "${repo_from}" "${STAGING_REPOS[@]}" && in_array "${repo_to}" "${STABLE_REPOS[@]}"; then
	check_leapfrog=true
fi

22
if ! check_repo_configured "$repo_from"; then
23
	die "%s is not a valid repository for %s" "$repo_from" "$0"
24
elif ! check_repo_configured "$repo_to"; then
25
	die "%s is not a valid repository for %s" "$repo_to" "$0"
26
27
fi

28
if ! check_repo_permission "$repo_to" || ! check_repo_permission "$repo_from"; then
29
	die "You don't have permission to move packages from %s to %s" "$repo_from" "$repo_to"
30
31
fi

Pierre Schmitz's avatar
Pierre Schmitz committed
32
# TODO: this might lock too much (architectures)
33
34
35
for pkgarch in "${ARCHES[@]}"; do
	repo_lock "${repo_to}" "${pkgarch}" || exit 1
	repo_lock "${repo_from}" "${pkgarch}" || exit 1
Pierre Schmitz's avatar
Pierre Schmitz committed
36
37
done

38
# check if packages to be moved exist in version control and ftp dir
39
for pkgbase in "${args[@]:2}"; do
40
41
42
43
44
45
46
47
	found=false
	for pkgarch in "${ARCHES[@]}"; do
		if vcsrepo_from=$(find_repo_for_package "${pkgbase}" "${pkgarch}" "${repo_from}"); then
			#FIXME: abort if PKGBUILD not there
			read -rd'\n' pkgver pkgnames < <(source_pkgbuild "${pkgbase}" "repos/${vcsrepo_from}"; \
			    get_full_version; echo "${pkgname[@]}")
			read -ra pkgnames <<<"$pkgnames"

Eli Schwartz's avatar
Eli Schwartz committed
48
			if (( ${#pkgnames[@]} < 1 )); then
49
50
51
				die "Could not read pkgname"
			fi

Eli Schwartz's avatar
Eli Schwartz committed
52
			if [[ -z ${pkgver} ]]; then
53
54
55
				die "Could not read pkgver"
			fi

56
			if [[ ${check_leapfrog} = true ]] && leapfrog=$(find_repo_for_package "${pkgbase}" "${pkgarch}" "${TESTING_REPOS[@]}"); then
Felix Yan's avatar
Felix Yan committed
57
				die "Package %s cannot be moved from %s to %s while it exists in %s" "${pkgbase}" "${repo_from}" "${repo_to}" "${leapfrog}"
58
59
			fi

60
			for pkgname in "${pkgnames[@]}"; do
61
				getpkgfile "${ftppath_from}/${pkgarch}/${pkgname}-${pkgver}-${vcsrepo_from##*-}"${PKGEXTS} >/dev/null
Pierre Schmitz's avatar
Pierre Schmitz committed
62
			done
63
			found=true
64
65
66
			continue 2
		fi
	done
67
	[[ ${found} = true ]] || die "%s not found in %s" "$pkgbase" "$repo_from"
68
69
done

70
msg "Moving packages from [%s] to [%s]..." "$repo_from" "$repo_to"
Aaron Griffin's avatar
Aaron Griffin committed
71

72
73
74
75
for arch in "${ARCHES[@]}"; do
	declare -a add_pkgs_$arch
	declare -a remove_pkgs_$arch
done
76
for pkgbase in "${args[@]:2}"; do
77
	tag_list=""
78
	for pkgarch in "${ARCHES[@]}" 'any'; do
79
		vcsrepo_from=$(find_repo_for_package "${pkgbase}" "${pkgarch}" "${repo_from}")
Pierre Schmitz's avatar
Pierre Schmitz committed
80

81
		if [[ ${vcsrepo_from} = ${repo_from}-${pkgarch} ]]; then
Eli Schwartz's avatar
Eli Schwartz committed
82
			if [[ ${pkgarch} = any ]]; then
83
				tarches=("${ARCHES[@]}")
84
85
86
			else
				tarches=("${pkgarch}")
			fi
87
			msg2 "%s (%s)" "$pkgbase" "${tarches[*]}"
88
89
90
			read -rd'\n' pkgver pkgnames < <(source_pkgbuild "${pkgbase}" "repos/${repo_from}-${pkgarch}"; \
			    get_full_version; echo "${pkgname[@]}")
			read -ra pkgnames <<<"$pkgnames"
91

92
			vcs_move_package "${pkgbase}" "${repo_from}-${pkgarch}" "${repo_to}-${pkgarch}"
93
			tag_list+=", $pkgarch"
94

95
96
97
98
			for tarch in "${tarches[@]}"; do
				declare -n add_pkgs="add_pkgs_${tarch}"
				declare -n remove_pkgs="remove_pkgs_${tarch}"
				for pkgname in "${pkgnames[@]}"; do
99
					pkgpath=$(getpkgfile "${ftppath_from}/${tarch}/${pkgname}-${pkgver}-${pkgarch}"${PKGEXTS})
Pierre Schmitz's avatar
Pierre Schmitz committed
100
					pkgfile="${pkgpath##*/}"
101

102
					ln -s "../../../${PKGPOOL}/${pkgfile}" "${ftppath_to}/${tarch}/"
Eli Schwartz's avatar
Eli Schwartz committed
103
					if [[ -f ${FTP_BASE}/${PKGPOOL}/${pkgfile}.sig ]]; then
104
						ln -s "../../../${PKGPOOL}/${pkgfile}.sig" "${ftppath_to}/${tarch}/"
105
					fi
106
107
					add_pkgs+=("${FTP_BASE}/${PKGPOOL}/${pkgfile}")
					remove_pkgs+=("${pkgname}")
108
				done
109
			done
110
111
		fi
	done
112
	tag_list="${tag_list#, }"
113
	vcs_commit "${0##*/}: moved ${pkgbase} from [${repo_from}] to [${repo_to}] (${tag_list})"
114
done
Pierre Schmitz's avatar
Pierre Schmitz committed
115

116
for tarch in "${ARCHES[@]}"; do
117
118
	declare -n add_pkgs="add_pkgs_${tarch}"
	declare -n remove_pkgs="remove_pkgs_${tarch}"
119
	if [[ -n ${add_pkgs[*]} ]]; then
120
121
		arch_repo_modify add "${repo_to}" "${tarch}" "${add_pkgs[@]}"
		arch_repo_modify remove "${repo_from}" "${tarch}" "${remove_pkgs[@]}"
122
	fi
123
done
Pierre Schmitz's avatar
Pierre Schmitz committed
124

125
126
127
for pkgarch in "${ARCHES[@]}"; do
	repo_unlock "${repo_from}" "${pkgarch}"
	repo_unlock "${repo_to}" "${pkgarch}"
128
done