-
Notifications
You must be signed in to change notification settings - Fork 4
/
checkout.sh
executable file
·87 lines (72 loc) · 2.49 KB
/
checkout.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
84
85
86
87
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o noclobber
set -o nounset
#set -o xtrace
################################################################################
# COLORS #
################################################################################
cerr=`tput setaf 1; tput bold` # Red and bold color
cwarn=`tput setaf 3; tput bold` # Yellow and bold color
crst=`tput sgr0` # Reset color
################################################################################
# READ command-line options #
################################################################################
# Handle the option arguments
before="" # Before date
dry_run=0 # Whether to do a dry-run (1) or not (0)
usage() {
echo ""
echo "Usage:"
echo " -b DATETIME Checkout all commits before date/time"
echo " -X Do a dry-run"
exit 3
}
while getopts ":b:X" o; do
case "${o}" in
b)
before="$2"
;;
X)
dry_run=1
;;
*)
echo "${cerr}Programming error${crst}"
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${before}" ]; then
usage
fi
# Get the script directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
################################################################################
repos=$(find . -type d -path '**/*/.git' -prune -print0 | sort -z | xargs -0)
echo "This will HARD reset all repositories, you will LOSE ALL CHANGES."
if [[ "$dry_run" -eq 0 ]]; then
read -r -p "Are you sure? [y/N] " response
if ! [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
dry_run=1
fi
fi
echo "Resetting all repositories to commit before $before..."
for repo in ${repos}; do
(
cd "$repo/.."
echo "${repo}"
remotebranch=$(git rev-parse --abbrev-ref --symbolic-full-name @\{u\})
branch=$(git rev-parse --abbrev-ref HEAD)
commit=$(git rev-list -n 1 --first-parent --before="$before" "$remotebranch")
echo " $remotebranch @ $commit"
if [[ "$dry_run" -eq 0 ]]; then
git reset --hard "$commit" --quiet
fi
)
done
if [[ "$dry_run" -ne 0 ]]; then
echo "${cwarn}No changes were made because of dry-run.${crst}"
fi
echo "Done!"