Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 1 | # |
Vincent Driessen | 6c2d30b | 2010-01-26 22:18:36 +0100 | [diff] [blame] | 2 | # git-flow -- A collection of Git extensions to provide high-level |
| 3 | # repository operations for Vincent Driessen's branching model. |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 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 | |
Vincent Driessen | ab3dc49 | 2010-01-29 12:35:49 +0100 | [diff] [blame] | 15 | VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag) |
Benedikt Böhm | 49dd62b | 2010-01-28 00:51:15 +0100 | [diff] [blame] | 16 | PREFIX=$(git config --get gitflow.prefix.hotfix || echo hotfix/) |
Vincent Driessen | 2acfffd | 2010-01-29 12:37:22 +0100 | [diff] [blame] | 17 | FLAG_FETCH=1 |
Benedikt Böhm | 49dd62b | 2010-01-28 00:51:15 +0100 | [diff] [blame] | 18 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 19 | usage() { |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 20 | echo "usage: git flow hotfix [list] [-v]" |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 21 | echo " git flow hotfix start <version> [<base>]" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 22 | echo " git flow hotfix finish <version>" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 23 | } |
| 24 | |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 25 | cmd_default() { |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 26 | cmd_list "$@" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 29 | cmd_list() { |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 30 | DEFINE_boolean verbose false 'verbose (more) output' v |
| 31 | parse_args "$@" |
| 32 | |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 33 | HOTFIX_BRANCHES="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")" |
| 34 | if [ -z "$HOTFIX_BRANCHES" ]; then |
| 35 | warn "No hotfix branches exist." |
| 36 | exit 0 |
| 37 | fi |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 38 | |
| 39 | CURRENT_BRANCH=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g') |
| 40 | SHORT_NAMES=$(echo "$HOTFIX_BRANCHES" | sed "s?^$PREFIX??g") |
| 41 | # determine column width first |
| 42 | width=0 |
| 43 | for branch in $SHORT_NAMES; do |
| 44 | len=$(($(echo "$branch" | wc -c) - 1)) |
| 45 | width=$(max $width $len) |
| 46 | done |
| 47 | width=$(($width + 3)) |
| 48 | |
| 49 | for branch in $SHORT_NAMES; do |
| 50 | fullname="$PREFIX$branch" |
| 51 | base=$(git merge-base "$fullname" "$MASTER_BRANCH") |
| 52 | master_sha=$(git rev-parse "$MASTER_BRANCH") |
| 53 | branch_sha=$(git rev-parse "$fullname") |
| 54 | if [ "$fullname" = "$CURRENT_BRANCH" ]; then |
| 55 | printf "* " |
| 56 | else |
| 57 | printf " " |
| 58 | fi |
| 59 | if flag verbose; then |
| 60 | printf "%-${width}s" "$branch" |
| 61 | if [ "$branch_sha" = "$master_sha" ]; then |
| 62 | printf "(no commits yet)" |
| 63 | else |
| 64 | tagname=$(git name-rev --tags --no-undefined --name-only $base) |
| 65 | if [ "$tagname" != "" ]; then |
| 66 | nicename="$tagname" |
| 67 | else |
| 68 | nicename="$(git rev-parse --short $base)" |
| 69 | fi |
| 70 | printf "(based on $nicename)" |
| 71 | fi |
| 72 | else |
| 73 | printf "%s" "$branch" |
| 74 | fi |
| 75 | echo |
| 76 | done |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 79 | cmd_help() { |
| 80 | usage |
| 81 | exit 0 |
| 82 | } |
| 83 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 84 | parse_args() { |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 85 | # parse options |
| 86 | FLAGS "$@" || exit $? |
| 87 | eval set -- "${FLAGS_ARGV}" |
| 88 | |
| 89 | # read arguments into global variables |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 90 | VERSION="$1" |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 91 | BRANCH=$PREFIX$VERSION |
| 92 | } |
| 93 | |
| 94 | require_version_arg() { |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 95 | if [ "$VERSION" = "" ]; then |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 96 | warn "Missing argument <version>" |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 97 | usage |
| 98 | exit 1 |
| 99 | fi |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 102 | require_base_is_on_master() { |
| 103 | if ! git branch --contains "$BASE" 2>/dev/null \ |
| 104 | | sed 's/[* ] //g' \ |
| 105 | | grep -q "^$MASTER_BRANCH\$"; then |
| 106 | die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'." |
| 107 | fi |
| 108 | } |
| 109 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 110 | cmd_start() { |
| 111 | parse_args "$@" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 112 | BASE="${2:-$MASTER_BRANCH}" |
| 113 | require_version_arg |
| 114 | require_base_is_on_master |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 115 | |
| 116 | # sanity checks |
Vincent Driessen | 4838644 | 2010-01-29 10:30:40 +0100 | [diff] [blame] | 117 | gitflow_require_clean_working_tree |
Vincent Driessen | 2acfffd | 2010-01-29 12:37:22 +0100 | [diff] [blame] | 118 | if [ $FLAG_FETCH -eq 1 ]; then |
| 119 | git fetch -q $ORIGIN $MASTER_BRANCH |
| 120 | fi |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 121 | gitflow_require_branches_equal $MASTER_BRANCH $ORIGIN/$MASTER_BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 122 | gitflow_require_branch_absent $BRANCH |
| 123 | |
| 124 | # create branch |
| 125 | git checkout -b $BRANCH $BASE |
| 126 | |
| 127 | echo |
| 128 | echo "Summary of actions:" |
| 129 | echo "- A new branch '$BRANCH' was created, based on '$BASE'" |
| 130 | echo "- You are now on branch '$BRANCH'" |
| 131 | echo |
| 132 | echo "Follow-up actions:" |
| 133 | echo "- Bump the version number now!" |
| 134 | echo "- Start committing your hot fixes" |
| 135 | echo "- When done, run:" |
| 136 | echo |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 137 | echo " git flow hotfix finish '$VERSION'" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 138 | echo |
| 139 | } |
| 140 | |
| 141 | cmd_finish() { |
| 142 | parse_args "$@" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 143 | require_version_arg |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 144 | |
| 145 | # sanity checks |
Vincent Driessen | 4838644 | 2010-01-29 10:30:40 +0100 | [diff] [blame] | 146 | gitflow_require_clean_working_tree |
Vincent Driessen | 2acfffd | 2010-01-29 12:37:22 +0100 | [diff] [blame] | 147 | if [ $FLAG_FETCH -eq 1 ]; then |
| 148 | git fetch -q $ORIGIN $MASTER_BRANCH |
| 149 | git fetch -q $ORIGIN $DEVELOP_BRANCH |
| 150 | fi |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 151 | gitflow_require_branches_equal $MASTER_BRANCH $ORIGIN/$MASTER_BRANCH |
| 152 | gitflow_require_branches_equal $DEVELOP_BRANCH $ORIGIN/$DEVELOP_BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 153 | |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 154 | # merge into master |
| 155 | git checkout $MASTER_BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 156 | git merge --no-ff $BRANCH |
Vincent Driessen | ab3dc49 | 2010-01-29 12:35:49 +0100 | [diff] [blame] | 157 | git tag $VERSION_PREFIX$VERSION |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 158 | |
| 159 | # merge into develop if we fixed a master issue |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 160 | git checkout $DEVELOP_BRANCH |
| 161 | git merge --no-ff $BRANCH |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 162 | |
| 163 | # delete branch |
| 164 | git branch -d $BRANCH |
| 165 | |
| 166 | # TODO: Implement an optional push to master |
| 167 | # git push origin develop; git push origin master; git push --tags origin |
| 168 | |
| 169 | echo |
| 170 | echo "Summary of actions:" |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 171 | echo "- Latest objects have been fetched from '$ORIGIN'" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 172 | echo "- Hotfix branch has been merged into '$MASTER_BRANCH'" |
Vincent Driessen | ab3dc49 | 2010-01-29 12:35:49 +0100 | [diff] [blame] | 173 | echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 174 | echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 175 | echo "- Hotfix branch '$BRANCH' has been deleted" |
| 176 | echo |
| 177 | } |