blob: 30ca43f45df3d58abf5d7e293864575637980a7e (
plain)
1 #!/usr/bin/env bash
2 # Library for easier Git hook scripting
3 # Copyright (C) 2019 Aaron Ball <nullspoon@oper.io>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 set -eou pipefail
18
19 export OLDREV NEWREV REFNAME
20 declare -a TAGS
21 declare -a REFS
22 declare -a FILES_A
23 declare -a FILES_M
24 declare -a FILES_D
25 declare -a FILES_R
26
27 export IFS=$' \n\t'
28 read OLDREV NEWREV REFNAME
29 export IFS=$'\n'
30 [ ${OLDREV} = '0000000000000000000000000000000000000000' ] && OLDREV=${NEWREV}
31 [ ${NEWREV} = '0000000000000000000000000000000000000000' ] && NEWREV=${OLDREV}
32
33
34 # load_tags:
35 # Loads any tags into the TAGS array that were created within the changed refs.
36 load_tags() {
37 if [ "${REFNAME:0:10}" = 'refs/tags/' ]; then
38 if [ -f "${REFNAME}" ]; then
39 printf '[tag] created %s\n' "$(basename ${REFNAME})"
40 TAGS+=("$(basename ${REFNAME})")
41 elif [ ! -f "${REFNAME}" ]; then
42 printf '[tag] deleted %s\n' "$(basename ${REFNAME})"
43 else
44 printf '[tag] Something went wrong with "%s"\n' "${REFNAME}"
45 fi
46 fi
47 }
48
49
50 # load_refs:
51 # Loads the list of new refs into the REFS array
52 load_refs() {
53 REFS=($(git rev-list "^${OLDREV}" "${NEWREV}"))
54 }
55
56
57 # load_file_actions:
58 # Reads the changed refs list and loads file paths into their respective files
59 # arrays (eg: FILES_A (added), FILES_D (deleted), FILES_M (modified), and
60 # FILES_R (renamed)).
61 load_file_actions() {
62 local changes=($(git diff --name-status "^${OLDREV}" "${NEWREV}"))
63 for change in ${changes[@]}; do
64 if [ "${change:0:1}" = 'A' ]; then
65 # File is added
66 FILES_A+=(${change#* })
67 elif [ "${change:0:1}" = 'M' ]; then
68 # File is modified
69 FILES_M+=(${change#* })
70 elif [ "${change:0:1}" = 'D' ]; then
71 # File is deleted
72 FILES_D+=(${change#* })
73 elif [ "${change:0:4}" = 'R100' ]; then
74 # File is moved/renamed
75 FILES_R+=(${change#* })
76 fi
77 done
78 }
79
80
81 # dump_sess:
82 # Dumps all gathered info about the git operation to stdout so clients can see
83 # the changes that were detected. This includes dumping the files arrays, tags
84 # arrays and the ref information.
85 dump_sess() {
86 printf '\n'
87 printf 'OLD: %s\n' "${OLDREV}"
88 printf 'NEW: %s\n' "${NEWREV}"
89 printf 'NAME: %s\n\n' "${REFNAME}"
90 printf 'Files added:\n'
91 printf ' %s\n' "${FILES_A[@]}"
92 printf 'Files modified:\n'
93 printf ' %s\n' "${FILES_M[@]}"
94 printf 'Files deleted:\n'
95 printf ' %s\n' "${FILES_D[@]}"
96 printf 'Files renamed:\n'
97 printf ' %s\n' "${FILES_R[@]}"
98 printf 'Tags created:\n'
99 printf ' %s\n' "${TAGS[@]}"
100 printf '\n'
101 }
|