2019-11-15 04:34:47 +00:00
|
|
|
#!/usr/bin/env bash
|
2021-07-16 14:15:31 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
2019-11-15 04:34:47 +00:00
|
|
|
|
2020-06-09 15:03:35 +01:00
|
|
|
# This script is executed by GitHub Actions for every pull request opened.
|
2019-11-15 04:34:47 +00:00
|
|
|
# It currently accomplishes the following objectives (for English pages only):
|
|
|
|
#
|
|
|
|
# 1. Detect pages that were just copied (i.e. cp pages/{common,linux}/7z.md).
|
|
|
|
# 2. Detect pages that were added in a platform specific directory although
|
|
|
|
# they already exist under 'common'.
|
|
|
|
# 3. Detect pages that were added in the 'common' platform although they
|
|
|
|
# already exist under a platform specific directory.
|
|
|
|
# 4. Detect other miscellaneous anomalies in the pages folder.
|
|
|
|
#
|
|
|
|
# Results are printed to stdout, logs and errors to stderr.
|
|
|
|
#
|
|
|
|
# NOTE: must be run from the repository root directory to correctly work!
|
|
|
|
# NOTE: no `set -e`, failure of this script should not invalidate the build.
|
|
|
|
|
|
|
|
# Check for duplicated pages.
|
|
|
|
function check_duplicates {
|
|
|
|
local page=$1 # page path in the format 'platform/pagename.md'
|
|
|
|
local parts
|
|
|
|
local other
|
|
|
|
|
|
|
|
readarray -td'/' parts < <(echo -n "$page")
|
|
|
|
|
|
|
|
local platform=${parts[0]}
|
|
|
|
local file=${parts[1]}
|
|
|
|
|
|
|
|
case "$platform" in
|
|
|
|
common) # check if page already exists in other platforms
|
2019-11-18 22:11:58 +00:00
|
|
|
for other in ${PLATFORMS/common/}; do
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ -f "pages/$other/$file" ]]; then
|
2019-11-18 22:11:58 +00:00
|
|
|
printf "\x2d $MSG_EXISTS" "$page" "$other"
|
2019-11-15 04:34:47 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
|
|
|
|
*) # check if page already exists under common
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ -f "pages/common/$file" ]]; then
|
2019-11-18 22:11:58 +00:00
|
|
|
printf "\x2d $MSG_EXISTS" "$page" 'common'
|
2019-11-15 04:34:47 +00:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
# Look at git diff and check for copied/duplicated pages.
|
|
|
|
function check_diff {
|
|
|
|
local git_diff
|
|
|
|
local line
|
|
|
|
local entry
|
|
|
|
|
2021-01-08 09:24:28 +00:00
|
|
|
git_diff=$(git diff --name-status --find-copies-harder --diff-filter=AC --relative=pages/ remotes/origin/main)
|
2019-11-15 04:34:47 +00:00
|
|
|
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ -n $git_diff ]]; then
|
2019-11-15 04:34:47 +00:00
|
|
|
echo -e "Check PR: git diff:\n$git_diff" >&2
|
|
|
|
else
|
2019-11-18 22:11:58 +00:00
|
|
|
echo 'Check PR: git diff looks fine, no interesting changes detected.' >&2
|
2019-11-15 04:34:47 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
while read line; do
|
|
|
|
readarray -td$'\t' entry < <(echo -n "$line")
|
|
|
|
|
|
|
|
local change="${entry[0]}"
|
|
|
|
local file1="${entry[1]}"
|
|
|
|
local file2="${entry[2]}"
|
|
|
|
|
|
|
|
case "$change" in
|
|
|
|
C*) # file2 is a copy of file1
|
|
|
|
local percentage=${change#C}
|
|
|
|
percentage=${percentage#0}
|
|
|
|
percentage=${percentage#0}
|
|
|
|
|
2019-11-18 22:11:58 +00:00
|
|
|
printf "\x2d $MSG_IS_COPY" "$file2" "$file1" "$percentage"
|
2019-11-15 04:34:47 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
A) # file1 was newly added
|
|
|
|
check_duplicates "$file1"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done <<< "$git_diff"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Recursively check the pages/ folder for anomalies.
|
|
|
|
function check_structure {
|
2019-11-18 22:11:58 +00:00
|
|
|
for platform in $PLATFORMS; do
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ ! -d "pages/$platform" ]]; then
|
2019-11-18 22:11:58 +00:00
|
|
|
printf "\x2d $MSG_NOT_DIR" "pages/$platform"
|
2019-11-15 04:34:47 +00:00
|
|
|
else
|
2019-11-18 22:11:58 +00:00
|
|
|
for page in "pages/$platform"/*; do
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ ! -f $page ]]; then
|
2019-11-18 22:11:58 +00:00
|
|
|
printf "\x2d $MSG_NOT_FILE" "$page"
|
2023-10-26 17:41:24 +01:00
|
|
|
elif [[ ${page:(-3)} != ".md" ]]; then
|
2019-11-18 22:11:58 +00:00
|
|
|
printf "\x2d $MSG_NOT_MD" "$page"
|
2019-11-15 04:34:47 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
###################################
|
|
|
|
# MAIN
|
|
|
|
###################################
|
|
|
|
|
2019-11-18 22:11:58 +00:00
|
|
|
MSG_EXISTS='The page `%s` already exists under the `%s` platform.\n'
|
|
|
|
MSG_IS_COPY='The page `%s` seems to be a copy of `%s` (%d%% matching).\n'
|
|
|
|
MSG_NOT_DIR='The file `%s` does not look like a directory.\n'
|
|
|
|
MSG_NOT_FILE='The file `%s` does not look like a regular file.\n'
|
|
|
|
MSG_NOT_MD='The file `%s` does not have a `.md` extension.\n'
|
|
|
|
|
2019-11-27 17:25:16 +00:00
|
|
|
PLATFORMS=$(ls pages/)
|
2019-11-18 22:11:58 +00:00
|
|
|
|
2023-10-26 17:41:24 +01:00
|
|
|
if [[ $CI == true && $GITHUB_REPOSITORY == "tldr-pages/tldr" && $PULL_REQUEST_ID != "" ]]; then
|
2019-11-15 04:34:47 +00:00
|
|
|
check_diff
|
|
|
|
check_structure
|
|
|
|
else
|
|
|
|
echo 'Not a pull request, refusing to run.' >&2
|
|
|
|
exit 0
|
|
|
|
fi
|