Advertisement
Nom1fan

Git worktree flow script

Apr 4th, 2023 (edited)
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.34 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. print_color() {
  4.     local color=$1
  5.     local message=$2
  6.     local reset_color='\033[0m' # ANSI escape code to reset color
  7.     echo -e "${color}${message}${reset_color}"
  8. }
  9.  
  10. info() {
  11.     print_color "\033[32m" "INFO: $*"
  12. }
  13.  
  14. error() {
  15.     print_color "\033[31m" "ERROR: $*"
  16. }
  17.  
  18. set +x # Enable debugging
  19.  
  20. if [ ! -e .git ]; then
  21.     error "'gwt' must be run from a root git repository (.git)."
  22.     exit 1
  23. fi
  24.  
  25. if [[ -z $1 ]]; then
  26.     error "Command parameter must be set"
  27.     exit 1
  28. fi
  29.  
  30. CMD=$1
  31.  
  32. if [ "$CMD" != "help" ] && [ -z "$2" ]; then
  33.     error "Branch parameter must be set"
  34.     exit 1
  35. fi
  36.  
  37. info "Current user: $(whoami)"
  38.  
  39. WORKTREE_DIR=~/worktree
  40.  
  41. if [[ $CMD == add ]]; then
  42.     mkdir -p $WORKTREE_DIR
  43.     info "Worktree root dir: $WORKTREE_DIR"
  44.     BRANCH_NAME=$2
  45.     info "Creating branch dir: $WORKTREE_DIR/$BRANCH_NAME"
  46.     if ! mkdir -p "$WORKTREE_DIR"/"$BRANCH_NAME"; then
  47.         error "Failed to create branch dir: $WORKTREE_DIR/$BRANCH_NAME"
  48.         exit 1
  49.     fi
  50.     info "Branch dir: $WORKTREE_DIR/$BRANCH_NAME created"
  51.  
  52.     if git rev-parse --verify "$BRANCH_NAME" >/dev/null 2>&1; then
  53.         info "Branch: $BRANCH_NAME already exists. Assigning a worktree for it."
  54.         if ! git worktree add "$WORKTREE_DIR"/"$BRANCH_NAME" "$BRANCH_NAME"; then
  55.             error "Failed to assign worktree."
  56.             exit 1
  57.         fi
  58.         cd "$WORKTREE_DIR/$BRANCH_NAME" || {
  59.             error "Failed to change directory to $WORKTREE_DIR/$BRANCH_NAME"
  60.             exit 1
  61.         }
  62.         info "Worktree branch: $WORKTREE_DIR/$BRANCH_NAME assigned"
  63.     else
  64.         info "Creating worktree branch: $BRANCH_NAME"
  65.         if ! git worktree add -b "$BRANCH_NAME" "$WORKTREE_DIR"/"$BRANCH_NAME"; then
  66.             error "Failed to create worktree branch."
  67.             exit 1
  68.         fi
  69.         cd "$WORKTREE_DIR/$BRANCH_NAME" || {
  70.             error "Failed to change directory to $WORKTREE_DIR/$BRANCH_NAME"
  71.             exit 1
  72.         }
  73.         info "Pushing worktree branch to upstream"
  74.         if ! git push --set-upstream origin "$BRANCH_NAME"; then
  75.             error "Failed to push worktree branch to upstream."
  76.             exit 1
  77.         fi
  78.         info "Worktree branch: $BRANCH_NAME created"
  79.     fi
  80.  
  81.     info "Calling jfdev init"
  82.     if ! jfdev init; then
  83.         exit 1
  84.     fi
  85.  
  86.     info "Worktree branch env ready. Please run: 'cd $WORKTREE_DIR/$BRANCH_NAME'. Happy developing!"
  87. elif [[ $CMD == remove ]]; then
  88.     info "Worktree root dir: $WORKTREE_DIR"
  89.     BRANCH_NAME=$2
  90.     info "Removing git worktree: $WORKTREE_DIR/$BRANCH_NAME"
  91.     git worktree remove "$WORKTREE_DIR"/"$BRANCH_NAME"
  92.     info "Git worktree: $WORKTREE_DIR/$BRANCH_NAME removed"
  93.     info "Removing git branch: $BRANCH_NAME"
  94.     git branch -D "$BRANCH_NAME"
  95.     if [[ -d $WORKTREE_DIR/$BRANCH_NAME ]]; then
  96.         info "Removing branch dir: $WORKTREE_DIR/$BRANCH_NAME"
  97.         rm -r ${WORKTREE_DIR/$BRANCH_NAME:?/}
  98.         info "Branch dir: $WORKTREE_DIR/$BRANCH_NAME removed"
  99.     fi
  100. elif [[ $CMD == help ]]; then
  101.     if [ -n "$2" ]; then
  102.         INNER_CMD=$2
  103.         if [ "$INNER_CMD" == "add" ]; then
  104.             info "NAME"
  105.             info "       add - Create/assign a git worktree branch"
  106.             info "SYNOPSIS"
  107.             info "       gwt add [<branch>]"
  108.             info "DESCRIPTION"
  109.             info "       Creates a new git worktree under ~/worktree/<branch> folder or assigns it if branch already exists"
  110.         elif [ "$INNER_CMD" == "remove" ]; then
  111.             info "NAME"
  112.             info "       remove - Removes a git worktree branch"
  113.             info "SYNOPSIS"
  114.             info "       gwt remove [<branch>]"
  115.             info "DESCRIPTION"
  116.             info "       Remove the git worktree, directory and branch"
  117.         else
  118.             error "Invalid command: $INNER_CMD"
  119.         fi
  120.     else
  121.         info "Usage: gwt <cmd> [<args>]"
  122.         info ""
  123.         info "A utility tool for JFrog developers."
  124.         info "Helps create/assign/delete git branches using git worktree and jfdev working with multiple branches in parallel"
  125.         info ""
  126.         info "help      Displays this message"
  127.         info "add       Create/assign a git worktree branch"
  128.         info "remove    Remove the git worktree, directory and git branch"
  129.     fi
  130. else
  131.     error "Command parameter must be add/remove"
  132. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement