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 | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame^] | 21 | typeset 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 | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame^] | 34 | flag() { typeset FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; } |
| 35 | noflag() { typeset 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 |
| 42 | LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //') |
| 43 | REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //') |
| 44 | ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES" |
| 45 | |
| 46 | gitflow_test_clean_working_tree() { |
| 47 | if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then |
| 48 | return 1 |
| 49 | elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then |
| 50 | return 2 |
| 51 | else |
| 52 | return 0 |
| 53 | fi |
| 54 | } |
| 55 | |
| 56 | gitflow_require_clean_working_tree() { |
| 57 | gitflow_test_clean_working_tree |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame^] | 58 | typeset -i result=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 59 | if [ $result -eq 1 ]; then |
| 60 | die "fatal: Working tree contains unstaged changes. Aborting." |
| 61 | fi |
| 62 | if [ $result -eq 2 ]; then |
| 63 | die "fatal: Index contains uncommited changes. Aborting." |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | gitflow_require_local_branch() { |
| 68 | if ! has $1 $LOCAL_BRANCHES; then |
| 69 | die "fatal: Local branch '$1' does not exist and is required." |
| 70 | fi |
| 71 | } |
| 72 | |
| 73 | gitflow_require_remote_branch() { |
| 74 | if ! has $1 $REMOTE_BRANCHES; then |
| 75 | die "Remote branch '$1' does not exist and is required." |
| 76 | fi |
| 77 | } |
| 78 | |
| 79 | gitflow_require_branch() { |
| 80 | if ! has $1 $ALL_BRANCHES; then |
| 81 | die "Branch '$1' does not exist and is required." |
| 82 | fi |
| 83 | } |
| 84 | |
| 85 | gitflow_require_branch_absent() { |
| 86 | if has $1 $ALL_BRANCHES; then |
| 87 | die "Branch '$1' already exists. Pick another name." |
| 88 | fi |
| 89 | } |
| 90 | |
| 91 | # |
| 92 | # gitflow_test_branches_equal() |
| 93 | # |
| 94 | # Tests whether branches and their "origin" counterparts have diverged and need |
| 95 | # merging first. It returns error codes to provide more detail, like so: |
| 96 | # |
| 97 | # 0 Branch heads point to the same commit |
| 98 | # 1 First given branch needs fast-forwarding |
| 99 | # 2 Second given branch needs fast-forwarding |
| 100 | # 3 Branch needs a real merge |
| 101 | # |
| 102 | gitflow_test_branches_equal() { |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame^] | 103 | typeset commit1=$(git rev-parse "$1") |
| 104 | typeset commit2=$(git rev-parse "$2") |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 105 | if [ "$commit1" != "$commit2" ]; then |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame^] | 106 | typeset base=$(git merge-base "$commit1" "$commit2") |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 107 | if [ "$commit1" = "$base" ]; then |
| 108 | return 1 |
| 109 | elif [ "$commit2" = "$base" ]; then |
| 110 | return 2 |
| 111 | else |
| 112 | return 3 |
| 113 | fi |
| 114 | else |
| 115 | return 0 |
| 116 | fi |
| 117 | } |
| 118 | |
| 119 | gitflow_require_branches_equal() { |
| 120 | gitflow_require_local_branch "$1" |
| 121 | gitflow_require_remote_branch "$2" |
| 122 | gitflow_test_branches_equal "$1" "$2" |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame^] | 123 | typeset -i status=$? |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 124 | if [ $status -gt 0 ]; then |
| 125 | warn "Branches '$1' and '$2' have diverged." |
| 126 | if [ $status -eq 1 ]; then |
| 127 | die "And branch '$1' may be fast-forwarded." |
| 128 | elif [ $status -eq 2 ]; then |
| 129 | # Warn here, since there is no harm in being ahead |
| 130 | warn "And local branch '$1' is ahead of '$2'." |
| 131 | else |
| 132 | die "Branches need merging first." |
| 133 | fi |
| 134 | fi |
| 135 | } |
| 136 | |
| 137 | # |
| 138 | # gitflow_is_branch_merged_into() |
| 139 | # |
| 140 | # Checks whether branch $1 is succesfully merged into $2 |
| 141 | # |
| 142 | gitflow_is_branch_merged_into() { |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame^] | 143 | typeset subject=$1 |
| 144 | typeset base=$2 |
| 145 | typeset all_merges=$(git branch --contains $subject | sed 's/^[* ] //') |
| 146 | has $base $all_merges |
Vincent Driessen | c3607ac | 2010-02-05 19:53:45 +0100 | [diff] [blame] | 147 | } |