-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gitupdate.sh
51 lines (46 loc) · 1.18 KB
/
gitupdate.sh
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
#!/bin/bash
# Scriptacular - gitupdate.sh
# Compare SHA-1 between local and remote repositories, git pull + FF merge in local if they differ
# Copyright 2013 Christopher Simpkins
# MIT License
# Default to working directory
LOCAL_REPO="."
# Default to git pull with FF merge in quiet mode
GIT_COMMAND="git pull --quiet"
# User messages
GU_ERROR_FETCH_FAIL="Unable to fetch the remote repository."
GU_ERROR_UPDATE_FAIL="Unable to update the local repository."
GU_ERROR_NO_GIT="This directory has not been initialized with Git."
GU_INFO_REPOS_EQUAL="The local repository is current. No update is needed."
GU_SUCCESS_REPORT="Update complete."
if [ $# -eq 1 ]; then
LOCAL_REPO="$1"
cd "$LOCAL_REPO"
fi
if [ -d ".git" ]; then
# update remote tracking branch
git remote update >&-
if (( $? )); then
echo $GU_ERROR_FETCH_FAIL >&2
exit 1
else
LOCAL_SHA=$(git rev-parse --verify HEAD)
REMOTE_SHA=$(git rev-parse --verify FETCH_HEAD)
if [ $LOCAL_SHA = $REMOTE_SHA ]; then
echo $GU_INFO_REPOS_EQUAL
exit 0
else
$GIT_COMMAND
if (( $? )); then
echo $GU_ERROR_UPDATE_FAIL >&2
exit 1
else
echo $GU_SUCCESS_REPORT
fi
fi
fi
else
echo $GU_ERROR_NO_GIT >&2
exit 1
fi
exit 0