-
Notifications
You must be signed in to change notification settings - Fork 0
/
versioning_install.sh
executable file
·173 lines (155 loc) · 6.22 KB
/
versioning_install.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
#
log_active=0
bash_file=~/.bashrc
git_wrapper_file=~/.git-wrapper
log() {
if [ $log_active -eq 1 ]; then
echo "$1"
fi
}
git() {
".git/hooks/pre-git" "$@"
}
generate_git_pre_merge_hook_file() {
printf "#!/bin/bash\n#\n\nfind_and_invoke_pre_git_hook() {\n\tfull_path_to_hook=\$(\git rev-parse --show-toplevel 2>/dev/null)\n\tif [ \"\$full_path_to_hook\" == \"\" ]; then\n\t\techo \"Git-Wrapper: Could not find a git repository. Invoking command directly.\"\n\t\t\\git \"\$@\"\n\telif [ -e \"\${full_path_to_hook}/.git/hooks/pre-git\" ]; then\n\t\t\"\${full_path_to_hook}/.git/hooks/pre-git\" \"\$@\"\n\telse\n\t\techo \"Git-Wrapper: Versioning-Tool not installed to this git repository. Invoking command directly.\"\n\t\t\git \"\$@\"\n\tfi\n}\n\nfind_and_invoke_pre_git_hook \"\$@\"\n" > "$git_wrapper_file"
}
add_alias_unless_existing_for_pre_merge_hook() {
# 1. scan ./bashrc for git alias.
result=$(grep 'alias git="~/.git-wrapper"' $bash_file)
if [ "$result" == "" ]; then
# 2. if not found add alias
printf "\n%s\n" "alias git=\"~/.git-wrapper\"" >> "$bash_file"
fi
if [ ! -f "$git_wrapper_file" ]; then
# 3. add script to user home dir where bashrc is too: this is the merge hook.
generate_git_pre_merge_hook_file
fi
}
delete_old_git_repo() {
rm -r -f ".git" >/dev/null 2>&1
rm -f "version" >/dev/null 2>&1
}
# only removes sample files.
remove_old_hooks() {
rm -f .git/hooks/*.sample >/dev/null 2>&1
}
install_new_hooks() {
cp versioning_hooks/versioning_tool_* .git/hooks/ >/dev/null 2>&1
cp versioning_hooks/pre-git .git/hooks/ >/dev/null 2>&1
full_path_to_hook=$(\git rev-parse --show-toplevel 2>/dev/null)
if [ "$full_path_to_hook" == "" ]; then
echo "Could not find a git repository. Cannot install scripts. This is an indicator that something went wrong in this script."
else
# post-commit
result=$(grep '.git/hooks/versioning_tool_post_commit.sh' "${full_path_to_hook}/.git/hooks/post-commit" 2>/dev/null)
if [ "$result" == "" ]; then
result=$(grep '#!/bin/' "${full_path_to_hook}/.git/hooks/post-commit" 2>/dev/null)
if [ "$result" == "" ]; then
printf "%s\n\n" "#!/bin/bash" > "${full_path_to_hook}/.git/hooks/post-commit"
fi
printf "\n%s\n" ".git/hooks/versioning_tool_post_commit.sh" >> "${full_path_to_hook}/.git/hooks/post-commit"
fi
# post-merge
result=$(grep '.git/hooks/versioning_tool_post_merge.sh' "${full_path_to_hook}/.git/hooks/post-merge" 2>/dev/null)
if [ "$result" == "" ]; then
result=$(grep '#!/bin/' "${full_path_to_hook}/.git/hooks/post-merge" 2>/dev/null)
if [ "$result" == "" ]; then
printf "%s\n\n" "#!/bin/bash" > "${full_path_to_hook}/.git/hooks/post-merge"
fi
printf "\n%s\n" '.git/hooks/versioning_tool_post_merge.sh "$@"' >> "${full_path_to_hook}/.git/hooks/post-merge"
fi
# prepare-commit-msg
result=$(grep '.git/hooks/versioning_tool_prepare_commit_msg.sh' "${full_path_to_hook}/.git/hooks/prepare-commit-msg" 2>/dev/null)
if [ "$result" == "" ]; then
result=$(grep '#!/bin/' "${full_path_to_hook}/.git/hooks/prepare-commit-msg" 2>/dev/null)
if [ "$result" == "" ]; then
printf "%s\n\n" "#!/bin/bash" > "${full_path_to_hook}/.git/hooks/prepare-commit-msg"
fi
printf "\n%s\n" '.git/hooks/versioning_tool_prepare_commit_msg.sh "$1"' >> "${full_path_to_hook}/.git/hooks/prepare-commit-msg"
fi
fi
chmod 755 .git/hooks/*
}
install_wrapper() {
add_alias_unless_existing_for_pre_merge_hook
chmod 755 "$git_wrapper_file"
}
init_git_repo() {
full_path_to_hook=$(\git rev-parse --show-toplevel 2>/dev/null)
if [ "$full_path_to_hook" == "" ]; then
log "Creating new git repo"
versioning_hooks/pre-git init >/dev/null 2>&1
fi
log "Remove sample hooks in git repo"
remove_old_hooks
log "Install hooks from versioning_hooks/"
install_new_hooks
}
setup() {
git commit -m 'Initial Commit' --no-increment #>/dev/null 2>&1
git checkout -b major #>/dev/null 2>&1
git commit -m 'Initialize Major Branch' --no-increment #>/dev/null 2>&1
git checkout -b minor #>/dev/null 2>&1
git commit -m 'Initialize Minor Branch' --no-increment #>/dev/null 2>&1
git checkout -b develop #>/dev/null 2>&1
}
print_usage() {
echo "versioning_install.sh - Usage:"
echo "\"./versioning_install.sh 0\": Install git_wrapper and alias to git_wrapper ONLY."
echo " No git repo will be initialized in the current directory"
echo ""
echo "\"./versioning_install.sh 1\": Same like mode 0 but also install scripts to git "
echo " repo in the current working directory."
echo " If no git repo exists, one will be initialized."
echo " No branches will be initialized."
echo " This is the way to go for existing git repos where"
echo " you want to start using the versioning tool."
echo ""
echo "\"./versioning_install.sh 2\": Same like mode 1 but also initialize branches"
echo " to match the versioning_tool's setup."
echo " This is the way to go if you want to start with a"
echo " fresh git repo using the versioning_tool in the"
echo " cleanest way possible from the beginning."
echo ""
echo "\"./versioning_install.sh 3\": Same like mode 1 but also deletes an existing"
echo " git repo if found in the current directory."
echo " This is used by the tests.sh, so be aware"
echo " that you run the tests in a safe environment!"
echo ""
echo "\"./versioning_install.sh 4\": Same like mode 2 but also deletes an existing"
echo " git repo if found in the current directory."
echo " This is used by the tests.sh, so be aware"
echo " that you run the tests in a safe environment!"
echo ""
}
if [ "$1" == "0" ]; then
log "Installing wrapper ONLY..."
install_wrapper
elif [ "$1" == "1" ]; then
log "Installing wrapper and init git repo..."
install_wrapper
init_git_repo
elif [ "$1" == "2" ]; then
log "Installing wrapper, init git repo and setup branches..."
install_wrapper
init_git_repo
setup
elif [ "$1" == "3" ]; then
log "Installing wrapper, delete old git repo, init new git repo..."
install_wrapper
log "Deleting old git repo"
delete_old_git_repo
init_git_repo
elif [ "$1" == "4" ]; then
log "Installing wrapper, delete old git repo, init new, setup branches..."
install_wrapper
log "Deleting old git repo"
delete_old_git_repo
init_git_repo
setup
else
print_usage
fi
chown -R $USER:$USER .
chmod -R 755 *