public
Last active

'authors-prog' for git-svn when importing subversion repositories from SourceForge

  • Download Gist
sf2email.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#!/bin/bash
 
# sf2email v0.2 (2011-05-11)
# By Robert Quattlebaum <darco@deepdarc.com>
#
# PUBLIC DOMAIN
#
# This shell script was originally designed to be used as the 'authors-prog'
# for git-svn. It takes the sourceforge username as an argument, and outputs
# the name and email address of the individual to stdout.
#
# The results are cached locally to speed up future checks.
#
# This script uses cURL to grab the user's sourceforge page and then scrapes
# their real name from the HTML.
#
# Up-to-date versions of this script can be found here:
#
# <https://gist.github.com/gists/935424>
#
# Warning: Currently only tested on MacOS X.
#
# Feedback and improvements are welcome!
#
 
[ $# = 1 ] || {
[ $# > 1 ] && echo "$0: error: Too many arguments."
echo "sf2email - Converts a sourceforge username to an name+email line"
echo "Usage: $0 " '<sf-username>'
exit 1
} 2>&1
 
which -s curl || {
echo "$0: error: 'curl' command not found."
exit 2
} 1>&2
 
SF_USERNAME=$1
 
# Handle "(no author)" cases.
[ "$SF_USERNAME" = "(no author)" ] && {
echo "(no author) <nobody@localhost>"
exit 0
}
 
SF_USERNAME_LC=`echo $1 | tr '[:upper:]' '[:lower:]' | tr -C -d '[:alnum:].'`
 
[ "$SF_USERNAME_LC" = "." -o "$SF_USERNAME_LC" = ".." -o "$SF_USERNAME_LC" = "" ] && {
echo "$0: error: Invalid username \"$SF_USERNAME\""
exit 2
} 1>&2
 
CACHE_DIR="$TMPDIR/sf2email.cache"
 
# If the cache dir doesn't exist, make it.
[ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
 
if [ -r "$CACHE_DIR/$SF_USERNAME_LC" ]
then # Cache hit
SF_EMAIL=`cat "$CACHE_DIR/$SF_USERNAME_LC"`
else # Cache miss
# Try to scrape their real name from their user page
SF_REALNAME="$(
curl -s "http://sourceforge.net/users/$SF_USERNAME" |
grep '<label>Public Name:</label>' |
sed -E '
s/<[^>]*>//g;
s/Public Name://g;
s/^[[:space:]]*([^[:space:]])/\1/;
s/([^[:space:]])[[:space:]]*$/\1/;
'
)"
# If the above command didn't work, we can just try using the username as-is
[ "$SF_REALNAME" = "" ] && {
SF_REALNAME=$SF_USERNAME
echo "$0: warning: Unable to scrape real name for \"$SF_USERNAME\"." 1>&2
}
 
SF_EMAIL="$SF_REALNAME <$SF_USERNAME_LC@users.sourceforge.net>"
# Cache the result to avoid future processing.
echo $SF_EMAIL > "$CACHE_DIR/$SF_USERNAME"
fi
 
echo $SF_EMAIL

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.