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