filter-branch.sh: remove temporary directory on failure
[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 # The following functions will also be available in the commit filter:
12
13 functions=$(cat << \EOF
14 warn () {
15         echo "$*" >&2
16 }
17
18 map()
19 {
20         # if it was not rewritten, take the original
21         if test -r "$workdir/../map/$1"
22         then
23                 cat "$workdir/../map/$1"
24         else
25                 echo "$1"
26         fi
27 }
28
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
31
32 skip_commit()
33 {
34         shift;
35         while [ -n "$1" ];
36         do
37                 shift;
38                 map "$1";
39                 shift;
40         done;
41 }
42
43 # override die(): this version puts in an extra line break, so that
44 # the progress is still visible
45
46 die()
47 {
48         echo >&2
49         echo "$*" >&2
50         exit 1
51 }
52 EOF
53 )
54
55 eval "$functions"
56
57 # When piped a commit, output a script to set the ident of either
58 # "author" or "committer
59
60 set_ident () {
61         lid="$(echo "$1" | tr "A-Z" "a-z")"
62         uid="$(echo "$1" | tr "a-z" "A-Z")"
63         pick_id_script='
64                 /^'$lid' /{
65                         s/'\''/'\''\\'\'\''/g
66                         h
67                         s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
68                         s/'\''/'\''\'\'\''/g
69                         s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
70
71                         g
72                         s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
73                         s/'\''/'\''\'\'\''/g
74                         s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
75
76                         g
77                         s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
78                         s/'\''/'\''\'\'\''/g
79                         s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
80
81                         q
82                 }
83         '
84
85         LANG=C LC_ALL=C sed -ne "$pick_id_script"
86         # Ensure non-empty id name.
87         echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
88 }
89
90 USAGE="[--env-filter <command>] [--tree-filter <command>] \
91 [--index-filter <command>] [--parent-filter <command>] \
92 [--msg-filter <command>] [--commit-filter <command>] \
93 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
94 [--original <namespace>] [-d <directory>] [-f | --force] \
95 [<rev-list options>...]"
96
97 OPTIONS_SPEC=
98 . git-sh-setup
99
100 git diff-files --quiet &&
101         git diff-index --cached --quiet HEAD -- ||
102         die "Cannot rewrite branch(es) with a dirty working directory."
103
104 tempdir=.git-rewrite
105 filter_env=
106 filter_tree=
107 filter_index=
108 filter_parent=
109 filter_msg=cat
110 filter_commit='git commit-tree "$@"'
111 filter_tag_name=
112 filter_subdir=
113 orig_namespace=refs/original/
114 force=
115 while :
116 do
117         test $# = 0 && usage
118         case "$1" in
119         --)
120                 shift
121                 break
122                 ;;
123         --force|-f)
124                 shift
125                 force=t
126                 continue
127                 ;;
128         -*)
129                 ;;
130         *)
131                 break;
132         esac
133
134         # all switches take one argument
135         ARG="$1"
136         case "$#" in 1) usage ;; esac
137         shift
138         OPTARG="$1"
139         shift
140
141         case "$ARG" in
142         -d)
143                 tempdir="$OPTARG"
144                 ;;
145         --env-filter)
146                 filter_env="$OPTARG"
147                 ;;
148         --tree-filter)
149                 filter_tree="$OPTARG"
150                 ;;
151         --index-filter)
152                 filter_index="$OPTARG"
153                 ;;
154         --parent-filter)
155                 filter_parent="$OPTARG"
156                 ;;
157         --msg-filter)
158                 filter_msg="$OPTARG"
159                 ;;
160         --commit-filter)
161                 filter_commit="$functions; $OPTARG"
162                 ;;
163         --tag-name-filter)
164                 filter_tag_name="$OPTARG"
165                 ;;
166         --subdirectory-filter)
167                 filter_subdir="$OPTARG"
168                 ;;
169         --original)
170                 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
171                 ;;
172         *)
173                 usage
174                 ;;
175         esac
176 done
177
178 case "$force" in
179 t)
180         rm -rf "$tempdir"
181 ;;
182 '')
183         test -d "$tempdir" &&
184                 die "$tempdir already exists, please remove it"
185 esac
186 mkdir -p "$tempdir/t" &&
187 tempdir="$(cd "$tempdir"; pwd)" &&
188 cd "$tempdir/t" &&
189 workdir="$(pwd)" ||
190 die ""
191
192 # Remove tempdir on exit
193 trap 'cd ../..; rm -rf "$tempdir"' 0
194
195 # Make sure refs/original is empty
196 git for-each-ref > "$tempdir"/backup-refs
197 while read sha1 type name
198 do
199         case "$force,$name" in
200         ,$orig_namespace*)
201                 die "Namespace $orig_namespace not empty"
202         ;;
203         t,$orig_namespace*)
204                 git update-ref -d "$name" $sha1
205         ;;
206         esac
207 done < "$tempdir"/backup-refs
208
209 ORIG_GIT_DIR="$GIT_DIR"
210 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
211 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
212 GIT_WORK_TREE=.
213 export GIT_DIR GIT_WORK_TREE
214
215 # The refs should be updated if their heads were rewritten
216 git rev-parse --no-flags --revs-only --symbolic-full-name "$@" |
217 sed -e '/^^/d' >"$tempdir"/heads
218
219 test -s "$tempdir"/heads ||
220         die "Which ref do you want to rewrite?"
221
222 GIT_INDEX_FILE="$(pwd)/../index"
223 export GIT_INDEX_FILE
224 git read-tree || die "Could not seed the index"
225
226 ret=0
227
228 # map old->new commit ids for rewriting parents
229 mkdir ../map || die "Could not create map/ directory"
230
231 case "$filter_subdir" in
232 "")
233         git rev-list --reverse --topo-order --default HEAD \
234                 --parents "$@"
235         ;;
236 *)
237         git rev-list --reverse --topo-order --default HEAD \
238                 --parents --full-history "$@" -- "$filter_subdir"
239 esac > ../revs || die "Could not get the commits"
240 commits=$(wc -l <../revs | tr -d " ")
241
242 test $commits -eq 0 && die "Found nothing to rewrite"
243
244 # Rewrite the commits
245
246 i=0
247 while read commit parents; do
248         i=$(($i+1))
249         printf "\rRewrite $commit ($i/$commits)"
250
251         case "$filter_subdir" in
252         "")
253                 git read-tree -i -m $commit
254                 ;;
255         *)
256                 git read-tree -i -m $commit:"$filter_subdir"
257         esac || die "Could not initialize the index"
258
259         GIT_COMMIT=$commit
260         export GIT_COMMIT
261         git cat-file commit "$commit" >../commit ||
262                 die "Cannot read commit $commit"
263
264         eval "$(set_ident AUTHOR <../commit)" ||
265                 die "setting author failed for commit $commit"
266         eval "$(set_ident COMMITTER <../commit)" ||
267                 die "setting committer failed for commit $commit"
268         eval "$filter_env" < /dev/null ||
269                 die "env filter failed: $filter_env"
270
271         if [ "$filter_tree" ]; then
272                 git checkout-index -f -u -a ||
273                         die "Could not checkout the index"
274                 # files that $commit removed are now still in the working tree;
275                 # remove them, else they would be added again
276                 git ls-files -z --others | xargs -0 rm -f
277                 eval "$filter_tree" < /dev/null ||
278                         die "tree filter failed: $filter_tree"
279
280                 git diff-index -r $commit | cut -f 2- | tr '\012' '\000' | \
281                         xargs -0 git update-index --add --replace --remove
282                 git ls-files -z --others | \
283                         xargs -0 git update-index --add --replace --remove
284         fi
285
286         eval "$filter_index" < /dev/null ||
287                 die "index filter failed: $filter_index"
288
289         parentstr=
290         for parent in $parents; do
291                 for reparent in $(map "$parent"); do
292                         parentstr="$parentstr -p $reparent"
293                 done
294         done
295         if [ "$filter_parent" ]; then
296                 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
297                                 die "parent filter failed: $filter_parent"
298         fi
299
300         sed -e '1,/^$/d' <../commit | \
301                 eval "$filter_msg" > ../message ||
302                         die "msg filter failed: $filter_msg"
303         sh -c "$filter_commit" "git commit-tree" \
304                 $(git write-tree) $parentstr < ../message > ../map/$commit
305 done <../revs
306
307 # In case of a subdirectory filter, it is possible that a specified head
308 # is not in the set of rewritten commits, because it was pruned by the
309 # revision walker.  Fix it by mapping these heads to the next rewritten
310 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
311
312 # NEEDSWORK: we should sort the unmapped refs topologically first
313 while read ref
314 do
315         sha1=$(git rev-parse "$ref"^0)
316         test -f "$workdir"/../map/$sha1 && continue
317         # Assign the boundarie(s) in the set of rewritten commits
318         # as the replacement commit(s).
319         # (This would look a bit nicer if --not --stdin worked.)
320         for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
321                 git rev-list $ref --boundary --stdin |
322                 sed -n "s/^-//p")
323         do
324                 map $p >> "$workdir"/../map/$sha1
325         done
326 done < "$tempdir"/heads
327
328 # Finally update the refs
329
330 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
331 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
332 echo
333 while read ref
334 do
335         # avoid rewriting a ref twice
336         test -f "$orig_namespace$ref" && continue
337
338         sha1=$(git rev-parse "$ref"^0)
339         rewritten=$(map $sha1)
340
341         test $sha1 = "$rewritten" &&
342                 warn "WARNING: Ref '$ref' is unchanged" &&
343                 continue
344
345         case "$rewritten" in
346         '')
347                 echo "Ref '$ref' was deleted"
348                 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
349                         die "Could not delete $ref"
350         ;;
351         $_x40)
352                 echo "Ref '$ref' was rewritten"
353                 git update-ref -m "filter-branch: rewrite" \
354                                 "$ref" $rewritten $sha1 ||
355                         die "Could not rewrite $ref"
356         ;;
357         *)
358                 # NEEDSWORK: possibly add -Werror, making this an error
359                 warn "WARNING: '$ref' was rewritten into multiple commits:"
360                 warn "$rewritten"
361                 warn "WARNING: Ref '$ref' points to the first one now."
362                 rewritten=$(echo "$rewritten" | head -n 1)
363                 git update-ref -m "filter-branch: rewrite to first" \
364                                 "$ref" $rewritten $sha1 ||
365                         die "Could not rewrite $ref"
366         ;;
367         esac
368         git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
369 done < "$tempdir"/heads
370
371 # TODO: This should possibly go, with the semantics that all positive given
372 #       refs are updated, and their original heads stored in refs/original/
373 # Filter tags
374
375 if [ "$filter_tag_name" ]; then
376         git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
377         while read sha1 type ref; do
378                 ref="${ref#refs/tags/}"
379                 # XXX: Rewrite tagged trees as well?
380                 if [ "$type" != "commit" -a "$type" != "tag" ]; then
381                         continue;
382                 fi
383
384                 if [ "$type" = "tag" ]; then
385                         # Dereference to a commit
386                         sha1t="$sha1"
387                         sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
388                 fi
389
390                 [ -f "../map/$sha1" ] || continue
391                 new_sha1="$(cat "../map/$sha1")"
392                 GIT_COMMIT="$sha1"
393                 export GIT_COMMIT
394                 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
395                         die "tag name filter failed: $filter_tag_name"
396
397                 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
398
399                 if [ "$type" = "tag" ]; then
400                         # Warn that we are not rewriting the tag object itself.
401                         warn "unreferencing tag object $sha1t"
402                 fi
403
404                 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
405                         die "Could not write tag $new_ref"
406         done
407 fi
408
409 cd ../..
410 rm -rf "$tempdir"
411
412 trap - 0
413
414 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
415 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
416 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
417         export GIT_WORK_TREE
418 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
419         export GIT_INDEX_FILE
420 git read-tree -u -m HEAD
421
422 exit $ret