-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_repo_as_needed
executable file
·109 lines (100 loc) · 3.26 KB
/
update_repo_as_needed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /usr/bin/env bash
function log(){
echo "`date` - ${1}"
}
################################################################################
#
# can be used to check and update (as needed) a git repository
# if invoked with no parameters the current directory will be checked and, if
# it's a git repo with remote changes available, updated
#
# usage:
# update_repo_as_needed [expand] [directory]
#
################################################################################
ACTION=$1
case $ACTION in
'-h')
ACTION=help
;;
'--help')
ACTION=help
;;
expand)
;;
test_only)
;;
*)
# don't know what it is, must be trying to give us a dir
REPO_DIR=$ACTION
# currently this action triggers nothing explicitly this is
# only here because I suck
ACTION=update
;;
esac
if [ $ACTION == "help" ]; then
echo "`basename $0` [expand | test_only] [directory]"
echo " expand - causes the output of the full path to the script and the"
echo " repo directories to be sent to stdout, used to facilitate"
echo " creating crontab entries"
echo " directory - optional repository directory to check, if not "
echo " provided the current directory is checked"
echo " test_only - indicates that the check to see if an update is available"
echo " should be performed, but no update should be done"
echo ""
echo "Actions:"
echo " If the repo dir contains a file named update_repo_action which is"
echo " executable, that file will be executed after an update of the "
echo " contents of the repo dir. That is: if new code is retrieved and an"
echo " executeable file 'update_repo_action' exists in the root of the repo"
echo " it will be executed."
exit 1
fi
shift
if [ "${REPO_DIR}x" == "x" ]; then
REPO_DIR=$1
shift
fi
if [ "${REPO_DIR}x" == "x" ]; then
# no dir provided, assume current directory
REPO_DIR=`pwd`
fi
cd ${REPO_DIR}
# want to get the full directory for our repo
REPO_DIR=`pwd`
git rev-parse --git-dir >> /dev/null 2>&1
if [ $? -ne 0 ]; then
log "${REPO_DIR} doesn't appear to be a git repo, nothing to do"
exit 2
fi
if [ $ACTION == "expand" ]; then
printf "* * * * *\t$0 $REPO_DIR >> ~/repo_update.log 2>&1"
exit 3
fi
FETCH_RESULTS=`git fetch --all `
if [ $? -ne 0 ]; then
echo "fetch error"
echo $FETCH_RESULTS
exit 4
fi
CURRENT_BRANCH=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
CURRENT_SHA=`git log -n 1 --no-merges --pretty=format:%h`
ORIGIN_URL=`git config remote.origin.url`
HAS_CHANGES=`git diff HEAD...origin/${CURRENT_BRANCH} --raw`
log "checking ${REPO_DIR} for any available changes from ${ORIGIN_URL}"
if [ "${HAS_CHANGES}x" == "x" ]; then
log "no changes, nothing to update"
exit 5
else
if [ ! "$ACTION" == "test_only" ]; then
log "deploying changes for branch ${CURRENT_BRANCH} from ${ORIGIN_URL}"
git reset --hard origin/${CURRENT_BRANCH}
# if we have a post update script, run it
if [ -x ${REPO_DIR}/update_repo_action ]; then
${REPO_DIR}/update_repo_action
fi
else
log "changes detected for branch ${CURRENT_BRANCH} from ${ORIGIN_URL}"
fi
exit 100
fi