Benedikt Böhm | e2fa491 | 2010-01-26 14:41:20 +0100 | [diff] [blame^] | 1 | #!/bin/sh |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 2 | # |
| 3 | # gitflow -- A collection of Git wrapper scripts to provide high-level |
| 4 | # repository operations for Vincent Driessen's branching model: |
| 5 | # |
| 6 | # Original blog post presenting this model is found at: |
| 7 | # http://nvie.com/archives/323 |
| 8 | # |
| 9 | # Feel free to contribute to this project at: |
| 10 | # http://github.com/nvie/gitflow |
| 11 | # |
| 12 | # Copyright (c) 2010 by Vincent Driessen |
| 13 | # Copyright (c) 2010 by Benedikt Böhm |
| 14 | # |
| 15 | |
Benedikt Böhm | e5eaff9 | 2010-01-26 12:44:55 +0100 | [diff] [blame] | 16 | # enable debug mode |
| 17 | if [ "$DEBUG" = "yes" ]; then |
| 18 | set -x |
| 19 | fi |
| 20 | |
Benedikt Böhm | 4a864fb | 2010-01-26 12:59:27 +0100 | [diff] [blame] | 21 | export GITFLOW_DIR=$(dirname "$0") |
| 22 | export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master) |
| 23 | export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop) |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 24 | export ORIGIN=$(git config --get gitflow.origin || echo origin) |
Benedikt Böhm | 4a864fb | 2010-01-26 12:59:27 +0100 | [diff] [blame] | 25 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 26 | warn() { echo "$@" >&2; } |
| 27 | die() { warn "$@"; exit 1; } |
Benedikt Böhm | e2fa491 | 2010-01-26 14:41:20 +0100 | [diff] [blame^] | 28 | |
| 29 | has() { |
| 30 | local item=$1; shift |
| 31 | echo " $@ " | grep -q " $item " |
| 32 | } |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 33 | |
| 34 | usage() { |
Benedikt Böhm | 7d703a8 | 2010-01-26 13:17:12 +0100 | [diff] [blame] | 35 | . "$GITFLOW_DIR/git-flow-version" |
| 36 | echo "gitflow, version $GITFLOW_VERSION" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 37 | echo |
Benedikt Böhm | 7d703a8 | 2010-01-26 13:17:12 +0100 | [diff] [blame] | 38 | echo "usage: git flow <cmd> <type> <args>" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 39 | echo |
Benedikt Böhm | 7d703a8 | 2010-01-26 13:17:12 +0100 | [diff] [blame] | 40 | echo "<type> can be any of: feature, release, hotfix, support" |
| 41 | echo |
| 42 | echo "Try 'git flow help <type>' for details." |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | main() { |
| 46 | if [ $# -lt 2 ]; then |
| 47 | usage |
| 48 | exit 1 |
| 49 | fi |
| 50 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 51 | # sanity checks |
| 52 | ACTION="$1" |
| 53 | BTYPE="$2" |
| 54 | shift 2 |
| 55 | |
| 56 | if [ ! -e "$GITFLOW_DIR/git-flow-$BTYPE" ]; then |
| 57 | usage |
| 58 | exit 1 |
| 59 | fi |
| 60 | |
| 61 | if ! git rev-parse --git-dir &>/dev/null; then |
| 62 | die "Not a git repository" |
| 63 | fi |
| 64 | |
| 65 | # get all available branches |
| 66 | LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //') |
| 67 | REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //') |
| 68 | ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES" |
| 69 | |
| 70 | # run command |
| 71 | . "$GITFLOW_DIR/git-flow-$BTYPE" |
| 72 | |
| 73 | if ! declare -f cmd_$ACTION >/dev/null; then |
| 74 | usage |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 78 | # run command |
| 79 | cmd_$ACTION "$@" |
| 80 | } |
| 81 | |
| 82 | gitflow_check_clean_working_tree() { |
| 83 | if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then |
| 84 | die "Working tree contains unstaged changes. Aborting ..." |
| 85 | fi |
| 86 | if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then |
| 87 | die "Index contains uncommited changes. Aborting ..." |
| 88 | fi |
| 89 | } |
| 90 | |
| 91 | gitflow_require_local_branch() { |
| 92 | if ! has $1 $LOCAL_BRANCHES; then |
| 93 | die "Local branch '$1' does not exist and is required." |
| 94 | fi |
| 95 | } |
| 96 | |
| 97 | gitflow_require_remote_branch() { |
| 98 | if ! has $1 $REMOTE_BRANCHES; then |
| 99 | die "Remote branch '$1' does not exist and is required." |
| 100 | fi |
| 101 | } |
| 102 | |
| 103 | gitflow_require_branch() { |
| 104 | if ! has $1 $ALL_BRANCHES; then |
| 105 | die "Branch '$1' does not exist and is required." |
| 106 | fi |
| 107 | } |
| 108 | |
| 109 | gitflow_require_branch_absent() { |
| 110 | if has $1 $ALL_BRANCHES; then |
| 111 | die "Branch '$1' already exists. Pick another name." |
| 112 | fi |
| 113 | } |
| 114 | |
| 115 | # |
| 116 | # gitflow_test_branches_equal() |
| 117 | # |
| 118 | # Tests whether branches and their "origin" counterparts have diverged and need |
| 119 | # merging first. It returns error codes to provide more detail, like so: |
| 120 | # |
| 121 | # 0 Branch heads point to the same commit |
| 122 | # 1 First given branch needs fast-forwarding |
| 123 | # 2 Second given branch needs fast-forwarding |
| 124 | # 3 Branch needs a real merge |
| 125 | # |
| 126 | gitflow_test_branches_equal() { |
| 127 | commit1=$(git rev-parse "$1") |
| 128 | commit2=$(git rev-parse "$2") |
| 129 | if [ "$commit1" != "$commit2" ]; then |
| 130 | base=$(git merge-base "$commit1" "$commit2") |
| 131 | if [ "$commit1" = "$base" ]; then |
| 132 | return 1 |
| 133 | elif [ "$commit2" = "$base" ]; then |
| 134 | return 2 |
| 135 | else |
| 136 | return 3 |
| 137 | fi |
| 138 | else |
| 139 | return 0 |
| 140 | fi |
| 141 | } |
| 142 | |
| 143 | gitflow_require_branches_equal() { |
| 144 | gitflow_require_local_branch "$1" |
| 145 | gitflow_require_remote_branch "$2" |
| 146 | gitflow_test_branches_equal "$1" "$2" |
| 147 | status=$? |
| 148 | if [ $status -gt 0 ]; then |
| 149 | warn "Branches '$1' and '$2' have diverged." |
| 150 | if [ $status -eq 1 ]; then |
| 151 | die "And branch '$1' may be fast-forwarded." |
| 152 | elif [ $status -eq 2 ]; then |
| 153 | # Warn here, since there is no harm in being ahead |
| 154 | warn "And local branch '$1' is ahead of '$2'." |
| 155 | else |
| 156 | die "Branches need merging first." |
| 157 | fi |
| 158 | fi |
| 159 | } |
| 160 | |
| 161 | main "$@" |