Skip to content

Commit ce80ca5

Browse files
peffgitster
authored andcommitted
git-sh-setup: refactor ident-parsing functions
The only ident-parsing function we currently provide is get_author_ident_from_commit. This is not very flexible for two reasons: 1. It takes a commit as an argument, and can't read from commit headers saved on disk. 2. It will only parse authors, not committers. This patch provides a more flexible interface which will parse multiple idents from a commit provide on stdin. We can easily use it as a building block for the current function to retain compatibility. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 87a5461 commit ce80ca5

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

git-sh-setup.sh

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -191,28 +191,52 @@ require_clean_work_tree () {
191191
fi
192192
}
193193
194+
# Generate a sed script to parse identities from a commit.
195+
#
196+
# Reads the commit from stdin, which should be in raw format (e.g., from
197+
# cat-file or "--pretty=raw").
198+
#
199+
# The first argument specifies the ident line to parse (e.g., "author"), and
200+
# the second specifies the environment variable to put it in (e.g., "AUTHOR"
201+
# for "GIT_AUTHOR_*"). Multiple pairs can be given to parse author and
202+
# committer.
203+
pick_ident_script () {
204+
while test $# -gt 0
205+
do
206+
lid=$1; shift
207+
uid=$1; shift
208+
printf '%s' "
209+
/^$lid /{
210+
s/'/'\\\\''/g
211+
h
212+
s/^$lid "'\([^<]*\) <[^>]*> .*$/\1/'"
213+
s/.*/GIT_${uid}_NAME='&'/p
214+
215+
g
216+
s/^$lid "'[^<]* <\([^>]*\)> .*$/\1/'"
217+
s/.*/GIT_${uid}_EMAIL='&'/p
218+
219+
g
220+
s/^$lid "'[^<]* <[^>]*> \(.*\)$/@\1/'"
221+
s/.*/GIT_${uid}_DATE='&'/p
222+
}
223+
"
224+
done
225+
echo '/^$/q'
226+
}
227+
228+
# Create a pick-script as above and feed it to sed. Stdout is suitable for
229+
# feeding to eval.
230+
parse_ident_from_commit () {
231+
LANG=C LC_ALL=C sed -ne "$(pick_ident_script "$@")"
232+
}
233+
234+
# Parse the author from a commit given as an argument. Stdout is suitable for
235+
# feeding to eval to set the usual GIT_* ident variables.
194236
get_author_ident_from_commit () {
195-
pick_author_script='
196-
/^author /{
197-
s/'\''/'\''\\'\'\''/g
198-
h
199-
s/^author \([^<]*\) <[^>]*> .*$/\1/
200-
s/.*/GIT_AUTHOR_NAME='\''&'\''/p
201-
202-
g
203-
s/^author [^<]* <\([^>]*\)> .*$/\1/
204-
s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
205-
206-
g
207-
s/^author [^<]* <[^>]*> \(.*\)$/@\1/
208-
s/.*/GIT_AUTHOR_DATE='\''&'\''/p
209-
210-
q
211-
}
212-
'
213237
encoding=$(git config i18n.commitencoding || echo UTF-8)
214238
git show -s --pretty=raw --encoding="$encoding" "$1" -- |
215-
LANG=C LC_ALL=C sed -ne "$pick_author_script"
239+
parse_ident_from_commit author AUTHOR
216240
}
217241
218242
# Clear repo-local GIT_* environment variables. Useful when switching to

0 commit comments

Comments
 (0)