Commit 5d198755 authored by Loic's avatar Loic
Browse files

Rewrite in sh

parent a92f9035
#!/bin/csh
#!/bin/sh
# set reply-to to robot in mail command
setenv REPLYTO "robot@hardenedbsd.org"
set OPWD=`pwd`
set SOURCE_DIR="/usr/data/source/git/opBSD"
set BRANCHES=`cat $SOURCE_DIR/hardenedBSD_branches.txt`
set SOURCE="$SOURCE_DIR/hardenedBSD.git"
set LOGS="$HOME/log/hardenedBSD"
set DATE=`date "+%Y%m%d%H%M%S"`
set TEE_CMD="tee -a"
set LOCK="${SOURCE_DIR}/hardenedbsd-repo-lock"
set DST_MAIL="robot@hardenedbsd.org"
set ENABLE_MAIL="YES"
REPLYTO="robot@hardenedbsd.org"
export REPLYTO
OPWD=`pwd`
SOURCE_DIR="/usr/data/source/git/opBSD"
BRANCHES=`cat $SOURCE_DIR/hardenedBSD_branches.txt`
SOURCE="$SOURCE_DIR/hardenedBSD.git"
LOGS="$HOME/log/hardenedBSD"
DATE=`date "+%Y%m%d%H%M%S"`
TEE_CMD="tee -a"
LOCK="${SOURCE_DIR}/hardenedbsd-repo-lock"
DST_MAIL="robot@hardenedbsd.org"
ENABLE_MAIL="YES"
test -d $LOGS || mkdir -p $LOGS
if ( -e ${LOCK} ) then
if [ -f "${LOCK}" ]
then
echo "update error at ${DATE} - lock exists"
if ( ${ENABLE_MAIL} == "YES" ) then
if [ "${ENABLE_MAIL}" = "YES" ]
then
echo "update error at ${DATE} - lock exists" | mail -s "hbsd - lock error" ${DST_MAIL}
endif
fi
exit 1
endif
fi
touch ${LOCK}
cd ${SOURCE}
set OHEAD=`git branch | awk '/\*/{print $2}'`
OHEAD=`git branch | awk '/\*/{print $2}'`
git stash
(git fetch origin) |& ${TEE_CMD} ${LOGS}/freebsd-fetch-${DATE}.log
(git fetch freebsd) |& ${TEE_CMD} ${LOGS}/freebsd-fetch-${DATE}.log
(git fetch origin) 2>&1 | ${TEE_CMD} ${LOGS}/freebsd-fetch-${DATE}.log
(git fetch freebsd) 2>&1 | ${TEE_CMD} ${LOGS}/freebsd-fetch-${DATE}.log
# pushing the freshly fetched FreeBSD commit notes to hardenedbsd repo
# these contains the svn revision ids
(git push --atomic origin refs/notes/commits) |& ${TEE_CMD} ${LOGS}/freebsd-fetch-${DATE}.log
(git push --atomic origin refs/notes/commits) 2>&1 | ${TEE_CMD} ${LOGS}/freebsd-fetch-${DATE}.log
foreach line ( ${BRANCHES} )
set err=0
set _mail_subject_prefix=""
for line in ${BRANCHES}
do
err=0
_mail_subject_prefix=""
set remote_branches=`echo ${line} | cut -d ':' -f 2 | tr '+' ' '`
set branch=`echo ${line} | cut -d ':' -f 1 | tr -d '#'`
set _branch=`echo ${branch} | tr '/' ':'`
remote_branches=`echo ${line} | cut -d ':' -f 2 | tr '+' ' '`
branch=`echo ${line} | cut -d ':' -f 1 | tr -d '#'`
_branch=`echo ${branch} | tr '/' ':'`
echo "==== BEGIN: ${branch} ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "==== BEGIN: ${branch} ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "current branch: ${branch}" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "mergeable branch: ${remote_branches}" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "current branch: ${branch}" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "mergeable branch: ${remote_branches}" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
# Skip lines beginning with '#'
echo ${line} | grep -Eq '^#.*'
if ( $? == 0 ) then
set _mail_subject_prefix="[SKIP]"
echo "==== SKIP: ${line} ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
goto handle_err
endif
echo "==== change branch ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if [ "${?}" = 0 ]
then
_mail_subject_prefix="[SKIP]"
echo "==== SKIP: ${line} ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
handle_err
fi
echo "==== change branch ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
# change branch
(git checkout ${branch}) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git checkout ${branch}) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "==== show current branch ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "==== show current branch ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
# show, that branch correctly switched
(git branch) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git branch) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "==== check for stale changes ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
set changed_files=`git status --porcelain -uall | wc -c`
echo "==== check for stale changes ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
changed_files=`git status --porcelain -uall | wc -c`
if ( ${changed_files} != 0 ) then
echo "==== drop stale changes ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if [ "${changed_files}" != 0 ]
then
echo "==== drop stale changes ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
# drop any stale change
(git status) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git reset --hard HEAD) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
endif
(git status) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git reset --hard HEAD) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
fi
echo "==== update to latest origin ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "==== update to latest origin ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
# pull in latest changes from main repo
(git pull) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if ( $? != 0 ) then
echo "ERROR: git pull failed, try to recover" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
( git reset --hard ) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
endif
echo "==== merge branches ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
foreach _remote_branch ( ${remote_branches} )
(git pull) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if [ "${?}" != 0 ]
then
echo "ERROR: git pull failed, try to recover" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
( git reset --hard ) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
fi
echo "==== merge branches ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
for _remote_branch in ${remote_branches}
do
# merge specific branches to current branch
echo "==== merge ${_remote_branch} branch ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git merge --log ${branch} ${_remote_branch}) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if ( $? != 0 ) then
set err=1
set _mail_subject_prefix="[MERGE]"
echo "==== merge ${_remote_branch} branch ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git merge --log ${branch} ${_remote_branch}) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if [ "${?}" != 0 ]
then
err=1
_mail_subject_prefix="[MERGE]"
# show what's wrong
echo "==== merge failed at ${_remote_branch} branch ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git diff) |& head -500 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git status) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git reset --hard) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git clean -fd) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
endif
end
if ( ${err} != 0 ) then
goto handle_err
endif
echo "==== merge failed at ${_remote_branch} branch ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git diff) 2>&1 | head -500 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git status) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git reset --hard) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git clean -fd) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
fi
done
if [ "${err}" != 0 ]
then
handle_err
fi
# update remote
(git push --atomic origin ${branch}) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if ( $? != 0 ) then
set _mail_subject_prefix="[PUSH]"
set err=1
goto handle_err
endif
(git push --atomic origin ${branch}) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if [ "${?}" != 0 ]
then
_mail_subject_prefix="[PUSH]"
err=1
handle_err
fi
handle_err:
if ( ${err} != 0 ) then
set _mail_subject_prefix="[FAILED]${_mail_subject_prefix}"
if [ "${err}" != 0 ]
then
_mail_subject_prefix="[FAILED]${_mail_subject_prefix}"
# create a clean state, if failed something
echo "==== merge failed and clean up after ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git status) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git reset --hard) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git clean -fd) |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
echo "==== merge failed and clean up after ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git status) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git reset --hard) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
(git clean -fd) 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
else
set _mail_subject_prefix="[OK]"
endif
_mail_subject_prefix="[OK]"
fi
echo "==== END: ${branch} ====" |& ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if ( ${ENABLE_MAIL} == "YES" ) then
echo "==== END: ${branch} ====" 2>&1 | ${TEE_CMD} ${LOGS}/${_branch}-${DATE}.log
if [ "${ENABLE_MAIL}" = "YES" ]
then
cat ${LOGS}/${_branch}-${DATE}.log | \
mail -s "${_mail_subject_prefix} ${_branch}-${DATE}.log" ${DST_MAIL}
endif
fi
echo
end
done
git checkout ${OHEAD}
git stash pop
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment