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 |
brian m. carlson | 03a7f38 | 2018-05-02 00:26:08 +0000 | [diff] [blame] | 14 | EMPTY_TREE=$(git hash-object -t tree /dev/null) |
| 15 | |
Steffen Prohaska | b5669a0 | 2007-07-04 10:36:24 +0200 | [diff] [blame] | 16 | warn () { |
Jon Seymour | 285c6cb | 2011-08-05 23:31:29 +1000 | [diff] [blame] | 17 | echo "$*" >&2 |
Steffen Prohaska | b5669a0 | 2007-07-04 10:36:24 +0200 | [diff] [blame] | 18 | } |
| 19 | |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 20 | map() |
| 21 | { |
Johannes Sixt | 3520e1e | 2007-06-06 20:38:35 +0200 | [diff] [blame] | 22 | # if it was not rewritten, take the original |
Johannes Sixt | c57a349 | 2007-07-04 14:08:17 +0200 | [diff] [blame] | 23 | if test -r "$workdir/../map/$1" |
| 24 | then |
| 25 | cat "$workdir/../map/$1" |
| 26 | else |
| 27 | echo "$1" |
| 28 | fi |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Johannes Schindelin | f95eef1 | 2007-08-31 20:06:27 +0100 | [diff] [blame] | 31 | # if you run 'skip_commit "$@"' in a commit filter, it will print |
| 32 | # the (mapped) parents, effectively skipping the commit. |
| 33 | |
| 34 | skip_commit() |
| 35 | { |
| 36 | shift; |
| 37 | while [ -n "$1" ]; |
| 38 | do |
| 39 | shift; |
| 40 | map "$1"; |
| 41 | shift; |
| 42 | done; |
| 43 | } |
| 44 | |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 45 | # if you run 'git_commit_non_empty_tree "$@"' in a commit filter, |
| 46 | # it will skip commits that leave the tree untouched, commit the other. |
| 47 | git_commit_non_empty_tree() |
| 48 | { |
| 49 | if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then |
| 50 | map "$3" |
brian m. carlson | 03a7f38 | 2018-05-02 00:26:08 +0000 | [diff] [blame] | 51 | elif test $# = 1 && test "$1" = $EMPTY_TREE; then |
Devin J. Pohly | a582a82 | 2017-02-23 02:27:35 -0600 | [diff] [blame] | 52 | : |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 53 | else |
| 54 | git commit-tree "$@" |
| 55 | fi |
| 56 | } |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 57 | # override die(): this version puts in an extra line break, so that |
| 58 | # the progress is still visible |
| 59 | |
| 60 | die() |
| 61 | { |
| 62 | echo >&2 |
| 63 | echo "$*" >&2 |
| 64 | exit 1 |
| 65 | } |
Johannes Schindelin | 16ed34a | 2007-11-28 15:50:38 +0000 | [diff] [blame] | 66 | EOF |
| 67 | ) |
| 68 | |
| 69 | eval "$functions" |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 70 | |
Jeff King | 3c730fa | 2012-10-18 06:33:02 -0400 | [diff] [blame] | 71 | finish_ident() { |
| 72 | # Ensure non-empty id name. |
| 73 | echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac" |
| 74 | # And make sure everything is exported. |
| 75 | echo "export GIT_$1_NAME" |
| 76 | echo "export GIT_$1_EMAIL" |
| 77 | echo "export GIT_$1_DATE" |
| 78 | } |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 79 | |
| 80 | set_ident () { |
Jeff King | 3c730fa | 2012-10-18 06:33:02 -0400 | [diff] [blame] | 81 | parse_ident_from_commit author AUTHOR committer COMMITTER |
| 82 | finish_ident AUTHOR |
| 83 | finish_ident COMMITTER |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Elijah Newren | 9df53c5 | 2019-09-04 15:32:38 -0700 | [diff] [blame] | 86 | if test -z "$FILTER_BRANCH_SQUELCH_WARNING$GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS" |
| 87 | then |
| 88 | cat <<EOF |
| 89 | WARNING: git-filter-branch has a glut of gotchas generating mangled history |
| 90 | rewrites. Hit Ctrl-C before proceeding to abort, then use an |
| 91 | alternative filtering tool such as 'git filter-repo' |
| 92 | (https://github.com/newren/git-filter-repo/) instead. See the |
| 93 | filter-branch manual page for more details; to squelch this warning, |
| 94 | set FILTER_BRANCH_SQUELCH_WARNING=1. |
| 95 | EOF |
| 96 | sleep 10 |
| 97 | printf "Proceeding with filter-branch...\n\n" |
| 98 | fi |
| 99 | |
Junio C Hamano | e336afd | 2017-10-19 14:45:45 +0900 | [diff] [blame] | 100 | USAGE="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>] |
Andreas Heiduk | 3b117f7 | 2017-06-10 10:54:44 +0200 | [diff] [blame] | 101 | [--tree-filter <command>] [--index-filter <command>] |
| 102 | [--parent-filter <command>] [--msg-filter <command>] |
| 103 | [--commit-filter <command>] [--tag-name-filter <command>] |
Junio C Hamano | e336afd | 2017-10-19 14:45:45 +0900 | [diff] [blame] | 104 | [--original <namespace>] |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 105 | [-d <directory>] [-f | --force] [--state-branch <branch>] |
Andreas Heiduk | d612975 | 2017-06-10 10:54:45 +0200 | [diff] [blame] | 106 | [--] [<rev-list options>...]" |
Johannes Schindelin | 7e0f170 | 2007-08-31 20:05:36 +0100 | [diff] [blame] | 107 | |
Junio C Hamano | 8f321a3 | 2007-11-06 01:50:02 -0800 | [diff] [blame] | 108 | OPTIONS_SPEC= |
Johannes Schindelin | 7e0f170 | 2007-08-31 20:05:36 +0100 | [diff] [blame] | 109 | . git-sh-setup |
| 110 | |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 111 | if [ "$(is_bare_repository)" = false ]; then |
Jeff King | 5347a50 | 2011-09-01 17:53:07 -0400 | [diff] [blame] | 112 | require_clean_work_tree 'rewrite branches' |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 113 | fi |
Johannes Schindelin | 46eb449 | 2007-10-17 03:23:10 +0100 | [diff] [blame] | 114 | |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 115 | tempdir=.git-rewrite |
Andreas Heiduk | 3b117f7 | 2017-06-10 10:54:44 +0200 | [diff] [blame] | 116 | filter_setup= |
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= |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 125 | state_branch= |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 126 | orig_namespace=refs/original/ |
| 127 | force= |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 128 | prune_empty= |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 129 | remap_to_ancestor= |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 130 | while : |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 131 | do |
| 132 | case "$1" in |
| 133 | --) |
| 134 | shift |
| 135 | break |
| 136 | ;; |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 137 | --force|-f) |
| 138 | shift |
| 139 | force=t |
| 140 | continue |
| 141 | ;; |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 142 | --remap-to-ancestor) |
Csaba Henk | 7ec344d | 2010-08-27 20:44:56 +0000 | [diff] [blame] | 143 | # deprecated ($remap_to_ancestor is set now automatically) |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 144 | shift |
| 145 | remap_to_ancestor=t |
| 146 | continue |
| 147 | ;; |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 148 | --prune-empty) |
| 149 | shift |
| 150 | prune_empty=t |
| 151 | continue |
| 152 | ;; |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 153 | -*) |
| 154 | ;; |
| 155 | *) |
| 156 | break; |
| 157 | esac |
| 158 | |
| 159 | # all switches take one argument |
| 160 | ARG="$1" |
| 161 | case "$#" in 1) usage ;; esac |
| 162 | shift |
| 163 | OPTARG="$1" |
| 164 | shift |
| 165 | |
| 166 | case "$ARG" in |
| 167 | -d) |
| 168 | tempdir="$OPTARG" |
| 169 | ;; |
Andreas Heiduk | 3b117f7 | 2017-06-10 10:54:44 +0200 | [diff] [blame] | 170 | --setup) |
| 171 | filter_setup="$OPTARG" |
| 172 | ;; |
David Glasser | 07c4984 | 2017-10-17 09:45:15 +0000 | [diff] [blame] | 173 | --subdirectory-filter) |
| 174 | filter_subdir="$OPTARG" |
| 175 | remap_to_ancestor=t |
| 176 | ;; |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 177 | --env-filter) |
| 178 | filter_env="$OPTARG" |
| 179 | ;; |
| 180 | --tree-filter) |
| 181 | filter_tree="$OPTARG" |
| 182 | ;; |
| 183 | --index-filter) |
| 184 | filter_index="$OPTARG" |
| 185 | ;; |
| 186 | --parent-filter) |
| 187 | filter_parent="$OPTARG" |
| 188 | ;; |
| 189 | --msg-filter) |
| 190 | filter_msg="$OPTARG" |
| 191 | ;; |
| 192 | --commit-filter) |
Johannes Schindelin | 16ed34a | 2007-11-28 15:50:38 +0000 | [diff] [blame] | 193 | filter_commit="$functions; $OPTARG" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 194 | ;; |
| 195 | --tag-name-filter) |
| 196 | filter_tag_name="$OPTARG" |
| 197 | ;; |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 198 | --original) |
Junio C Hamano | 55ced83 | 2007-08-30 19:17:42 -0700 | [diff] [blame] | 199 | orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/ |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 200 | ;; |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 201 | --state-branch) |
| 202 | state_branch="$OPTARG" |
| 203 | ;; |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 204 | *) |
| 205 | usage |
| 206 | ;; |
| 207 | esac |
| 208 | done |
| 209 | |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 210 | case "$prune_empty,$filter_commit" in |
| 211 | ,) |
| 212 | filter_commit='git commit-tree "$@"';; |
| 213 | t,) |
| 214 | filter_commit="$functions;"' git_commit_non_empty_tree "$@"';; |
| 215 | ,*) |
| 216 | ;; |
| 217 | *) |
Jacob Helwig | 5da8171 | 2010-02-11 18:46:22 -0800 | [diff] [blame] | 218 | die "Cannot set --prune-empty and --commit-filter at the same time" |
Pierre Habouzit | d3240d9 | 2008-10-31 10:12:21 +0100 | [diff] [blame] | 219 | esac |
| 220 | |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 221 | case "$force" in |
| 222 | t) |
| 223 | rm -rf "$tempdir" |
| 224 | ;; |
| 225 | '') |
| 226 | test -d "$tempdir" && |
| 227 | die "$tempdir already exists, please remove it" |
| 228 | esac |
Jeff King | 9727601 | 2013-04-02 10:22:19 -0400 | [diff] [blame] | 229 | orig_dir=$(pwd) |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 230 | mkdir -p "$tempdir/t" && |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 231 | tempdir="$(cd "$tempdir"; pwd)" && |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 232 | cd "$tempdir/t" && |
| 233 | workdir="$(pwd)" || |
| 234 | die "" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 235 | |
Brandon Casey | def16e7 | 2008-01-28 15:16:02 -0600 | [diff] [blame] | 236 | # Remove tempdir on exit |
Jeff King | 9727601 | 2013-04-02 10:22:19 -0400 | [diff] [blame] | 237 | trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0 |
Brandon Casey | def16e7 | 2008-01-28 15:16:02 -0600 | [diff] [blame] | 238 | |
Lars Noschinski | 88e3880 | 2009-02-18 09:35:36 +0100 | [diff] [blame] | 239 | ORIG_GIT_DIR="$GIT_DIR" |
| 240 | ORIG_GIT_WORK_TREE="$GIT_WORK_TREE" |
| 241 | ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE" |
Ian Campbell | 7b1378b | 2017-09-21 08:49:30 +0100 | [diff] [blame] | 242 | ORIG_GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" |
| 243 | ORIG_GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" |
| 244 | ORIG_GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" |
| 245 | ORIG_GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME" |
| 246 | ORIG_GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL" |
| 247 | ORIG_GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE" |
| 248 | |
Lars Noschinski | 88e3880 | 2009-02-18 09:35:36 +0100 | [diff] [blame] | 249 | GIT_WORK_TREE=. |
| 250 | export GIT_DIR GIT_WORK_TREE |
| 251 | |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 252 | # Make sure refs/original is empty |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 253 | git for-each-ref > "$tempdir"/backup-refs || exit |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 254 | while read sha1 type name |
| 255 | do |
| 256 | case "$force,$name" in |
| 257 | ,$orig_namespace*) |
John Tapsell | 734cd57 | 2009-02-19 07:36:35 +0000 | [diff] [blame] | 258 | die "Cannot create a new backup. |
| 259 | A previous backup already exists in $orig_namespace |
| 260 | Force overwriting the backup with -f" |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 261 | ;; |
| 262 | t,$orig_namespace*) |
| 263 | git update-ref -d "$name" $sha1 |
| 264 | ;; |
| 265 | esac |
| 266 | done < "$tempdir"/backup-refs |
| 267 | |
Junio C Hamano | 418fa3a | 2008-01-05 12:18:43 -0800 | [diff] [blame] | 268 | # The refs should be updated if their heads were rewritten |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 269 | git rev-parse --no-flags --revs-only --symbolic-full-name \ |
Yuki Kokubun | f78ab35 | 2018-03-25 16:54:58 +0000 | [diff] [blame] | 270 | --default HEAD "$@" > "$tempdir"/raw-refs || exit |
| 271 | while read ref |
| 272 | do |
| 273 | case "$ref" in ^?*) continue ;; esac |
| 274 | |
| 275 | if git rev-parse --verify "$ref"^0 >/dev/null 2>&1 |
| 276 | then |
| 277 | echo "$ref" |
| 278 | else |
| 279 | warn "WARNING: not rewriting '$ref' (not a committish)" |
| 280 | fi |
| 281 | done >"$tempdir"/heads <"$tempdir"/raw-refs |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 282 | |
| 283 | test -s "$tempdir"/heads || |
Jean-Noel Avila | 6963893 | 2017-05-11 14:06:34 +0200 | [diff] [blame] | 284 | die "You must specify a ref to rewrite." |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 285 | |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 286 | GIT_INDEX_FILE="$(pwd)/../index" |
| 287 | export GIT_INDEX_FILE |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 288 | |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 289 | # map old->new commit ids for rewriting parents |
| 290 | mkdir ../map || die "Could not create map/ directory" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 291 | |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 292 | if test -n "$state_branch" |
| 293 | then |
| 294 | state_commit=$(git rev-parse --no-flags --revs-only "$state_branch") |
| 295 | if test -n "$state_commit" |
| 296 | then |
| 297 | echo "Populating map from $state_branch ($state_commit)" 1>&2 |
| 298 | perl -e'open(MAP, "-|", "git show $ARGV[0]:filter.map") or die; |
| 299 | while (<MAP>) { |
| 300 | m/(.*):(.*)/ or die; |
| 301 | open F, ">../map/$1" or die; |
| 302 | print F "$2" or die; |
| 303 | close(F) or die; |
| 304 | } |
| 305 | close(MAP) or die;' "$state_commit" \ |
| 306 | || die "Unable to load state from $state_branch:filter.map" |
| 307 | else |
| 308 | echo "Branch $state_branch does not exist. Will create" 1>&2 |
| 309 | fi |
| 310 | fi |
| 311 | |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 312 | # we need "--" only if there are no path arguments in $@ |
| 313 | nonrevs=$(git rev-parse --no-revs "$@") || exit |
Csaba Henk | 7ec344d | 2010-08-27 20:44:56 +0000 | [diff] [blame] | 314 | if test -z "$nonrevs" |
| 315 | then |
| 316 | dashdash=-- |
| 317 | else |
| 318 | dashdash= |
| 319 | remap_to_ancestor=t |
| 320 | fi |
| 321 | |
Lee Carver | 3361a54 | 2013-09-10 22:55:35 +0000 | [diff] [blame] | 322 | git rev-parse --revs-only "$@" >../parse |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 323 | |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 324 | case "$filter_subdir" in |
| 325 | "") |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 326 | eval set -- "$(git rev-parse --sq --no-revs "$@")" |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 327 | ;; |
| 328 | *) |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 329 | eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \ |
| 330 | "$filter_subdir")" |
| 331 | ;; |
| 332 | esac |
| 333 | |
| 334 | git rev-list --reverse --topo-order --default HEAD \ |
Lee Carver | 3361a54 | 2013-09-10 22:55:35 +0000 | [diff] [blame] | 335 | --parents --simplify-merges --stdin "$@" <../parse >../revs || |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 336 | die "Could not get the commits" |
Josh Triplett | 9d6f220 | 2007-07-14 01:05:43 -0700 | [diff] [blame] | 337 | commits=$(wc -l <../revs | tr -d " ") |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 338 | |
Michele Locati | 0a0eb2e | 2018-03-15 18:09:18 +0100 | [diff] [blame] | 339 | test $commits -eq 0 && die_with_status 2 "Found nothing to rewrite" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 340 | |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 341 | # Rewrite the commits |
Gabor Bernat | 6a9d16a | 2015-09-07 15:52:08 +0200 | [diff] [blame] | 342 | report_progress () |
| 343 | { |
| 344 | if test -n "$progress" && |
| 345 | test $git_filter_branch__commit_count -gt $next_sample_at |
| 346 | then |
Junio C Hamano | 71400d9 | 2015-09-21 15:16:20 -0700 | [diff] [blame] | 347 | count=$git_filter_branch__commit_count |
| 348 | |
| 349 | now=$(date +%s) |
| 350 | elapsed=$(($now - $start_timestamp)) |
| 351 | remaining=$(( ($commits - $count) * $elapsed / $count )) |
| 352 | if test $elapsed -gt 0 |
Gabor Bernat | 6a9d16a | 2015-09-07 15:52:08 +0200 | [diff] [blame] | 353 | then |
Junio C Hamano | 71400d9 | 2015-09-21 15:16:20 -0700 | [diff] [blame] | 354 | next_sample_at=$(( ($elapsed + 1) * $count / $elapsed )) |
Gabor Bernat | 6a9d16a | 2015-09-07 15:52:08 +0200 | [diff] [blame] | 355 | else |
| 356 | next_sample_at=$(($next_sample_at + 1)) |
| 357 | fi |
Junio C Hamano | 71400d9 | 2015-09-21 15:16:20 -0700 | [diff] [blame] | 358 | progress=" ($elapsed seconds passed, remaining $remaining predicted)" |
Gabor Bernat | 6a9d16a | 2015-09-07 15:52:08 +0200 | [diff] [blame] | 359 | fi |
Junio C Hamano | 71400d9 | 2015-09-21 15:16:20 -0700 | [diff] [blame] | 360 | printf "\rRewrite $commit ($count/$commits)$progress " |
Gabor Bernat | 6a9d16a | 2015-09-07 15:52:08 +0200 | [diff] [blame] | 361 | } |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 362 | |
Elijah Newren | d5b0c97 | 2009-03-25 15:51:01 -0600 | [diff] [blame] | 363 | git_filter_branch__commit_count=0 |
Gabor Bernat | 6a9d16a | 2015-09-07 15:52:08 +0200 | [diff] [blame] | 364 | |
| 365 | progress= start_timestamp= |
| 366 | if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$' |
| 367 | then |
| 368 | next_sample_at=0 |
| 369 | progress="dummy to ensure this is not empty" |
| 370 | start_timestamp=$(date '+%s') |
| 371 | fi |
| 372 | |
Jeff King | 348d4f2 | 2015-11-06 01:24:29 -0500 | [diff] [blame] | 373 | if test -n "$filter_index" || |
| 374 | test -n "$filter_tree" || |
| 375 | test -n "$filter_subdir" |
| 376 | then |
| 377 | need_index=t |
| 378 | else |
| 379 | need_index= |
| 380 | fi |
| 381 | |
Andreas Heiduk | 3b117f7 | 2017-06-10 10:54:44 +0200 | [diff] [blame] | 382 | eval "$filter_setup" < /dev/null || |
| 383 | die "filter setup failed: $filter_setup" |
| 384 | |
Johannes Sixt | 813b473 | 2007-06-08 23:28:39 +0200 | [diff] [blame] | 385 | while read commit parents; do |
Elijah Newren | d5b0c97 | 2009-03-25 15:51:01 -0600 | [diff] [blame] | 386 | git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1)) |
Gabor Bernat | 6a9d16a | 2015-09-07 15:52:08 +0200 | [diff] [blame] | 387 | |
| 388 | report_progress |
Michael Barabanov | 709cfe8 | 2018-06-25 21:07:33 -0700 | [diff] [blame] | 389 | test -f "$workdir"/../map/$commit && continue |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 390 | |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 391 | case "$filter_subdir" in |
| 392 | "") |
Jeff King | 348d4f2 | 2015-11-06 01:24:29 -0500 | [diff] [blame] | 393 | if test -n "$need_index" |
| 394 | then |
| 395 | GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit |
| 396 | fi |
Johannes Schindelin | 685ef54 | 2007-06-08 01:30:35 +0100 | [diff] [blame] | 397 | ;; |
| 398 | *) |
Junio C Hamano | 5b044ac | 2008-03-08 12:25:58 -0800 | [diff] [blame] | 399 | # The commit may not have the subdirectory at all |
Jeff King | 83bd743 | 2013-08-27 16:41:12 -0400 | [diff] [blame] | 400 | err=$(GIT_ALLOW_NULL_SHA1=1 \ |
| 401 | git read-tree -i -m $commit:"$filter_subdir" 2>&1) || { |
Miklos Vajna | d69da76 | 2008-12-03 14:26:48 +0100 | [diff] [blame] | 402 | if ! git rev-parse -q --verify $commit:"$filter_subdir" |
Junio C Hamano | 5b044ac | 2008-03-08 12:25:58 -0800 | [diff] [blame] | 403 | then |
| 404 | rm -f "$GIT_INDEX_FILE" |
| 405 | else |
| 406 | echo >&2 "$err" |
| 407 | false |
| 408 | fi |
| 409 | } |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 410 | esac || die "Could not initialize the index" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 411 | |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 412 | GIT_COMMIT=$commit |
| 413 | export GIT_COMMIT |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 414 | git cat-file commit "$commit" >../commit || |
| 415 | die "Cannot read commit $commit" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 416 | |
Jeff King | 3c730fa | 2012-10-18 06:33:02 -0400 | [diff] [blame] | 417 | eval "$(set_ident <../commit)" || |
| 418 | die "setting author/committer failed for commit $commit" |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 419 | eval "$filter_env" < /dev/null || |
| 420 | die "env filter failed: $filter_env" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 421 | |
| 422 | if [ "$filter_tree" ]; then |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 423 | git checkout-index -f -u -a || |
| 424 | die "Could not checkout the index" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 425 | # files that $commit removed are now still in the working tree; |
| 426 | # remove them, else they would be added again |
veillette@yahoo.ca | 6a589fd | 2008-03-31 09:14:15 +0200 | [diff] [blame] | 427 | git clean -d -q -f -x |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 428 | eval "$filter_tree" < /dev/null || |
| 429 | die "tree filter failed: $filter_tree" |
| 430 | |
Junio C Hamano | 1fe32cb | 2008-02-15 23:57:26 -0800 | [diff] [blame] | 431 | ( |
SZEDER Gábor | 4d2a364 | 2015-11-23 13:23:16 +0100 | [diff] [blame] | 432 | git diff-index -r --name-only --ignore-submodules $commit -- && |
Junio C Hamano | 1fe32cb | 2008-02-15 23:57:26 -0800 | [diff] [blame] | 433 | git ls-files --others |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 434 | ) > "$tempdir"/tree-state || exit |
| 435 | git update-index --add --replace --remove --stdin \ |
| 436 | < "$tempdir"/tree-state || exit |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 437 | fi |
| 438 | |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 439 | eval "$filter_index" < /dev/null || |
| 440 | die "index filter failed: $filter_index" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 441 | |
| 442 | parentstr= |
Johannes Sixt | 813b473 | 2007-06-08 23:28:39 +0200 | [diff] [blame] | 443 | for parent in $parents; do |
Johannes Sixt | 3520e1e | 2007-06-06 20:38:35 +0200 | [diff] [blame] | 444 | for reparent in $(map "$parent"); do |
Charles Bailey | 79bc4ef | 2014-06-30 22:20:27 +0100 | [diff] [blame] | 445 | case "$parentstr " in |
| 446 | *" -p $reparent "*) |
| 447 | ;; |
| 448 | *) |
| 449 | parentstr="$parentstr -p $reparent" |
| 450 | ;; |
| 451 | esac |
Johannes Sixt | 3520e1e | 2007-06-06 20:38:35 +0200 | [diff] [blame] | 452 | done |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 453 | done |
| 454 | if [ "$filter_parent" ]; then |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 455 | parentstr="$(echo "$parentstr" | eval "$filter_parent")" || |
| 456 | die "parent filter failed: $filter_parent" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 457 | fi |
| 458 | |
Jeff King | df06201 | 2015-04-29 11:48:58 -0400 | [diff] [blame] | 459 | { |
James McCoy | a5a4b3f | 2015-10-08 20:21:13 -0400 | [diff] [blame] | 460 | while IFS='' read -r header_line && test -n "$header_line" |
Jeff King | df06201 | 2015-04-29 11:48:58 -0400 | [diff] [blame] | 461 | do |
| 462 | # skip header lines... |
| 463 | :; |
| 464 | done |
| 465 | # and output the actual commit message |
| 466 | cat |
| 467 | } <../commit | |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 468 | eval "$filter_msg" > ../message || |
| 469 | die "msg filter failed: $filter_msg" |
Jeff King | 348d4f2 | 2015-11-06 01:24:29 -0500 | [diff] [blame] | 470 | |
| 471 | if test -n "$need_index" |
| 472 | then |
| 473 | tree=$(git write-tree) |
| 474 | else |
Jeff King | 1dc413e | 2016-01-19 17:07:22 -0500 | [diff] [blame] | 475 | tree=$(git rev-parse "$commit^{tree}") |
Jeff King | 348d4f2 | 2015-11-06 01:24:29 -0500 | [diff] [blame] | 476 | fi |
Michael Witten | 0906f6e | 2011-08-07 02:44:43 +0000 | [diff] [blame] | 477 | workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \ |
Jeff King | 348d4f2 | 2015-11-06 01:24:29 -0500 | [diff] [blame] | 478 | "$tree" $parentstr < ../message > ../map/$commit || |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 479 | die "could not write rewritten commit" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 480 | done <../revs |
| 481 | |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 482 | # If we are filtering for paths, as in the case of a subdirectory |
| 483 | # filter, it is possible that a specified head is not in the set of |
| 484 | # rewritten commits, because it was pruned by the revision walker. |
| 485 | # Ancestor remapping fixes this by mapping these heads to the unique |
| 486 | # nearest ancestor that survived the pruning. |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 487 | |
Thomas Rast | f2f3a6b | 2009-11-10 22:04:51 +0100 | [diff] [blame] | 488 | if test "$remap_to_ancestor" = t |
Thomas Rast | a0e4639 | 2008-08-12 10:45:58 +0200 | [diff] [blame] | 489 | then |
| 490 | while read ref |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 491 | do |
Thomas Rast | a0e4639 | 2008-08-12 10:45:58 +0200 | [diff] [blame] | 492 | sha1=$(git rev-parse "$ref"^0) |
| 493 | test -f "$workdir"/../map/$sha1 && continue |
Thomas Rast | 2c1d2d8 | 2009-11-11 09:53:46 +0100 | [diff] [blame] | 494 | ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@") |
Jeff King | 98fe9e6 | 2021-03-10 12:07:27 -0500 | [diff] [blame] | 495 | test "$ancestor" && echo $(map $ancestor) >"$workdir"/../map/$sha1 |
Thomas Rast | a0e4639 | 2008-08-12 10:45:58 +0200 | [diff] [blame] | 496 | done < "$tempdir"/heads |
| 497 | fi |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 498 | |
| 499 | # Finally update the refs |
| 500 | |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 501 | echo |
| 502 | while read ref |
| 503 | do |
| 504 | # avoid rewriting a ref twice |
| 505 | test -f "$orig_namespace$ref" && continue |
| 506 | |
| 507 | sha1=$(git rev-parse "$ref"^0) |
| 508 | rewritten=$(map $sha1) |
| 509 | |
| 510 | test $sha1 = "$rewritten" && |
| 511 | warn "WARNING: Ref '$ref' is unchanged" && |
| 512 | continue |
| 513 | |
| 514 | case "$rewritten" in |
| 515 | '') |
| 516 | echo "Ref '$ref' was deleted" |
| 517 | git update-ref -m "filter-branch: delete" -d "$ref" $sha1 || |
| 518 | die "Could not delete $ref" |
Johannes Schindelin | 9840906 | 2007-06-05 16:58:13 +0100 | [diff] [blame] | 519 | ;; |
Jeff King | 42efa12 | 2021-03-10 12:07:37 -0500 | [diff] [blame] | 520 | *) |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 521 | echo "Ref '$ref' was rewritten" |
Thomas Rast | 261044e | 2008-08-08 01:50:31 +0200 | [diff] [blame] | 522 | if ! git update-ref -m "filter-branch: rewrite" \ |
| 523 | "$ref" $rewritten $sha1 2>/dev/null; then |
| 524 | if test $(git cat-file -t "$ref") = tag; then |
| 525 | if test -z "$filter_tag_name"; then |
| 526 | warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag." |
| 527 | warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag." |
| 528 | fi |
| 529 | else |
| 530 | die "Could not rewrite $ref" |
| 531 | fi |
| 532 | fi |
Johannes Schindelin | 9840906 | 2007-06-05 16:58:13 +0100 | [diff] [blame] | 533 | ;; |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 534 | esac |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 535 | git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 || |
| 536 | exit |
Johannes Schindelin | dfd05e3 | 2007-07-23 18:34:13 +0100 | [diff] [blame] | 537 | done < "$tempdir"/heads |
| 538 | |
| 539 | # TODO: This should possibly go, with the semantics that all positive given |
| 540 | # refs are updated, and their original heads stored in refs/original/ |
| 541 | # Filter tags |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 542 | |
| 543 | if [ "$filter_tag_name" ]; then |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 544 | git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags | |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 545 | while read sha1 type ref; do |
| 546 | ref="${ref#refs/tags/}" |
| 547 | # XXX: Rewrite tagged trees as well? |
| 548 | if [ "$type" != "commit" -a "$type" != "tag" ]; then |
| 549 | continue; |
| 550 | fi |
| 551 | |
| 552 | if [ "$type" = "tag" ]; then |
| 553 | # Dereference to a commit |
| 554 | sha1t="$sha1" |
Dan Loewenherz | 7bd93c1 | 2009-04-22 21:46:02 -0400 | [diff] [blame] | 555 | sha1="$(git rev-parse -q "$sha1"^{commit})" || continue |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 556 | fi |
| 557 | |
| 558 | [ -f "../map/$sha1" ] || continue |
| 559 | new_sha1="$(cat "../map/$sha1")" |
Johannes Schindelin | 3addc94 | 2007-11-28 15:56:11 +0000 | [diff] [blame] | 560 | GIT_COMMIT="$sha1" |
| 561 | export GIT_COMMIT |
Johannes Schindelin | 8c1ce0f | 2007-07-04 15:36:01 +0100 | [diff] [blame] | 562 | new_ref="$(echo "$ref" | eval "$filter_tag_name")" || |
| 563 | die "tag name filter failed: $filter_tag_name" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 564 | |
| 565 | echo "$ref -> $new_ref ($sha1 -> $new_sha1)" |
| 566 | |
| 567 | if [ "$type" = "tag" ]; then |
Johannes Sixt | a9da166 | 2008-08-21 16:45:11 +0200 | [diff] [blame] | 568 | new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \ |
| 569 | "$new_sha1" "$new_ref" |
| 570 | git cat-file tag "$ref" | |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 571 | sed -n \ |
Stephen Boyd | 9524cf2 | 2010-01-26 15:08:31 -0800 | [diff] [blame] | 572 | -e '1,/^$/{ |
Johannes Sixt | a9da166 | 2008-08-21 16:45:11 +0200 | [diff] [blame] | 573 | /^object /d |
| 574 | /^type /d |
| 575 | /^tag /d |
Stephen Boyd | 9524cf2 | 2010-01-26 15:08:31 -0800 | [diff] [blame] | 576 | }' \ |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 577 | -e '/^-----BEGIN PGP SIGNATURE-----/q' \ |
Johannes Sixt | a9da166 | 2008-08-21 16:45:11 +0200 | [diff] [blame] | 578 | -e 'p' ) | |
Ian Campbell | b2c1ca6 | 2017-09-21 08:49:32 +0100 | [diff] [blame] | 579 | git hash-object -t tag -w --stdin) || |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 580 | die "Could not create new tag object for $ref" |
| 581 | if git cat-file tag "$ref" | \ |
Ævar Arnfjörð Bjarmason | ebeb39f | 2021-10-21 21:58:00 +0200 | [diff] [blame] | 582 | grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1 |
Brandon Casey | 1bf6551 | 2008-03-26 10:47:09 -0500 | [diff] [blame] | 583 | then |
| 584 | warn "gpg signature stripped from tag object $sha1t" |
| 585 | fi |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 586 | fi |
| 587 | |
Johannes Schindelin | af580e9 | 2007-07-18 14:17:43 +0100 | [diff] [blame] | 588 | git update-ref "refs/tags/$new_ref" "$new_sha1" || |
| 589 | die "Could not write tag $new_ref" |
Johannes Schindelin | 6f6826c | 2007-06-03 01:31:28 +0100 | [diff] [blame] | 590 | done |
| 591 | fi |
| 592 | |
Eric Kidd | 9273b56 | 2009-02-03 13:27:03 -0500 | [diff] [blame] | 593 | unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE |
Ian Campbell | 7b1378b | 2017-09-21 08:49:30 +0100 | [diff] [blame] | 594 | unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE |
| 595 | unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE |
Eric Kidd | 9273b56 | 2009-02-03 13:27:03 -0500 | [diff] [blame] | 596 | test -z "$ORIG_GIT_DIR" || { |
| 597 | GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR |
| 598 | } |
| 599 | test -z "$ORIG_GIT_WORK_TREE" || { |
| 600 | GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" && |
| 601 | export GIT_WORK_TREE |
| 602 | } |
| 603 | test -z "$ORIG_GIT_INDEX_FILE" || { |
| 604 | GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" && |
| 605 | export GIT_INDEX_FILE |
| 606 | } |
Ian Campbell | 7b1378b | 2017-09-21 08:49:30 +0100 | [diff] [blame] | 607 | test -z "$ORIG_GIT_AUTHOR_NAME" || { |
| 608 | GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" && |
| 609 | export GIT_AUTHOR_NAME |
| 610 | } |
| 611 | test -z "$ORIG_GIT_AUTHOR_EMAIL" || { |
| 612 | GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" && |
| 613 | export GIT_AUTHOR_EMAIL |
| 614 | } |
| 615 | test -z "$ORIG_GIT_AUTHOR_DATE" || { |
| 616 | GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" && |
| 617 | export GIT_AUTHOR_DATE |
| 618 | } |
| 619 | test -z "$ORIG_GIT_COMMITTER_NAME" || { |
| 620 | GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" && |
| 621 | export GIT_COMMITTER_NAME |
| 622 | } |
| 623 | test -z "$ORIG_GIT_COMMITTER_EMAIL" || { |
| 624 | GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" && |
| 625 | export GIT_COMMITTER_EMAIL |
| 626 | } |
| 627 | test -z "$ORIG_GIT_COMMITTER_DATE" || { |
| 628 | GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" && |
| 629 | export GIT_COMMITTER_DATE |
| 630 | } |
Eric Kidd | 9273b56 | 2009-02-03 13:27:03 -0500 | [diff] [blame] | 631 | |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 632 | if test -n "$state_branch" |
| 633 | then |
| 634 | echo "Saving rewrite state to $state_branch" 1>&2 |
| 635 | state_blob=$( |
| 636 | perl -e'opendir D, "../map" or die; |
| 637 | open H, "|-", "git hash-object -w --stdin" or die; |
| 638 | foreach (sort readdir(D)) { |
| 639 | next if m/^\.\.?$/; |
| 640 | open F, "<../map/$_" or die; |
| 641 | chomp($f = <F>); |
| 642 | print H "$_:$f\n" or die; |
| 643 | } |
| 644 | close(H) or die;' || die "Unable to save state") |
Michele Locati | 206a6ae | 2018-03-19 16:52:59 +0100 | [diff] [blame] | 645 | state_tree=$(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree) |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 646 | if test -n "$state_commit" |
| 647 | then |
Michele Locati | 206a6ae | 2018-03-19 16:52:59 +0100 | [diff] [blame] | 648 | state_commit=$(echo "Sync" | git commit-tree "$state_tree" -p "$state_commit") |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 649 | else |
Michele Locati | 206a6ae | 2018-03-19 16:52:59 +0100 | [diff] [blame] | 650 | state_commit=$(echo "Sync" | git commit-tree "$state_tree" ) |
Ian Campbell | bd2c79f | 2017-09-21 08:49:31 +0100 | [diff] [blame] | 651 | fi |
| 652 | git update-ref "$state_branch" "$state_commit" |
| 653 | fi |
| 654 | |
Ian Campbell | d24813c | 2017-09-21 08:49:29 +0100 | [diff] [blame] | 655 | cd "$orig_dir" |
| 656 | rm -rf "$tempdir" |
| 657 | |
| 658 | trap - 0 |
| 659 | |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 660 | if [ "$(is_bare_repository)" = false ]; then |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 661 | git read-tree -u -m HEAD || exit |
Petr Baudis | a4661b0 | 2008-07-24 00:15:36 +0200 | [diff] [blame] | 662 | fi |
Johannes Schindelin | 46eb449 | 2007-10-17 03:23:10 +0100 | [diff] [blame] | 663 | |
Eric Kidd | 0ea29cc | 2009-02-11 16:10:41 -0500 | [diff] [blame] | 664 | exit 0 |