Last active
May 8, 2021 11:12
-
-
Save oneshadab/edc75df068c28d9bec2e7f1461b363ad to your computer and use it in GitHub Desktop.
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 -e | |
# --- Globals --- | |
declare default_remote=newscred | |
declare remote | |
declare branch | |
# --- Function definitions --- | |
help() { | |
local -r script_name=$(basename "${BASH_SOURCE[0]}") | |
log "Usage: $script_name [remote_name]:[branch_name]" | |
} | |
review() { | |
remote=$(echo "$1" | awk -F: '{print $1}') | |
branch=$(echo "$1" | awk -F: '{print $2}') | |
ensure_remote_exists | |
log "Fetching branch '$branch' from remote '$remote'" | |
git fetch "$remote" "$branch" | |
log "Switching to branch '$remote/$branch'" | |
git checkout "$remote"/"$branch" | |
} | |
ensure_remote_exists() { | |
local remote_url | |
local existing_remote | |
remote_url=$(git remote get-url $default_remote | sed "s/:.*\//:$remote\//") | |
existing_remote=$(git remote -v | grep "$remote_url" | head -1 | awk '{ print $1 }') | |
if [ -z "$existing_remote" ]; then | |
log "Adding new remote '$remote'" | |
git remote add "$remote" "$remote_url" | |
else | |
log "Using existing remote '$existing_remote'" | |
remote=$existing_remote | |
fi | |
} | |
log() { | |
echo >&2 -e "${1-}" | |
} | |
main() { | |
case $1 in | |
"") | |
help | |
exit 1 | |
;; | |
*) | |
review "$1" | |
;; | |
esac | |
} | |
# --- Start of execution --- | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment