repowarden 0.0.0-stage → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +103 -2
  3. package/bin/code-mort +33 -0
  4. package/bin/commit +412 -0
  5. package/bin/npm-publication +151 -0
  6. package/bin/proteger +283 -0
  7. package/bin/repogarde +20 -0
  8. package/bin/repowarden +63 -0
  9. package/bin/statut +100 -0
  10. package/bin/ticket +129 -0
  11. package/bin/tickets +135 -0
  12. package/hooks/commit-msg +64 -0
  13. package/hooks/lang/actions.sh +9 -0
  14. package/hooks/lang/ansible.sh +18 -0
  15. package/hooks/lang/cpp.sh +22 -0
  16. package/hooks/lang/dart.sh +20 -0
  17. package/hooks/lang/deno.sh +15 -0
  18. package/hooks/lang/docker.sh +26 -0
  19. package/hooks/lang/dotnet.sh +15 -0
  20. package/hooks/lang/elixir.sh +13 -0
  21. package/hooks/lang/go.sh +22 -0
  22. package/hooks/lang/gradle.sh +28 -0
  23. package/hooks/lang/helm.sh +28 -0
  24. package/hooks/lang/kubernetes.sh +29 -0
  25. package/hooks/lang/maven.sh +27 -0
  26. package/hooks/lang/node.sh +75 -0
  27. package/hooks/lang/packer.sh +16 -0
  28. package/hooks/lang/php.sh +20 -0
  29. package/hooks/lang/python.sh +79 -0
  30. package/hooks/lang/ruby.sh +24 -0
  31. package/hooks/lang/rust.sh +17 -0
  32. package/hooks/lang/shell.sh +9 -0
  33. package/hooks/lang/swift.sh +20 -0
  34. package/hooks/lang/tasks.sh +16 -0
  35. package/hooks/lang/terraform.sh +29 -0
  36. package/hooks/lib/common.sh +393 -0
  37. package/hooks/lib/compat.sh +24 -0
  38. package/hooks/lib/deadcode.sh +153 -0
  39. package/hooks/lib/forge.sh +39 -0
  40. package/hooks/lib/i18n/en.sh +393 -0
  41. package/hooks/lib/i18n/fr.sh +393 -0
  42. package/hooks/lib/i18n.sh +81 -0
  43. package/hooks/lib/lang.sh +338 -0
  44. package/hooks/lib/safety.sh +108 -0
  45. package/hooks/lib/tickets.sh +184 -0
  46. package/hooks/lib/ui.sh +40 -0
  47. package/hooks/post-checkout +26 -0
  48. package/hooks/post-merge +47 -0
  49. package/hooks/pre-commit +58 -0
  50. package/hooks/pre-push +91 -0
  51. package/hooks/prepare-commit-msg +48 -0
  52. package/install.sh +245 -0
  53. package/package.json +31 -4
@@ -0,0 +1,393 @@
1
+ #!/usr/bin/env bash
2
+ # Fonctions communes à tous les hooks : désactivation par projet et chaînage
3
+ # des hooks propres au projet. Compatible bash 3.2 (macOS).
4
+ # shellcheck disable=SC2034 # variables utilisées par les hooks qui sourcent ce fichier
5
+
6
+ HOOK_NAME="$(basename "$0")"
7
+ HOOKS_DIR="$(cd "$(dirname "$0")" && pwd -P)"
8
+
9
+ # shellcheck source=ui.sh
10
+ source "$(dirname "${BASH_SOURCE[0]}")/ui.sh"
11
+ # Traduction (langue provisoire jusqu'à la lecture de la configuration)
12
+ # shellcheck source=i18n.sh
13
+ source "$(dirname "${BASH_SOURCE[0]}")/i18n.sh"
14
+
15
+ # Avertissement ; si REPOWARDEN_WARN_FILE est défini (CI), il est aussi journalisé
16
+ # pour le mode strict (outil manquant = échec)
17
+ warn() {
18
+ echo "${UI_Y}⚠${UI_N} $*" >&2
19
+ if [ -n "${REPOWARDEN_WARN_FILE:-}" ]; then echo "$*" >>"$REPOWARDEN_WARN_FILE"; fi
20
+ }
21
+ has() { command -v "$1" >/dev/null 2>&1; }
22
+
23
+ # Config repowarden.* chargée UNE fois par processus : chaque appel git coûte cher
24
+ # (20 à 50 ms sous Windows), et un hook lit la config des dizaines de fois.
25
+ # Priorité : git config (local puis global), puis .repowarden.conf versionné.
26
+ # Anciens noms (repogarde.*, .repogarde.conf) lus aussi, le nouveau l'emporte.
27
+ CFG_GIT=""
28
+ CFG_FILE=""
29
+ CFG_LOADED=""
30
+ # Racine du dépôt, calculée une fois et réutilisée par les hooks
31
+ GIT_TOPLEVEL=""
32
+
33
+ cfg_load() {
34
+ local file=""
35
+ GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null || true)"
36
+ # Anciennes clés d'abord : cfg_lookup garde la dernière valeur trouvée
37
+ CFG_GIT="$(git config --get-regexp '^(repogarde|repowarden)\.' 2>/dev/null || true)"
38
+ case "$CFG_GIT" in repogarde.* | *$'\n'repogarde.*) compat_note 'repogarde.*' ;; esac
39
+ CFG_FILE=""
40
+ if [ -n "$GIT_TOPLEVEL" ]; then
41
+ for file in "$GIT_TOPLEVEL/.repogarde.conf" "$GIT_TOPLEVEL/.repowarden.conf"; do
42
+ [ -f "$file" ] || continue
43
+ [ "${file##*/}" = .repogarde.conf ] && compat_note .repogarde.conf
44
+ CFG_FILE="$CFG_FILE${CFG_FILE:+$'\n'}$(git config -f "$file" --get-regexp '^(repogarde|repowarden)\.' 2>/dev/null || true)"
45
+ done
46
+ fi
47
+ CFG_LOADED=1
48
+ }
49
+
50
+ # REPLY = dernière valeur de repowarden.<$1> dans $2 (sortie de --get-regexp, où git
51
+ # met les noms en minuscules : comparaison insensible à la casse). Échec si absente.
52
+ cfg_lookup() {
53
+ local line found="" nocase=""
54
+ REPLY=""
55
+ shopt -q nocasematch && nocase=1
56
+ shopt -s nocasematch
57
+ while IFS= read -r line; do
58
+ if [[ "$line" == "repowarden.$1 "* || "$line" == "repogarde.$1 "* ]]; then
59
+ REPLY="${line#* }"
60
+ found=1
61
+ elif [[ "$line" == "repowarden.$1" || "$line" == "repogarde.$1" ]]; then
62
+ REPLY=true # clé sans valeur
63
+ found=1
64
+ fi
65
+ done <<<"$2"
66
+ [ -n "$nocase" ] || shopt -u nocasematch
67
+ [ -n "$found" ]
68
+ }
69
+
70
+ # REPLY = réglage repowarden.<$1>, ou $2 par défaut (sans sous-processus)
71
+ cfg_r() {
72
+ [ -n "$CFG_LOADED" ] || cfg_load
73
+ cfg_lookup "$1" "$CFG_GIT" || cfg_lookup "$1" "$CFG_FILE" || REPLY="${2-}"
74
+ return 0
75
+ }
76
+
77
+ # Lit un réglage : cfg allowedBranches "main master" (2e argument = défaut)
78
+ cfg() {
79
+ cfg_r "$@"
80
+ [ -z "$REPLY" ] || printf '%s\n' "$REPLY"
81
+ }
82
+
83
+ # Conventional Commits (partagé par commit-msg et prepare-commit-msg)
84
+ CC_TYPES="feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert"
85
+ CC_PATTERN="^($CC_TYPES)(\([a-z0-9._-]+\))?!?: .+"
86
+
87
+ # REPLY = longueur de la première ligne écrite par l'auteur : le suffixe
88
+ # « (#123) » ajouté par GitHub lors d'un merge squash n'est pas compté.
89
+ authored_length() {
90
+ local subject="$1" continuation
91
+ if [[ "$subject" =~ ^(.*)\ \(\#[0-9]+\)$ ]]; then subject="${BASH_REMATCH[1]}"; fi
92
+ # Caractères et non octets, quelle que soit la locale (Git Bash sous Windows
93
+ # est en locale C : « é » compterait double). En UTF-8, on retire du nombre
94
+ # d'octets les octets de continuation (0x80 à 0xBF).
95
+ local LC_ALL=C
96
+ continuation="${subject//[^$'\x80'-$'\xbf']/}"
97
+ REPLY=$((${#subject} - ${#continuation}))
98
+ }
99
+
100
+ # Nommage des branches : <type>/<sujet>
101
+ BRANCH_ALIASES="feature|bugfix|hotfix"
102
+ BRANCH_PATTERN="^($CC_TYPES|$BRANCH_ALIASES)/[a-z0-9._-]+(/[a-z0-9._-]+)*$"
103
+ # Branches des bots (release-please, Dependabot, Renovate) : nommées par eux
104
+ DEFAULT_ALLOWED_BRANCHES="main master develop release/* release-please--* dependabot/* renovate/*"
105
+
106
+ # Liste des types avec leur rôle (affichée dans les messages d'aide)
107
+ types_help() {
108
+ local tp desc
109
+ for tp in feat fix docs style refactor perf test build ci chore revert; do
110
+ _tr "cc.type.$tp"
111
+ desc="$_T"
112
+ case "$tp" in
113
+ feat | fix) _tr "help.alias.$tp"; desc="$(printf '%-32s %s' "$desc" "$_T")" ;;
114
+ esac
115
+ printf ' %-9s %s\n' "$tp" "$desc" >&2
116
+ done
117
+ }
118
+
119
+ # Vrai si la branche respecte la convention ou fait partie des exceptions
120
+ branch_name_valid() {
121
+ local branch="$1" allowed pat
122
+ [ -z "$branch" ] && return 0 # HEAD détachée
123
+ cfg_r allowedBranches "$DEFAULT_ALLOWED_BRANCHES"
124
+ allowed="$REPLY"
125
+ set -f # pas d'expansion de "release/*" sur le disque
126
+ for pat in $allowed; do
127
+ # shellcheck disable=SC2254
128
+ case "$branch" in $pat) set +f; return 0 ;; esac
129
+ done
130
+ set +f
131
+ [[ "$branch" =~ $BRANCH_PATTERN ]]
132
+ }
133
+
134
+ # Flux avec branche d'intégration (réglage integrationBranch, ex. develop) :
135
+ # REPLY = branches cibles autorisées pour une PR depuis la branche $1, vide si
136
+ # aucun flux n'est configuré (PR libres, en pratique vers main).
137
+ # travail (feat/…, fix/…, bots) → intégration
138
+ # intégration, PR de release-please → principale (mainBranch, défaut main)
139
+ # release/…, hotfix/… → principale ou intégration
140
+ pr_targets_r() {
141
+ local integration
142
+ cfg_r integrationBranch ""
143
+ integration="$REPLY"
144
+ [ -n "$integration" ] || return 0
145
+ cfg_r mainBranch main
146
+ # Seule la branche d'intégration entre dans la branche principale : tout
147
+ # le reste (correctifs urgents compris) passe par elle et ses préversions
148
+ case "$1" in
149
+ "$integration" | release-please--*) ;;
150
+ *) REPLY="$integration" ;;
151
+ esac
152
+ }
153
+
154
+ # Nature d'une PR partant d'une branche persistante (flux integrationBranch),
155
+ # source $1, cible $2 : REPLY = livraison (intégration → principale), retour
156
+ # (principale → intégration), inversee (persistante vers autre chose : sans
157
+ # objet), ou vide (PR de travail, ou hors PR)
158
+ persistent_pr_r() {
159
+ local integration main
160
+ cfg_r integrationBranch ""
161
+ integration="$REPLY"
162
+ cfg_r mainBranch main
163
+ main="$REPLY"
164
+ REPLY=""
165
+ [ -n "$integration" ] && [ -n "$2" ] || return 0
166
+ case "$1" in
167
+ "$integration") if [ "$2" = "$main" ]; then REPLY=livraison; else REPLY=inversee; fi ;;
168
+ "$main") if [ "$2" = "$integration" ]; then REPLY=retour; else REPLY=inversee; fi ;;
169
+ esac
170
+ }
171
+
172
+ # Vrai si $1 est un en-tête conforme (format et 72 caractères)
173
+ header_valid() {
174
+ [[ "$1" =~ $CC_PATTERN ]] || return 1
175
+ authored_length "$1"
176
+ [ "$REPLY" -le 72 ]
177
+ }
178
+
179
+ # REPLY = titre de PR conforme déduit de la branche $1 :
180
+ # feat/ajout-panier → « feat: ajout panier », hotfix/crash → « fix: crash »,
181
+ # release/1.2.0 → « chore(release): 1.2.0 », develop → livraison sur main
182
+ suggest_pr_title_r() {
183
+ local branch="$1" type rest
184
+ cfg_r integrationBranch ""
185
+ if [ -n "$REPLY" ] && [ "$branch" = "$REPLY" ]; then
186
+ local integration="$REPLY"
187
+ cfg_r mainBranch main
188
+ REPLY="chore(release): livrer $integration sur $REPLY"
189
+ return 0
190
+ fi
191
+ type="${branch%%/*}"
192
+ rest="${branch#*/}"
193
+ [ "$rest" != "$branch" ] || { type=chore; rest="$branch"; }
194
+ case "$type" in
195
+ feature) type=feat ;;
196
+ bugfix | hotfix) type=fix ;;
197
+ release) REPLY="chore(release): $rest"; return 0 ;;
198
+ esac
199
+ [[ "$type" =~ ^($CC_TYPES)$ ]] || type=chore
200
+ rest="${rest//[\/_-]/ }"
201
+ REPLY="$type: $rest"
202
+ # 72 caractères au plus (coupe sur un mot)
203
+ authored_length "$REPLY"
204
+ while [ "$REPLY" -gt 72 ]; do
205
+ if [[ "$rest" == *" "* ]]; then rest="${rest% *}"; else rest="${rest%?}"; fi
206
+ authored_length "$type: $rest"
207
+ done
208
+ REPLY="$type: $rest"
209
+ }
210
+
211
+ # Propose un nom valide à partir d'un nom invalide (Feat/Mon Truc → feat/mon-truc)
212
+ suggest_branch_name() {
213
+ local b type rest
214
+ b="$(echo "$1" | tr '[:upper:] _' '[:lower:]--' | tr -cd 'a-z0-9._/-' |
215
+ sed -e 's#//*#/#g' -e 's#^[/.-]*##' -e 's#[/.-]*$##')"
216
+ if [[ "$b" != */* ]]; then
217
+ echo "feat/${b:-ma-feature}"
218
+ return
219
+ fi
220
+ type="${b%%/*}"
221
+ rest="${b#*/}"
222
+ case "$type" in
223
+ bug | bugs) type=fix ;;
224
+ doc) type=docs ;;
225
+ features) type=feat ;;
226
+ tests) type="test" ;;
227
+ refacto) type=refactor ;;
228
+ esac
229
+ [[ "$type" =~ ^($CC_TYPES|$BRANCH_ALIASES)$ ]] || type=feat
230
+ echo "$type/$rest"
231
+ }
232
+
233
+ # Message d'aide complet pour une branche invalide
234
+ # Aide courte (création de la branche, non bloquant) : le problème, la commande
235
+ # pour renommer, et où trouver le détail. L'aide complète vient au commit refusé.
236
+ branch_hint() {
237
+ local branch="$1" suggested
238
+ suggested="$(suggest_branch_name "$branch")"
239
+ attention_t hook.branch_hint.1 "$branch" >&2
240
+ printf ' %s%s%s\n' "$UI_B" "git branch -m $suggested" "$UI_N" >&2
241
+ dim_t hook.branch_hint.2 >&2
242
+ }
243
+
244
+ # Aide complète (commit ou push refusé) : la commande pour renommer d'abord,
245
+ # puis le format attendu, les types et les exceptions
246
+ branch_help() {
247
+ local branch="$1" suggested allowed
248
+ suggested="$(suggest_branch_name "$branch")"
249
+ allowed="$(cfg allowedBranches "$DEFAULT_ALLOWED_BRANCHES")"
250
+ if [ "${REPOWARDEN_LANG:-}" = en ]; then
251
+ cat >&2 <<EOF
252
+
253
+ Invalid branch name: "$branch"
254
+
255
+ 👉 Rename the current branch:
256
+ ${UI_B}git branch -m $suggested${UI_N}
257
+ Already pushed? Rename the remote one too:
258
+ git push origin -u $suggested && git push origin --delete $branch
259
+
260
+ Expected format: <type>/<topic>
261
+ topic: lowercase letters, digits, . _ - (and / to split further)
262
+ e.g.: feat/signup, fix/user/login, hotfix/db-timeout
263
+
264
+ Types:
265
+ EOF
266
+ types_help
267
+ cat >&2 <<EOF
268
+
269
+ Allowed exceptions: $allowed
270
+ (change: git config repowarden.allowedBranches "main develop release/*")
271
+ Disable for this repository: git config repowarden.skip branch-name
272
+ EOF
273
+ else
274
+ cat >&2 <<EOF
275
+
276
+ Nom de branche invalide : "$branch"
277
+
278
+ 👉 Renommer la branche courante :
279
+ ${UI_B}git branch -m $suggested${UI_N}
280
+ Déjà poussée ? Renomme aussi le distant :
281
+ git push origin -u $suggested && git push origin --delete $branch
282
+
283
+ Format attendu : <type>/<sujet>
284
+ sujet : minuscules, chiffres, . _ - (et / pour sous-découper)
285
+ ex. : feat/inscription, fix/user/login, hotfix/timeout-db
286
+
287
+ Types :
288
+ EOF
289
+ types_help
290
+ cat >&2 <<EOF
291
+
292
+ Exceptions autorisées : $allowed
293
+ (modifier : git config repowarden.allowedBranches "main develop release/*")
294
+ Désactiver pour ce dépôt : git config repowarden.skip branch-name
295
+ EOF
296
+ fi
297
+ }
298
+
299
+ # Vrai si l'élément est désactivé (repowarden.skip, git config ou .repowarden.conf).
300
+ # Valeurs : true|all, ou liste séparée par espaces/virgules parmi
301
+ # pre-commit prepare-commit-msg commit-msg pre-push post-checkout
302
+ # branch-name protect-branch secrets format tests, ou un langage (node, python…)
303
+ skipped() {
304
+ local v
305
+ cfg_r skip
306
+ v="$REPLY"
307
+ [ "$v" = true ] || [ "$v" = all ] && return 0
308
+ case " ${v//,/ } " in *" $1 "*) return 0 ;; esac
309
+ return 1
310
+ }
311
+
312
+ # Le hook entier est-il désactivé ? (à appeler en tête de hook)
313
+ exit_if_skipped() {
314
+ if skipped "$HOOK_NAME"; then
315
+ info_t hook.common.3 "$HOOK_NAME"
316
+ exit 0
317
+ fi
318
+ }
319
+
320
+ # Version minimale de repowarden demandée par le projet (repowarden.version,
321
+ # ex. 3.1.4 ou 3) : le poste prévenu s'il est en retard, avec la commande de
322
+ # mise à jour. Avertissement seulement : la CI, à sa propre version, fait foi.
323
+ check_version() {
324
+ local wanted installed root w i n
325
+ cfg_r version ""
326
+ wanted="${REPLY#v}"
327
+ [ -n "$wanted" ] || return 0
328
+ root="${HOOKS_DIR%/hooks}"
329
+ repowarden_version_r "$root"
330
+ installed="$REPLY"
331
+ [ -n "$installed" ] || return 0
332
+ local IFS=.
333
+ # shellcheck disable=SC2206 # découpage voulu sur les points
334
+ w=($wanted) i=($installed)
335
+ for n in 0 1 2; do
336
+ [ "${i[$n]:-0}" -gt "${w[$n]:-0}" ] 2>/dev/null && return 0
337
+ [ "${i[$n]:-0}" -lt "${w[$n]:-0}" ] 2>/dev/null && break
338
+ [ "$n" = 2 ] && return 0
339
+ done
340
+ unset IFS
341
+ if [[ "$root" == */node_modules/* ]]; then REPLY="npm update -g repowarden"
342
+ elif [ -d "$root/.git" ]; then REPLY="git -C $root pull"
343
+ else REPLY="$root"
344
+ fi
345
+ attention_t hook.version_old "$wanted" "$installed" "$REPLY" >&2
346
+ }
347
+
348
+ # Exécute les hooks propres au projet (.repowarden/<hook> ou .git/hooks/<hook>),
349
+ # ignorés par git dès que core.hooksPath pointe ici.
350
+ run_local_hook() {
351
+ local candidate real
352
+ for candidate in ".repowarden/$HOOK_NAME" ".repogarde/$HOOK_NAME" "$(git rev-parse --git-common-dir)/hooks/$HOOK_NAME"; do
353
+ [ -x "$candidate" ] || continue
354
+ # Ancien dossier (.repogarde/) : lancé seulement sans le nouveau
355
+ if [ "$candidate" = ".repogarde/$HOOK_NAME" ]; then
356
+ [ -x ".repowarden/$HOOK_NAME" ] && continue
357
+ compat_note .repogarde/
358
+ fi
359
+ real="$(cd "$(dirname "$candidate")" && pwd -P)"
360
+ [ "$real" = "$HOOKS_DIR" ] && continue
361
+ # Hook installé par lefthook (« lefthook install ») : lefthook n'est plus
362
+ # pris en charge depuis repowarden 4. Ses commandes se déclarent dans
363
+ # .repowarden/<hook>, lancé juste avant.
364
+ if grep -qs lefthook "$candidate"; then
365
+ attention_t hook.common.lefthook_ignored "$candidate" "$HOOK_NAME"
366
+ continue
367
+ fi
368
+ info_t hook.common.5 "$candidate"
369
+ compat_warn
370
+ "$candidate" "$@" || return $?
371
+ done
372
+ compat_warn
373
+ }
374
+
375
+ # Chargement immédiat dans le processus qui source ce fichier : un appel $(cfg …)
376
+ # s'exécute dans un sous-shell et ne pourrait pas remplir le cache du parent.
377
+ cfg_load
378
+
379
+ # Langue confirmée et plateforme, une fois la configuration lue
380
+ i18n_init
381
+
382
+ # Anciens noms rencontrés (REPOGARDE_*, repogarde.*, .repogarde.conf) : un
383
+ # avertissement par processus, avec les nouveaux noms
384
+ compat_warn() {
385
+ [ -n "$REPOWARDEN_OLD_NAMES" ] || return 0
386
+ # Un seul avertissement par commit ou push : pas dans les hooks qui suivent
387
+ case "$HOOK_NAME" in prepare-commit-msg | commit-msg | post-checkout | post-merge) return 0 ;; esac
388
+ attention_t compat.old_names "$REPOWARDEN_OLD_NAMES"
389
+ REPOWARDEN_OLD_NAMES=""
390
+ }
391
+ compat_warn
392
+ # shellcheck source=forge.sh
393
+ source "$(dirname "${BASH_SOURCE[0]}")/forge.sh"
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env bash
2
+ # Anciens noms de repogarde (avant repowarden 4), encore lus pendant la v4 et
3
+ # retirés en v5. Chargé en premier (par ui.sh), sans sous-processus.
4
+ # variables REPOGARDE_* → REPOWARDEN_* (la nouvelle l'emporte si les deux
5
+ # sont définies) ; les clés repogarde.* et .repogarde.conf sont lues par
6
+ # cfg_load (common.sh) ; .repogarde/<hook> par run_local_hook.
7
+ # Les noms rencontrés sont notés dans REPOWARDEN_OLD_NAMES, pour un seul
8
+ # avertissement par processus (compat_warn, une fois la langue connue).
9
+ # shellcheck disable=SC2034 # REPOWARDEN_OLD_NAMES lu par common.sh
10
+
11
+ REPOWARDEN_OLD_NAMES="${REPOWARDEN_OLD_NAMES:-}"
12
+
13
+ compat_note() {
14
+ case " $REPOWARDEN_OLD_NAMES " in *" $1 "*) ;; *) REPOWARDEN_OLD_NAMES="${REPOWARDEN_OLD_NAMES:+$REPOWARDEN_OLD_NAMES }$1" ;; esac
15
+ }
16
+
17
+ for _ancienne in ${!REPOGARDE_*}; do
18
+ _nouvelle="REPOWARDEN_${_ancienne#REPOGARDE_}"
19
+ if [ -z "${!_nouvelle+x}" ]; then
20
+ export "$_nouvelle=${!_ancienne}"
21
+ fi
22
+ compat_note "$_ancienne"
23
+ done
24
+ unset _ancienne _nouvelle
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env bash
2
+ # Détection de code mort : seul le NOUVEAU code mort compte (lignes ajoutées ou
3
+ # modifiées depuis la base), pour que la dette ne grossisse plus sans noyer un
4
+ # projet existant sous son historique.
5
+ #
6
+ # Chaque plugin de langage peut fournir <plugin>_deadcode, lancé à la racine du
7
+ # projet, qui écrit une ligne par élément :
8
+ # niveau<TAB>fichier<TAB>ligne<TAB>message
9
+ # niveau : prouve (inaccessible, inutilisé dans sa portée : sûr à 100 %)
10
+ # candidat (non référencé dans le projet : appel dynamique possible)
11
+ # ligne : 0 si l'élément concerne tout le fichier (fichier, dépendance)
12
+ #
13
+ # Réglages : deadcode = block (défaut : le prouvé bloque, les candidats avertissent)
14
+ # | warn (rien ne bloque) | strict (tout bloque)
15
+ # deadcodeIgnore = chemins ignorés (motifs, comme exclude)
16
+
17
+ # Code mort introduit depuis $1 (base) dans les fichiers lus sur stdin.
18
+ # Retour : 1 si un élément bloque selon le réglage deadcode.
19
+ deadcode_projects() {
20
+ local base="$1" changed added found dir p plugins niveau f l msg path proven=0 cand=0 mode ignore pat code var
21
+ local ran="" file
22
+ changed="$(mktemp)"
23
+ added="$(mktemp)"
24
+ found="$(mktemp)"
25
+ cat >"$changed"
26
+
27
+ # Lignes ajoutées ou modifiées (fichier:ligne), du commit de base à
28
+ # l'arbre de travail (en CI, identique au commit vérifié)
29
+ git diff -U0 --no-color --no-ext-diff "$base" -- 2>/dev/null | awk '
30
+ /^\+\+\+ / { f = substr($0, 5); sub(/^b\//, "", f); next }
31
+ /^@@/ {
32
+ split($3, a, ","); s = substr(a[1], 2); n = (a[2] == "" ? 1 : a[2])
33
+ for (i = 0; i < n; i++) print f ":" (s + i)
34
+ }' >"$added"
35
+ # Fichiers pas encore suivis (analyse locale) : toutes leurs lignes sont nouvelles
36
+ { git ls-files --others --exclude-standard 2>/dev/null | grep -xFf "$changed" || true; } |
37
+ while IFS= read -r f; do
38
+ awk -v f="$f" '{ print f ":" NR }' "$f"
39
+ done >>"$added"
40
+
41
+ cfg_r deadcodeIgnore ""
42
+ ignore="$REPLY"
43
+ # Projets touchés (fichier marqueur : pom.xml, package.json…), puis
44
+ # fichiers isolés d'un langage autonome (script Python sans projet…),
45
+ # analysés depuis la racine
46
+ while IFS= read -r dir; do
47
+ plugins=""
48
+ active_plugins_init
49
+ if [ "${dir#\*}" != "$dir" ]; then
50
+ plugins="${dir#\*}"
51
+ dir=.
52
+ # Fichiers isolés (hors projet) : analyse seulement si du code de ce
53
+ # langage a changé ; des .md ou .yml rattachés à node pour le
54
+ # formatage ne justifient pas une analyse JavaScript
55
+ plugin_get "$plugins" EXT
56
+ code="$REPLY"
57
+ var="$(printf '%s' "$plugins" | tr '[:lower:]' '[:upper:]')_CODE_EXT"
58
+ [ -z "${!var:-}" ] || code="${!var}"
59
+ grep -qE "$code" "$changed" || continue
60
+ else
61
+ for p in $ACTIVE_PLUGINS; do
62
+ has_marker "$dir" "$p" && declare -F "${p}_deadcode" >/dev/null && plugins="$plugins $p"
63
+ done
64
+ fi
65
+ for p in $plugins; do
66
+ [[ " $ran " == *" $dir:$p "* ]] && continue
67
+ ran="$ran $dir:$p"
68
+ # Un outil d'analyse renvoie souvent un code d'erreur quand il trouve
69
+ # quelque chose : seule sa sortie compte
70
+ { (cd "$dir" && "${p}_deadcode") </dev/null || true; } |
71
+ while IFS=$'\t' read -r niveau f l msg; do
72
+ [ -n "$f" ] || continue
73
+ f="${f#"$PWD/$dir/"}"
74
+ f="${f#./}"
75
+ if [ "$dir" = . ]; then path="$f"; else path="$dir/$f"; fi
76
+ if [ "${l:-0}" = 0 ]; then
77
+ grep -qxF "$path" "$changed" || continue
78
+ else
79
+ grep -qxF "$path:$l" "$added" || continue
80
+ fi
81
+ set -f
82
+ for pat in $ignore; do
83
+ # shellcheck disable=SC2254
84
+ case "$path" in $pat) set +f; continue 2 ;; esac
85
+ done
86
+ set +f
87
+ printf '%s\t%s\t%s\n' "$niveau" "$path${l:+:$l}" "$msg"
88
+ done >>"$found"
89
+ done
90
+ done < <(
91
+ projects_for_files <"$changed" || true # aucun projet : pas une erreur
92
+ while IFS= read -r file; do
93
+ plugins_for_file_r "$file"
94
+ for p in $REPLY; do
95
+ plugin_has_flag "$p" standalone && declare -F "${p}_deadcode" >/dev/null && echo "*$p"
96
+ done
97
+ done <"$changed" | sort -u
98
+ )
99
+
100
+ while IFS=$'\t' read -r niveau path msg; do
101
+ path="${path%:0}"
102
+ if [ "$niveau" = prouve ]; then
103
+ proven=$((proven + 1))
104
+ if declare -F ci_error_t >/dev/null; then ci_error_t dc.proven "$path" "$msg"
105
+ else err_t dc.proven "$path" "$msg"
106
+ fi
107
+ else
108
+ cand=$((cand + 1))
109
+ attention_t dc.candidate "$path" "$msg"
110
+ fi
111
+ done <"$found"
112
+ rm -f "$changed" "$added" "$found"
113
+
114
+ if [ -z "$ran" ]; then
115
+ info_t dc.nothing
116
+ return 0
117
+ fi
118
+ if [ "$proven" = 0 ] && [ "$cand" = 0 ]; then
119
+ ok_t dc.none
120
+ return 0
121
+ fi
122
+ [ "$cand" = 0 ] || info_t dc.summary_candidates "$cand"
123
+ cfg_r deadcode block
124
+ mode="$REPLY"
125
+ case "$mode" in
126
+ warn) return 0 ;;
127
+ strict)
128
+ err_t dc.summary_strict "$((proven + cand))"
129
+ return 1
130
+ ;;
131
+ *)
132
+ [ "$proven" = 0 ] && return 0
133
+ err_t dc.summary_proven "$proven"
134
+ return 1
135
+ ;;
136
+ esac
137
+ }
138
+
139
+ # Outil d'analyse absent : signalé, sans faire échouer le mode strict
140
+ # (la détection de code mort reste une aide, pas un prérequis du projet)
141
+ deadcode_tool_missing() { attention_t dc.tool_missing "$1" "$2"; }
142
+
143
+ # Java (Maven, Gradle) : règles PMD dont le résultat est prouvé — import,
144
+ # champ, méthode ou variable privés jamais utilisés dans leur portée
145
+ JAVA_DEADCODE_RULES="category/java/bestpractices.xml/UnusedPrivateMethod,category/java/bestpractices.xml/UnusedPrivateField,category/java/bestpractices.xml/UnusedLocalVariable,category/java/bestpractices.xml/UnusedFormalParameter,category/java/codestyle.xml/UnnecessaryImport"
146
+ java_deadcode() {
147
+ has pmd || { deadcode_tool_missing Java pmd; return 0; }
148
+ [ -d src ] || return 0
149
+ # « src/a/A.java:3:<TAB>Règle:<TAB>message »
150
+ # code de sortie 4 = violations trouvées : attendu
151
+ { pmd check --no-progress --no-cache -d src -f text -R "$JAVA_DEADCODE_RULES" 2>/dev/null || true; } |
152
+ awk -F'\t' 'NF >= 3 { n = split($1, a, ":"); print "prouve\t" a[1] "\t" a[2] "\t" $3 }'
153
+ }
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env bash
2
+ # Plateforme de gestion de version : github, gitlab, bitbucket, gitea (Gitea,
3
+ # Forgejo, Codeberg) ou other. Réglage forge (git config ou .repowarden.conf),
4
+ # sinon variables de la CI, sinon adresse du remote origin (domaine interne
5
+ # reconnu s'il contient gitlab, gitea…). Résultat mémorisé : FORGE.
6
+ # Vocabulaire : FORGE_PR (PR / MR), FORGE_REFS (exemple de références).
7
+ # shellcheck disable=SC2034 # variables utilisées par les scripts qui sourcent ce fichier
8
+
9
+ forge_init() {
10
+ local forge="" url
11
+ if declare -F cfg_r >/dev/null; then
12
+ cfg_r forge ""
13
+ forge="$REPLY"
14
+ fi
15
+ if [ -z "$forge" ]; then
16
+ if [ -n "${GITHUB_ACTIONS:-}" ]; then forge=github
17
+ elif [ -n "${GITLAB_CI:-}" ]; then forge=gitlab
18
+ elif [ -n "${BITBUCKET_BUILD_NUMBER:-}" ]; then forge=bitbucket
19
+ elif [ -n "${GITEA_ACTIONS:-}${FORGEJO_ACTIONS:-}" ]; then forge=gitea
20
+ else
21
+ url="$(git remote get-url origin 2>/dev/null || true)"
22
+ case "$url" in
23
+ *github*) forge=github ;;
24
+ *gitlab*) forge=gitlab ;;
25
+ *bitbucket*) forge=bitbucket ;;
26
+ *gitea* | *forgejo* | *codeberg*) forge=gitea ;;
27
+ *) forge=other ;;
28
+ esac
29
+ fi
30
+ fi
31
+ FORGE="$forge"
32
+ case "$FORGE" in
33
+ gitlab) FORGE_PR=MR FORGE_REFS="Closes #12, !34" ;;
34
+ bitbucket) FORGE_PR=PR FORGE_REFS="PROJ-42" ;;
35
+ *) FORGE_PR=PR FORGE_REFS="Closes #12" ;;
36
+ esac
37
+ }
38
+
39
+ forge_init