builtin-commit: use reduce_heads() only when appropriate
[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 if [ "$(is_bare_repository)" = false ]; then
101         git diff-files --quiet &&
102         git diff-index --cached --quiet HEAD -- ||
103         die "Cannot rewrite branch(es) with a dirty working directory."
104 fi
105
106 tempdir=.git-rewrite
107 filter_env=
108 filter_tree=
109 filter_index=
110 filter_parent=
111 filter_msg=cat
112 filter_commit='git commit-tree "$@"'
113 filter_tag_name=
114 filter_subdir=
115 orig_namespace=refs/original/
116 force=
117 while :
118 do
119         case "$1" in
120         --)
121                 shift
122                 break
123                 ;;
124         --force|-f)
125                 shift
126                 force=t
127                 continue
128                 ;;
129         -*)
130                 ;;
131         *)
132                 break;
133         esac
134
135         # all switches take one argument
136         ARG="$1"
137         case "$#" in 1) usage ;; esac
138         shift
139         OPTARG="$1"
140         shift
141
142         case "$ARG" in
143         -d)
144                 tempdir="$OPTARG"
145                 ;;
146         --env-filter)
147                 filter_env="$OPTARG"
148                 ;;
149         --tree-filter)
150                 filter_tree="$OPTARG"
151                 ;;
152         --index-filter)
153                 filter_index="$OPTARG"
154                 ;;
155         --parent-filter)
156                 filter_parent="$OPTARG"
157                 ;;
158         --msg-filter)
159                 filter_msg="$OPTARG"
160                 ;;
161         --commit-filter)
162                 filter_commit="$functions; $OPTARG"
163                 ;;
164         --tag-name-filter)
165                 filter_tag_name="$OPTARG"
166                 ;;
167         --subdirectory-filter)
168                 filter_subdir="$OPTARG"
169                 ;;
170         --original)
171                 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
172                 ;;
173         *)
174                 usage
175                 ;;
176         esac
177 done
178
179 case "$force" in
180 t)
181         rm -rf "$tempdir"
182 ;;
183 '')
184         test -d "$tempdir" &&
185                 die "$tempdir already exists, please remove it"
186 esac
187 mkdir -p "$tempdir/t" &&
188 tempdir="$(cd "$tempdir"; pwd)" &&
189 cd "$tempdir/t" &&
190 workdir="$(pwd)" ||
191 die ""
192
193 # Remove tempdir on exit
194 trap 'cd ../..; rm -rf "$tempdir"' 0
195
196 # Make sure refs/original is empty
197 git for-each-ref > "$tempdir"/backup-refs
198 while read sha1 type name
199 do
200         case "$force,$name" in
201         ,$orig_namespace*)
202                 die "Namespace $orig_namespace not empty"
203         ;;
204         t,$orig_namespace*)
205                 git update-ref -d "$name" $sha1
206         ;;
207         esac
208 done < "$tempdir"/backup-refs
209
210 ORIG_GIT_DIR="$GIT_DIR"
211 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
212 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
213 GIT_WORK_TREE=.
214 export GIT_DIR GIT_WORK_TREE
215
216 # The refs should be updated if their heads were rewritten
217 git rev-parse --no-flags --revs-only --symbolic-full-name --default HEAD "$@" |
218 sed -e '/^^/d' >"$tempdir"/heads
219
220 test -s "$tempdir"/heads ||
221         die "Which ref do you want to rewrite?"
222
223 GIT_INDEX_FILE="$(pwd)/../index"
224 export GIT_INDEX_FILE
225 git read-tree || die "Could not seed the index"
226
227 ret=0
228
229 # map old->new commit ids for rewriting parents
230 mkdir ../map || die "Could not create map/ directory"
231
232 case "$filter_subdir" in
233 "")
234         git rev-list --reverse --topo-order --default HEAD \
235                 --parents --simplify-merges "$@"
236         ;;
237 *)
238         git rev-list --reverse --topo-order --default HEAD \
239                 --parents --simplify-merges "$@" -- "$filter_subdir"
240 esac > ../revs || die "Could not get the commits"
241 commits=$(wc -l <../revs | tr -d " ")
242
243 test $commits -eq 0 && die "Found nothing to rewrite"
244
245 # Rewrite the commits
246
247 i=0
248 while read commit parents; do
249         i=$(($i+1))
250         printf "\rRewrite $commit ($i/$commits)"
251
252         case "$filter_subdir" in
253         "")
254                 git read-tree -i -m $commit
255                 ;;
256         *)
257                 # The commit may not have the subdirectory at all
258                 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
259                         if ! git rev-parse --verify $commit:"$filter_subdir" 2>/dev/null
260                         then
261                                 rm -f "$GIT_INDEX_FILE"
262                         else
263                                 echo >&2 "$err"
264                                 false
265                         fi
266                 }
267         esac || die "Could not initialize the index"
268
269         GIT_COMMIT=$commit
270         export GIT_COMMIT
271         git cat-file commit "$commit" >../commit ||
272                 die "Cannot read commit $commit"
273
274         eval "$(set_ident AUTHOR <../commit)" ||
275                 die "setting author failed for commit $commit"
276         eval "$(set_ident COMMITTER <../commit)" ||
277                 die "setting committer failed for commit $commit"
278         eval "$filter_env" < /dev/null ||
279                 die "env filter failed: $filter_env"
280
281         if [ "$filter_tree" ]; then
282                 git checkout-index -f -u -a ||
283                         die "Could not checkout the index"
284                 # files that $commit removed are now still in the working tree;
285                 # remove them, else they would be added again
286                 git clean -d -q -f -x
287                 eval "$filter_tree" < /dev/null ||
288                         die "tree filter failed: $filter_tree"
289
290                 (
291                         git diff-index -r --name-only $commit
292                         git ls-files --others
293                 ) |
294                 git update-index --add --replace --remove --stdin
295         fi
296
297         eval "$filter_index" < /dev/null ||
298                 die "index filter failed: $filter_index"
299
300         parentstr=
301         for parent in $parents; do
302                 for reparent in $(map "$parent"); do
303                         parentstr="$parentstr -p $reparent"
304                 done
305         done
306         if [ "$filter_parent" ]; then
307                 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
308                                 die "parent filter failed: $filter_parent"
309         fi
310
311         sed -e '1,/^$/d' <../commit | \
312                 eval "$filter_msg" > ../message ||
313                         die "msg filter failed: $filter_msg"
314         @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
315                 $(git write-tree) $parentstr < ../message > ../map/$commit
316 done <../revs
317
318 # In case of a subdirectory filter, it is possible that a specified head
319 # is not in the set of rewritten commits, because it was pruned by the
320 # revision walker.  Fix it by mapping these heads to the unique nearest
321 # ancestor that survived the pruning.
322
323 if test "$filter_subdir"
324 then
325         while read ref
326         do
327                 sha1=$(git rev-parse "$ref"^0)
328                 test -f "$workdir"/../map/$sha1 && continue
329                 ancestor=$(git rev-list --simplify-merges -1 \
330                                 $ref -- "$filter_subdir")
331                 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
332         done < "$tempdir"/heads
333 fi
334
335 # Finally update the refs
336
337 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
338 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
339 echo
340 while read ref
341 do
342         # avoid rewriting a ref twice
343         test -f "$orig_namespace$ref" && continue
344
345         sha1=$(git rev-parse "$ref"^0)
346         rewritten=$(map $sha1)
347
348         test $sha1 = "$rewritten" &&
349                 warn "WARNING: Ref '$ref' is unchanged" &&
350                 continue
351
352         case "$rewritten" in
353         '')
354                 echo "Ref '$ref' was deleted"
355                 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
356                         die "Could not delete $ref"
357         ;;
358         $_x40)
359                 echo "Ref '$ref' was rewritten"
360                 if ! git update-ref -m "filter-branch: rewrite" \
361                                         "$ref" $rewritten $sha1 2>/dev/null; then
362                         if test $(git cat-file -t "$ref") = tag; then
363                                 if test -z "$filter_tag_name"; then
364                                         warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
365                                         warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
366                                 fi
367                         else
368                                 die "Could not rewrite $ref"
369                         fi
370                 fi
371         ;;
372         *)
373                 # NEEDSWORK: possibly add -Werror, making this an error
374                 warn "WARNING: '$ref' was rewritten into multiple commits:"
375                 warn "$rewritten"
376                 warn "WARNING: Ref '$ref' points to the first one now."
377                 rewritten=$(echo "$rewritten" | head -n 1)
378                 git update-ref -m "filter-branch: rewrite to first" \
379                                 "$ref" $rewritten $sha1 ||
380                         die "Could not rewrite $ref"
381         ;;
382         esac
383         git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
384 done < "$tempdir"/heads
385
386 # TODO: This should possibly go, with the semantics that all positive given
387 #       refs are updated, and their original heads stored in refs/original/
388 # Filter tags
389
390 if [ "$filter_tag_name" ]; then
391         git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
392         while read sha1 type ref; do
393                 ref="${ref#refs/tags/}"
394                 # XXX: Rewrite tagged trees as well?
395                 if [ "$type" != "commit" -a "$type" != "tag" ]; then
396                         continue;
397                 fi
398
399                 if [ "$type" = "tag" ]; then
400                         # Dereference to a commit
401                         sha1t="$sha1"
402                         sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
403                 fi
404
405                 [ -f "../map/$sha1" ] || continue
406                 new_sha1="$(cat "../map/$sha1")"
407                 GIT_COMMIT="$sha1"
408                 export GIT_COMMIT
409                 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
410                         die "tag name filter failed: $filter_tag_name"
411
412                 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
413
414                 if [ "$type" = "tag" ]; then
415                         new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
416                                                 "$new_sha1" "$new_ref"
417                                 git cat-file tag "$ref" |
418                                 sed -n \
419                                     -e "1,/^$/{
420                                           /^object /d
421                                           /^type /d
422                                           /^tag /d
423                                         }" \
424                                     -e '/^-----BEGIN PGP SIGNATURE-----/q' \
425                                     -e 'p' ) |
426                                 git mktag) ||
427                                 die "Could not create new tag object for $ref"
428                         if git cat-file tag "$ref" | \
429                            grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
430                         then
431                                 warn "gpg signature stripped from tag object $sha1t"
432                         fi
433                 fi
434
435                 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
436                         die "Could not write tag $new_ref"
437         done
438 fi
439
440 cd ../..
441 rm -rf "$tempdir"
442
443 trap - 0
444
445 if [ "$(is_bare_repository)" = false ]; then
446         unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
447         test -z "$ORIG_GIT_DIR" || {
448                 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
449         }
450         test -z "$ORIG_GIT_WORK_TREE" || {
451                 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
452                 export GIT_WORK_TREE
453         }
454         test -z "$ORIG_GIT_INDEX_FILE" || {
455                 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
456                 export GIT_INDEX_FILE
457         }
458         git read-tree -u -m HEAD
459 fi
460
461 exit $ret