Permalink
Browse files

Add cloneToWt script

  • Loading branch information...
1 parent e9d703f commit 7f09f3321e9e1dde8bf752ddf4eaa2ba66e3a970 @zolotov zolotov committed Feb 26, 2016
Showing with 45 additions and 0 deletions.
  1. +45 −0 cloneToWt.sh
View
@@ -0,0 +1,45 @@
+#!/bin/bash
+# Creates new worktrees for all IntelliJ IDEA repositories pointing to a new release branch.
+# Usage: ./cloneToWt.sh <target directory> <branch>
+# Restrictions:
+# None of the given Git repositories should have the branch with name <branch>.
+# There should be a remote branch origin/<branch> from which a new local branch will be created by this script.
+# You must use Git 2.5.0 or later.
+
+set -e # Any command which returns non-zero exit code will cause this shell script to exit immediately
+
+if [[ -z "$1" || -z "$2" ]] ; then
+ echo "
+ Usage: ./cloneToWt.sh <target directory> <branch>
+ Example: ./cloneToWt.sh ~/intellij-go-143 143"
+ exit 1
+fi
+
+NEW_REPO="$1"
+BRANCH="$2"
+
+if [[ "$BRANCH" == origin/* ]]; then
+ BRANCH="${BRANCH/origin\//}"
+fi
+
+# Absolute path to directory containing existing IntelliJ IDEA repo (and this script as well)
+OLD_REPO="$(cd "`dirname "$0"`"; pwd)"
+ROOTS=("/")
+
+if [ -d "$NEW_REPO" ]; then
+ echo "Directory '$NEW_REPO' already exists"
+ exit 2
+fi
+
+for ROOT in ${ROOTS[@]}; do
+ if [[ ! -z `git --git-dir="${OLD_REPO}${ROOT}/.git" --work-tree="${OLD_REPO}${ROOT}" branch --list $BRANCH` ]]; then
+ echo "Branch '$BRANCH' already exists in $ROOT"
+ exit 3
+ fi
+done
+
+for ROOT in ${ROOTS[@]}; do
+ git --git-dir="${OLD_REPO}${ROOT}/.git" --work-tree="${OLD_REPO}${ROOT}" worktree add -b $BRANCH "${NEW_REPO}${ROOT}" origin/${BRANCH}
+done
+
+cp -a "$OLD_REPO/.idea/workspace.xml" "$NEW_REPO/.idea/"

2 comments on commit 7f09f33

@ignatov
Contributor

Something like git worktree add -b 143 ../go-143 origin/143?

@zolotov
Contributor

@ignatov simplify it with getting rid of ROOTS processing if you want

Please sign in to comment.