commit:     f425d58d16a71736f6f8f5f3d41d2c898e9b0a92
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue Oct 14 06:46:01 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Tue Oct 14 12:59:09 2025 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f425d58d

Improve the chdir() implementation

Re-implement the chdir() function in a manner that resembles the sample
code provided by POSIX in the APPLICATION USAGE section of cd(1).

This introduces two behavioural changes:

- the -P option shall now be specified
- executing chdir() without any arguments shall now raise an error

Thus, increment GENFUN_API_LEVEL to 2.

Link: 
https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/cd.html#tag_20_14_16
Link: https://austingroupbugs.net/view.php?id=1047
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 functions.sh   | 24 ++++++++++++------------
 test-functions |  1 +
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/functions.sh b/functions.sh
index 771a7bc..0c98e84 100644
--- a/functions.sh
+++ b/functions.sh
@@ -91,22 +91,22 @@ assign()
 #
 chdir()
 {
-       if [ "$#" -eq 1 ]; then
-               case $1 in
-                       '')
-                               _warn_for_args chdir "$1"
-                               return 1
-                               ;;
-                       -)
-                               set -- ./-
-               esac
-       fi
        if [ "${BASH}" ]; then
                # shellcheck disable=3044
                shopt -u cdable_vars
        fi
        # shellcheck disable=1007,2164
-       CDPATH= cd -- "$@"
+       case $1 in
+               '')
+                       printf >&2 'chdir: null directory\n'
+                       false
+                       ;;
+               /*)
+                       cd -P "$1"
+                       ;;
+               *)
+                       CDPATH= cd -P "./$1"
+       esac
 }
 
 #
@@ -1035,7 +1035,7 @@ _warn_for_args()
 # This shall be incremented by one upon any change being made to the public 
API.
 # It was introduced by gentoo-functions-1.7 with an initial value of 1.
 # shellcheck disable=2034
-GENFUN_API_LEVEL=1
+GENFUN_API_LEVEL=2
 
 # If genfun_basedir is unset, set genfun_prefix to the value of EPREFIX, as it
 # was at the time of installing gentoo-functions, before setting genfun_basedir

diff --git a/test-functions b/test-functions
index 16f9481..0aa2f21 100755
--- a/test-functions
+++ b/test-functions
@@ -50,6 +50,7 @@ test_local() {
 test_chdir() {
        set -- \
                ge  1          ''  \
+               ge  1         N/A  \
                ge  1  grandchild  \
                ge  1         var  \
                eq  0          -L  \

Reply via email to