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