Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 1 | # |
| 2 | # git-flow -- A collection of Git extensions to provide high-level |
| 3 | # repository operations for Vincent Driessen's branching model. |
| 4 | # |
| 5 | # Original blog post presenting this model is found at: |
| 6 | # http://nvie.com/archives/323 |
| 7 | # |
| 8 | # Feel free to contribute to this project at: |
| 9 | # http://github.com/nvie/gitflow |
| 10 | # |
| 11 | # Copyright (c) 2010 by Vincent Driessen |
| 12 | # Copyright (c) 2010 by Benedikt Böhm |
| 13 | # |
| 14 | |
| 15 | # shell output |
| 16 | warn() { echo "$@" >&2; } |
| 17 | die() { warn "$@"; exit 1; } |
| 18 | |
| 19 | # set logic |
| 20 | has() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 21 | local item=$1; shift |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 22 | echo " $@ " | grep -q " $item " |
| 23 | } |
| 24 | |
| 25 | # basic math |
| 26 | min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; } |
| 27 | max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; } |
| 28 | |
| 29 | # basic string matching |
| 30 | startswith() { [ "$1" != "${1#$2}" ]; } |
| 31 | endswith() { [ "$1" != "${1#$2}" ]; } |
| 32 | |
| 33 | # convenience functions for checking shFlags flags |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 34 | flag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; } |
| 35 | noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; } |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 36 | |
| 37 | # |
| 38 | # Git specific common functionality |
| 39 | # |
| 40 | |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 41 | # check if this repo has been inited for gitflow |
| 42 | gitflow_has_master_configured() { |
| 43 | local master=$(git config --get gitflow.branch.master) |
| 44 | [ "$master" != "" ] && gitflow_local_branch_exists "$master" |
| 45 | } |
| 46 | |
| 47 | gitflow_has_develop_configured() { |
| 48 | local develop=$(git config --get gitflow.branch.develop) |
| 49 | [ "$develop" != "" ] && gitflow_local_branch_exists "$develop" |
| 50 | } |
| 51 | |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 52 | gitflow_has_prefixes_configured() { |
| 53 | git config --get gitflow.prefix.feature >/dev/null 2>&1 && \ |
| 54 | git config --get gitflow.prefix.release >/dev/null 2>&1 && \ |
| 55 | git config --get gitflow.prefix.hotfix >/dev/null 2>&1 && \ |
| 56 | git config --get gitflow.prefix.support >/dev/null 2>&1 && \ |
| 57 | git config --get gitflow.prefix.versiontag >/dev/null 2>&1 |
| 58 | } |
| 59 | |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 60 | gitflow_is_initialized() { |
Vincent Driessen | 1d8bb0d | 2010-02-20 16:46:38 +0100 | [diff] [blame] | 61 | gitflow_has_master_configured && \ |
| 62 | gitflow_has_develop_configured && \ |
| 63 | [ "$(git config --get gitflow.branch.master)" != \ |
| 64 | "$(git config --get gitflow.branch.develop)" ] && \ |
| 65 | gitflow_has_prefixes_configured |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 68 | # get all available branches |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 69 | gitflow_local_branches() { git branch | sed 's/^[* ] //'; } |
| 70 | gitflow_remote_branches() { git branch -r | sed 's/^[* ] //'; } |
Vincent Driessen | 4e11dd6 | 2010-02-19 19:48:23 +0100 | [diff] [blame] | 71 | gitflow_all_branches() { ( git branch; git branch -r) | sed 's/^[* ] //'; } |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 72 | gitflow_all_tags() { git tag; } |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 73 | |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 74 | # loading settings that can be overridden using git config |
| 75 | gitflow_load_settings() { |
Vincent Driessen | cf6e92a | 2010-02-19 19:44:48 +0100 | [diff] [blame] | 76 | export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1) |
Vincent Driessen | c1598bf | 2010-02-20 16:52:48 +0100 | [diff] [blame] | 77 | export MASTER_BRANCH=$(git config --get gitflow.branch.master) |
| 78 | export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop) |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 79 | export ORIGIN=$(git config --get gitflow.origin || echo origin) |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 82 | # |
| 83 | # resolve_nameprefix |
| 84 | # |
| 85 | # Inputs: |
| 86 | # $1 = name prefix to resolve |
| 87 | # $2 = branch prefix to use |
| 88 | # |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 89 | # Searches branch names from gitflow_local_branches() to look for a unique |
| 90 | # branch name whose name starts with the given name prefix. |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 91 | # |
| 92 | # There are multiple exit codes possible: |
| 93 | # 0: The unambiguous full name of the branch is written to stdout |
| 94 | # (success) |
| 95 | # 1: No match is found. |
| 96 | # 2: Multiple matches found. These matches are written to stderr |
| 97 | # |
| 98 | resolve_nameprefix() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 99 | local name=$1 |
| 100 | local prefix=$2 |
| 101 | local matches |
| 102 | local num_matches |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 103 | |
| 104 | # first, check if there is a perfect match |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 105 | if has "$(gitflow_local_branches)" "$prefix$name"; then |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 106 | echo "$name" |
| 107 | return 0 |
| 108 | fi |
| 109 | |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 110 | matches=$(echo "$(gitflow_local_branches)" | grep "^$prefix$name") |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 111 | num_matches=$(echo "$matches" | wc -l) |
| 112 | if [ -z "$matches" ]; then |
| 113 | # no prefix match, so take it literally |
| 114 | warn "No branch matches prefix '$name'" |
| 115 | return 1 |
| 116 | else |
| 117 | if [ $num_matches -eq 1 ]; then |
| 118 | echo "${matches#$prefix}" |
| 119 | return 0 |
| 120 | else |
| 121 | # multiple matches, cannot decide |
| 122 | warn "Multiple branches match prefix '$name':" |
| 123 | for match in $matches; do |
| 124 | warn "- $match" |
| 125 | done |
| 126 | return 2 |
| 127 | fi |
| 128 | fi |
| 129 | } |
| 130 | |
| 131 | gitflow_current_branch() { |
| 132 | git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g' |
| 133 | } |
| 134 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 135 | gitflow_test_clean_working_tree() { |
| 136 | if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then |
| 137 | return 1 |
| 138 | elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then |
| 139 | return 2 |
| 140 | else |
| 141 | return 0 |
| 142 | fi |
| 143 | } |
| 144 | |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 145 | gitflow_require_git_repo() { |
| 146 | if ! git rev-parse --git-dir >/dev/null 2>&1; then |
Vincent Driessen | c1598bf | 2010-02-20 16:52:48 +0100 | [diff] [blame] | 147 | die "fatal: Not a git repository" |
| 148 | fi |
| 149 | } |
| 150 | |
| 151 | gitflow_require_initialized() { |
| 152 | if ! gitflow_is_initialized; then |
| 153 | die "fatal: Not a gitflow-enabled repo yet. Please run \"git flow init\" first." |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 154 | fi |
| 155 | } |
| 156 | |
Vincent Driessen | ef43cbd | 2010-02-21 11:20:05 +0100 | [diff] [blame^] | 157 | git_repo_is_headless() { |
| 158 | ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1 |
| 159 | } |
| 160 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 161 | gitflow_require_clean_working_tree() { |
| 162 | gitflow_test_clean_working_tree |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 163 | local result=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 164 | if [ $result -eq 1 ]; then |
| 165 | die "fatal: Working tree contains unstaged changes. Aborting." |
| 166 | fi |
| 167 | if [ $result -eq 2 ]; then |
| 168 | die "fatal: Index contains uncommited changes. Aborting." |
| 169 | fi |
| 170 | } |
| 171 | |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 172 | gitflow_local_branch_exists() { |
| 173 | has $1 $(gitflow_local_branches) |
| 174 | } |
| 175 | |
Vincent Driessen | 49094bd | 2010-02-18 12:05:01 +0100 | [diff] [blame] | 176 | gitflow_branch_exists() { |
| 177 | has $1 $(gitflow_all_branches) |
| 178 | } |
| 179 | |
| 180 | gitflow_tag_exists() { |
| 181 | has $1 $(gitflow_all_tags) |
| 182 | } |
| 183 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 184 | gitflow_require_local_branch() { |
Vincent Driessen | b25ab83 | 2010-02-20 11:21:23 +0100 | [diff] [blame] | 185 | if ! gitflow_local_branch_exists; then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 186 | die "fatal: Local branch '$1' does not exist and is required." |
| 187 | fi |
| 188 | } |
| 189 | |
| 190 | gitflow_require_remote_branch() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 191 | if ! has $1 $(gitflow_remote_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 192 | die "Remote branch '$1' does not exist and is required." |
| 193 | fi |
| 194 | } |
| 195 | |
| 196 | gitflow_require_branch() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 197 | if ! has $1 $(gitflow_all_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 198 | die "Branch '$1' does not exist and is required." |
| 199 | fi |
| 200 | } |
| 201 | |
| 202 | gitflow_require_branch_absent() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 203 | if has $1 $(gitflow_all_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 204 | die "Branch '$1' already exists. Pick another name." |
| 205 | fi |
| 206 | } |
| 207 | |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 208 | gitflow_require_tag_absent() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 209 | if has $1 $(gitflow_all_tags); then |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 210 | die "Tag '$1' already exists. Pick another name." |
| 211 | fi |
| 212 | } |
| 213 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 214 | # |
| 215 | # gitflow_test_branches_equal() |
| 216 | # |
| 217 | # Tests whether branches and their "origin" counterparts have diverged and need |
| 218 | # merging first. It returns error codes to provide more detail, like so: |
| 219 | # |
| 220 | # 0 Branch heads point to the same commit |
| 221 | # 1 First given branch needs fast-forwarding |
| 222 | # 2 Second given branch needs fast-forwarding |
| 223 | # 3 Branch needs a real merge |
Vincent Driessen | 49094bd | 2010-02-18 12:05:01 +0100 | [diff] [blame] | 224 | # 4 There is no merge base, i.e. the branches have no common ancestors |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 225 | # |
| 226 | gitflow_test_branches_equal() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 227 | local commit1=$(git rev-parse "$1") |
| 228 | local commit2=$(git rev-parse "$2") |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 229 | if [ "$commit1" != "$commit2" ]; then |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 230 | local base=$(git merge-base "$commit1" "$commit2") |
Vincent Driessen | 49094bd | 2010-02-18 12:05:01 +0100 | [diff] [blame] | 231 | if [ $? -ne 0 ]; then |
| 232 | return 4 |
| 233 | elif [ "$commit1" = "$base" ]; then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 234 | return 1 |
| 235 | elif [ "$commit2" = "$base" ]; then |
| 236 | return 2 |
| 237 | else |
| 238 | return 3 |
| 239 | fi |
| 240 | else |
| 241 | return 0 |
| 242 | fi |
| 243 | } |
| 244 | |
| 245 | gitflow_require_branches_equal() { |
| 246 | gitflow_require_local_branch "$1" |
| 247 | gitflow_require_remote_branch "$2" |
| 248 | gitflow_test_branches_equal "$1" "$2" |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 249 | local status=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 250 | if [ $status -gt 0 ]; then |
| 251 | warn "Branches '$1' and '$2' have diverged." |
| 252 | if [ $status -eq 1 ]; then |
| 253 | die "And branch '$1' may be fast-forwarded." |
| 254 | elif [ $status -eq 2 ]; then |
| 255 | # Warn here, since there is no harm in being ahead |
| 256 | warn "And local branch '$1' is ahead of '$2'." |
| 257 | else |
| 258 | die "Branches need merging first." |
| 259 | fi |
| 260 | fi |
| 261 | } |
| 262 | |
| 263 | # |
| 264 | # gitflow_is_branch_merged_into() |
| 265 | # |
| 266 | # Checks whether branch $1 is succesfully merged into $2 |
| 267 | # |
| 268 | gitflow_is_branch_merged_into() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 269 | local subject=$1 |
| 270 | local base=$2 |
| 271 | local all_merges=$(git branch --contains $subject | sed 's/^[* ] //') |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame] | 272 | has $base $all_merges |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 273 | } |