Test commit
[cogito/jonas.git] / cg-admin-rewritehist
blobb4347f67d28b20d48a910d291d53710dbcd38fd9
1 #!/usr/bin/env bash
3 # Rewrite revision history
4 # Copyright (c) Petr Baudis, 2006
6 # Lets you rewrite GIT revision history by creating a new branch from
7 # your current branch by applying custom filters on each revision.
8 # Those filters can modify each tree (e.g. removing a file or running
9 # a perl rewrite on all files) or information about each commit.
10 # Otherwise, all information (including original commit times or merge
11 # information) will be preserved.
13 # The command takes the new branch name as a mandatory argument and
14 # the filters as optional arguments. If you specify no filters, the
15 # commits will be recommitted without any changes, which would normally
16 # have no effect and result with the new branch pointing to the same
17 # branch as your current branch. (Nevertheless, this may be useful in
18 # the future for compensating for some Git bugs or such, therefore
19 # such a usage is permitted.)
21 # WARNING! The rewritten history will have different ids for all the
22 # objects and will not converge with the original branch. You will not
23 # be able to easily push and distribute the rewritten branch. Please do
24 # not use this command if you do not know the full implications, and
25 # avoid using it anyway - do not do what a simple single commit on top
26 # of the current version would fix.
28 # Always verify that the rewritten version is correct before disposing
29 # the original branch.
31 # Note that since this operation is extensively I/O expensive, it might
32 # be a good idea to do it off-disk, e.g. on tmpfs. Reportedly the speedup
33 # is very noticeable.
35 # OPTIONS
36 # -------
37 # -d TEMPDIR:: The path to the temporary tree used for rewriting
38 # When applying a tree filter, the command needs to temporary
39 # checkout the tree to some directory, which may consume
40 # considerable space in case of large projects. By default it
41 # does this in the '.git-rewrite/' directory but you can override
42 # that choice by this parameter.
44 # -r STARTREV:: The commit id to start the rewrite at
45 # Normally, the command will rewrite the entire history. If you
46 # pass this argument, though, this will be the first commit it
47 # will rewrite and keep the previous commits intact.
49 # -k KEEPREV:: A commit id until which _not_ to rewrite history
50 # If you pass this argument, this commit and all of its
51 # predecessors are kept intact.
53 # Filters
54 # ~~~~~~~
55 # The filters are applied in the order as listed below. The COMMAND
56 # argument is always evaluated in shell using the 'eval' command.
57 # The $GIT_COMMIT environment variable is permanently set to contain
58 # the id of the commit being rewritten. The author/committer environment
59 # variables are set before the first filter is run.
61 # A 'map' function is available that takes an "original sha1 id" argument
62 # and outputs a "rewritten sha1 id" if the commit has been already
63 # rewritten, fails otherwise; the 'map' function can return several
64 # ids on separate lines if your commit filter emitted multiple commits
65 # (see below).
67 # --env-filter COMMAND:: The filter for modifying environment
68 # This is the filter for modifying the environment in which
69 # the commit will be performed. Specifically, you might want
70 # to rewrite the author/committer name/email/time environment
71 # variables (see `cg-commit` for details). Do not forget to
72 # re-export the variables.
74 # --tree-filter COMMAND:: The filter for rewriting tree (and its contents)
75 # This is the filter for rewriting the tree and its contents.
76 # The COMMAND argument is evaluated in shell with the working
77 # directory set to the root of the checked out tree. The new tree
78 # is then used as-is (new files are auto-added, disappeared files
79 # are auto-removed - .gitignore files nor any other ignore rules
80 # HAVE NO EFFECT!).
82 # --index-filter COMMAND:: The filter for rewriting index
83 # This is the filter for rewriting the Git's directory index.
84 # It is similar to the tree filter but does not check out the
85 # tree, which makes it much faster. However, you must use the
86 # lowlevel Git index manipulation commands to do your work.
88 # --parent-filter COMMAND:: The filter for rewriting parents
89 # This is the filter for rewriting the commit's parent list.
90 # It will receive the parent string on stdin and shall output
91 # the new parent string on stdout. The parent string is in
92 # format accepted by `git-commit-tree`: empty for initial
93 # commit, "-p parent" for a normal commit and "-p parent1
94 # -p parent2 -p parent3 ..." for a merge commit.
96 # --msg-filter COMMAND:: The filter for rewriting commit message
97 # This is the filter for rewriting the commit messages.
98 # The COMMAND argument is evaluated in shell with the original
99 # commit message on standard input; its standard output is
100 # is used as the new commit message.
102 # --commit-filter COMMAND:: The filter for performing the commit
103 # If this filter is passed, it will be called instead of the
104 # `git-commit-tree` command, with those arguments:
106 # TREE_ID [-p PARENT_COMMIT_ID]...
108 # and the log message on stdin. The commit id is expected on
109 # stdout. As a special extension, the commit filter may emit
110 # multiple commit ids; in that case, all of them will be used
111 # as parents instead of the original commit in further commits.
113 # --tag-name-filter COMMAND:: The filter for rewriting tag names.
114 # If this filter is passed, it will be called for every tag ref
115 # that points to a rewritten object (or to a tag object which
116 # points to a rewritten object). The original tag name is passed
117 # via standard input, and the new tag name is expected on standard
118 # output.
120 # The original tags are not deleted, but can be overwritten;
121 # use "--tag-name-filter=cat" to simply update the tags. In this
122 # case, be very careful and make sure you have the old tags
123 # backed up in case the conversion has run afoul.
125 # Note that there is currently no support for proper rewriting of
126 # tag objects; in layman terms, if the tag has a message or signature
127 # attached, the rewritten tag won't have it. Sorry. (It is by
128 # definition impossible to preserve signatures at any rate, though.)
130 # EXAMPLE USAGE
131 # -------------
132 # Suppose you want to remove a file (containing confidential information
133 # or copyright violation) from all commits:
135 # cg-admin-rewritehist --tree-filter 'rm filename' newbranch
137 # A significantly faster version:
139 # cg-admin-rewritehist --index-filter 'git-update-index --remove filename' newbranch
141 # Now, you will get the rewritten history saved in the branch 'newbranch'
142 # (your current branch is left untouched).
144 # To "etch-graft" a commit to the revision history (set a commit to be
145 # the parent of the current initial commit and propagate that):
147 # cg-admin-rewritehist --parent-filter sed\ 's/^$/-p graftcommitid/' newbranch
149 # (if the parent string is empty - therefore we are dealing with the
150 # initial commit - add graftcommit as a parent). Note that this assumes
151 # history with a single root (that is, no cg-merge -j happened). If this
152 # is not the case, use:
154 # cg-admin-rewritehist --parent-filter 'cat; [ "$GIT_COMMIT" = "COMMIT" ] && echo "-p GRAFTCOMMIT"' newbranch
156 # To remove commits authored by "Darl McBribe" from the history:
158 # cg-admin-rewritehist --commit-filter 'if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ]; then shift; while [ -n "$1" ]; do shift; echo "$1"; shift; done; else git-commit-tree "$@"; fi' newbranch
160 # (the shift magic first throws away the tree id and then the -p
161 # parameters). Note that this handles merges properly! In case Darl
162 # committed a merge between P1 and P2, it will be propagated properly
163 # and all children of the merge will become merge commits with P1,P2
164 # as their parents instead of the merge commit.
166 # To restrict rewriting to only part of the history, use -r or -k or both.
167 # Consider this history:
169 # D--E--F--G--H
170 # / /
171 # A--B-----C
173 # To rewrite only commits F,G,H, use:
175 # cg-admin-rewritehist -r F ...
177 # To rewrite commits E,F,G,H, use one of these:
179 # cg-admin-rewritehist -r E -k C ...
180 # cg-admin-rewritehist -k D -k C ...
182 # Testsuite: TODO
184 set -e
186 USAGE="cg-admin-rewritehist [-d TEMPDIR] [-r STARTREV]... [-k KEEPREV]... [FILTERS] DESTBRANCH"
187 _git_wc_unneeded=1
188 _git_requires_root=1
190 . "${COGITO_LIB}"cg-Xlib || exit 1
193 map()
195 [ -r "$workdir/../map/$1" ] || return 1
196 cat "$workdir/../map/$1"
200 tempdir=.git-rewrite
201 declare -a unchanged
202 filter_env=
203 filter_tree=
204 filter_index=
205 filter_parent=
206 filter_msg=cat
207 filter_commit='git-commit-tree "$@"'
208 filter_tag_name=
209 while optparse; do
210 if optparse -d=; then
211 tempdir="$OPTARG"
212 elif optparse -r=; then
213 unchanged=(${unchanged[@]} $(cg-object-id -p "$OPTARG")) || exit 1
214 elif optparse -k=; then
215 unchanged=(${unchanged[@]} $(cg-object-id -c "$OPTARG")) || exit 1
216 elif optparse --env-filter=; then
217 filter_env="$OPTARG"
218 elif optparse --tree-filter=; then
219 filter_tree="$OPTARG"
220 elif optparse --index-filter=; then
221 filter_index="$OPTARG"
222 elif optparse --parent-filter=; then
223 filter_parent="$OPTARG"
224 elif optparse --msg-filter=; then
225 filter_msg="$OPTARG"
226 elif optparse --commit-filter=; then
227 filter_commit="$OPTARG"
228 elif optparse --tag-name-filter=; then
229 filter_tag_name="$OPTARG"
230 else
231 optfail
233 done
235 dstbranch="${ARGS[0]}"
236 [ -n "$dstbranch" ] || die "missing branch name"
237 ! exists_ref "refs/heads/$dstbranch" || die "branch $dstbranch already exists"
238 [ ! -s "$_git/branches/$dstbranch" ] || die "branch $dstbranch is already a remote branch"
240 [ ! -e "$tempdir" ] || die "$tempdir already exists, please remove it"
241 mkdir -p "$tempdir/t"
242 cd "$tempdir/t"
243 workdir="$(pwd)"
245 [ -n "$GIT_DIR" ] || export GIT_DIR=.git
246 [[ "$GIT_DIR" == /* ]] || export GIT_DIR="$(pwd)/../../$GIT_DIR"
247 export GIT_INDEX_FILE="$(pwd)/../index"
248 git-read-tree # seed the index file
250 ret=0
253 mkdir ../map # map old->new commit ids for rewriting parents
255 # seed with identity mappings for the parents where we start off
256 for commit in ${unchanged[@]}; do
257 echo $commit > ../map/$commit
258 done
260 git-rev-list --topo-order HEAD ${unchanged[@]/#/^} | tac >../revs
261 commits=$(cat ../revs | wc -l)
263 if [ $commits -eq 0 ]; then
264 die "Found nothing to rewrite"
268 while read commit; do
269 i=$((i+1))
270 echo -n "$commit ($i/$commits) "
272 git-read-tree -i -m $commit
274 export GIT_COMMIT=$commit
275 git-cat-file commit "$commit" >../commit
277 eval "$(pick_author <../commit)"
278 eval "$(pick_id committer COMMITTER <../commit)"
279 eval "$filter_env"
281 if [ "$filter_tree" ]; then
282 git-checkout-index -f -u -a
283 # files that $commit removed are now still in the working tree;
284 # remove them, else they would be added again
285 git-ls-files -z --others | xargs -0 rm -f
286 eval "$filter_tree"
287 git-diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
288 xargs -0 git-update-index --add --replace --remove
289 git-ls-files -z --others | \
290 xargs -0 git-update-index --add --replace --remove
293 eval "$filter_index"
295 parentstr=
296 for parent in $(cg-object-id -p $commit); do
297 if [ -r "../map/$parent" ]; then
298 for reparent in $(cat "../map/$parent"); do
299 parentstr="$parentstr -p $reparent"
300 done
301 else
302 die "assertion failed: parent $parent for commit $commit not found in rewritten ones"
304 done
305 if [ "$filter_parent" ]; then
306 parentstr="$(echo "$parentstr" | eval "$filter_parent")"
309 sed -e '1,/^$/d' <../commit | \
310 eval "$filter_msg" | \
311 sh -c "$filter_commit" git-commit-tree $(git-write-tree) $parentstr | \
312 tee ../map/$commit
313 done <../revs
315 git-update-ref refs/heads/"$dstbranch" $(head -n 1 ../map/$(tail -n 1 ../revs))
316 if [ "$(cat ../map/$(tail -n 1 ../revs) | wc -l)" -gt 1 ]; then
317 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
318 sed 's/^/ /' ../map/$(tail -n 1 ../revs) >&2
319 ret=1
322 if [ "$filter_tag_name" ]; then
323 git-for-each-ref --format='%(objectname) %(objecttype) %(refname)' refs/tags |
324 while read sha1 type ref; do
325 ref="${ref#refs/tags/}"
326 # XXX: Rewrite tagged trees as well?
327 if [ "$type" != "commit" -a "$type" != "tag" ]; then
328 continue;
331 if [ "$type" = "tag" ]; then
332 # Dereference to a commit
333 sha1t="$sha1"
334 sha1="$(cg-object-id -c "$sha1" 2>/dev/null)" || continue
337 [ -f "../map/$sha1" ] || continue
338 new_sha1="$(cat "../map/$sha1")"
339 export GIT_COMMIT="$sha1"
340 new_ref="$(echo "$ref" | eval "$filter_tag_name")"
342 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
344 if [ "$type" = "tag" ]; then
345 # Warn that we are not rewriting the tag object itself.
346 warn "unreferencing tag object $sha1t"
349 git-update-ref "refs/tags/$new_ref" "$new_sha1"
350 done
353 cd ../..
354 rm -rf "$tempdir"
355 echo "Rewritten history saved to the $dstbranch branch"
357 exit $ret