Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Last active November 30, 2018 08:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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 <henriquemoody@gmail.com>.
#
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,.+ (<alexandre@gaigalas.net>),Alexandre Gomes Gaigalas \1,g' |
sed -E 's,.+ (<alganet@alganet-laptop.\(none\)>),Alexandre Gomes Gaigalas <alexandre@gaigalas.net>,g' |
sed -E 's,.+ (<andre@andre.\(none\)>),Carlos André Ferrari <caferrari@gmail.com>,g' |
sed -E 's,.+ (<emmersonsiqueira@gmail.com>),Emmerson Siqueira \1,g' |
sed -E 's,.+ (<fajar90alone@gmail.com>),Fajar Khairil <fajar.khairil@gmail.com>,g' |
sed -E 's,.+ (<github@jigsoft.co.za>),Nick Lombard \1,g' |
sed -E 's,.+ (<ian@glutenite.co.uk>),Ian Nisbet \1,g' |
sed -E 's,.+ (<jayson.reis@sabbre.com.br>),Jayson Reis <santosdosreis@gmail.com>,g' |
sed -E 's,.+ (<kmilotxm@gmail.com>),Camilo Teixeira de Melo \1,g' |
sed -E 's,.+ (<kmilotxm@users.noreply.github.com>),Camilo Teixeira de Melo <kmilotxm@gmail.com>,g' |
sed -E 's,.+ (<kolyshkin@.sqlmaze.local>),Andrey Kolyshkin <a.kolyshkin@semrush.com>,g' |
sed -E 's,.+ (<mazanax@yandex.ru>),Alexander Gorshkov \1,g' |
sed -E 's,.+ (<michael.firsikov@gmail.com>),Michael Firsikov \1,g' |
sed -E 's,.+ (<moritzgitfromm@gmail.com>),Moritz Fromm \1,g' |
sed -E 's,.+ (<nawarian@phoenix>),Níckolas Daniel da Silva <nickolas@phpsp.org.br>,g' |
sed -E 's,.+ (<pasdavide@gmail.com>),Davide Pastore \1,g' |
sed -E 's,.+ (<paulkarikari1@gmail.com>),Paul Karikari \1,g' |
sed -E 's,.+ (<rafael_bartalotti@hotmail.com>),Rafael Bartalotti \1,g' |
sed -E 's,.+ (<roman.derevianko@gmail.com>),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 <alexandre@foo.bar
 * @author Alexandre Gaigalas <alexandre@foo.bar
 * @author Gabriel Caruso <carusogabriel34@foo.bar
 * @author Henrique Moody <henriquemoody@foo.bar
 * @author Nawarian <nickolas@foo.bar
 * @author William Espindola <oi@foo.bar

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