@matheusbsbs / matheus-bagliano / commits / f95eef15f2
Fork 0

filter-branch: introduce convenience function "skip_commit"

With this function, a commit filter can leave out unwanted commits (such as temporary commits). It does _not_ undo the changeset corresponding to that commit, but it _skips_ the revision. IOW no tree object is changed by this. If you like to commit early and often, but want to filter out all intermediate commits, marked by "@@@" in the commit message, you can now do this with git filter-branch --commit-filter ' if git cat-file commit $GIT_COMMIT | grep '@@@' > /dev/null; then skip_commit "$@"; else git commit-tree "$@"; fi' newbranch Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Aug 31, 2007 at 20:06 UTC f95eef15f2f8a336b9a42749f5458c841a5a5d63
3 files changed +42 -14
Documentation/git-filter-branch.txt
+27 -7
@@ -112,6 +112,11 @@ OPTIONS
112 As a special extension, the commit filter may emit multiple
113 commit ids; in that case, ancestors of the original commit will
114 have all of them as parents.
115 ++
116 +You can use the 'map' convenience function in this filter, and other
117 +convenience functions, too. For example, calling 'skip_commit "$@"'
118 +will leave out the current commit (but not its changes! If you want
119 +that, use gitlink:git-rebase[1] instead).
120
121 --tag-name-filter <command>::
122 This is the filter for rewriting tag names. When passed,
@@ -209,24 +214,39 @@ To remove commits authored by "Darl McBribe" from the history:
214 git filter-branch --commit-filter '
215 if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ];
216 then
212 - shift;
213 - while [ -n "$1" ];
214 - do
215 - shift;
216 - echo "$1";
217 - shift;
218 - done;
217 + skip_commit "$@";
218 else
219 git commit-tree "$@";
220 fi' HEAD
221 ------------------------------------------------------------------------------
222
223 +Note that the changes introduced by the commits, and not reverted by
224 +subsequent commits, will still be in the rewritten branch. If you want
225 +to throw out _changes_ together with the commits, you should use the
226 +interactive mode of gitlink:git-rebase[1].
227 +
228 +The function 'skip_commits' is defined as follows:
229 +
230 +--------------------------
231 +skip_commit()
232 +{
233 + shift;
234 + while [ -n "$1" ];
235 + do
236 + shift;
237 + map "$1";
238 + shift;
239 + done;
240 +}
241 +--------------------------
242 +
243 The shift magic first throws away the tree id and then the -p
244 parameters. Note that this handles merges properly! In case Darl
245 committed a merge between P1 and P2, it will be propagated properly
246 and all children of the merge will become merge commits with P1,P2
247 as their parents instead of the merge commit.
248
249 +
250 To restrict rewriting to only part of the history, specify a revision
251 range in addition to the new branch name. The new branch name will
252 point to the top-most revision that a 'git rev-list' of this range
git-filter-branch.sh
+14
@@ -23,6 +23,20 @@ map()
23 fi
24 }
25
26 +# if you run 'skip_commit "$@"' in a commit filter, it will print
27 +# the (mapped) parents, effectively skipping the commit.
28 +
29 +skip_commit()
30 +{
31 + shift;
32 + while [ -n "$1" ];
33 + do
34 + shift;
35 + map "$1";
36 + shift;
37 + done;
38 +}
39 +
40 # override die(): this version puts in an extra line break, so that
41 # the progress is still visible
42
t/t7003-filter-branch.sh
+1 -7
@@ -138,13 +138,7 @@ test_expect_success "remove a certain author's commits" '
138 git-filter-branch -f --commit-filter "\
139 if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
140 then\
141 - shift;\
142 - while [ -n \"\$1\" ];\
143 - do\
144 - shift;\
145 - echo \"\$1\";\
146 - shift;\
147 - done;\
141 + skip_commit \"\$@\";
142 else\
143 git commit-tree \"\$@\";\
144 fi" removed-author &&