From 2fe6646f040e03e27b19708458996030850dc5d8 Mon Sep 17 00:00:00 2001 From: eneller Date: Mon, 4 Mar 2024 21:42:47 +0100 Subject: [PATCH] feat: improve git-root --- .local/bin/git-root | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/.local/bin/git-root b/.local/bin/git-root index a8d538e..6eb27fe 100755 --- a/.local/bin/git-root +++ b/.local/bin/git-root @@ -1,29 +1,9 @@ #!/usr/bin/env bash - -git_root() { - git rev-parse --show-toplevel -} - -# get the relative path of current path according to root of repo -git_root_relative() { - rel=$(git rev-parse --show-prefix) - if [ -z "$rel" ]; then - # git rev-parse --show-prefix will output empty string when we are in the root dir - echo "." - else - echo "$rel" - fi -} - -if test $# -eq 0; then - git_root +# git script that will return the root path of a git project, even if currently in a submodule. +# uses git >= 2.13 --show-superproject-working-tree +superproject=$(git rev-parse --show-superproject-working-tree) +if [ -z "$superproject" ]; then + git rev-parse --show-toplevel; else - case "$1" in - -r|--relative) - git_root_relative - ;; - *) - git_root - ;; - esac -fi \ No newline at end of file + echo "$superproject"; +fi