-
-
Save chrismeyersfsu/9c77423f8403b74b5da1ebd2e105cc2a to your computer and use it in GitHub Desktop.
workflow bash script
This file contains hidden or 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
| # Lives in ~/.bash_functions.d/wf | |
| _wf_body() { | |
| local subcommand="$1" | |
| case "$subcommand" in | |
| add) | |
| local repo_path="${2%/}" | |
| local from_branch="$3" | |
| local new_branch repo_name worktree_path | |
| new_branch=$(basename "$PWD") | |
| repo_name=$(basename "$repo_path") | |
| worktree_path="$PWD/$repo_name" | |
| local base_branch | |
| if [[ -n "$from_branch" ]]; then | |
| base_branch="$from_branch" | |
| git -C "$repo_path" worktree add -b "$new_branch" "$worktree_path" "$from_branch" | |
| else | |
| base_branch=$(git -C "$repo_path" symbolic-ref --short HEAD 2>/dev/null || git -C "$repo_path" rev-parse --short HEAD) | |
| git -C "$repo_path" worktree add -b "$new_branch" "$worktree_path" | |
| fi | |
| echo "Branch '$new_branch' created from '$base_branch'" | |
| ;; | |
| remove) | |
| local repo_name worktree_path git_dir main_repo | |
| repo_name=$(basename "${2%/}") | |
| worktree_path="$PWD/$repo_name" | |
| git_dir=$(git -C "$worktree_path" rev-parse --git-common-dir) || return 1 | |
| main_repo=$(dirname "$git_dir") | |
| git -C "$main_repo" worktree remove "$worktree_path" | |
| ;; | |
| removeb) | |
| local branch repo_name worktree_path git_dir main_repo | |
| branch=$(basename "$PWD") | |
| repo_name=$(basename "${2%/}") | |
| worktree_path="$PWD/$repo_name" | |
| git_dir=$(git -C "$worktree_path" rev-parse --git-common-dir) || return 1 | |
| main_repo=$(dirname "$git_dir") | |
| git -C "$main_repo" worktree remove "$worktree_path" | |
| git -C "$main_repo" branch -D "$branch" | |
| ;; | |
| *) | |
| echo "Usage: wf [-v] <add|remove|removeb> <git-repo-path|repo-name> [from-branch]" >&2 | |
| return 1 | |
| ;; | |
| esac | |
| } | |
| _wf_help() { | |
| cat >&2 <<'EOF' | |
| Usage: wf [-v] <add|remove|removeb> <git-repo-path|repo-name> [from-branch] | |
| Options: | |
| -v Verbose: trace commands with set -x | |
| -h, --help Show this help | |
| Subcommands: | |
| add <repo-path> [from-branch] | |
| Create worktree for <repo-path> inside current directory. | |
| New branch name = basename of $PWD | |
| Worktree path = $PWD/<repo-name> | |
| If [from-branch] given, branch off that ref; otherwise off HEAD. | |
| remove <repo-name> | |
| Remove worktree at $PWD/<repo-name>. | |
| Branch is kept. | |
| removeb <repo-name> | |
| Remove worktree at $PWD/<repo-name> AND delete branch. | |
| Branch deleted = basename of $PWD | |
| Examples: | |
| # Inside ~/work/my-feature, add a worktree from myrepo's main: | |
| wf add ~/repos/myrepo main | |
| # Remove the worktree (keep branch): | |
| wf remove myrepo | |
| # Remove worktree and delete branch my-feature from myrepo: | |
| wf removeb myrepo | |
| EOF | |
| } | |
| wf() { | |
| case "$1" in | |
| -h|--help) _wf_help; return 0 ;; | |
| esac | |
| if [[ "$1" == "-v" ]]; then | |
| shift | |
| ( set -x; _wf_body "$@" ) | |
| else | |
| _wf_body "$@" | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment