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
49 elif test $# = 1 && test "$1" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
; then
55 # override die(): this version puts in an extra line break, so that
56 # the progress is still visible
70 # Ensure non-empty id name.
71 echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac"
72 # And make sure everything is exported.
73 echo "export GIT_$1_NAME"
74 echo "export GIT_$1_EMAIL"
75 echo "export GIT_$1_DATE"
79 parse_ident_from_commit author AUTHOR committer COMMITTER
81 finish_ident COMMITTER
84 USAGE
="[--env-filter <command>] [--tree-filter <command>]
85 [--index-filter <command>] [--parent-filter <command>]
86 [--msg-filter <command>] [--commit-filter <command>]
87 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
88 [--original <namespace>] [-d <directory>] [-f | --force]
89 [<rev-list options>...]"
94 if [ "$(is_bare_repository)" = false
]; then
95 require_clean_work_tree
'rewrite branches'
107 orig_namespace
=refs
/original
/
124 # deprecated ($remap_to_ancestor is set now automatically)
140 # all switches take one argument
142 case "$#" in 1) usage
;; esac
155 filter_tree
="$OPTARG"
158 filter_index
="$OPTARG"
161 filter_parent
="$OPTARG"
167 filter_commit
="$functions; $OPTARG"
170 filter_tag_name
="$OPTARG"
172 --subdirectory-filter)
173 filter_subdir
="$OPTARG"
177 orig_namespace
=$
(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
185 case "$prune_empty,$filter_commit" in
187 filter_commit
='git commit-tree "$@"';;
189 filter_commit
="$functions;"' git_commit_non_empty_tree "$@"';;
193 die
"Cannot set --prune-empty and --commit-filter at the same time"
201 test -d "$tempdir" &&
202 die
"$tempdir already exists, please remove it"
205 mkdir
-p "$tempdir/t" &&
206 tempdir
="$(cd "$tempdir"; pwd)" &&
211 # Remove tempdir on exit
212 trap 'cd "$orig_dir"; rm -rf "$tempdir"' 0
214 ORIG_GIT_DIR
="$GIT_DIR"
215 ORIG_GIT_WORK_TREE
="$GIT_WORK_TREE"
216 ORIG_GIT_INDEX_FILE
="$GIT_INDEX_FILE"
218 export GIT_DIR GIT_WORK_TREE
220 # Make sure refs/original is empty
221 git for-each-ref
> "$tempdir"/backup-refs ||
exit
222 while read sha1
type name
224 case "$force,$name" in
226 die
"Cannot create a new backup.
227 A previous backup already exists in $orig_namespace
228 Force overwriting the backup with -f"
231 git update-ref
-d "$name" $sha1
234 done < "$tempdir"/backup-refs
236 # The refs should be updated if their heads were rewritten
237 git rev-parse
--no-flags --revs-only --symbolic-full-name \
238 --default HEAD
"$@" > "$tempdir"/raw-heads ||
exit
239 sed -e '/^^/d' "$tempdir"/raw-heads
>"$tempdir"/heads
241 test -s "$tempdir"/heads ||
242 die
"Which ref do you want to rewrite?"
244 GIT_INDEX_FILE
="$(pwd)/../index"
245 export GIT_INDEX_FILE
247 # map old->new commit ids for rewriting parents
248 mkdir ..
/map || die
"Could not create map/ directory"
250 # we need "--" only if there are no path arguments in $@
251 nonrevs
=$
(git rev-parse
--no-revs "$@") ||
exit
252 if test -z "$nonrevs"
260 git rev-parse
--revs-only "$@" >..
/parse
262 case "$filter_subdir" in
264 eval set -- "$(git rev-parse --sq --no-revs "$@
")"
267 eval set -- "$(git rev-parse --sq --no-revs "$@
" $dashdash \
272 git rev-list
--reverse --topo-order --default HEAD \
273 --parents --simplify-merges --stdin "$@" <..
/parse
>..
/revs ||
274 die
"Could not get the commits"
275 commits
=$
(wc -l <..
/revs |
tr -d " ")
277 test $commits -eq 0 && die
"Found nothing to rewrite"
279 # Rewrite the commits
282 if test -n "$progress" &&
283 test $git_filter_branch__commit_count -gt $next_sample_at
285 count
=$git_filter_branch__commit_count
288 elapsed
=$
(($now - $start_timestamp))
289 remaining
=$
(( ($commits - $count) * $elapsed / $count ))
290 if test $elapsed -gt 0
292 next_sample_at
=$
(( ($elapsed + 1) * $count / $elapsed ))
294 next_sample_at
=$
(($next_sample_at + 1))
296 progress
=" ($elapsed seconds passed, remaining $remaining predicted)"
298 printf "\rRewrite $commit ($count/$commits)$progress "
301 git_filter_branch__commit_count
=0
303 progress
= start_timestamp
=
304 if date '+%s' 2>/dev
/null |
grep -q '^[0-9][0-9]*$'
307 progress
="dummy to ensure this is not empty"
308 start_timestamp
=$
(date '+%s')
311 if test -n "$filter_index" ||
312 test -n "$filter_tree" ||
313 test -n "$filter_subdir"
320 while read commit parents
; do
321 git_filter_branch__commit_count
=$
(($git_filter_branch__commit_count+1))
325 case "$filter_subdir" in
327 if test -n "$need_index"
329 GIT_ALLOW_NULL_SHA1
=1 git read-tree
-i -m $commit
333 # The commit may not have the subdirectory at all
334 err
=$
(GIT_ALLOW_NULL_SHA1
=1 \
335 git read-tree
-i -m $commit:"$filter_subdir" 2>&1) ||
{
336 if ! git rev-parse
-q --verify $commit:"$filter_subdir"
338 rm -f "$GIT_INDEX_FILE"
344 esac || die
"Could not initialize the index"
348 git cat-file commit
"$commit" >..
/commit ||
349 die
"Cannot read commit $commit"
351 eval "$(set_ident <../commit)" ||
352 die
"setting author/committer failed for commit $commit"
353 eval "$filter_env" < /dev
/null ||
354 die
"env filter failed: $filter_env"
356 if [ "$filter_tree" ]; then
357 git checkout-index
-f -u -a ||
358 die
"Could not checkout the index"
359 # files that $commit removed are now still in the working tree;
360 # remove them, else they would be added again
361 git clean
-d -q -f -x
362 eval "$filter_tree" < /dev
/null ||
363 die
"tree filter failed: $filter_tree"
366 git diff-index
-r --name-only --ignore-submodules $commit -- &&
367 git ls-files
--others
368 ) > "$tempdir"/tree-state ||
exit
369 git update-index
--add --replace --remove --stdin \
370 < "$tempdir"/tree-state ||
exit
373 eval "$filter_index" < /dev
/null ||
374 die
"index filter failed: $filter_index"
377 for parent
in $parents; do
378 for reparent
in $
(map
"$parent"); do
379 case "$parentstr " in
383 parentstr
="$parentstr -p $reparent"
388 if [ "$filter_parent" ]; then
389 parentstr
="$(echo "$parentstr" | eval "$filter_parent")" ||
390 die
"parent filter failed: $filter_parent"
394 while IFS
='' read -r header_line
&& test -n "$header_line"
396 # skip header lines...
399 # and output the actual commit message
402 eval "$filter_msg" > ..
/message ||
403 die
"msg filter failed: $filter_msg"
405 if test -n "$need_index"
407 tree
=$
(git write-tree
)
409 tree
=$
(git rev-parse
"$commit^{tree}")
411 workdir
=$workdir @SHELL_PATH@
-c "$filter_commit" "git commit-tree" \
412 "$tree" $parentstr < ..
/message
> ..
/map
/$commit ||
413 die
"could not write rewritten commit"
416 # If we are filtering for paths, as in the case of a subdirectory
417 # filter, it is possible that a specified head is not in the set of
418 # rewritten commits, because it was pruned by the revision walker.
419 # Ancestor remapping fixes this by mapping these heads to the unique
420 # nearest ancestor that survived the pruning.
422 if test "$remap_to_ancestor" = t
426 sha1
=$
(git rev-parse
"$ref"^
0)
427 test -f "$workdir"/..
/map
/$sha1 && continue
428 ancestor
=$
(git rev-list
--simplify-merges -1 "$ref" "$@")
429 test "$ancestor" && echo $
(map
$ancestor) >> "$workdir"/..
/map
/$sha1
430 done < "$tempdir"/heads
433 # Finally update the refs
435 _x40
='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
436 _x40
="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
440 # avoid rewriting a ref twice
441 test -f "$orig_namespace$ref" && continue
443 sha1
=$
(git rev-parse
"$ref"^
0)
444 rewritten
=$
(map
$sha1)
446 test $sha1 = "$rewritten" &&
447 warn
"WARNING: Ref '$ref' is unchanged" &&
452 echo "Ref '$ref' was deleted"
453 git update-ref
-m "filter-branch: delete" -d "$ref" $sha1 ||
454 die
"Could not delete $ref"
457 echo "Ref '$ref' was rewritten"
458 if ! git update-ref
-m "filter-branch: rewrite" \
459 "$ref" $rewritten $sha1 2>/dev
/null
; then
460 if test $
(git cat-file
-t "$ref") = tag
; then
461 if test -z "$filter_tag_name"; then
462 warn
"WARNING: You said to rewrite tagged commits, but not the corresponding tag."
463 warn
"WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
466 die
"Could not rewrite $ref"
471 # NEEDSWORK: possibly add -Werror, making this an error
472 warn
"WARNING: '$ref' was rewritten into multiple commits:"
474 warn
"WARNING: Ref '$ref' points to the first one now."
475 rewritten
=$
(echo "$rewritten" |
head -n 1)
476 git update-ref
-m "filter-branch: rewrite to first" \
477 "$ref" $rewritten $sha1 ||
478 die
"Could not rewrite $ref"
481 git update-ref
-m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
483 done < "$tempdir"/heads
485 # TODO: This should possibly go, with the semantics that all positive given
486 # refs are updated, and their original heads stored in refs/original/
489 if [ "$filter_tag_name" ]; then
490 git for-each-ref
--format='%(objectname) %(objecttype) %(refname)' refs
/tags |
491 while read sha1
type ref
; do
492 ref
="${ref#refs/tags/}"
493 # XXX: Rewrite tagged trees as well?
494 if [ "$type" != "commit" -a "$type" != "tag" ]; then
498 if [ "$type" = "tag" ]; then
499 # Dereference to a commit
501 sha1
="$(git rev-parse -q "$sha1"^{commit})" ||
continue
504 [ -f "../map/$sha1" ] ||
continue
505 new_sha1
="$(cat "..
/map
/$sha1")"
508 new_ref
="$(echo "$ref" | eval "$filter_tag_name")" ||
509 die
"tag name filter failed: $filter_tag_name"
511 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
513 if [ "$type" = "tag" ]; then
514 new_sha1
=$
( ( printf 'object %s\ntype commit\ntag %s\n' \
515 "$new_sha1" "$new_ref"
516 git cat-file tag
"$ref" |
523 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
526 die
"Could not create new tag object for $ref"
527 if git cat-file tag
"$ref" | \
528 sane_grep
'^-----BEGIN PGP SIGNATURE-----' >/dev
/null
2>&1
530 warn
"gpg signature stripped from tag object $sha1t"
534 git update-ref
"refs/tags/$new_ref" "$new_sha1" ||
535 die
"Could not write tag $new_ref"
544 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
545 test -z "$ORIG_GIT_DIR" ||
{
546 GIT_DIR
="$ORIG_GIT_DIR" && export GIT_DIR
548 test -z "$ORIG_GIT_WORK_TREE" ||
{
549 GIT_WORK_TREE
="$ORIG_GIT_WORK_TREE" &&
552 test -z "$ORIG_GIT_INDEX_FILE" ||
{
553 GIT_INDEX_FILE
="$ORIG_GIT_INDEX_FILE" &&
554 export GIT_INDEX_FILE
557 if [ "$(is_bare_repository)" = false
]; then
558 git read-tree
-u -m HEAD ||
exit