-
Notifications
You must be signed in to change notification settings - Fork 10
/
has-git-remote-new-commits.sh
executable file
·47 lines (38 loc) · 1.19 KB
/
has-git-remote-new-commits.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
##!/bin/bash
echo "== Checks if a REMOTE git repo has NEW COMMITS ==
";
function initGlobals(){
set -Eeuo pipefail; # set -o xtrace;
IFS=$'\n\t'
readonly DIR=$PWD;
readonly BRANCH_TO_CHECK="master";
readonly ROOT_DIR="$(dirname "${DIR}")";
}
function getLatestCommitHashFromLocal(){
echo "Collecting the HASH from the latest LOCAL commit..." >&2;
local readonly LAST_LOCAL_COMMIT=`git rev-parse refs/heads/"${BRANCH_TO_CHECK}"`;
echo $LAST_LOCAL_COMMIT >&2;
echo $LAST_LOCAL_COMMIT;
}
function getLatestCommitHashFromRemote(){
echo "Collecting the HASH from the latest REMOTE commit..." >&2;
local readonly LAST_REMOTE_COMMIT=`git ls-remote origin -h refs/heads/"${BRANCH_TO_CHECK}" | awk '{ print $1}'`;
echo $LAST_REMOTE_COMMIT >&2;
echo $LAST_REMOTE_COMMIT;
}
function hasRemoteNewCommits(){
local LOCAL_HASH=$(getLatestCommitHashFromLocal);
local REMOTE_HASH=$(getLatestCommitHashFromRemote);
if [[ $LOCAL_HASH != $REMOTE_HASH ]]; then
echo true;
else
echo false;
fi
}
initGlobals
readonly HAS_REMOTE_NEW_COMMITS=$(hasRemoteNewCommits);
if [[ $HAS_REMOTE_NEW_COMMITS = true ]]; then
echo "REMOTE has new COMMITS!";
else
echo "Nothing new."
fi