Skip to content
This repository
Newer
Older
100755 514 lines (451 sloc) 12.116 kb
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
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 #
c401b33c » dscho
2007-07-04 Document git-filter-branch
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.
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
10
16ed34ad » dscho
2007-11-28 filter-branch: fix dirty way to provide the helpers to commit filters
11 # The following functions will also be available in the commit filter:
12
13 functions=$(cat << \EOF
b5669a05 » sprohaska
2007-07-04 filter-branch: added missing warn function
14 warn () {
285c6cbf » jonseymour
2011-08-05 misc-sh: fix up whitespace in some other .sh files.
15 echo "$*" >&2
b5669a05 » sprohaska
2007-07-04 filter-branch: added missing warn function
16 }
17
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
18 map()
19 {
3520e1e8 » Johannes Sixt
2007-06-06 filter-branch: also don't fail in map() if a commit cannot be mapped
20 # if it was not rewritten, take the original
c57a3494 » Johannes Sixt
2007-07-04 filter-branch: Avoid an error message in the map function.
21 if test -r "$workdir/../map/$1"
22 then
23 cat "$workdir/../map/$1"
24 else
25 echo "$1"
26 fi
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
27 }
28
f95eef15 » dscho
2007-08-31 filter-branch: introduce convenience function "skip_commit"
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
d3240d93 » MadCoder
2008-10-31 filter-branch: add git_commit_non_empty_tree and --prune-empty.
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 }
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
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 }
16ed34ad » dscho
2007-11-28 filter-branch: fix dirty way to provide the helpers to commit filters
62 EOF
63 )
64
65 eval "$functions"
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
66
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
67 # When piped a commit, output a script to set the ident of either
68 # "author" or "committer
69
70 set_ident () {
40a7ce64 » peff
2008-03-12 tr portability fixes
71 lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
72 uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
73 pick_id_script='
74 /^'$lid' /{
75 s/'\''/'\''\\'\'\''/g
76 h
77 s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
78 s/'\''/'\''\'\'\''/g
3addc94a » dscho
2007-11-28 Replace instances of export VAR=VAL with VAR=VAL; export VAR
79 s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
80
81 g
82 s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
83 s/'\''/'\''\'\'\''/g
3addc94a » dscho
2007-11-28 Replace instances of export VAR=VAL with VAR=VAL; export VAR
84 s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
85
86 g
cb102b08 » gitster
2012-07-09 filter-branch: do not forget the '@' prefix to force git-timestamp
87 s/^'$lid' [^<]* <[^>]*> \(.*\)$/@\1/
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
88 s/'\''/'\''\'\'\''/g
3addc94a » dscho
2007-11-28 Replace instances of export VAR=VAL with VAR=VAL; export VAR
89 s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
90
91 q
92 }
93 '
94
95 LANG=C LC_ALL=C sed -ne "$pick_id_script"
96 # Ensure non-empty id name.
3addc94a » dscho
2007-11-28 Replace instances of export VAR=VAL with VAR=VAL; export VAR
97 echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
98 }
99
83e355a6 » moy
2009-08-18 filter-branch: make the usage string fit on 80 chars terminals.
100 USAGE="[--env-filter <command>] [--tree-filter <command>]
9dcca58d » gitster
2011-08-05 filter-branch.sh: de-dent usage string
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>...]"
7e0f1704 » dscho
2007-08-31 filter-branch: provide the convenience functions also for commit filters
106
8f321a39 » gitster
2007-11-06 scripts: Add placeholders for OPTIONS_SPEC
107 OPTIONS_SPEC=
7e0f1704 » dscho
2007-08-31 filter-branch: provide the convenience functions also for commit filters
108 . git-sh-setup
109
a4661b01 » Petr Baudis
2008-07-24 git-filter-branch.sh: Allow running in bare repositories
110 if [ "$(is_bare_repository)" = false ]; then
5347a50f » peff
2011-09-01 filter-branch: use require_clean_work_tree
111 require_clean_work_tree 'rewrite branches'
a4661b01 » Petr Baudis
2008-07-24 git-filter-branch.sh: Allow running in bare repositories
112 fi
46eb449c » dscho
2007-10-17 filter-branch: update current branch when rewritten
113
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
114 tempdir=.git-rewrite
115 filter_env=
116 filter_tree=
117 filter_index=
118 filter_parent=
119 filter_msg=cat
d3240d93 » MadCoder
2008-10-31 filter-branch: add git_commit_non_empty_tree and --prune-empty.
120 filter_commit=
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
121 filter_tag_name=
685ef546 » dscho
2007-06-08 Teach filter-branch about subdirectory filtering
122 filter_subdir=
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
123 orig_namespace=refs/original/
124 force=
d3240d93 » MadCoder
2008-10-31 filter-branch: add git_commit_non_empty_tree and --prune-empty.
125 prune_empty=
f2f3a6b8 » trast
2009-11-10 filter-branch: nearest-ancestor rewriting outside subdir filter
126 remap_to_ancestor=
822f7c73 » David Kastrup
2007-09-23 Supplant the "while case ... break ;; esac" idiom
127 while :
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
128 do
129 case "$1" in
130 --)
131 shift
132 break
133 ;;
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
134 --force|-f)
135 shift
136 force=t
137 continue
138 ;;
f2f3a6b8 » trast
2009-11-10 filter-branch: nearest-ancestor rewriting outside subdir filter
139 --remap-to-ancestor)
7ec344d8 » csabahenk
2010-08-27 filter-branch: retire --remap-to-ancestor
140 # deprecated ($remap_to_ancestor is set now automatically)
f2f3a6b8 » trast
2009-11-10 filter-branch: nearest-ancestor rewriting outside subdir filter
141 shift
142 remap_to_ancestor=t
143 continue
144 ;;
d3240d93 » MadCoder
2008-10-31 filter-branch: add git_commit_non_empty_tree and --prune-empty.
145 --prune-empty)
146 shift
147 prune_empty=t
148 continue
149 ;;
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
150 -*)
151 ;;
152 *)
153 break;
154 esac
155
156 # all switches take one argument
157 ARG="$1"
158 case "$#" in 1) usage ;; esac
159 shift
160 OPTARG="$1"
161 shift
162
163 case "$ARG" in
164 -d)
165 tempdir="$OPTARG"
166 ;;
167 --env-filter)
168 filter_env="$OPTARG"
169 ;;
170 --tree-filter)
171 filter_tree="$OPTARG"
172 ;;
173 --index-filter)
174 filter_index="$OPTARG"
175 ;;
176 --parent-filter)
177 filter_parent="$OPTARG"
178 ;;
179 --msg-filter)
180 filter_msg="$OPTARG"
181 ;;
182 --commit-filter)
16ed34ad » dscho
2007-11-28 filter-branch: fix dirty way to provide the helpers to commit filters
183 filter_commit="$functions; $OPTARG"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
184 ;;
185 --tag-name-filter)
186 filter_tag_name="$OPTARG"
187 ;;
685ef546 » dscho
2007-06-08 Teach filter-branch about subdirectory filtering
188 --subdirectory-filter)
189 filter_subdir="$OPTARG"
f2f3a6b8 » trast
2009-11-10 filter-branch: nearest-ancestor rewriting outside subdir filter
190 remap_to_ancestor=t
685ef546 » dscho
2007-06-08 Teach filter-branch about subdirectory filtering
191 ;;
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
192 --original)
55ced83d » gitster
2007-08-30 filter-branch: make sure orig_namespace ends with a single slash.
193 orig_namespace=$(expr "$OPTARG/" : '\(.*[^/]\)/*$')/
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
194 ;;
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
195 *)
196 usage
197 ;;
198 esac
199 done
200
d3240d93 » MadCoder
2008-10-31 filter-branch: add git_commit_non_empty_tree and --prune-empty.
201 case "$prune_empty,$filter_commit" in
202 ,)
203 filter_commit='git commit-tree "$@"';;
204 t,)
205 filter_commit="$functions;"' git_commit_non_empty_tree "$@"';;
206 ,*)
207 ;;
208 *)
5da81713 » jhelwig
2010-02-11 filter-branch: Fix error message for --prune-empty --commit-filter
209 die "Cannot set --prune-empty and --commit-filter at the same time"
d3240d93 » MadCoder
2008-10-31 filter-branch: add git_commit_non_empty_tree and --prune-empty.
210 esac
211
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
212 case "$force" in
213 t)
214 rm -rf "$tempdir"
215 ;;
216 '')
217 test -d "$tempdir" &&
218 die "$tempdir already exists, please remove it"
219 esac
af580e9c » dscho
2007-07-18 filter-branch: get rid of "set -e"
220 mkdir -p "$tempdir/t" &&
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
221 tempdir="$(cd "$tempdir"; pwd)" &&
af580e9c » dscho
2007-07-18 filter-branch: get rid of "set -e"
222 cd "$tempdir/t" &&
223 workdir="$(pwd)" ||
224 die ""
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
225
def16e71 » Brandon Casey
2008-01-28 filter-branch.sh: remove temporary directory on failure
226 # Remove tempdir on exit
227 trap 'cd ../..; rm -rf "$tempdir"' 0
228
88e38808 » noschinl
2009-02-18 filter-branch -d: Export GIT_DIR earlier
229 ORIG_GIT_DIR="$GIT_DIR"
230 ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
231 ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
232 GIT_WORK_TREE=.
233 export GIT_DIR GIT_WORK_TREE
234
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
235 # Make sure refs/original is empty
0ea29cce » emk
2009-02-11 filter-branch: Add more error-handling
236 git for-each-ref > "$tempdir"/backup-refs || exit
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
237 while read sha1 type name
238 do
239 case "$force,$name" in
240 ,$orig_namespace*)
734cd572 » johnflux
2009-02-19 Improve error message for git-filter-branch
241 die "Cannot create a new backup.
242 A previous backup already exists in $orig_namespace
243 Force overwriting the backup with -f"
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
244 ;;
245 t,$orig_namespace*)
246 git update-ref -d "$name" $sha1
247 ;;
248 esac
249 done < "$tempdir"/backup-refs
250
418fa3a5 » gitster
2008-01-05 filter-branch: work correctly with ambiguous refnames
251 # The refs should be updated if their heads were rewritten
0ea29cce » emk
2009-02-11 filter-branch: Add more error-handling
252 git rev-parse --no-flags --revs-only --symbolic-full-name \
253 --default HEAD "$@" > "$tempdir"/raw-heads || exit
254 sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
255
256 test -s "$tempdir"/heads ||
257 die "Which ref do you want to rewrite?"
258
3addc94a » dscho
2007-11-28 Replace instances of export VAR=VAL with VAR=VAL; export VAR
259 GIT_INDEX_FILE="$(pwd)/../index"
260 export GIT_INDEX_FILE
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
261
af580e9c » dscho
2007-07-18 filter-branch: get rid of "set -e"
262 # map old->new commit ids for rewriting parents
263 mkdir ../map || die "Could not create map/ directory"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
264
2c1d2d81 » trast
2009-11-11 filter-branch: stop special-casing $filter_subdir argument
265 # we need "--" only if there are no path arguments in $@
266 nonrevs=$(git rev-parse --no-revs "$@") || exit
7ec344d8 » csabahenk
2010-08-27 filter-branch: retire --remap-to-ancestor
267 if test -z "$nonrevs"
268 then
269 dashdash=--
270 else
271 dashdash=
272 remap_to_ancestor=t
273 fi
274
2c1d2d81 » trast
2009-11-11 filter-branch: stop special-casing $filter_subdir argument
275 rev_args=$(git rev-parse --revs-only "$@")
276
685ef546 » dscho
2007-06-08 Teach filter-branch about subdirectory filtering
277 case "$filter_subdir" in
278 "")
2c1d2d81 » trast
2009-11-11 filter-branch: stop special-casing $filter_subdir argument
279 eval set -- "$(git rev-parse --sq --no-revs "$@")"
685ef546 » dscho
2007-06-08 Teach filter-branch about subdirectory filtering
280 ;;
281 *)
2c1d2d81 » trast
2009-11-11 filter-branch: stop special-casing $filter_subdir argument
282 eval set -- "$(git rev-parse --sq --no-revs "$@" $dashdash \
283 "$filter_subdir")"
284 ;;
285 esac
286
287 git rev-list --reverse --topo-order --default HEAD \
288 --parents --simplify-merges $rev_args "$@" > ../revs ||
289 die "Could not get the commits"
9d6f220c » Josh Triplett
2007-07-14 Remove useless uses of cat, and replace with filename arguments
290 commits=$(wc -l <../revs | tr -d " ")
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
291
292 test $commits -eq 0 && die "Found nothing to rewrite"
293
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
294 # Rewrite the commits
295
d5b0c97d » newren
2009-03-25 git-filter-branch: avoid collisions with variables in eval'ed commands
296 git_filter_branch__commit_count=0
813b4734 » Johannes Sixt
2007-06-08 filter-branch: Simplify parent computation.
297 while read commit parents; do
d5b0c97d » newren
2009-03-25 git-filter-branch: avoid collisions with variables in eval'ed commands
298 git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
299 printf "\rRewrite $commit ($git_filter_branch__commit_count/$commits)"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
300
685ef546 » dscho
2007-06-08 Teach filter-branch about subdirectory filtering
301 case "$filter_subdir" in
302 "")
5be60078 » gitster
2007-07-02 Rewrite "git-frotz" to "git frotz"
303 git read-tree -i -m $commit
685ef546 » dscho
2007-06-08 Teach filter-branch about subdirectory filtering
304 ;;
305 *)
5b044ac3 » gitster
2008-03-08 filter-branch: handle "disappearing tree" case correctly in subdir fi…
306 # The commit may not have the subdirectory at all
307 err=$(git read-tree -i -m $commit:"$filter_subdir" 2>&1) || {
d69da76d » vmiklos
2008-12-03 filter-branch: use git rev-parse -q
308 if ! git rev-parse -q --verify $commit:"$filter_subdir"
5b044ac3 » gitster
2008-03-08 filter-branch: handle "disappearing tree" case correctly in subdir fi…
309 then
310 rm -f "$GIT_INDEX_FILE"
311 else
312 echo >&2 "$err"
313 false
314 fi
315 }
af580e9c » dscho
2007-07-18 filter-branch: get rid of "set -e"
316 esac || die "Could not initialize the index"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
317
3addc94a » dscho
2007-11-28 Replace instances of export VAR=VAL with VAR=VAL; export VAR
318 GIT_COMMIT=$commit
319 export GIT_COMMIT
af580e9c » dscho
2007-07-18 filter-branch: get rid of "set -e"
320 git cat-file commit "$commit" >../commit ||
321 die "Cannot read commit $commit"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
322
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
323 eval "$(set_ident AUTHOR <../commit)" ||
324 die "setting author failed for commit $commit"
325 eval "$(set_ident COMMITTER <../commit)" ||
326 die "setting committer failed for commit $commit"
327 eval "$filter_env" < /dev/null ||
328 die "env filter failed: $filter_env"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
329
330 if [ "$filter_tree" ]; then
af580e9c » dscho
2007-07-18 filter-branch: get rid of "set -e"
331 git checkout-index -f -u -a ||
332 die "Could not checkout the index"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
333 # files that $commit removed are now still in the working tree;
334 # remove them, else they would be added again
6a589fda » veillette@yahoo.ca
2008-03-31 filter-branch: Fix renaming a directory in the tree-filter
335 git clean -d -q -f -x
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
336 eval "$filter_tree" < /dev/null ||
337 die "tree filter failed: $filter_tree"
338
1fe32cb9 » gitster
2008-02-15 filter-branch: handle filenames that need quoting
339 (
03ca8395 » wentasah
2010-01-28 filter-branch: Fix to allow replacing submodules with another content
340 git diff-index -r --name-only --ignore-submodules $commit &&
1fe32cb9 » gitster
2008-02-15 filter-branch: handle filenames that need quoting
341 git ls-files --others
0ea29cce » emk
2009-02-11 filter-branch: Add more error-handling
342 ) > "$tempdir"/tree-state || exit
343 git update-index --add --replace --remove --stdin \
344 < "$tempdir"/tree-state || exit
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
345 fi
346
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
347 eval "$filter_index" < /dev/null ||
348 die "index filter failed: $filter_index"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
349
350 parentstr=
813b4734 » Johannes Sixt
2007-06-08 filter-branch: Simplify parent computation.
351 for parent in $parents; do
3520e1e8 » Johannes Sixt
2007-06-06 filter-branch: also don't fail in map() if a commit cannot be mapped
352 for reparent in $(map "$parent"); do
353 parentstr="$parentstr -p $reparent"
354 done
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
355 done
356 if [ "$filter_parent" ]; then
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
357 parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
358 die "parent filter failed: $filter_parent"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
359 fi
360
361 sed -e '1,/^$/d' <../commit | \
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
362 eval "$filter_msg" > ../message ||
363 die "msg filter failed: $filter_msg"
0906f6e1 » mfwitten
2011-08-07 filter-branch: Export variable `workdir' for --commit-filter
364 workdir=$workdir @SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
0ea29cce » emk
2009-02-11 filter-branch: Add more error-handling
365 $(git write-tree) $parentstr < ../message > ../map/$commit ||
366 die "could not write rewritten commit"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
367 done <../revs
368
f2f3a6b8 » trast
2009-11-10 filter-branch: nearest-ancestor rewriting outside subdir filter
369 # If we are filtering for paths, as in the case of a subdirectory
370 # filter, it is possible that a specified head is not in the set of
371 # rewritten commits, because it was pruned by the revision walker.
372 # Ancestor remapping fixes this by mapping these heads to the unique
373 # nearest ancestor that survived the pruning.
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
374
f2f3a6b8 » trast
2009-11-10 filter-branch: nearest-ancestor rewriting outside subdir filter
375 if test "$remap_to_ancestor" = t
a0e46390 » trast
2008-08-12 filter-branch: fix ref rewriting with --subdirectory-filter
376 then
377 while read ref
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
378 do
a0e46390 » trast
2008-08-12 filter-branch: fix ref rewriting with --subdirectory-filter
379 sha1=$(git rev-parse "$ref"^0)
380 test -f "$workdir"/../map/$sha1 && continue
2c1d2d81 » trast
2009-11-11 filter-branch: stop special-casing $filter_subdir argument
381 ancestor=$(git rev-list --simplify-merges -1 "$ref" "$@")
a0e46390 » trast
2008-08-12 filter-branch: fix ref rewriting with --subdirectory-filter
382 test "$ancestor" && echo $(map $ancestor) >> "$workdir"/../map/$sha1
383 done < "$tempdir"/heads
384 fi
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
385
386 # Finally update the refs
387
388 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
389 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
390 echo
391 while read ref
392 do
393 # avoid rewriting a ref twice
394 test -f "$orig_namespace$ref" && continue
395
396 sha1=$(git rev-parse "$ref"^0)
397 rewritten=$(map $sha1)
398
399 test $sha1 = "$rewritten" &&
400 warn "WARNING: Ref '$ref' is unchanged" &&
401 continue
402
403 case "$rewritten" in
404 '')
405 echo "Ref '$ref' was deleted"
406 git update-ref -m "filter-branch: delete" -d "$ref" $sha1 ||
407 die "Could not delete $ref"
98409060 » dscho
2007-06-05 filter-branch: fix behaviour of '-k'
408 ;;
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
409 $_x40)
410 echo "Ref '$ref' was rewritten"
261044e8 » trast
2008-08-08 filter-branch: be more helpful when an annotated tag changes
411 if ! git update-ref -m "filter-branch: rewrite" \
412 "$ref" $rewritten $sha1 2>/dev/null; then
413 if test $(git cat-file -t "$ref") = tag; then
414 if test -z "$filter_tag_name"; then
415 warn "WARNING: You said to rewrite tagged commits, but not the corresponding tag."
416 warn "WARNING: Perhaps use '--tag-name-filter cat' to rewrite the tag."
417 fi
418 else
419 die "Could not rewrite $ref"
420 fi
421 fi
98409060 » dscho
2007-06-05 filter-branch: fix behaviour of '-k'
422 ;;
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
423 *)
424 # NEEDSWORK: possibly add -Werror, making this an error
425 warn "WARNING: '$ref' was rewritten into multiple commits:"
426 warn "$rewritten"
427 warn "WARNING: Ref '$ref' points to the first one now."
428 rewritten=$(echo "$rewritten" | head -n 1)
429 git update-ref -m "filter-branch: rewrite to first" \
430 "$ref" $rewritten $sha1 ||
431 die "Could not rewrite $ref"
432 ;;
433 esac
0ea29cce » emk
2009-02-11 filter-branch: Add more error-handling
434 git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
435 exit
dfd05e38 » dscho
2007-07-23 filter-branch: Big syntax change; support rewriting multiple refs
436 done < "$tempdir"/heads
437
438 # TODO: This should possibly go, with the semantics that all positive given
439 # refs are updated, and their original heads stored in refs/original/
440 # Filter tags
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
441
442 if [ "$filter_tag_name" ]; then
5be60078 » gitster
2007-07-02 Rewrite "git-frotz" to "git frotz"
443 git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
444 while read sha1 type ref; do
445 ref="${ref#refs/tags/}"
446 # XXX: Rewrite tagged trees as well?
447 if [ "$type" != "commit" -a "$type" != "tag" ]; then
448 continue;
449 fi
450
451 if [ "$type" = "tag" ]; then
452 # Dereference to a commit
453 sha1t="$sha1"
7bd93c1c » Dan Loewenherz
2009-04-22 Convert to use quiet option when available
454 sha1="$(git rev-parse -q "$sha1"^{commit})" || continue
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
455 fi
456
457 [ -f "../map/$sha1" ] || continue
458 new_sha1="$(cat "../map/$sha1")"
3addc94a » dscho
2007-11-28 Replace instances of export VAR=VAL with VAR=VAL; export VAR
459 GIT_COMMIT="$sha1"
460 export GIT_COMMIT
8c1ce0f4 » dscho
2007-07-04 filter-branch: fail gracefully when a filter fails
461 new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
462 die "tag name filter failed: $filter_tag_name"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
463
464 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
465
466 if [ "$type" = "tag" ]; then
a9da1663 » Johannes Sixt
2008-08-21 filter-branch: Grok special characters in tag names
467 new_sha1=$( ( printf 'object %s\ntype commit\ntag %s\n' \
468 "$new_sha1" "$new_ref"
469 git cat-file tag "$ref" |
1bf6551e » Brandon Casey
2008-03-26 filter-branch.sh: support nearly proper tag name filtering
470 sed -n \
9524cf29 » bebarino
2010-01-26 fix portability issues with $ in double quotes
471 -e '1,/^$/{
a9da1663 » Johannes Sixt
2008-08-21 filter-branch: Grok special characters in tag names
472 /^object /d
473 /^type /d
474 /^tag /d
9524cf29 » bebarino
2010-01-26 fix portability issues with $ in double quotes
475 }' \
1bf6551e » Brandon Casey
2008-03-26 filter-branch.sh: support nearly proper tag name filtering
476 -e '/^-----BEGIN PGP SIGNATURE-----/q' \
a9da1663 » Johannes Sixt
2008-08-21 filter-branch: Grok special characters in tag names
477 -e 'p' ) |
1bf6551e » Brandon Casey
2008-03-26 filter-branch.sh: support nearly proper tag name filtering
478 git mktag) ||
479 die "Could not create new tag object for $ref"
480 if git cat-file tag "$ref" | \
e1622bfc » gitster
2009-11-23 Protect scripted Porcelains from GREP_OPTIONS insanity
481 sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
1bf6551e » Brandon Casey
2008-03-26 filter-branch.sh: support nearly proper tag name filtering
482 then
483 warn "gpg signature stripped from tag object $sha1t"
484 fi
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
485 fi
486
af580e9c » dscho
2007-07-18 filter-branch: get rid of "set -e"
487 git update-ref "refs/tags/$new_ref" "$new_sha1" ||
488 die "Could not write tag $new_ref"
6f6826c5 » dscho
2007-06-03 Add git-filter-branch
489 done
490 fi
491
492 cd ../..
493 rm -rf "$tempdir"
494
def16e71 » Brandon Casey
2008-01-28 filter-branch.sh: remove temporary directory on failure
495 trap - 0
496
9273b562 » emk
2009-02-03 filter-branch: Fix fatal error on bare repositories
497 unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
498 test -z "$ORIG_GIT_DIR" || {
499 GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
500 }
501 test -z "$ORIG_GIT_WORK_TREE" || {
502 GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
503 export GIT_WORK_TREE
504 }
505 test -z "$ORIG_GIT_INDEX_FILE" || {
506 GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
507 export GIT_INDEX_FILE
508 }
509
a4661b01 » Petr Baudis
2008-07-24 git-filter-branch.sh: Allow running in bare repositories
510 if [ "$(is_bare_repository)" = false ]; then
0ea29cce » emk
2009-02-11 filter-branch: Add more error-handling
511 git read-tree -u -m HEAD || exit
a4661b01 » Petr Baudis
2008-07-24 git-filter-branch.sh: Allow running in bare repositories
512 fi
46eb449c » dscho
2007-10-17 filter-branch: update current branch when rewritten
513
0ea29cce » emk
2009-02-11 filter-branch: Add more error-handling
514 exit 0
Something went wrong with that request. Please try again.