]> git.ipfire.org Git - thirdparty/git.git/blob - git-filter-branch.sh
filter-branch: make output nicer
[thirdparty/git.git] / git-filter-branch.sh
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 #
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.
10
11 set -e
12
13 USAGE="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]"
14 . git-sh-setup
15
16 map()
17 {
18 # if it was not rewritten, take the original
19 if test -r "$workdir/../map/$1"
20 then
21 cat "$workdir/../map/$1"
22 else
23 echo "$1"
24 fi
25 }
26
27 # When piped a commit, output a script to set the ident of either
28 # "author" or "committer
29
30 set_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
60 tempdir=.git-rewrite
61 filter_env=
62 filter_tree=
63 filter_index=
64 filter_parent=
65 filter_msg=cat
66 filter_commit='git commit-tree "$@"'
67 filter_tag_name=
68 filter_subdir=
69 while case "$#" in 0) usage;; esac
70 do
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 ;;
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 ;;
114 --subdirectory-filter)
115 filter_subdir="$OPTARG"
116 ;;
117 *)
118 usage
119 ;;
120 esac
121 done
122
123 dstbranch="$1"
124 shift
125 test -n "$dstbranch" || die "missing branch name"
126 git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
127 die "branch $dstbranch already exists"
128
129 test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
130 mkdir -p "$tempdir/t"
131 cd "$tempdir/t"
132 workdir="$(pwd)"
133
134 case "$GIT_DIR" in
135 /*)
136 ;;
137 *)
138 GIT_DIR="$(pwd)/../../$GIT_DIR"
139 ;;
140 esac
141 export GIT_DIR GIT_WORK_TREE=.
142
143 export GIT_INDEX_FILE="$(pwd)/../index"
144 git read-tree # seed the index file
145
146 ret=0
147
148
149 mkdir ../map # map old->new commit ids for rewriting parents
150
151 case "$filter_subdir" in
152 "")
153 git rev-list --reverse --topo-order --default HEAD \
154 --parents "$@"
155 ;;
156 *)
157 git rev-list --reverse --topo-order --default HEAD \
158 --parents --full-history "$@" -- "$filter_subdir"
159 esac > ../revs
160 commits=$(cat ../revs | wc -l | tr -d " ")
161
162 test $commits -eq 0 && die "Found nothing to rewrite"
163
164 i=0
165 while read commit parents; do
166 i=$(($i+1))
167 printf "\rRewrite $commit ($i/$commits)"
168
169 case "$filter_subdir" in
170 "")
171 git read-tree -i -m $commit
172 ;;
173 *)
174 git read-tree -i -m $commit:"$filter_subdir"
175 esac
176
177 export GIT_COMMIT=$commit
178 git cat-file commit "$commit" >../commit
179
180 eval "$(set_ident AUTHOR <../commit)"
181 eval "$(set_ident COMMITTER <../commit)"
182 eval "$filter_env" < /dev/null
183
184 if [ "$filter_tree" ]; then
185 git checkout-index -f -u -a
186 # files that $commit removed are now still in the working tree;
187 # remove them, else they would be added again
188 git ls-files -z --others | xargs -0 rm -f
189 eval "$filter_tree" < /dev/null
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
194 fi
195
196 eval "$filter_index" < /dev/null
197
198 parentstr=
199 for parent in $parents; do
200 for reparent in $(map "$parent"); do
201 parentstr="$parentstr -p $reparent"
202 done
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" | \
210 sh -c "$filter_commit" "git commit-tree" $(git write-tree) \
211 $parentstr > ../map/$commit
212 done <../revs
213
214 src_head=$(tail -n 1 ../revs | sed -e 's/ .*//')
215 target_head=$(head -n 1 ../map/$src_head)
216 case "$target_head" in
217 '')
218 echo Nothing rewritten
219 ;;
220 *)
221 git update-ref refs/heads/"$dstbranch" $target_head
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 ;;
228 esac
229
230 if [ "$filter_tag_name" ]; then
231 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
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"
242 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
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
257 git update-ref "refs/tags/$new_ref" "$new_sha1"
258 done
259 fi
260
261 cd ../..
262 rm -rf "$tempdir"
263 printf "\nRewritten history saved to the $dstbranch branch\n"
264
265 exit $ret