From 714215113fc7add5b31d73a7da7d6eca3d2ccb11 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Thu, 22 May 2025 15:05:23 +0200 Subject: [PATCH] Add switching to Git repositories via cdp. --- home/dotfiles/profile | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/home/dotfiles/profile b/home/dotfiles/profile index 7b87d5a..a54ff47 100644 --- a/home/dotfiles/profile +++ b/home/dotfiles/profile @@ -73,6 +73,7 @@ section_end ############################################################################## # Profile tweaks and features +PROFILE_CDP=${PROFILE_CDP:-1} PROFILE_COMPOSER=${PROFILE_COMPOSER:-$(when_binary_available composer)} PROFILE_CUSTOM=${PROFILE_CUSTOM:-1} PROFILE_DEFAULT_TERM=${PROFILE_DEFAULT_TERM:-1} @@ -92,6 +93,8 @@ PROFILE_SYSTEMD=${PROFILE_SYSTEMD:-$(when_binary_available systemctl)} PROFILE_THEFUCK=${PROFILE_THEFUCK:-$(when_binary_available thefuck)} PROFILE_YARN=${PROFILE_YARN:-$(when_binary_available yarn)} +REPOSITORIES_PATH="${HOME}/Documents/Source" + section "profile variables" log "$(set | grep '^PROFILE_')" section_end @@ -268,3 +271,40 @@ if [ $PROFILE_SYSTEMD -gt 0 ]; then log "$(systemctl --user import-environment PATH)" fi section_end + +section "cdp" +if [ $PROFILE_CDP -gt 0 ]; then + find_git_project_paths() { + local cur + cur="$1" + while read -r found_path; do + echo "$(dirname "$found_path")" + done < <(find "$REPOSITORIES_PATH" -maxdepth 7 -mindepth 2 -type d -path "*/$cur/.git" 2>/dev/null) + } + + cd_project() { + found_dir="$(find_git_project_paths "$1" | head -n1)" + if [ -z "$found_dir" ]; then + echo "Could not find project $1." >&2 + return 1 + fi + cd "$found_dir" + } + + _cd_project() { + local cur + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + while read -r found_path; do + COMPREPLY+=("$(basename "$found_path")") + done < <( + # find git projects + find_git_project_paths "*$cur*" + ) + return 0 + } + complete -F _cd_project cd_project + + alias cdp=cd_project +fi +section_end