Skip to content
Open
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
13 changes: 13 additions & 0 deletions function/colcon_cd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ colcon_cd() {

if [ "$_colcon_cd_root" != "" ]; then
# try to find the given package from the saved path

# Store the previous and current working directory to make the user history make sense
# (As a result of this script using cd to search directories)
_colcon_cd_old_pwd="$OLDPWD"
_colcon_cd_pwd="$(pwd)"

cd "$_colcon_cd_root"

_colcon_cd_pkg_path="$(COLCON_LOG_PATH=/dev/null colcon list --packages-select $1 --paths-only 2> /dev/null)"
Expand All @@ -66,8 +71,11 @@ colcon_cd() {
echo "cd to the first one"
fi
cd "$_colcon_cd_pkg_path"
# Set the OLDPWD to the path on which colcon_cd was invoked, so 'cd -' works as expected.
OLDPWD="$_colcon_cd_pwd"
unset _colcon_cd_pkg_path
unset _colcon_cd_pwd
unset _colcon_cd_old_pwd
return 0
fi
unset _colcon_cd_pkg_path
Expand Down Expand Up @@ -95,6 +103,7 @@ colcon_cd() {
fi
cd "$_colcon_cd_pkg_path"
unset _colcon_cd_pkg_path
unset _colcon_cd_old_pwd
return 0
fi
unset _colcon_cd_pkg_path
Expand All @@ -106,6 +115,10 @@ colcon_cd() {
echo "Could not find package '$1' from the current working" \
"directory" 1>&2
fi
# Restore the OLDPWD to the value from when colcon_cd was invoked, so 'cd -' works as expected.
# This is using the original OLDPWD value, since the colcon_cd failed and no move happend.
OLDPWD="$_colcon_cd_old_pwd"

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.

My understanding is that this change is here so that if no package is found and thus no change in directory is finalized this restores the shell context back as if when running cd normally you tried to cd into a non-existent directory. It might be worth going through and annotating for future contributors when OLDPWD and the internal _colcon_cd_old_pwd variables are set, updated and unset to make the maintenance easier on future contributors.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I added some comments. Are they clear enough?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@nuclearsandwich are the comments enough to address your feedback here?

unset _colcon_cd_old_pwd
return 1

else
Expand Down
Loading