Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Last active November 30, 2018 08:18
Show Gist options
  • Save henriquemoody/d4409df72a86dd07b9a794635ce0995a to your computer and use it in GitHub Desktop.
Save henriquemoody/d4409df72a86dd07b9a794635ce0995a to your computer and use it in GitHub Desktop.
Example: list-authors name/of/file
#!/usr/bin/env bash
# Usage: {script} FILENAME
# Add the list of authors to a PHP or PHPT file.
# The list of authors is generated by the `git blame` command.
#
# --help, -h Displays this help
#
# Report bugs to Henrique Moody <[email protected]>.
#
set -euo pipefail
declare -r FILENAME="${1}"
show-help()
{
# Usage: show-help
sed -E 's/^#\s?(.*)/\1/g' "${0}" |
sed -nE '/^Usage/,/^Report/p' |
sed "s/{script}/$(basename "${0}")/g"
}
show-authors() {
# Usage: show-authors FILENAME
local -r filename="${1}"
git blame --line-porcelain "${filename}" |
egrep '^author' |
tr '\n' ' ' |
sed 's,author ,\n,g' |
sed -E 's,^(.+) author-mail (<[^>]+>).+$,\1 \2,g' |
egrep -v '^$' |
uniq |
sed -E "s,Not Committed Yet <not.committed.yet>,$(git config user.name) <$(git config user.email)>,g" |
sed -E 's,.+ (<[email protected]>),Alexandre Gomes Gaigalas \1,g' |
sed -E 's,.+ (<alganet@alganet-laptop.\(none\)>),Alexandre Gomes Gaigalas <[email protected]>,g' |
sed -E 's,.+ (<andre@andre.\(none\)>),Carlos André Ferrari <[email protected]>,g' |
sed -E 's,.+ (<[email protected]>),Emmerson Siqueira \1,g' |
sed -E 's,.+ (<[email protected]>),Fajar Khairil <[email protected]>,g' |
sed -E 's,.+ (<[email protected]>),Nick Lombard \1,g' |
sed -E 's,.+ (<[email protected]>),Ian Nisbet \1,g' |
sed -E 's,.+ (<[email protected]>),Jayson Reis <[email protected]>,g' |
sed -E 's,.+ (<[email protected]>),Camilo Teixeira de Melo \1,g' |
sed -E 's,.+ (<[email protected]>),Camilo Teixeira de Melo <[email protected]>,g' |
sed -E 's,.+ (<[email protected]>),Andrey Kolyshkin <[email protected]>,g' |
sed -E 's,.+ (<[email protected]>),Alexander Gorshkov \1,g' |
sed -E 's,.+ (<[email protected]>),Michael Firsikov \1,g' |
sed -E 's,.+ (<[email protected]>),Moritz Fromm \1,g' |
sed -E 's,.+ (<nawarian@phoenix>),Níckolas Daniel da Silva <[email protected]>,g' |
sed -E 's,.+ (<[email protected]>),Davide Pastore \1,g' |
sed -E 's,.+ (<[email protected]>),Paul Karikari \1,g' |
sed -E 's,.+ (<[email protected]>),Rafael Bartalotti \1,g' |
sed -E 's,.+ (<[email protected]>),Roman Derevianko \1,g' |
sort -u
}
add-php-authors() {
# Usage: add-php-authors FILENAME
local -r filename="${1}"
local -r temporaty=$(mktemp)
local docblock
{
egrep -q '^/\*\*$' "${filename}" || echo "/**"
show-authors "${filename}" |
sed 's,^, * @author ,'
echo " */"
} > "${temporaty}"
docblock=$(tr '\n' '#' < "${temporaty}" | sed 's,#,\\n,g')
echo "Adding authors to PHP file ${filename}"
sed -i '/@author/d' "${filename}"
sed -i -E "s%^(final class|abstract class|trait|interface|class)%${docblock}\1%g" "${filename}"
sed -i ':begin;$!N;s, \*/\n \* @author, * @author,;tbegin;P;D' "${filename}"
}
add-phpt-authors() {
# Usage: add-phpt-authors FILENAME
local -r filename="${1}"
local -r temporaty=$(mktemp)
{
echo "--CREDITS--"
show-authors "${filename}"
cat "${filename}"
} > "${temporaty}"
echo "Adding authors to PHPT file ${filename}"
cat "${temporaty}" > "${filename}"
}
if [[ "${FILENAME}" = "--help" ]] || [[ "${FILENAME}" = "-h" ]]; then
show-help
elif [[ "${FILENAME}" =~ "phpt" ]]; then
add-phpt-authors "${FILENAME}"
elif [[ "${FILENAME}" =~ "php" ]]; then
add-php-authors "${FILENAME}"
else
show-help 1>&2
exit 1
fi
@williamespindola
Copy link

williamespindola commented May 31, 2018

I don't know why but this not work for me. 😕 maybe can be my terminal:
This works:

#!/usr/bin/env bash

git blame --line-porcelain ${1} |
    egrep '^author |^author-mail' |
    sed 's,author-mail ,,g' |
    tr '\n' ' ' |
    tr '/>' '\n' |
    sort |
    egrep -v '^$' |
    uniq |
    sort -rn |
    sed -E 's, *author, * @author,g'
    sort 

Needs some improvements, for example put > on the end of email, but is good so far
results

➜  Validation git:(feature/refactor-contains) ✗ ./list-authors tests/unit/Rules/ContainsTest.php

 * @author Alexandre Gaigalas <[email protected]
 * @author Alexandre Gaigalas <[email protected]
 * @author Gabriel Caruso <[email protected]
 * @author Henrique Moody <[email protected]
 * @author Nawarian <[email protected]
 * @author William Espindola <[email protected]

this stars at the begin is for Docblocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment