forked from CoatiSoftware/Sourcetrail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-sync.sh
More file actions
executable file
·105 lines (85 loc) · 2.09 KB
/
Copy pathgit-sync.sh
File metadata and controls
executable file
·105 lines (85 loc) · 2.09 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
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
#!/bin/bash
ABORT="\033[31mAbort:\033[00m"
ALERT="\033[31mAlert:\033[00m"
SUCCESS="\033[32mSuccess:\033[00m"
INFO="\033[33mInfo:\033[00m"
BRANCH_NAME=$(git symbolic-ref -q --short HEAD)
#set branch to sync with
if [[ $# -gt 0 ]]
then
SYNC_BRANCH=$1
else
SYNC_BRANCH="master"
fi
echo -e $INFO "branch to commit: $SYNC_BRANCH"
# check if on branch
echo -e $INFO "Checking on branch"
if [ -z $BRANCH_NAME ]
then
echo -e $ABORT "You are not on any branch."
exit 1
fi
# check clean index
echo -e $INFO "Checking index"
if [[ -n $(git diff HEAD) ]]
then
echo -e $ABORT "You have uncommited changes."
exit 1
fi
# switch to $SYNC_BRANCH
if [ $BRANCH_NAME != '$SYNC_BRANCH' ]
then
echo -e $INFO "Switching to $SYNC_BRANCH"
git checkout -q $SYNC_BRANCH
if [ $? != 0 ]
then
echo -e $ABORT "Switching to $SYNC_BRANCH failed."
exit 1
fi
fi
# fetch remote changes
echo -e $INFO "Fetching changes"
git fetch origin $SYNC_BRANCH --tags
if [ $? != 0 ]
then
echo -e $ABORT "Fetching from origin $SYNC_BRANCH failed"
git checkout -q "$BRANCH_NAME"
exit 1
fi
# rebase $SYNC_BRANCH to origin/$SYNC_BRANCH
if [[ -z $(git diff origin/$SYNC_BRANCH) ]]
then
echo -e $SUCCESS "Your $SYNC_BRANCH is up-to-date"
else
echo -e $INFO "Rebasing to origin/$SYNC_BRANCH"
git rebase origin/$SYNC_BRANCH
if [ $? != 0 ]
then
echo -e $ALERT "Rebasing on $SYNC_BRANCH caused conflicts. This should never happen. Follow the instructions above to resolve them then use 'git push origin master' to update the remote."
exit 1
fi
echo -e $SUCCESS "Updated $SYNC_BRANCH"
fi
if [ $BRANCH_NAME == '$SYNC_BRANCH' ]
then
exit 0
fi
# switch to branch
echo -e $INFO "Switching back to $BRANCH_NAME"
git checkout -q "$BRANCH_NAME"
if [ $? != 0 ]
then
echo -e $ABORT "Switching back to $BRANCH_NAME failed."
exit 1
fi
# rebase branch to $SYNC_BRANCH
echo -e $INFO "Rebasing $BRANCH_NAME to $SYNC_BRANCH"
git rebase $SYNC_BRANCH
if [ $? != 0 ]
then
echo -e $ALERT "Rebasing $BRANCH_NAME to $SYNC_BRANCH caused conflicts. Follow the instructions above to resolve them."
exit 1
else
echo -e $SUCCESS "$BRANCH_NAME is now up-to-date."
fi
exit 0