Created
December 4, 2022 16:58
-
-
Save meros/74ce957ed9102e67b76d546fc07c3c77 to your computer and use it in GitHub Desktop.
This script is a command-line utility that uses fzf to quickly navigate to a Git repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set the default directory to search | |
search_dir=~/git | |
# Check if a directory was specified as an argument | |
if [ $# -gt 0 ]; then | |
# Use the specified directory as the search directory | |
search_dir=$1 | |
fi | |
# Check if fzf is installed | |
if ! [ -x "$(command -v fzf)" ]; then | |
# Check if brew is available | |
if [ -x "$(command -v brew)" ]; then | |
# Install fzf using brew if it is not installed | |
brew install fzf | |
else | |
# Print a message explaining how to install fzf manually | |
echo "❌ fzf is not installed. To install it, please follow the instructions at https://github.com/junegunn/fzf#using-git" | |
exit 1 | |
fi | |
fi | |
# Use the find command to search for directories that contain a .git subdirectory | |
# Use the -maxdepth 3 option to limit the search to only 3 folders deep | |
# Use sed to extract only the directory name from each line of the output | |
# Use fzf to let the user select a directory from the list | |
selected_dir=$(find $search_dir -type d -maxdepth 3 -name .git -prune | sed -E "s/.*\/(.*)\/.git/\1/" | fzf --no-clear --prompt="Select a git repository: ") | |
# Check if a directory was selected | |
if [ -z "$selected_dir" ]; then | |
# Print an error message if no directory was selected | |
echo "❌ No git repository was selected. Exiting script." | |
exit 1 | |
fi | |
# Change to the selected directory | |
cd $selected_dir | |
# Print a success message | |
echo "✅ Successfully changed to directory: $selected_dir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment