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,
11 # The following functions will also be available in the commit filter:
13 functions
=$
(cat << \EOF
20 # if it was not rewritten, take the original
21 if test -r "$workdir/../map/$1"
23 cat "$workdir/../map/$1"
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
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
()
47 if test $# = 3 && test "$1" = $
(git rev-parse
"$3^{tree}"); then
53 # override die(): this version puts in an extra line break, so that
54 # the progress is still visible
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"
77 parse_ident_from_commit author AUTHOR committer COMMITTER
79 finish_ident COMMITTER
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>...]"
92 if [ "$(is_bare_repository)" = false
]; then
93 require_clean_work_tree
'rewrite branches'
105 orig_namespace
=refs
/original
/
122 # deprecated ($remap_to_ancestor is set now automatically)
138 # all switches take one argument
140 case "$#" in 1) usage
;; esac
153 filter_tree
="$OPTARG"
156 filter_index
="$OPTARG"
159 filter_parent
="$OPTARG"
165 filter_commit
="$functions; $OPTARG"
168 filter_tag_name
="$OPTARG"
170 --subdirectory-filter)
171 filter_subdir
="$OPTARG"
175 orig_namespace
=$
(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
183 case "$prune_empty,$filter_commit" in
185 filter_commit
='git commit-tree "$@"';;
187 filter_commit
="$functions;"' git_commit_non_empty_tree "$@"';;
191 die
"Cannot set --prune-empty and --commit-filter at the same time"
199 test -d "$tempdir" &&
200 die
"$tempdir already exists, please remove it"
203 mkdir
-p "$tempdir/t" &&
204 tempdir
="$(cd "$tempdir"; pwd)" &&
209 # Remove tempdir on exit
210 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
212 ORIG_GIT_DIR
="$GIT_DIR"
213 ORIG_GIT_WORK_TREE
="$GIT_WORK_TREE"
214 ORIG_GIT_INDEX_FILE
="$GIT_INDEX_FILE"
216 export GIT_DIR GIT_WORK_TREE
218 # Make sure refs/original is empty
219 git for-each-ref
> "$tempdir"/backup-refs ||
exit
220 while read sha1
type name
222 case "$force,$name" in
224 die
"Cannot create a new backup.
225 A previous backup already exists in $orig_namespace
226 Force overwriting the backup with -f"
229 git update-ref
-d "$name" $sha1
232 done < "$tempdir"/backup-refs
234 # The refs should be updated if their heads were rewritten
235 git rev-parse
--no-flags --revs-only --symbolic-full-name \
236 --default HEAD
"$@" > "$tempdir"/raw-heads ||
exit
237 sed -e '/^^/d' "$tempdir"/raw-heads
>"$tempdir"/heads
239 test -s "$tempdir"/heads ||
240 die
"Which ref do you want to rewrite?"
242 GIT_INDEX_FILE
="$(pwd)/../index"
243 export GIT_INDEX_FILE
245 # map old->new commit ids for rewriting parents
246 mkdir ..
/map || die
"Could not create map/ directory"
248 # we need "--" only if there are no path arguments in $@
249 nonrevs
=$
(git rev-parse
--no-revs "$@") ||
exit
250 if test -z "$nonrevs"
258 git rev-parse
--revs-only "$@" >..
/parse
260 case "$filter_subdir" in
262 eval set -- "$(git rev-parse --sq --no-revs "$@
")"
265 eval set -- "$(git rev-parse --sq --no-revs "$@
" $dashdash \
270 git rev-list
--reverse --topo-order --default HEAD \
271 --parents --simplify-merges --stdin "$@" <..
/parse
>..
/revs ||
272 die
"Could not get the commits"
273 commits
=$
(wc -l <..
/revs |
tr -d " ")
275 test $commits -eq 0 && die
"Found nothing to rewrite"
277 # Rewrite the commits
279 git_filter_branch__commit_count
=0
280 while read commit parents
; do
281 git_filter_branch__commit_count
=$
(($git_filter_branch__commit_count+1))
282 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
284 case "$filter_subdir" in
286 GIT_ALLOW_NULL_SHA1
=1 git read-tree
-i -m $commit
289 # The commit may not have the subdirectory at all
290 err
=$
(GIT_ALLOW_NULL_SHA1
=1 \
291 git read-tree
-i -m $commit:"$filter_subdir" 2>&1) ||
{
292 if ! git rev-parse
-q --verify $commit:"$filter_subdir"
294 rm -f "$GIT_INDEX_FILE"
300 esac || die
"Could not initialize the index"
304 git cat-file commit
"$commit" >..
/commit ||
305 die
"Cannot read commit $commit"
307 eval "$(set_ident <../commit)" ||
308 die
"setting author/committer failed for commit $commit"
309 eval "$filter_env" < /dev
/null ||
310 die
"env filter failed: $filter_env"
312 if [ "$filter_tree" ]; then
313 git checkout-index
-f -u -a ||
314 die
"Could not checkout the index"
315 # files that $commit removed are now still in the working tree;
316 # remove them, else they would be added again
317 git clean
-d -q -f -x
318 eval "$filter_tree" < /dev
/null ||
319 die
"tree filter failed: $filter_tree"
322 git diff-index
-r --name-only --ignore-submodules $commit &&
323 git ls-files
--others
324 ) > "$tempdir"/tree-state ||
exit
325 git update-index
--add --replace --remove --stdin \
326 < "$tempdir"/tree-state ||
exit
329 eval "$filter_index" < /dev
/null ||
330 die
"index filter failed: $filter_index"
333 for parent
in $parents; do
334 for reparent
in $
(map
"$parent"); do
335 parentstr
="$parentstr -p $reparent"
338 if [ "$filter_parent" ]; then
339 parentstr
="$(echo "$parentstr" | eval "$filter_parent")" ||
340 die
"parent filter failed: $filter_parent"
343 sed -e '1,/^$/d' <..
/commit | \
344 eval "$filter_msg" > ..
/message ||
345 die
"msg filter failed: $filter_msg"
346 workdir
=$workdir @SHELL_PATH@
-c "$filter_commit" "git commit-tree" \
347 $
(git write-tree
) $parentstr < ..
/message
> ..
/map
/$commit ||
348 die
"could not write rewritten commit"
351 # If we are filtering for paths, as in the case of a subdirectory
352 # filter, it is possible that a specified head is not in the set of
353 # rewritten commits, because it was pruned by the revision walker.
354 # Ancestor remapping fixes this by mapping these heads to the unique
355 # nearest ancestor that survived the pruning.
357 if test "$remap_to_ancestor" = t
361 sha1
=$
(git rev-parse
"$ref"^
0)
362 test -f "$workdir"/..
/map
/$sha1 && continue
363 ancestor
=$
(git rev-list
--simplify-merges -1 "$ref" "$@")
364 test "$ancestor" && echo $
(map
$ancestor) >> "$workdir"/..
/map
/$sha1
365 done < "$tempdir"/heads
368 # Finally update the refs
370 _x40
='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
371 _x40
="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
375 # avoid rewriting a ref twice
376 test -f "$orig_namespace$ref" && continue
378 sha1
=$
(git rev-parse
"$ref"^
0)
379 rewritten
=$
(map
$sha1)
381 test $sha1 = "$rewritten" &&
382 warn
"WARNING: Ref '$ref' is unchanged" &&
387 echo "Ref '$ref' was deleted"
388 git update-ref
-m "filter-branch: delete" -d "$ref" $sha1 ||
389 die
"Could not delete $ref"
392 echo "Ref '$ref' was rewritten"
393 if ! git update-ref
-m "filter-branch: rewrite" \
394 "$ref" $rewritten $sha1 2>/dev
/null
; then
395 if test $
(git cat-file
-t "$ref") = tag
; then
396 if test -z "$filter_tag_name"; then
397 warn
"WARNING: You said to rewrite tagged commits, but not the corresponding tag."
398 warn
"WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
401 die
"Could not rewrite $ref"
406 # NEEDSWORK: possibly add -Werror, making this an error
407 warn
"WARNING: '$ref' was rewritten into multiple commits:"
409 warn
"WARNING: Ref '$ref' points to the first one now."
410 rewritten
=$
(echo "$rewritten" |
head -n 1)
411 git update-ref
-m "filter-branch: rewrite to first" \
412 "$ref" $rewritten $sha1 ||
413 die
"Could not rewrite $ref"
416 git update-ref
-m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
418 done < "$tempdir"/heads
420 # TODO: This should possibly go, with the semantics that all positive given
421 # refs are updated, and their original heads stored in refs/original/
424 if [ "$filter_tag_name" ]; then
425 git for-each-ref
--format='%(objectname) %(objecttype) %(refname)' refs
/tags |
426 while read sha1
type ref
; do
427 ref
="${ref#refs/tags/}"
428 # XXX: Rewrite tagged trees as well?
429 if [ "$type" != "commit" -a "$type" != "tag" ]; then
433 if [ "$type" = "tag" ]; then
434 # Dereference to a commit
436 sha1
="$(git rev-parse -q "$sha1"^{commit})" ||
continue
439 [ -f "../map/$sha1" ] ||
continue
440 new_sha1
="$(cat "..
/map
/$sha1")"
443 new_ref
="$(echo "$ref" | eval "$filter_tag_name")" ||
444 die
"tag name filter failed: $filter_tag_name"
446 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
448 if [ "$type" = "tag" ]; then
449 new_sha1
=$
( ( printf 'object %s\ntype commit\ntag %s\n' \
450 "$new_sha1" "$new_ref"
451 git cat-file tag
"$ref" |
458 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
461 die
"Could not create new tag object for $ref"
462 if git cat-file tag
"$ref" | \
463 sane_grep
'^-----BEGIN PGP SIGNATURE-----' >/dev
/null
2>&1
465 warn
"gpg signature stripped from tag object $sha1t"
469 git update-ref
"refs/tags/$new_ref" "$new_sha1" ||
470 die
"Could not write tag $new_ref"
479 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
480 test -z "$ORIG_GIT_DIR" ||
{
481 GIT_DIR
="$ORIG_GIT_DIR" && export GIT_DIR
483 test -z "$ORIG_GIT_WORK_TREE" ||
{
484 GIT_WORK_TREE
="$ORIG_GIT_WORK_TREE" &&
487 test -z "$ORIG_GIT_INDEX_FILE" ||
{
488 GIT_INDEX_FILE
="$ORIG_GIT_INDEX_FILE" &&
489 export GIT_INDEX_FILE
492 if [ "$(is_bare_repository)" = false
]; then
493 git read-tree
-u -m HEAD ||
exit