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