\$\begingroup\$
\$\endgroup\$
here's the updated version of the code for being reviewed. Fixes:
- "private"
_cg
function - functions definition as
cg() {}
- general clean-up
#!/bin/bash
# cd to the root of the current git directory
# If $PWD is submodule, will cd to the root of the top ancestor
# It requires to stay in the current directory, if the root is . or unknown,
# and use cd only once, to have a way to do `cd -`
cg() {
_cg() {
local top; top="$(git rev-parse --show-cdup)"
top="${top:-./}"
local super_root; super_root="$(git rev-parse --show-superproject-working-tree)"
if [[ "$super_root" ]]; then
printf '%s' "$top../"
( cd "$top../" && _cg || return )
fi
printf '%s' "$top"
}
local git_root
git_root="$(_cg)"
[[ "x${git_root}" != "x./" ]] && cd "${git_root}" && return || return 0
}
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
This is looking great. Thanks for incorporating the previous improvements and coming back for another round of review. I noticed a few new things this time:
- What does
printf '%s'
do for you thatecho -n
wouldn't? Usingprintf
when you're just passing through a string seems a little odd to me. It should work either way, butprintf
would be more idiomatic for C whereecho
is much more common in shell scripts. - Adding the
x
constants in the comparison[[ "x${git_root}" != "x./" ]]
are not needed. Using the double square brackets deals with the disappearing variables. You would need the extrax
for[
comparisons because empty arguments become non-existant arguments which causes an error when you try to compare the non-existant to something. - I would use
-n
in theif [[ "$super_root" ]]; then
line like so:if [[ -n "$super_root" ]]; then
to make clear what you're trying to do there.
answered Nov 30, 2020 at 16:36
-
\$\begingroup\$ I just found that didn't come back with "thank you". Thank you very much for your help! And the objectives behind the implementations: 1) I feel like
echo -n
is less portable thanprintf
, that's why I stuck to it; 2) Thank you, I've learned something new; 3) perfectly makes sense too! \$\endgroup\$Felixoid– Felixoid2021年07月18日 21:44:05 +00:00Commented Jul 18, 2021 at 21:44
lang-bash