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