stash: end commit log with a newline
[git/git.git] / git-filter-branch.sh
CommitLineData
6f6826c5
JS
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 GIT revision history by creating a new branch from
8# your current branch by applying custom filters on each revision.
9# Those filters can modify each tree (e.g. removing a file or running
10# a perl rewrite on all files) or information about each commit.
11# Otherwise, all information (including original commit times or merge
12# information) will be preserved.
13#
14# The command takes the new branch name as a mandatory argument and
15# the filters as optional arguments. If you specify no filters, the
16# commits will be recommitted without any changes, which would normally
17# have no effect and result with the new branch pointing to the same
18# branch as your current branch. (Nevertheless, this may be useful in
19# the future for compensating for some Git bugs or such, therefore
20# such a usage is permitted.)
21#
22# WARNING! The rewritten history will have different ids for all the
23# objects and will not converge with the original branch. You will not
24# be able to easily push and distribute the rewritten branch. Please do
25# not use this command if you do not know the full implications, and
26# avoid using it anyway - do not do what a simple single commit on top
27# of the current version would fix.
28#
29# Always verify that the rewritten version is correct before disposing
30# the original branch.
31#
32# Note that since this operation is extensively I/O expensive, it might
33# be a good idea to do it off-disk, e.g. on tmpfs. Reportedly the speedup
34# is very noticeable.
35#
36# OPTIONS
37# -------
38# -d TEMPDIR:: The path to the temporary tree used for rewriting
39# When applying a tree filter, the command needs to temporary
40# checkout the tree to some directory, which may consume
41# considerable space in case of large projects. By default it
42# does this in the '.git-rewrite/' directory but you can override
43# that choice by this parameter.
44#
6f6826c5
JS
45# Filters
46# ~~~~~~~
47# The filters are applied in the order as listed below. The COMMAND
48# argument is always evaluated in shell using the 'eval' command.
49# The $GIT_COMMIT environment variable is permanently set to contain
50# the id of the commit being rewritten. The author/committer environment
51# variables are set before the first filter is run.
52#
53# A 'map' function is available that takes an "original sha1 id" argument
54# and outputs a "rewritten sha1 id" if the commit has been already
55# rewritten, fails otherwise; the 'map' function can return several
56# ids on separate lines if your commit filter emitted multiple commits
57# (see below).
58#
59# --env-filter COMMAND:: The filter for modifying environment
60# This is the filter for modifying the environment in which
61# the commit will be performed. Specifically, you might want
62# to rewrite the author/committer name/email/time environment
63# variables (see `git-commit` for details). Do not forget to
64# re-export the variables.
65#
66# --tree-filter COMMAND:: The filter for rewriting tree (and its contents)
67# This is the filter for rewriting the tree and its contents.
68# The COMMAND argument is evaluated in shell with the working
69# directory set to the root of the checked out tree. The new tree
70# is then used as-is (new files are auto-added, disappeared files
71# are auto-removed - .gitignore files nor any other ignore rules
72# HAVE NO EFFECT!).
73#
74# --index-filter COMMAND:: The filter for rewriting index
75# This is the filter for rewriting the Git's directory index.
76# It is similar to the tree filter but does not check out the
77# tree, which makes it much faster. However, you must use the
78# lowlevel Git index manipulation commands to do your work.
79#
80# --parent-filter COMMAND:: The filter for rewriting parents
81# This is the filter for rewriting the commit's parent list.
82# It will receive the parent string on stdin and shall output
83# the new parent string on stdout. The parent string is in
5be60078 84# format accepted by `git commit-tree`: empty for initial
6f6826c5
JS
85# commit, "-p parent" for a normal commit and "-p parent1
86# -p parent2 -p parent3 ..." for a merge commit.
87#
88# --msg-filter COMMAND:: The filter for rewriting commit message
89# This is the filter for rewriting the commit messages.
90# The COMMAND argument is evaluated in shell with the original
91# commit message on standard input; its standard output is
92# is used as the new commit message.
93#
94# --commit-filter COMMAND:: The filter for performing the commit
95# If this filter is passed, it will be called instead of the
5be60078 96# `git commit-tree` command, with those arguments:
6f6826c5
JS
97#
98# TREE_ID [-p PARENT_COMMIT_ID]...
99#
100# and the log message on stdin. The commit id is expected on
101# stdout. As a special extension, the commit filter may emit
102# multiple commit ids; in that case, all of them will be used
103# as parents instead of the original commit in further commits.
104#
105# --tag-name-filter COMMAND:: The filter for rewriting tag names.
106# If this filter is passed, it will be called for every tag ref
107# that points to a rewritten object (or to a tag object which
108# points to a rewritten object). The original tag name is passed
109# via standard input, and the new tag name is expected on standard
110# output.
111#
112# The original tags are not deleted, but can be overwritten;
113# use "--tag-name-filter=cat" to simply update the tags. In this
114# case, be very careful and make sure you have the old tags
115# backed up in case the conversion has run afoul.
116#
117# Note that there is currently no support for proper rewriting of
118# tag objects; in layman terms, if the tag has a message or signature
119# attached, the rewritten tag won't have it. Sorry. (It is by
120# definition impossible to preserve signatures at any rate, though.)
121#
685ef546
JS
122# --subdirectory-filter DIRECTORY:: Only regard the history, as seen by
123# the given subdirectory. The result will contain that directory as
124# its project root.
125#
6f6826c5
JS
126# EXAMPLE USAGE
127# -------------
128# Suppose you want to remove a file (containing confidential information
129# or copyright violation) from all commits:
130#
131# git-filter-branch --tree-filter 'rm filename' newbranch
132#
133# A significantly faster version:
134#
5be60078 135# git-filter-branch --index-filter 'git update-index --remove filename' newbranch
6f6826c5
JS
136#
137# Now, you will get the rewritten history saved in the branch 'newbranch'
138# (your current branch is left untouched).
139#
140# To "etch-graft" a commit to the revision history (set a commit to be
141# the parent of the current initial commit and propagate that):
142#
143# git-filter-branch --parent-filter sed\ 's/^$/-p graftcommitid/' newbranch
144#
145# (if the parent string is empty - therefore we are dealing with the
146# initial commit - add graftcommit as a parent). Note that this assumes
147# history with a single root (that is, no git-merge without common ancestors
148# happened). If this is not the case, use:
149#
150# git-filter-branch --parent-filter 'cat; [ "$GIT_COMMIT" = "COMMIT" ] && echo "-p GRAFTCOMMIT"' newbranch
151#
152# To remove commits authored by "Darl McBribe" from the history:
153#
5be60078 154# git-filter-branch --commit-filter 'if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ]; then shift; while [ -n "$1" ]; do shift; echo "$1"; shift; done; else git commit-tree "$@"; fi' newbranch
6f6826c5
JS
155#
156# (the shift magic first throws away the tree id and then the -p
157# parameters). Note that this handles merges properly! In case Darl
158# committed a merge between P1 and P2, it will be propagated properly
159# and all children of the merge will become merge commits with P1,P2
160# as their parents instead of the merge commit.
161#
2766ce28
JS
162# To restrict rewriting to only part of the history, specify a revision
163# range in addition to the new branch name. The new branch name will
164# point to the top-most revision that a 'git rev-list' of this range
165# will print.
166#
6f6826c5
JS
167# Consider this history:
168#
169# D--E--F--G--H
170# / /
171# A--B-----C
172#
2766ce28 173# To rewrite commits D,E,F,G,H, use:
6f6826c5 174#
2766ce28 175# git-filter-branch ... new-H C..H
6f6826c5
JS
176#
177# To rewrite commits E,F,G,H, use one of these:
178#
2766ce28
JS
179# git-filter-branch ... new-H C..H --not D
180# git-filter-branch ... new-H D..H --not C
55f22ff2
JS
181#
182# To move the whole tree into a subdirectory, or remove it from there:
183#
184# git-filter-branch --index-filter \
5be60078 185# 'git ls-files -s | sed "s-\t-&newsubdir/-" |
55f22ff2 186# GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
5be60078 187# git update-index --index-info &&
55f22ff2 188# mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' directorymoved
6f6826c5
JS
189
190# Testsuite: TODO
191
192set -e
193
2766ce28 194USAGE="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]"
6f6826c5
JS
195. git-sh-setup
196
197map()
198{
3520e1e8
JS
199 # if it was not rewritten, take the original
200 test -r "$workdir/../map/$1" || echo "$1"
6f6826c5
JS
201 cat "$workdir/../map/$1"
202}
203
204# When piped a commit, output a script to set the ident of either
205# "author" or "committer
206
207set_ident () {
208 lid="$(echo "$1" | tr "A-Z" "a-z")"
209 uid="$(echo "$1" | tr "a-z" "A-Z")"
210 pick_id_script='
211 /^'$lid' /{
212 s/'\''/'\''\\'\'\''/g
213 h
214 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
215 s/'\''/'\''\'\'\''/g
216 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
217
218 g
219 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
220 s/'\''/'\''\'\'\''/g
221 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
222
223 g
224 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
225 s/'\''/'\''\'\'\''/g
226 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
227
228 q
229 }
230 '
231
232 LANG=C LC_ALL=C sed -ne "$pick_id_script"
233 # Ensure non-empty id name.
234 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
235}
236
6f6826c5 237tempdir=.git-rewrite
6f6826c5
JS
238filter_env=
239filter_tree=
240filter_index=
241filter_parent=
242filter_msg=cat
5be60078 243filter_commit='git commit-tree "$@"'
6f6826c5 244filter_tag_name=
685ef546 245filter_subdir=
6f6826c5
JS
246while case "$#" in 0) usage;; esac
247do
248 case "$1" in
249 --)
250 shift
251 break
252 ;;
253 -*)
254 ;;
255 *)
256 break;
257 esac
258
259 # all switches take one argument
260 ARG="$1"
261 case "$#" in 1) usage ;; esac
262 shift
263 OPTARG="$1"
264 shift
265
266 case "$ARG" in
267 -d)
268 tempdir="$OPTARG"
269 ;;
6f6826c5
JS
270 --env-filter)
271 filter_env="$OPTARG"
272 ;;
273 --tree-filter)
274 filter_tree="$OPTARG"
275 ;;
276 --index-filter)
277 filter_index="$OPTARG"
278 ;;
279 --parent-filter)
280 filter_parent="$OPTARG"
281 ;;
282 --msg-filter)
283 filter_msg="$OPTARG"
284 ;;
285 --commit-filter)
286 filter_commit="$OPTARG"
287 ;;
288 --tag-name-filter)
289 filter_tag_name="$OPTARG"
290 ;;
685ef546
JS
291 --subdirectory-filter)
292 filter_subdir="$OPTARG"
293 ;;
6f6826c5
JS
294 *)
295 usage
296 ;;
297 esac
298done
299
300dstbranch="$1"
2766ce28 301shift
6f6826c5 302test -n "$dstbranch" || die "missing branch name"
5be60078 303git show-ref "refs/heads/$dstbranch" 2> /dev/null &&
6f6826c5
JS
304 die "branch $dstbranch already exists"
305
306test ! -e "$tempdir" || die "$tempdir already exists, please remove it"
307mkdir -p "$tempdir/t"
308cd "$tempdir/t"
309workdir="$(pwd)"
310
311case "$GIT_DIR" in
312/*)
313 ;;
314*)
9489d0f1 315 GIT_DIR="$(pwd)/../../$GIT_DIR"
6f6826c5
JS
316 ;;
317esac
9489d0f1 318export GIT_DIR GIT_WORK_TREE=.
6f6826c5
JS
319
320export GIT_INDEX_FILE="$(pwd)/../index"
5be60078 321git read-tree # seed the index file
6f6826c5
JS
322
323ret=0
324
325
326mkdir ../map # map old->new commit ids for rewriting parents
327
685ef546
JS
328case "$filter_subdir" in
329"")
5be60078 330 git rev-list --reverse --topo-order --default HEAD \
813b4734 331 --parents "$@"
685ef546
JS
332 ;;
333*)
5be60078 334 git rev-list --reverse --topo-order --default HEAD \
cfabd6ee 335 --parents --full-history "$@" -- "$filter_subdir"
685ef546 336esac > ../revs
6f6826c5
JS
337commits=$(cat ../revs | wc -l | tr -d " ")
338
339test $commits -eq 0 && die "Found nothing to rewrite"
340
341i=0
813b4734 342while read commit parents; do
c12764b8 343 i=$(($i+1))
6f6826c5
JS
344 printf "$commit ($i/$commits) "
345
685ef546
JS
346 case "$filter_subdir" in
347 "")
5be60078 348 git read-tree -i -m $commit
685ef546
JS
349 ;;
350 *)
5be60078 351 git read-tree -i -m $commit:"$filter_subdir"
685ef546 352 esac
6f6826c5
JS
353
354 export GIT_COMMIT=$commit
5be60078 355 git cat-file commit "$commit" >../commit
6f6826c5
JS
356
357 eval "$(set_ident AUTHOR <../commit)"
358 eval "$(set_ident COMMITTER <../commit)"
350d8575 359 eval "$filter_env" < /dev/null
6f6826c5
JS
360
361 if [ "$filter_tree" ]; then
5be60078 362 git checkout-index -f -u -a
6f6826c5
JS
363 # files that $commit removed are now still in the working tree;
364 # remove them, else they would be added again
5be60078 365 git ls-files -z --others | xargs -0 rm -f
350d8575 366 eval "$filter_tree" < /dev/null
5be60078
JH
367 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
368 xargs -0 git update-index --add --replace --remove
369 git ls-files -z --others | \
370 xargs -0 git update-index --add --replace --remove
6f6826c5
JS
371 fi
372
350d8575 373 eval "$filter_index" < /dev/null
6f6826c5
JS
374
375 parentstr=
813b4734 376 for parent in $parents; do
3520e1e8
JS
377 for reparent in $(map "$parent"); do
378 parentstr="$parentstr -p $reparent"
379 done
6f6826c5
JS
380 done
381 if [ "$filter_parent" ]; then
382 parentstr="$(echo "$parentstr" | eval "$filter_parent")"
383 fi
384
385 sed -e '1,/^$/d' <../commit | \
386 eval "$filter_msg" | \
5be60078 387 sh -c "$filter_commit" "git commit-tree" $(git write-tree) $parentstr | \
6f6826c5
JS
388 tee ../map/$commit
389done <../revs
390
813b4734 391src_head=$(tail -n 1 ../revs | sed -e 's/ .*//')
98409060
JS
392target_head=$(head -n 1 ../map/$src_head)
393case "$target_head" in
394'')
395 echo Nothing rewritten
396 ;;
397*)
5be60078 398 git update-ref refs/heads/"$dstbranch" $target_head
98409060
JS
399 if [ $(cat ../map/$src_head | wc -l) -gt 1 ]; then
400 echo "WARNING: Your commit filter caused the head commit to expand to several rewritten commits. Only the first such commit was recorded as the current $dstbranch head but you will need to resolve the situation now (probably by manually merging the other commits). These are all the commits:" >&2
401 sed 's/^/ /' ../map/$src_head >&2
402 ret=1
403 fi
404 ;;
405esac
6f6826c5
JS
406
407if [ "$filter_tag_name" ]; then
5be60078 408 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5
JS
409 while read sha1 type ref; do
410 ref="${ref#refs/tags/}"
411 # XXX: Rewrite tagged trees as well?
412 if [ "$type" != "commit" -a "$type" != "tag" ]; then
413 continue;
414 fi
415
416 if [ "$type" = "tag" ]; then
417 # Dereference to a commit
418 sha1t="$sha1"
5be60078 419 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
6f6826c5
JS
420 fi
421
422 [ -f "../map/$sha1" ] || continue
423 new_sha1="$(cat "../map/$sha1")"
424 export GIT_COMMIT="$sha1"
425 new_ref="$(echo "$ref" | eval "$filter_tag_name")"
426
427 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
428
429 if [ "$type" = "tag" ]; then
430 # Warn that we are not rewriting the tag object itself.
431 warn "unreferencing tag object $sha1t"
432 fi
433
5be60078 434 git update-ref "refs/tags/$new_ref" "$new_sha1"
6f6826c5
JS
435 done
436fi
437
438cd ../..
439rm -rf "$tempdir"
440echo "Rewritten history saved to the $dstbranch branch"
441
442exit $ret