blob: fea79646172184c630508908dd6eb61d3470074d [file] [log] [blame]
Johannes Schindelin6f6826c2007-06-03 01:31:28 +01001#!/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 Schindelinc401b332007-07-04 00:41:55 +01007# 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 Schindelin6f6826c2007-06-03 01:31:28 +010010
Johannes Schindelin16ed34a2007-11-28 15:50:38 +000011# The following functions will also be available in the commit filter:
12
13functions=$(cat << \EOF
brian m. carlson03a7f382018-05-02 00:26:08 +000014EMPTY_TREE=$(git hash-object -t tree /dev/null)
15
Steffen Prohaskab5669a02007-07-04 10:36:24 +020016warn () {
Jon Seymour285c6cb2011-08-05 23:31:29 +100017 echo "$*" >&2
Steffen Prohaskab5669a02007-07-04 10:36:24 +020018}
19
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010020map()
21{
Johannes Sixt3520e1e2007-06-06 20:38:35 +020022 # if it was not rewritten, take the original
Johannes Sixtc57a3492007-07-04 14:08:17 +020023 if test -r "$workdir/../map/$1"
24 then
25 cat "$workdir/../map/$1"
26 else
27 echo "$1"
28 fi
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010029}
30
Johannes Schindelinf95eef12007-08-31 20:06:27 +010031# if you run 'skip_commit "$@"' in a commit filter, it will print
32# the (mapped) parents, effectively skipping the commit.
33
34skip_commit()
35{
36 shift;
37 while [ -n "$1" ];
38 do
39 shift;
40 map "$1";
41 shift;
42 done;
43}
44
Pierre Habouzitd3240d92008-10-31 10:12:21 +010045# 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.
47git_commit_non_empty_tree()
48{
49 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
50 map "$3"
brian m. carlson03a7f382018-05-02 00:26:08 +000051 elif test $# = 1 && test "$1" = $EMPTY_TREE; then
Devin J. Pohlya582a822017-02-23 02:27:35 -060052 :
Pierre Habouzitd3240d92008-10-31 10:12:21 +010053 else
54 git commit-tree "$@"
55 fi
56}
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +010057# override die(): this version puts in an extra line break, so that
58# the progress is still visible
59
60die()
61{
62 echo >&2
63 echo "$*" >&2
64 exit 1
65}
Johannes Schindelin16ed34a2007-11-28 15:50:38 +000066EOF
67)
68
69eval "$functions"
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +010070
Jeff King3c730fa2012-10-18 06:33:02 -040071finish_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 Schindelin6f6826c2007-06-03 01:31:28 +010079
80set_ident () {
Jeff King3c730fa2012-10-18 06:33:02 -040081 parse_ident_from_commit author AUTHOR committer COMMITTER
82 finish_ident AUTHOR
83 finish_ident COMMITTER
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010084}
85
Elijah Newren9df53c52019-09-04 15:32:38 -070086if test -z "$FILTER_BRANCH_SQUELCH_WARNING$GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS"
87then
88 cat <<EOF
89WARNING: 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.
95EOF
96 sleep 10
97 printf "Proceeding with filter-branch...\n\n"
98fi
99
Junio C Hamanoe336afd2017-10-19 14:45:45 +0900100USAGE="[--setup <command>] [--subdirectory-filter <directory>] [--env-filter <command>]
Andreas Heiduk3b117f72017-06-10 10:54:44 +0200101 [--tree-filter <command>] [--index-filter <command>]
102 [--parent-filter <command>] [--msg-filter <command>]
103 [--commit-filter <command>] [--tag-name-filter <command>]
Junio C Hamanoe336afd2017-10-19 14:45:45 +0900104 [--original <namespace>]
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100105 [-d <directory>] [-f | --force] [--state-branch <branch>]
Andreas Heidukd6129752017-06-10 10:54:45 +0200106 [--] [<rev-list options>...]"
Johannes Schindelin7e0f1702007-08-31 20:05:36 +0100107
Junio C Hamano8f321a32007-11-06 01:50:02 -0800108OPTIONS_SPEC=
Johannes Schindelin7e0f1702007-08-31 20:05:36 +0100109. git-sh-setup
110
Petr Baudisa4661b02008-07-24 00:15:36 +0200111if [ "$(is_bare_repository)" = false ]; then
Jeff King5347a502011-09-01 17:53:07 -0400112 require_clean_work_tree 'rewrite branches'
Petr Baudisa4661b02008-07-24 00:15:36 +0200113fi
Johannes Schindelin46eb4492007-10-17 03:23:10 +0100114
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100115tempdir=.git-rewrite
Andreas Heiduk3b117f72017-06-10 10:54:44 +0200116filter_setup=
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100117filter_env=
118filter_tree=
119filter_index=
120filter_parent=
121filter_msg=cat
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100122filter_commit=
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100123filter_tag_name=
Johannes Schindelin685ef542007-06-08 01:30:35 +0100124filter_subdir=
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100125state_branch=
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100126orig_namespace=refs/original/
127force=
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100128prune_empty=
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100129remap_to_ancestor=
David Kastrup822f7c72007-09-23 22:42:08 +0200130while :
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100131do
132 case "$1" in
133 --)
134 shift
135 break
136 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100137 --force|-f)
138 shift
139 force=t
140 continue
141 ;;
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100142 --remap-to-ancestor)
Csaba Henk7ec344d2010-08-27 20:44:56 +0000143 # deprecated ($remap_to_ancestor is set now automatically)
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100144 shift
145 remap_to_ancestor=t
146 continue
147 ;;
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100148 --prune-empty)
149 shift
150 prune_empty=t
151 continue
152 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100153 -*)
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 Heiduk3b117f72017-06-10 10:54:44 +0200170 --setup)
171 filter_setup="$OPTARG"
172 ;;
David Glasser07c49842017-10-17 09:45:15 +0000173 --subdirectory-filter)
174 filter_subdir="$OPTARG"
175 remap_to_ancestor=t
176 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100177 --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 Schindelin16ed34a2007-11-28 15:50:38 +0000193 filter_commit="$functions; $OPTARG"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100194 ;;
195 --tag-name-filter)
196 filter_tag_name="$OPTARG"
197 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100198 --original)
Junio C Hamano55ced832007-08-30 19:17:42 -0700199 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100200 ;;
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100201 --state-branch)
202 state_branch="$OPTARG"
203 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100204 *)
205 usage
206 ;;
207 esac
208done
209
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100210case "$prune_empty,$filter_commit" in
211,)
212 filter_commit='git commit-tree "$@"';;
213t,)
214 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
215,*)
216 ;;
217*)
Jacob Helwig5da81712010-02-11 18:46:22 -0800218 die "Cannot set --prune-empty and --commit-filter at the same time"
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100219esac
220
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100221case "$force" in
222t)
223 rm -rf "$tempdir"
224;;
225'')
226 test -d "$tempdir" &&
227 die "$tempdir already exists, please remove it"
228esac
Jeff King97276012013-04-02 10:22:19 -0400229orig_dir=$(pwd)
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100230mkdir -p "$tempdir/t" &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100231tempdir="$(cd "$tempdir"; pwd)" &&
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100232cd "$tempdir/t" &&
233workdir="$(pwd)" ||
234die ""
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100235
Brandon Caseydef16e72008-01-28 15:16:02 -0600236# Remove tempdir on exit
Jeff King97276012013-04-02 10:22:19 -0400237trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
Brandon Caseydef16e72008-01-28 15:16:02 -0600238
Lars Noschinski88e38802009-02-18 09:35:36 +0100239ORIG_GIT_DIR="$GIT_DIR"
240ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
241ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
Ian Campbell7b1378b2017-09-21 08:49:30 +0100242ORIG_GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME"
243ORIG_GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL"
244ORIG_GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE"
245ORIG_GIT_COMMITTER_NAME="$GIT_COMMITTER_NAME"
246ORIG_GIT_COMMITTER_EMAIL="$GIT_COMMITTER_EMAIL"
247ORIG_GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE"
248
Lars Noschinski88e38802009-02-18 09:35:36 +0100249GIT_WORK_TREE=.
250export GIT_DIR GIT_WORK_TREE
251
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100252# Make sure refs/original is empty
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500253git for-each-ref > "$tempdir"/backup-refs || exit
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100254while read sha1 type name
255do
256 case "$force,$name" in
257 ,$orig_namespace*)
John Tapsell734cd572009-02-19 07:36:35 +0000258 die "Cannot create a new backup.
259A previous backup already exists in $orig_namespace
260Force overwriting the backup with -f"
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100261 ;;
262 t,$orig_namespace*)
263 git update-ref -d "$name" $sha1
264 ;;
265 esac
266done < "$tempdir"/backup-refs
267
Junio C Hamano418fa3a2008-01-05 12:18:43 -0800268# The refs should be updated if their heads were rewritten
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500269git rev-parse --no-flags --revs-only --symbolic-full-name \
Yuki Kokubunf78ab352018-03-25 16:54:58 +0000270 --default HEAD "$@" > "$tempdir"/raw-refs || exit
271while read ref
272do
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
281done >"$tempdir"/heads <"$tempdir"/raw-refs
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100282
283test -s "$tempdir"/heads ||
Jean-Noel Avila69638932017-05-11 14:06:34 +0200284 die "You must specify a ref to rewrite."
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100285
Johannes Schindelin3addc942007-11-28 15:56:11 +0000286GIT_INDEX_FILE="$(pwd)/../index"
287export GIT_INDEX_FILE
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100288
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100289# map old->new commit ids for rewriting parents
290mkdir ../map || die "Could not create map/ directory"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100291
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100292if test -n "$state_branch"
293then
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
310fi
311
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100312# we need "--" only if there are no path arguments in $@
313nonrevs=$(git rev-parse --no-revs "$@") || exit
Csaba Henk7ec344d2010-08-27 20:44:56 +0000314if test -z "$nonrevs"
315then
316 dashdash=--
317else
318 dashdash=
319 remap_to_ancestor=t
320fi
321
Lee Carver3361a542013-09-10 22:55:35 +0000322git rev-parse --revs-only "$@" >../parse
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100323
Johannes Schindelin685ef542007-06-08 01:30:35 +0100324case "$filter_subdir" in
325"")
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100326 eval set -- "$(git rev-parse --sq --no-revs "$@")"
Johannes Schindelin685ef542007-06-08 01:30:35 +0100327 ;;
328*)
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100329 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
330 "$filter_subdir")"
331 ;;
332esac
333
334git rev-list --reverse --topo-order --default HEAD \
Lee Carver3361a542013-09-10 22:55:35 +0000335 --parents --simplify-merges --stdin "$@" <../parse >../revs ||
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100336 die "Could not get the commits"
Josh Triplett9d6f2202007-07-14 01:05:43 -0700337commits=$(wc -l <../revs | tr -d " ")
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100338
Michele Locati0a0eb2e2018-03-15 18:09:18 +0100339test $commits -eq 0 && die_with_status 2 "Found nothing to rewrite"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100340
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100341# Rewrite the commits
Gabor Bernat6a9d16a2015-09-07 15:52:08 +0200342report_progress ()
343{
344 if test -n "$progress" &&
345 test $git_filter_branch__commit_count -gt $next_sample_at
346 then
Junio C Hamano71400d92015-09-21 15:16:20 -0700347 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 Bernat6a9d16a2015-09-07 15:52:08 +0200353 then
Junio C Hamano71400d92015-09-21 15:16:20 -0700354 next_sample_at=$(( ($elapsed + 1) * $count / $elapsed ))
Gabor Bernat6a9d16a2015-09-07 15:52:08 +0200355 else
356 next_sample_at=$(($next_sample_at + 1))
357 fi
Junio C Hamano71400d92015-09-21 15:16:20 -0700358 progress=" ($elapsed seconds passed, remaining $remaining predicted)"
Gabor Bernat6a9d16a2015-09-07 15:52:08 +0200359 fi
Junio C Hamano71400d92015-09-21 15:16:20 -0700360 printf "\rRewrite $commit ($count/$commits)$progress "
Gabor Bernat6a9d16a2015-09-07 15:52:08 +0200361}
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100362
Elijah Newrend5b0c972009-03-25 15:51:01 -0600363git_filter_branch__commit_count=0
Gabor Bernat6a9d16a2015-09-07 15:52:08 +0200364
365progress= start_timestamp=
366if date '+%s' 2>/dev/null | grep -q '^[0-9][0-9]*$'
367then
368 next_sample_at=0
369 progress="dummy to ensure this is not empty"
370 start_timestamp=$(date '+%s')
371fi
372
Jeff King348d4f22015-11-06 01:24:29 -0500373if test -n "$filter_index" ||
374 test -n "$filter_tree" ||
375 test -n "$filter_subdir"
376then
377 need_index=t
378else
379 need_index=
380fi
381
Andreas Heiduk3b117f72017-06-10 10:54:44 +0200382eval "$filter_setup" < /dev/null ||
383 die "filter setup failed: $filter_setup"
384
Johannes Sixt813b4732007-06-08 23:28:39 +0200385while read commit parents; do
Elijah Newrend5b0c972009-03-25 15:51:01 -0600386 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
Gabor Bernat6a9d16a2015-09-07 15:52:08 +0200387
388 report_progress
Michael Barabanov709cfe82018-06-25 21:07:33 -0700389 test -f "$workdir"/../map/$commit && continue
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100390
Johannes Schindelin685ef542007-06-08 01:30:35 +0100391 case "$filter_subdir" in
392 "")
Jeff King348d4f22015-11-06 01:24:29 -0500393 if test -n "$need_index"
394 then
395 GIT_ALLOW_NULL_SHA1=1 git read-tree -i -m $commit
396 fi
Johannes Schindelin685ef542007-06-08 01:30:35 +0100397 ;;
398 *)
Junio C Hamano5b044ac2008-03-08 12:25:58 -0800399 # The commit may not have the subdirectory at all
Jeff King83bd7432013-08-27 16:41:12 -0400400 err=$(GIT_ALLOW_NULL_SHA1=1 \
401 git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
Miklos Vajnad69da762008-12-03 14:26:48 +0100402 if ! git rev-parse -q --verify $commit:"$filter_subdir"
Junio C Hamano5b044ac2008-03-08 12:25:58 -0800403 then
404 rm -f "$GIT_INDEX_FILE"
405 else
406 echo >&2 "$err"
407 false
408 fi
409 }
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100410 esac || die "Could not initialize the index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100411
Johannes Schindelin3addc942007-11-28 15:56:11 +0000412 GIT_COMMIT=$commit
413 export GIT_COMMIT
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100414 git cat-file commit "$commit" >../commit ||
415 die "Cannot read commit $commit"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100416
Jeff King3c730fa2012-10-18 06:33:02 -0400417 eval "$(set_ident <../commit)" ||
418 die "setting author/committer failed for commit $commit"
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100419 eval "$filter_env" < /dev/null ||
420 die "env filter failed: $filter_env"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100421
422 if [ "$filter_tree" ]; then
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100423 git checkout-index -f -u -a ||
424 die "Could not checkout the index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100425 # files that $commit removed are now still in the working tree;
426 # remove them, else they would be added again
veillette@yahoo.ca6a589fd2008-03-31 09:14:15 +0200427 git clean -d -q -f -x
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100428 eval "$filter_tree" < /dev/null ||
429 die "tree filter failed: $filter_tree"
430
Junio C Hamano1fe32cb2008-02-15 23:57:26 -0800431 (
SZEDER Gábor4d2a3642015-11-23 13:23:16 +0100432 git diff-index -r --name-only --ignore-submodules $commit -- &&
Junio C Hamano1fe32cb2008-02-15 23:57:26 -0800433 git ls-files --others
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500434 ) > "$tempdir"/tree-state || exit
435 git update-index --add --replace --remove --stdin \
436 < "$tempdir"/tree-state || exit
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100437 fi
438
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100439 eval "$filter_index" < /dev/null ||
440 die "index filter failed: $filter_index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100441
442 parentstr=
Johannes Sixt813b4732007-06-08 23:28:39 +0200443 for parent in $parents; do
Johannes Sixt3520e1e2007-06-06 20:38:35 +0200444 for reparent in $(map "$parent"); do
Charles Bailey79bc4ef2014-06-30 22:20:27 +0100445 case "$parentstr " in
446 *" -p $reparent "*)
447 ;;
448 *)
449 parentstr="$parentstr -p $reparent"
450 ;;
451 esac
Johannes Sixt3520e1e2007-06-06 20:38:35 +0200452 done
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100453 done
454 if [ "$filter_parent" ]; then
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100455 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
456 die "parent filter failed: $filter_parent"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100457 fi
458
Jeff Kingdf062012015-04-29 11:48:58 -0400459 {
James McCoya5a4b3f2015-10-08 20:21:13 -0400460 while IFS='' read -r header_line && test -n "$header_line"
Jeff Kingdf062012015-04-29 11:48:58 -0400461 do
462 # skip header lines...
463 :;
464 done
465 # and output the actual commit message
466 cat
467 } <../commit |
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100468 eval "$filter_msg" > ../message ||
469 die "msg filter failed: $filter_msg"
Jeff King348d4f22015-11-06 01:24:29 -0500470
471 if test -n "$need_index"
472 then
473 tree=$(git write-tree)
474 else
Jeff King1dc413e2016-01-19 17:07:22 -0500475 tree=$(git rev-parse "$commit^{tree}")
Jeff King348d4f22015-11-06 01:24:29 -0500476 fi
Michael Witten0906f6e2011-08-07 02:44:43 +0000477 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
Jeff King348d4f22015-11-06 01:24:29 -0500478 "$tree" $parentstr < ../message > ../map/$commit ||
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500479 die "could not write rewritten commit"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100480done <../revs
481
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100482# 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 Schindelindfd05e32007-07-23 18:34:13 +0100487
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100488if test "$remap_to_ancestor" = t
Thomas Rasta0e46392008-08-12 10:45:58 +0200489then
490 while read ref
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100491 do
Thomas Rasta0e46392008-08-12 10:45:58 +0200492 sha1=$(git rev-parse "$ref"^0)
493 test -f "$workdir"/../map/$sha1 && continue
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100494 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
Thomas Rasta0e46392008-08-12 10:45:58 +0200495 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
496 done < "$tempdir"/heads
497fi
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100498
499# Finally update the refs
500
501_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
502_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100503echo
504while read ref
505do
506 # avoid rewriting a ref twice
507 test -f "$orig_namespace$ref" && continue
508
509 sha1=$(git rev-parse "$ref"^0)
510 rewritten=$(map $sha1)
511
512 test $sha1 = "$rewritten" &&
513 warn "WARNING: Ref '$ref' is unchanged" &&
514 continue
515
516 case "$rewritten" in
517 '')
518 echo "Ref '$ref' was deleted"
519 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
520 die "Could not delete $ref"
Johannes Schindelin98409062007-06-05 16:58:13 +0100521 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100522 $_x40)
523 echo "Ref '$ref' was rewritten"
Thomas Rast261044e2008-08-08 01:50:31 +0200524 if ! git update-ref -m "filter-branch: rewrite" \
525 "$ref" $rewritten $sha1 2>/dev/null; then
526 if test $(git cat-file -t "$ref") = tag; then
527 if test -z "$filter_tag_name"; then
528 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
529 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
530 fi
531 else
532 die "Could not rewrite $ref"
533 fi
534 fi
Johannes Schindelin98409062007-06-05 16:58:13 +0100535 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100536 *)
537 # NEEDSWORK: possibly add -Werror, making this an error
538 warn "WARNING: '$ref' was rewritten into multiple commits:"
539 warn "$rewritten"
540 warn "WARNING: Ref '$ref' points to the first one now."
541 rewritten=$(echo "$rewritten" | head -n 1)
542 git update-ref -m "filter-branch: rewrite to first" \
543 "$ref" $rewritten $sha1 ||
544 die "Could not rewrite $ref"
545 ;;
546 esac
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500547 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
548 exit
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100549done < "$tempdir"/heads
550
551# TODO: This should possibly go, with the semantics that all positive given
552# refs are updated, and their original heads stored in refs/original/
553# Filter tags
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100554
555if [ "$filter_tag_name" ]; then
Junio C Hamano5be60072007-07-02 22:52:14 -0700556 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100557 while read sha1 type ref; do
558 ref="${ref#refs/tags/}"
559 # XXX: Rewrite tagged trees as well?
560 if [ "$type" != "commit" -a "$type" != "tag" ]; then
561 continue;
562 fi
563
564 if [ "$type" = "tag" ]; then
565 # Dereference to a commit
566 sha1t="$sha1"
Dan Loewenherz7bd93c12009-04-22 21:46:02 -0400567 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100568 fi
569
570 [ -f "../map/$sha1" ] || continue
571 new_sha1="$(cat "../map/$sha1")"
Johannes Schindelin3addc942007-11-28 15:56:11 +0000572 GIT_COMMIT="$sha1"
573 export GIT_COMMIT
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100574 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
575 die "tag name filter failed: $filter_tag_name"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100576
577 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
578
579 if [ "$type" = "tag" ]; then
Johannes Sixta9da1662008-08-21 16:45:11 +0200580 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
581 "$new_sha1" "$new_ref"
582 git cat-file tag "$ref" |
Brandon Casey1bf65512008-03-26 10:47:09 -0500583 sed -n \
Stephen Boyd9524cf22010-01-26 15:08:31 -0800584 -e '1,/^$/{
Johannes Sixta9da1662008-08-21 16:45:11 +0200585 /^object /d
586 /^type /d
587 /^tag /d
Stephen Boyd9524cf22010-01-26 15:08:31 -0800588 }' \
Brandon Casey1bf65512008-03-26 10:47:09 -0500589 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
Johannes Sixta9da1662008-08-21 16:45:11 +0200590 -e 'p' ) |
Ian Campbellb2c1ca62017-09-21 08:49:32 +0100591 git hash-object -t tag -w --stdin) ||
Brandon Casey1bf65512008-03-26 10:47:09 -0500592 die "Could not create new tag object for $ref"
593 if git cat-file tag "$ref" | \
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800594 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
Brandon Casey1bf65512008-03-26 10:47:09 -0500595 then
596 warn "gpg signature stripped from tag object $sha1t"
597 fi
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100598 fi
599
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100600 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
601 die "Could not write tag $new_ref"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100602 done
603fi
604
Eric Kidd9273b562009-02-03 13:27:03 -0500605unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
Ian Campbell7b1378b2017-09-21 08:49:30 +0100606unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
607unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE
Eric Kidd9273b562009-02-03 13:27:03 -0500608test -z "$ORIG_GIT_DIR" || {
609 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
610}
611test -z "$ORIG_GIT_WORK_TREE" || {
612 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
613 export GIT_WORK_TREE
614}
615test -z "$ORIG_GIT_INDEX_FILE" || {
616 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
617 export GIT_INDEX_FILE
618}
Ian Campbell7b1378b2017-09-21 08:49:30 +0100619test -z "$ORIG_GIT_AUTHOR_NAME" || {
620 GIT_AUTHOR_NAME="$ORIG_GIT_AUTHOR_NAME" &&
621 export GIT_AUTHOR_NAME
622}
623test -z "$ORIG_GIT_AUTHOR_EMAIL" || {
624 GIT_AUTHOR_EMAIL="$ORIG_GIT_AUTHOR_EMAIL" &&
625 export GIT_AUTHOR_EMAIL
626}
627test -z "$ORIG_GIT_AUTHOR_DATE" || {
628 GIT_AUTHOR_DATE="$ORIG_GIT_AUTHOR_DATE" &&
629 export GIT_AUTHOR_DATE
630}
631test -z "$ORIG_GIT_COMMITTER_NAME" || {
632 GIT_COMMITTER_NAME="$ORIG_GIT_COMMITTER_NAME" &&
633 export GIT_COMMITTER_NAME
634}
635test -z "$ORIG_GIT_COMMITTER_EMAIL" || {
636 GIT_COMMITTER_EMAIL="$ORIG_GIT_COMMITTER_EMAIL" &&
637 export GIT_COMMITTER_EMAIL
638}
639test -z "$ORIG_GIT_COMMITTER_DATE" || {
640 GIT_COMMITTER_DATE="$ORIG_GIT_COMMITTER_DATE" &&
641 export GIT_COMMITTER_DATE
642}
Eric Kidd9273b562009-02-03 13:27:03 -0500643
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100644if test -n "$state_branch"
645then
646 echo "Saving rewrite state to $state_branch" 1>&2
647 state_blob=$(
648 perl -e'opendir D, "../map" or die;
649 open H, "|-", "git hash-object -w --stdin" or die;
650 foreach (sort readdir(D)) {
651 next if m/^\.\.?$/;
652 open F, "<../map/$_" or die;
653 chomp($f = <F>);
654 print H "$_:$f\n" or die;
655 }
656 close(H) or die;' || die "Unable to save state")
Michele Locati206a6ae2018-03-19 16:52:59 +0100657 state_tree=$(printf '100644 blob %s\tfilter.map\n' "$state_blob" | git mktree)
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100658 if test -n "$state_commit"
659 then
Michele Locati206a6ae2018-03-19 16:52:59 +0100660 state_commit=$(echo "Sync" | git commit-tree "$state_tree" -p "$state_commit")
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100661 else
Michele Locati206a6ae2018-03-19 16:52:59 +0100662 state_commit=$(echo "Sync" | git commit-tree "$state_tree" )
Ian Campbellbd2c79f2017-09-21 08:49:31 +0100663 fi
664 git update-ref "$state_branch" "$state_commit"
665fi
666
Ian Campbelld24813c2017-09-21 08:49:29 +0100667cd "$orig_dir"
668rm -rf "$tempdir"
669
670trap - 0
671
Petr Baudisa4661b02008-07-24 00:15:36 +0200672if [ "$(is_bare_repository)" = false ]; then
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500673 git read-tree -u -m HEAD || exit
Petr Baudisa4661b02008-07-24 00:15:36 +0200674fi
Johannes Schindelin46eb4492007-10-17 03:23:10 +0100675
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500676exit 0