Alter git-checkout reflog message to include "from" branch
[git/git.git] / git-filter-branch.sh
CommitLineData
6f6826c5
JS
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#
c401b33c
JS
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.
6f6826c5
JS
10
11set -e
12
2766ce28 13USAGE="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]"
6f6826c5
JS
14. git-sh-setup
15
16map()
17{
3520e1e8 18 # if it was not rewritten, take the original
c57a3494
JS
19 if test -r "$workdir/../map/$1"
20 then
21 cat "$workdir/../map/$1"
22 else
23 echo "$1"
24 fi
6f6826c5
JS
25}
26
27# When piped a commit, output a script to set the ident of either
28# "author" or "committer
29
30set_ident () {
31 lid="$(echo "$1" | tr "A-Z" "a-z")"
32 uid="$(echo "$1" | tr "a-z" "A-Z")"
33 pick_id_script='
34 /^'$lid' /{
35 s/'\''/'\''\\'\'\''/g
36 h
37 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
38 s/'\''/'\''\'\'\''/g
39 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
40
41 g
42 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
43 s/'\''/'\''\'\'\''/g
44 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
45
46 g
47 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
48 s/'\''/'\''\'\'\''/g
49 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
50
51 q
52 }
53 '
54
55 LANG=C LC_ALL=C sed -ne "$pick_id_script"
56 # Ensure non-empty id name.
57 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
58}
59
6f6826c5 60tempdir=.git-rewrite
6f6826c5
JS
61filter_env=
62filter_tree=
63filter_index=
64filter_parent=
65filter_msg=cat
5be60078 66filter_commit='git commit-tree "$@"'
6f6826c5 67filter_tag_name=
685ef546 68filter_subdir=
6f6826c5
JS
69while case "$#" in 0) usage;; esac
70do
71 case "$1" in
72 --)
73 shift
74 break
75 ;;
76 -*)
77 ;;
78 *)
79 break;
80 esac
81
82 # all switches take one argument
83 ARG="$1"
84 case "$#" in 1) usage ;; esac
85 shift
86 OPTARG="$1"
87 shift
88
89 case "$ARG" in
90 -d)
91 tempdir="$OPTARG"
92 ;;
6f6826c5
JS
93 --env-filter)
94 filter_env="$OPTARG"
95 ;;
96 --tree-filter)
97 filter_tree="$OPTARG"
98 ;;
99 --index-filter)
100 filter_index="$OPTARG"
101 ;;
102 --parent-filter)
103 filter_parent="$OPTARG"
104 ;;
105 --msg-filter)
106 filter_msg="$OPTARG"
107 ;;
108 --commit-filter)
109 filter_commit="$OPTARG"
110 ;;
111 --tag-name-filter)
112 filter_tag_name="$OPTARG"
113 ;;
685ef546
JS
114 --subdirectory-filter)
115 filter_subdir="$OPTARG"
116 ;;
6f6826c5
JS
117 *)
118 usage
119 ;;
120 esac
121done
122
123dstbranch="$1"
2766ce28 124shift
6f6826c5 125test -n "$dstbranch" || die "missing branch name"
5be60078 126git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
6f6826c5
JS
127 die "branch $dstbranch already exists"
128
129test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
130mkdir -p "$tempdir/t"
131cd "$tempdir/t"
132workdir="$(pwd)"
133
134case "$GIT_DIR" in
135/*)
136 ;;
137*)
9489d0f1 138 GIT_DIR="$(pwd)/../../$GIT_DIR"
6f6826c5
JS
139 ;;
140esac
9489d0f1 141export GIT_DIR GIT_WORK_TREE=.
6f6826c5
JS
142
143export GIT_INDEX_FILE="$(pwd)/../index"
5be60078 144git read-tree # seed the index file
6f6826c5
JS
145
146ret=0
147
148
149mkdir ../map # map old->new commit ids for rewriting parents
150
685ef546
JS
151case "$filter_subdir" in
152"")
5be60078 153 git rev-list --reverse --topo-order --default HEAD \
813b4734 154 --parents "$@"
685ef546
JS
155 ;;
156*)
5be60078 157 git rev-list --reverse --topo-order --default HEAD \
cfabd6ee 158 --parents --full-history "$@" -- "$filter_subdir"
685ef546 159esac > ../revs
6f6826c5
JS
160commits=$(cat ../revs | wc -l | tr -d " ")
161
162test $commits -eq 0 && die "Found nothing to rewrite"
163
164i=0
813b4734 165while read commit parents; do
c12764b8 166 i=$(($i+1))
5efb48b5 167 printf "\rRewrite $commit ($i/$commits)"
6f6826c5 168
685ef546
JS
169 case "$filter_subdir" in
170 "")
5be60078 171 git read-tree -i -m $commit
685ef546
JS
172 ;;
173 *)
5be60078 174 git read-tree -i -m $commit:"$filter_subdir"
685ef546 175 esac
6f6826c5
JS
176
177 export GIT_COMMIT=$commit
5be60078 178 git cat-file commit "$commit" >../commit
6f6826c5
JS
179
180 eval "$(set_ident AUTHOR <../commit)"
181 eval "$(set_ident COMMITTER <../commit)"
350d8575 182 eval "$filter_env" < /dev/null
6f6826c5
JS
183
184 if [ "$filter_tree" ]; then
5be60078 185 git checkout-index -f -u -a
6f6826c5
JS
186 # files that $commit removed are now still in the working tree;
187 # remove them, else they would be added again
5be60078 188 git ls-files -z --others | xargs -0 rm -f
350d8575 189 eval "$filter_tree" < /dev/null
5be60078
JH
190 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
191 xargs -0 git update-index --add --replace --remove
192 git ls-files -z --others | \
193 xargs -0 git update-index --add --replace --remove
6f6826c5
JS
194 fi
195
350d8575 196 eval "$filter_index" < /dev/null
6f6826c5
JS
197
198 parentstr=
813b4734 199 for parent in $parents; do
3520e1e8
JS
200 for reparent in $(map "$parent"); do
201 parentstr="$parentstr -p $reparent"
202 done
6f6826c5
JS
203 done
204 if [ "$filter_parent" ]; then
205 parentstr="$(echo "$parentstr" | eval "$filter_parent")"
206 fi
207
208 sed -e '1,/^$/d' <../commit | \
209 eval "$filter_msg" | \
5efb48b5
JS
210 sh -c "$filter_commit" "git commit-tree" $(git write-tree) \
211 $parentstr > ../map/$commit
6f6826c5
JS
212done <../revs
213
813b4734 214src_head=$(tail -n 1 ../revs | sed -e 's/ .*//')
98409060
JS
215target_head=$(head -n 1 ../map/$src_head)
216case "$target_head" in
217'')
218 echo Nothing rewritten
219 ;;
220*)
5be60078 221 git update-ref refs/heads/"$dstbranch" $target_head
98409060
JS
222 if [ $(cat ../map/$src_head | wc -l) -gt 1 ]; then
223 echo "WARNING: Your commit filter caused the head commit to expand to several rewritten commits. Only the first such commit was recorded as the current $dstbranch head but you will need to resolve the situation now (probably by manually merging the other commits). These are all the commits:" >&2
224 sed 's/^/ /' ../map/$src_head >&2
225 ret=1
226 fi
227 ;;
228esac
6f6826c5
JS
229
230if [ "$filter_tag_name" ]; then
5be60078 231 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5
JS
232 while read sha1 type ref; do
233 ref="${ref#refs/tags/}"
234 # XXX: Rewrite tagged trees as well?
235 if [ "$type" != "commit" -a "$type" != "tag" ]; then
236 continue;
237 fi
238
239 if [ "$type" = "tag" ]; then
240 # Dereference to a commit
241 sha1t="$sha1"
5be60078 242 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
6f6826c5
JS
243 fi
244
245 [ -f "../map/$sha1" ] || continue
246 new_sha1="$(cat "../map/$sha1")"
247 export GIT_COMMIT="$sha1"
248 new_ref="$(echo "$ref" | eval "$filter_tag_name")"
249
250 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
251
252 if [ "$type" = "tag" ]; then
253 # Warn that we are not rewriting the tag object itself.
254 warn "unreferencing tag object $sha1t"
255 fi
256
5be60078 257 git update-ref "refs/tags/$new_ref" "$new_sha1"
6f6826c5
JS
258 done
259fi
260
261cd ../..
262rm -rf "$tempdir"
5efb48b5 263printf "\nRewritten history saved to the $dstbranch branch\n"
6f6826c5
JS
264
265exit $ret