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