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 | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 15 | require_git_repo |
| 16 | require_gitflow_initialized |
Vincent Driessen | d72e4ac | 2010-02-16 21:33:51 +0100 | [diff] [blame] | 17 | gitflow_load_settings |
Vincent Driessen | ab3dc49 | 2010-01-29 12:35:49 +0100 | [diff] [blame] | 18 | VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag) |
Vincent Driessen | c1598bf | 2010-02-20 16:52:48 +0100 | [diff] [blame] | 19 | PREFIX=$(git config --get gitflow.prefix.hotfix) |
Benedikt Böhm | 49dd62b | 2010-01-28 00:51:15 +0100 | [diff] [blame] | 20 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 21 | usage() { |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 22 | echo "usage: git flow hotfix [list] [-v]" |
Vincent Driessen | a2e4116 | 2010-02-24 01:37:07 +0100 | [diff] [blame] | 23 | echo " git flow hotfix start [-F] <version> [<base>]" |
| 24 | echo " git flow hotfix finish [-Fsump] <version>" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 25 | } |
| 26 | |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 27 | cmd_default() { |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 28 | cmd_list "$@" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 31 | cmd_list() { |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 32 | DEFINE_boolean verbose false 'verbose (more) output' v |
| 33 | parse_args "$@" |
| 34 | |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 35 | local hotfix_branches |
| 36 | local current_branch |
| 37 | local short_names |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 38 | hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX") |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame] | 39 | if [ -z "$hotfix_branches" ]; then |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 40 | warn "No hotfix branches exist." |
| 41 | exit 0 |
| 42 | fi |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame] | 43 | current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g') |
Vincent Driessen | 6884523 | 2010-02-10 00:43:21 +0100 | [diff] [blame] | 44 | short_names=$(echo "$hotfix_branches" | sed "s ^$PREFIX g") |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 45 | |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 46 | # determine column width first |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 47 | local width=0 |
| 48 | local branch |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame] | 49 | for branch in $short_names; do |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 50 | local len=${#branch} |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 51 | width=$(max $width $len) |
| 52 | done |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 53 | width=$(($width+3)) |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 54 | |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 55 | local branch |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame] | 56 | for branch in $short_names; do |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 57 | local fullname=$PREFIX$branch |
| 58 | local base=$(git merge-base "$fullname" "$MASTER_BRANCH") |
| 59 | local master_sha=$(git rev-parse "$MASTER_BRANCH") |
| 60 | local branch_sha=$(git rev-parse "$fullname") |
Vincent Driessen | 27592dd | 2010-02-06 14:45:39 +0100 | [diff] [blame] | 61 | if [ "$fullname" = "$current_branch" ]; then |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 62 | printf "* " |
| 63 | else |
| 64 | printf " " |
| 65 | fi |
| 66 | if flag verbose; then |
| 67 | printf "%-${width}s" "$branch" |
| 68 | if [ "$branch_sha" = "$master_sha" ]; then |
| 69 | printf "(no commits yet)" |
| 70 | else |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 71 | local tagname=$(git name-rev --tags --no-undefined --name-only "$base") |
| 72 | local nicename |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 73 | if [ "$tagname" != "" ]; then |
Vincent Driessen | c5fcc01 | 2010-02-10 00:18:08 +0100 | [diff] [blame] | 74 | nicename=$tagname |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 75 | else |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 76 | nicename=$(git rev-parse --short "$base") |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 77 | fi |
| 78 | printf "(based on $nicename)" |
| 79 | fi |
| 80 | else |
| 81 | printf "%s" "$branch" |
| 82 | fi |
| 83 | echo |
| 84 | done |
Vincent Driessen | 186d2b5 | 2010-01-27 23:48:39 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 87 | cmd_help() { |
| 88 | usage |
| 89 | exit 0 |
| 90 | } |
| 91 | |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 92 | parse_args() { |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 93 | # parse options |
| 94 | FLAGS "$@" || exit $? |
| 95 | eval set -- "${FLAGS_ARGV}" |
| 96 | |
| 97 | # read arguments into global variables |
Vincent Driessen | c5fcc01 | 2010-02-10 00:18:08 +0100 | [diff] [blame] | 98 | VERSION=$1 |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 99 | BRANCH=$PREFIX$VERSION |
| 100 | } |
| 101 | |
| 102 | require_version_arg() { |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 103 | if [ "$VERSION" = "" ]; then |
Vincent Driessen | 3c337fb | 2010-02-04 11:30:18 +0100 | [diff] [blame] | 104 | warn "Missing argument <version>" |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 105 | usage |
| 106 | exit 1 |
| 107 | fi |
Vincent Driessen | b866b01 | 2010-01-28 01:01:53 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 110 | require_base_is_on_master() { |
| 111 | if ! git branch --contains "$BASE" 2>/dev/null \ |
| 112 | | sed 's/[* ] //g' \ |
| 113 | | grep -q "^$MASTER_BRANCH\$"; then |
| 114 | die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'." |
| 115 | fi |
| 116 | } |
| 117 | |
Vincent Driessen | 6d64d2c | 2010-02-16 06:41:13 +0100 | [diff] [blame] | 118 | require_no_existing_hotfix_branches() { |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 119 | local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX") |
Vincent Driessen | 6d64d2c | 2010-02-16 06:41:13 +0100 | [diff] [blame] | 120 | local first_branch=$(echo ${hotfix_branches} | head -n1) |
| 121 | first_branch=${first_branch#$PREFIX} |
| 122 | [ -z "$hotfix_branches" ] || \ |
| 123 | die "There is an existing hotfix branch ($first_branch). Finish that one first." |
| 124 | } |
| 125 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 126 | cmd_start() { |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 127 | DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 128 | parse_args "$@" |
Vincent Driessen | c5fcc01 | 2010-02-10 00:18:08 +0100 | [diff] [blame] | 129 | BASE=${2:-$MASTER_BRANCH} |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 130 | require_version_arg |
| 131 | require_base_is_on_master |
Vincent Driessen | 6d64d2c | 2010-02-16 06:41:13 +0100 | [diff] [blame] | 132 | require_no_existing_hotfix_branches |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 133 | |
| 134 | # sanity checks |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 135 | require_clean_working_tree |
| 136 | require_branch_absent "$BRANCH" |
| 137 | require_tag_absent "$VERSION_PREFIX$VERSION" |
Vincent Driessen | ca73caf | 2010-02-07 19:46:38 +0100 | [diff] [blame] | 138 | if flag fetch; then |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 139 | git fetch -q "$ORIGIN" "$MASTER_BRANCH" |
Vincent Driessen | 2acfffd | 2010-01-29 12:37:22 +0100 | [diff] [blame] | 140 | fi |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 141 | require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 142 | |
| 143 | # create branch |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 144 | git checkout -b "$BRANCH" "$BASE" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 145 | |
| 146 | echo |
| 147 | echo "Summary of actions:" |
| 148 | echo "- A new branch '$BRANCH' was created, based on '$BASE'" |
| 149 | echo "- You are now on branch '$BRANCH'" |
| 150 | echo |
| 151 | echo "Follow-up actions:" |
| 152 | echo "- Bump the version number now!" |
| 153 | echo "- Start committing your hot fixes" |
| 154 | echo "- When done, run:" |
| 155 | echo |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 156 | echo " git flow hotfix finish '$VERSION'" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 157 | echo |
| 158 | } |
| 159 | |
| 160 | cmd_finish() { |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 161 | DEFINE_boolean fetch true "fetch from $ORIGIN before performing finish" F |
| 162 | DEFINE_boolean sign false "sign the release tag cryptographically" s |
| 163 | DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u |
| 164 | DEFINE_string message "" "use the given tag message" m |
Vincent Driessen | ddba2df | 2010-02-19 06:42:18 +0100 | [diff] [blame] | 165 | DEFINE_boolean push false "push to $ORIGIN after performing finish" p |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 166 | parse_args "$@" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 167 | require_version_arg |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 168 | |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 169 | # handle flags that imply other flags |
| 170 | if [ "$FLAGS_signingkey" != "" ]; then |
| 171 | FLAGS_sign=$FLAGS_TRUE |
| 172 | fi |
| 173 | |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 174 | # sanity checks |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 175 | require_branch "$BRANCH" |
| 176 | require_clean_working_tree |
Vincent Driessen | ca73caf | 2010-02-07 19:46:38 +0100 | [diff] [blame] | 177 | if flag fetch; then |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 178 | git fetch -q "$ORIGIN" "$MASTER_BRANCH" || \ |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 179 | die "Could not fetch $MASTER_BRANCH from $ORIGIN." |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 180 | git fetch -q "$ORIGIN" "$DEVELOP_BRANCH" || \ |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 181 | die "Could not fetch $DEVELOP_BRANCH from $ORIGIN." |
Vincent Driessen | 2acfffd | 2010-01-29 12:37:22 +0100 | [diff] [blame] | 182 | fi |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 183 | require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH" |
| 184 | require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 185 | |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 186 | # try to merge into master |
| 187 | # in case a previous attempt to finish this release branch has failed, |
| 188 | # but the merge into master was successful, we skip it now |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 189 | if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 190 | git checkout "$MASTER_BRANCH" || \ |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 191 | die "Could not check out $MASTER_BRANCH." |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 192 | git merge --no-ff "$BRANCH" || \ |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 193 | die "There were merge conflicts." |
| 194 | # TODO: What do we do now? |
| 195 | fi |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 196 | |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 197 | # try to tag the release |
| 198 | # in case a previous attempt to finish this release branch has failed, |
| 199 | # but the tag was set successful, we skip it now |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 200 | local tagname=$VERSION_PREFIX$VERSION |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 201 | if ! git_tag_exists "$tagname"; then |
Vincent Driessen | f46e290 | 2010-02-15 23:01:52 +0100 | [diff] [blame] | 202 | local opts="-a" |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 203 | flag sign && opts="$opts -s" |
| 204 | [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'" |
| 205 | [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'" |
| 206 | git tag $opts "$VERSION_PREFIX$VERSION" || \ |
| 207 | die "Tagging failed. Please run finish again to retry." |
| 208 | fi |
Vincent Driessen | 1a2868b | 2010-02-07 23:23:16 +0100 | [diff] [blame] | 209 | |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 210 | # try to merge into develop |
| 211 | # in case a previous attempt to finish this release branch has failed, |
| 212 | # but the merge into develop was successful, we skip it now |
Vincent Driessen | 7832d6e | 2010-02-21 21:31:03 +0100 | [diff] [blame] | 213 | if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 214 | git checkout "$DEVELOP_BRANCH" || \ |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 215 | die "Could not check out $DEVELOP_BRANCH." |
| 216 | |
Vincent Driessen | d29e315 | 2010-02-09 00:55:06 +0100 | [diff] [blame] | 217 | # TODO: Actually, accounting for 'git describe' pays, so we should |
| 218 | # ideally git merge --no-ff $tagname here, instead! |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 219 | git merge --no-ff "$BRANCH" || \ |
Vincent Driessen | 5fa4758 | 2010-02-09 00:31:33 +0100 | [diff] [blame] | 220 | die "There were merge conflicts." |
| 221 | # TODO: What do we do now? |
| 222 | fi |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 223 | |
| 224 | # delete branch |
Vincent Driessen | a4dd223 | 2010-02-10 00:34:59 +0100 | [diff] [blame] | 225 | git branch -d "$BRANCH" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 226 | |
Vincent Driessen | ddba2df | 2010-02-19 06:42:18 +0100 | [diff] [blame] | 227 | if flag push; then |
| 228 | git push "$ORIGIN" "$DEVELOP_BRANCH" || \ |
| 229 | die "Could not push to $DEVELOP_BRANCH from $ORIGIN." |
| 230 | git push "$ORIGIN" "$MASTER_BRANCH" || \ |
| 231 | die "Could not push to $MASTER_BRANCH from $ORIGIN." |
| 232 | git push --tags "$ORIGIN" || \ |
| 233 | die "Could not push tags to $ORIGIN." |
| 234 | fi |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 235 | |
| 236 | echo |
| 237 | echo "Summary of actions:" |
Benedikt Böhm | 350e715 | 2010-01-26 13:05:05 +0100 | [diff] [blame] | 238 | echo "- Latest objects have been fetched from '$ORIGIN'" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 239 | echo "- Hotfix branch has been merged into '$MASTER_BRANCH'" |
Vincent Driessen | ab3dc49 | 2010-01-29 12:35:49 +0100 | [diff] [blame] | 240 | echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'" |
Vincent Driessen | 010252a | 2010-02-04 10:31:29 +0100 | [diff] [blame] | 241 | echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'" |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 242 | echo "- Hotfix branch '$BRANCH' has been deleted" |
Vincent Driessen | ddba2df | 2010-02-19 06:42:18 +0100 | [diff] [blame] | 243 | if flag push; then |
| 244 | echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'" |
| 245 | fi |
Benedikt Böhm | 00ccea6 | 2010-01-26 12:39:36 +0100 | [diff] [blame] | 246 | echo |
| 247 | } |