Skip to content

Instantly share code, notes, and snippets.

@thcipriani
Last active July 6, 2020 20:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thcipriani/f95dd68d7527152f709d264d3914c2ef to your computer and use it in GitHub Desktop.
Save thcipriani/f95dd68d7527152f709d264d3914c2ef to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# 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 "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment