DEV Community

Daniel 📎
Daniel 📎

Posted on

Quick access to the most relevant folder

If anyone else is a terminal fan like I am, I think you might like this. Being part of azure-sdk-for-js has presented me with a new set of challenges, one of them being to navigate deep in folders to get where proposed changes are located. To aid in this, I made this little bash script that changes directory to the parent folder common to all the changes in the current branch. I'm enjoying it so far! Let me know if you like it or if you have any feedback <3

About the name: csd is fine to me since I'm using this a lot, just like one would use cd. You might prefer to use a different name for clarity.

# csd: "changes' directory", as in the directory of the changes.
# Changes the directory to the common parent to all the changes in current branch.
# Specially useful to switch to the folder relevant to the changes that a specific branch or Pull Request makes in a large project.
# By default acts based on the current changes to the current branch, but another target can be specified by passing a single argument.
# Example: csd master # changes to the common parent relative to the master branch.
# Source of the regexp: https://stackoverflow.com/a/17475354
function csd() {
 common_prefix=$(git diff $1 --name-only | sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D') 
 common_parent=${common_prefix%/*}
 cd $(git rev-parse --show-toplevel)/$common_parent
}

Top comments (0)