Debian Codegraph @

.github/scripts/detect-new-plugins.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 set -euo pipefail
4
5 base_sha=${1:?base sha required}
6 head_sha=${2:?head sha required}
7
8 mode=full
9 reason="changed files require the full suite"
10 declare -a plugin_dirs=()
11 declare -a targets=()
12 declare -a plugin_paths=()
13
14 fatal() {
15 printf 'error: %s\n' "$1" >&2
16 exit 1
17 }
18
19 is_plugin_tree() {
20 case "$1" in
21 src/filter/*|src/mixer2/*|src/mixer3/*) return 0 ;;
22 *) return 1 ;;
23 esac
24 }
25
26 plugin_dir_from_path() {
27 local path=$1
28 IFS=/ read -r top category name _ <<< "$path"
29 if [[ "$top" == "src" && -n "${category:-}" && -n "${name:-}" ]]; then
30 printf '%s/%s/%s\n' "$top" "$category" "$name"
31 return 0
32 fi
33 return 1
34 }
35
36 contains_dir() {
37 local needle=$1
38 shift || true
39 local item
40 for item in "$@"; do
41 if [[ "$item" == "$needle" ]]; then
42 return 0
43 fi
44 done
45 return 1
46 }
47
48 dir_exists_in_commit() {
49 local commit=$1
50 local path=$2
51 git cat-file -e "${commit}:${path}" 2>/dev/null
52 }
53
54 parent_cmake_registers_plugin() {
55 local commit=$1
56 local plugin_dir=$2
57 local category=${plugin_dir#src/}
58 category=${category%%/*}
59 local plugin_name=${plugin_dir##*/}
60 local parent_cmake="src/${category}/CMakeLists.txt"
61
62 local parent_text
63 parent_text=$(git show "${commit}:${parent_cmake}" 2>/dev/null) || {
64 return 1
65 }
66
67 printf '%s\n' "$parent_text" | grep -Eq "^[[:space:]]*add_subdirectory[[:space:]]*\\([[:space:]]*${plugin_name}[[:space:]]*\\)"
68 }
69
70 extract_target_name() {
71 local commit=$1
72 local plugin_dir=$2
73 local plugin_name=${plugin_dir##*/}
74 local cmake_file="${plugin_dir}/CMakeLists.txt"
75
76 local cmake_text
77 cmake_text=$(git show "${commit}:${cmake_file}" 2>/dev/null) || {
78 return 1
79 }
80
81 local target
82 target=$(printf '%s\n' "$cmake_text" | sed -nE 's/^[[:space:]]*set[[:space:]]*\([[:space:]]*TARGET[[:space:]]+([^[:space:])]+).*/\1/p' | head -n1)
83 if [[ -n "$target" ]]; then
84 printf '%s\n' "$target"
85 return 0
86 fi
87
88 target=$(printf '%s\n' "$cmake_text" | sed -nE 's/^[[:space:]]*add_library[[:space:]]*\(([[:alnum:]_.+-]+)[[:space:]]+MODULE.*/\1/p' | head -n1)
89 if [[ -n "$target" ]]; then
90 printf '%s\n' "$target"
91 return 0
92 fi
93
94 printf '%s\n' "$plugin_name"
95 }
96
97 while IFS=$'\t' read -r status path new_path; do
98 [[ -n "${status:-}" ]] || continue
99
100 if [[ "$status" == A* ]] && is_plugin_tree "$path"; then
101 plugin_dir=$(plugin_dir_from_path "$path") || true
102 if [[ -n "${plugin_dir:-}" ]] && dir_exists_in_commit "$base_sha" "$plugin_dir"; then
103 reason="PR adds files inside existing plugin directory: $plugin_dir"
104 plugin_dirs=()
105 break
106 fi
107
108 if [[ -n "${plugin_dir:-}" ]] && ! contains_dir "$plugin_dir" "${plugin_dirs[@]}"; then
109 plugin_dirs+=("$plugin_dir")
110 fi
111 continue
112 fi
113
114 case "$path" in
115 src/filter/CMakeLists.txt|src/mixer2/CMakeLists.txt|src/mixer3/CMakeLists.txt)
116 continue
117 ;;
118 *)
119 reason="PR touches non-new-plugin files: $path"
120 plugin_dirs=()
121 break
122 ;;
123 esac
124 done < <(git diff --name-status --find-renames "$base_sha" "$head_sha")
125
126 if [[ ${#plugin_dirs[@]} -gt 0 ]]; then
127 mode=targeted
128 reason="PR only adds new plugin directories"
129
130 for plugin_dir in "${plugin_dirs[@]}"; do
131 if ! parent_cmake_registers_plugin "$head_sha" "$plugin_dir"; then
132 fatal "parent CMakeLists.txt does not register ${plugin_dir}. Add add_subdirectory(${plugin_dir##*/}) to the category CMakeLists.txt."
133 fi
134
135 target=$(extract_target_name "$head_sha" "$plugin_dir") || {
136 mode=full
137 reason="could not resolve target for $plugin_dir"
138 targets=()
139 plugin_paths=()
140 break
141 }
142
143 plugin_paths+=("build/${plugin_dir}/${target}.so")
144 targets+=("$target")
145 done
146 fi
147
148 printf 'mode=%s\n' "$mode"
149 printf 'reason=%s\n' "$reason"
150
151 if [[ "$mode" == "targeted" ]]; then
152 {
153 printf 'targets<<EOF\n'
154 printf '%s\n' "${targets[*]}"
155 printf 'EOF\n'
156 printf 'plugin_paths<<EOF\n'
157 printf '%s\n' "${plugin_paths[*]}"
158 printf 'EOF\n'
159 printf 'plugin_dirs<<EOF\n'
160 printf '%s\n' "${plugin_dirs[*]}"
161 printf 'EOF\n'
162 }
163 else
164 {
165 printf 'targets=\n'
166 printf 'plugin_paths=\n'
167 printf 'plugin_dirs=\n'
168 }
169 fi