From: =?UTF-8?q?Samo=20Poga=C4=8Dnik?= <samo_pogacnik@t-2.net>
Date: Sat, 19 Oct 2024 18:55:50 +0200
Forwarded: https://github.com/ingydotnet/git-subrepo/pull/639
Subject: Fixed bash-completion integration with git

Using the 'make install' method, the default 'git-core' location
of git-subrepo executable does not automatically integrate git-subrepo
into git's own bash-completion. This change moves git-subrepo executable
with support scripts into /usr/share/git-subrepo. Then a symlink to the
'git-subrepo' executable script has been added into /usr/bin to achieve
recognition of the 'git subrepo' sub-command under the git bash-completion
(through git's: --list-cmds=...,other,...).

Additional adjustment of Makefile and git-subrepo script were made to
make Makefile more generic and to make DESTDIR usage together with
potentially overridden install vars more manageable.
By overriding INSTALL_LIB and INSTALL_EXT to the same path, we achieve
removal of the unnecessary 'git-subrepo.d' subdirectory inside the
/usr/share/git-subrepo path.

Note that non 'make install' ways of installation should work as
before but without the need for the GIT_SUBREPO_ROOT variable.
Potentially, if 'realpath' addition in git-sibrepo change isn't available
on all target systems, readlink could still be used instead.
---
 Makefile        | 29 ++++++++++++++++++-----------
 lib/git-subrepo | 21 ++++-----------------
 2 files changed, 22 insertions(+), 28 deletions(-)

diff --git a/Makefile b/Makefile
index 4b028a5..8dd567f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
 SHELL := bash
+INSTALL ?= install
 
 # Make sure we have git:
 ifeq ($(shell which git),)
@@ -17,9 +18,11 @@ SHARE = share
 
 # Install variables:
 PREFIX ?= /usr/local
-INSTALL_LIB  ?= $(DESTDIR)$(shell git --exec-path)
+INSTALL_BIN  ?= $(PREFIX)/bin
+INSTALL_LIB  ?= $(PREFIX)/share/$(NAME)
 INSTALL_EXT  ?= $(INSTALL_LIB)/$(NAME).d
-INSTALL_MAN1 ?= $(DESTDIR)$(PREFIX)/share/man/man1
+INSTALL_MAN1 ?= $(PREFIX)/share/man/man1
+LINK_REL_DIR := $(shell realpath --relative-to=$(INSTALL_BIN) $(INSTALL_LIB))
 
 # Docker variables:
 DOCKER_TAG ?= 0.0.6
@@ -60,18 +63,22 @@ $(DOCKER_TESTS):
 
 # Install support:
 install:
-	install -d -m 0755 $(INSTALL_LIB)/
-	install -C -m 0755 $(LIB) $(INSTALL_LIB)/
-	install -d -m 0755 $(INSTALL_EXT)/
-	install -C -m 0644 $(EXTS) $(INSTALL_EXT)/
-	install -d -m 0755 $(INSTALL_MAN1)/
-	install -C -m 0644 $(MAN1)/$(NAME).1 $(INSTALL_MAN1)/
+	$(INSTALL) -d -m 0755 $(DESTDIR)$(INSTALL_LIB)/
+	$(INSTALL) -C -m 0755 $(LIB) $(DESTDIR)$(INSTALL_LIB)/
+	sed -i 's!^SUBREPO_EXT_DIR=.*!SUBREPO_EXT_DIR=$(INSTALL_EXT)!' $(DESTDIR)$(INSTALL_LIB)/$(NAME)
+	$(INSTALL) -d -m 0755 $(DESTDIR)$(INSTALL_BIN)
+	ln -s $(LINK_REL_DIR)/$(NAME) $(DESTDIR)$(INSTALL_BIN)/$(NAME)
+	$(INSTALL) -d -m 0755 $(DESTDIR)$(INSTALL_EXT)/
+	$(INSTALL) -C -m 0644 $(EXTS) $(DESTDIR)$(INSTALL_EXT)/
+	$(INSTALL) -d -m 0755 $(DESTDIR)$(INSTALL_MAN1)/
+	$(INSTALL) -C -m 0644 $(MAN1)/$(NAME).1 $(DESTDIR)$(INSTALL_MAN1)/
 
 # Uninstall support:
 uninstall:
-	rm -f $(INSTALL_LIB)/$(NAME)
-	rm -fr $(INSTALL_EXT)
-	rm -f $(INSTALL_MAN1)/$(NAME).1
+	rm -f $(DESTDIR)$(INSTALL_BIN)/$(NAME)
+	rm -fr $(DESTDIR)$(INSTALL_EXT)
+	rm -fr $(DESTDIR)$(INSTALL_LIB)
+	rm -f $(DESTDIR)$(INSTALL_MAN1)/$(NAME).1
 
 env:
 	@echo "export PATH=\"$$PWD/lib:\$$PATH\""
diff --git a/lib/git-subrepo b/lib/git-subrepo
index cfeeae9..ca3c739 100755
--- a/lib/git-subrepo
+++ b/lib/git-subrepo
@@ -12,21 +12,8 @@ set -e
 export FILTER_BRANCH_SQUELCH_WARNING=1
 
 # Import Bash+ helper functions:
-SOURCE=${BASH_SOURCE[0]}
-while [[ -h $SOURCE ]]; do
-  DIR=$( cd -P "$( dirname "$SOURCE" )" && pwd )
-  SOURCE=$(readlink "$SOURCE")
-  [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
-done
-SOURCE_DIR=$(dirname "$SOURCE")
-
-if [[ -z $GIT_SUBREPO_ROOT ]]; then
-  # If `make install` installation used:
-  source "${SOURCE_DIR}/git-subrepo.d/bash+.bash"
-else
-  # If `source .rc` method used:
-  source "${SOURCE_DIR}/../ext/bashplus/lib/bash+.bash"
-fi
+SUBREPO_EXT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")/git-subrepo.d" # replaced by `make install`
+source "${SUBREPO_EXT_DIR}/bash+.bash"
 bash+:import :std can version-check
 
 
@@ -396,7 +383,7 @@ command:config() {
 
 # Launch the manpage viewer:
 command:help() {
-  source "${SOURCE_DIR}/git-subrepo.d/help-functions.bash"
+  source "${SUBREPO_EXT_DIR}/help-functions.bash"
   local cmd=${command_arguments[0]}
   if [[ $cmd ]]; then
     if can "help:$cmd"; then
@@ -1986,7 +1973,7 @@ OK() {
 usage-error() {
   local msg="git-subrepo: $1" usage=
   if [[ $GIT_SUBREPO_TEST_ERRORS != true ]]; then
-    source "${SOURCE_DIR}/git-subrepo.d/help-functions.bash"
+    source "${SUBREPO_EXT_DIR}/help-functions.bash"
     if can "help:$command"; then
       msg=$'\n'"$msg"$'\n'"$("help:$command")"$'\n'
     fi
