File: execute-on-staged-files.sh

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (25 lines) | stat: -rwxr-xr-x 665 bytes parent folder | download
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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# The yarn run command we'd like to run
command="$1"
# The file types we'd like to target, use something like '(js|vue)'
file_types="$2"

# Removing first two arguments
shift
shift

# Read all staged non-deleted files into an array
staged_files=()
while IFS= read -r line; do
    staged_files+=( "$line" )
done < <( git diff --diff-filter=d --cached --name-only | { grep -E ".$file_types$" || true; })

if [ "${#staged_files[@]}" == "0" ]; then
    echo "No staged '$file_types' files"
else
    echo "Running $command on ${#staged_files[@]} staged '$file_types' files"
    yarn run "$command" "$@" "${staged_files[@]}"
fi