Skip to content

Instantly share code, notes, and snippets.

@src4026
Last active January 2, 2024 15:45
Show Gist options
  • Save src4026/d2ce56e70ae05a538e2329a379660c80 to your computer and use it in GitHub Desktop.
Save src4026/d2ce56e70ae05a538e2329a379660c80 to your computer and use it in GitHub Desktop.
A script to automate fetching folders for testing Map PRs for Minetetst Capture the Flag by rubenwardy.
#!/bin/bash
ctf_folder="capturetheflag"
temp_folder=$(mktemp -d)
usage() {
echo "Usage: $0 [ -b BRANCH ] [ -n CTF_FOLDER_NAME ] <path/to/Minetest/folder> <fork repository>"
echo "Options:"
echo " -b BRANCH: The branch used for the PR (default: origin)"
echo " -n CTF_FOLDER_NAME: Name of your CTF folder (default: capturetheflag)"
echo "Arguments:"
echo " <path/to/Minetest/folder>: Absolute path to your Minetest folder"
echo " <fork repository>: The link to the git repository of the fork by the PR author."
}
while getopts ":b:n:h" opt; do
case $opt in
b)
branch="$OPTARG"
;;
n)
ctf_folder="${OPTARG%/}"
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option -$OPTARG"
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument."
usage
exit 1
;;
esac
done
shift "$((OPTIND-1))"
if [ $# -ne 2 ]; then
echo "Error: Two positional arguments are required."
usage
exit 1
fi
minetest_path="${1%/}"
fork_repo="$2"
maps_folder_path="${minetest_path}"/"games/${ctf_folder}/mods/ctf/ctf_map/maps"
git clone $fork_repo "$temp_folder"
if [ -n "$branch" ]; then
cd $temp_folder
git switch "$branch"
fi
diff -rq --exclude=".git" "$maps_folder_path" "$temp_folder" | grep "Only in $temp_folder" | awk -F "Only in " '{print $2}' | sed "s/: /\//g "
changes=$(diff -rq --exclude=".git" "$maps_folder_path" "$temp_folder" | grep "Only in $temp_folder")
IFS=$'\n'
for line in $changes; do
file_or_folder=$(echo "$line" | awk -F "Only in " '{print $2}')
modified_file_or_folder=$(echo "$file_or_folder" | sed "s/: /\//g")
echo "Changes detected: $modified_file_or_folder"
read -p "Do you want to copy the modified file/folder to your maps folder path? (y/n): " applied_changes
if [ "$applied_changes" == "y" ]; then
subfolder=$(dirname "${modified_file_or_folder#$temp_folder}")
echo "$subfolder"
cp -r $modified_file_or_folder $maps_folder_path/$subfolder
echo "Copied $modified_file_or_folder to $maps_folder_path"
else
echo "No changes copied over."
fi
done
rm -rf $temp_folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment