-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
update.sh
executable file
·83 lines (70 loc) · 2.14 KB
/
update.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
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
#!/bin/bash
NOTIFICATION_SENT_FILE="/tmp/git_update_notification_sent"
FORCE_UPDATE=false
if [ "$1" == "--force" ]; then
FORCE_UPDATE=true
fi
perform_update() {
echo ":: Removing skip-worktree flags..."
git ls-files -v | grep '^S' | awk '{print $2}' | xargs git update-index --no-skip-worktree
BACKUP_DIR="$HOME/dotfiles/.backup"
TIMESTAMP=$(date +%F_%H-%M-%S)
if [ -d "$BACKUP_DIR" ]; then
NEW_BACKUP_DIR="${BACKUP_DIR}_old/${TIMESTAMP}"
mkdir -p "$NEW_BACKUP_DIR"
echo ":: Moving existing backup to $NEW_BACKUP_DIR..."
mv "$BACKUP_DIR" "$NEW_BACKUP_DIR"
fi
echo ":: Creating backup of modified files..."
mkdir -p "$BACKUP_DIR"
git diff --name-only | while read -r file; do
if [ -f "$file" ]; then
mkdir -p "$BACKUP_DIR/$(dirname "$file")"
cp "$file" "$BACKUP_DIR/$file"
fi
done
echo ":: Performing repository update..."
git fetch origin
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo ":: Performing hard reset..."
git reset --hard origin/$CURRENT_BRANCH
echo ":: Running post-update script..."
"$HOME/dotfiles/setup/after_update.sh"
rm -f $NOTIFICATION_SENT_FILE
}
"$HOME/dotfiles/scripts/check_updates.sh" >/dev/null
status=$?
case $status in
0)
echo ":: No updates available."
if [ "$FORCE_UPDATE" == true ]; then
echo ":: Forcing update despite no updates..."
perform_update
fi
;;
1)
echo ":: Cannot update: Changes detected in files."
if [ "$FORCE_UPDATE" == true ]; then
echo ":: Forcing update despite local changes..."
perform_update
else
echo ":: You can write \"$ git diff-index --name-only HEAD --\" to see which files have changed"
echo ":: Use --force to ignore changes"
exit 1
fi
;;
2)
echo ":: Updates are available."
perform_update
;;
3)
echo ":: Branches have diverged. Manual intervention may be required."
if [ "$FORCE_UPDATE" == true ]; then
echo ":: Forcing update despite branch divergence..."
perform_update
fi
;;
*)
echo ":: Unknown error."
;;
esac