blob: add2c0247fa91e0f629428c295fc581f19cf85e1 [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
Steffen Prohaskab5669a02007-07-04 10:36:24 +020014warn () {
Jon Seymour285c6cb2011-08-05 23:31:29 +100015 echo "$*" >&2
Steffen Prohaskab5669a02007-07-04 10:36:24 +020016}
17
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010018map()
19{
Johannes Sixt3520e1e2007-06-06 20:38:35 +020020 # if it was not rewritten, take the original
Johannes Sixtc57a3492007-07-04 14:08:17 +020021 if test -r "$workdir/../map/$1"
22 then
23 cat "$workdir/../map/$1"
24 else
25 echo "$1"
26 fi
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010027}
28
Johannes Schindelinf95eef12007-08-31 20:06:27 +010029# if you run 'skip_commit "$@"' in a commit filter, it will print
30# the (mapped) parents, effectively skipping the commit.
31
32skip_commit()
33{
34 shift;
35 while [ -n "$1" ];
36 do
37 shift;
38 map "$1";
39 shift;
40 done;
41}
42
Pierre Habouzitd3240d92008-10-31 10:12:21 +010043# 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.
45git_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 Schindelin8c1ce0f2007-07-04 15:36:01 +010053# override die(): this version puts in an extra line break, so that
54# the progress is still visible
55
56die()
57{
58 echo >&2
59 echo "$*" >&2
60 exit 1
61}
Johannes Schindelin16ed34a2007-11-28 15:50:38 +000062EOF
63)
64
65eval "$functions"
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +010066
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010067# When piped a commit, output a script to set the ident of either
68# "author" or "committer
69
70set_ident () {
Jeff King40a7ce62008-03-12 17:29:57 -040071 lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
72 uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010073 pick_id_script='
74 /^'$lid' /{
75 s/'\''/'\''\\'\'\''/g
76 h
77 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
78 s/'\''/'\''\'\'\''/g
Johannes Schindelin3addc942007-11-28 15:56:11 +000079 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010080
81 g
82 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
83 s/'\''/'\''\'\'\''/g
Johannes Schindelin3addc942007-11-28 15:56:11 +000084 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010085
86 g
87 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
88 s/'\''/'\''\'\'\''/g
Johannes Schindelin3addc942007-11-28 15:56:11 +000089 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010090
91 q
92 }
93 '
94
95 LANG=C LC_ALL=C sed -ne "$pick_id_script"
96 # Ensure non-empty id name.
Johannes Schindelin3addc942007-11-28 15:56:11 +000097 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +010098}
99
Matthieu Moy83e355a2009-08-18 11:51:00 +0200100USAGE="[--env-filter <command>] [--tree-filter <command>]
Junio C Hamano9dcca582011-08-05 15:06:21 -0700101 [--index-filter <command>] [--parent-filter <command>]
102 [--msg-filter <command>] [--commit-filter <command>]
103 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
104 [--original <namespace>] [-d <directory>] [-f | --force]
105 [<rev-list options>...]"
Johannes Schindelin7e0f1702007-08-31 20:05:36 +0100106
Junio C Hamano8f321a32007-11-06 01:50:02 -0800107OPTIONS_SPEC=
Johannes Schindelin7e0f1702007-08-31 20:05:36 +0100108. git-sh-setup
109
Petr Baudisa4661b02008-07-24 00:15:36 +0200110if [ "$(is_bare_repository)" = false ]; then
Jeff King5347a502011-09-01 17:53:07 -0400111 require_clean_work_tree 'rewrite branches'
Petr Baudisa4661b02008-07-24 00:15:36 +0200112fi
Johannes Schindelin46eb4492007-10-17 03:23:10 +0100113
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100114tempdir=.git-rewrite
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100115filter_env=
116filter_tree=
117filter_index=
118filter_parent=
119filter_msg=cat
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100120filter_commit=
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100121filter_tag_name=
Johannes Schindelin685ef542007-06-08 01:30:35 +0100122filter_subdir=
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100123orig_namespace=refs/original/
124force=
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100125prune_empty=
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100126remap_to_ancestor=
David Kastrup822f7c72007-09-23 22:42:08 +0200127while :
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100128do
129 case "$1" in
130 --)
131 shift
132 break
133 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100134 --force|-f)
135 shift
136 force=t
137 continue
138 ;;
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100139 --remap-to-ancestor)
Csaba Henk7ec344d2010-08-27 20:44:56 +0000140 # deprecated ($remap_to_ancestor is set now automatically)
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100141 shift
142 remap_to_ancestor=t
143 continue
144 ;;
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100145 --prune-empty)
146 shift
147 prune_empty=t
148 continue
149 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100150 -*)
151 ;;
152 *)
153 break;
154 esac
155
156 # all switches take one argument
157 ARG="$1"
158 case "$#" in 1) usage ;; esac
159 shift
160 OPTARG="$1"
161 shift
162
163 case "$ARG" in
164 -d)
165 tempdir="$OPTARG"
166 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100167 --env-filter)
168 filter_env="$OPTARG"
169 ;;
170 --tree-filter)
171 filter_tree="$OPTARG"
172 ;;
173 --index-filter)
174 filter_index="$OPTARG"
175 ;;
176 --parent-filter)
177 filter_parent="$OPTARG"
178 ;;
179 --msg-filter)
180 filter_msg="$OPTARG"
181 ;;
182 --commit-filter)
Johannes Schindelin16ed34a2007-11-28 15:50:38 +0000183 filter_commit="$functions; $OPTARG"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100184 ;;
185 --tag-name-filter)
186 filter_tag_name="$OPTARG"
187 ;;
Johannes Schindelin685ef542007-06-08 01:30:35 +0100188 --subdirectory-filter)
189 filter_subdir="$OPTARG"
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100190 remap_to_ancestor=t
Johannes Schindelin685ef542007-06-08 01:30:35 +0100191 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100192 --original)
Junio C Hamano55ced832007-08-30 19:17:42 -0700193 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100194 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100195 *)
196 usage
197 ;;
198 esac
199done
200
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100201case "$prune_empty,$filter_commit" in
202,)
203 filter_commit='git commit-tree "$@"';;
204t,)
205 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
206,*)
207 ;;
208*)
Jacob Helwig5da81712010-02-11 18:46:22 -0800209 die "Cannot set --prune-empty and --commit-filter at the same time"
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100210esac
211
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100212case "$force" in
213t)
214 rm -rf "$tempdir"
215;;
216'')
217 test -d "$tempdir" &&
218 die "$tempdir already exists, please remove it"
219esac
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100220mkdir -p "$tempdir/t" &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100221tempdir="$(cd "$tempdir"; pwd)" &&
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100222cd "$tempdir/t" &&
223workdir="$(pwd)" ||
224die ""
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100225
Brandon Caseydef16e72008-01-28 15:16:02 -0600226# Remove tempdir on exit
227trap 'cd ../..; rm -rf "$tempdir"' 0
228
Lars Noschinski88e38802009-02-18 09:35:36 +0100229ORIG_GIT_DIR="$GIT_DIR"
230ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
231ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
232GIT_WORK_TREE=.
233export GIT_DIR GIT_WORK_TREE
234
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100235# Make sure refs/original is empty
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500236git for-each-ref > "$tempdir"/backup-refs || exit
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100237while read sha1 type name
238do
239 case "$force,$name" in
240 ,$orig_namespace*)
John Tapsell734cd572009-02-19 07:36:35 +0000241 die "Cannot create a new backup.
242A previous backup already exists in $orig_namespace
243Force overwriting the backup with -f"
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100244 ;;
245 t,$orig_namespace*)
246 git update-ref -d "$name" $sha1
247 ;;
248 esac
249done < "$tempdir"/backup-refs
250
Junio C Hamano418fa3a2008-01-05 12:18:43 -0800251# The refs should be updated if their heads were rewritten
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500252git rev-parse --no-flags --revs-only --symbolic-full-name \
253 --default HEAD "$@" > "$tempdir"/raw-heads || exit
254sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100255
256test -s "$tempdir"/heads ||
257 die "Which ref do you want to rewrite?"
258
Johannes Schindelin3addc942007-11-28 15:56:11 +0000259GIT_INDEX_FILE="$(pwd)/../index"
260export GIT_INDEX_FILE
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100261
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100262# map old->new commit ids for rewriting parents
263mkdir ../map || die "Could not create map/ directory"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100264
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100265# we need "--" only if there are no path arguments in $@
266nonrevs=$(git rev-parse --no-revs "$@") || exit
Csaba Henk7ec344d2010-08-27 20:44:56 +0000267if test -z "$nonrevs"
268then
269 dashdash=--
270else
271 dashdash=
272 remap_to_ancestor=t
273fi
274
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100275rev_args=$(git rev-parse --revs-only "$@")
276
Johannes Schindelin685ef542007-06-08 01:30:35 +0100277case "$filter_subdir" in
278"")
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100279 eval set -- "$(git rev-parse --sq --no-revs "$@")"
Johannes Schindelin685ef542007-06-08 01:30:35 +0100280 ;;
281*)
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100282 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
283 "$filter_subdir")"
284 ;;
285esac
286
287git rev-list --reverse --topo-order --default HEAD \
288 --parents --simplify-merges $rev_args "$@" > ../revs ||
289 die "Could not get the commits"
Josh Triplett9d6f2202007-07-14 01:05:43 -0700290commits=$(wc -l <../revs | tr -d " ")
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100291
292test $commits -eq 0 && die "Found nothing to rewrite"
293
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100294# Rewrite the commits
295
Elijah Newrend5b0c972009-03-25 15:51:01 -0600296git_filter_branch__commit_count=0
Johannes Sixt813b4732007-06-08 23:28:39 +0200297while read commit parents; do
Elijah Newrend5b0c972009-03-25 15:51:01 -0600298 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
299 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100300
Johannes Schindelin685ef542007-06-08 01:30:35 +0100301 case "$filter_subdir" in
302 "")
Junio C Hamano5be60072007-07-02 22:52:14 -0700303 git read-tree -i -m $commit
Johannes Schindelin685ef542007-06-08 01:30:35 +0100304 ;;
305 *)
Junio C Hamano5b044ac2008-03-08 12:25:58 -0800306 # The commit may not have the subdirectory at all
307 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
Miklos Vajnad69da762008-12-03 14:26:48 +0100308 if ! git rev-parse -q --verify $commit:"$filter_subdir"
Junio C Hamano5b044ac2008-03-08 12:25:58 -0800309 then
310 rm -f "$GIT_INDEX_FILE"
311 else
312 echo >&2 "$err"
313 false
314 fi
315 }
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100316 esac || die "Could not initialize the index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100317
Johannes Schindelin3addc942007-11-28 15:56:11 +0000318 GIT_COMMIT=$commit
319 export GIT_COMMIT
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100320 git cat-file commit "$commit" >../commit ||
321 die "Cannot read commit $commit"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100322
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100323 eval "$(set_ident AUTHOR <../commit)" ||
324 die "setting author failed for commit $commit"
325 eval "$(set_ident COMMITTER <../commit)" ||
326 die "setting committer failed for commit $commit"
327 eval "$filter_env" < /dev/null ||
328 die "env filter failed: $filter_env"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100329
330 if [ "$filter_tree" ]; then
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100331 git checkout-index -f -u -a ||
332 die "Could not checkout the index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100333 # files that $commit removed are now still in the working tree;
334 # remove them, else they would be added again
veillette@yahoo.ca6a589fd2008-03-31 09:14:15 +0200335 git clean -d -q -f -x
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100336 eval "$filter_tree" < /dev/null ||
337 die "tree filter failed: $filter_tree"
338
Junio C Hamano1fe32cb2008-02-15 23:57:26 -0800339 (
Michal Sojka03ca8392010-01-28 10:08:46 +0100340 git diff-index -r --name-only --ignore-submodules $commit &&
Junio C Hamano1fe32cb2008-02-15 23:57:26 -0800341 git ls-files --others
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500342 ) > "$tempdir"/tree-state || exit
343 git update-index --add --replace --remove --stdin \
344 < "$tempdir"/tree-state || exit
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100345 fi
346
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100347 eval "$filter_index" < /dev/null ||
348 die "index filter failed: $filter_index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100349
350 parentstr=
Johannes Sixt813b4732007-06-08 23:28:39 +0200351 for parent in $parents; do
Johannes Sixt3520e1e2007-06-06 20:38:35 +0200352 for reparent in $(map "$parent"); do
353 parentstr="$parentstr -p $reparent"
354 done
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100355 done
356 if [ "$filter_parent" ]; then
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100357 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
358 die "parent filter failed: $filter_parent"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100359 fi
360
361 sed -e '1,/^$/d' <../commit | \
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100362 eval "$filter_msg" > ../message ||
363 die "msg filter failed: $filter_msg"
Michael Witten0906f6e2011-08-07 02:44:43 +0000364 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500365 $(git write-tree) $parentstr < ../message > ../map/$commit ||
366 die "could not write rewritten commit"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100367done <../revs
368
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100369# If we are filtering for paths, as in the case of a subdirectory
370# filter, it is possible that a specified head is not in the set of
371# rewritten commits, because it was pruned by the revision walker.
372# Ancestor remapping fixes this by mapping these heads to the unique
373# nearest ancestor that survived the pruning.
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100374
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100375if test "$remap_to_ancestor" = t
Thomas Rasta0e46392008-08-12 10:45:58 +0200376then
377 while read ref
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100378 do
Thomas Rasta0e46392008-08-12 10:45:58 +0200379 sha1=$(git rev-parse "$ref"^0)
380 test -f "$workdir"/../map/$sha1 && continue
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100381 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
Thomas Rasta0e46392008-08-12 10:45:58 +0200382 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
383 done < "$tempdir"/heads
384fi
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100385
386# Finally update the refs
387
388_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
389_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100390echo
391while read ref
392do
393 # avoid rewriting a ref twice
394 test -f "$orig_namespace$ref" && continue
395
396 sha1=$(git rev-parse "$ref"^0)
397 rewritten=$(map $sha1)
398
399 test $sha1 = "$rewritten" &&
400 warn "WARNING: Ref '$ref' is unchanged" &&
401 continue
402
403 case "$rewritten" in
404 '')
405 echo "Ref '$ref' was deleted"
406 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
407 die "Could not delete $ref"
Johannes Schindelin98409062007-06-05 16:58:13 +0100408 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100409 $_x40)
410 echo "Ref '$ref' was rewritten"
Thomas Rast261044e2008-08-08 01:50:31 +0200411 if ! git update-ref -m "filter-branch: rewrite" \
412 "$ref" $rewritten $sha1 2>/dev/null; then
413 if test $(git cat-file -t "$ref") = tag; then
414 if test -z "$filter_tag_name"; then
415 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
416 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
417 fi
418 else
419 die "Could not rewrite $ref"
420 fi
421 fi
Johannes Schindelin98409062007-06-05 16:58:13 +0100422 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100423 *)
424 # NEEDSWORK: possibly add -Werror, making this an error
425 warn "WARNING: '$ref' was rewritten into multiple commits:"
426 warn "$rewritten"
427 warn "WARNING: Ref '$ref' points to the first one now."
428 rewritten=$(echo "$rewritten" | head -n 1)
429 git update-ref -m "filter-branch: rewrite to first" \
430 "$ref" $rewritten $sha1 ||
431 die "Could not rewrite $ref"
432 ;;
433 esac
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500434 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
435 exit
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100436done < "$tempdir"/heads
437
438# TODO: This should possibly go, with the semantics that all positive given
439# refs are updated, and their original heads stored in refs/original/
440# Filter tags
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100441
442if [ "$filter_tag_name" ]; then
Junio C Hamano5be60072007-07-02 22:52:14 -0700443 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100444 while read sha1 type ref; do
445 ref="${ref#refs/tags/}"
446 # XXX: Rewrite tagged trees as well?
447 if [ "$type" != "commit" -a "$type" != "tag" ]; then
448 continue;
449 fi
450
451 if [ "$type" = "tag" ]; then
452 # Dereference to a commit
453 sha1t="$sha1"
Dan Loewenherz7bd93c12009-04-22 21:46:02 -0400454 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100455 fi
456
457 [ -f "../map/$sha1" ] || continue
458 new_sha1="$(cat "../map/$sha1")"
Johannes Schindelin3addc942007-11-28 15:56:11 +0000459 GIT_COMMIT="$sha1"
460 export GIT_COMMIT
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100461 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
462 die "tag name filter failed: $filter_tag_name"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100463
464 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
465
466 if [ "$type" = "tag" ]; then
Johannes Sixta9da1662008-08-21 16:45:11 +0200467 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
468 "$new_sha1" "$new_ref"
469 git cat-file tag "$ref" |
Brandon Casey1bf65512008-03-26 10:47:09 -0500470 sed -n \
Stephen Boyd9524cf22010-01-26 15:08:31 -0800471 -e '1,/^$/{
Johannes Sixta9da1662008-08-21 16:45:11 +0200472 /^object /d
473 /^type /d
474 /^tag /d
Stephen Boyd9524cf22010-01-26 15:08:31 -0800475 }' \
Brandon Casey1bf65512008-03-26 10:47:09 -0500476 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
Johannes Sixta9da1662008-08-21 16:45:11 +0200477 -e 'p' ) |
Brandon Casey1bf65512008-03-26 10:47:09 -0500478 git mktag) ||
479 die "Could not create new tag object for $ref"
480 if git cat-file tag "$ref" | \
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800481 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
Brandon Casey1bf65512008-03-26 10:47:09 -0500482 then
483 warn "gpg signature stripped from tag object $sha1t"
484 fi
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100485 fi
486
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100487 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
488 die "Could not write tag $new_ref"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100489 done
490fi
491
492cd ../..
493rm -rf "$tempdir"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100494
Brandon Caseydef16e72008-01-28 15:16:02 -0600495trap - 0
496
Eric Kidd9273b562009-02-03 13:27:03 -0500497unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
498test -z "$ORIG_GIT_DIR" || {
499 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
500}
501test -z "$ORIG_GIT_WORK_TREE" || {
502 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
503 export GIT_WORK_TREE
504}
505test -z "$ORIG_GIT_INDEX_FILE" || {
506 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
507 export GIT_INDEX_FILE
508}
509
Petr Baudisa4661b02008-07-24 00:15:36 +0200510if [ "$(is_bare_repository)" = false ]; then
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500511 git read-tree -u -m HEAD || exit
Petr Baudisa4661b02008-07-24 00:15:36 +0200512fi
Johannes Schindelin46eb4492007-10-17 03:23:10 +0100513
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500514exit 0