Last active
January 5, 2023 15:34
-
-
Save beccasaurus/5102373 to your computer and use it in GitHub Desktop.
Quick hack for checkout git branches into different directories (for when I'm actively working on different branches and want different working directories)
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
# Working on lots of different branches of different repos | |
# has been a pain lately, especially when trying to address | |
# issues in multiple branches at once. | |
GOTO_BRANCH_DIR="$HOME/.goto-branch" | |
GOTO_BRANCH_REPOS="$GOTO_BRANCH_DIR/repos" | |
GOTO_BRANCH_BRANCHES="$GOTO_BRANCH_DIR/branches" | |
_goto-branch_usage() { | |
echo "Usage: goto-branch repo-name [branch-name | master]" | |
echo | |
echo "Available repos:" | |
cd "$GOTO_BRANCH_REPOS" | |
for repo in *; do | |
cd "$GOTO_BRANCH_REPOS/$repo" | |
origin_url="`git config --get remote.origin.url`" | |
echo " - `basename $repo` ($origin_url)" | |
done | |
} | |
_goto-branch-init_usage() { | |
echo "Usage: goto-branch-init repo-name git://git-url" | |
} | |
_goto-branch_uninitialized-repo() { | |
echo "Unknown repo: $1" | |
echo "Initialize new repos using git-branch-init." | |
_goto-branch-init_usage | |
} | |
goto-branch-init() { | |
repo_name="$1" | |
repo_url="$2" | |
if [[ -z "$repo_name" || -z "$repo_url" ]]; then | |
_goto-branch-init_usage | |
return | |
fi | |
repo_dir="$GOTO_BRANCH_REPOS/$repo_name" | |
if [[ -d "$repo_dir" ]]; then | |
echo "Repo already initialized: $repo_dir" | |
return | |
fi | |
( | |
mkdir -pv "$GOTO_BRANCH_REPOS" | |
cd "$GOTO_BRANCH_REPOS" | |
git clone "$repo_url" "$repo_name" | |
if [[ -d "$repo_dir" ]]; then | |
echo "Initialized repo: $repo_dir" | |
fi | |
) | |
} | |
# main() | |
goto-branch() { | |
repo_name="$1" | |
if [[ -z "$repo_name" ]]; then | |
_goto-branch_usage | |
return | |
fi | |
branch_name="master" | |
if [[ -n "$2" ]]; then | |
branch_name="$2" | |
fi | |
# Have we initialized this repo? | |
repo_dir="$GOTO_BRANCH_REPOS/$repo_name" | |
if [[ ! -d "$repo_dir" ]]; then | |
_goto-branch_uninitialized-repo "$repo_name" | |
return | |
fi | |
# Have we checked out this branch from this repo? | |
branch_dir="$GOTO_BRANCH_BRANCHES/$repo_name/$branch_name" | |
if [[ ! -d "$branch_dir" ]]; then | |
( | |
# Run a fetch on main repo to keep its DB somewhat up-to-date | |
cd "$repo_dir" | |
git fetch | |
# Copy the main repo to a new directory for this branch | |
mkdir -pv "$GOTO_BRANCH_BRANCHES/$repo_name" | |
cp -r "$repo_dir" "$branch_dir" | |
if [[ -d "$branch_dir" ]]; then | |
cd "$branch_dir" | |
git checkout "$branch_name" | |
echo "Initialized branch: $branch_dir" | |
fi | |
) | |
fi | |
if [[ -d "$branch_dir" ]]; then | |
cd "$branch_dir" | |
else | |
echo "[ERROR] Branch not checkout out? Expected: $branch_dir" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment