Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifyouseewendy/9bdf7ad7173f9c78026c to your computer and use it in GitHub Desktop.
Save ifyouseewendy/9bdf7ad7173f9c78026c to your computer and use it in GitHub Desktop.
Update git history of author and email.
File.open("stats/stats_#{Time.now.to_i}.tsv","w") do |of|
lines = `git log --format=format:"%an\t%ae\t%ce"`.split("\n").uniq
stats = lines.reduce({}) do |ret, line|
author, a_email, c_email = line.strip.split("\t")
ret[author] ||= {}
ret[author][:a_email] ||= []
ret[author][:c_email] ||= []
ret[author][:a_email] << a_email unless ret[author][:a_email].include? a_email
ret[author][:c_email] << c_email unless ret[author][:c_email].include? c_email
ret
end
of.puts %w(Author AuthorEmail CommitterEmail).join("\t")
stats.sort_by{|k,v| k}.each do |k, v|
a_email = v[:a_email].empty? ? '-' : v[:a_email].join(', ')
c_email = v[:c_email].empty? ? '-' : v[:c_email].join(', ')
of.puts [k, a_email, c_email].join("\t")
end
end
git filter-branch -f --commit-filter '
char="[[:alnum:]!#\$%&\*\+/=?^_\`{|}~-]";
name_part="(${char}+\.*${char}+)";
domain="([[:alnum:]]([[:alnum:]-]*[[:alnum:]])?)+\.[[:alnum:]\(]([[:alnum:]-]*[[:alnum:]\)])?";
begin="(^|[[:space:]])";
end="($|[[:space:]])";
other_email="${begin}${name_part}@${domain}${end}";
umeng_email="${begin}${name_part}@umeng.com";
email="$GIT_AUTHOR_EMAIL";
# CAUTION
# `echo` will interrupt `git filter-branch`,
# copy out the script to debug.
if [[ $email =~ $umeng_email ]]; then
git commit-tree "$@";
elif [[ $email =~ $other_email ]]; then
name=${BASH_REMATCH[2]};
domain=${BASH_REMATCH[3]};
# Need an absolute path when doing `git filter-branch`, remember to change yours.
updated_name=`ruby /path/to/update_name.rb $name`;
if [[ $updated_name = "" ]]; then
author_email="umeng_$name-$domain@umeng.com";
updated_name="$name";
else
author_email="$updated_name@umeng.com";
fi
GIT_AUTHOR_NAME="$updated_name";
GIT_AUTHOR_EMAIL="$author_email";
GIT_COMMITTER_EMAIL="$author_email";
git commit-tree "$@";
else
git commit-tree "$@";
fi
' -- HEAD
char="[[:alnum:]!#\$%&\*\+/=?^_\`{|}~-]";
name_part="(${char}+\.*${char}+)";
domain="([[:alnum:]]([[:alnum:]-]*[[:alnum:]])?)+\.[[:alnum:]\(]([[:alnum:]-]*[[:alnum:]\)])?";
begin="(^|[[:space:]])";
end="($|[[:space:]])";
other_email="${begin}${name_part}@${domain}${end}";
umeng_email="${begin}${name_part}@umeng.com";
# email="joe.smith@example.com"
# email="lt@lt-ThinkPad-R400.(none)"
# email="wendi@umeng.com"
# echo ${BASH_REMATCH[0]} => joe.smith@example.com
# echo ${BASH_REMATCH[1]} =>
# echo ${BASH_REMATCH[2]} => joe.smith
# echo ${BASH_REMATCH[3]} => example
# $GIT_AUTHOR_EMAIL="wendi@umeng.com";
email="$GIT_AUTHOR_EMAIL";
if [[ $email =~ $umeng_email ]]; then
git commit-tree "$@";
elif [[ $email =~ $other_email ]]; then
name=${BASH_REMATCH[2]};
domain=${BASH_REMATCH[3]};
# Need an absolute path when doing git filter-branch, remember to change yours.
updated_name=`ruby /path/to/update_name.rb $name`;
if [[ $updated_name = "" ]]; then
author_email="umeng_$name-$domain@umeng.com";
updated_name="$name";
else
author_email="$updated_name@umeng.com";
fi
GIT_AUTHOR_NAME="$updated_name";
GIT_AUTHOR_EMAIL="$author_email";
GIT_COMMITTER_EMAIL="$author_email";
git commit-tree "$@";
else
git commit-tree "$@";
fi
mapping = File.read( File.expand_path('../mapping.txt', __FILE__) ).lines.reduce({}) do |ret, line|
umeng_name, *names = line.strip.split(/\s/)
names.each{|n| ret[n] = umeng_name }
ret
end
File.open('log', 'a') do |of|
of.puts mapping[ARGV[0]] || ''
end
puts mapping[ARGV[0]] || ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment