From: =?UTF-8?q?Samo=20Poga=C4=8Dnik?= <samo_pogacnik@t-2.net>
Date: Sun, 24 Nov 2024 11:03:31 +0000
Forwarded: https://github.com/ingydotnet/git-subrepo/pull/639
Subject: Fixing tests running on macos (github)

Using 'readlink -f' instead of 'realpath'.
Added pure shell function to get relative path between two dirs.
Set LC_ALL only for Linux.
Echo 'uname' on 'make test'
---
 Makefile           |  3 ++-
 lib/git-subrepo    |  2 +-
 share/pnrelpath.sh | 25 +++++++++++++++++++++++++
 test/setup         |  4 +++-
 4 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 share/pnrelpath.sh

diff --git a/Makefile b/Makefile
index 7a930e1..486a7a7 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ INSTALL_BIN  ?= $(PREFIX)/bin
 INSTALL_LIB  ?= $(PREFIX)/share/$(NAME)
 INSTALL_EXT  ?= $(INSTALL_LIB)/$(NAME).d
 INSTALL_MAN1 ?= $(PREFIX)/share/man/man1
-LINK_REL_DIR := $(shell realpath --relative-to=$(INSTALL_BIN) $(INSTALL_LIB))
+LINK_REL_DIR := $(shell bash share/pnrelpath.sh $(INSTALL_BIN) $(INSTALL_LIB))
 
 # Docker variables:
 DOCKER_TAG ?= 0.0.6
@@ -49,6 +49,7 @@ help:
 
 .PHONY: test
 test:
+	@echo uname: '$(shell uname)'
 	prove $(prove) $(test)
 
 test-all: test docker-tests
diff --git a/lib/git-subrepo b/lib/git-subrepo
index ca3c739..6ad6bbb 100755
--- a/lib/git-subrepo
+++ b/lib/git-subrepo
@@ -12,7 +12,7 @@ set -e
 export FILTER_BRANCH_SQUELCH_WARNING=1
 
 # Import Bash+ helper functions:
-SUBREPO_EXT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")/git-subrepo.d" # replaced by `make install`
+SUBREPO_EXT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/git-subrepo.d" # replaced by `make install`
 source "${SUBREPO_EXT_DIR}/bash+.bash"
 bash+:import :std can version-check
 
diff --git a/share/pnrelpath.sh b/share/pnrelpath.sh
new file mode 100644
index 0000000..800e822
--- /dev/null
+++ b/share/pnrelpath.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+#
+# from: https://unix.stackexchange.com/questions/573047/how-to-get-the-relative-path-between-two-directories
+#
+# Expects two parameters, source-dir and target-dir, both absolute canonicalized
+# non-empty pathnames, either may be /-ended, neither need exist.
+# Returns result in shell variable $REPLY as a relative path from source-dir
+# to target-dir without trailing /, . if void.
+#
+# Algorithm is from a 2005 comp.unix.shell posting which has now ascended to
+# archive.org.
+
+pnrelpath() {
+    set -- "${1%/}/" "${2%/}/" ''               ## '/'-end to avoid mismatch
+    while [ "$1" ] && [ "$2" = "${2#"$1"}" ]    ## reduce $1 to shared path
+    do  set -- "${1%/?*/}/"  "$2" "../$3"       ## source/.. target ../relpath
+    done
+    REPLY="${3}${2#"$1"}"                       ## build result
+    # unless root chomp trailing '/', replace '' with '.'
+    [ "${REPLY#/}" ] && REPLY="${REPLY%/}" || REPLY="${REPLY:-.}"
+}
+
+pnrelpath "$1" "$2"
+
+echo $REPLY
diff --git a/test/setup b/test/setup
index ac315e9..8faa496 100644
--- a/test/setup
+++ b/test/setup
@@ -2,7 +2,9 @@
 
 set -e
 
-export LC_ALL=C.UTF-8
+if [ "$(uname)" == "Linux" ]; then
+	export LC_ALL=C.UTF-8
+fi
 
 # Get the location of this script
 SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
