Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Rewrite revision history |
| 4 | # Copyright (c) Petr Baudis, 2006 |
| 5 | # Minimal changes to "port" it to core-git (c) Johannes Schindelin, 2007 |
| 6 | # |
Johannes Schindelin | c401b33 | 2007-07-04 00:41:55 +0100 | [diff] [blame] | 7 | # Lets you rewrite the revision history of the current branch, creating |
| 8 | # a new branch. You can specify a number of filters to modify the commits, |
| 9 | # files and trees. |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 10 | |
Johannes Schindelin | 16ed34a | 2007-11-28 15:50:38 +0000 | [diff] [blame] | 11 | # The following functions will also be available in the commit filter: |
| 12 | |
| 13 | functions=$(cat << \EOF |
Steffen Prohaska | b5669a0 | 2007-07-04 10:36:24 +0200 | [diff] [blame] | 14 | warn () { |
| 15 | echo "$*" >&2 |
| 16 | } |
| 17 | |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 18 | map() |
| 19 | { |
Johannes Sixt | 3520e1e | 2007-06-06 20:38:35 +0200 | [diff] [blame] | 20 | # if it was not rewritten, take the original |
Johannes Sixt | c57a349 | 2007-07-04 14:08:17 +0200 | [diff] [blame] | 21 | if test -r "$workdir/../map/$1" |
| 22 | then |
| 23 | cat "$workdir/../map/$1" |
| 24 | else |
| 25 | echo "$1" |
| 26 | fi |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Johannes Schindelin | f95eef1 | 2007-08-31 20:06:27 +0100 | [diff] [blame] | 29 | # if you run 'skip_commit "$@"' in a commit filter, it will print |
| 30 | # the (mapped) parents, effectively skipping the commit. |
| 31 | |
| 32 | skip_commit() |
| 33 | { |
| 34 | shift; |
| 35 | while [ -n "$1" ]; |
| 36 | do |
| 37 | shift; |
| 38 | map "$1"; |
| 39 | shift; |
| 40 | done; |
| 41 | } |
| 42 | |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 43 | # if you run 'git_commit_non_empty_tree "$@"' in a commit filter, |
| 44 | # it will skip commits that leave the tree untouched, commit the other. |
| 45 | git_commit_non_empty_tree() |
| 46 | { |
| 47 | if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then |
| 48 | map "$3" |
| 49 | else |
| 50 | git commit-tree "$@" |
| 51 | fi |
| 52 | } |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 53 | # override die(): this version puts in an extra line break, so that |
| 54 | # the progress is still visible |
| 55 | |
| 56 | die() |
| 57 | { |
| 58 | echo >&2 |
| 59 | echo "$*" >&2 |
| 60 | exit 1 |
| 61 | } |
Johannes Schindelin | 16ed34a | 2007-11-28 15:50:38 +0000 | [diff] [blame] | 62 | EOF |
| 63 | ) |
| 64 | |
| 65 | eval "$functions" |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 66 | |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 67 | # When piped a commit, output a script to set the ident of either |
| 68 | # "author" or "committer |
| 69 | |
| 70 | set_ident () { |
Jeff King | 40a7ce6 | 2008-03-12 17:29:57 -0400 | [diff] [blame] | 71 | lid="$(echo "$1" | tr "[A-Z]" "[a-z]")" |
| 72 | uid="$(echo "$1" | tr "[a-z]" "[A-Z]")" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 73 | pick_id_script=' |
| 74 | /^'$lid' /{ |
| 75 | s/'\''/'\''\\'\'\''/g |
| 76 | h |
| 77 | s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/ |
| 78 | s/'\''/'\''\'\'\''/g |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 79 | s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 80 | |
| 81 | g |
| 82 | s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/ |
| 83 | s/'\''/'\''\'\'\''/g |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 84 | s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 85 | |
| 86 | g |
| 87 | s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/ |
| 88 | s/'\''/'\''\'\'\''/g |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 89 | s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 90 | |
| 91 | q |
| 92 | } |
| 93 | ' |
| 94 | |
| 95 | LANG=C LC_ALL=C sed -ne "$pick_id_script" |
| 96 | # Ensure non-empty id name. |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 97 | echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Matthieu Moy | 83e355a | 2009-08-18 11:51:00 +0200 | [diff] [blame] | 100 | USAGE="[--env-filter <command>] [--tree-filter <command>] |
| 101 | [--index-filter <command>] [--parent-filter <command>] |
| 102 | [--msg-filter <command>] [--commit-filter <command>] |
| 103 | [--tag-name-filter <command>] [--subdirectory-filter <directory>] |
| 104 | [--original <namespace>] [-d <directory>] [-f | --force] |
| 105 | [<rev-list options>...]" |
Johannes Schindelin | 7e0f170 | 2007-08-31 20:05:36 +0100 | [diff] [blame] | 106 | |
Junio C Hamano | 8f321a3 | 2007-11-06 01:50:02 -0800 | [diff] [blame] | 107 | OPTIONS_SPEC= |
Johannes Schindelin | 7e0f170 | 2007-08-31 20:05:36 +0100 | [diff] [blame] | 108 | . git-sh-setup |
| 109 | |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 110 | if [ "$(is_bare_repository)" = false ]; then |
Johannes Schindelin | 26be15f | 2009-02-05 19:19:33 +0100 | [diff] [blame] | 111 | git diff-files --ignore-submodules --quiet && |
Junio C Hamano | 38762c4 | 2007-11-28 16:15:04 -0800 | [diff] [blame] | 112 | git diff-index --cached --quiet HEAD -- || |
Johannes Schindelin | 46eb449 | 2007-10-17 03:23:10 +0100 | [diff] [blame] | 113 | die "Cannot rewrite branch(es) with a dirty working directory." |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 114 | fi |
Johannes Schindelin | 46eb449 | 2007-10-17 03:23:10 +0100 | [diff] [blame] | 115 | |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 116 | tempdir=.git-rewrite |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 117 | filter_env= |
| 118 | filter_tree= |
| 119 | filter_index= |
| 120 | filter_parent= |
| 121 | filter_msg=cat |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 122 | filter_commit= |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 123 | filter_tag_name= |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 124 | filter_subdir= |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 125 | orig_namespace=refs/original/ |
| 126 | force= |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 127 | prune_empty= |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 128 | remap_to_ancestor= |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 129 | while : |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 130 | do |
| 131 | case "$1" in |
| 132 | --) |
| 133 | shift |
| 134 | break |
| 135 | ;; |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 136 | --force|-f) |
| 137 | shift |
| 138 | force=t |
| 139 | continue |
| 140 | ;; |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 141 | --remap-to-ancestor) |
Csaba Henk | 7ec344d | 2010-08-27 20:44:56 +0000 | [diff] [blame] | 142 | # deprecated ($remap_to_ancestor is set now automatically) |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 143 | shift |
| 144 | remap_to_ancestor=t |
| 145 | continue |
| 146 | ;; |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 147 | --prune-empty) |
| 148 | shift |
| 149 | prune_empty=t |
| 150 | continue |
| 151 | ;; |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 152 | -*) |
| 153 | ;; |
| 154 | *) |
| 155 | break; |
| 156 | esac |
| 157 | |
| 158 | # all switches take one argument |
| 159 | ARG="$1" |
| 160 | case "$#" in 1) usage ;; esac |
| 161 | shift |
| 162 | OPTARG="$1" |
| 163 | shift |
| 164 | |
| 165 | case "$ARG" in |
| 166 | -d) |
| 167 | tempdir="$OPTARG" |
| 168 | ;; |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 169 | --env-filter) |
| 170 | filter_env="$OPTARG" |
| 171 | ;; |
| 172 | --tree-filter) |
| 173 | filter_tree="$OPTARG" |
| 174 | ;; |
| 175 | --index-filter) |
| 176 | filter_index="$OPTARG" |
| 177 | ;; |
| 178 | --parent-filter) |
| 179 | filter_parent="$OPTARG" |
| 180 | ;; |
| 181 | --msg-filter) |
| 182 | filter_msg="$OPTARG" |
| 183 | ;; |
| 184 | --commit-filter) |
Johannes Schindelin | 16ed34a | 2007-11-28 15:50:38 +0000 | [diff] [blame] | 185 | filter_commit="$functions; $OPTARG" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 186 | ;; |
| 187 | --tag-name-filter) |
| 188 | filter_tag_name="$OPTARG" |
| 189 | ;; |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 190 | --subdirectory-filter) |
| 191 | filter_subdir="$OPTARG" |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 192 | remap_to_ancestor=t |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 193 | ;; |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 194 | --original) |
Junio C Hamano | 55ced83 | 2007-08-30 19:17:42 -0700 | [diff] [blame] | 195 | orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/ |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 196 | ;; |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 197 | *) |
| 198 | usage |
| 199 | ;; |
| 200 | esac |
| 201 | done |
| 202 | |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 203 | case "$prune_empty,$filter_commit" in |
| 204 | ,) |
| 205 | filter_commit='git commit-tree "$@"';; |
| 206 | t,) |
| 207 | filter_commit="$functions;"' git_commit_non_empty_tree "$@"';; |
| 208 | ,*) |
| 209 | ;; |
| 210 | *) |
Jacob Helwig | 5da8171 | 2010-02-11 18:46:22 -0800 | [diff] [blame] | 211 | die "Cannot set --prune-empty and --commit-filter at the same time" |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 212 | esac |
| 213 | |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 214 | case "$force" in |
| 215 | t) |
| 216 | rm -rf "$tempdir" |
| 217 | ;; |
| 218 | '') |
| 219 | test -d "$tempdir" && |
| 220 | die "$tempdir already exists, please remove it" |
| 221 | esac |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 222 | mkdir -p "$tempdir/t" && |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 223 | tempdir="$(cd "$tempdir"; pwd)" && |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 224 | cd "$tempdir/t" && |
| 225 | workdir="$(pwd)" || |
| 226 | die "" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 227 | |
Brandon Casey | def16e7 | 2008-01-28 15:16:02 -0600 | [diff] [blame] | 228 | # Remove tempdir on exit |
| 229 | trap 'cd ../..; rm -rf "$tempdir"' 0 |
| 230 | |
Lars Noschinski | 88e3880 | 2009-02-18 09:35:36 +0100 | [diff] [blame] | 231 | ORIG_GIT_DIR="$GIT_DIR" |
| 232 | ORIG_GIT_WORK_TREE="$GIT_WORK_TREE" |
| 233 | ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE" |
| 234 | GIT_WORK_TREE=. |
| 235 | export GIT_DIR GIT_WORK_TREE |
| 236 | |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 237 | # Make sure refs/original is empty |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 238 | git for-each-ref > "$tempdir"/backup-refs || exit |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 239 | while read sha1 type name |
| 240 | do |
| 241 | case "$force,$name" in |
| 242 | ,$orig_namespace*) |
John Tapsell | 734cd57 | 2009-02-19 07:36:35 +0000 | [diff] [blame] | 243 | die "Cannot create a new backup. |
| 244 | A previous backup already exists in $orig_namespace |
| 245 | Force overwriting the backup with -f" |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 246 | ;; |
| 247 | t,$orig_namespace*) |
| 248 | git update-ref -d "$name" $sha1 |
| 249 | ;; |
| 250 | esac |
| 251 | done < "$tempdir"/backup-refs |
| 252 | |
Junio C Hamano | 418fa3a | 2008-01-05 12:18:43 -0800 | [diff] [blame] | 253 | # The refs should be updated if their heads were rewritten |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 254 | git rev-parse --no-flags --revs-only --symbolic-full-name \ |
| 255 | --default HEAD "$@" > "$tempdir"/raw-heads || exit |
| 256 | sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 257 | |
| 258 | test -s "$tempdir"/heads || |
| 259 | die "Which ref do you want to rewrite?" |
| 260 | |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 261 | GIT_INDEX_FILE="$(pwd)/../index" |
| 262 | export GIT_INDEX_FILE |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 263 | |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 264 | # map old->new commit ids for rewriting parents |
| 265 | mkdir ../map || die "Could not create map/ directory" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 266 | |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 267 | # we need "--" only if there are no path arguments in $@ |
| 268 | nonrevs=$(git rev-parse --no-revs "$@") || exit |
Csaba Henk | 7ec344d | 2010-08-27 20:44:56 +0000 | [diff] [blame] | 269 | if test -z "$nonrevs" |
| 270 | then |
| 271 | dashdash=-- |
| 272 | else |
| 273 | dashdash= |
| 274 | remap_to_ancestor=t |
| 275 | fi |
| 276 | |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 277 | rev_args=$(git rev-parse --revs-only "$@") |
| 278 | |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 279 | case "$filter_subdir" in |
| 280 | "") |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 281 | eval set -- "$(git rev-parse --sq --no-revs "$@")" |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 282 | ;; |
| 283 | *) |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 284 | eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \ |
| 285 | "$filter_subdir")" |
| 286 | ;; |
| 287 | esac |
| 288 | |
| 289 | git rev-list --reverse --topo-order --default HEAD \ |
| 290 | --parents --simplify-merges $rev_args "$@" > ../revs || |
| 291 | die "Could not get the commits" |
Josh Triplett | 9d6f220 | 2007-07-14 01:05:43 -0700 | [diff] [blame] | 292 | commits=$(wc -l <../revs | tr -d " ") |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 293 | |
| 294 | test $commits -eq 0 && die "Found nothing to rewrite" |
| 295 | |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 296 | # Rewrite the commits |
| 297 | |
Elijah Newren | d5b0c97 | 2009-03-25 15:51:01 -0600 | [diff] [blame] | 298 | git_filter_branch__commit_count=0 |
Johannes Sixt | 813b473 | 2007-06-08 23:28:39 +0200 | [diff] [blame] | 299 | while read commit parents; do |
Elijah Newren | d5b0c97 | 2009-03-25 15:51:01 -0600 | [diff] [blame] | 300 | git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1)) |
| 301 | printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 302 | |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 303 | case "$filter_subdir" in |
| 304 | "") |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 305 | git read-tree -i -m $commit |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 306 | ;; |
| 307 | *) |
Junio C Hamano | 5b044ac | 2008-03-08 12:25:58 -0800 | [diff] [blame] | 308 | # The commit may not have the subdirectory at all |
| 309 | err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || { |
Miklos Vajna | d69da76 | 2008-12-03 14:26:48 +0100 | [diff] [blame] | 310 | if ! git rev-parse -q --verify $commit:"$filter_subdir" |
Junio C Hamano | 5b044ac | 2008-03-08 12:25:58 -0800 | [diff] [blame] | 311 | then |
| 312 | rm -f "$GIT_INDEX_FILE" |
| 313 | else |
| 314 | echo >&2 "$err" |
| 315 | false |
| 316 | fi |
| 317 | } |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 318 | esac || die "Could not initialize the index" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 319 | |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 320 | GIT_COMMIT=$commit |
| 321 | export GIT_COMMIT |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 322 | git cat-file commit "$commit" >../commit || |
| 323 | die "Cannot read commit $commit" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 324 | |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 325 | eval "$(set_ident AUTHOR <../commit)" || |
| 326 | die "setting author failed for commit $commit" |
| 327 | eval "$(set_ident COMMITTER <../commit)" || |
| 328 | die "setting committer failed for commit $commit" |
| 329 | eval "$filter_env" < /dev/null || |
| 330 | die "env filter failed: $filter_env" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 331 | |
| 332 | if [ "$filter_tree" ]; then |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 333 | git checkout-index -f -u -a || |
| 334 | die "Could not checkout the index" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 335 | # files that $commit removed are now still in the working tree; |
| 336 | # remove them, else they would be added again |
veillette@yahoo.ca | 6a589fd | 2008-03-31 09:14:15 +0200 | [diff] [blame] | 337 | git clean -d -q -f -x |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 338 | eval "$filter_tree" < /dev/null || |
| 339 | die "tree filter failed: $filter_tree" |
| 340 | |
Junio C Hamano | 1fe32cb | 2008-02-15 23:57:26 -0800 | [diff] [blame] | 341 | ( |
Michal Sojka | 03ca839 | 2010-01-28 10:08:46 +0100 | [diff] [blame] | 342 | git diff-index -r --name-only --ignore-submodules $commit && |
Junio C Hamano | 1fe32cb | 2008-02-15 23:57:26 -0800 | [diff] [blame] | 343 | git ls-files --others |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 344 | ) > "$tempdir"/tree-state || exit |
| 345 | git update-index --add --replace --remove --stdin \ |
| 346 | < "$tempdir"/tree-state || exit |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 347 | fi |
| 348 | |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 349 | eval "$filter_index" < /dev/null || |
| 350 | die "index filter failed: $filter_index" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 351 | |
| 352 | parentstr= |
Johannes Sixt | 813b473 | 2007-06-08 23:28:39 +0200 | [diff] [blame] | 353 | for parent in $parents; do |
Johannes Sixt | 3520e1e | 2007-06-06 20:38:35 +0200 | [diff] [blame] | 354 | for reparent in $(map "$parent"); do |
| 355 | parentstr="$parentstr -p $reparent" |
| 356 | done |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 357 | done |
| 358 | if [ "$filter_parent" ]; then |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 359 | parentstr="$(echo "$parentstr" | eval "$filter_parent")" || |
| 360 | die "parent filter failed: $filter_parent" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 361 | fi |
| 362 | |
| 363 | sed -e '1,/^$/d' <../commit | \ |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 364 | eval "$filter_msg" > ../message || |
| 365 | die "msg filter failed: $filter_msg" |
Jeff King | 4bf9f27 | 2008-03-12 17:41:39 -0400 | [diff] [blame] | 366 | @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \ |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 367 | $(git write-tree) $parentstr < ../message > ../map/$commit || |
| 368 | die "could not write rewritten commit" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 369 | done <../revs |
| 370 | |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 371 | # If we are filtering for paths, as in the case of a subdirectory |
| 372 | # filter, it is possible that a specified head is not in the set of |
| 373 | # rewritten commits, because it was pruned by the revision walker. |
| 374 | # Ancestor remapping fixes this by mapping these heads to the unique |
| 375 | # nearest ancestor that survived the pruning. |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 376 | |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 377 | if test "$remap_to_ancestor" = t |
Thomas Rast | a0e4639 | 2008-08-12 10:45:58 +0200 | [diff] [blame] | 378 | then |
| 379 | while read ref |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 380 | do |
Thomas Rast | a0e4639 | 2008-08-12 10:45:58 +0200 | [diff] [blame] | 381 | sha1=$(git rev-parse "$ref"^0) |
| 382 | test -f "$workdir"/../map/$sha1 && continue |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 383 | ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@") |
Thomas Rast | a0e4639 | 2008-08-12 10:45:58 +0200 | [diff] [blame] | 384 | test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1 |
| 385 | done < "$tempdir"/heads |
| 386 | fi |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 387 | |
| 388 | # Finally update the refs |
| 389 | |
| 390 | _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' |
| 391 | _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 392 | echo |
| 393 | while read ref |
| 394 | do |
| 395 | # avoid rewriting a ref twice |
| 396 | test -f "$orig_namespace$ref" && continue |
| 397 | |
| 398 | sha1=$(git rev-parse "$ref"^0) |
| 399 | rewritten=$(map $sha1) |
| 400 | |
| 401 | test $sha1 = "$rewritten" && |
| 402 | warn "WARNING: Ref '$ref' is unchanged" && |
| 403 | continue |
| 404 | |
| 405 | case "$rewritten" in |
| 406 | '') |
| 407 | echo "Ref '$ref' was deleted" |
| 408 | git update-ref -m "filter-branch: delete" -d "$ref" $sha1 || |
| 409 | die "Could not delete $ref" |
Johannes Schindelin | 9840906 | 2007-06-05 16:58:13 +0100 | [diff] [blame] | 410 | ;; |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 411 | $_x40) |
| 412 | echo "Ref '$ref' was rewritten" |
Thomas Rast | 261044e | 2008-08-08 01:50:31 +0200 | [diff] [blame] | 413 | if ! git update-ref -m "filter-branch: rewrite" \ |
| 414 | "$ref" $rewritten $sha1 2>/dev/null; then |
| 415 | if test $(git cat-file -t "$ref") = tag; then |
| 416 | if test -z "$filter_tag_name"; then |
| 417 | warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag." |
| 418 | warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag." |
| 419 | fi |
| 420 | else |
| 421 | die "Could not rewrite $ref" |
| 422 | fi |
| 423 | fi |
Johannes Schindelin | 9840906 | 2007-06-05 16:58:13 +0100 | [diff] [blame] | 424 | ;; |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 425 | *) |
| 426 | # NEEDSWORK: possibly add -Werror, making this an error |
| 427 | warn "WARNING: '$ref' was rewritten into multiple commits:" |
| 428 | warn "$rewritten" |
| 429 | warn "WARNING: Ref '$ref' points to the first one now." |
| 430 | rewritten=$(echo "$rewritten" | head -n 1) |
| 431 | git update-ref -m "filter-branch: rewrite to first" \ |
| 432 | "$ref" $rewritten $sha1 || |
| 433 | die "Could not rewrite $ref" |
| 434 | ;; |
| 435 | esac |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 436 | git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 || |
| 437 | exit |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 438 | done < "$tempdir"/heads |
| 439 | |
| 440 | # TODO: This should possibly go, with the semantics that all positive given |
| 441 | # refs are updated, and their original heads stored in refs/original/ |
| 442 | # Filter tags |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 443 | |
| 444 | if [ "$filter_tag_name" ]; then |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 445 | git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags | |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 446 | while read sha1 type ref; do |
| 447 | ref="${ref#refs/tags/}" |
| 448 | # XXX: Rewrite tagged trees as well? |
| 449 | if [ "$type" != "commit" -a "$type" != "tag" ]; then |
| 450 | continue; |
| 451 | fi |
| 452 | |
| 453 | if [ "$type" = "tag" ]; then |
| 454 | # Dereference to a commit |
| 455 | sha1t="$sha1" |
Dan Loewenherz | 7bd93c1 | 2009-04-22 21:46:02 -0400 | [diff] [blame] | 456 | sha1="$(git rev-parse -q "$sha1"^{commit})" || continue |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 457 | fi |
| 458 | |
| 459 | [ -f "../map/$sha1" ] || continue |
| 460 | new_sha1="$(cat "../map/$sha1")" |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 461 | GIT_COMMIT="$sha1" |
| 462 | export GIT_COMMIT |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 463 | new_ref="$(echo "$ref" | eval "$filter_tag_name")" || |
| 464 | die "tag name filter failed: $filter_tag_name" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 465 | |
| 466 | echo "$ref -> $new_ref ($sha1 -> $new_sha1)" |
| 467 | |
| 468 | if [ "$type" = "tag" ]; then |
Johannes Sixt | a9da166 | 2008-08-21 16:45:11 +0200 | [diff] [blame] | 469 | new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \ |
| 470 | "$new_sha1" "$new_ref" |
| 471 | git cat-file tag "$ref" | |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 472 | sed -n \ |
Stephen Boyd | 9524cf2 | 2010-01-26 15:08:31 -0800 | [diff] [blame] | 473 | -e '1,/^$/{ |
Johannes Sixt | a9da166 | 2008-08-21 16:45:11 +0200 | [diff] [blame] | 474 | /^object /d |
| 475 | /^type /d |
| 476 | /^tag /d |
Stephen Boyd | 9524cf2 | 2010-01-26 15:08:31 -0800 | [diff] [blame] | 477 | }' \ |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 478 | -e '/^-----BEGIN PGP SIGNATURE-----/q' \ |
Johannes Sixt | a9da166 | 2008-08-21 16:45:11 +0200 | [diff] [blame] | 479 | -e 'p' ) | |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 480 | git mktag) || |
| 481 | die "Could not create new tag object for $ref" |
| 482 | if git cat-file tag "$ref" | \ |
Junio C Hamano | e1622bf | 2009-11-23 15:56:32 -0800 | [diff] [blame] | 483 | sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1 |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 484 | then |
| 485 | warn "gpg signature stripped from tag object $sha1t" |
| 486 | fi |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 487 | fi |
| 488 | |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 489 | git update-ref "refs/tags/$new_ref" "$new_sha1" || |
| 490 | die "Could not write tag $new_ref" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 491 | done |
| 492 | fi |
| 493 | |
| 494 | cd ../.. |
| 495 | rm -rf "$tempdir" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 496 | |
Brandon Casey | def16e7 | 2008-01-28 15:16:02 -0600 | [diff] [blame] | 497 | trap - 0 |
| 498 | |
Eric Kidd | 9273b56 | 2009-02-03 13:27:03 -0500 | [diff] [blame] | 499 | unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE |
| 500 | test -z "$ORIG_GIT_DIR" || { |
| 501 | GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR |
| 502 | } |
| 503 | test -z "$ORIG_GIT_WORK_TREE" || { |
| 504 | GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" && |
| 505 | export GIT_WORK_TREE |
| 506 | } |
| 507 | test -z "$ORIG_GIT_INDEX_FILE" || { |
| 508 | GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" && |
| 509 | export GIT_INDEX_FILE |
| 510 | } |
| 511 | |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 512 | if [ "$(is_bare_repository)" = false ]; then |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 513 | git read-tree -u -m HEAD || exit |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 514 | fi |
Johannes Schindelin | 46eb449 | 2007-10-17 03:23:10 +0100 | [diff] [blame] | 515 | |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 516 | exit 0 |