Message ID | 20250407-b4-pks-meson-install-completions-v1-1-8a7eb8b9284b@pks.im (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | meson: install shell completion scripts | expand |
Hi Patrick, On 2025-04-07 09:42 +0200, Patrick Steinhardt wrote: > Hi, > > this patch is a result from the discussion at [1]. Thanks! > > Patrick > > [1]: <Z-uLqQd7QHZq-tB7@akshay.is> Awesome, thanks for the patch! I applied it on top of git 2.49.0 and can confirm completion scripts get auto-installed at their appropriate locations under datadir. The datadir can also be customized by passing in '-Ddatadir' to `meson setup` so it's pretty flexible. One thing of note is that the git completion script for zsh also depends on the bash completion script. So if you use a non-standard install location like I do (I'm pretty weird, I use macOS with a package manager I've written myself), you'll get an error with the git completion not being able to find the bash script. The fix is to tell zsh where the bash completion script is located. This is also helpfully communicated in the completion script for zsh: # You need git's bash completion script installed. By default bash-completion's # location will be used (e.g. pkg-config --variable=completionsdir bash-completion). # # If your bash completion script is somewhere else, you can specify the # location in your ~/.zshrc: # # zstyle ':completion:*:*:git:*' script ~/.git-completion.bash Adding the zstyle line to my ~/.zshrc made the completion script work without issues. Most people won't run into this since if you have this installed in the standard locations, it should just work, and the zsh script does have logic to look for additional paths it may be under. I just wanted to mention it for info. Cheers,
Akshay Hegde wrote: > Hi Patrick, > > On 2025-04-07 09:42 +0200, Patrick Steinhardt wrote: >> Hi, >> >> this patch is a result from the discussion at [1]. Thanks! >> >> Patrick >> >> [1]: <Z-uLqQd7QHZq-tB7@akshay.is> > > Awesome, thanks for the patch! I applied it on top of git 2.49.0 and can > confirm completion scripts get auto-installed at their appropriate > locations under datadir. The datadir can also be customized by passing > in '-Ddatadir' to `meson setup` so it's pretty flexible. > > One thing of note is that the git completion script for zsh also depends > on the bash completion script. > > So if you use a non-standard install location like I do (I'm pretty > weird, I use macOS with a package manager I've written myself), you'll > get an error with the git completion not being able to find the bash > script. The fix is to tell zsh where the bash completion script is > located. This is also helpfully communicated in the completion script > for zsh: > > # You need git's bash completion script installed. By default bash-completion's > # location will be used (e.g. pkg-config --variable=completionsdir bash-completion). > # > # If your bash completion script is somewhere else, you can specify the > # location in your ~/.zshrc: > # > # zstyle ':completion:*:*:git:*' script ~/.git-completion.bash > > Adding the zstyle line to my ~/.zshrc made the completion script work > without issues. > > Most people won't run into this since if you have this installed in the > standard locations, it should just work, and the zsh script does have > logic to look for additional paths it may be under. I just wanted to > mention it for info. I wonder whether it is proper to install the completion scripts relative to git's $datadir by default. I think the default ought to use the pkg-config call to get the completionsdir variable, as the zsh completion script suggests. I am presuming that's something meson can do rather trivially, just as it would do to find the compile options for git's various build dependencies? I don't know if that becomes too messy to be worthwhile when determining whether git is being installed by a normal user in $HOME or by a privileged user in a system-wide prefix like /usr. (Or, perhaps more confusingly, in /usr/local, while the bash-completion bits are in /usr. /usr/local is one of the prefixes bash-completion uses by default, so that one would happen to work.)
diff --git a/contrib/completion/meson.build b/contrib/completion/meson.build index 3a9ddab5940..019c1457488 100644 --- a/contrib/completion/meson.build +++ b/contrib/completion/meson.build @@ -1,16 +1,42 @@ -foreach script : [ - 'git-completion.bash', - 'git-completion.tcsh', - 'git-completion.zsh', - 'git-prompt.sh' -] +foreach script, config : { + 'git-completion.bash': { + 'filename': 'git', + 'install_dir': get_option('datadir') / 'bash-completion/completions', + }, + 'git-completion.tcsh': {}, + 'git-completion.zsh': { + 'filename': '_git', + 'install_dir': get_option('datadir') / 'zsh/site-functions', + }, + 'git-prompt.sh': {}, +} + # We have to discern between the test dependency and the installed file. Our + # tests assume the completion scripts to have the same name as the in-tree + # files, but the installed filenames need to match the executable's basename. if meson.version().version_compare('>=1.3.0') test_dependencies += fs.copyfile(script) + + if config.has_key('install_dir') + fs.copyfile(script, config.get('filename'), + install: true, + install_dir: config.get('install_dir'), + ) + endif else configure_file( input: script, output: script, copy: true, ) + + if config.has_key('install_dir') + configure_file( + input: script, + output: config.get('filename'), + copy: true, + install: true, + install_dir: config.get('install_dir'), + ) + endif endif endforeach
While Meson has support for _building_ our completion scripts, it does not yet know to also _install_ them. This omission is intentional as it matches the status quo of our Makefile, which doesn't know to install these scripts, either. In fact, our Makefile does not know about these scripts at all: the "build" step that Meson performs is basically just to copy over the files into the build directory, which is required so that our tests know to pick them up for an out-of-tree build. The status quo is somewhat confusing for our users though, as the build steps need to be enabled manually by passing `-Dcontrib=completion` to Meson. So the user explicitly asks for completion scripts, but all they get is that we copy them into the build directory and execute tests. Teach Meson to install completions for Bash and Zsh into the prefix. For now, we try to do the "right thing" and install the scripts into the installation prefix at their canonical paths. These paths should be standardized enough these days so that this works alright for most distributions. If we ever discover that these paths don't work well we can still introduce build options at a later point in time. Signed-off-by: Patrick Steinhardt <ps@pks.im> --- Hi, this patch is a result from the discussion at [1]. Thanks! Patrick [1]: <Z-uLqQd7QHZq-tB7@akshay.is> --- contrib/completion/meson.build | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) --- base-commit: 5b97a56fa0e7d580dc8865b73107407c9b3f0eff change-id: 20250407-b4-pks-meson-install-completions-e5552f1ae2bf