Skip to content

Commit

Permalink
travis: improve the detection mode
Browse files Browse the repository at this point in the history
由于hosts检测脚本有很多不必要的判断/操作,且依赖 dos2unix 的高版本。
因此,重新修正检测方式,使用 file 替代 dos2unix 来侦测 DOS 换行符,
取消设置环境变量才检验日期和格式的限制。

同时也精简了格式化hosts的脚本。

Signed-off-by: Andy Deng <[email protected]>
  • Loading branch information
andytimes committed Sep 28, 2016
1 parent 8ff60bd commit 85f8687
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 139 deletions.
14 changes: 1 addition & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
language: c
sudo: requried
sudo: false
dist: trusty

before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq -y gettext

install:
- wget -q https://github.com/racaljk/hosts/releases/download/checkhosts/dos2unix-7.3.4.tar.gz
- tar xf dos2unix-7.3.4.tar.gz
- cd dos2unix-7.3.4 && make && sudo make prefix=/usr install
- cd ..

script:
# Be permissive, unset following enviroment
- export STRICT_HOSTS_FORMAT=1
- bash tools/checkhosts.sh hosts
63 changes: 31 additions & 32 deletions tools/Hindent
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
#!/bin/bash

hosts_date() {
local repo_date=$(git log --date=short -n1 "$1" | egrep "[0-9]+-[0-9]+-[0-9]+" -o -m1)
local cur_date=$(date +%F)

# detect hosts file changes
if git diff --exit-code "$1" > /dev/null; then
# hosts file isn't changed.
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$(echo "$repo_date")/" "$1"
else
# hosts file is changed.
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$(echo "$cur_date")/" "$1"
fi
# https://github.com/racaljk/hosts

date_amend()
{
# check if hosts file changes
if git diff --exit-code "$1" > /dev/null; then
# hosts file is not changed,
# the date string will be set to the git log records.
local repo_date=$(git log --date=short -n1 "$1" | \
grep -Pom1 "\d+-\d+-\d+")
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$repo_date/" "$1"
else
# hosts file has been changed,
# set date string to the system date.
sed -i "s/[0-9]\+-[0-9]\+-[0-9]\+/$(date +%F)/" "$1"
fi
}

hosts_line() {
local your_os=$(uname -s | cut -c 1-5)

if [ "$your_os" = "MINGW" -o "$your_os" = "CYGWI" ]; then
echo -e "Note: Please check line endings in Windows by yourself."
else
dos2unix -q "$1"
fi
eol_amend()
{
dos2unix -q "$1"
}

hosts_format() {
local cur="\([0-9]\+.[0-9]\+.[0-9]\+.[0-9]\+\)[[:blank:]]\+"
local fmt="\1$(echo -e "\t")"
style_amend()
{
local orig="\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)[[:blank:]]\+"
local modified="\1$(echo -e "\t")"

sed -i "s/$(echo "$cur")/$(echo "$fmt")/g" "$1"
sed -i "s/$orig/$modified/g" "$1"
}

if [ "$1" = "" ]; then
echo -e "Error, requires an argument!\n\n"
echo -e "Example:\n\ntools/Hindent hosts"
exit 1
if [ -z "$1" ]; then
echo "Usage: $0 [your-hosts-file]"
exit 1
fi

hosts_line "$1"
hosts_date "$1"
hosts_format "$1"

eol_amend "$1"
date_amend "$1"
style_amend "$1"
168 changes: 74 additions & 94 deletions tools/checkhosts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,113 +5,93 @@ LINE_BREAK=0
FORMAT_BREAK=0
DATE_BREAK=0

# 1. check line endings
#
chk_line() {
local ret=$(dos2unix -id "$1" | egrep -o "[0-9]+")

echo -e "1. check line endings:\n"

if [ "$ret" -ne 0 ]; then
echo -e "\033[41mDOS line endings $ret times appeared, " \
"it must be coverted!\033[0m\n\n"
LINE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
chk_eol()
{
echo -e "- Check line endings:\n"

if file "$1" | grep "CRLF" -q; then
echo -e "\033[41mDOS line endings have appeared, " \
"it must be coverted now!\033[0m\n\n"
LINE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
}

# 2. check hosts format, only used if STRICT_HOSTS_FORMAT already set
#
chk_format() {
local loc="[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+"
local in_fmt="[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$(echo -e "\t")[[:alnum:]]\+"

echo -e "2. check hosts format:\n"
chk_format()
{
echo -e "- Check hosts format:\n"

grep "$loc" "$1" > 1.txt
grep "$in_fmt" "$1" > 2.txt
grep -P "\d+\.\d+\.\d+\.\d+" "$1" > 1.txt
grep -P "\d+\.\d+\.\d+\.\d+\t\w+" "$1" > 2.txt

diff -q 1.txt 2.txt
if ! diff 1.txt 2.txt > 0.txt; then
echo -e "\n\033[41mhosts format mismatch! " \
"The following rules should be normalized:\033[0m"
cat 0.txt
FORMAT_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m"
fi

if [ "$?" -ne 0 ]; then
echo -e "\n\033[41mhosts format mismatch! " \
"The following rules should be normalized:\033[0m"
diff 1.txt 2.txt
FORMAT_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m"
fi

echo -e "\n"
rm -f 1.txt 2.txt
echo -e "\n"
rm -f 0.txt 1.txt 2.txt
}

# 3. check "Last updated", only used if STRICT_HOSTS_FORMAT already set
#
chk_date() {
# system date
local real_date=$(date +%F)
# date in git repo's log
local repo_date=$(git log --date=short "$1" | egrep -o "[0-9]+-[0-9]+-[0-9]+" -m 1)
# date string in hosts file.
local hosts_date=$(egrep -o "[0-9]+-[0-9]+-[0-9]+" "$1")

echo -e "3. check hosts date:\n"

# detect hosts file changes
if git diff --exit-code "$1" > /dev/null; then
# hosts file isn't changed, compare date with git's log.
if [ "$repo_date" != "$hosts_date" ]; then
echo -e "\033[41mhosts date mismatch, last modified is $repo_date, " \
"but hosts tells $hosts_date\033[0m\n\n"
DATE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
else
# hosts file is being editing, and not commit yet.
# Compare date with system date.
if [ "$real_date" != "$hosts_date" ]; then
echo -e "\033[41mhosts date mismatch, last modified is $real_date, " \
"but hosts tells $hosts_date\033[0m\n\n"
DATE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
fi
chk_date()
{
# get the system date
local real_date=$(date +%F)
# get the hosts last modified in git log records
local repo_date=$(git log --date=short "$1" | grep -Pom1 "\d+-\d+-\d+")
# date string in hosts file.
local hosts_date=$(grep -Po "\d+-\d+-\d+" "$1")

echo -e "- Check hosts date:\n"

# check if hosts file changes
if git diff --exit-code "$1" > /dev/null 2>&1; then
# hosts file is not changed, compare file's date with git log.
if [ "$repo_date" != "$hosts_date" ]; then
echo -e "\033[41mhosts date mismatch, last modified " \
"is $repo_date, but hosts tells " \
"$hosts_date\033[0m\n\n"
DATE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
else
# hosts file is being editing, and has not been committed.
# Compare file's date with the system date.
if [ "$real_date" != "$hosts_date" ]; then
echo -e "\033[41mhosts date mismatch, last modified " \
"is $real_date, but hosts tells " \
"$hosts_date\033[0m\n\n"
DATE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
fi
}

#
# Result
#
result () {
echo -e "Result:\n"

echo -e "line endings break? $LINE_BREAK (1 = yes, 0 = no)"

if [ -n "$STRICT_HOSTS_FORMAT" ]; then
echo -e "hosts format mismatch? $FORMAT_BREAK (1 = yes, 0 = no)"
echo -e "hosts date mismatch? $DATE_BREAK (1 = yes, 0 = no)"
result()
{
echo -e "Result (1 = yes, 0 = no):\n"
echo "line endings break? [ $LINE_BREAK ]"
echo "hosts format mismatch? [ $FORMAT_BREAK ]"
echo "hosts date mismatch? [ $DATE_BREAK ]"

local ret=$(echo -e "$LINE_BREAK $FORMAT_BREAK $DATE_BREAK" \
| grep -o "1" | wc -w)
exit $ret
else
exit $LINE_BREAK
fi
local ret=$(expr $LINE_BREAK + $FORMAT_BREAK + $DATE_BREAK)
exit $ret
}

if [ "$1" = "" ]; then
echo -e "\033[41mError, requires an argument!\033[0m"
exit -1
if [ -z "$1" ]; then
echo "Usage: $0 [your-hosts-file]"
exit -1
fi

chk_line "$1"

if [ -n "$STRICT_HOSTS_FORMAT" ]; then
chk_format "$1"
chk_date "$1"
fi
chk_eol "$1"
chk_format "$1"
chk_date "$1"

result

0 comments on commit 85f8687

Please sign in to comment.