From 6ecabf51a7e6b50dae0593172d6edd8378facdf3 Mon Sep 17 00:00:00 2001 From: diviocity Date: Wed, 11 Nov 2026 00:24:56 +0100 Subject: [PATCH] feat: optional pkgbuild updates for upgrade mode --- src/cli.v | 4 + src/frontend_test.v | 96 ++++++++++++++- src/help.v | 3 + src/pkgbuild_install.v | 26 ++-- src/pkgbuild_install_test.v | 24 ++++ src/remote.v | 2 +- src/upgrade.v | 239 +++++++++++++++++++++++++++++++----- 7 files changed, 350 insertions(+), 44 deletions(-) diff --git a/src/cli.v b/src/cli.v index d2bd16d..9ff6c4c 100644 --- a/src/cli.v +++ b/src/cli.v @@ -160,6 +160,10 @@ pub mut: ignoregroup []string @[long: ignoregroup] disable_timeout bool @[long: disable_timeout] assume_installed []string @[long: assume_installed] + pkgbuild bool @[long: pkgbuild; short: b] + // nopkgbuildconfirm is install's: the pkgbuild pass asks about every + // candidate, and this takes the yes without asking, review still shown + no_pkgbuild_confirm bool @[long: nopkgbuildconfirm] } struct ConfigFiles { diff --git a/src/frontend_test.v b/src/frontend_test.v index e72bfe5..ba601bb 100644 --- a/src/frontend_test.v +++ b/src/frontend_test.v @@ -2057,6 +2057,98 @@ fn test_a_failed_refresh_is_an_error() ! { assert said[0]['error'] or { json2.Any(false) }.bool() } +// --pkgbuild on an upgrade: the candidates are the locally installed +// packages the sync dbs do not carry, the held and ignored ones left out, +// and only a pkgbuild version the installed one does not come up to is one +fn test_pkgbuild_upgrade_pass_picks_the_foreign_newer_ones() ! { + s := fake_system('pkgbuildupgrade')! + defer { os.rmdir_all(s.tmp) or {} } + s.install_local(foo('1.0-1', 'one')) or { return error('at install_local: ${err.msg()}') } + s.install_local(FakePkg{ + name: 'aurpkg' + version: '1.0-1' + files: { + 'usr/share/aurpkg/hello': 'one' + } + }) or { return error('at install_local aurpkg: ${err.msg()}') } + s.publish([foo('2.0-1', 'two')]) or { return error('at publish: ${err.msg()}') } + mut handle := alpm.initialize(s.path('root') + '/', s.path('db') + '/') or { + return error('at initialize: ${err.msg()}') + } + defer { handle.release() or { went_on('release the alpm handle', err) } } + cfg_repos = [CfgRepo{ + name: 'core' + kind: 'sync' + servers: ['file://' + s.path('repo')] + }] + init_repos(mut handle) or { return error('at init_repos: ${err.msg()}') } + // foo is the sync db's to upgrade; aurpkg the repos do not carry + mut foreign := foreign_pkgs(handle) or { return error('at foreign_pkgs: ${err.msg()}') } + assert foreign.map(it.get_name()) == ['aurpkg'] + // the config's held packages stay out, libalpm's own ignore check + handle.option_add_ignore_pkg('aurpkg') or { return error('at add_ignore: ${err.msg()}') } + assert foreign_pkgs(handle)!.len == 0 + // the ignore list lives on the handle, so a fresh one reads it all again + // (option_remove_ignore_pkg answers 1 for a removal, which its wrapper + // reports as an error) + mut fresh := alpm.initialize(s.path('root') + '/', s.path('db') + '/') or { + return error('at initialize: ${err.msg()}') + } + defer { fresh.release() or { went_on('release the alpm handle', err) } } + cfg_repos = [CfgRepo{ + name: 'core' + kind: 'sync' + servers: ['file://' + s.path('repo')] + }] + init_repos(mut fresh) or { return error('at init_repos: ${err.msg()}') } + foreign = foreign_pkgs(fresh) or { return error('at foreign_pkgs2: ${err.msg()}') } + // a newer record is the upgrade; the same and an older are not offered, + // and a name the sync dbs carry is no candidate whatever its record says + found := { + 'aurpkg': PackagePkgbuild{ name: 'aurpkg', version: '1.1-1' } + 'foo': PackagePkgbuild{ name: 'foo', version: '9.9-1' } + } + up := foreign_upgrades(foreign, found) + assert up.map(it.info.name) == ['aurpkg'] + for v in ['0.9-1', '1.0-1'] { + assert foreign_upgrades(foreign, { + 'aurpkg': PackagePkgbuild{ name: 'aurpkg', version: v } + }).len == 0 + } +} + +// a build installs what it makes, so download-only refuses the pkgbuild pass +// before any of the run's work, as install's does +fn test_upgrade_refuses_download_only_pkgbuild() ! { + s := fake_system('pkgupgdownonly')! + defer { os.rmdir_all(s.tmp) or {} } + s.install_local(foo('1.0-1', 'one'))! + s.publish([foo('2.0-1', 'two')])! + up := s.run('', fn () ! { + upgrade(ConfigUpgrade{ noconfirm: false, download_only: true, pkgbuild: true })! + })! + assert up.err.contains('download-only does not build pkgbuild packages'), up.err + // nothing of the run went ahead: the upgrade is not half-done + assert s.installed()!['foo']! == '1.0-1' +} + +// --noconfirm would skip the per-package verification a pkgbuild build +// needs — makepkg runs what the PKGBUILD says — so the pass is refused +// before any of the run's work, naming the way to have it anyway +fn test_upgrade_refuses_noconfirm_pkgbuild() ! { + s := fake_system('pkgupgnoconfirm')! + defer { os.rmdir_all(s.tmp) or {} } + s.install_local(foo('1.0-1', 'one'))! + s.publish([foo('2.0-1', 'two')])! + up := s.run('', fn () ! { + upgrade(ConfigUpgrade{ noconfirm: true, pkgbuild: true })! + })! + assert up.err.contains('the pkgbuild upgrades are asked about'), up.err + assert up.err.contains('--nopkgbuildconfirm'), up.err + // the refusal is said before the sync upgrade, not after it + assert s.installed()!['foo']! == '1.0-1' +} + // a mirror that lacks the package falls through to the next server: libalpm // tries a repo's servers in order, and a file the first one does not have // 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() ! { // retry, then completes retries := up.of('download').filter(it['retry'] or { json2.Any(false) }.bool()) assert retries.len == 1 - assert retries[0]['file'].str() == 'foo-2.0-1-any.pkg.tar' + assert (retries[0]['file'] or { json2.Any('') }).str() == 'foo-2.0-1-any.pkg.tar' // the 404 the lagging mirror got is not said: a mirror served the file said := up.of('message').filter(it['text'] or { json2.Any('') }.str().contains('failed retrieving')) assert said.len == 0 @@ -2120,7 +2212,7 @@ fn test_verbose_says_the_fallback_error() ! { assert s.installed()!['foo']! == '2.0-1' said := up.of('message').filter(it['text'] or { json2.Any('') }.str().contains('failed retrieving')) assert said.len == 1 - assert said[0]['text'].str().contains('from disk') + assert (said[0]['text'] or { json2.Any('') }).str().contains('from disk') } // check is an assertion inside a run's closure, where a failed assert would diff --git a/src/help.v b/src/help.v index 67ab4af..19cb7b2 100644 --- a/src/help.v +++ b/src/help.v @@ -188,6 +188,8 @@ const help_modes = [ HelpFlag{'--assume-installed ', 'count a dependency as installed (repeatable, comma separated)'}, HelpFlag{'--overwrite ', 'overwrite conflicting files that match (repeatable, comma separated)'}, HelpFlag{'--dbonly', 'change the database only, not the files on disk'}, + HelpFlag{'-b, --pkgbuild', 'after the repos, offer the installed packages the repos do not carry, from the pkgbuild repos, asked one by one'}, + HelpFlag{'--nopkgbuildconfirm', 'take yes at the pkgbuild pass without asking, the review still shown'}, HelpFlag{'--disable-timeout', 'let slow downloads take their time'}, HelpFlag{'-p, --print', 'list the archives the upgrade would install, and upgrade nothing'}, HelpFlag{'--noconfirm', 'take the default at every prompt, never read stdin'}, @@ -196,6 +198,7 @@ const help_modes = [ HelpExample{'vpac u -s', 'refresh the databases and upgrade the system'}, HelpExample{'vpac u --ignore linux', 'upgrade everything but linux'}, HelpExample{'vpac u -s -w', 'refresh, then only download what the upgrade needs'}, + HelpExample{'vpac u -s --pkgbuild', 'refresh, upgrade the repos, then ask about the pkgbuild packages'}, HelpExample{'vpac u -uu', 'bring packages newer than the repos back down to them'}, ] }, diff --git a/src/pkgbuild_install.v b/src/pkgbuild_install.v index 85a16a1..8e65045 100644 --- a/src/pkgbuild_install.v +++ b/src/pkgbuild_install.v @@ -333,11 +333,13 @@ fn pkgbuild_warnings(info PackagePkgbuild) []string { // from the pkgbuild repo at all (no by default), then whether its pkgbuild // is to be read first (yes by default), then the build's own question, asked // whether or not the review was read, so the build can still be declined. -// searched says the repos were looked in before, which a -b target's never -// were. only then does it install the repo packages the build needs, and -// build. returns the archives; a no to a question is a BuildDeclined, which -// is the user's answer and not a failure of the build -fn review_and_build(mut ui TransUI, user string, node PkgbuildNode, config ConfigInstall, searched bool) ![]string { +// the gate is the caller's to word: install asks the generic question, and +// an upgrade names the versions it would go from and to. searched says the +// repos were looked in before, which a -b target's never were. only then +// does it install the repo packages the build needs, and build. returns the +// archives; a no to a question is a BuildDeclined, which is the user's +// answer and not a failure of the build +fn review_and_build(mut ui TransUI, user string, node PkgbuildNode, config ConfigInstall, searched bool, gate string) ![]string { name := node.info.name base := node.info.package_base dir := pkgbuild_build_dir(user, base)! @@ -354,7 +356,12 @@ fn review_and_build(mut ui TransUI, user string, node PkgbuildNode, config Confi if searched { warn('${name} was not found in the repos') } - if ask && !ui.noyes('install ${name} from the pkgbuild repo (${node.repo})?') { + q := if gate != '' { + gate + } else { + 'install ${name} from the pkgbuild repo (${node.repo})?' + } + if ask && !ui.noyes(q) { return BuildDeclined{} } run_as_user(user, ['mkdir', '-p', pkgbuild_root(user)!])! @@ -519,8 +526,9 @@ fn pkgbuild_plan(handle &alpm.Handle, target string) ![]PkgbuildNode { } // pkgbuild_install builds an AUR package and the AUR packages it needs, reviewing -// each one, then installs what was built -fn pkgbuild_install(mut ui TransUI, mut handle &alpm.Handle, config ConfigInstall, target string) ! { +// each one, then installs what was built. the gate is the first question's +// word, the caller's to write — the generic install question when it is empty +fn pkgbuild_install(mut ui TransUI, mut handle &alpm.Handle, config ConfigInstall, target string, gate string) ! { user := invoker_user()! // the plan is resolved against the installed packages as they are now: // 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 repo_deps: step.repo_deps } archives := review_and_build(mut ui, user, node, config, !(is_target - && config.pkgbuild)) or { + && config.pkgbuild), gate) or { if err is BuildDeclined { // a no is a quiet stop: nothing after this node can be built // without it, and nothing went wrong diff --git a/src/pkgbuild_install_test.v b/src/pkgbuild_install_test.v index f2661c4..3aa6a93 100644 --- a/src/pkgbuild_install_test.v +++ b/src/pkgbuild_install_test.v @@ -337,3 +337,27 @@ fn test_local_now_sees_what_an_older_handle_does_not() ! { // nothing to install asks for no transaction at all install_repo_deps([])! } + +fn test_upgrade_pkgbuild_config_carries_the_transaction() { + up := upgrade_pkgbuild_config(ConfigUpgrade{ + noconfirm: true + no_pkgbuild_confirm: true + nodeps: true + nodeps_level: 2 + overwrite: ['usr/*'] + ignore: ['held'] + ignoregroup: ['grp'] + disable_timeout: true + }) + // the questions are review_and_build's own, the gate's word written by + // the pass: what the command line said of asking is carried as it was + assert up.pkgbuild && up.no_pkgbuild_confirm + assert up.noconfirm && up.nodeps && up.nodeps_level == 2 + assert up.overwrite == ['usr/*'] && up.ignore == ['held'] && up.ignoregroup == ['grp'] + assert up.disable_timeout + // what the outer run settles itself stays behind + assert !up.needed && !up.print_only && !up.download_only + // without the command line's say, the questions stand + plain := upgrade_pkgbuild_config(ConfigUpgrade{}) + assert plain.pkgbuild && !plain.no_pkgbuild_confirm && !plain.noconfirm +} diff --git a/src/remote.v b/src/remote.v index 22827e9..7977fb3 100644 --- a/src/remote.v +++ b/src/remote.v @@ -449,6 +449,6 @@ fn install(config ConfigInstall) ! { return } for target in pkgbuild_builds { - pkgbuild_install(mut ui, mut handle, config, target)! + pkgbuild_install(mut ui, mut handle, config, target, '')! } } diff --git a/src/upgrade.v b/src/upgrade.v index 4ac55be..aff27d3 100644 --- a/src/upgrade.v +++ b/src/upgrade.v @@ -1,6 +1,17 @@ -module main +import alpm fn upgrade(config ConfigUpgrade) ! { + // a build installs what it makes: download-only has nothing a pkgbuild + // upgrade serves, as install's says + if config.pkgbuild && config.download_only { + return error('download-only does not build pkgbuild packages') + } + // the pkgbuild pass asks about every candidate one by one, and a build + // runs what a PKGBUILD says: --noconfirm would skip that verification, so + // the refusal comes before any of the run's work, as install's does + if config.pkgbuild && config.noconfirm && !config.no_pkgbuild_confirm { + return error('the pkgbuild upgrades are asked about: drop --noconfirm, or pass --nopkgbuildconfirm') + } // a --print preview asks nothing and takes no lock, as pacman's mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only) ui.print_only = config.print_only @@ -52,55 +63,219 @@ fn upgrade(config ConfigUpgrade) ! { end_frame(mut ui, mut look) + // the sync part's names, the pkgbuild pass's added to them + mut upgraded := []string{} + if targets.len == 0 { - // --print's output is the list alone - if !config.print_only { + // --pkgbuild may still have work; without it, this is the end + if !config.print_only && !config.pkgbuild { report('nothing to upgrade.') + return } - return - } - - // --print stops here: the upgrade is listed with the archive names, as - // install's --print lists a transaction, and nothing is downloaded - if config.print_only { - if ipc_on() { - mut preview := trans_listing(handle, targets, .upgrade) - preview.preview = true - emit(preview) + } else { + // --print stops here: the upgrade is listed with the archive names, as + // install's --print lists a transaction, and nothing is downloaded + if config.print_only { + if ipc_on() { + mut preview := trans_listing(handle, targets, .upgrade) + preview.preview = true + emit(preview) + return + } + for pkg in targets { + file := pkg.get_filename() + println(if file == '' { pkg.get_name() } else { file }) + } + // --pkgbuild lists what the pass would offer, as install's + // --print lists what it would build + if config.pkgbuild { + for u in pkgbuild_upgrade_candidates(mut ui, pkgbuild_repos(), foreign_pkgs(handle)!)! { + report('would upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})') + } + } return } - for pkg in targets { - file := pkg.get_filename() - println(if file == '' { pkg.get_name() } else { file }) + + // what the upgrade removes on the way — a replaced or conflicting + // package — is listed before the question, as install lists its own + removals := handle.trans_get_remove().pkgs() + if removals.len > 0 { + ui.show_packages('${removals.len} packages to remove:', removals) + } + if !trans_confirm(mut ui, handle, targets, .upgrade) { + return } - return + + mut s2 := ui.spinner( + text: if config.download_only { 'downloading the upgrades' } else { 'upgrading system' } + ) + s2.start() + defer { stop_spinner(mut s2) } + trans_commit(handle)! + stop_spinner(mut s2) + upgraded = targets.map(it.get_name()) } - // what the upgrade removes on the way — a replaced or conflicting - // package — is listed before the question, as install lists its own - removals := handle.trans_get_remove().pkgs() - if removals.len > 0 { - ui.show_packages('${removals.len} packages to remove:', removals) + // --pkgbuild: the locally installed packages the repos do not carry are + // the pkgbuild repos' to upgrade, asked about one by one. a --print + // preview asks nothing and said its list already + if config.pkgbuild && !config.print_only { + upgraded << upgrade_pkgbuilds(mut ui, mut handle, config)! } - if !trans_confirm(mut ui, handle, targets, .upgrade) { + + if upgraded.len == 0 { return } - names := targets.map(it.get_name()) - mut s2 := ui.spinner( - text: if config.download_only { 'downloading the upgrades' } else { 'upgrading system' } - ) - s2.start() - defer { stop_spinner(mut s2) } - trans_commit(handle)! - stop_spinner(mut s2) // a frontend learns what changed as from every transaction: a done event if ipc_on() { emit(DoneEvent{ action: if config.download_only { 'downloaded' } else { 'upgraded' } - packages: names + packages: upgraded }) return } ui.say(if config.download_only { 'upgrades downloaded' } else { 'system upgraded' }) } + +// foreign_pkgs lists the locally installed packages the sync dbs do not +// carry, the held and ignored ones left out: the sync upgrade just did its +// part around them, and the pkgbuild repos are what could have the rest +fn foreign_pkgs(handle &alpm.Handle) ![]&alpm.Package { + sync := handle.get_syncdbs()! + dbs := sync.dbs() + localdb := handle.get_localdb() or { + return error('could not open the local database: ${err.msg()}') + } + mut out := []&alpm.Package{} + cache := pkgcache_of(handle, localdb)! + for pkg in cache.pkgs() { + if find_repo_pkg(dbs, pkg.get_name()) == none && handle.pkg_should_ignore(pkg) == 0 { + out << pkg + } + } + return out +} + +// PkgbuildUpgrade is one locally installed package a pkgbuild repo would +// upgrade: the record it would build from, the repo's name, and the version +// installed now +struct PkgbuildUpgrade { + info PackagePkgbuild + repo string + local string +} + +// foreign_upgrades keeps the foreign packages a record would upgrade: the +// record's version must come after the installed one — never a downgrade. +// it answers the records, each with the version it would upgrade from +fn foreign_upgrades(foreign []&alpm.Package, found map[string]PackagePkgbuild) []PkgbuildUpgrade { + mut out := []PkgbuildUpgrade{} + for pkg in foreign { + rec := found[pkg.get_name()] or { continue } + if alpm.pkg_vercmp(pkg.get_version(), rec.version) >= 0 { + continue + } + out << PkgbuildUpgrade{ + info: rec + local: pkg.get_version() + } + } + return out +} + +// pkgbuild_upgrade_candidates asks the pkgbuild repos after the foreign +// names, batched per repo in the config's order — the first repo that holds +// a name is the one it would build from, as pkgbuild_lookup tries them one +// name at a time — and keeps the records that would upgrade +fn pkgbuild_upgrade_candidates(mut ui TransUI, repos []CfgRepo, foreign []&alpm.Package) ![]PkgbuildUpgrade { + mut left := foreign.map(it.get_name()) + mut found := map[string]PackagePkgbuild{} + mut repo_of := map[string]string{} + mut s := ui.spinner(text: 'looking for pkgbuild upgrades') + s.start() + for repo in repos { + site := repo.servers[0].trim_right('/') + infos := get_pkgbuild_info(site, left) or { + stop_spinner(mut s) + return err + } + mut answered := []string{} + for info in infos { + found[info.name] = info + repo_of[info.name] = repo.name + answered << info.name + } + left = left.filter(it !in answered) + if left.len == 0 { + break + } + } + stop_spinner(mut s) + mut ups := []PkgbuildUpgrade{} + for u in foreign_upgrades(foreign, found) { + ups << PkgbuildUpgrade{ + ...u + repo: repo_of[u.info.name] + } + } + return ups +} + +// upgrade_pkgbuild_config is the install a pkgbuild upgrade runs as: the +// questions are review_and_build's own, the gate worded by the pass with the +// versions, and what the upgrade said of its transaction — the ignores, the +// overwrites, the dependency checks, the timeout — holds for it +fn upgrade_pkgbuild_config(config ConfigUpgrade) ConfigInstall { + return ConfigInstall{ + pkgbuild: true + no_pkgbuild_confirm: config.no_pkgbuild_confirm + noconfirm: config.noconfirm + nodeps: config.nodeps + nodeps_level: config.nodeps_level + overwrite: config.overwrite + assume_installed: config.assume_installed + ignore: config.ignore + ignoregroup: config.ignoregroup + disable_timeout: config.disable_timeout + } +} + +// upgrade_pkgbuilds offers the candidates one by one, each asked the way a +// pkgbuild install asks — the upgrade as the gate, then the review, then the +// build — and builds and installs the accepted ones. a declined package +// stops the pass, as a declined build stops an install. it answers the names +// that were upgraded +fn upgrade_pkgbuilds(mut ui TransUI, mut handle &alpm.Handle, config ConfigUpgrade) ![]string { + repos := pkgbuild_repos() + if repos.len == 0 { + warn('no pkgbuild repo in the config: the pkgbuild pass has nothing to query') + return [] + } + candidates := pkgbuild_upgrade_candidates(mut ui, repos, foreign_pkgs(handle)!)! + if candidates.len == 0 { + return [] + } + mut upgraded := []string{} + for u in candidates { + gate := 'upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})?' + pkgbuild_install(mut ui, mut handle, upgrade_pkgbuild_config(config), u.info.name, gate) or { + return err + } + // the local db tells a built package from a declined one: a decline + // is a stop, the way a declined build stops an install + view, installed := local_now()! + built := if local := installed.pkg_find(u.info.name) { + alpm.pkg_vercmp(local.get_version(), u.info.version) >= 0 + } else { + false + } + view.release() or { went_on('release the alpm handle', err) } + if !built { + advise('${u.info.name} was declined, the pass stops') + return upgraded + } + upgraded << u.info.name + } + return upgraded +} -- 2.56.1