From 3af2582729c7b84940327ee4e9146dbc2c3eef0f Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Thu, 8 Oct 2026 12:21:24 +0200 Subject: [PATCH 01/10] bash-completion: Complete option parameters The initial draft did not complete the arguments of options and mixed up several details. Rework the script so that all commands, options and positional arguments complete correctly. - Skip the arguments of -D/--debug-domains and -C/--directory when looking for the command, so that e.g. a file named like a command is not mistaken for it. - Complete only directories for -C/--directory, also in the --directory=DIR form, and nothing for -D/--debug-domains. - Complete the PACKAGE and DEVICE arguments of install by position and only offer one PACKAGE for show. - Offer the help options only directly after a command. - Add the SPDX license header and prefix all variables and functions with _partup_ to avoid polluting the shell environment. Signed-off-by: Martin Schwan --- partup-completion.bash | 259 ++++++++++++++++++++++++----------------- 1 file changed, 152 insertions(+), 107 deletions(-) diff --git a/partup-completion.bash b/partup-completion.bash index 80dd36dd..5d87699f 100644 --- a/partup-completion.bash +++ b/partup-completion.bash @@ -1,119 +1,164 @@ #!/usr/bin/env bash - -partup_commands="install package show" -help_options="-h --help -V --version" -app_options="-q --quiet -d --debug -D --debug-domains" -install_options="-s --skip-checksums" -package_options="-f --force -C --directory" -show_options="-s --size" - -function exists_in_list() +# SPDX-License-Identifier: GPL-3.0-or-later +# +# Bash completion for partup + +_partup_commands="install package show" +_partup_help_options="-h --help -V --version" +_partup_app_options="-q --quiet -d --debug -D --debug-domains" +_partup_install_options="-s --skip-checksums" +_partup_package_options="-f --force -C --directory" +_partup_show_options="-s --size" + +_partup_in_list() { - LIST=$1 - DELIMITER=$2 - VALUE=$3 - [[ "$LIST" =~ ($DELIMITER|^)$VALUE($DELIMITER|$) ]] + local list=$1 + local value=$2 + + [[ " ${list} " == *" ${value} "* ]] } -function add_command_help() +_partup_complete_files() { - if [[ ${previous} == ${command} ]] ; then - # only add help if we are first after command - COMPREPLY+=( $(compgen -W "${help_options}" -- ${current}) ) - fi - if exists_in_list "${help_options}" " " "${previous}" ; then - # if we have choosen help option, nothing should follow - COMPREPLY=() - return 1 - fi - return 0 + local pattern=$1 + local current=$2 + + if [[ -n ${pattern} ]]; then + mapfile -t -O "${#COMPREPLY[@]}" COMPREPLY < <(compgen -f -X "!${pattern}" -- "${current}") + mapfile -t -O "${#COMPREPLY[@]}" COMPREPLY < <(compgen -d -- "${current}") + else + mapfile -t -O "${#COMPREPLY[@]}" COMPREPLY < <(compgen -f -- "${current}") + fi } - _partup_completions() { - local current="" - local previous="" - local command="" - - if (( ${COMP_CWORD} > 0 )) ; then - current="${COMP_WORDS[COMP_CWORD]}" - - if (( ${COMP_CWORD} > 1 )) ; then - # iterate over all COMP_WORDS - for s in "${COMP_WORDS[@]:1}"; do - if exists_in_list "${partup_commands}" " " "${s}" ; then - command="${s}" - fi - done - previous="${COMP_WORDS[COMP_CWORD - 1]}" - fi - fi - - if [[ -z ${command} ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - COMPREPLY+=( $(compgen -W "${partup_commands} ${app_options}" -- ${current}) ) - return 0 - fi - - # we may have things like "partup --debug install ..." - - if [[ ${command} == install ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - - COMPREPLY+=( $(compgen -W "${install_options} ${app_options}" -- ${current}) ) - - # PACKAGE - COMPREPLY+=( $(compgen -f -o plusdirs -X '!*.partup' -- ${current}) ) - - # DEVICE -> if previous ends with partup, complete only from /dev - if [[ ${previous} == *.partup ]] ; then - if [[ -z ${current} ]] ; then - COMPREPLY=( '/dev' ) - return 0 - else - COMPREPLY+=( $(compgen -f -o plusdirs -- ${current} ) ) - fi - fi - - # don't allow options after dev - if [[ ${previous} == /dev/* ]] ; then - COMPREPLY=() - fi - - return 0 - fi - - if [[ ${command} == package ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - - COMPREPLY+=( $(compgen -W "${package_options} ${app_options}" -- ${current}) ) - - # -C, --directory=DIR - # PACKAGE FILES… - - if exists_in_list "${package_options}" " " ${previous} ; then - #if output is previous, a file must follow - COMPREPLY=( $(compgen -f -- ${current}) ) - fi - - return 0 - fi - - if [[ ${command} == show ]] ; then - add_command_help ${command} ${current} ${previous} || return 0 - - COMPREPLY+=( $(compgen -W "${show_options} ${app_options}" -- ${current}) ) - - # PACKAGE - if we do not already have one, complete available partup files - if [[ ${previous} != *.partup ]] ; then - COMPREPLY+=( $(compgen -f -o plusdirs -X '!*.partup' -- ${current}) ) - fi - - return 0 - fi - - return 1 + local current="${COMP_WORDS[COMP_CWORD]}" + local previous="" + local command="" + local -a positionals=() + local i word skip=0 + + COMPREPLY=() + + if (( COMP_CWORD > 0 )); then + previous="${COMP_WORDS[COMP_CWORD - 1]}" + fi + + # Find the command and the positional arguments in front of the cursor. + # Arguments of options that take a value are skipped. + for (( i = 1; i < COMP_CWORD; i++ )); do + word="${COMP_WORDS[i]}" + + if (( skip )); then + skip=0 + continue + fi + + case "${word}" in + -D|--debug-domains|-C|--directory) + skip=1 + continue + ;; + -*) + continue + ;; + esac + + if [[ -z ${command} ]] && _partup_in_list "${_partup_commands}" "${word}"; then + command="${word}" + else + positionals+=( "${word}" ) + fi + done + + # Complete the argument of an option that takes a value. + case "${previous}" in + -D|--debug-domains) + return 0 + ;; + -C|--directory) + if [[ ${command} == package ]]; then + mapfile -t COMPREPLY < <(compgen -d -- "${current}") + return 0 + fi + ;; + esac + + case "${current}" in + --debug-domains=*) + return 0 + ;; + --directory=*) + if [[ ${command} == package ]]; then + mapfile -t COMPREPLY < <(compgen -d -P "--directory=" -- "${current#--directory=}") + return 0 + fi + ;; + esac + + if [[ -z ${command} ]]; then + mapfile -t COMPREPLY < <(compgen -W "${_partup_help_options} ${_partup_commands} ${_partup_app_options}" -- "${current}") + return 0 + fi + + local options="${_partup_app_options}" + + # Only offer help directly after the command + if (( ${#positionals[@]} == 0 )) && [[ ${previous} == "${command}" ]]; then + options+=" ${_partup_help_options}" + fi + + case "${command}" in + install) + options+=" ${_partup_install_options}" + ;; + package) + options+=" ${_partup_package_options}" + ;; + show) + options+=" ${_partup_show_options}" + ;; + esac + + if [[ ${current} == -* ]]; then + mapfile -t COMPREPLY < <(compgen -W "${options}" -- "${current}") + return 0 + fi + + case "${command}" in + install) + # install PACKAGE DEVICE + case ${#positionals[@]} in + 0) + _partup_complete_files "*.partup" "${current}" + ;; + 1) + mapfile -t COMPREPLY < <(compgen -f -- "${current:-/dev/}") + ;; + esac + ;; + package) + # package PACKAGE FILES... + if (( ${#positionals[@]} == 0 )); then + _partup_complete_files "*.partup" "${current}" + else + _partup_complete_files "" "${current}" + fi + ;; + show) + # show PACKAGE + if (( ${#positionals[@]} == 0 )); then + _partup_complete_files "*.partup" "${current}" + fi + ;; + esac + + if [[ -z ${current} ]] && (( ${#COMPREPLY[@]} == 0 )); then + mapfile -t COMPREPLY < <(compgen -W "${options}" -- "${current}") + fi + + return 0 } complete -o filenames -F _partup_completions partup From 7b6e83e18f17dc6a9cd5bc151969367c5d7c6171 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Thu, 8 Oct 2026 12:22:00 +0200 Subject: [PATCH 02/10] meson: Install bash completion script Install the completion script as 'partup' into the bash-completion directory reported by pkg-config. Fall back to the default location below datadir if bash-completion is not installed, so that the script is available for all packaging and not only for Debian. Signed-off-by: Martin Schwan --- meson.build | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/meson.build b/meson.build index d873bd5f..cf07f58c 100644 --- a/meson.build +++ b/meson.build @@ -56,6 +56,22 @@ exec = executable( install : true ) +bash_completion_dep = dependency('bash-completion', required : false) +if bash_completion_dep.found() + bash_completion_dir = bash_completion_dep.get_variable( + 'completionsdir', + pkgconfig_define : ['datadir', get_option('prefix') / get_option('datadir')] + ) +else + bash_completion_dir = get_option('prefix') / get_option('datadir') / 'bash-completion' / 'completions' +endif + +install_data( + 'partup-completion.bash', + install_dir : bash_completion_dir, + rename : 'partup' +) + partup_dep = declare_dependency( dependencies : deps, include_directories : ['src'], From f69206d401634b72455ee76d391ef29a5cc368ab Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Thu, 8 Oct 2026 12:22:04 +0200 Subject: [PATCH 03/10] debian: Install bash completion via meson Meson now installs the completion script, so dh_bash-completion would only duplicate it. Drop the extra install file and the debhelper sequence addition. Keep bash-completion as a build dependency, as meson queries it with pkg-config for the install location. Signed-off-by: Martin Schwan --- debian/partup.bash-completion | 1 - debian/rules | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 debian/partup.bash-completion diff --git a/debian/partup.bash-completion b/debian/partup.bash-completion deleted file mode 100644 index c6b068de..00000000 --- a/debian/partup.bash-completion +++ /dev/null @@ -1 +0,0 @@ -partup-completion.bash diff --git a/debian/rules b/debian/rules index 79d7d8c3..71a79be6 100755 --- a/debian/rules +++ b/debian/rules @@ -3,7 +3,7 @@ export DH_VERBOSE=1 %: - dh $@ --with bash-completion + dh $@ override_dh_auto_configure: rm -rf build From 9a5aee9ceb851729e7e4c754dd7397bf77b4a9b8 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Thu, 8 Oct 2026 12:23:15 +0200 Subject: [PATCH 04/10] doc: usage: Describe shell completion Document the Bash completion script, what it completes and how to use it without installing partup. Signed-off-by: Martin Schwan --- doc/usage.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/usage.rst b/doc/usage.rst index 7d24f51a..cb591a80 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -61,6 +61,19 @@ version .. deprecated:: 5.0.0 Use the global ``-V, --version`` option instead. +Shell Completion +---------------- + +partup ships a completion script for Bash. It completes commands, options and +their arguments, e.g. directories for ``--directory``, ``*.partup`` files for +the *PACKAGE* argument and device paths for the *DEVICE* argument. + +The script is installed together with partup to the ``bash-completion`` +directory of the installation prefix. To use it without installing partup, +source the script ``partup-completion.bash`` from the source tree:: + + source partup-completion.bash + Supported Output Devices ------------------------ From a8d14e4bc9faa60103540e3a049ec728c400f3e7 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Thu, 8 Oct 2026 12:23:15 +0200 Subject: [PATCH 05/10] CHANGELOG: Add bash completion Add the Bash completion script to the release notes of the upcoming release. Signed-off-by: Martin Schwan --- CHANGELOG.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5c97a8f6..1a801425 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,13 @@ - Add a FAQ section to the documentation explaining the ``Filesystem has unsupported feature(s)`` error of ``resize2fs``, caused by the ext4 ``orphan_file`` feature and old e2fsprogs versions. +- Add a Bash completion script, which completes commands, options and their + arguments. It is installed together with partup. + +.. rubric:: Contributors + +`Steffen Hemer `__, +`Martin Schwan `__ .. _release-4.0.0: From 825c278d00dbfaa549ce9a6d4240016271696688 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Fri, 6 Nov 2026 13:32:37 +0100 Subject: [PATCH 06/10] completions: bash: Move completion script to its own directory Keep shell completion scripts in a dedicated completions/ directory with one subdirectory per shell, so that scripts for other shells can be added later without cluttering the top-level directory. Name the script after the command, as it is installed, so that the meson install rule no longer needs to rename it. Update the documentation accordingly. Signed-off-by: Martin Schwan --- partup-completion.bash => completions/bash/partup | 0 doc/usage.rst | 4 ++-- meson.build | 5 ++--- 3 files changed, 4 insertions(+), 5 deletions(-) rename partup-completion.bash => completions/bash/partup (100%) diff --git a/partup-completion.bash b/completions/bash/partup similarity index 100% rename from partup-completion.bash rename to completions/bash/partup diff --git a/doc/usage.rst b/doc/usage.rst index cb591a80..0b60f4ee 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -70,9 +70,9 @@ the *PACKAGE* argument and device paths for the *DEVICE* argument. The script is installed together with partup to the ``bash-completion`` directory of the installation prefix. To use it without installing partup, -source the script ``partup-completion.bash`` from the source tree:: +source the script ``completions/bash/partup`` from the source tree:: - source partup-completion.bash + source completions/bash/partup Supported Output Devices ------------------------ diff --git a/meson.build b/meson.build index cf07f58c..6bee6d1e 100644 --- a/meson.build +++ b/meson.build @@ -67,9 +67,8 @@ else endif install_data( - 'partup-completion.bash', - install_dir : bash_completion_dir, - rename : 'partup' + 'completions/bash/partup', + install_dir : bash_completion_dir ) partup_dep = declare_dependency( From 56f736c6ba8e8a51ea971bf9f03e260097393e05 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Mon, 9 Nov 2026 15:21:18 +0100 Subject: [PATCH 07/10] github: workflows: system-tests: Use sudo when installing partup When installing partup in this workflow, use sudo, as required for files owned by root. Signed-off-by: Martin Schwan --- .github/workflows/system-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/system-tests.yml b/.github/workflows/system-tests.yml index 4525f902..4353d62a 100644 --- a/.github/workflows/system-tests.yml +++ b/.github/workflows/system-tests.yml @@ -19,7 +19,7 @@ jobs: - name: Build and install application run: | meson setup build --buildtype=release - meson install -C build + sudo meson install -C build - name: Setup loopdev and symlink run: | dd if=/dev/zero of=loop-file bs=1M count=100 From 1dc6d7b8a58e22e3eb3cca265c522509976d838d Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Tue, 10 Nov 2026 15:30:44 +0100 Subject: [PATCH 08/10] AUTHORS: Add Steffen Hemer as contributor Add Steffen Hemer as a new contributor for his work on bash completion support. Signed-off-by: Martin Schwan --- AUTHORS.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index d0bc5a3f..eaaf8ea5 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -6,4 +6,5 @@ Martin Schwan Contributors ============ -Leonard Anderweit +* Leonard Anderweit +* Steffen Hemer From 535cff62e99134fa8c4ad3df0d05e6c4c5b134e9 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Wed, 11 Nov 2026 14:23:00 +0100 Subject: [PATCH 09/10] debian: Remove dirs file The debian/dirs file is redundant and not needed, as the directory structure is already created by Meson Build. Signed-off-by: Martin Schwan --- debian/dirs | 1 - 1 file changed, 1 deletion(-) delete mode 100644 debian/dirs diff --git a/debian/dirs b/debian/dirs deleted file mode 100644 index e7724817..00000000 --- a/debian/dirs +++ /dev/null @@ -1 +0,0 @@ -usr/bin From fae2de6ff876b20d52f3164a8e89977d39f19599 Mon Sep 17 00:00:00 2001 From: Martin Schwan Date: Wed, 11 Nov 2026 14:23:56 +0100 Subject: [PATCH 10/10] tools: debian-package: Containerfile: Add bash-completion Add the bash-completion package to the build container for building Debian packages. This is required for bash completion to successfully build into the Debian package. Signed-off-by: Martin Schwan --- tools/debian-package/Containerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/debian-package/Containerfile b/tools/debian-package/Containerfile index 8a819c1b..6ace2010 100644 --- a/tools/debian-package/Containerfile +++ b/tools/debian-package/Containerfile @@ -3,6 +3,7 @@ FROM ubuntu:24.04 ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && \ apt-get install --yes \ + bash-completion \ debhelper \ git \ meson \