contrib/completion: install Bash completion

The shell completion scripts in "contrib/completion" are being tested, but none of our build systems support installing them. This is somewhat confusing for Meson, where users can explicitly enable building these scripts via `-Dcontrib=completion`. This option only controlls whether the completions are built and tested against, where "building" is a bit of an euphemism for "copying them into the build directory". Teach both our Makefile and Meson to install our Bash completion script. For now, this is the only completion script that we're installing given that Bash completions "just work" with a canonical well-known location nowadays. Other completion scripts, like for example the one for zsh, don't have a well-known location and/or require extra steps by the user to make them available. As such, we skip installing these scripts for now, but we may do so in the future if we ever figure out a proper way to do this. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 22, 2025 at 10:36 UTC fe35ce2ef83b597f0e0ca9fb0f7662ce018d3443
2 files changed +24
Makefile
+6
@@ -618,6 +618,7 @@ prefix = $(HOME)
618 bindir = $(prefix)/bin
619 mandir = $(prefix)/share/man
620 infodir = $(prefix)/share/info
621 +bash_completion_dir = $(prefix)/share/bash-completion/completions
622 gitexecdir = libexec/git-core
623 mergetoolsdir = $(gitexecdir)/mergetools
624 sharedir = $(prefix)/share
@@ -2325,6 +2326,7 @@ bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
2326 mandir_SQ = $(subst ','\'',$(mandir))
2327 mandir_relative_SQ = $(subst ','\'',$(mandir_relative))
2328 infodir_relative_SQ = $(subst ','\'',$(infodir_relative))
2329 +bash_completion_dir_SQ = $(subst ','\'',$(bash_completion_dir))
2330 perllibdir_SQ = $(subst ','\'',$(perllibdir))
2331 localedir_SQ = $(subst ','\'',$(localedir))
2332 localedir_relative_SQ = $(subst ','\'',$(localedir_relative))
@@ -3569,6 +3571,10 @@ endif
3571 ifneq (,$X)
3572 $(foreach p,$(patsubst %$X,%,$(filter %$X,$(ALL_COMMANDS_TO_INSTALL) $(OTHER_PROGRAMS))), test '$(DESTDIR_SQ)$(gitexec_instdir_SQ)/$p' -ef '$(DESTDIR_SQ)$(gitexec_instdir_SQ)/$p$X' || $(RM) '$(DESTDIR_SQ)$(gitexec_instdir_SQ)/$p';)
3573 endif
3574 +ifndef NO_BASH_COMPLETION
3575 + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(bash_completion_dir_SQ)' && \
3576 + $(INSTALL) -m 644 contrib/completion/git-completion.bash '$(DESTDIR_SQ)$(bash_completion_dir_SQ)/git'
3577 +endif
3578
3579 bindir=$$(cd '$(DESTDIR_SQ)$(bindir_SQ)' && pwd) && \
3580 execdir=$$(cd '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' && pwd) && \
contrib/completion/meson.build
+18
@@ -14,3 +14,21 @@ foreach script : [
14 )
15 endif
16 endforeach
17 +
18 +# We have to discern between the test dependency and the installed file. Our
19 +# tests assume the completion scripts to have the same name as the in-tree
20 +# files, but the installed filenames need to match the executable's basename.
21 +if meson.version().version_compare('>=1.3.0')
22 + fs.copyfile('git-completion.bash', 'git',
23 + install: true,
24 + install_dir: get_option('datadir') / 'bash-completion/completions',
25 + )
26 +else
27 + configure_file(
28 + input: 'git-completion.bash',
29 + output: 'git',
30 + copy: true,
31 + install: true,
32 + install_dir: get_option('datadir') / 'bash-completion/completions',
33 + )
34 +endif