Do not generate full commit log message if it is not going to be used
[git/spearce.git] / git-filter-branch.sh
blobc9f515d0ee0f36fc44f667f17674462ced7c2c9e
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 warn () {
12 echo "$*" >&2
15 map()
17 # if it was not rewritten, take the original
18 if test -r "$workdir/../map/$1"
19 then
20 cat "$workdir/../map/$1"
21 else
22 echo "$1"
26 # if you run 'skip_commit "$@"' in a commit filter, it will print
27 # the (mapped) parents, effectively skipping the commit.
29 skip_commit()
31 shift;
32 while [ -n "$1" ];
34 shift;
35 map "$1";
36 shift;
37 done;
40 # override die(): this version puts in an extra line break, so that
41 # the progress is still visible
43 die()
45 echo >&2
46 echo "$*" >&2
47 exit 1
50 # When piped a commit, output a script to set the ident of either
51 # "author" or "committer
53 set_ident () {
54 lid="$(echo "$1" | tr "A-Z" "a-z")"
55 uid="$(echo "$1" | tr "a-z" "A-Z")"
56 pick_id_script='
57 /^'$lid' /{
58 s/'\''/'\''\\'\'\''/g
60 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
61 s/'\''/'\''\'\'\''/g
62 s/.*/export GIT_'$uid'_NAME='\''&'\''/p
65 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
66 s/'\''/'\''\'\'\''/g
67 s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
70 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
71 s/'\''/'\''\'\'\''/g
72 s/.*/export GIT_'$uid'_DATE='\''&'\''/p
78 LANG=C LC_ALL=C sed -ne "$pick_id_script"
79 # Ensure non-empty id name.
80 echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
83 # This script can be sourced by the commit filter to get the functions
84 test "a$SOURCE_FUNCTIONS" = a1 && return
85 this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
86 export this_script
88 USAGE="[--env-filter <command>] [--tree-filter <command>] \
89 [--index-filter <command>] [--parent-filter <command>] \
90 [--msg-filter <command>] [--commit-filter <command>] \
91 [--tag-name-filter <command>] [--subdirectory-filter <directory>] \
92 [--original <namespace>] [-d <directory>] [-f | --force] \
93 [<rev-list options>...]"
95 OPTIONS_SPEC=
96 . git-sh-setup
98 git diff-files --quiet &&
99 git diff-index --cached --quiet HEAD ||
100 die "Cannot rewrite branch(es) with a dirty working directory."
102 tempdir=.git-rewrite
103 filter_env=
104 filter_tree=
105 filter_index=
106 filter_parent=
107 filter_msg=cat
108 filter_commit='git commit-tree "$@"'
109 filter_tag_name=
110 filter_subdir=
111 orig_namespace=refs/original/
112 force=
113 while :
115 test $# = 0 && usage
116 case "$1" in
118 shift
119 break
121 --force|-f)
122 shift
123 force=t
124 continue
129 break;
130 esac
132 # all switches take one argument
133 ARG="$1"
134 case "$#" in 1) usage ;; esac
135 shift
136 OPTARG="$1"
137 shift
139 case "$ARG" in
141 tempdir="$OPTARG"
143 --env-filter)
144 filter_env="$OPTARG"
146 --tree-filter)
147 filter_tree="$OPTARG"
149 --index-filter)
150 filter_index="$OPTARG"
152 --parent-filter)
153 filter_parent="$OPTARG"
155 --msg-filter)
156 filter_msg="$OPTARG"
158 --commit-filter)
159 filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
161 --tag-name-filter)
162 filter_tag_name="$OPTARG"
164 --subdirectory-filter)
165 filter_subdir="$OPTARG"
167 --original)
168 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
171 usage
173 esac
174 done
176 case "$force" in
178 rm -rf "$tempdir"
181 test -d "$tempdir" &&
182 die "$tempdir already exists, please remove it"
183 esac
184 mkdir -p "$tempdir/t" &&
185 tempdir="$(cd "$tempdir"; pwd)" &&
186 cd "$tempdir/t" &&
187 workdir="$(pwd)" ||
188 die ""
190 # Make sure refs/original is empty
191 git for-each-ref > "$tempdir"/backup-refs
192 while read sha1 type name
194 case "$force,$name" in
195 ,$orig_namespace*)
196 die "Namespace $orig_namespace not empty"
198 t,$orig_namespace*)
199 git update-ref -d "$name" $sha1
201 esac
202 done < "$tempdir"/backup-refs
204 ORIG_GIT_DIR="$GIT_DIR"
205 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
206 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
207 export GIT_DIR GIT_WORK_TREE=.
209 # These refs should be updated if their heads were rewritten
211 git rev-parse --revs-only --symbolic "$@" |
212 while read ref
214 # normalize ref
215 case "$ref" in
216 HEAD)
217 ref="$(git symbolic-ref "$ref")"
219 refs/*)
222 ref="$(git for-each-ref --format='%(refname)' |
223 grep /"$ref")"
224 esac
226 git check-ref-format "$ref" && echo "$ref"
227 done > "$tempdir"/heads
229 test -s "$tempdir"/heads ||
230 die "Which ref do you want to rewrite?"
232 export GIT_INDEX_FILE="$(pwd)/../index"
233 git read-tree || die "Could not seed the index"
235 ret=0
237 # map old->new commit ids for rewriting parents
238 mkdir ../map || die "Could not create map/ directory"
240 case "$filter_subdir" in
242 git rev-list --reverse --topo-order --default HEAD \
243 --parents "$@"
246 git rev-list --reverse --topo-order --default HEAD \
247 --parents --full-history "$@" -- "$filter_subdir"
248 esac > ../revs || die "Could not get the commits"
249 commits=$(wc -l <../revs | tr -d " ")
251 test $commits -eq 0 && die "Found nothing to rewrite"
253 # Rewrite the commits
256 while read commit parents; do
257 i=$(($i+1))
258 printf "\rRewrite $commit ($i/$commits)"
260 case "$filter_subdir" in
262 git read-tree -i -m $commit
265 git read-tree -i -m $commit:"$filter_subdir"
266 esac || die "Could not initialize the index"
268 export GIT_COMMIT=$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 ls-files -z --others | xargs -0 rm -f
285 eval "$filter_tree" < /dev/null ||
286 die "tree filter failed: $filter_tree"
288 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
289 xargs -0 git update-index --add --replace --remove
290 git ls-files -z --others | \
291 xargs -0 git update-index --add --replace --remove
294 eval "$filter_index" < /dev/null ||
295 die "index filter failed: $filter_index"
297 parentstr=
298 for parent in $parents; do
299 for reparent in $(map "$parent"); do
300 parentstr="$parentstr -p $reparent"
301 done
302 done
303 if [ "$filter_parent" ]; then
304 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
305 die "parent filter failed: $filter_parent"
308 sed -e '1,/^$/d' <../commit | \
309 eval "$filter_msg" > ../message ||
310 die "msg filter failed: $filter_msg"
311 sh -c "$filter_commit" "git commit-tree" \
312 $(git write-tree) $parentstr < ../message > ../map/$commit
313 done <../revs
315 # In case of a subdirectory filter, it is possible that a specified head
316 # is not in the set of rewritten commits, because it was pruned by the
317 # revision walker. Fix it by mapping these heads to the next rewritten
318 # ancestor(s), i.e. the boundaries in the set of rewritten commits.
320 # NEEDSWORK: we should sort the unmapped refs topologically first
321 while read ref
323 sha1=$(git rev-parse "$ref"^0)
324 test -f "$workdir"/../map/$sha1 && continue
325 # Assign the boundarie(s) in the set of rewritten commits
326 # as the replacement commit(s).
327 # (This would look a bit nicer if --not --stdin worked.)
328 for p in $( (cd "$workdir"/../map; ls | sed "s/^/^/") |
329 git rev-list $ref --boundary --stdin |
330 sed -n "s/^-//p")
332 map $p >> "$workdir"/../map/$sha1
333 done
334 done < "$tempdir"/heads
336 # Finally update the refs
338 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
339 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
340 count=0
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 count=$(($count+1))
379 done < "$tempdir"/heads
381 # TODO: This should possibly go, with the semantics that all positive given
382 # refs are updated, and their original heads stored in refs/original/
383 # Filter tags
385 if [ "$filter_tag_name" ]; then
386 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
387 while read sha1 type ref; do
388 ref="${ref#refs/tags/}"
389 # XXX: Rewrite tagged trees as well?
390 if [ "$type" != "commit" -a "$type" != "tag" ]; then
391 continue;
394 if [ "$type" = "tag" ]; then
395 # Dereference to a commit
396 sha1t="$sha1"
397 sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue
400 [ -f "../map/$sha1" ] || continue
401 new_sha1="$(cat "../map/$sha1")"
402 export GIT_COMMIT="$sha1"
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 # Warn that we are not rewriting the tag object itself.
410 warn "unreferencing tag object $sha1t"
413 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
414 die "Could not write tag $new_ref"
415 done
418 cd ../..
419 rm -rf "$tempdir"
420 echo
421 test $count -gt 0 && echo "These refs were rewritten:"
422 git show-ref | grep ^"$orig_namespace"
424 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
425 test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
426 test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
427 export GIT_WORK_TREE
428 test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
429 export GIT_INDEX_FILE
430 git read-tree -u -m HEAD
432 exit $ret