Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # The common script is only meant to be included from other LLVM helper scripts |
| 4 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 5 | # Verify the compulsory environment variables - these must be set for all the |
| 6 | # helper scripts. |
| 7 | if [[ $LLVM_ROOT = '' ]]; then |
| 8 | echo "Please, define \$LLVM_ROOT to point to the root" |
| 9 | echo "path where the worktree setup should be performed" |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 10 | exit -1 |
| 11 | fi |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 12 | |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 13 | if [[ $LLVM_GITRW = '' ]]; then |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 14 | echo "Please, define \$LLVM_GITRW to yes if you" |
| 15 | echo "want to set up Git-SVN and no otherwise" |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 16 | exit -1 |
| 17 | fi |
Diana Picus | 600c05a | 2016-06-16 19:28:38 +0300 | [diff] [blame] | 18 | if [[ $LLVM_GITRW = "yes" ]] && [[ $LLVM_GITUSER = '' ]]; then |
Renato Golin | 7c10e6e | 2016-05-12 16:20:10 +0100 | [diff] [blame] | 19 | echo "Please, define \$LLVM_GITUSER to access Linaro's Git server" |
| 20 | exit 1 |
| 21 | fi |
| 22 | if [[ $LLVM_GITRW = 'yes' ]] && [[ $LLVM_SVNUSER = '' ]]; then |
| 23 | echo "Please, define \$LLVM_SVNUSER when using GITRW=yes" |
| 24 | echo "SVNUSER is your upstream LLVM commit user" |
| 25 | exit 1 |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 26 | fi |
| 27 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 28 | # Verify the environment variables that should be set by llvm-env |
| 29 | verify_env() { |
| 30 | if [[ $LLVM_SRC = '' ]]; then |
| 31 | echo "Please, define \$LLVM_SRC to point to the current LLVM" |
| 32 | echo "worktree directory, or run llvm-env to set it for you" |
| 33 | exit -1 |
| 34 | fi |
| 35 | if [[ $LLVM_BLD = '' ]]; then |
| 36 | echo "Please, define \$LLVM_BLD to point to the current LLVM" |
| 37 | echo "build directory, or run llvm-env to set it for you" |
| 38 | exit -1 |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | has() { |
| 43 | if [[ $1 = '' || $2 = '' ]]; then |
| 44 | echo no |
| 45 | return |
| 46 | fi |
| 47 | local item=$1 |
| 48 | shift |
| 49 | for each in $*; do |
| 50 | if [[ $item = $each ]]; then |
| 51 | echo yes |
| 52 | return |
| 53 | fi |
| 54 | done |
| 55 | echo no |
| 56 | } |
| 57 | |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 58 | get_branch() { |
| 59 | branch=`git rev-parse --abbrev-ref HEAD` |
| 60 | if [[ $? != 0 ]]; then |
| 61 | local dir=`pwd` |
| 62 | echo "Source dir '$dir' is not in a git repository" 1>&2 |
| 63 | exit -1 |
| 64 | fi |
| 65 | echo $branch |
| 66 | } |
| 67 | |
| 68 | get_branches() { |
| 69 | branches=`git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)'` |
| 70 | if [[ $? != 0 ]]; then |
| 71 | local dir=`pwd` |
| 72 | echo "Source dir '$dir' is not in a git repository" 1>&2 |
| 73 | exit -1 |
| 74 | fi |
| 75 | echo $branches |
| 76 | } |
| 77 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 78 | has_branch() { |
| 79 | branch=$1 |
| 80 | branches=`get_branches` |
| 81 | result=`has $branch $branches` |
| 82 | echo $result |
| 83 | } |
| 84 | |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 85 | safe_run() { |
Diana Picus | 416bc4f | 2016-04-22 16:47:57 +0300 | [diff] [blame] | 86 | "$@" |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 87 | if [[ $? != 0 ]]; then |
Diana Picus | 416bc4f | 2016-04-22 16:47:57 +0300 | [diff] [blame] | 88 | echo "'$@' failed, bailing out" |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 89 | exit 1 |
| 90 | fi |
| 91 | } |
| 92 | |
| 93 | is_git() { |
| 94 | test -d `git rev-parse --show-toplevel`/.git |
| 95 | } |
| 96 | |
| 97 | is_git_svn() { |
| 98 | test -f `git rev-parse --show-toplevel`/.git/svn/.metadata |
| 99 | } |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 100 | |
| 101 | # Quiet pushd & popd |
| 102 | pushdq() { |
| 103 | pushd "$@" > /dev/null |
| 104 | } |
| 105 | |
| 106 | popdq() { |
| 107 | popd "$@" > /dev/null |
| 108 | } |
| 109 | |
| 110 | add_worktree() { |
| 111 | repo_dir=$1 |
| 112 | worktree_dir=$2 |
| 113 | branch=$3 |
| 114 | |
| 115 | pushdq $repo_dir |
| 116 | |
| 117 | if [ `get_branch` != 'master' ]; then |
| 118 | echo "$repo_dir isn't on master, bailing out" |
| 119 | exit 1 |
| 120 | fi |
| 121 | |
| 122 | if [ `has_branch $branch` = "yes" ]; then |
| 123 | safe_run git worktree add $worktree_dir $branch |
| 124 | else |
| 125 | safe_run git worktree add -b $branch $worktree_dir |
| 126 | fi |
| 127 | popdq |
| 128 | } |
| 129 | |
| 130 | remove_worktree() { |
| 131 | repo_dir=$1 |
| 132 | |
| 133 | if [ "$2" != "" ]; then |
| 134 | worktree_dir=$2 |
| 135 | safe_run rm -rf $worktree_dir |
| 136 | fi |
| 137 | |
| 138 | pushdq $repo_dir |
| 139 | safe_run git worktree prune |
| 140 | popdq |
| 141 | } |