Script 'game-add': create a Git commit automatically.

This commit is contained in:
vonavi 2014-05-09 20:24:14 +03:00
parent 31a859197b
commit 75fa50368a

View File

@ -8,14 +8,14 @@ TOURNAMENT="2014/2-summertime"
# Config file for players # Config file for players
ply_ini="${REPODIR}/${TOURNAMENT}/players.ini" ply_ini="${REPODIR}/${TOURNAMENT}/players.ini"
get_game_players() { game_get_players() {
# Extract players on Lichess # Extract players on Lichess
local w_lichess=$(sed -En "s/\[White \"([^\"]*)\"\]/\1/p" < "$tmp_pgn") local w_lichess=$(sed -En "s/\[White \"([^\"]*)\"\]/\1/p" < "$tmp_pgn")
local b_lichess=$(sed -En "s/\[Black \"([^\"]*)\"\]/\1/p" < "$tmp_pgn") local b_lichess=$(sed -En "s/\[Black \"([^\"]*)\"\]/\1/p" < "$tmp_pgn")
# Get names of white and black players # Get names of white and black players
local counter=1 players=() local counter=1 players=()
parse_config game_parse_config
while eval "config_section_player${counter}" 2>/dev/null; do while eval "config_section_player${counter}" 2>/dev/null; do
players+=("$name") players+=("$name")
[[ "$lichess" == "$w_lichess" ]] && white="$name" [[ "$lichess" == "$w_lichess" ]] && white="$name"
@ -26,7 +26,7 @@ get_game_players() {
[[ -z "$black" ]] && fix_black_player [[ -z "$black" ]] && fix_black_player
} }
parse_config() { game_parse_config() {
# Copy player INI file to the temporary location # Copy player INI file to the temporary location
# NOTE: an empty line is added to the file beginning in order to # NOTE: an empty line is added to the file beginning in order to
# match the only first occurrence for non-GNU sed # match the only first occurrence for non-GNU sed
@ -75,7 +75,7 @@ choose_player() {
number=$((number-1)) number=$((number-1))
} }
add_game() { game_add_to_repo() {
local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}" local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}"
# Check if the tour number is correct # Check if the tour number is correct
local correct=false local correct=false
@ -89,11 +89,6 @@ add_game() {
die "Game '${white} vs. ${black}' not found in ${tour_info}." die "Game '${white} vs. ${black}' not found in ${tour_info}."
fi fi
# Extract the game date from PGN
local pgn_date=$(sed -En "s/\[Date \"([^\"]*)\"\]/\1/p" < "$tmp_pgn")
pgn_date=$(tr '.' '-' <<< "$pgn_date")
pgn_dir="${REPODIR}/${TOURNAMENT}/tours/${TOUR}/${pgn_date}-${white}-vs-${black}"
[[ -d "$pgn_dir" ]] && die "Directory ${pgn_dir} already exist." [[ -d "$pgn_dir" ]] && die "Directory ${pgn_dir} already exist."
echo "Creating directory ${pgn_dir}..." echo "Creating directory ${pgn_dir}..."
mkdir -p "$pgn_dir" mkdir -p "$pgn_dir"
@ -116,6 +111,13 @@ add_game() {
rm "${tour_info}.orig" rm "${tour_info}.orig"
} }
game_git_commit() {
git pull
git add "${pgn_dir}/1.pgn" "$tour_info"
git commit -m "Tour ${TOUR#0}: ${white} vs. ${black}."
git push
}
die() { die() {
echo "$@" 1>&2 echo "$@" 1>&2
exit 1 exit 1
@ -156,13 +158,19 @@ trap "rm ${tmp_ini} ${tmp_ini}.prev ${tmp_pgn}" EXIT
# Download PGN file # Download PGN file
[[ "$1" =~ ^(http://[^/]*)/([^/]*) ]] [[ "$1" =~ ^(http://[^/]*)/([^/]*) ]]
pgn_url="${BASH_REMATCH[1]}/${BASH_REMATCH[2]:0:8}/pgn" pgn_url="${BASH_REMATCH[1]}/${BASH_REMATCH[2]::8}/pgn"
curl -q --fail --location --silent "$pgn_url" > "$tmp_pgn" \ curl -q --fail --location --silent "$pgn_url" > "$tmp_pgn" \
|| die "PGN file not found." || die "PGN file not found."
white="" black="" white="" black=""
get_game_players game_get_players
add_game # Extract the game date from PGN
pgn_date=$(sed -En "s/\[Date \"([^\"]*)\"\]/\1/p" < "$tmp_pgn")
pgn_date=$(tr '.' '-' <<< "$pgn_date")
pgn_dir="${REPODIR}/${TOURNAMENT}/tours/${TOUR}/${pgn_date}-${white}-vs-${black}"
game_add_to_repo
game_git_commit
exit 0 exit 0