Skip to content

Commit

Permalink
filter-branch: use git-sh-setup's ident parsing functions
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Oct 18, 2012
1 parent ce80ca5 commit 3c730fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
46 changes: 13 additions & 33 deletions git-filter-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,19 @@ EOF

eval "$functions"

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

set_ident () {
lid="$(echo "$1" | tr "[A-Z]" "[a-z]")"
uid="$(echo "$1" | tr "[a-z]" "[A-Z]")"
pick_id_script='
/^'$lid' /{
s/'\''/'\''\\'\'\''/g
h
s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
s/'\''/'\''\'\'\''/g
s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
g
s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
s/'\''/'\''\'\'\''/g
s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
g
s/^'$lid' [^<]* <[^>]*> \(.*\)$/@\1/
s/'\''/'\''\'\'\''/g
s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
q
}
'

LANG=C LC_ALL=C sed -ne "$pick_id_script"
# Ensure non-empty id name.
echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
parse_ident_from_commit author AUTHOR committer COMMITTER
finish_ident AUTHOR
finish_ident COMMITTER
}

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

eval "$(set_ident AUTHOR <../commit)" ||
die "setting author failed for commit $commit"
eval "$(set_ident COMMITTER <../commit)" ||
die "setting committer failed for commit $commit"
eval "$(set_ident <../commit)" ||
die "setting author/committer failed for commit $commit"
eval "$filter_env" < /dev/null ||
die "env filter failed: $filter_env"

Expand Down
5 changes: 3 additions & 2 deletions t/t7003-filter-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ test_expect_success 'author information is preserved' '
test_tick &&
GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
git branch preserved-author &&
git filter-branch -f --msg-filter "cat; \
(sane_unset GIT_AUTHOR_NAME &&
git filter-branch -f --msg-filter "cat; \
test \$GIT_COMMIT != $(git rev-parse master) || \
echo Hallo" \
preserved-author &&
preserved-author) &&
test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)
'

Expand Down

0 comments on commit 3c730fa

Please sign in to comment.