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