This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-git-repo
executable file
·81 lines (72 loc) · 1.64 KB
/
sync-git-repo
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
#!/usr/bin/env bash
Help(){
# Show Help
echo "Automatically commit & sync changes in git repositoy."
echo
echo "Syntax: sync-git-repo [-option] <path>"
echo
echo "options:"
echo " -h Print this help message"
echo
}
Run(){
TARGET_GIT_DIR="$1"
echo $TARGET_GIT_DIR
echo "Begining sync..."
# Move to notable directory
if cd $TARGET_GIT_DIR; then
echo "Working from directory '`pwd`'..."
else
echo "Error: Issues arose changing to directory '$TARGET_GIT_DIR'"
echo "Exiting..."
exit 1
fi
# If changes exist, commit changes
if [[ `git status --porcelain` ]]; then
echo "Changes found..."
TIMESTAMP=`date +%Y-%m-%d_%H:%M:%S`
echo "Commit to be stamped $TIMESTAMP..."
git add .
git commit -q -m "Auto commit - $TIMESTAMP"
echo "Changes commited, commit `git rev-parse --short HEAD` created..."
else
echo "No changes detected..."
fi
# Sync with remote
echo "Syncing repository..."
if `git pull -q`; then
echo "Pull successful."
else
echo "Error: Something went wrong during 'git pull'..."
echo "Exiting..."
exit 1
fi
if `git push -q`; then
echo "Push successful."
else
echo "Error: Sync unsuccessful!"
echo "Something went wrong during 'git push'..."
exit 1
fi
echo "Complete."
}
while getopts "h:" opt; do
case $opt in
h) # display Help
Help
exit 0;;
\?) # incorrect option
echo "Error: Invalid option $opt"
exit 2;;
esac
done
EXPECTED_ARGS=1
if [[ $# -eq 0 ]]; then
echo "Error: No arguments supplied"
exit 2
elif [[ $# -eq $EXPECTED_ARGS ]]; then
Run "$@"
else
echo "Error: Incorrect number of arguments supplied. Expected $EXPECTED_ARGS, recieved $#"
exit 2
fi