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