rev-parse --verify: do not output anything on error
[git/git-bigfiles.git] / git-filter-branch.sh
blob333f6a8f3b783d46a0ce3b2f59bae152936cd11c
1 #!/bin/sh
3 # Rewrite revision history
4 # Copyright (c) Petr Baudis, 2006
5 # Minimal changes to "port" it to core-git (c) Johannes Schindelin, 2007
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.
11 # The following functions will also be available in the commit filter:
13 functions=$(cat << \EOF
14 warn () {
15 echo "$*" >&2
18 map()
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"
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
32 skip_commit()
34 shift;
35 while [ -n "$1" ];
37 shift;
38 map "$1";
39 shift;
40 done;
43 # override die(): this version puts in an extra line break, so that
44 # the progress is still visible
46 die()
48 echo >&2
49 echo "$*" >&2
50 exit 1
52 EOF
55 eval "$functions"
57 # When piped a commit, output a script to set the ident of either
58 # "author" or "committer
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
67 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
68 s/'\''/'\''\'\'\''/g
69 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
72 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
73 s/'\''/'\''\'\'\''/g
74 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
77 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
78 s/'\''/'\''\'\'\''/g
79 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
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"
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>...]"
97 OPTIONS_SPEC=
98 . git-sh-setup
100 git diff-files --quiet &&
101 git diff-index --cached --quiet HEAD -- ||
102 die "Cannot rewrite branch(es) with a dirty working directory."
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 :
117 case "$1" in
119 shift
120 break
122 --force|-f)
123 shift
124 force=t
125 continue
130 break;
131 esac
133 # all switches take one argument
134 ARG="$1"
135 case "$#" in 1) usage ;; esac
136 shift
137 OPTARG="$1"
138 shift
140 case "$ARG" in
142 tempdir="$OPTARG"
144 --env-filter)
145 filter_env="$OPTARG"
147 --tree-filter)
148 filter_tree="$OPTARG"
150 --index-filter)
151 filter_index="$OPTARG"
153 --parent-filter)
154 filter_parent="$OPTARG"
156 --msg-filter)
157 filter_msg="$OPTARG"
159 --commit-filter)
160 filter_commit="$functions; $OPTARG"
162 --tag-name-filter)
163 filter_tag_name="$OPTARG"
165 --subdirectory-filter)
166 filter_subdir="$OPTARG"
168 --original)
169 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
172 usage
174 esac
175 done
177 case "$force" in
179 rm -rf "$tempdir"
182 test -d "$tempdir" &&
183 die "$tempdir already exists, please remove it"
184 esac
185 mkdir -p "$tempdir/t" &&
186 tempdir="$(cd "$tempdir"; pwd)" &&
187 cd "$tempdir/t" &&
188 workdir="$(pwd)" ||
189 die ""
191 # Remove tempdir on exit
192 trap 'cd ../..; rm -rf "$tempdir"' 0
194 # Make sure refs/original is empty
195 git for-each-ref > "$tempdir"/backup-refs
196 while read sha1 type name
198 case "$force,$name" in
199 ,$orig_namespace*)
200 die "Namespace $orig_namespace not empty"
202 t,$orig_namespace*)
203 git update-ref -d "$name" $sha1
205 esac
206 done < "$tempdir"/backup-refs
208 ORIG_GIT_DIR="$GIT_DIR"
209 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
210 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
211 GIT_WORK_TREE=.
212 export GIT_DIR GIT_WORK_TREE
214 # The refs should be updated if their heads were rewritten
215 git rev-parse --no-flags --revs-only --symbolic-full-name --default HEAD "$@" |
216 sed -e '/^^/d' >"$tempdir"/heads
218 test -s "$tempdir"/heads ||
219 die "Which ref do you want to rewrite?"
221 GIT_INDEX_FILE="$(pwd)/../index"
222 export GIT_INDEX_FILE
223 git read-tree || die "Could not seed the index"
225 ret=0
227 # map old->new commit ids for rewriting parents
228 mkdir ../map || die "Could not create map/ directory"
230 case "$filter_subdir" in
232 git rev-list --reverse --topo-order --default HEAD \
233 --parents "$@"
236 git rev-list --reverse --topo-order --default HEAD \
237 --parents --full-history "$@" -- "$filter_subdir"
238 esac > ../revs || die "Could not get the commits"
239 commits=$(wc -l <../revs | tr -d " ")
241 test $commits -eq 0 && die "Found nothing to rewrite"
243 # Rewrite the commits
246 while read commit parents; do
247 i=$(($i+1))
248 printf "\rRewrite $commit ($i/$commits)"
250 case "$filter_subdir" in
252 git read-tree -i -m $commit
255 # The commit may not have the subdirectory at all
256 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
257 if ! git rev-parse --verify $commit:"$filter_subdir" 2>/dev/null
258 then
259 rm -f "$GIT_INDEX_FILE"
260 else
261 echo >&2 "$err"
262 false
265 esac || die "Could not initialize the index"
267 GIT_COMMIT=$commit
268 export GIT_COMMIT
269 git cat-file commit "$commit" >../commit ||
270 die "Cannot read commit $commit"
272 eval "$(set_ident AUTHOR <../commit)" ||
273 die "setting author failed for commit $commit"
274 eval "$(set_ident COMMITTER <../commit)" ||
275 die "setting committer failed for commit $commit"
276 eval "$filter_env" < /dev/null ||
277 die "env filter failed: $filter_env"
279 if [ "$filter_tree" ]; then
280 git checkout-index -f -u -a ||
281 die "Could not checkout the index"
282 # files that $commit removed are now still in the working tree;
283 # remove them, else they would be added again
284 git clean -d -q -f -x
285 eval "$filter_tree" < /dev/null ||
286 die "tree filter failed: $filter_tree"
289 git diff-index -r --name-only $commit
290 git ls-files --others
292 git update-index --add --replace --remove --stdin
295 eval "$filter_index" < /dev/null ||
296 die "index filter failed: $filter_index"
298 parentstr=
299 for parent in $parents; do
300 for reparent in $(map "$parent"); do
301 parentstr="$parentstr -p $reparent"
302 done
303 done
304 if [ "$filter_parent" ]; then
305 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
306 die "parent filter failed: $filter_parent"
309 sed -e '1,/^$/d' <../commit | \
310 eval "$filter_msg" > ../message ||
311 die "msg filter failed: $filter_msg"
312 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
313 $(git write-tree) $parentstr < ../message > ../map/$commit
314 done <../revs
316 # In case of a subdirectory filter, it is possible that a specified head
317 # is not in the set of rewritten commits, because it was pruned by the
318 # revision walker. Fix it by mapping these heads to the next rewritten
319 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
321 # NEEDSWORK: we should sort the unmapped refs topologically first
322 while read ref
324 sha1=$(git rev-parse "$ref"^0)
325 test -f "$workdir"/../map/$sha1 && continue
326 # Assign the boundarie(s) in the set of rewritten commits
327 # as the replacement commit(s).
328 # (This would look a bit nicer if --not --stdin worked.)
329 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
330 git rev-list $ref --boundary --stdin |
331 sed -n "s/^-//p")
333 map $p >> "$workdir"/../map/$sha1
334 done
335 done < "$tempdir"/heads
337 # Finally update the refs
339 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
340 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
341 echo
342 while read ref
344 # avoid rewriting a ref twice
345 test -f "$orig_namespace$ref" && continue
347 sha1=$(git rev-parse "$ref"^0)
348 rewritten=$(map $sha1)
350 test $sha1 = "$rewritten" &&
351 warn "WARNING: Ref '$ref' is unchanged" &&
352 continue
354 case "$rewritten" in
356 echo "Ref '$ref' was deleted"
357 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
358 die "Could not delete $ref"
360 $_x40)
361 echo "Ref '$ref' was rewritten"
362 git update-ref -m "filter-branch: rewrite" \
363 "$ref" $rewritten $sha1 ||
364 die "Could not rewrite $ref"
367 # NEEDSWORK: possibly add -Werror, making this an error
368 warn "WARNING: '$ref' was rewritten into multiple commits:"
369 warn "$rewritten"
370 warn "WARNING: Ref '$ref' points to the first one now."
371 rewritten=$(echo "$rewritten" | head -n 1)
372 git update-ref -m "filter-branch: rewrite to first" \
373 "$ref" $rewritten $sha1 ||
374 die "Could not rewrite $ref"
376 esac
377 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
378 done < "$tempdir"/heads
380 # TODO: This should possibly go, with the semantics that all positive given
381 # refs are updated, and their original heads stored in refs/original/
382 # Filter tags
384 if [ "$filter_tag_name" ]; then
385 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
386 while read sha1 type ref; do
387 ref="${ref#refs/tags/}"
388 # XXX: Rewrite tagged trees as well?
389 if [ "$type" != "commit" -a "$type" != "tag" ]; then
390 continue;
393 if [ "$type" = "tag" ]; then
394 # Dereference to a commit
395 sha1t="$sha1"
396 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
399 [ -f "../map/$sha1" ] || continue
400 new_sha1="$(cat "../map/$sha1")"
401 GIT_COMMIT="$sha1"
402 export GIT_COMMIT
403 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
404 die "tag name filter failed: $filter_tag_name"
406 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
408 if [ "$type" = "tag" ]; then
409 new_sha1=$(git cat-file tag "$ref" |
410 sed -n \
411 -e "1,/^$/{
412 s/^object .*/object $new_sha1/
413 s/^type .*/type commit/
414 s/^tag .*/tag $new_ref/
415 }" \
416 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
417 -e 'p' |
418 git mktag) ||
419 die "Could not create new tag object for $ref"
420 if git cat-file tag "$ref" | \
421 grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
422 then
423 warn "gpg signature stripped from tag object $sha1t"
427 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
428 die "Could not write tag $new_ref"
429 done
432 cd ../..
433 rm -rf "$tempdir"
435 trap - 0
437 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
438 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
439 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
440 export GIT_WORK_TREE
441 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
442 export GIT_INDEX_FILE
443 git read-tree -u -m HEAD
445 exit $ret