]>
Commit | Line | Data |
---|---|---|
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 | # | |
c401b33c JS |
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 JS |
10 | |
11 | set -e | |
12 | ||
2766ce28 | 13 | USAGE="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]" |
6f6826c5 JS |
14 | . git-sh-setup |
15 | ||
b5669a05 SP |
16 | warn () { |
17 | echo "$*" >&2 | |
18 | } | |
19 | ||
6f6826c5 JS |
20 | map() |
21 | { | |
3520e1e8 | 22 | # if it was not rewritten, take the original |
c57a3494 JS |
23 | if test -r "$workdir/../map/$1" |
24 | then | |
25 | cat "$workdir/../map/$1" | |
26 | else | |
27 | echo "$1" | |
28 | fi | |
6f6826c5 JS |
29 | } |
30 | ||
8c1ce0f4 JS |
31 | # override die(): this version puts in an extra line break, so that |
32 | # the progress is still visible | |
33 | ||
34 | die() | |
35 | { | |
36 | echo >&2 | |
37 | echo "$*" >&2 | |
38 | exit 1 | |
39 | } | |
40 | ||
6f6826c5 JS |
41 | # When piped a commit, output a script to set the ident of either |
42 | # "author" or "committer | |
43 | ||
44 | set_ident () { | |
45 | lid="$(echo "$1" | tr "A-Z" "a-z")" | |
46 | uid="$(echo "$1" | tr "a-z" "A-Z")" | |
47 | pick_id_script=' | |
48 | /^'$lid' /{ | |
49 | s/'\''/'\''\\'\'\''/g | |
50 | h | |
51 | s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/ | |
52 | s/'\''/'\''\'\'\''/g | |
53 | s/.*/export GIT_'$uid'_NAME='\''&'\''/p | |
54 | ||
55 | g | |
56 | s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/ | |
57 | s/'\''/'\''\'\'\''/g | |
58 | s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p | |
59 | ||
60 | g | |
61 | s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/ | |
62 | s/'\''/'\''\'\'\''/g | |
63 | s/.*/export GIT_'$uid'_DATE='\''&'\''/p | |
64 | ||
65 | q | |
66 | } | |
67 | ' | |
68 | ||
69 | LANG=C LC_ALL=C sed -ne "$pick_id_script" | |
70 | # Ensure non-empty id name. | |
71 | echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\"" | |
72 | } | |
73 | ||
6f6826c5 | 74 | tempdir=.git-rewrite |
6f6826c5 JS |
75 | filter_env= |
76 | filter_tree= | |
77 | filter_index= | |
78 | filter_parent= | |
79 | filter_msg=cat | |
5be60078 | 80 | filter_commit='git commit-tree "$@"' |
6f6826c5 | 81 | filter_tag_name= |
685ef546 | 82 | filter_subdir= |
6f6826c5 JS |
83 | while case "$#" in 0) usage;; esac |
84 | do | |
85 | case "$1" in | |
86 | --) | |
87 | shift | |
88 | break | |
89 | ;; | |
90 | -*) | |
91 | ;; | |
92 | *) | |
93 | break; | |
94 | esac | |
95 | ||
96 | # all switches take one argument | |
97 | ARG="$1" | |
98 | case "$#" in 1) usage ;; esac | |
99 | shift | |
100 | OPTARG="$1" | |
101 | shift | |
102 | ||
103 | case "$ARG" in | |
104 | -d) | |
105 | tempdir="$OPTARG" | |
106 | ;; | |
6f6826c5 JS |
107 | --env-filter) |
108 | filter_env="$OPTARG" | |
109 | ;; | |
110 | --tree-filter) | |
111 | filter_tree="$OPTARG" | |
112 | ;; | |
113 | --index-filter) | |
114 | filter_index="$OPTARG" | |
115 | ;; | |
116 | --parent-filter) | |
117 | filter_parent="$OPTARG" | |
118 | ;; | |
119 | --msg-filter) | |
120 | filter_msg="$OPTARG" | |
121 | ;; | |
122 | --commit-filter) | |
123 | filter_commit="$OPTARG" | |
124 | ;; | |
125 | --tag-name-filter) | |
126 | filter_tag_name="$OPTARG" | |
127 | ;; | |
685ef546 JS |
128 | --subdirectory-filter) |
129 | filter_subdir="$OPTARG" | |
130 | ;; | |
6f6826c5 JS |
131 | *) |
132 | usage | |
133 | ;; | |
134 | esac | |
135 | done | |
136 | ||
137 | dstbranch="$1" | |
2766ce28 | 138 | shift |
6f6826c5 | 139 | test -n "$dstbranch" || die "missing branch name" |
5be60078 | 140 | git show-ref "refs/heads/$dstbranch" 2> /dev/null && |
6f6826c5 JS |
141 | die "branch $dstbranch already exists" |
142 | ||
143 | test ! -e "$tempdir" || die "$tempdir already exists, please remove it" | |
144 | mkdir -p "$tempdir/t" | |
145 | cd "$tempdir/t" | |
146 | workdir="$(pwd)" | |
147 | ||
148 | case "$GIT_DIR" in | |
149 | /*) | |
150 | ;; | |
151 | *) | |
9489d0f1 | 152 | GIT_DIR="$(pwd)/../../$GIT_DIR" |
6f6826c5 JS |
153 | ;; |
154 | esac | |
9489d0f1 | 155 | export GIT_DIR GIT_WORK_TREE=. |
6f6826c5 JS |
156 | |
157 | export GIT_INDEX_FILE="$(pwd)/../index" | |
5be60078 | 158 | git read-tree # seed the index file |
6f6826c5 JS |
159 | |
160 | ret=0 | |
161 | ||
162 | ||
163 | mkdir ../map # map old->new commit ids for rewriting parents | |
164 | ||
685ef546 JS |
165 | case "$filter_subdir" in |
166 | "") | |
5be60078 | 167 | git rev-list --reverse --topo-order --default HEAD \ |
813b4734 | 168 | --parents "$@" |
685ef546 JS |
169 | ;; |
170 | *) | |
5be60078 | 171 | git rev-list --reverse --topo-order --default HEAD \ |
cfabd6ee | 172 | --parents --full-history "$@" -- "$filter_subdir" |
685ef546 | 173 | esac > ../revs |
9d6f220c | 174 | commits=$(wc -l <../revs | tr -d " ") |
6f6826c5 JS |
175 | |
176 | test $commits -eq 0 && die "Found nothing to rewrite" | |
177 | ||
178 | i=0 | |
813b4734 | 179 | while read commit parents; do |
c12764b8 | 180 | i=$(($i+1)) |
5efb48b5 | 181 | printf "\rRewrite $commit ($i/$commits)" |
6f6826c5 | 182 | |
685ef546 JS |
183 | case "$filter_subdir" in |
184 | "") | |
5be60078 | 185 | git read-tree -i -m $commit |
685ef546 JS |
186 | ;; |
187 | *) | |
5be60078 | 188 | git read-tree -i -m $commit:"$filter_subdir" |
685ef546 | 189 | esac |
6f6826c5 JS |
190 | |
191 | export GIT_COMMIT=$commit | |
5be60078 | 192 | git cat-file commit "$commit" >../commit |
6f6826c5 | 193 | |
8c1ce0f4 JS |
194 | eval "$(set_ident AUTHOR <../commit)" || |
195 | die "setting author failed for commit $commit" | |
196 | eval "$(set_ident COMMITTER <../commit)" || | |
197 | die "setting committer failed for commit $commit" | |
198 | eval "$filter_env" < /dev/null || | |
199 | die "env filter failed: $filter_env" | |
6f6826c5 JS |
200 | |
201 | if [ "$filter_tree" ]; then | |
5be60078 | 202 | git checkout-index -f -u -a |
6f6826c5 JS |
203 | # files that $commit removed are now still in the working tree; |
204 | # remove them, else they would be added again | |
5be60078 | 205 | git ls-files -z --others | xargs -0 rm -f |
8c1ce0f4 JS |
206 | eval "$filter_tree" < /dev/null || |
207 | die "tree filter failed: $filter_tree" | |
208 | ||
5be60078 JH |
209 | git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \ |
210 | xargs -0 git update-index --add --replace --remove | |
211 | git ls-files -z --others | \ | |
212 | xargs -0 git update-index --add --replace --remove | |
6f6826c5 JS |
213 | fi |
214 | ||
8c1ce0f4 JS |
215 | eval "$filter_index" < /dev/null || |
216 | die "index filter failed: $filter_index" | |
6f6826c5 JS |
217 | |
218 | parentstr= | |
813b4734 | 219 | for parent in $parents; do |
3520e1e8 JS |
220 | for reparent in $(map "$parent"); do |
221 | parentstr="$parentstr -p $reparent" | |
222 | done | |
6f6826c5 JS |
223 | done |
224 | if [ "$filter_parent" ]; then | |
8c1ce0f4 JS |
225 | parentstr="$(echo "$parentstr" | eval "$filter_parent")" || |
226 | die "parent filter failed: $filter_parent" | |
6f6826c5 JS |
227 | fi |
228 | ||
229 | sed -e '1,/^$/d' <../commit | \ | |
8c1ce0f4 JS |
230 | eval "$filter_msg" > ../message || |
231 | die "msg filter failed: $filter_msg" | |
232 | sh -c "$filter_commit" "git commit-tree" \ | |
233 | $(git write-tree) $parentstr < ../message > ../map/$commit | |
6f6826c5 JS |
234 | done <../revs |
235 | ||
813b4734 | 236 | src_head=$(tail -n 1 ../revs | sed -e 's/ .*//') |
98409060 JS |
237 | target_head=$(head -n 1 ../map/$src_head) |
238 | case "$target_head" in | |
239 | '') | |
240 | echo Nothing rewritten | |
241 | ;; | |
242 | *) | |
5be60078 | 243 | git update-ref refs/heads/"$dstbranch" $target_head |
9d6f220c | 244 | if [ $(wc -l <../map/$src_head) -gt 1 ]; then |
98409060 JS |
245 | 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 |
246 | sed 's/^/ /' ../map/$src_head >&2 | |
247 | ret=1 | |
248 | fi | |
249 | ;; | |
250 | esac | |
6f6826c5 JS |
251 | |
252 | if [ "$filter_tag_name" ]; then | |
5be60078 | 253 | git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags | |
6f6826c5 JS |
254 | while read sha1 type ref; do |
255 | ref="${ref#refs/tags/}" | |
256 | # XXX: Rewrite tagged trees as well? | |
257 | if [ "$type" != "commit" -a "$type" != "tag" ]; then | |
258 | continue; | |
259 | fi | |
260 | ||
261 | if [ "$type" = "tag" ]; then | |
262 | # Dereference to a commit | |
263 | sha1t="$sha1" | |
5be60078 | 264 | sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue |
6f6826c5 JS |
265 | fi |
266 | ||
267 | [ -f "../map/$sha1" ] || continue | |
268 | new_sha1="$(cat "../map/$sha1")" | |
269 | export GIT_COMMIT="$sha1" | |
8c1ce0f4 JS |
270 | new_ref="$(echo "$ref" | eval "$filter_tag_name")" || |
271 | die "tag name filter failed: $filter_tag_name" | |
6f6826c5 JS |
272 | |
273 | echo "$ref -> $new_ref ($sha1 -> $new_sha1)" | |
274 | ||
275 | if [ "$type" = "tag" ]; then | |
276 | # Warn that we are not rewriting the tag object itself. | |
277 | warn "unreferencing tag object $sha1t" | |
278 | fi | |
279 | ||
5be60078 | 280 | git update-ref "refs/tags/$new_ref" "$new_sha1" |
6f6826c5 JS |
281 | done |
282 | fi | |
283 | ||
284 | cd ../.. | |
285 | rm -rf "$tempdir" | |
5efb48b5 | 286 | printf "\nRewritten history saved to the $dstbranch branch\n" |
6f6826c5 JS |
287 | |
288 | exit $ret |