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 | |
Vincent Driessen | 58995b5 | 2010-01-28 12:39:06 +0100 | [diff] [blame] | 21 | export GIT_DIR=$(git rev-parse --git-dir) |
Benedikt Böhm | 4a864fb | 2010-01-26 12:59:27 +0100 | [diff] [blame] | 22 | export GITFLOW_DIR=$(dirname "$0") |
| 23 | export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master) |
| 24 | export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop) |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 25 | export ORIGIN=$(git config --get gitflow.origin || echo origin) |
Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 26 | export README=$(git config --get gitflow.readme || echo README) |
Benedikt Böhm | 4a864fb | 2010-01-26 12:59:27 +0100 | [diff] [blame] | 27 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 28 | warn() { echo "$@" >&2; } |
| 29 | die() { warn "$@"; exit 1; } |
Benedikt Böhm | e2fa491 | 2010-01-26 14:41:20 +0100 | [diff] [blame] | 30 | |
| 31 | has() { |
| 32 | local item=$1; shift |
| 33 | echo " $@ " | grep -q " $item " |
| 34 | } |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 35 | |
| 36 | usage() { |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 37 | echo "usage: git flow <subcommand>" |
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 "Available subcommands are:" |
| 40 | echo " init Initialize a new git repo with support for the branching model." |
| 41 | echo " feature Manage your feature branches." |
| 42 | echo " release Manage your release branches." |
| 43 | echo " hotfix Manage your hotfix branches." |
| 44 | echo " support Manage your support branches." |
Vincent Driessen | 3625f39 | 2010-01-28 00:13:26 +0100 | [diff] [blame] | 45 | echo " version Shows version information." |
Benedikt Böhm | 7d703a8 | 2010-01-26 13:17:12 +0100 | [diff] [blame] | 46 | echo |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 47 | echo "Try 'git flow <subcommand> help' for details." |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | main() { |
Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 51 | if [ $# -lt 1 ]; then |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 52 | usage |
| 53 | exit 1 |
| 54 | fi |
| 55 | |
Vincent Driessen | ea58d0f | 2010-01-30 20:51:03 +0100 | [diff] [blame] | 56 | # use the shFlags project to parse the command line arguments |
Vincent Driessen | c3b7db9 | 2010-02-02 22:50:13 +0100 | [diff] [blame] | 57 | . "$GITFLOW_DIR/shFlags.sh" |
Vincent Driessen | 4417492 | 2010-02-02 23:53:21 +0100 | [diff] [blame] | 58 | FLAGS_PARENT="git flow" |
Vincent Driessen | ea58d0f | 2010-01-30 20:51:03 +0100 | [diff] [blame] | 59 | #DEFINE_boolean quiet 0 'run without output' q |
| 60 | #DEFINE_boolean verbose 0 'run verbose (more output)' v |
| 61 | FLAGS "$@" || exit $? |
| 62 | eval set -- "${FLAGS_ARGV}" |
| 63 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 64 | # sanity checks |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 65 | SUBCOMMAND="$1"; shift |
Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 66 | |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 67 | if [ ! -e "$GITFLOW_DIR/git-flow-$SUBCOMMAND" ]; then |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 68 | usage |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
Vincent Driessen | 2ba9a4d | 2010-01-27 12:51:07 +0100 | [diff] [blame] | 72 | if ! git rev-parse --git-dir >/dev/null; then |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 73 | die "Not a git repository" |
| 74 | fi |
| 75 | |
| 76 | # get all available branches |
| 77 | LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //') |
| 78 | REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //') |
| 79 | ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES" |
| 80 | |
| 81 | # run command |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 82 | . "$GITFLOW_DIR/git-flow-$SUBCOMMAND" |
Vincent Driessen | 4417492 | 2010-02-02 23:53:21 +0100 | [diff] [blame] | 83 | FLAGS_PARENT="git flow $SUBCOMMAND" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 84 | |
Vincent Driessen | 1b81923 | 2010-02-01 15:51:43 +0100 | [diff] [blame] | 85 | # test if the first argument is a flag (i.e. starts with '-') |
| 86 | # in that case, we interpret this arg as a flag for the default |
| 87 | # command |
| 88 | SUBACTION="default" |
| 89 | if [ "$1" != "" ] && ! echo "$1" | grep -q "^-"; then |
| 90 | SUBACTION="$1"; shift |
| 91 | fi |
Vincent Driessen | e168487 | 2010-02-02 06:33:53 +0100 | [diff] [blame] | 92 | if ! typeset -f "cmd_$SUBACTION" 2>&1 >/dev/null; then |
Vincent Driessen | 9277024 | 2010-02-04 11:49:47 +0100 | [diff] [blame] | 93 | warn "Unknown subcommand: '$SUBACTION'" |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 94 | usage |
| 95 | exit 1 |
Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 96 | fi |
| 97 | |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 98 | # run the specified action |
| 99 | cmd_$SUBACTION "$@" |
Benedikt Böhm | 427c5db | 2010-01-26 18:23:44 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Vincent Driessen | 62345d5 | 2010-01-29 10:27:01 +0100 | [diff] [blame] | 102 | gitflow_test_clean_working_tree() { |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 103 | if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then |
Vincent Driessen | 62345d5 | 2010-01-29 10:27:01 +0100 | [diff] [blame] | 104 | return 1 |
| 105 | elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then |
| 106 | return 2 |
| 107 | else |
| 108 | return 0 |
| 109 | fi |
| 110 | } |
| 111 | |
| 112 | gitflow_require_clean_working_tree() { |
| 113 | gitflow_test_clean_working_tree |
| 114 | result=$? |
| 115 | if [ $result -eq 1 ]; then |
Vincent Driessen | 4c92a9d | 2010-02-02 09:42:51 +0100 | [diff] [blame] | 116 | die "fatal: Working tree contains unstaged changes. Aborting." |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 117 | fi |
Vincent Driessen | 62345d5 | 2010-01-29 10:27:01 +0100 | [diff] [blame] | 118 | if [ $result -eq 2 ]; then |
Vincent Driessen | 4c92a9d | 2010-02-02 09:42:51 +0100 | [diff] [blame] | 119 | die "fatal: Index contains uncommited changes. Aborting." |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 120 | fi |
| 121 | } |
| 122 | |
| 123 | gitflow_require_local_branch() { |
| 124 | if ! has $1 $LOCAL_BRANCHES; then |
Vincent Driessen | 4c92a9d | 2010-02-02 09:42:51 +0100 | [diff] [blame] | 125 | die "fatal: Local branch '$1' does not exist and is required." |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 126 | fi |
| 127 | } |
| 128 | |
| 129 | gitflow_require_remote_branch() { |
| 130 | if ! has $1 $REMOTE_BRANCHES; then |
| 131 | die "Remote branch '$1' does not exist and is required." |
| 132 | fi |
| 133 | } |
| 134 | |
| 135 | gitflow_require_branch() { |
| 136 | if ! has $1 $ALL_BRANCHES; then |
| 137 | die "Branch '$1' does not exist and is required." |
| 138 | fi |
| 139 | } |
| 140 | |
| 141 | gitflow_require_branch_absent() { |
| 142 | if has $1 $ALL_BRANCHES; then |
| 143 | die "Branch '$1' already exists. Pick another name." |
| 144 | fi |
| 145 | } |
| 146 | |
| 147 | # |
| 148 | # gitflow_test_branches_equal() |
| 149 | # |
| 150 | # Tests whether branches and their "origin" counterparts have diverged and need |
| 151 | # merging first. It returns error codes to provide more detail, like so: |
| 152 | # |
| 153 | # 0 Branch heads point to the same commit |
| 154 | # 1 First given branch needs fast-forwarding |
| 155 | # 2 Second given branch needs fast-forwarding |
| 156 | # 3 Branch needs a real merge |
| 157 | # |
| 158 | gitflow_test_branches_equal() { |
| 159 | commit1=$(git rev-parse "$1") |
| 160 | commit2=$(git rev-parse "$2") |
| 161 | if [ "$commit1" != "$commit2" ]; then |
| 162 | base=$(git merge-base "$commit1" "$commit2") |
| 163 | if [ "$commit1" = "$base" ]; then |
| 164 | return 1 |
| 165 | elif [ "$commit2" = "$base" ]; then |
| 166 | return 2 |
| 167 | else |
| 168 | return 3 |
| 169 | fi |
| 170 | else |
| 171 | return 0 |
| 172 | fi |
| 173 | } |
| 174 | |
| 175 | gitflow_require_branches_equal() { |
| 176 | gitflow_require_local_branch "$1" |
| 177 | gitflow_require_remote_branch "$2" |
| 178 | gitflow_test_branches_equal "$1" "$2" |
| 179 | status=$? |
| 180 | if [ $status -gt 0 ]; then |
| 181 | warn "Branches '$1' and '$2' have diverged." |
| 182 | if [ $status -eq 1 ]; then |
| 183 | die "And branch '$1' may be fast-forwarded." |
| 184 | elif [ $status -eq 2 ]; then |
| 185 | # Warn here, since there is no harm in being ahead |
| 186 | warn "And local branch '$1' is ahead of '$2'." |
| 187 | else |
| 188 | die "Branches need merging first." |
| 189 | fi |
| 190 | fi |
| 191 | } |
| 192 | |
Vincent Driessen | 58995b5 | 2010-01-28 12:39:06 +0100 | [diff] [blame] | 193 | # |
| 194 | # gitflow_is_branch_merged_into() |
| 195 | # |
| 196 | # Checks whether branch $1 is succesfully merged into $2 |
| 197 | # |
| 198 | gitflow_is_branch_merged_into() { |
| 199 | local SUBJECT=$1 |
| 200 | local BASE=$2 |
| 201 | ALL_MERGES=$(git branch --contains $SUBJECT | sed 's/^[* ] //') |
| 202 | has $BASE $ALL_MERGES |
| 203 | } |
| 204 | |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 205 | # helper functions for common reuse |
| 206 | max() { if [ "$1" -gt "$2" ]; then echo "$1"; else echo "$2"; fi; } |
| 207 | |
Vincent Driessen | 4417492 | 2010-02-02 23:53:21 +0100 | [diff] [blame] | 208 | # convenience functions for checking whether flags have been set or not |
| 209 | flag() { eval FLAG=\$FLAGS_$1; [ $FLAG -eq $FLAGS_TRUE ]; } |
| 210 | noflag() { eval FLAG=\$FLAGS_$1; [ $FLAG -ne $FLAGS_TRUE ]; } |
| 211 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 212 | main "$@" |