blob: 962a93b586571eb6fc60aae53c77f6e6b9fb281f [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 () {
15 echo "$*" >&2
16}
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>]
101 [--index-filter <command>] [--parent-filter <command>]
102 [--msg-filter <command>] [--commit-filter <command>]
103 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
104 [--original <namespace>] [-d <directory>] [-f | --force]
105 [<rev-list options>...]"
Johannes 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
Johannes Schindelin26be15f2009-02-05 19:19:33 +0100111 git diff-files --ignore-submodules --quiet &&
Junio C Hamano38762c42007-11-28 16:15:04 -0800112 git diff-index --cached --quiet HEAD -- ||
Johannes Schindelin46eb4492007-10-17 03:23:10 +0100113 die "Cannot rewrite branch(es) with a dirty working directory."
Petr Baudisa4661b02008-07-24 00:15:36 +0200114fi
Johannes Schindelin46eb4492007-10-17 03:23:10 +0100115
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100116tempdir=.git-rewrite
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=
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100125orig_namespace=refs/original/
126force=
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100127prune_empty=
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100128remap_to_ancestor=
David Kastrup822f7c72007-09-23 22:42:08 +0200129while :
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100130do
131 case "$1" in
132 --)
133 shift
134 break
135 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100136 --force|-f)
137 shift
138 force=t
139 continue
140 ;;
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100141 --remap-to-ancestor)
Csaba Henk7ec344d2010-08-27 20:44:56 +0000142 # deprecated ($remap_to_ancestor is set now automatically)
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100143 shift
144 remap_to_ancestor=t
145 continue
146 ;;
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100147 --prune-empty)
148 shift
149 prune_empty=t
150 continue
151 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100152 -*)
153 ;;
154 *)
155 break;
156 esac
157
158 # all switches take one argument
159 ARG="$1"
160 case "$#" in 1) usage ;; esac
161 shift
162 OPTARG="$1"
163 shift
164
165 case "$ARG" in
166 -d)
167 tempdir="$OPTARG"
168 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100169 --env-filter)
170 filter_env="$OPTARG"
171 ;;
172 --tree-filter)
173 filter_tree="$OPTARG"
174 ;;
175 --index-filter)
176 filter_index="$OPTARG"
177 ;;
178 --parent-filter)
179 filter_parent="$OPTARG"
180 ;;
181 --msg-filter)
182 filter_msg="$OPTARG"
183 ;;
184 --commit-filter)
Johannes Schindelin16ed34a2007-11-28 15:50:38 +0000185 filter_commit="$functions; $OPTARG"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100186 ;;
187 --tag-name-filter)
188 filter_tag_name="$OPTARG"
189 ;;
Johannes Schindelin685ef542007-06-08 01:30:35 +0100190 --subdirectory-filter)
191 filter_subdir="$OPTARG"
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100192 remap_to_ancestor=t
Johannes Schindelin685ef542007-06-08 01:30:35 +0100193 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100194 --original)
Junio C Hamano55ced832007-08-30 19:17:42 -0700195 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100196 ;;
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100197 *)
198 usage
199 ;;
200 esac
201done
202
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100203case "$prune_empty,$filter_commit" in
204,)
205 filter_commit='git commit-tree "$@"';;
206t,)
207 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
208,*)
209 ;;
210*)
Jacob Helwig5da81712010-02-11 18:46:22 -0800211 die "Cannot set --prune-empty and --commit-filter at the same time"
Pierre Habouzitd3240d92008-10-31 10:12:21 +0100212esac
213
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100214case "$force" in
215t)
216 rm -rf "$tempdir"
217;;
218'')
219 test -d "$tempdir" &&
220 die "$tempdir already exists, please remove it"
221esac
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100222mkdir -p "$tempdir/t" &&
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100223tempdir="$(cd "$tempdir"; pwd)" &&
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100224cd "$tempdir/t" &&
225workdir="$(pwd)" ||
226die ""
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100227
Brandon Caseydef16e72008-01-28 15:16:02 -0600228# Remove tempdir on exit
229trap 'cd ../..; rm -rf "$tempdir"' 0
230
Lars Noschinski88e38802009-02-18 09:35:36 +0100231ORIG_GIT_DIR="$GIT_DIR"
232ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
233ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
234GIT_WORK_TREE=.
235export GIT_DIR GIT_WORK_TREE
236
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100237# Make sure refs/original is empty
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500238git for-each-ref > "$tempdir"/backup-refs || exit
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100239while read sha1 type name
240do
241 case "$force,$name" in
242 ,$orig_namespace*)
John Tapsell734cd572009-02-19 07:36:35 +0000243 die "Cannot create a new backup.
244A previous backup already exists in $orig_namespace
245Force overwriting the backup with -f"
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100246 ;;
247 t,$orig_namespace*)
248 git update-ref -d "$name" $sha1
249 ;;
250 esac
251done < "$tempdir"/backup-refs
252
Junio C Hamano418fa3a2008-01-05 12:18:43 -0800253# The refs should be updated if their heads were rewritten
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500254git rev-parse --no-flags --revs-only --symbolic-full-name \
255 --default HEAD "$@" > "$tempdir"/raw-heads || exit
256sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100257
258test -s "$tempdir"/heads ||
259 die "Which ref do you want to rewrite?"
260
Johannes Schindelin3addc942007-11-28 15:56:11 +0000261GIT_INDEX_FILE="$(pwd)/../index"
262export GIT_INDEX_FILE
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100263
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100264# map old->new commit ids for rewriting parents
265mkdir ../map || die "Could not create map/ directory"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100266
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100267# we need "--" only if there are no path arguments in $@
268nonrevs=$(git rev-parse --no-revs "$@") || exit
Csaba Henk7ec344d2010-08-27 20:44:56 +0000269if test -z "$nonrevs"
270then
271 dashdash=--
272else
273 dashdash=
274 remap_to_ancestor=t
275fi
276
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100277rev_args=$(git rev-parse --revs-only "$@")
278
Johannes Schindelin685ef542007-06-08 01:30:35 +0100279case "$filter_subdir" in
280"")
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100281 eval set -- "$(git rev-parse --sq --no-revs "$@")"
Johannes Schindelin685ef542007-06-08 01:30:35 +0100282 ;;
283*)
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100284 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
285 "$filter_subdir")"
286 ;;
287esac
288
289git rev-list --reverse --topo-order --default HEAD \
290 --parents --simplify-merges $rev_args "$@" > ../revs ||
291 die "Could not get the commits"
Josh Triplett9d6f2202007-07-14 01:05:43 -0700292commits=$(wc -l <../revs | tr -d " ")
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100293
294test $commits -eq 0 && die "Found nothing to rewrite"
295
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100296# Rewrite the commits
297
Elijah Newrend5b0c972009-03-25 15:51:01 -0600298git_filter_branch__commit_count=0
Johannes Sixt813b4732007-06-08 23:28:39 +0200299while read commit parents; do
Elijah Newrend5b0c972009-03-25 15:51:01 -0600300 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
301 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100302
Johannes Schindelin685ef542007-06-08 01:30:35 +0100303 case "$filter_subdir" in
304 "")
Junio C Hamano5be60072007-07-02 22:52:14 -0700305 git read-tree -i -m $commit
Johannes Schindelin685ef542007-06-08 01:30:35 +0100306 ;;
307 *)
Junio C Hamano5b044ac2008-03-08 12:25:58 -0800308 # The commit may not have the subdirectory at all
309 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
Miklos Vajnad69da762008-12-03 14:26:48 +0100310 if ! git rev-parse -q --verify $commit:"$filter_subdir"
Junio C Hamano5b044ac2008-03-08 12:25:58 -0800311 then
312 rm -f "$GIT_INDEX_FILE"
313 else
314 echo >&2 "$err"
315 false
316 fi
317 }
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100318 esac || die "Could not initialize the index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100319
Johannes Schindelin3addc942007-11-28 15:56:11 +0000320 GIT_COMMIT=$commit
321 export GIT_COMMIT
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100322 git cat-file commit "$commit" >../commit ||
323 die "Cannot read commit $commit"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100324
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100325 eval "$(set_ident AUTHOR <../commit)" ||
326 die "setting author failed for commit $commit"
327 eval "$(set_ident COMMITTER <../commit)" ||
328 die "setting committer failed for commit $commit"
329 eval "$filter_env" < /dev/null ||
330 die "env filter failed: $filter_env"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100331
332 if [ "$filter_tree" ]; then
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100333 git checkout-index -f -u -a ||
334 die "Could not checkout the index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100335 # files that $commit removed are now still in the working tree;
336 # remove them, else they would be added again
veillette@yahoo.ca6a589fd2008-03-31 09:14:15 +0200337 git clean -d -q -f -x
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100338 eval "$filter_tree" < /dev/null ||
339 die "tree filter failed: $filter_tree"
340
Junio C Hamano1fe32cb2008-02-15 23:57:26 -0800341 (
Michal Sojka03ca8392010-01-28 10:08:46 +0100342 git diff-index -r --name-only --ignore-submodules $commit &&
Junio C Hamano1fe32cb2008-02-15 23:57:26 -0800343 git ls-files --others
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500344 ) > "$tempdir"/tree-state || exit
345 git update-index --add --replace --remove --stdin \
346 < "$tempdir"/tree-state || exit
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100347 fi
348
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100349 eval "$filter_index" < /dev/null ||
350 die "index filter failed: $filter_index"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100351
352 parentstr=
Johannes Sixt813b4732007-06-08 23:28:39 +0200353 for parent in $parents; do
Johannes Sixt3520e1e2007-06-06 20:38:35 +0200354 for reparent in $(map "$parent"); do
355 parentstr="$parentstr -p $reparent"
356 done
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100357 done
358 if [ "$filter_parent" ]; then
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100359 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
360 die "parent filter failed: $filter_parent"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100361 fi
362
363 sed -e '1,/^$/d' <../commit | \
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100364 eval "$filter_msg" > ../message ||
365 die "msg filter failed: $filter_msg"
Jeff King4bf9f272008-03-12 17:41:39 -0400366 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500367 $(git write-tree) $parentstr < ../message > ../map/$commit ||
368 die "could not write rewritten commit"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100369done <../revs
370
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100371# If we are filtering for paths, as in the case of a subdirectory
372# filter, it is possible that a specified head is not in the set of
373# rewritten commits, because it was pruned by the revision walker.
374# Ancestor remapping fixes this by mapping these heads to the unique
375# nearest ancestor that survived the pruning.
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100376
Thomas Rastf2f3a6b2009-11-10 22:04:51 +0100377if test "$remap_to_ancestor" = t
Thomas Rasta0e46392008-08-12 10:45:58 +0200378then
379 while read ref
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100380 do
Thomas Rasta0e46392008-08-12 10:45:58 +0200381 sha1=$(git rev-parse "$ref"^0)
382 test -f "$workdir"/../map/$sha1 && continue
Thomas Rast2c1d2d82009-11-11 09:53:46 +0100383 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
Thomas Rasta0e46392008-08-12 10:45:58 +0200384 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
385 done < "$tempdir"/heads
386fi
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100387
388# Finally update the refs
389
390_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
391_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100392echo
393while read ref
394do
395 # avoid rewriting a ref twice
396 test -f "$orig_namespace$ref" && continue
397
398 sha1=$(git rev-parse "$ref"^0)
399 rewritten=$(map $sha1)
400
401 test $sha1 = "$rewritten" &&
402 warn "WARNING: Ref '$ref' is unchanged" &&
403 continue
404
405 case "$rewritten" in
406 '')
407 echo "Ref '$ref' was deleted"
408 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
409 die "Could not delete $ref"
Johannes Schindelin98409062007-06-05 16:58:13 +0100410 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100411 $_x40)
412 echo "Ref '$ref' was rewritten"
Thomas Rast261044e2008-08-08 01:50:31 +0200413 if ! git update-ref -m "filter-branch: rewrite" \
414 "$ref" $rewritten $sha1 2>/dev/null; then
415 if test $(git cat-file -t "$ref") = tag; then
416 if test -z "$filter_tag_name"; then
417 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
418 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
419 fi
420 else
421 die "Could not rewrite $ref"
422 fi
423 fi
Johannes Schindelin98409062007-06-05 16:58:13 +0100424 ;;
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100425 *)
426 # NEEDSWORK: possibly add -Werror, making this an error
427 warn "WARNING: '$ref' was rewritten into multiple commits:"
428 warn "$rewritten"
429 warn "WARNING: Ref '$ref' points to the first one now."
430 rewritten=$(echo "$rewritten" | head -n 1)
431 git update-ref -m "filter-branch: rewrite to first" \
432 "$ref" $rewritten $sha1 ||
433 die "Could not rewrite $ref"
434 ;;
435 esac
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500436 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
437 exit
Johannes Schindelindfd05e32007-07-23 18:34:13 +0100438done < "$tempdir"/heads
439
440# TODO: This should possibly go, with the semantics that all positive given
441# refs are updated, and their original heads stored in refs/original/
442# Filter tags
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100443
444if [ "$filter_tag_name" ]; then
Junio C Hamano5be60072007-07-02 22:52:14 -0700445 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100446 while read sha1 type ref; do
447 ref="${ref#refs/tags/}"
448 # XXX: Rewrite tagged trees as well?
449 if [ "$type" != "commit" -a "$type" != "tag" ]; then
450 continue;
451 fi
452
453 if [ "$type" = "tag" ]; then
454 # Dereference to a commit
455 sha1t="$sha1"
Dan Loewenherz7bd93c12009-04-22 21:46:02 -0400456 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100457 fi
458
459 [ -f "../map/$sha1" ] || continue
460 new_sha1="$(cat "../map/$sha1")"
Johannes Schindelin3addc942007-11-28 15:56:11 +0000461 GIT_COMMIT="$sha1"
462 export GIT_COMMIT
Johannes Schindelin8c1ce0f2007-07-04 15:36:01 +0100463 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
464 die "tag name filter failed: $filter_tag_name"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100465
466 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
467
468 if [ "$type" = "tag" ]; then
Johannes Sixta9da1662008-08-21 16:45:11 +0200469 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
470 "$new_sha1" "$new_ref"
471 git cat-file tag "$ref" |
Brandon Casey1bf65512008-03-26 10:47:09 -0500472 sed -n \
Stephen Boyd9524cf22010-01-26 15:08:31 -0800473 -e '1,/^$/{
Johannes Sixta9da1662008-08-21 16:45:11 +0200474 /^object /d
475 /^type /d
476 /^tag /d
Stephen Boyd9524cf22010-01-26 15:08:31 -0800477 }' \
Brandon Casey1bf65512008-03-26 10:47:09 -0500478 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
Johannes Sixta9da1662008-08-21 16:45:11 +0200479 -e 'p' ) |
Brandon Casey1bf65512008-03-26 10:47:09 -0500480 git mktag) ||
481 die "Could not create new tag object for $ref"
482 if git cat-file tag "$ref" | \
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800483 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
Brandon Casey1bf65512008-03-26 10:47:09 -0500484 then
485 warn "gpg signature stripped from tag object $sha1t"
486 fi
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100487 fi
488
Johannes Schindelinaf580e92007-07-18 14:17:43 +0100489 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
490 die "Could not write tag $new_ref"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100491 done
492fi
493
494cd ../..
495rm -rf "$tempdir"
Johannes Schindelin6f6826c2007-06-03 01:31:28 +0100496
Brandon Caseydef16e72008-01-28 15:16:02 -0600497trap - 0
498
Eric Kidd9273b562009-02-03 13:27:03 -0500499unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
500test -z "$ORIG_GIT_DIR" || {
501 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
502}
503test -z "$ORIG_GIT_WORK_TREE" || {
504 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
505 export GIT_WORK_TREE
506}
507test -z "$ORIG_GIT_INDEX_FILE" || {
508 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
509 export GIT_INDEX_FILE
510}
511
Petr Baudisa4661b02008-07-24 00:15:36 +0200512if [ "$(is_bare_repository)" = false ]; then
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500513 git read-tree -u -m HEAD || exit
Petr Baudisa4661b02008-07-24 00:15:36 +0200514fi
Johannes Schindelin46eb4492007-10-17 03:23:10 +0100515
Eric Kidd0ea29cc2009-02-11 16:10:41 -0500516exit 0