|
#!/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 |
I don't know why but this not work for me. 😕 maybe can be my terminal:
This works:
Needs some improvements, for example put
>
on the end of email, but is good so farresults
this stars at the begin is for Docblocks