Page MenuHomePhabricator
Paste P8606

gerritUsernameToLowercase.sh
ActivePublic

Authored by thcipriani on Jun 11 2019, 7:27 PM.
#!/usr/bin/env bash
#
# Gerrit Username To Lowercase
# =====================
#
# Script for converting gerrit:-scheme usernames to lowercase directly
# in a checkout of refs/meta/external-ids of the All-Users git repo.
#
# Copyright: Tyler Cipriani <tcipriani@wikimedia.org> 2019
# License: GPLv3+
# Utility logging methods
info() {
printf '[INFO] %s' "$@"
}
println() {
printf '%s\n' "$@"
}
# Convert username with gerrit: schema to lowercase
#
# accepts a username as a mixed case string without a schema by:
#
# 1. Convert username mixed case to username lowercase
# 2. Compute sha1sum of lowercase schema for use as new file name
# 3. Find the old file name using git grep
# 4. Git move the old file to the new file path (computed with sha1sum)
# 5. Replace the username mixed-case in the new file with username lowercase
#
# param username: string, mixed case
usernameToLower() {
local username username_lower shasum new_file old_file
username="$1"
username_lower="${username,,}"
shasum=$(printf "gerrit:%s" "${username_lower}" | shasum -a 1)
new_file=$(printf '%s/%s\n' "${shasum:0:2}" "${shasum:2:38}")
old_file=$(git grep --full-name --files-with-matches "\"gerrit:${username}\"")
if [ -f "$new_file" ]; then
println "The new file '${new_file}' exists!!!! for '${username}'. Aborting!"
exit 1
fi
git mv "$old_file" "$new_file"
# Change username to lowercase in new file
sed -i "s/gerrit:${username}/gerrit:${username_lower}/" "$new_file"
}
# Find any gerrit:-schema users with capital letters
# Look to see if there is a lowercase version
# If not, convert user to lowercase
main() {
while read -r user; do
# Grep for lowercase user
if git grep "\\[externalId \"gerrit:${user,,}\"\\]" &>/dev/null; then
continue
fi
info "Converting ${user}..."
usernameToLower "$user"
println "DONE!"
done < <(git grep -P 'gerrit:.*[A-Z]+.*' | sed -e 's/.*:\[externalId "gerrit:\(.*\)"]/\1/')
}
main "$@"