Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 494 lines (433 sloc) 11.809 kb
6f6826c @dscho Add git-filter-branch
dscho authored
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 #
c401b33 @dscho Document git-filter-branch
dscho authored
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.
6f6826c @dscho Add git-filter-branch
dscho authored
10
16ed34a @dscho filter-branch: fix dirty way to provide the helpers to commit filters
dscho authored
11 # The following functions will also be available in the commit filter:
12
13 functions=$(cat << \EOF
b5669a0 @sprohaska filter-branch: added missing warn function
sprohaska authored
14 warn () {
15 echo "$*" >&2
16 }
17
6f6826c @dscho Add git-filter-branch
dscho authored
18 map()
19 {
3520e1e filter-branch: also don't fail in map() if a commit cannot be mapped
Johannes Sixt authored
20 # if it was not rewritten, take the original
c57a349 filter-branch: Avoid an error message in the map function.
Johannes Sixt authored
21 if test -r "$workdir/../map/$1"
22 then
23 cat "$workdir/../map/$1"
24 else
25 echo "$1"
26 fi
6f6826c @dscho Add git-filter-branch
dscho authored
27 }
28
f95eef1 @dscho filter-branch: introduce convenience function "skip_commit"
dscho authored
29 # if you run 'skip_commit "$@"' in a commit filter, it will print
30 # the (mapped) parents, effectively skipping the commit.
31
32 skip_commit()
33 {
34 shift;
35 while [ -n "$1" ];
36 do
37 shift;
38 map "$1";
39 shift;
40 done;
41 }
42
d3240d9 @MadCoder filter-branch: add git_commit_non_empty_tree and --prune-empty.
MadCoder authored
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()
46 {
47 if test $# = 3 && test "$1" = $(git rev-parse "$3^{tree}"); then
48 map "$3"
49 else
50 git commit-tree "$@"
51 fi
52 }
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
53 # override die(): this version puts in an extra line break, so that
54 # the progress is still visible
55
56 die()
57 {
58 echo >&2
59 echo "$*" >&2
60 exit 1
61 }
16ed34a @dscho filter-branch: fix dirty way to provide the helpers to commit filters
dscho authored
62 EOF
63 )
64
65 eval "$functions"
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
66
6f6826c @dscho Add git-filter-branch
dscho authored
67 # When piped a commit, output a script to set the ident of either
68 # "author" or "committer
69
70 set_ident () {
40a7ce6 @peff tr portability fixes
peff authored
71 lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
72 uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
6f6826c @dscho Add git-filter-branch
dscho authored
73 pick_id_script='
74 /^'$lid' /{
75 s/'\''/'\''\\'\'\''/g
76 h
77 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
78 s/'\''/'\''\'\'\''/g
3addc94 @dscho Replace instances of export VAR=VAL with VAR=VAL; export VAR
dscho authored
79 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
6f6826c @dscho Add git-filter-branch
dscho authored
80
81 g
82 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
83 s/'\''/'\''\'\'\''/g
3addc94 @dscho Replace instances of export VAR=VAL with VAR=VAL; export VAR
dscho authored
84 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
6f6826c @dscho Add git-filter-branch
dscho authored
85
86 g
87 s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
88 s/'\''/'\''\'\'\''/g
3addc94 @dscho Replace instances of export VAR=VAL with VAR=VAL; export VAR
dscho authored
89 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
6f6826c @dscho Add git-filter-branch
dscho authored
90
91 q
92 }
93 '
94
95 LANG=C LC_ALL=C sed -ne "$pick_id_script"
96 # Ensure non-empty id name.
3addc94 @dscho Replace instances of export VAR=VAL with VAR=VAL; export VAR
dscho authored
97 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
6f6826c @dscho Add git-filter-branch
dscho authored
98 }
99
83e355a @moy filter-branch: make the usage string fit on 80 chars terminals.
moy authored
100 USAGE="[--env-filter <command>] [--tree-filter <command>]
101 [--index-filter <command>] [--parent-filter <command>]
102 [--msg-filter <command>] [--commit-filter <command>]
103 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
104 [--original <namespace>] [-d <directory>] [-f | --force]
105 [<rev-list options>...]"
7e0f170 @dscho filter-branch: provide the convenience functions also for commit filters
dscho authored
106
8f321a3 @gitster scripts: Add placeholders for OPTIONS_SPEC
gitster authored
107 OPTIONS_SPEC=
7e0f170 @dscho filter-branch: provide the convenience functions also for commit filters
dscho authored
108 . git-sh-setup
109
a4661b0 git-filter-branch.sh: Allow running in bare repositories
Petr Baudis authored
110 if [ "$(is_bare_repository)" = false ]; then
26be15f @dscho filter-branch: do not consider diverging submodules a 'dirty worktree'
dscho authored
111 git diff-files --ignore-submodules --quiet &&
38762c4 @gitster scripts: do not get confused with HEAD in work tree
gitster authored
112 git diff-index --cached --quiet HEAD -- ||
46eb449 @dscho filter-branch: update current branch when rewritten
dscho authored
113 die "Cannot rewrite branch(es) with a dirty working directory."
a4661b0 git-filter-branch.sh: Allow running in bare repositories
Petr Baudis authored
114 fi
46eb449 @dscho filter-branch: update current branch when rewritten
dscho authored
115
6f6826c @dscho Add git-filter-branch
dscho authored
116 tempdir=.git-rewrite
117 filter_env=
118 filter_tree=
119 filter_index=
120 filter_parent=
121 filter_msg=cat
d3240d9 @MadCoder filter-branch: add git_commit_non_empty_tree and --prune-empty.
MadCoder authored
122 filter_commit=
6f6826c @dscho Add git-filter-branch
dscho authored
123 filter_tag_name=
685ef54 @dscho Teach filter-branch about subdirectory filtering
dscho authored
124 filter_subdir=
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
125 orig_namespace=refs/original/
126 force=
d3240d9 @MadCoder filter-branch: add git_commit_non_empty_tree and --prune-empty.
MadCoder authored
127 prune_empty=
822f7c7 Supplant the "while case ... break ;; esac" idiom
David Kastrup authored
128 while :
6f6826c @dscho Add git-filter-branch
dscho authored
129 do
130 case "$1" in
131 --)
132 shift
133 break
134 ;;
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
135 --force|-f)
136 shift
137 force=t
138 continue
139 ;;
d3240d9 @MadCoder filter-branch: add git_commit_non_empty_tree and --prune-empty.
MadCoder authored
140 --prune-empty)
141 shift
142 prune_empty=t
143 continue
144 ;;
6f6826c @dscho Add git-filter-branch
dscho authored
145 -*)
146 ;;
147 *)
148 break;
149 esac
150
151 # all switches take one argument
152 ARG="$1"
153 case "$#" in 1) usage ;; esac
154 shift
155 OPTARG="$1"
156 shift
157
158 case "$ARG" in
159 -d)
160 tempdir="$OPTARG"
161 ;;
162 --env-filter)
163 filter_env="$OPTARG"
164 ;;
165 --tree-filter)
166 filter_tree="$OPTARG"
167 ;;
168 --index-filter)
169 filter_index="$OPTARG"
170 ;;
171 --parent-filter)
172 filter_parent="$OPTARG"
173 ;;
174 --msg-filter)
175 filter_msg="$OPTARG"
176 ;;
177 --commit-filter)
16ed34a @dscho filter-branch: fix dirty way to provide the helpers to commit filters
dscho authored
178 filter_commit="$functions; $OPTARG"
6f6826c @dscho Add git-filter-branch
dscho authored
179 ;;
180 --tag-name-filter)
181 filter_tag_name="$OPTARG"
182 ;;
685ef54 @dscho Teach filter-branch about subdirectory filtering
dscho authored
183 --subdirectory-filter)
184 filter_subdir="$OPTARG"
185 ;;
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
186 --original)
55ced83 @gitster filter-branch: make sure orig_namespace ends with a single slash.
gitster authored
187 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
188 ;;
6f6826c @dscho Add git-filter-branch
dscho authored
189 *)
190 usage
191 ;;
192 esac
193 done
194
d3240d9 @MadCoder filter-branch: add git_commit_non_empty_tree and --prune-empty.
MadCoder authored
195 case "$prune_empty,$filter_commit" in
196 ,)
197 filter_commit='git commit-tree "$@"';;
198 t,)
199 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
200 ,*)
201 ;;
202 *)
203 die "Cannot set --prune-empty and --filter-commit at the same time"
204 esac
205
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
206 case "$force" in
207 t)
208 rm -rf "$tempdir"
209 ;;
210 '')
211 test -d "$tempdir" &&
212 die "$tempdir already exists, please remove it"
213 esac
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
214 mkdir -p "$tempdir/t" &&
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
215 tempdir="$(cd "$tempdir"; pwd)" &&
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
216 cd "$tempdir/t" &&
217 workdir="$(pwd)" ||
218 die ""
6f6826c @dscho Add git-filter-branch
dscho authored
219
def16e7 filter-branch.sh: remove temporary directory on failure
Brandon Casey authored
220 # Remove tempdir on exit
221 trap 'cd ../..; rm -rf "$tempdir"' 0
222
88e3880 @noschinl filter-branch -d: Export GIT_DIR earlier
noschinl authored
223 ORIG_GIT_DIR="$GIT_DIR"
224 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
225 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
226 GIT_WORK_TREE=.
227 export GIT_DIR GIT_WORK_TREE
228
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
229 # Make sure refs/original is empty
0ea29cc @emk filter-branch: Add more error-handling
emk authored
230 git for-each-ref > "$tempdir"/backup-refs || exit
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
231 while read sha1 type name
232 do
233 case "$force,$name" in
234 ,$orig_namespace*)
734cd57 @johnflux Improve error message for git-filter-branch
johnflux authored
235 die "Cannot create a new backup.
236 A previous backup already exists in $orig_namespace
237 Force overwriting the backup with -f"
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
238 ;;
239 t,$orig_namespace*)
240 git update-ref -d "$name" $sha1
241 ;;
242 esac
243 done < "$tempdir"/backup-refs
244
418fa3a @gitster filter-branch: work correctly with ambiguous refnames
gitster authored
245 # The refs should be updated if their heads were rewritten
0ea29cc @emk filter-branch: Add more error-handling
emk authored
246 git rev-parse --no-flags --revs-only --symbolic-full-name \
247 --default HEAD "$@" > "$tempdir"/raw-heads || exit
248 sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
249
250 test -s "$tempdir"/heads ||
251 die "Which ref do you want to rewrite?"
252
3addc94 @dscho Replace instances of export VAR=VAL with VAR=VAL; export VAR
dscho authored
253 GIT_INDEX_FILE="$(pwd)/../index"
254 export GIT_INDEX_FILE
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
255 git read-tree || die "Could not seed the index"
6f6826c @dscho Add git-filter-branch
dscho authored
256
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
257 # map old->new commit ids for rewriting parents
258 mkdir ../map || die "Could not create map/ directory"
6f6826c @dscho Add git-filter-branch
dscho authored
259
685ef54 @dscho Teach filter-branch about subdirectory filtering
dscho authored
260 case "$filter_subdir" in
261 "")
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
262 git rev-list --reverse --topo-order --default HEAD \
f34a941 @trast filter-branch: use --simplify-merges
trast authored
263 --parents --simplify-merges "$@"
685ef54 @dscho Teach filter-branch about subdirectory filtering
dscho authored
264 ;;
265 *)
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
266 git rev-list --reverse --topo-order --default HEAD \
f34a941 @trast filter-branch: use --simplify-merges
trast authored
267 --parents --simplify-merges "$@" -- "$filter_subdir"
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
268 esac > ../revs || die "Could not get the commits"
9d6f220 @joshtriplett Remove useless uses of cat, and replace with filename arguments
joshtriplett authored
269 commits=$(wc -l <../revs | tr -d " ")
6f6826c @dscho Add git-filter-branch
dscho authored
270
271 test $commits -eq 0 && die "Found nothing to rewrite"
272
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
273 # Rewrite the commits
274
d5b0c97 @newren git-filter-branch: avoid collisions with variables in eval'ed commands
newren authored
275 git_filter_branch__commit_count=0
813b473 filter-branch: Simplify parent computation.
Johannes Sixt authored
276 while read commit parents; do
d5b0c97 @newren git-filter-branch: avoid collisions with variables in eval'ed commands
newren authored
277 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
278 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
6f6826c @dscho Add git-filter-branch
dscho authored
279
685ef54 @dscho Teach filter-branch about subdirectory filtering
dscho authored
280 case "$filter_subdir" in
281 "")
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
282 git read-tree -i -m $commit
685ef54 @dscho Teach filter-branch about subdirectory filtering
dscho authored
283 ;;
284 *)
5b044ac @gitster filter-branch: handle "disappearing tree" case correctly in subdir filte...
gitster authored
285 # The commit may not have the subdirectory at all
286 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
d69da76 filter-branch: use git rev-parse -q
Miklos Vajna authored
287 if ! git rev-parse -q --verify $commit:"$filter_subdir"
5b044ac @gitster filter-branch: handle "disappearing tree" case correctly in subdir filte...
gitster authored
288 then
289 rm -f "$GIT_INDEX_FILE"
290 else
291 echo >&2 "$err"
292 false
293 fi
294 }
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
295 esac || die "Could not initialize the index"
6f6826c @dscho Add git-filter-branch
dscho authored
296
3addc94 @dscho Replace instances of export VAR=VAL with VAR=VAL; export VAR
dscho authored
297 GIT_COMMIT=$commit
298 export GIT_COMMIT
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
299 git cat-file commit "$commit" >../commit ||
300 die "Cannot read commit $commit"
6f6826c @dscho Add git-filter-branch
dscho authored
301
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
302 eval "$(set_ident AUTHOR <../commit)" ||
303 die "setting author failed for commit $commit"
304 eval "$(set_ident COMMITTER <../commit)" ||
305 die "setting committer failed for commit $commit"
306 eval "$filter_env" < /dev/null ||
307 die "env filter failed: $filter_env"
6f6826c @dscho Add git-filter-branch
dscho authored
308
309 if [ "$filter_tree" ]; then
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
310 git checkout-index -f -u -a ||
311 die "Could not checkout the index"
6f6826c @dscho Add git-filter-branch
dscho authored
312 # files that $commit removed are now still in the working tree;
313 # remove them, else they would be added again
6a589fd filter-branch: Fix renaming a directory in the tree-filter
veillette@yahoo.ca authored
314 git clean -d -q -f -x
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
315 eval "$filter_tree" < /dev/null ||
316 die "tree filter failed: $filter_tree"
317
1fe32cb @gitster filter-branch: handle filenames that need quoting
gitster authored
318 (
0ea29cc @emk filter-branch: Add more error-handling
emk authored
319 git diff-index -r --name-only $commit &&
1fe32cb @gitster filter-branch: handle filenames that need quoting
gitster authored
320 git ls-files --others
0ea29cc @emk filter-branch: Add more error-handling
emk authored
321 ) > "$tempdir"/tree-state || exit
322 git update-index --add --replace --remove --stdin \
323 < "$tempdir"/tree-state || exit
6f6826c @dscho Add git-filter-branch
dscho authored
324 fi
325
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
326 eval "$filter_index" < /dev/null ||
327 die "index filter failed: $filter_index"
6f6826c @dscho Add git-filter-branch
dscho authored
328
329 parentstr=
813b473 filter-branch: Simplify parent computation.
Johannes Sixt authored
330 for parent in $parents; do
3520e1e filter-branch: also don't fail in map() if a commit cannot be mapped
Johannes Sixt authored
331 for reparent in $(map "$parent"); do
332 parentstr="$parentstr -p $reparent"
333 done
6f6826c @dscho Add git-filter-branch
dscho authored
334 done
335 if [ "$filter_parent" ]; then
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
336 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
337 die "parent filter failed: $filter_parent"
6f6826c @dscho Add git-filter-branch
dscho authored
338 fi
339
340 sed -e '1,/^$/d' <../commit | \
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
341 eval "$filter_msg" > ../message ||
342 die "msg filter failed: $filter_msg"
4bf9f27 @peff filter-branch: use $SHELL_PATH instead of 'sh'
peff authored
343 @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
0ea29cc @emk filter-branch: Add more error-handling
emk authored
344 $(git write-tree) $parentstr < ../message > ../map/$commit ||
345 die "could not write rewritten commit"
6f6826c @dscho Add git-filter-branch
dscho authored
346 done <../revs
347
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
348 # In case of a subdirectory filter, it is possible that a specified head
349 # is not in the set of rewritten commits, because it was pruned by the
a0e4639 @trast filter-branch: fix ref rewriting with --subdirectory-filter
trast authored
350 # revision walker. Fix it by mapping these heads to the unique nearest
351 # ancestor that survived the pruning.
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
352
a0e4639 @trast filter-branch: fix ref rewriting with --subdirectory-filter
trast authored
353 if test "$filter_subdir"
354 then
355 while read ref
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
356 do
a0e4639 @trast filter-branch: fix ref rewriting with --subdirectory-filter
trast authored
357 sha1=$(git rev-parse "$ref"^0)
358 test -f "$workdir"/../map/$sha1 && continue
f34a941 @trast filter-branch: use --simplify-merges
trast authored
359 ancestor=$(git rev-list --simplify-merges -1 \
360 $ref -- "$filter_subdir")
a0e4639 @trast filter-branch: fix ref rewriting with --subdirectory-filter
trast authored
361 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
362 done < "$tempdir"/heads
363 fi
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
364
365 # Finally update the refs
366
367 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
368 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
369 echo
370 while read ref
371 do
372 # avoid rewriting a ref twice
373 test -f "$orig_namespace$ref" && continue
374
375 sha1=$(git rev-parse "$ref"^0)
376 rewritten=$(map $sha1)
377
378 test $sha1 = "$rewritten" &&
379 warn "WARNING: Ref '$ref' is unchanged" &&
380 continue
381
382 case "$rewritten" in
383 '')
384 echo "Ref '$ref' was deleted"
385 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
386 die "Could not delete $ref"
9840906 @dscho filter-branch: fix behaviour of '-k'
dscho authored
387 ;;
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
388 $_x40)
389 echo "Ref '$ref' was rewritten"
261044e @trast filter-branch: be more helpful when an annotated tag changes
trast authored
390 if ! git update-ref -m "filter-branch: rewrite" \
391 "$ref" $rewritten $sha1 2>/dev/null; then
392 if test $(git cat-file -t "$ref") = tag; then
393 if test -z "$filter_tag_name"; then
394 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
395 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
396 fi
397 else
398 die "Could not rewrite $ref"
399 fi
400 fi
9840906 @dscho filter-branch: fix behaviour of '-k'
dscho authored
401 ;;
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
402 *)
403 # NEEDSWORK: possibly add -Werror, making this an error
404 warn "WARNING: '$ref' was rewritten into multiple commits:"
405 warn "$rewritten"
406 warn "WARNING: Ref '$ref' points to the first one now."
407 rewritten=$(echo "$rewritten" | head -n 1)
408 git update-ref -m "filter-branch: rewrite to first" \
409 "$ref" $rewritten $sha1 ||
410 die "Could not rewrite $ref"
411 ;;
412 esac
0ea29cc @emk filter-branch: Add more error-handling
emk authored
413 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
414 exit
dfd05e3 @dscho filter-branch: Big syntax change; support rewriting multiple refs
dscho authored
415 done < "$tempdir"/heads
416
417 # TODO: This should possibly go, with the semantics that all positive given
418 # refs are updated, and their original heads stored in refs/original/
419 # Filter tags
6f6826c @dscho Add git-filter-branch
dscho authored
420
421 if [ "$filter_tag_name" ]; then
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
422 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c @dscho Add git-filter-branch
dscho authored
423 while read sha1 type ref; do
424 ref="${ref#refs/tags/}"
425 # XXX: Rewrite tagged trees as well?
426 if [ "$type" != "commit" -a "$type" != "tag" ]; then
427 continue;
428 fi
429
430 if [ "$type" = "tag" ]; then
431 # Dereference to a commit
432 sha1t="$sha1"
7bd93c1 Convert to use quiet option when available
Dan Loewenherz authored
433 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
6f6826c @dscho Add git-filter-branch
dscho authored
434 fi
435
436 [ -f "../map/$sha1" ] || continue
437 new_sha1="$(cat "../map/$sha1")"
3addc94 @dscho Replace instances of export VAR=VAL with VAR=VAL; export VAR
dscho authored
438 GIT_COMMIT="$sha1"
439 export GIT_COMMIT
8c1ce0f @dscho filter-branch: fail gracefully when a filter fails
dscho authored
440 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
441 die "tag name filter failed: $filter_tag_name"
6f6826c @dscho Add git-filter-branch
dscho authored
442
443 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
444
445 if [ "$type" = "tag" ]; then
a9da166 filter-branch: Grok special characters in tag names
Johannes Sixt authored
446 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
447 "$new_sha1" "$new_ref"
448 git cat-file tag "$ref" |
1bf6551 filter-branch.sh: support nearly proper tag name filtering
Brandon Casey authored
449 sed -n \
450 -e "1,/^$/{
a9da166 filter-branch: Grok special characters in tag names
Johannes Sixt authored
451 /^object /d
452 /^type /d
453 /^tag /d
1bf6551 filter-branch.sh: support nearly proper tag name filtering
Brandon Casey authored
454 }" \
455 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
a9da166 filter-branch: Grok special characters in tag names
Johannes Sixt authored
456 -e 'p' ) |
1bf6551 filter-branch.sh: support nearly proper tag name filtering
Brandon Casey authored
457 git mktag) ||
458 die "Could not create new tag object for $ref"
459 if git cat-file tag "$ref" | \
e1622bf @gitster Protect scripted Porcelains from GREP_OPTIONS insanity
gitster authored
460 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
1bf6551 filter-branch.sh: support nearly proper tag name filtering
Brandon Casey authored
461 then
462 warn "gpg signature stripped from tag object $sha1t"
463 fi
6f6826c @dscho Add git-filter-branch
dscho authored
464 fi
465
af580e9 @dscho filter-branch: get rid of "set -e"
dscho authored
466 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
467 die "Could not write tag $new_ref"
6f6826c @dscho Add git-filter-branch
dscho authored
468 done
469 fi
470
471 cd ../..
472 rm -rf "$tempdir"
473
def16e7 filter-branch.sh: remove temporary directory on failure
Brandon Casey authored
474 trap - 0
475
9273b56 @emk filter-branch: Fix fatal error on bare repositories
emk authored
476 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
477 test -z "$ORIG_GIT_DIR" || {
478 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
479 }
480 test -z "$ORIG_GIT_WORK_TREE" || {
481 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
482 export GIT_WORK_TREE
483 }
484 test -z "$ORIG_GIT_INDEX_FILE" || {
485 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
486 export GIT_INDEX_FILE
487 }
488
a4661b0 git-filter-branch.sh: Allow running in bare repositories
Petr Baudis authored
489 if [ "$(is_bare_repository)" = false ]; then
0ea29cc @emk filter-branch: Add more error-handling
emk authored
490 git read-tree -u -m HEAD || exit
a4661b0 git-filter-branch.sh: Allow running in bare repositories
Petr Baudis authored
491 fi
46eb449 @dscho filter-branch: update current branch when rewritten
dscho authored
492
0ea29cc @emk filter-branch: Add more error-handling
emk authored
493 exit 0
Something went wrong with that request. Please try again.