-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_upd
More file actions
executable file
·48 lines (43 loc) · 2.12 KB
/
git_upd
File metadata and controls
executable file
·48 lines (43 loc) · 2.12 KB
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
#!/usr/bin/env bash
# credit: @chrstphr
# $1 is local directory path
# $2 is git branch to create diff against
# $3 is ssh remote host
# $4 is remote directory path
LOCAL_PATH="${1:?"Local directory not specified!"}"
LOCAL_DIRNAME=$(basename "$LOCAL_PATH")
GIT_BRANCH="${2:?"Branch to compare with not specified"}"
REMOTE_HOST="${3:?"Remote host not specified!"}"
REMOTE_DIR="${4:?"Remote directory not specified!"}"
# check if local and remote repos are on the same commit. If not, error.
LOCAL_COMMIT=$(git -C "$LOCAL_PATH" rev-parse "$GIT_BRANCH")
REMOTE_COMMIT=$(ssh "$REMOTE_HOST" " git -C '$REMOTE_DIR' rev-parse '$GIT_BRANCH' ")
if [ "$LOCAL_COMMIT" != "$REMOTE_COMMIT" ]; then
echo "Local and remote are on different commits. Abort."
notify-send "❌❌❌ ${LOCAL_DIRNAME} ❌❌❌" "local and remote repositories are on different commits in branch ${GIT_BRANCH}!"
exit
fi
# check if the local diff is empty, if so only reset the remote repository, then early exit
git -C "$LOCAL_PATH" update-index --refresh -q
git -C "$LOCAL_PATH" diff-index --quiet "$GIT_BRANCH" --
if [ $? -eq 0 ]; then
echo "Git status was empty, therefore just clearing remote repo"
ssh "$REMOTE_HOST" " git -C '$REMOTE_DIR' reset --hard '$GIT_BRANCH' "
echo "Exit status of the clearing operation was $?"
notify-send "${LOCAL_DIRNAME} ✓" "Reset remote repo"
exit
fi
# git apply --index adds files that are untracked into the index, such that on
# the next git reset, this file will be deleted. Therefore, there will be no
# conflicts with untracked files
echo "Syncing"
git -C "$LOCAL_PATH" diff "$GIT_BRANCH" | ssh "$REMOTE_HOST" " git -C '$REMOTE_DIR' reset --hard '$GIT_BRANCH' && git -C '$REMOTE_DIR' apply --index --whitespace=nowarn - "
# get the exit status of the above command (even if the command within ssh fails, that is propagated to here) to notify of success/failure
exit_status=$?
if [ $exit_status -eq 0 ]; then
echo "Remote command succeeded (status 0)"
notify-send "${LOCAL_DIRNAME} ✓" "Syncing successful"
else
echo "Remote command failed with status $exit_status"
notify-send "❌❌❌ ${LOCAL_DIRNAME} ❌❌❌" "Syncing FAILED"
fi