Skip to content

Commit 7e0f170

Browse files
dschogitster
authored andcommitted
filter-branch: provide the convenience functions also for commit filters
Move the convenience functions to the top of git-filter-branch.sh, and return from the script when the environment variable SOURCE_FUNCTIONS is set. By sourcing git-filter-branch with that variable set automatically, all commit filters may access the convenience functions like "map". Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f0fd889 commit 7e0f170

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

Documentation/git-filter-branch.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ OPTIONS
112112
As a special extension, the commit filter may emit multiple
113113
commit ids; in that case, ancestors of the original commit will
114114
have all of them as parents.
115-
+
116-
Note that the 'map' function is not available in the commit filter yet.
117-
This will be changed in a future version.
118115

119116
--tag-name-filter <command>::
120117
This is the filter for rewriting tag names. When passed,

git-filter-branch.sh

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@
88
# a new branch. You can specify a number of filters to modify the commits,
99
# files and trees.
1010

11-
USAGE="[--env-filter <command>] [--tree-filter <command>] \
12-
[--index-filter <command>] [--parent-filter <command>] \
13-
[--msg-filter <command>] [--commit-filter <command>] \
14-
[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
15-
[--original <namespace>] [-d <directory>] [-f | --force] \
16-
[<rev-list options>...]"
17-
18-
. git-sh-setup
19-
2011
warn () {
2112
echo "$*" >&2
2213
}
@@ -75,6 +66,20 @@ set_ident () {
7566
echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
7667
}
7768

69+
# This script can be sourced by the commit filter to get the functions
70+
test "a$SOURCE_FUNCTIONS" = a1 && return
71+
this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0")
72+
export this_script
73+
74+
USAGE="[--env-filter <command>] [--tree-filter <command>] \
75+
[--index-filter <command>] [--parent-filter <command>] \
76+
[--msg-filter <command>] [--commit-filter <command>] \
77+
[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
78+
[--original <namespace>] [-d <directory>] [-f | --force] \
79+
[<rev-list options>...]"
80+
81+
. git-sh-setup
82+
7883
tempdir=.git-rewrite
7984
filter_env=
8085
filter_tree=
@@ -131,7 +136,7 @@ do
131136
filter_msg="$OPTARG"
132137
;;
133138
--commit-filter)
134-
filter_commit="$OPTARG"
139+
filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG"
135140
;;
136141
--tag-name-filter)
137142
filter_tag_name="$OPTARG"

t/t7003-filter-branch.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,14 @@ test_expect_success 'barf on invalid name' '
159159
! git filter-branch -f HEAD^
160160
'
161161

162+
test_expect_success '"map" works in commit filter' '
163+
git filter-branch -f --commit-filter "\
164+
parent=\$(git rev-parse \$GIT_COMMIT^) &&
165+
mapped=\$(map \$parent) &&
166+
actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
167+
test \$mapped = \$actual &&
168+
git commit-tree \"\$@\";" master~2..master &&
169+
git rev-parse --verify master
170+
'
171+
162172
test_done

0 commit comments

Comments
 (0)