@@ -90,22 +90,34 @@ get_sources_format() {
9090 echo " $sources_format "
9191}
9292
93+ # Function to get sources file extension.
94+ get_sources_extension () {
95+ [ " $1 " = " deb822" ] && echo " sources" || echo " list"
96+ }
97+
98+ # Function to escape regex special characters.
9399escape_regex () {
94100 printf ' %s' " $1 " | sed -e ' s/[][\.^$*+?{}()|\/]/\\&/g'
95101}
96102
103+ # Function to merge two components strings.
97104merge_components () {
98105 local out=() t
99106 for t in $1 $2 ; do [[ $t && " ${out[*]} " != * " $t " * ]] && out+=(" $t " ); done
100107 printf ' %s\n' " ${out[*]} "
101108}
102109
110+ # Function to merge components from a file.
103111merge_components_from_file () {
104112 local path=$1
105113 local incoming=$2
106114 local current=
107115 if [ -n " $path " ] && [ -e " $path " ]; then
108- current=" $( grep -E ' ^Components:' " $path " | head -n 1 | cut -d ' :' -f 2 | xargs) "
116+ if grep -Eq ' ^Components:' " $path " ; then
117+ current=" $( grep -E ' ^Components:' " $path " | head -n 1 | cut -d ' :' -f 2 | xargs) "
118+ else
119+ current=" $( sed -E -n ' s/^deb[[:space:]]+(\[[^]]*\][[:space:]]+)?[^[:space:]]+[[:space:]]+[^[:space:]]+[[:space:]]+//p' " $path " | head -n 1 | xargs) "
120+ fi
109121 fi
110122 local merged
111123 merged=" $( merge_components " $current " " $incoming " ) "
@@ -127,12 +139,14 @@ get_repo_patterns() {
127139 escaped_url=" $( escape_regex " $ppa_url " ) "
128140 escaped_dist=" $( escape_regex " $package_dist " ) "
129141 escaped_branches=" $( escape_regex " $branches " ) "
130- local deb_pattern=" ^deb .*${escaped_url} ${escaped_dist} .*${escaped_branches} $"
131- local deb822_pattern=" ^URIs: ${escaped_url} $"
142+ local deb_primary=" ^deb[[:space:]]+(\\ [[^]]*\\ ][[:space:]]+)?${escaped_url} [[:space:]]+${escaped_dist} [[:space:]]"
143+ local deb_secondary=" ^deb[[:space:]]+(\\ [[^]]*\\ ][[:space:]]+)?${escaped_url} [[:space:]]+${escaped_dist} [[:space:]]+.*${escaped_branches} ([[:space:]]|$)"
144+ local deb822_primary=" ^URIs: ${escaped_url} $"
145+ local deb822_secondary=" ^Suites: ${escaped_dist} $"
132146 if [ " $list_format " = " deb822" ]; then
133- printf ' %s|%s\n' " $deb822_pattern " " $deb_pattern "
147+ printf ' %s|%s\n' " $deb822_primary " " $deb822_secondary "
134148 else
135- printf ' %s|%s\n' " $deb_pattern " " $deb822_pattern "
149+ printf ' %s|%s\n' " $deb_primary " " $deb_secondary "
136150 fi
137151}
138152
@@ -176,26 +190,22 @@ add_key() {
176190 fi
177191}
178192
193+ # Function to handle existing list files.
179194handle_existing_list () {
180195 local ppa=$1
181196 local list_format=$2
182- branches=$3
183- [[ " $list_format " = " deb822" && -n " $check_lists_file " ]] || {
184- echo " Repository $ppa ($branches ) already exists"
185- return 1
186- }
187- [[ " $check_lists_file " = * .list ]] && {
188- sudo rm -f " $check_lists_file "
189- return 0
190- }
197+ local branches=$3
191198 local merged_components
192- merged_components=" $( merge_components_from_file " $check_lists_file " " $branches " ) " && {
193- sudo rm -f " $check_lists_file "
194- branches=" $merged_components "
195- return 0
196- }
197- echo " Repository $ppa ($branches ) already exists"
198- return 1
199+ if [ -z " $check_lists_file " ]; then
200+ echo " Repository $ppa ($branches ) already exists" && return 1
201+ fi
202+ if merged_components=" $( merge_components_from_file " $check_lists_file " " $branches " ) " ; then
203+ sudo rm -f " $check_lists_file " && printf ' %s\n' " $merged_components " && return 0
204+ fi
205+ if [[ " $list_format " = " deb822" && " $check_lists_file " = * .list ]]; then
206+ sudo rm -f " $check_lists_file " && printf ' %s\n' " $branches " && return 0
207+ fi
208+ echo " Repository $ppa ($branches ) already exists" && return 1
199209}
200210
201211# Function to write a list file.
@@ -258,22 +268,30 @@ add_list() {
258268 package_dist=${4:- " $VERSION_CODENAME " }
259269 branches=${5:- main}
260270 local list_format
261- list_format= " $( get_sources_format ) "
271+ local list_extension
262272 local status_token
263- status_token= " ${ppa_url} | ${package_dist} | ${branches} "
273+ local resolved_branches
264274 local list_path=
275+ list_format=" $( get_sources_format) "
276+ list_extension=" $( get_sources_extension " $list_format " ) "
277+ status_token=" ${ppa_url} |${package_dist} |${branches} "
265278 IFS=' |' read -r primary_pattern secondary_pattern <<< " $(get_repo_patterns " $list_format " " $ppa_url " " $package_dist " " $branches " )"
266279 if check_lists " $ppa " " $primary_pattern " " $secondary_pattern " " $status_token " ; then
267280 list_path=" $check_lists_file "
268- handle_existing_list " $ppa " " $list_format " " $branches " || return 1;
281+ if resolved_branches=" $( handle_existing_list " $ppa " " $list_format " " $branches " ) " ; then
282+ branches=" $resolved_branches "
283+ else
284+ [ -n " $resolved_branches " ] && echo " $resolved_branches " && return 1
285+ fi
269286 check_lists_file=
270287 IFS=' |' read -r primary_pattern secondary_pattern <<< " $(get_repo_patterns " $list_format " " $ppa_url " " $package_dist " " $branches " )"
271288 status_token=" ${ppa_url} |${package_dist} |${branches} "
272289 fi
273290 [ -e " $key_source " ] && key_file=$key_source || key_file=" $key_dir " /" ${ppa/ \/ / -} " -keyring.gpg
274291 add_key " $ppa " " $ppa_url " " $package_dist " " $key_source " " $key_file "
275292 write_list " $list_format " " $ppa " " $ppa_url " " $package_dist " " $branches " " $key_file "
276- update_lists " $ppa " " $primary_pattern " " $status_token "
293+ list_path=" $list_dir " /" ${ppa%%/* } " -" $ID " -" ${ppa#*/ } " -" $package_dist " ." $list_extension "
294+ update_lists " $ppa " " $list_path " " $status_token "
277295 . /etc/os-release
278296 return 0;
279297}
0 commit comments