Skip to content

Commit

Permalink
gcd: add a "cdg" command for navigating within the current worktree
Browse files Browse the repository at this point in the history
  • Loading branch information
davvid committed Feb 18, 2024
1 parent 24f91bc commit ddc1284
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

`gcd` lets you quickly navigate to Git worktrees on your filesystem.

`cdg` lets you quickly navigate to directories within your current worktree.

![Preview](gcd.gif)

## Dependencies
Expand Down
31 changes: 31 additions & 0 deletions gcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ gcd () {
fi
}

# search for directories inside the current repository and change directories.
# "cdg /" goes to the root of the current repository.
cdg () {
if test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true"
then
return 0
fi
__gcd_initialize
__gcd_curdir="${PWD}"
__gcd_gitdir=$(git rev-parse --show-cdup 2>/dev/null) || return 0
if test -n "${__gcd_gitdir}"
then
if ! cd "${__gcd_gitdir}"
then
__gcd_finalize
return 0
fi
fi
__gcd_dir=$(
(echo . && git ls-tree -r -d --name-only HEAD 2>/dev/null) | __gcd_fzf "$@"
)
__gcd_finalize

if test -n "${__gcd_dir}"
then
cd "${__gcd_dir}" || return 0
else
cd "${__gcd_curdir}" || return 0
fi
}

# Initialize the zsh shell environment.
__gcd_initialize () {
__gcd_restore_zsh_wordsplit=
Expand Down

0 comments on commit ddc1284

Please sign in to comment.