Skip to content

Commit 3c730fa

Browse files
peffgitster
authored andcommitted
filter-branch: use git-sh-setup's ident parsing functions
This saves us some code, but it also reduces the number of processes we start for each filtered commit. Since we can parse both author and committer in the same sed invocation, we save one process. And since the new interface avoids tr, we save 4 processes. It also avoids using "tr", which has had some odd portability problems reported with from Solaris's xpg6 version. We also tweak one of the tests in t7003 to double-check that we are properly exporting the variables (because test-lib.sh exports GIT_AUTHOR_NAME, it will be automatically exported in subprograms. We override this to make sure that filter-branch handles it properly itself). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ce80ca5 commit 3c730fa

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

git-filter-branch.sh

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,19 @@ EOF
6464

6565
eval "$functions"
6666

67-
# When piped a commit, output a script to set the ident of either
68-
# "author" or "committer
67+
finish_ident() {
68+
# Ensure non-empty id name.
69+
echo "case \"\$GIT_$1_NAME\" in \"\") GIT_$1_NAME=\"\${GIT_$1_EMAIL%%@*}\" && export GIT_$1_NAME;; esac"
70+
# And make sure everything is exported.
71+
echo "export GIT_$1_NAME"
72+
echo "export GIT_$1_EMAIL"
73+
echo "export GIT_$1_DATE"
74+
}
6975

7076
set_ident () {
71-
lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
72-
uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
73-
pick_id_script='
74-
/^'$lid' /{
75-
s/'\''/'\''\\'\'\''/g
76-
h
77-
s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
78-
s/'\''/'\''\'\'\''/g
79-
s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
80-
81-
g
82-
s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
83-
s/'\''/'\''\'\'\''/g
84-
s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
85-
86-
g
87-
s/^'$lid' [^<]* <[^>]*> \(.*\)$/@\1/
88-
s/'\''/'\''\'\'\''/g
89-
s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
90-
91-
q
92-
}
93-
'
94-
95-
LANG=C LC_ALL=C sed -ne "$pick_id_script"
96-
# Ensure non-empty id name.
97-
echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
77+
parse_ident_from_commit author AUTHOR committer COMMITTER
78+
finish_ident AUTHOR
79+
finish_ident COMMITTER
9880
}
9981

10082
USAGE="[--env-filter <command>] [--tree-filter <command>]
@@ -320,10 +302,8 @@ while read commit parents; do
320302
git cat-file commit "$commit" >../commit ||
321303
die "Cannot read commit $commit"
322304

323-
eval "$(set_ident AUTHOR <../commit)" ||
324-
die "setting author failed for commit $commit"
325-
eval "$(set_ident COMMITTER <../commit)" ||
326-
die "setting committer failed for commit $commit"
305+
eval "$(set_ident <../commit)" ||
306+
die "setting author/committer failed for commit $commit"
327307
eval "$filter_env" < /dev/null ||
328308
die "env filter failed: $filter_env"
329309

t/t7003-filter-branch.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,11 @@ test_expect_success 'author information is preserved' '
167167
test_tick &&
168168
GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
169169
git branch preserved-author &&
170-
git filter-branch -f --msg-filter "cat; \
170+
(sane_unset GIT_AUTHOR_NAME &&
171+
git filter-branch -f --msg-filter "cat; \
171172
test \$GIT_COMMIT != $(git rev-parse master) || \
172173
echo Hallo" \
173-
preserved-author &&
174+
preserved-author) &&
174175
test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
175176
'
176177

0 commit comments

Comments
 (0)