-
Notifications
You must be signed in to change notification settings - Fork 2
/
post-checkout
executable file
·89 lines (70 loc) · 2.34 KB
/
post-checkout
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
#!/bin/sh
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
CONF_FILE="/htdocs/conf/conf.php"
BACKUP="/.git/dolibarr_conf/"
prev_head="$1"
new_head="$2"
chg_branch="$3"
if [ -z "$GIT_DIR" ]; then # Test si vide.
exit 1
fi
if [ $chg_branch -ne 1 ]; then
exit 1
fi
rldesc="$(git reflog | head -n 1)"
before="$(echo $rldesc | sed -e 's/^.* from \(.*\) to.*/\1/')"
after="$(echo $rldesc | sed -e 's/^.*to \(.*\)/\1/')"
if [ ! -z "$(git tag | grep -Fx $before)" ]; then
before="$(git rev-list -n 1 $before)"
fi
if [ ! -z "$(git tag | grep -Fx $after)" ]; then
after="$(git rev-list -n 1 $after)"
fi
toplevel="$(git rev-parse --show-toplevel)" # Get racine
prev_branch=$before # Get branch/tag name before checkout
new_branch=$after # Get branch/tag name after checkout
if [ $prev_branch = $new_branch ]; then
exit 0
fi
if [ ! -d "$toplevel$BACKUP" ]; then
mkdir -p $toplevel$BACKUP # Create dossier backup
fi
if [ -f $toplevel$CONF_FILE ]; then
if [ "$color_prompt" = yes ]; then
echo "\033[1;35mBacking up\033[0m ${CONF_FILE} into conf.php.${prev_branch}"
else
echo "Backing up ${CONF_FILE} into conf.php.${prev_branch}"
fi
mv "${toplevel}${CONF_FILE}" "${toplevel}${BACKUP}conf.php.${prev_branch}" # Backing up conf.php
fi
if [ -f $toplevel$BACKUP"conf.php."$new_branch ]; then
if [ "$color_prompt" = yes ]; then
echo "\033[1;35mRestoring\033[0m ${BACKUP}conf.php.${new_branch}"
else
echo "Restoring ${BACKUP}conf.php.${new_branch}"
fi
mv "${toplevel}${BACKUP}conf.php.${new_branch}" "${toplevel}${CONF_FILE}" # Restauring backup conf.php
else
echo "No backup found for this branch."
exec < /dev/tty # Input
while true; do
read -p "Do you want to keep the current configuration? (Y/n)" yn
if [ -z "$yn" ]; then
yn='Y'
fi
case $yn in
[Yy]*)
cp $toplevel$BACKUP"conf.php."$prev_branch $toplevel$CONF_FILE; break
;;
[Nn]*)
exit
;;
*)
echo "Please answer y or n."
;;
esac
done
fi
exit 0