Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions linux/compat.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ function load_vsi_compat()
local bash_major_version="${BASH_VERSINFO[0]}"
local bash_minor_version="${bash_major_version}${BASH_VERSINFO[1]}"


#**
# .. var:: bash_behavior_null_pushd
#
# In bash version 5.3 and newer, pushd on an empty string now returns an error instead of true. Additionally it does not push the CWD onto the directory stack.
#
# :Value: * ``0`` ``pushd ""`` returns 0
# * ``1`` ``pushd ""`` returns 1
#
# .. var:: bash_behavior_multiline_caller
#
# In bash version 5.3 and newer, caller returns the first line of a multiline command, whereas before it returned the last line.
#
# :Value: * ``0`` ``caller`` returns the last line of a multiline command
# * ``1`` ``caller`` returns the first line of a multiline command
#**
if [ "${bash_minor_version}" -ge "53" ]; then
bash_behavior_null_pushd=1
bash_behavior_multiline_caller=1
else
bash_behavior_null_pushd=0
bash_behavior_multiline_caller=0
fi

#**
# .. var:: bash_feature_posix_process_substitution
#
Expand Down
199 changes: 103 additions & 96 deletions linux/git_functions.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,36 @@ function _http_to_git_protocol()
fi
}

#**
# .. function:: git_helper_cdup
#
# Helper function to get consistent relative paths from ``--show-cdup``
#**
function git_helper_cdup()
{
# Relative path from the CWD to the root of the current repository
local up_path="$("${GIT}" rev-parse --show-cdup)"
# Remove trailing / which might be inconsistently added
up_path="${up_path%/}"
# handle empty string as .
echo "${up_path:-.}"
}

#**
# .. function:: git_helper_toplevel
#
# Helper function to get consistent relative paths from ``--show-toplevel``
#**
function git_helper_toplevel()
{
# The absolute path to the current repository/submodule work tree; e.g.,
# from /src/vsi_common/docker/recipes/hooks, this command prints
# /src/vsi_common/docker/recipes
local toplevel="$("${GIT}" rev-parse --show-toplevel 2> /dev/null)"
# Strip trailing / (this may be guaranteed by --show-toplevel)
echo "${toplevel%/}"
}

#**
# .. function:: is_submodule
#
Expand All @@ -372,19 +402,14 @@ function _http_to_git_protocol()

function is_submodule()
{
local toplevel="$("${GIT}" rev-parse --show-toplevel 2> /dev/null)"
# Strip trailing / (this may be guaranteed by --show-toplevel)
toplevel="${toplevel%/}"
local toplevel="$(git_helper_toplevel)"
local sm_basename="$(basename "${toplevel}")"
local is_submod=1
pushd "${toplevel}/../" &> /dev/null
# Are we still in a git repo
local superlevel="$("${GIT}" rev-parse --show-toplevel 2> /dev/null)"
local superlevel="$(git_helper_toplevel)"
if [ -n "${superlevel:+set}" ]; then
local prefix="$("${GIT}" rev-parse --show-prefix)"
# Guarantee a trailing / or empty string (this may be guaranteed by
# --show-prefix)
prefix="${prefix:+"${prefix%/}/"}"
local prefix=$("${GIT}" rev-parse --show-prefix)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the trailing / here guaranteed by --show-prefix or something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and no. If it returns a non-empty value, it appears to be guaranteed. But if it's empty, then clearly, no slash

pushd "${superlevel}" &> /dev/null
# In git v1.8.3, git submodule must be run in the toplevel of the
# working tree
Expand All @@ -404,28 +429,6 @@ function is_submodule()
popd &> /dev/null
fi
popd &> /dev/null
#
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ehh. I'd restore

# git rev-parse --git-dir | awk -F '/.git' '{print $1}')"
# The (usually) absolute path to the repository's .git directory or the
# submodule's equivalent (e.g., .git/modules/docker/recipes); e.g., from
# /src/vsi_common/linux, this command prints /src/vsi_common/.git
# git rev-parse --show-toplevel
# The absolute path to the current repository/submodule work tree; e.g.,
# from /src/vsi_common/docker/recipes/hooks, this command prints
# /src/vsi_common/docker/recipes
# git rev-parse --show-cdup
# The relative path from the CWD to the current repository/submodule
# work tree; e.g., from /src/vsi_common/docker/recipes/hooks, this command
# prints ../ Although, from /src/vsi_common/docker/recipes, this
# command prints '' (unfortunately)
# git rev-parse --show-prefix
# The relative path from the current repository/submodule work tree to the
# CWD
# git rev-parse --show-superproject-working-tree (available in git v2.13.7)
# The absolute path to the working tree of the superproject of the current
# submodule; e.g., from /src/vsi_common/docker/recipes/hooks, this command
# prints /src/vsi_common

return "${is_submod}"
}

Expand All @@ -443,7 +446,7 @@ function is_submodule()
# RE Should mirror git_project_git_dir
function git_project_root_dir()
{
local toplevel="$("${GIT}" rev-parse --show-toplevel 2> /dev/null)"
local toplevel="$(git_helper_toplevel)"
if is_submodule; then
pushd "${toplevel}/../" &> /dev/null
git_project_root_dir
Expand Down Expand Up @@ -486,7 +489,7 @@ function git_project_git_dir()
# will (unfortunately/surprisingly) not output anything. (Similarly,
# core.worktree isn't set). Fortunately, in this case, --git-dir is correct
local dir
dir="$("${GIT}" rev-parse --show-toplevel)"
dir="$("${GIT}" rev-parse --show-toplevel)" # Don't use git_helper_toplevel, logic reasons
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh. I don't get it, but def a good comment

if [ -z "${dir:+set}" ]; then
dir="$("${GIT}" rev-parse --git-dir)"
if [ "${dir}" == "." ]; then
Expand Down Expand Up @@ -584,66 +587,61 @@ function git_submodule_summary()
# If we are not in the top-level directory of the repo, change to it and
# re-call this function
# Alternatively, re-write using git_submodule_displaypaths
local up_path="$("${GIT}" rev-parse --show-cdup)"
if [ -n "${up_path:+set}" ]; then
pushd "${up_path}" &> /dev/null
git_submodule_summary
popd &> /dev/null
return
fi

echo "Entering ${PWD}"
local up_path="$(git_helper_cdup)"
pushd "${up_path}" &> /dev/null
echo "Entering ${PWD}"

local OLD_IFS="${IFS}"
IFS=$'\n'
local submodule_keys=($(git_submodule_names))
IFS="${OLD_IFS}"
local OLD_IFS="${IFS}"
IFS=$'\n'
local submodule_keys=($(git_submodule_names))
IFS="${OLD_IFS}"

local sm_key
for sm_key in ${submodule_keys[@]+"${submodule_keys[@]}"}; do
local sm_path="$("${GIT}" config --file .gitmodules --get submodule."${sm_key}".path)"

# List the changes (in commits) to the working tree of a submodule
# cf. git submodule summary, which doesn't show that there is modified or
# staged content, although it does count up differences (ahead/behind)
# RE The implementation for git submodule summary sm_path [1] and this
# command, git diff --submodule=log sm_path [2], are not the same
# RE Unfortunately, both commands only list the first parents [3]
# Alternatively, git -c status.submoduleSummary=True status sm_path is
# similar as well
# REVIEW Because of these limitations, it might be nicer if this function
# entered into each submodule and ran git log... directly (which is not too
# hard, see below), which could show the full set of changes (in commits)
# to the working tree (not only the first parents)
# RE Could even run git status --porcelain=1 to show changes in the working
# tree
#
# [1] https://github.com/git/git/blob/v2.24.1/git-submodule.sh#L777
# [2] https://github.com/git/git/blob/v2.24.1/diff.c#L3400 =>
# https://github.com/git/git/blob/v2.24.1/submodule.c#L613-L640
# https://github.com/git/git/blob/v2.24.1/submodule.c#L554-L563
# [3] https://github.com/git/git/blob/v2.24.1/submodule.c#L450
"${GIT}" diff --submodule=log "${sm_path}"


# git diff --submodule=log SHA~1..SHA sm_path (or equivalently
# git log --patch --submodule=log SHA~1..SHA sm_path) can also be used to
# list the changes (in commits) to a submodule (but again, only the first
# parents) between two commits in the enclosing repository
# RE For example,
# git diff --submodule=log 0088b126~1..0088b126 docker/recipes
# ~= git log --patch --submodule=log 0088b126~1..0088b126 docker/recipes
# vs ( cd docker/recipes && git log --graph 18ca5ee9..fe4f42d4 --first-parent )
# cf. ( cd docker/recipes && git log --graph 18ca5ee9..fe4f42d4 )


if [ -n "${_recursive_summary:+set}" ]; then
pushd "${sm_path}" &>/dev/null
# Depth-first traversal
git_submodule_summary
popd &>/dev/null
fi
done
local sm_key
for sm_key in ${submodule_keys[@]+"${submodule_keys[@]}"}; do
local sm_path="$("${GIT}" config --file .gitmodules --get submodule."${sm_key}".path)"

# List the changes (in commits) to the working tree of a submodule
# cf. git submodule summary, which doesn't show that there is modified or
# staged content, although it does count up differences (ahead/behind)
# RE The implementation for git submodule summary sm_path [1] and this
# command, git diff --submodule=log sm_path [2], are not the same
# RE Unfortunately, both commands only list the first parents [3]
# Alternatively, git -c status.submoduleSummary=True status sm_path is
# similar as well
# REVIEW Because of these limitations, it might be nicer if this function
# entered into each submodule and ran git log... directly (which is not too
# hard, see below), which could show the full set of changes (in commits)
# to the working tree (not only the first parents)
# RE Could even run git status --porcelain=1 to show changes in the working
# tree
#
# [1] https://github.com/git/git/blob/v2.24.1/git-submodule.sh#L777
# [2] https://github.com/git/git/blob/v2.24.1/diff.c#L3400 =>
# https://github.com/git/git/blob/v2.24.1/submodule.c#L613-L640
# https://github.com/git/git/blob/v2.24.1/submodule.c#L554-L563
# [3] https://github.com/git/git/blob/v2.24.1/submodule.c#L450
"${GIT}" diff --submodule=log "${sm_path}"


# git diff --submodule=log SHA~1..SHA sm_path (or equivalently
# git log --patch --submodule=log SHA~1..SHA sm_path) can also be used to
# list the changes (in commits) to a submodule (but again, only the first
# parents) between two commits in the enclosing repository
# RE For example,
# git diff --submodule=log 0088b126~1..0088b126 docker/recipes
# ~= git log --patch --submodule=log 0088b126~1..0088b126 docker/recipes
# vs ( cd docker/recipes && git log --graph 18ca5ee9..fe4f42d4 --first-parent )
# cf. ( cd docker/recipes && git log --graph 18ca5ee9..fe4f42d4 )


if [ -n "${_recursive_summary:+set}" ]; then
pushd "${sm_path}" &>/dev/null
# Depth-first traversal
git_submodule_summary
popd &>/dev/null
fi
done
popd &> /dev/null
}

#**
Expand Down Expand Up @@ -694,8 +692,9 @@ function git_submodule_displaypaths()
recursive_flag=("--recursive")
fi

# Slashes are added because that's how the git 1.8.3 compatibility code was originally written
# Relative path from the CWD to the root of the current repository
local up_path="$("${GIT}" rev-parse --show-cdup)"
local up_path="$(git_helper_cdup)/"
# Relative path from the root of the current repository to the CWD
local relative_cwd="$("${GIT}" rev-parse --show-prefix)"
# Guarantee a trailing / or empty string (this probably is guaranteed by
Expand All @@ -717,6 +716,11 @@ function git_submodule_displaypaths()
local submodule_paths
if [ "${submodule_foreach_rv}" -eq "0" ]; then
pushd "${up_path}" &> /dev/null
# The following logic works based off of these variables being empty instead of `./` for proper output
if [ "${up_path}" = "./" ]; then
up_path=
fi

# In git v1.8.3, git submodule must be run in the toplevel of the working
# tree
# git v1.8.3 does not support git -C
Expand Down Expand Up @@ -818,7 +822,8 @@ function git_submodule_urls()

# git submodule foreach must be run from the top-level working tree in
# git v1.8.3
pushd "$("${GIT}" rev-parse --show-cdup)" &> /dev/null
local git_top_dir="$(git_helper_cdup)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should prob be called up_path or git_up_path. You have something called git_helper_toplevel

pushd "${git_top_dir}" &> /dev/null
# Query the URL from the submodule's repository configuration
# (e.g., .git/modules/docker/recipes/config)
# REVIEW Should i unset GIT_DIR?
Expand Down Expand Up @@ -909,7 +914,8 @@ function git_submodule_names()

# git submodule foreach must be run from the top-level working tree in
# git v1.8.3
pushd "$("${GIT}" rev-parse --show-cdup)" &> /dev/null
local git_top_dir="$(git_helper_cdup)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here again

pushd "${git_top_dir}" &> /dev/null
"${GIT}" submodule foreach --quiet ${recursive_flag[@]+"${recursive_flag[@]}"} 'echo "${name}"'
popd &> /dev/null
#
Expand Down Expand Up @@ -975,7 +981,8 @@ function git_submodule_toplevels()

# git submodule foreach must be run from the top-level working tree in
# git v1.8.3
pushd "$("${GIT}" rev-parse --show-cdup)" &> /dev/null
local git_top_dir="$(git_helper_cdup)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and again

pushd "${git_top_dir}" &> /dev/null
"${GIT}" submodule foreach --quiet ${recursive_flag[@]+"${recursive_flag[@]}"} \
'if [ "${OS-}" = "Windows_NT" ]; then
cygpath "${toplevel}"
Expand Down Expand Up @@ -1169,7 +1176,7 @@ function git_submodule_sync()
# If we are not in the top-level directory of the repo, change to it
# Alternatively, strip the 'git rev-parse --show-cdup' prefix off of each
# in_displaypath
local up_path="$("${GIT}" rev-parse --show-cdup)"
local up_path="$(git_helper_cdup)"
pushd "${up_path}" &> /dev/null
_git_submodule_sync "${remote_name}" "${submodule_names[@]}"
popd &> /dev/null
Expand Down Expand Up @@ -1259,7 +1266,7 @@ function git_submodule_is_published_recursive()
local is_tracked_change_private=0

# If we are not in the top-level directory of the repo, change to it
local up_path="$("${GIT}" rev-parse --show-cdup)"
local up_path="$(git_helper_cdup)"
pushd "${up_path}" &> /dev/null
_git_submodule_is_published_recursive
popd &> /dev/null
Expand Down
5 changes: 4 additions & 1 deletion linux/just_files/docker_functions.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,10 @@ function parse_docker_compose()
# This is one of the few times the directory you are in matters. To create
# an expected default behavior, switch to the JUSTFILE dir before searching
# for the docker compose file.
pushd "${JUST_DOCKER_COMPOSE_DIR-$(dirname "${JUSTFILE}")}" > /dev/null
# "./_" is used so that when JUSTFILE and JUST_DOCKER_COMPOSE_DIR are unset,
# it will use that value that is dirnamed and throws away "/_" and results
# in pushd . to handle the bash 5.3 issue where pushd "" is now an error
Comment on lines +1040 to +1042
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# "./_" is used so that when JUSTFILE and JUST_DOCKER_COMPOSE_DIR are unset,
# it will use that value that is dirnamed and throws away "/_" and results
# in pushd . to handle the bash 5.3 issue where pushd "" is now an error
# "./_" is used so that when JUSTFILE and JUST_DOCKER_COMPOSE_DIR are unset,
# it will dirname that value, which throws away "/_" and results in pushd .
# in order to handle the bash 5.3 issue where pushd "" is now an error

pushd "${JUST_DOCKER_COMPOSE_DIR-$(dirname "${JUSTFILE-./_}")}" > /dev/null
parent_find_files docker-compose.yml docker-compose.yaml
popd > /dev/null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function add_import-repo_just_project()

JUST_LOCAL_SETTINGS=/dev/null source "${VSI_COMMON_DIR}/linux/just_env" /dev/null

source "${VSI_COMMON_DIR}/linux/just_git_airgap_repo.bsh"
source "${VSI_COMMON_DIR}/linux/just_files/just_git_airgap_repo.bsh"

cd "${AIRGAP_MIRROR_CWD}"
function caseify() { defaultify ${@+"${@}"}; }
Expand Down
3 changes: 2 additions & 1 deletion linux/just_files/just_git_functions.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ git_reattach_heads()
{
# git submodule foreach must be run from the top-level working tree in
# git v1.8.3
pushd "$("${GIT}" rev-parse --show-cdup)" &> /dev/null
local git_top_dir="$("${GIT}" rev-parse --show-cdup)"
pushd "${git_top_dir:-.}" &> /dev/null
local submodule_paths
if [ "${#}" -eq "0" ]; then
local OLD_IFS="${IFS}"
Expand Down
3 changes: 2 additions & 1 deletion linux/just_files/singularity_functions.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ function singular_load_env()
# This is one of the few times the directory you are in matters. To create
# an expected default behavior, switch to the JUSTFILE dir before searching
# for the docker compose file.
pushd "${JUST_SINGULAR_COMPOSE_DIR-$(dirname "${JUSTFILE}")}" > /dev/null
# "./_" see docker_functions.bsh for explanation
pushd "${JUST_SINGULAR_COMPOSE_DIR-$(dirname "${JUSTFILE-./_}")}" > /dev/null
parent_find_files singular-compose.env
popd > /dev/null

Expand Down
9 changes: 5 additions & 4 deletions linux/uwecho.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ function uwecho()
# Get the line number
line_number="${line_number%% *}"

# In the case of a multiline command, determine number of lines to backtrack
local lines="$(echo -n ${@+"${@}"} | wc -l)"
line_number="$((line_number-lines))"

if [ "${bash_behavior_multiline_caller}" = "0" ]; then
# In the case of a multiline command, determine number of lines to backtrack
local lines="$(echo -n ${@+"${@}"} | wc -l)"
line_number="$((line_number-lines))"
fi

local source_line="$(sed -n "${line_number}p" "${file_name}")"

Expand Down
Loading
Loading