gfwlist/sendGFWList.sh

139 lines
4.2 KiB
Bash
Raw Normal View History

2009-03-11 08:36:57 +00:00
#!/bin/bash
#
# A simple script help to maintain AutoProxy gfwList easily.
#
# Features:
2009-03-11 08:36:57 +00:00
# Update local svn repository;
# Commit decoded changes(by others in your team) to local git repository
# with decoded message and authors name;
# Update "Last Modified" & "Checksum";
2009-03-11 08:36:57 +00:00
# Commit your changes to local git repository;
# Commit your encoded changes to remote svn server with encoded log;
# Plus some error handling.
2009-03-11 08:36:57 +00:00
# Usage:
# Initialize:
# $svn checkout https://autoproxy-gfwlist.googlecode.com/svn/trunk/ gfwList --username your-google-user-name
# $cd gfwList
# $git init
# $openssl base64 -d -in gfwlist.txt -out list.txt
2009-03-11 08:36:57 +00:00
# $git add list.txt
# $git commit -a -m "init"
# Normal Usage:
# edit list.txt as usual;
2009-03-11 08:49:49 +00:00
# $./sendGFWList.sh "say something about this edit"
2009-03-11 08:36:57 +00:00
# Note:
# 1: You can use "$git log" "$git show" "$git diff"...;
2009-08-11 08:32:46 +00:00
# 2: Do NOT commit "list.txt" to svn server (it won't by default);
# 3: Do NOT use any unicode character in the list, there is a known bug;
# 4: Do NOT "svn update", run this script to update / commit at any time.
2009-08-11 08:42:19 +00:00
################################################################################
2009-03-11 08:36:57 +00:00
# dependence
for cmd in sed openssl awk svn git perl file
2009-03-11 08:36:57 +00:00
do
2009-03-11 09:10:07 +00:00
which $cmd &> /dev/null;
if [ $? -ne 0 ]; then
2010-04-18 07:14:09 +00:00
echo "Error: depends on $cmd, please install it first.";
2009-03-11 09:10:07 +00:00
exit 1;
fi
2009-03-11 08:36:57 +00:00
done
# get formated author and log information
log=$(svn log --xml -r BASE:HEAD) || exit 1;
log=$(echo $log | awk -v RS='' -F '</?author>|</?msg>' '{ for(i=6;i<NF;i+=4) printf "%s:%s;",$i,$(i+2); }') &&
2009-03-11 08:36:57 +00:00
# convert from base64
i=0 &&
convertedLog="" &&
while [ "$log" != "" ]
do
if (( $i%2 == 0 )); then # author
temp=${log%%:*};
2009-03-11 16:13:48 +00:00
convertedLog+=${temp%@*}; # don't include "@gmail.com"
convertedLog+=": ";
2009-03-11 08:45:34 +00:00
# discard used string
2009-03-11 08:36:57 +00:00
log=${log#*:};
else # log, decode it
temp=$( echo ${log%%;*} | openssl base64 -d );
2009-03-11 08:36:57 +00:00
convertedLog+=$temp;
convertedLog+="\n";
2009-03-11 08:36:57 +00:00
log=${log#*;};
fi
((i++));
done
2010-04-18 07:14:09 +00:00
# modified by others, commit to local git repository
2009-03-11 08:36:57 +00:00
if [ "$convertedLog" != "" ]; then
2010-04-18 07:14:09 +00:00
git diff > temp.patch &&
2009-03-15 01:00:35 +00:00
2010-04-18 07:14:09 +00:00
svn update || exit 1;
2009-03-15 01:00:35 +00:00
openssl base64 -d -in gfwlist.txt -out list.txt &&
./validateChecksum.pl list.txt;
2010-04-18 07:14:09 +00:00
if [ $? -ne 0 ]; then
# recover, discard broken list.txt
git checkout list.txt && git apply temp.patch && rm temp.patch;
echo -e "\n\n\n*********************************************************\n";
echo "Error: gfwlist.txt from svn is invalid!!!";
2010-04-18 07:14:09 +00:00
echo "It must be a download error or somebody made a mistake.";
echo -e "\nYou can simply run this script again to fix the problem.";
echo "But wait...!"
echo "This would overwrite all commits till your last update!!!";
echo -e "\nIf you are confused, wait somebody else to fix it.";
echo "Please always report this to our maintainers group!";
echo -e "\n*********************************************************\n\n\n";
2010-04-18 07:14:09 +00:00
exit 1;
fi
echo -e $convertedLog | git commit -a -F - &&
2009-03-11 08:36:57 +00:00
[ -s temp.patch ] && git apply temp.patch &&
2010-04-18 07:56:48 +00:00
rm temp.patch;
2010-04-18 07:14:09 +00:00
2010-04-18 07:56:48 +00:00
if [ -s temp.patch ]; then
echo "Error: git apply failed, your work saved at temp.patch";
exit 1;
elif [ -a temp.patch ]; then
# empty, remove it
rm temp.patch;
fi
2009-03-15 01:00:35 +00:00
fi
if [ "$(git diff)" == "" ]; then
2010-04-18 07:14:09 +00:00
echo "Info: list.txt not modified.";
2009-03-15 01:00:35 +00:00
exit 0;
fi
if [ "$*" == "" ]; then
2010-04-18 07:14:09 +00:00
echo "Error: empty log, please say something about this modification.";
2009-03-15 01:00:35 +00:00
exit 1;
2009-03-11 08:36:57 +00:00
fi
# update date and checksum
./addChecksum.pl list.txt &&
if [ "$(file -b list.txt)" != "ASCII text" ]; then
echo "Error: list.txt invalid, please make sure:";
2010-04-18 07:14:09 +00:00
echo "1. there is no non-ASCII characters;";
echo "2. configure your text editor to use unix style line break.";
2010-03-26 03:07:38 +00:00
exit 1;
2010-04-18 07:14:09 +00:00
fi
2010-04-18 07:14:09 +00:00
# save local changes to git & svn
# if conflict or network problem occurs: do nothing & throw error message
2009-03-11 08:36:57 +00:00
git commit -a -m "$*" &&
2009-03-15 01:00:35 +00:00
(
openssl base64 -in list.txt |
2010-04-18 07:14:09 +00:00
# convert dos new line to unix style, old mac style ignored
tr -d '\r' > gfwlist.txt &&
2010-04-18 07:14:09 +00:00
# may be failed because of connection/authentication problems
svn ci gfwlist.txt -m $( echo "$*" | openssl base64 | tr -d '\r\n' ) ||
2010-04-18 07:14:09 +00:00
# "svn ci" and "git commit" are atomic operations
2009-03-15 03:00:18 +00:00
git reset HEAD^ 1> /dev/null;
) &&
2010-04-18 07:14:09 +00:00
# BASE++ if committed
2009-03-15 03:00:18 +00:00
svn update 1> /dev/null;