| 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 | # |
| 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. |
| 10 | |
| 11 | set -e |
| 12 | |
| 13 | USAGE="git-filter-branch [-d TEMPDIR] [FILTERS] DESTBRANCH [REV-RANGE]" |
| 14 | . git-sh-setup |
| 15 | |
| 16 | warn () { |
| 17 | echo "$*" >&2 |
| 18 | } |
| 19 | |
| 20 | map() |
| 21 | { |
| 22 | # if it was not rewritten, take the original |
| 23 | if test -r "$workdir/../map/$1" |
| 24 | then |
| 25 | cat "$workdir/../map/$1" |
| 26 | else |
| 27 | echo "$1" |
| 28 | fi |
| 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 | |
| 64 | tempdir=.git-rewrite |
| 65 | filter_env= |
| 66 | filter_tree= |
| 67 | filter_index= |
| 68 | filter_parent= |
| 69 | filter_msg=cat |
| 70 | filter_commit='git commit-tree "$@"' |
| 71 | filter_tag_name= |
| 72 | filter_subdir= |
| 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 | ;; |
| 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 | ;; |
| 118 | --subdirectory-filter) |
| 119 | filter_subdir="$OPTARG" |
| 120 | ;; |
| 121 | *) |
| 122 | usage |
| 123 | ;; |
| 124 | esac |
| 125 | done |
| 126 | |
| 127 | dstbranch="$1" |
| 128 | shift |
| 129 | test -n "$dstbranch" || die "missing branch name" |
| 130 | git show-ref "refs/heads/$dstbranch" 2> /dev/null && |
| 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 | *) |
| 142 | GIT_DIR="$(pwd)/../../$GIT_DIR" |
| 143 | ;; |
| 144 | esac |
| 145 | export GIT_DIR GIT_WORK_TREE=. |
| 146 | |
| 147 | export GIT_INDEX_FILE="$(pwd)/../index" |
| 148 | git read-tree # seed the index file |
| 149 | |
| 150 | ret=0 |
| 151 | |
| 152 | |
| 153 | mkdir ../map # map old->new commit ids for rewriting parents |
| 154 | |
| 155 | case "$filter_subdir" in |
| 156 | "") |
| 157 | git rev-list --reverse --topo-order --default HEAD \ |
| 158 | --parents "$@" |
| 159 | ;; |
| 160 | *) |
| 161 | git rev-list --reverse --topo-order --default HEAD \ |
| 162 | --parents --full-history "$@" -- "$filter_subdir" |
| 163 | esac > ../revs |
| 164 | commits=$(cat ../revs | wc -l | tr -d " ") |
| 165 | |
| 166 | test $commits -eq 0 && die "Found nothing to rewrite" |
| 167 | |
| 168 | i=0 |
| 169 | while read commit parents; do |
| 170 | i=$(($i+1)) |
| 171 | printf "\rRewrite $commit ($i/$commits)" |
| 172 | |
| 173 | case "$filter_subdir" in |
| 174 | "") |
| 175 | git read-tree -i -m $commit |
| 176 | ;; |
| 177 | *) |
| 178 | git read-tree -i -m $commit:"$filter_subdir" |
| 179 | esac |
| 180 | |
| 181 | export GIT_COMMIT=$commit |
| 182 | git cat-file commit "$commit" >../commit |
| 183 | |
| 184 | eval "$(set_ident AUTHOR <../commit)" |
| 185 | eval "$(set_ident COMMITTER <../commit)" |
| 186 | eval "$filter_env" < /dev/null |
| 187 | |
| 188 | if [ "$filter_tree" ]; then |
| 189 | git checkout-index -f -u -a |
| 190 | # files that $commit removed are now still in the working tree; |
| 191 | # remove them, else they would be added again |
| 192 | git ls-files -z --others | xargs -0 rm -f |
| 193 | eval "$filter_tree" < /dev/null |
| 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 |
| 198 | fi |
| 199 | |
| 200 | eval "$filter_index" < /dev/null |
| 201 | |
| 202 | parentstr= |
| 203 | for parent in $parents; do |
| 204 | for reparent in $(map "$parent"); do |
| 205 | parentstr="$parentstr -p $reparent" |
| 206 | done |
| 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" | \ |
| 214 | sh -c "$filter_commit" "git commit-tree" $(git write-tree) \ |
| 215 | $parentstr > ../map/$commit |
| 216 | done <../revs |
| 217 | |
| 218 | src_head=$(tail -n 1 ../revs | sed -e 's/ .*//') |
| 219 | target_head=$(head -n 1 ../map/$src_head) |
| 220 | case "$target_head" in |
| 221 | '') |
| 222 | echo Nothing rewritten |
| 223 | ;; |
| 224 | *) |
| 225 | git update-ref refs/heads/"$dstbranch" $target_head |
| 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 |
| 233 | |
| 234 | if [ "$filter_tag_name" ]; then |
| 235 | git for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags | |
| 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" |
| 246 | sha1="$(git rev-parse "$sha1"^{commit} 2>/dev/null)" || continue |
| 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 | |
| 261 | git update-ref "refs/tags/$new_ref" "$new_sha1" |
| 262 | done |
| 263 | fi |
| 264 | |
| 265 | cd ../.. |
| 266 | rm -rf "$tempdir" |
| 267 | printf "\nRewritten history saved to the $dstbranch branch\n" |
| 268 | |
| 269 | exit $ret |