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 | |
| 41 | # get all available branches |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 42 | gitflow_local_branches() { git branch | sed 's/^[* ] //'; } |
| 43 | gitflow_remote_branches() { git branch -r | sed 's/^[* ] //'; } |
Vincent Driessen | 49094bd | 2010-02-18 12:05:01 +0100 | [diff] [blame] | 44 | gitflow_all_branches() { git branch -a | sed 's/^[* ] //'; } |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 45 | gitflow_all_tags() { git tag; } |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 46 | |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 47 | # loading settings that can be overridden using git config |
| 48 | gitflow_load_settings() { |
Vincent Driessen | cf6e92a | 2010-02-19 19:44:48 +0100 | [diff] [blame^] | 49 | export DOT_GIT_DIR=$(git rev-parse --git-dir >/dev/null 2>&1) |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 50 | export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master) |
| 51 | export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop) |
| 52 | export ORIGIN=$(git config --get gitflow.origin || echo origin) |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 55 | # |
| 56 | # resolve_nameprefix |
| 57 | # |
| 58 | # Inputs: |
| 59 | # $1 = name prefix to resolve |
| 60 | # $2 = branch prefix to use |
| 61 | # |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 62 | # Searches branch names from gitflow_local_branches() to look for a unique |
| 63 | # branch name whose name starts with the given name prefix. |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 64 | # |
| 65 | # There are multiple exit codes possible: |
| 66 | # 0: The unambiguous full name of the branch is written to stdout |
| 67 | # (success) |
| 68 | # 1: No match is found. |
| 69 | # 2: Multiple matches found. These matches are written to stderr |
| 70 | # |
| 71 | resolve_nameprefix() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 72 | local name=$1 |
| 73 | local prefix=$2 |
| 74 | local matches |
| 75 | local num_matches |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 76 | |
| 77 | # first, check if there is a perfect match |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 78 | if has "$(gitflow_local_branches)" "$prefix$name"; then |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 79 | echo "$name" |
| 80 | return 0 |
| 81 | fi |
| 82 | |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 83 | matches=$(echo "$(gitflow_local_branches)" | grep "^$prefix$name") |
Vincent Driessen | d099126 | 2010-02-06 21:19:07 +0100 | [diff] [blame] | 84 | num_matches=$(echo "$matches" | wc -l) |
| 85 | if [ -z "$matches" ]; then |
| 86 | # no prefix match, so take it literally |
| 87 | warn "No branch matches prefix '$name'" |
| 88 | return 1 |
| 89 | else |
| 90 | if [ $num_matches -eq 1 ]; then |
| 91 | echo "${matches#$prefix}" |
| 92 | return 0 |
| 93 | else |
| 94 | # multiple matches, cannot decide |
| 95 | warn "Multiple branches match prefix '$name':" |
| 96 | for match in $matches; do |
| 97 | warn "- $match" |
| 98 | done |
| 99 | return 2 |
| 100 | fi |
| 101 | fi |
| 102 | } |
| 103 | |
| 104 | gitflow_current_branch() { |
| 105 | git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g' |
| 106 | } |
| 107 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 108 | gitflow_test_clean_working_tree() { |
| 109 | if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then |
| 110 | return 1 |
| 111 | elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then |
| 112 | return 2 |
| 113 | else |
| 114 | return 0 |
| 115 | fi |
| 116 | } |
| 117 | |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 118 | gitflow_require_git_repo() { |
| 119 | if ! git rev-parse --git-dir >/dev/null 2>&1; then |
| 120 | die "Not a git repository" |
| 121 | fi |
| 122 | } |
| 123 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 124 | gitflow_require_clean_working_tree() { |
| 125 | gitflow_test_clean_working_tree |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 126 | local result=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 127 | if [ $result -eq 1 ]; then |
| 128 | die "fatal: Working tree contains unstaged changes. Aborting." |
| 129 | fi |
| 130 | if [ $result -eq 2 ]; then |
| 131 | die "fatal: Index contains uncommited changes. Aborting." |
| 132 | fi |
| 133 | } |
| 134 | |
Vincent Driessen | 49094bd | 2010-02-18 12:05:01 +0100 | [diff] [blame] | 135 | gitflow_branch_exists() { |
| 136 | has $1 $(gitflow_all_branches) |
| 137 | } |
| 138 | |
| 139 | gitflow_tag_exists() { |
| 140 | has $1 $(gitflow_all_tags) |
| 141 | } |
| 142 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 143 | gitflow_require_local_branch() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 144 | if ! has $1 $(gitflow_local_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 145 | die "fatal: Local branch '$1' does not exist and is required." |
| 146 | fi |
| 147 | } |
| 148 | |
| 149 | gitflow_require_remote_branch() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 150 | if ! has $1 $(gitflow_remote_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 151 | die "Remote branch '$1' does not exist and is required." |
| 152 | fi |
| 153 | } |
| 154 | |
| 155 | gitflow_require_branch() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 156 | if ! has $1 $(gitflow_all_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 157 | die "Branch '$1' does not exist and is required." |
| 158 | fi |
| 159 | } |
| 160 | |
| 161 | gitflow_require_branch_absent() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 162 | if has $1 $(gitflow_all_branches); then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 163 | die "Branch '$1' already exists. Pick another name." |
| 164 | fi |
| 165 | } |
| 166 | |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 167 | gitflow_require_tag_absent() { |
Vincent Driessen | 21c7aa9 | 2010-02-16 20:57:35 +0100 | [diff] [blame] | 168 | if has $1 $(gitflow_all_tags); then |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 169 | die "Tag '$1' already exists. Pick another name." |
| 170 | fi |
| 171 | } |
| 172 | |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 173 | # |
| 174 | # gitflow_test_branches_equal() |
| 175 | # |
| 176 | # Tests whether branches and their "origin" counterparts have diverged and need |
| 177 | # merging first. It returns error codes to provide more detail, like so: |
| 178 | # |
| 179 | # 0 Branch heads point to the same commit |
| 180 | # 1 First given branch needs fast-forwarding |
| 181 | # 2 Second given branch needs fast-forwarding |
| 182 | # 3 Branch needs a real merge |
Vincent Driessen | 49094bd | 2010-02-18 12:05:01 +0100 | [diff] [blame] | 183 | # 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] | 184 | # |
| 185 | gitflow_test_branches_equal() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 186 | local commit1=$(git rev-parse "$1") |
| 187 | local commit2=$(git rev-parse "$2") |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 188 | if [ "$commit1" != "$commit2" ]; then |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 189 | local base=$(git merge-base "$commit1" "$commit2") |
Vincent Driessen | 49094bd | 2010-02-18 12:05:01 +0100 | [diff] [blame] | 190 | if [ $? -ne 0 ]; then |
| 191 | return 4 |
| 192 | elif [ "$commit1" = "$base" ]; then |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 193 | return 1 |
| 194 | elif [ "$commit2" = "$base" ]; then |
| 195 | return 2 |
| 196 | else |
| 197 | return 3 |
| 198 | fi |
| 199 | else |
| 200 | return 0 |
| 201 | fi |
| 202 | } |
| 203 | |
| 204 | gitflow_require_branches_equal() { |
| 205 | gitflow_require_local_branch "$1" |
| 206 | gitflow_require_remote_branch "$2" |
| 207 | gitflow_test_branches_equal "$1" "$2" |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 208 | local status=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 209 | if [ $status -gt 0 ]; then |
| 210 | warn "Branches '$1' and '$2' have diverged." |
| 211 | if [ $status -eq 1 ]; then |
| 212 | die "And branch '$1' may be fast-forwarded." |
| 213 | elif [ $status -eq 2 ]; then |
| 214 | # Warn here, since there is no harm in being ahead |
| 215 | warn "And local branch '$1' is ahead of '$2'." |
| 216 | else |
| 217 | die "Branches need merging first." |
| 218 | fi |
| 219 | fi |
| 220 | } |
| 221 | |
| 222 | # |
| 223 | # gitflow_is_branch_merged_into() |
| 224 | # |
| 225 | # Checks whether branch $1 is succesfully merged into $2 |
| 226 | # |
| 227 | gitflow_is_branch_merged_into() { |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 228 | local subject=$1 |
| 229 | local base=$2 |
| 230 | local all_merges=$(git branch --contains $subject | sed 's/^[* ] //') |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame] | 231 | has $base $all_merges |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 232 | } |