-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·117 lines (102 loc) · 2.63 KB
/
update.sh
File metadata and controls
executable file
·117 lines (102 loc) · 2.63 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
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
items_to_update=(
"/home/swert/biosector01.com"
"/home/swert/biosector01.com/errors"
)
unneeded_updates=()
successful_updates=()
failed_updates=()
# adapted from https://stackoverflow.com/a/3278427
# Determines whether or not the branch currently at the argument's path has
# upstream changes.
# Echos:
# 1: local copy needs to be updated
# 0: local copy doesn't need to be updated
# -1: local copy needs to be pushed
# -2: local copy and upstream have diverged
# -3: local copy has uncommitted changes
needs_update () {
# if files in version control have local changes
if [ -n "$(git ls-files -m)" ]; then
echo -3
fi
local upstream='@{u}'
local local_id=$(git rev-parse @)
local remote_id=$(git rev-parse "$upstream")
local base_id=$(git merge-base @ "$upstream") # last common ancestor of local and remote
if [ $local_id = $remote_id ]; then # don't need to do anything
echo 0
elif [ $local_id = $base_id ]; then # need pull
echo 1
elif [ $remote_id = $base_id ]; then # need push
echo -1
else # diverged
echo -2
fi
}
# if possible, update the argument with git pull
update_item_if_needed () {
cd $1
git fetch -q
case $(needs_update $1) in
0)
echo -e "\t$1: already up-to-date"
unneeded_updates+=("$1")
;;
1)
echo -e "\t$1: updating from remote..."
git pull
successful_updates+=("$1")
;;
-1)
echo -e "\t$1: can't update (local is ahead of remote, need to push)"
failed_updates+=("$1: local is ahead of remote")
;;
-2)
echo -e "\t$1: can't update (local and remote diverged)"
failed_updates+=("$1: local and remote diverged")
;;
-3)
echo -e "\t$1: can't update (local has uncommitted changes)"
failed_updates+=("$1: local has uncommitted changes")
;;
esac
}
update () {
echo 'Attempting updates from git...'
# perform updates
for i in "${items_to_update[@]}"
do
update_item_if_needed $i
done
# echo '#################################################'
# echo
echo 'Finished updating!'
echo
echo "Summary:"
if ! [ ${#unneeded_updates[@]} -eq 0 ]; then
echo -e "\t${#unneeded_updates[@]} directories were already up-to-date"
for i in "${unneeded_updates[@]}"
do
echo -e "\t\t$i"
done
fi
# if updates succeeded, print which ones
if ! [ ${#succeeded_updates[@]} -eq 0 ]; then
echo -e "\t${#succeeded_updates[@]} directories successfully updated"
for i in "${succeeded_updates[@]}"
do
echo -e "\t\t$i"
done
fi
# if updates failed, print which ones and why
if ! [ ${#failed_updates[@]} -eq 0 ]; then
echo -e "\t${#failed_updates[@]} directories failed to update"
for i in "${failed_updates[@]}"
do
echo -e "\t\t$i"
done
fi
}
update