feat: optional pkgbuild updates for upgrade mode
@@ -160,6 +160,10 @@ pub mut:
160160 ignoregroup []string @[long: ignoregroup]
161161 disable_timeout bool @[long: disable_timeout]
162162 assume_installed []string @[long: assume_installed]
163+ pkgbuild bool @[long: pkgbuild; short: b]
164+ // nopkgbuildconfirm is install's: the pkgbuild pass asks about every
165+ // candidate, and this takes the yes without asking, review still shown
166+ no_pkgbuild_confirm bool @[long: nopkgbuildconfirm]
163167}
164168
165169struct ConfigFiles {
@@ -2057,6 +2057,98 @@ fn test_a_failed_refresh_is_an_error() ! {
20572057 assert said[0]['error'] or { json2.Any(false) }.bool()
20582058}
20592059
2060+// --pkgbuild on an upgrade: the candidates are the locally installed
2061+// packages the sync dbs do not carry, the held and ignored ones left out,
2062+// and only a pkgbuild version the installed one does not come up to is one
2063+fn test_pkgbuild_upgrade_pass_picks_the_foreign_newer_ones() ! {
2064+ s := fake_system('pkgbuildupgrade')!
2065+ defer { os.rmdir_all(s.tmp) or {} }
2066+ s.install_local(foo('1.0-1', 'one')) or { return error('at install_local: ${err.msg()}') }
2067+ s.install_local(FakePkg{
2068+ name: 'aurpkg'
2069+ version: '1.0-1'
2070+ files: {
2071+ 'usr/share/aurpkg/hello': 'one'
2072+ }
2073+ }) or { return error('at install_local aurpkg: ${err.msg()}') }
2074+ s.publish([foo('2.0-1', 'two')]) or { return error('at publish: ${err.msg()}') }
2075+ mut handle := alpm.initialize(s.path('root') + '/', s.path('db') + '/') or {
2076+ return error('at initialize: ${err.msg()}')
2077+ }
2078+ defer { handle.release() or { went_on('release the alpm handle', err) } }
2079+ cfg_repos = [CfgRepo{
2080+ name: 'core'
2081+ kind: 'sync'
2082+ servers: ['file://' + s.path('repo')]
2083+ }]
2084+ init_repos(mut handle) or { return error('at init_repos: ${err.msg()}') }
2085+ // foo is the sync db's to upgrade; aurpkg the repos do not carry
2086+ mut foreign := foreign_pkgs(handle) or { return error('at foreign_pkgs: ${err.msg()}') }
2087+ assert foreign.map(it.get_name()) == ['aurpkg']
2088+ // the config's held packages stay out, libalpm's own ignore check
2089+ handle.option_add_ignore_pkg('aurpkg') or { return error('at add_ignore: ${err.msg()}') }
2090+ assert foreign_pkgs(handle)!.len == 0
2091+ // the ignore list lives on the handle, so a fresh one reads it all again
2092+ // (option_remove_ignore_pkg answers 1 for a removal, which its wrapper
2093+ // reports as an error)
2094+ mut fresh := alpm.initialize(s.path('root') + '/', s.path('db') + '/') or {
2095+ return error('at initialize: ${err.msg()}')
2096+ }
2097+ defer { fresh.release() or { went_on('release the alpm handle', err) } }
2098+ cfg_repos = [CfgRepo{
2099+ name: 'core'
2100+ kind: 'sync'
2101+ servers: ['file://' + s.path('repo')]
2102+ }]
2103+ init_repos(mut fresh) or { return error('at init_repos: ${err.msg()}') }
2104+ foreign = foreign_pkgs(fresh) or { return error('at foreign_pkgs2: ${err.msg()}') }
2105+ // a newer record is the upgrade; the same and an older are not offered,
2106+ // and a name the sync dbs carry is no candidate whatever its record says
2107+ found := {
2108+ 'aurpkg': PackagePkgbuild{ name: 'aurpkg', version: '1.1-1' }
2109+ 'foo': PackagePkgbuild{ name: 'foo', version: '9.9-1' }
2110+ }
2111+ up := foreign_upgrades(foreign, found)
2112+ assert up.map(it.info.name) == ['aurpkg']
2113+ for v in ['0.9-1', '1.0-1'] {
2114+ assert foreign_upgrades(foreign, {
2115+ 'aurpkg': PackagePkgbuild{ name: 'aurpkg', version: v }
2116+ }).len == 0
2117+ }
2118+}
2119+
2120+// a build installs what it makes, so download-only refuses the pkgbuild pass
2121+// before any of the run's work, as install's does
2122+fn test_upgrade_refuses_download_only_pkgbuild() ! {
2123+ s := fake_system('pkgupgdownonly')!
2124+ defer { os.rmdir_all(s.tmp) or {} }
2125+ s.install_local(foo('1.0-1', 'one'))!
2126+ s.publish([foo('2.0-1', 'two')])!
2127+ up := s.run('', fn () ! {
2128+ upgrade(ConfigUpgrade{ noconfirm: false, download_only: true, pkgbuild: true })!
2129+ })!
2130+ assert up.err.contains('download-only does not build pkgbuild packages'), up.err
2131+ // nothing of the run went ahead: the upgrade is not half-done
2132+ assert s.installed()!['foo']! == '1.0-1'
2133+}
2134+
2135+// --noconfirm would skip the per-package verification a pkgbuild build
2136+// needs — makepkg runs what the PKGBUILD says — so the pass is refused
2137+// before any of the run's work, naming the way to have it anyway
2138+fn test_upgrade_refuses_noconfirm_pkgbuild() ! {
2139+ s := fake_system('pkgupgnoconfirm')!
2140+ defer { os.rmdir_all(s.tmp) or {} }
2141+ s.install_local(foo('1.0-1', 'one'))!
2142+ s.publish([foo('2.0-1', 'two')])!
2143+ up := s.run('', fn () ! {
2144+ upgrade(ConfigUpgrade{ noconfirm: true, pkgbuild: true })!
2145+ })!
2146+ assert up.err.contains('the pkgbuild upgrades are asked about'), up.err
2147+ assert up.err.contains('--nopkgbuildconfirm'), up.err
2148+ // the refusal is said before the sync upgrade, not after it
2149+ assert s.installed()!['foo']! == '1.0-1'
2150+}
2151+
20602152// a mirror that lacks the package falls through to the next server: libalpm
20612153// tries a repo's servers in order, and a file the first one does not have
20622154// moves it on — the way a mirror that has not synced the archive yet is met.
@@ -2086,7 +2178,7 @@ fn test_a_mirror_without_the_package_falls_through_to_the_next() ! {
20862178 // retry, then completes
20872179 retries := up.of('download').filter(it['retry'] or { json2.Any(false) }.bool())
20882180 assert retries.len == 1
2089- assert retries[0]['file'].str() == 'foo-2.0-1-any.pkg.tar'
2181+ assert (retries[0]['file'] or { json2.Any('') }).str() == 'foo-2.0-1-any.pkg.tar'
20902182 // the 404 the lagging mirror got is not said: a mirror served the file
20912183 said := up.of('message').filter(it['text'] or { json2.Any('') }.str().contains('failed retrieving'))
20922184 assert said.len == 0
@@ -2120,7 +2212,7 @@ fn test_verbose_says_the_fallback_error() ! {
21202212 assert s.installed()!['foo']! == '2.0-1'
21212213 said := up.of('message').filter(it['text'] or { json2.Any('') }.str().contains('failed retrieving'))
21222214 assert said.len == 1
2123- assert said[0]['text'].str().contains('from disk')
2215+ assert (said[0]['text'] or { json2.Any('') }).str().contains('from disk')
21242216}
21252217
21262218// check is an assertion inside a run's closure, where a failed assert would
@@ -188,6 +188,8 @@ const help_modes = [
188188 HelpFlag{'--assume-installed <pkg=ver>', 'count a dependency as installed (repeatable, comma separated)'},
189189 HelpFlag{'--overwrite <glob>', 'overwrite conflicting files that match (repeatable, comma separated)'},
190190 HelpFlag{'--dbonly', 'change the database only, not the files on disk'},
191+ HelpFlag{'-b, --pkgbuild', 'after the repos, offer the installed packages the repos do not carry, from the pkgbuild repos, asked one by one'},
192+ HelpFlag{'--nopkgbuildconfirm', 'take yes at the pkgbuild pass without asking, the review still shown'},
191193 HelpFlag{'--disable-timeout', 'let slow downloads take their time'},
192194 HelpFlag{'-p, --print', 'list the archives the upgrade would install, and upgrade nothing'},
193195 HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'},
@@ -196,6 +198,7 @@ const help_modes = [
196198 HelpExample{'vpac u -s', 'refresh the databases and upgrade the system'},
197199 HelpExample{'vpac u --ignore linux', 'upgrade everything but linux'},
198200 HelpExample{'vpac u -s -w', 'refresh, then only download what the upgrade needs'},
201+ HelpExample{'vpac u -s --pkgbuild', 'refresh, upgrade the repos, then ask about the pkgbuild packages'},
199202 HelpExample{'vpac u -uu', 'bring packages newer than the repos back down to them'},
200203 ]
201204 },
@@ -333,11 +333,13 @@ fn pkgbuild_warnings(info PackagePkgbuild) []string {
333333// from the pkgbuild repo at all (no by default), then whether its pkgbuild
334334// is to be read first (yes by default), then the build's own question, asked
335335// whether or not the review was read, so the build can still be declined.
336-// searched says the repos were looked in before, which a -b target's never
337-// were. only then does it install the repo packages the build needs, and
338-// build. returns the archives; a no to a question is a BuildDeclined, which
339-// is the user's answer and not a failure of the build
340-fn review_and_build(mut ui TransUI, user string, node PkgbuildNode, config ConfigInstall, searched bool) ![]string {
336+// the gate is the caller's to word: install asks the generic question, and
337+// an upgrade names the versions it would go from and to. searched says the
338+// repos were looked in before, which a -b target's never were. only then
339+// does it install the repo packages the build needs, and build. returns the
340+// archives; a no to a question is a BuildDeclined, which is the user's
341+// answer and not a failure of the build
342+fn review_and_build(mut ui TransUI, user string, node PkgbuildNode, config ConfigInstall, searched bool, gate string) ![]string {
341343 name := node.info.name
342344 base := node.info.package_base
343345 dir := pkgbuild_build_dir(user, base)!
@@ -354,7 +356,12 @@ fn review_and_build(mut ui TransUI, user string, node PkgbuildNode, config Confi
354356 if searched {
355357 warn('${name} was not found in the repos')
356358 }
357- if ask && !ui.noyes('install ${name} from the pkgbuild repo (${node.repo})?') {
359+ q := if gate != '' {
360+ gate
361+ } else {
362+ 'install ${name} from the pkgbuild repo (${node.repo})?'
363+ }
364+ if ask && !ui.noyes(q) {
358365 return BuildDeclined{}
359366 }
360367 run_as_user(user, ['mkdir', '-p', pkgbuild_root(user)!])!
@@ -519,8 +526,9 @@ fn pkgbuild_plan(handle &alpm.Handle, target string) ![]PkgbuildNode {
519526}
520527
521528// pkgbuild_install builds an AUR package and the AUR packages it needs, reviewing
522-// each one, then installs what was built
523-fn pkgbuild_install(mut ui TransUI, mut handle &alpm.Handle, config ConfigInstall, target string) ! {
529+// each one, then installs what was built. the gate is the first question's
530+// word, the caller's to write — the generic install question when it is empty
531+fn pkgbuild_install(mut ui TransUI, mut handle &alpm.Handle, config ConfigInstall, target string, gate string) ! {
524532 user := invoker_user()!
525533 // the plan is resolved against the installed packages as they are now:
526534 // an earlier target of this run may have been built and installed since
@@ -545,7 +553,7 @@ fn pkgbuild_install(mut ui TransUI, mut handle &alpm.Handle, config ConfigInstal
545553 repo_deps: step.repo_deps
546554 }
547555 archives := review_and_build(mut ui, user, node, config, !(is_target
548- && config.pkgbuild)) or {
556+ && config.pkgbuild), gate) or {
549557 if err is BuildDeclined {
550558 // a no is a quiet stop: nothing after this node can be built
551559 // without it, and nothing went wrong
@@ -337,3 +337,27 @@ fn test_local_now_sees_what_an_older_handle_does_not() ! {
337337 // nothing to install asks for no transaction at all
338338 install_repo_deps([])!
339339}
340+
341+fn test_upgrade_pkgbuild_config_carries_the_transaction() {
342+ up := upgrade_pkgbuild_config(ConfigUpgrade{
343+ noconfirm: true
344+ no_pkgbuild_confirm: true
345+ nodeps: true
346+ nodeps_level: 2
347+ overwrite: ['usr/*']
348+ ignore: ['held']
349+ ignoregroup: ['grp']
350+ disable_timeout: true
351+ })
352+ // the questions are review_and_build's own, the gate's word written by
353+ // the pass: what the command line said of asking is carried as it was
354+ assert up.pkgbuild && up.no_pkgbuild_confirm
355+ assert up.noconfirm && up.nodeps && up.nodeps_level == 2
356+ assert up.overwrite == ['usr/*'] && up.ignore == ['held'] && up.ignoregroup == ['grp']
357+ assert up.disable_timeout
358+ // what the outer run settles itself stays behind
359+ assert !up.needed && !up.print_only && !up.download_only
360+ // without the command line's say, the questions stand
361+ plain := upgrade_pkgbuild_config(ConfigUpgrade{})
362+ assert plain.pkgbuild && !plain.no_pkgbuild_confirm && !plain.noconfirm
363+}
@@ -449,6 +449,6 @@ fn install(config ConfigInstall) ! {
449449 return
450450 }
451451 for target in pkgbuild_builds {
452- pkgbuild_install(mut ui, mut handle, config, target)!
452+ pkgbuild_install(mut ui, mut handle, config, target, '')!
453453 }
454454}
@@ -1,6 +1,17 @@
1-module main
1+import alpm
22
33fn upgrade(config ConfigUpgrade) ! {
4+ // a build installs what it makes: download-only has nothing a pkgbuild
5+ // upgrade serves, as install's says
6+ if config.pkgbuild && config.download_only {
7+ return error('download-only does not build pkgbuild packages')
8+ }
9+ // the pkgbuild pass asks about every candidate one by one, and a build
10+ // runs what a PKGBUILD says: --noconfirm would skip that verification, so
11+ // the refusal comes before any of the run's work, as install's does
12+ if config.pkgbuild && config.noconfirm && !config.no_pkgbuild_confirm {
13+ return error('the pkgbuild upgrades are asked about: drop --noconfirm, or pass --nopkgbuildconfirm')
14+ }
415 // a --print preview asks nothing and takes no lock, as pacman's
516 mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only)
617 ui.print_only = config.print_only
@@ -52,55 +63,219 @@ fn upgrade(config ConfigUpgrade) ! {
5263
5364 end_frame(mut ui, mut look)
5465
66+ // the sync part's names, the pkgbuild pass's added to them
67+ mut upgraded := []string{}
68+
5569 if targets.len == 0 {
56- // --print's output is the list alone
57- if !config.print_only {
70+ // --pkgbuild may still have work; without it, this is the end
71+ if !config.print_only && !config.pkgbuild {
5872 report('nothing to upgrade.')
73+ return
5974 }
60- return
61- }
62-
63- // --print stops here: the upgrade is listed with the archive names, as
64- // install's --print lists a transaction, and nothing is downloaded
65- if config.print_only {
66- if ipc_on() {
67- mut preview := trans_listing(handle, targets, .upgrade)
68- preview.preview = true
69- emit(preview)
75+ } else {
76+ // --print stops here: the upgrade is listed with the archive names, as
77+ // install's --print lists a transaction, and nothing is downloaded
78+ if config.print_only {
79+ if ipc_on() {
80+ mut preview := trans_listing(handle, targets, .upgrade)
81+ preview.preview = true
82+ emit(preview)
83+ return
84+ }
85+ for pkg in targets {
86+ file := pkg.get_filename()
87+ println(if file == '' { pkg.get_name() } else { file })
88+ }
89+ // --pkgbuild lists what the pass would offer, as install's
90+ // --print lists what it would build
91+ if config.pkgbuild {
92+ for u in pkgbuild_upgrade_candidates(mut ui, pkgbuild_repos(), foreign_pkgs(handle)!)! {
93+ report('would upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})')
94+ }
95+ }
7096 return
7197 }
72- for pkg in targets {
73- file := pkg.get_filename()
74- println(if file == '' { pkg.get_name() } else { file })
98+
99+ // what the upgrade removes on the way — a replaced or conflicting
100+ // package — is listed before the question, as install lists its own
101+ removals := handle.trans_get_remove().pkgs()
102+ if removals.len > 0 {
103+ ui.show_packages('${removals.len} packages to remove:', removals)
104+ }
105+ if !trans_confirm(mut ui, handle, targets, .upgrade) {
106+ return
75107 }
76- return
108+
109+ mut s2 := ui.spinner(
110+ text: if config.download_only { 'downloading the upgrades' } else { 'upgrading system' }
111+ )
112+ s2.start()
113+ defer { stop_spinner(mut s2) }
114+ trans_commit(handle)!
115+ stop_spinner(mut s2)
116+ upgraded = targets.map(it.get_name())
77117 }
78118
79- // what the upgrade removes on the way — a replaced or conflicting
80- // package — is listed before the question, as install lists its own
81- removals := handle.trans_get_remove().pkgs()
82- if removals.len > 0 {
83- ui.show_packages('${removals.len} packages to remove:', removals)
119+ // --pkgbuild: the locally installed packages the repos do not carry are
120+ // the pkgbuild repos' to upgrade, asked about one by one. a --print
121+ // preview asks nothing and said its list already
122+ if config.pkgbuild && !config.print_only {
123+ upgraded << upgrade_pkgbuilds(mut ui, mut handle, config)!
84124 }
85- if !trans_confirm(mut ui, handle, targets, .upgrade) {
125+
126+ if upgraded.len == 0 {
86127 return
87128 }
88- names := targets.map(it.get_name())
89129
90- mut s2 := ui.spinner(
91- text: if config.download_only { 'downloading the upgrades' } else { 'upgrading system' }
92- )
93- s2.start()
94- defer { stop_spinner(mut s2) }
95- trans_commit(handle)!
96- stop_spinner(mut s2)
97130 // a frontend learns what changed as from every transaction: a done event
98131 if ipc_on() {
99132 emit(DoneEvent{
100133 action: if config.download_only { 'downloaded' } else { 'upgraded' }
101- packages: names
134+ packages: upgraded
102135 })
103136 return
104137 }
105138 ui.say(if config.download_only { 'upgrades downloaded' } else { 'system upgraded' })
106139}
140+
141+// foreign_pkgs lists the locally installed packages the sync dbs do not
142+// carry, the held and ignored ones left out: the sync upgrade just did its
143+// part around them, and the pkgbuild repos are what could have the rest
144+fn foreign_pkgs(handle &alpm.Handle) ![]&alpm.Package {
145+ sync := handle.get_syncdbs()!
146+ dbs := sync.dbs()
147+ localdb := handle.get_localdb() or {
148+ return error('could not open the local database: ${err.msg()}')
149+ }
150+ mut out := []&alpm.Package{}
151+ cache := pkgcache_of(handle, localdb)!
152+ for pkg in cache.pkgs() {
153+ if find_repo_pkg(dbs, pkg.get_name()) == none && handle.pkg_should_ignore(pkg) == 0 {
154+ out << pkg
155+ }
156+ }
157+ return out
158+}
159+
160+// PkgbuildUpgrade is one locally installed package a pkgbuild repo would
161+// upgrade: the record it would build from, the repo's name, and the version
162+// installed now
163+struct PkgbuildUpgrade {
164+ info PackagePkgbuild
165+ repo string
166+ local string
167+}
168+
169+// foreign_upgrades keeps the foreign packages a record would upgrade: the
170+// record's version must come after the installed one — never a downgrade.
171+// it answers the records, each with the version it would upgrade from
172+fn foreign_upgrades(foreign []&alpm.Package, found map[string]PackagePkgbuild) []PkgbuildUpgrade {
173+ mut out := []PkgbuildUpgrade{}
174+ for pkg in foreign {
175+ rec := found[pkg.get_name()] or { continue }
176+ if alpm.pkg_vercmp(pkg.get_version(), rec.version) >= 0 {
177+ continue
178+ }
179+ out << PkgbuildUpgrade{
180+ info: rec
181+ local: pkg.get_version()
182+ }
183+ }
184+ return out
185+}
186+
187+// pkgbuild_upgrade_candidates asks the pkgbuild repos after the foreign
188+// names, batched per repo in the config's order — the first repo that holds
189+// a name is the one it would build from, as pkgbuild_lookup tries them one
190+// name at a time — and keeps the records that would upgrade
191+fn pkgbuild_upgrade_candidates(mut ui TransUI, repos []CfgRepo, foreign []&alpm.Package) ![]PkgbuildUpgrade {
192+ mut left := foreign.map(it.get_name())
193+ mut found := map[string]PackagePkgbuild{}
194+ mut repo_of := map[string]string{}
195+ mut s := ui.spinner(text: 'looking for pkgbuild upgrades')
196+ s.start()
197+ for repo in repos {
198+ site := repo.servers[0].trim_right('/')
199+ infos := get_pkgbuild_info(site, left) or {
200+ stop_spinner(mut s)
201+ return err
202+ }
203+ mut answered := []string{}
204+ for info in infos {
205+ found[info.name] = info
206+ repo_of[info.name] = repo.name
207+ answered << info.name
208+ }
209+ left = left.filter(it !in answered)
210+ if left.len == 0 {
211+ break
212+ }
213+ }
214+ stop_spinner(mut s)
215+ mut ups := []PkgbuildUpgrade{}
216+ for u in foreign_upgrades(foreign, found) {
217+ ups << PkgbuildUpgrade{
218+ ...u
219+ repo: repo_of[u.info.name]
220+ }
221+ }
222+ return ups
223+}
224+
225+// upgrade_pkgbuild_config is the install a pkgbuild upgrade runs as: the
226+// questions are review_and_build's own, the gate worded by the pass with the
227+// versions, and what the upgrade said of its transaction — the ignores, the
228+// overwrites, the dependency checks, the timeout — holds for it
229+fn upgrade_pkgbuild_config(config ConfigUpgrade) ConfigInstall {
230+ return ConfigInstall{
231+ pkgbuild: true
232+ no_pkgbuild_confirm: config.no_pkgbuild_confirm
233+ noconfirm: config.noconfirm
234+ nodeps: config.nodeps
235+ nodeps_level: config.nodeps_level
236+ overwrite: config.overwrite
237+ assume_installed: config.assume_installed
238+ ignore: config.ignore
239+ ignoregroup: config.ignoregroup
240+ disable_timeout: config.disable_timeout
241+ }
242+}
243+
244+// upgrade_pkgbuilds offers the candidates one by one, each asked the way a
245+// pkgbuild install asks — the upgrade as the gate, then the review, then the
246+// build — and builds and installs the accepted ones. a declined package
247+// stops the pass, as a declined build stops an install. it answers the names
248+// that were upgraded
249+fn upgrade_pkgbuilds(mut ui TransUI, mut handle &alpm.Handle, config ConfigUpgrade) ![]string {
250+ repos := pkgbuild_repos()
251+ if repos.len == 0 {
252+ warn('no pkgbuild repo in the config: the pkgbuild pass has nothing to query')
253+ return []
254+ }
255+ candidates := pkgbuild_upgrade_candidates(mut ui, repos, foreign_pkgs(handle)!)!
256+ if candidates.len == 0 {
257+ return []
258+ }
259+ mut upgraded := []string{}
260+ for u in candidates {
261+ gate := 'upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})?'
262+ pkgbuild_install(mut ui, mut handle, upgrade_pkgbuild_config(config), u.info.name, gate) or {
263+ return err
264+ }
265+ // the local db tells a built package from a declined one: a decline
266+ // is a stop, the way a declined build stops an install
267+ view, installed := local_now()!
268+ built := if local := installed.pkg_find(u.info.name) {
269+ alpm.pkg_vercmp(local.get_version(), u.info.version) >= 0
270+ } else {
271+ false
272+ }
273+ view.release() or { went_on('release the alpm handle', err) }
274+ if !built {
275+ advise('${u.info.name} was declined, the pass stops')
276+ return upgraded
277+ }
278+ upgraded << u.info.name
279+ }
280+ return upgraded
281+}