Skip to content

Instantly share code, notes, and snippets.

@3D-I
Created March 13, 2021 05:13
Show Gist options
  • Save 3D-I/fd14dbc16761ff553e2226c774d85658 to your computer and use it in GitHub Desktop.
Save 3D-I/fd14dbc16761ff553e2226c774d85658 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Brought to you by phpBB Studio © 2020 ( phpbbstudio.com )
#
current=$(git symbolic-ref --short -q HEAD)
function markup {
local bold=1
local dim=2
local line=4
local white=39
local black=30
local red=31
local green=32
local yellow=33
local blue=34
local prefix=()
for var in "${@:2}"
do
if [[ ${var} == "bg"* ]]; then
var=${var:2}
var=${!var}
var=$(expr ${var} + 10)
prefix+=("${var}")
else
prefix+=("${!var}")
fi
done
local prefix=$(printf ";%s" "${prefix[@]}")
local string="\e[${prefix:1}m${1}\e[0m"
echo -e "${string}"
}
function checkout_branch {
local branch="${1}"
if [[ "${branch}" != ${current} ]]; then
echo
git checkout ${1}
if ! [[ $? = 0 ]]; then exit 1; fi
current="${branch}"
fi
}
function validate_branch {
local branch="${1}"
local must_exist="${2}"
local does_exist=$(git branch --list "${branch}")
if [[ ${must_exist} ]]; then
if ! [[ ${does_exist} ]]; then
echo
markup " The branch '${branch}' does not exist. " bgred white bold
exit 1
fi
else
if [[ ${does_exist} ]]; then
echo
markup " The branch '${branch}' already exists. " bgred white bold
exit 1
fi
fi
}
function validate_ticket {
local ticket="${1}"
if [[ "${ticket}" != [0-9][0-9][0-9][0-9][0-9] ]]; then
echo
markup " That is not a valid ticket number. " bgred white bold line
markup " A ticket consists of 5 digits. " bgred white bold
markup " If you wish to create a ticket, " bgred white bold
markup " Go to: https://tracker.phpbb.com " bgred white
exit 1
fi
}
function validate_base {
local base="${1}"
if ! [[ "${base}" =~ ^(master|3.3.x|3.2.x)$ ]]; then
echo
markup " Invalid branch supplied. " bgred white bold
exit 1;
fi
}
function retrieve_base {
local ticket="$1"
local master=$(git merge-base ${ticket} master)
local three=$(git merge-base ${ticket} 3.3.x)
local two=$(git merge-base ${ticket} 3.2.x)
if ! [[ ${master} == ${three} ]]; then
echo 'master'
fi
if ! [[ ${three} == ${two} ]]; then
echo '3.3.x'
fi
echo '3.2.x'
}
function prompt_install {
echo
echo "Should we also (re-)install your phpBB board?"
read -p "Install [Y/N]: " -n 1 install
echo
case "${install}" in
y|Y)
install
;;
n|N)
;;
*)
echo
markup " Invalid answer supplied. " bgred white bold
exit 1
;;
esac
}
function prompt_update {
echo
echo "Should we first update your phpBB fork?"
read -p "Update [Y/N]: " -n 1 update
echo
case "${update}" in
y|Y)
update
;;
n|N)
;;
*)
echo
markup " Invalid answer supplied. " bgred white bold
exit 1
;;
esac
}
function prompt_url {
local msg="$1"
local url="$2"
echo
echo ${msg}
read -p "Open [Y/N]: " -n 1 open
echo
case "${open}" in
y|Y)
start "${url}"
;;
n|N)
;;
*)
echo
markup " Invalid answer supplied. " bgred white bold
exit 1
;;
esac
}
function amend {
local msg="$1"
local ticket=${current:7}
if [[ ${current} != "ticket/"* ]]; then
echo
markup " You are not on a ticket branch " bgred white bold
echo
echo "To what ticket do you want to switch to?"
read -p "Ticket: PHPBB3-" ticket
validate_ticket "${ticket}"
fi
local branch="ticket/${ticket}"
validate_branch "${branch}" true
checkout_branch "${branch}"
if ! [[ "${msg}" ]]; then
local last_msg=$(git log -1 --pretty=%B)
if [[ $? = 0 ]]; then
markup " Your last commit message was: " bgyellow white bold
markup "${last_msg}" yellow bold
fi
fi
if [[ ${msg} == '' ]]; then
echo
echo "Please provide the commit comment (max 64 characters)."
read -p "Comment: " msg
fi
local length=${#msg}
while (( length > 64 )); do
markup " Your commit comment is too long! " bgred white bold line
markup " It can contain no more than 64 characters. " bgred white bold
markup " It currently contains ${length} characters. " bgred white bold
echo
read -p "Comment: " msg
local length=${#msg}
done
if ! [[ "${msg}" ]]; then
echo
markup " No commit comment provided. " bgred white bold
exit 1
fi
git commit --amend -m "[ticket/${ticket}] ${msg}
PHPBB3-${ticket}
"
if ! [[ $? = 0 ]]; then exit 1; fi
git push -f origin "ticket/${ticket}"
if ! [[ $? = 0 ]]; then exit 1; fi
echo
markup " " bggreen
markup " Last commit message has been amended! " bggreen white bold line
markup " " bggreen
}
function clean {
prompt_update
checkout_branch "master"
git branch --merged | egrep -v "(^\*|master|2.0.x|3.0.x|3.1.x|3.2.x|3.3.x)" | xargs -d $'\n' sh -c 'for arg do git branch -d $arg; git push origin --delete $arg; done' _
if ! [[ $? = 0 ]]; then exit 1; fi
git remote prune origin
if ! [[ $? = 0 ]]; then exit 1; fi
echo
markup " " bggreen
markup " Fork has been successfully cleaned! " bggreen white bold line
markup " " bggreen
}
function commit {
local msg="$1"
local ticket=${current:7}
if [[ ${current} != "ticket/"* ]]; then
echo
markup " You are not on a ticket branch " bgred white bold
echo
echo "To what ticket do you want to switch to?"
read -p "Ticket: PHPBB3-" ticket
validate_ticket "${ticket}"
fi
local branch="ticket/${ticket}"
validate_branch "${branch}" true
checkout_branch "${branch}"
if [[ ${msg} == '' ]]; then
echo
echo "Please provide the commit comment (max 64 characters)."
read -p "Comment: " msg
fi
local length=${#msg}
while (( length > 64 )); do
echo
markup " Your commit comment is too long! " bgred white bold line
markup " It can contain no more than 64 characters. " bgred white bold
markup " It currently contains ${length} characters. " bgred white bold
echo
read -p "Comment: " msg
local length=${#msg}
done
if ! [[ "${msg}" ]]; then
echo
markup " No commit comment provided. " bgred white bold
exit 1
fi
git commit -a -m "[ticket/${ticket}] ${msg}
PHPBB3-${ticket}
"
if ! [[ $? = 0 ]]; then exit 1; fi
git push origin ticket/${ticket}
if ! [[ $? = 0 ]]; then exit 1; fi
echo
markup " " bggreen
markup " Changes have been committed and pushed! " bggreen white bold line
markup " " bggreen
markup " If you wish to create a Pull Request, " bggreen white bold
markup " Go to: https://github.com/phpbb/phpbb " bggreen white bold
markup " " bggreen
local base=$(retrieve_base "ticket/${ticket}")
local user=$(git ls-remote --get-url origin | cut -d@ -f1 | cut -d/ -f4| cut -d: -f1)
local url="https://github.com/phpbb/phpbb/compare/${base}...${user}:ticket/${ticket}"
prompt_url "Do you want to create a Pull Request right now?" "${url}"
}
function create {
local branch="$1"
local ticket="$2"
prompt_update
if ! [[ ${branch} ]]; then
echo "Into what branch do you want merge this ticket?"
markup " master | 3.3.x | 3.2.x " dim
read -p "Branch: " branch
fi
validate_base "${branch}"
checkout_branch "${branch}"
if ! [[ "${ticket}" ]]; then
echo
echo "For what ticket do you want to create a branch?"
read -p "Ticket: PHPBB3-" ticket
fi
validate_ticket ${ticket}
validate_branch "ticket/${ticket}" #false
git checkout -b "ticket/${ticket}"
if ! [[ $? = 0 ]]; then exit 1; fi
echo
markup " " bggreen
markup " Branch has successfully been set up! " bggreen white bold line
markup " " bggreen
markup " You can now make your changes to the files, " bggreen white bold
markup " save the files and add any new files. " bggreen white bold
markup " Once done, you can commit your changes. " bggreen white bold
markup " sh studio commit <\"commit comment\"> " bggreen black
markup " " bggreen
}
function install {
if ! [[ -f "./tests/test_config.php" ]]; then
echo
markup " You do not have a test config yet. " bgred white bold
echo
markup "A test config is required for us to install the board for you." yellow
markup "You can read how to set it up in 'test/RUNNING_TESTS.md'." yellow
prompt_url "Do you want to open the documentation in your browser?" "https://github.com/phpbb/phpbb/blob/3.2.x/tests/RUNNING_TESTS.md#database-tests"
exit 1
fi
echo
markup "Please wait while we install the board..." yellow
echo
php -r '
$phpbb_root_path = "./phpBB/";
$phpEx = "php";
define("IN_PHPBB", true);
define("IN_INSTALL", true);
require "./phpBB/install/startup.php";
require "./tests/mock/container_builder.php";
require "./tests/test_framework/phpbb_database_test_connection_manager.php";
require "./tests/test_framework/phpbb_test_case.php";
require "./tests/test_framework/phpbb_test_case_helpers.php";
require "./tests/test_framework/phpbb_functional_test_case.php";
phpbb_functional_test_case::setUpBeforeClass();'
if ! [[ $? = 0 ]]; then exit 1; fi
echo
markup " " bggreen
markup " Your board has successfully been installed! " bggreen white bold line
markup " " bggreen
markup " You can visit it through your localhost, " bggreen white bold
markup " browse the files for this specific branch. " bggreen white bold
markup " Or create a new ticket branch with: " bggreen white bold
markup " sh studio create <\"base\"> <\"ticket\"> " bggreen black
markup " " bggreen
}
function setup {
local name="$1"
local email="$2"
if ! [[ "${name}" ]]; then
read -p "Name: " name
if [[ ${name} ]]; then
git config --global user.name "${name}"
if ! [[ $? = 0 ]]; then exit 1; fi
fi
fi
if ! [[ "${email}" ]]; then
read -p "Email: " email
if [[ ${email} ]]; then
git config --add user.email "${email}"
if ! [[ $? = 0 ]]; then exit 1; fi
fi
fi
local crlf=$(git config core.autocrlf)
if [[ ${crlf} ]]; then
git config --global core.autocrlf false
if ! [[ $? = 0 ]]; then exit 1; fi
fi
local remote=$(git config remote.upstream.url > /dev/null)
if ! [[ ${remote} ]]; then
git remote add upstream git://github.com/phpbb/phpbb.git
if ! [[ $? = 0 ]]; then exit 1; fi
fi
echo
markup " " bggreen
markup " Setup successfully completed. " bggreen white bold line
markup " " bggreen
if [[ ${name} ]]; then
markup " - User's name has been updated " bggreen white bold
fi
if [[ ${email} ]]; then
markup " - Email address has been updated " bggreen white bold
fi
if [[ ${crlf} ]]; then
markup " - Line endings have been updated " bggreen white bold
fi
if ! [[ ${remote} ]]; then
markup " - Upstream remote has been added " bggreen white bold
fi
markup " " bggreen
}
function switch {
branch="$1"
if ! [[ ${branch} ]]; then
echo "To what branch do you want to switch to?"
markup " master | 3.3.x | 3.2.x " dim
read -p "Branch: " branch
fi
validate_base "${branch}"
checkout_branch "master"
checkout_branch "${branch}"
cd phpBB
php ../composer.phar install
cd ../
prompt_install
}
function test_pr {
pull="$1"
git fetch https://github.com/phpbb/phpbb.git pull/${pull}/head && git checkout FETCH_HEAD
cd phpBB
php ../composer.phar install
cd ../
markup " " bggreen
markup " Pull Request #${pull} has been applied. " bggreen white bold
markup " " bggreen
markup " All dependencies have been installed. " bggreen white
markup " You can now test the changes locally. " bggreen white
markup " Once done, switch branches again: " bggreen white
markup " git switch - " bggreen dim
markup " " bggreen
prompt_install
}
function update {
local time=$SECONDS
echo
echo "Updating master..."
echo
checkout_branch "master"
git pull https://github.com/phpbb/phpbb.git master
if ! [[ $? = 0 ]]; then exit 1; fi
git push origin master
if ! [[ $? = 0 ]]; then exit 1; fi
echo
echo "Done."
echo
echo "Updating 3.3.x..."
echo
checkout_branch "3.3.x"
git pull https://github.com/phpbb/phpbb.git 3.3.x
if ! [[ $? = 0 ]]; then exit 1; fi
git push origin 3.3.x
if ! [[ $? = 0 ]]; then exit 1; fi
echo
echo "Done."
echo
echo "Updating 3.2.x..."
echo
checkout_branch "3.2.x"
git pull https://github.com/phpbb/phpbb.git 3.2.x
if ! [[ $? = 0 ]]; then exit 1; fi
git push origin 3.2.x
if ! [[ $? = 0 ]]; then exit 1; fi
echo
echo "Done."
echo
echo "Switching back to master..."
checkout_branch "master"
echo
echo "Done."
echo
time=$(($SECONDS - ${time}))
markup " " bggreen
markup " Update done in ${time} seconds. " bggreen white bold
markup " " bggreen
}
function menu {
if [[ $1 != '' ]]; then
markup " How may we help you today? " bgblue white
markup " " bgblue white
fi
echo
markup "Actions:" bold line
markup " amend" bold green
markup " sh studio amend <\"commit comment\">"
markup " Change the comment of your last commit message." dim
markup " clean" bold green
markup " sh studio clean"
markup " Clean your fork, removes all branches that are merged into master." dim
markup " commit" bold green
markup " sh studio commit <\"commit comment\">"
markup " Commit and push your changes with the correct commit template." dim
markup " create" bold green
markup " sh studio create <base branch> <ticket number>"
markup " Create a new ticket branch against the correct base branch." dim
markup " setup" bold green
markup " sh studio setup <\"your name\"> <\"your email\">"
markup " Sets up your git name, email and adds the upstream remote." dim
markup " switch" bold green
markup " sh studio switch <branch>"
markup " Switches to a base branch and installs the dependencies." dim
markup " test-pr" bold green
markup " sh studio test-pr <pull request number>"
markup " Applies a PR locally (github.com/phpbb/phpbb/pull/<NUMBER>)" dim
markup " update" bold green
markup " sh studio update"
markup " Updates your local main branches from the phpBB repository." dim
exit 1
}
markup " " bgblue white
markup " phpBB Studio " bgblue white bold line
markup " Welcome to the Studio " bgblue white
markup " " bgblue white
case "$1" in
"amend")
amend "$2"
;;
"clean")
clean
;;
"commit")
commit "$2"
;;
"create")
create "$2" "$3"
;;
"install")
install
;;
"setup")
setup "$2" "$3"
;;
"switch")
switch "$2"
;;
"test-pr")
test_pr "$2"
;;
"update")
update
;;
"")
menu "help"
;;
*)
echo
markup " Invalid action supplied. " bgred white bold
menu
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment