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