Last active
January 9, 2018 19:09
-
-
Save Aleksandr-ru/c6a78ea3f21ffa6fd53ed45b2a9c030c to your computer and use it in GitHub Desktop.
Pull git repo script with reset support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Usage: | |
# ./pull-repo.sh /path/to/reponame [branchname =master] | |
# | |
# Logs: | |
# /var/log/gitbot/reponame.log | |
if [ ! -d "$1" ]; then | |
echo "'$1' is not a directory!" | |
echo "Usage:" | |
echo "./pull-repo.sh /path/to/reponame [branchname =master]" | |
exit 1 | |
fi | |
if [ -z $2 ]; then | |
BRANCH=master | |
else | |
BRANCH=$2 | |
fi | |
REPO_DIR=${1%/} | |
REPO_NAME=${REPO_DIR##*/} | |
LOG=/var/log/gitbot/$REPO_NAME.log | |
DATE=`date +%Y-%m-%d.%H:%M:%S` | |
echo "$DATE $REPO_NAME [$BRANCH] $REPO_DIR" >> $LOG | |
cd $REPO_DIR | |
#git pull origin $BRANCH 2>&1 >> $LOG | |
git fetch origin 2>&1 | grep $BRANCH | grep "(forced update)" >> $LOG 2>&1 | |
if [[ $? -eq 0 ]]; then | |
git reset --hard origin/$BRANCH >> $LOG 2>&1 | |
if [[ $? -eq 0 ]]; then | |
echo Resetting brahch $BRANCH | |
else | |
echo Error resetting branch $BRANCH, see $LOG for details | |
exit 1 | |
fi | |
else | |
git pull origin $BRANCH >> $LOG 2>&1 | |
if [[ $? -eq 0 ]]; then | |
echo Normal pull branch $BRANCH | |
else | |
echo Error pulling branch $BRANCH, see $LOG for details | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment