| 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 | # | 
| Vincent Driessen | 6c2d30b | 2010-01-26 22:18:36 +0100 | [diff] [blame] | 3 | # git-flow -- A collection of Git extensions to provide high-level | 
|  | 4 | # repository operations for Vincent Driessen's branching model. | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 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 | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 25 | export README=$(git config --get gitflow.readme || echo README) | 
| Benedikt Böhm | 4a864fb | 2010-01-26 12:59:27 +0100 | [diff] [blame] | 26 |  | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 27 | warn() { echo "$@" >&2; } | 
|  | 28 | die() { warn "$@"; exit 1; } | 
| Benedikt Böhm | e2fa491 | 2010-01-26 14:41:20 +0100 | [diff] [blame] | 29 |  | 
|  | 30 | has() { | 
|  | 31 | local item=$1; shift | 
|  | 32 | echo " $@ " | grep -q " $item " | 
|  | 33 | } | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 34 |  | 
|  | 35 | usage() { | 
| Benedikt Böhm | 7d703a8 | 2010-01-26 13:17:12 +0100 | [diff] [blame] | 36 | . "$GITFLOW_DIR/git-flow-version" | 
| Vincent Driessen | 6c2d30b | 2010-01-26 22:18:36 +0100 | [diff] [blame] | 37 | echo "git-flow, version $GITFLOW_VERSION" | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 38 | echo | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 39 | echo "usage: git flow <subcommand>" | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 40 | echo | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 41 | echo "Available subcommands are:" | 
|  | 42 | echo "   init      Initialize a new git repo with support for the branching model." | 
|  | 43 | echo "   feature   Manage your feature branches." | 
|  | 44 | echo "   release   Manage your release branches." | 
|  | 45 | echo "   hotfix    Manage your hotfix branches." | 
|  | 46 | echo "   support   Manage your support branches." | 
| Benedikt Böhm | 7d703a8 | 2010-01-26 13:17:12 +0100 | [diff] [blame] | 47 | echo | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 48 | echo "Try 'git flow <subcommand> help' for details." | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | main() { | 
| Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 52 | if [ $# -lt 1 ]; then | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 53 | usage | 
|  | 54 | exit 1 | 
|  | 55 | fi | 
|  | 56 |  | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 57 | # sanity checks | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 58 | SUBCOMMAND="$1"; shift | 
| Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 59 |  | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 60 | if [ ! -e "$GITFLOW_DIR/git-flow-$SUBCOMMAND" ]; then | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 61 | usage | 
|  | 62 | exit 1 | 
|  | 63 | fi | 
|  | 64 |  | 
| Vincent Driessen | 2ba9a4d | 2010-01-27 12:51:07 +0100 | [diff] [blame] | 65 | if ! git rev-parse --git-dir >/dev/null; then | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 66 | die "Not a git repository" | 
|  | 67 | fi | 
|  | 68 |  | 
|  | 69 | # get all available branches | 
|  | 70 | LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //') | 
|  | 71 | REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //') | 
|  | 72 | ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES" | 
|  | 73 |  | 
|  | 74 | # run command | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 75 | . "$GITFLOW_DIR/git-flow-$SUBCOMMAND" | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 76 |  | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 77 | if ! typeset -f sub_main >/dev/null; then | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 78 | usage | 
|  | 79 | exit 1 | 
|  | 80 | fi | 
|  | 81 |  | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 82 | # run command | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 83 | sub_main "$@" | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 86 | sub_main() { | 
|  | 87 | SUBACTION="${1:-default}"; shift | 
|  | 88 | if ! typeset -f cmd_$SUBACTION 2>&1 >/dev/null; then | 
|  | 89 | warn "Unknown subcommand: '$1'" | 
|  | 90 | usage | 
|  | 91 | exit 1 | 
| Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 92 | fi | 
|  | 93 |  | 
| Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame^] | 94 | # run the specified action | 
|  | 95 | cmd_$SUBACTION "$@" | 
| Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 98 | gitflow_check_clean_working_tree() { | 
|  | 99 | if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then | 
|  | 100 | die "Working tree contains unstaged changes. Aborting ..." | 
|  | 101 | fi | 
|  | 102 | if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then | 
|  | 103 | die "Index contains uncommited changes. Aborting ..." | 
|  | 104 | fi | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | gitflow_require_local_branch() { | 
|  | 108 | if ! has $1 $LOCAL_BRANCHES; then | 
|  | 109 | die "Local branch '$1' does not exist and is required." | 
|  | 110 | fi | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | gitflow_require_remote_branch() { | 
|  | 114 | if ! has $1 $REMOTE_BRANCHES; then | 
|  | 115 | die "Remote branch '$1' does not exist and is required." | 
|  | 116 | fi | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | gitflow_require_branch() { | 
|  | 120 | if ! has $1 $ALL_BRANCHES; then | 
|  | 121 | die "Branch '$1' does not exist and is required." | 
|  | 122 | fi | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | gitflow_require_branch_absent() { | 
|  | 126 | if has $1 $ALL_BRANCHES; then | 
|  | 127 | die "Branch '$1' already exists. Pick another name." | 
|  | 128 | fi | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | # | 
|  | 132 | # gitflow_test_branches_equal() | 
|  | 133 | # | 
|  | 134 | # Tests whether branches and their "origin" counterparts have diverged and need | 
|  | 135 | # merging first. It returns error codes to provide more detail, like so: | 
|  | 136 | # | 
|  | 137 | # 0    Branch heads point to the same commit | 
|  | 138 | # 1    First given branch needs fast-forwarding | 
|  | 139 | # 2    Second given branch needs fast-forwarding | 
|  | 140 | # 3    Branch needs a real merge | 
|  | 141 | # | 
|  | 142 | gitflow_test_branches_equal() { | 
|  | 143 | commit1=$(git rev-parse "$1") | 
|  | 144 | commit2=$(git rev-parse "$2") | 
|  | 145 | if [ "$commit1" != "$commit2" ]; then | 
|  | 146 | base=$(git merge-base "$commit1" "$commit2") | 
|  | 147 | if [ "$commit1" = "$base" ]; then | 
|  | 148 | return 1 | 
|  | 149 | elif [ "$commit2" = "$base" ]; then | 
|  | 150 | return 2 | 
|  | 151 | else | 
|  | 152 | return 3 | 
|  | 153 | fi | 
|  | 154 | else | 
|  | 155 | return 0 | 
|  | 156 | fi | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | gitflow_require_branches_equal() { | 
|  | 160 | gitflow_require_local_branch "$1" | 
|  | 161 | gitflow_require_remote_branch "$2" | 
|  | 162 | gitflow_test_branches_equal "$1" "$2" | 
|  | 163 | status=$? | 
|  | 164 | if [ $status -gt 0 ]; then | 
|  | 165 | warn "Branches '$1' and '$2' have diverged." | 
|  | 166 | if [ $status -eq 1 ]; then | 
|  | 167 | die "And branch '$1' may be fast-forwarded." | 
|  | 168 | elif [ $status -eq 2 ]; then | 
|  | 169 | # Warn here, since there is no harm in being ahead | 
|  | 170 | warn "And local branch '$1' is ahead of '$2'." | 
|  | 171 | else | 
|  | 172 | die "Branches need merging first." | 
|  | 173 | fi | 
|  | 174 | fi | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | main "$@" |