File: pre-commit

package info (click to toggle)
3270font 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,424 kB
  • sloc: python: 184; makefile: 140; sh: 81; xml: 21
file content (58 lines) | stat: -rwxr-xr-x 1,650 bytes parent folder | download | duplicates (3)
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
#!/bin/bash

set -euo pipefail

function lint_added_file () {
    # This is called for every file added in the current branch. This
    # is the place we should feel more free to validate files more
    # rigorously so that nothing we don't like can enter the codebase.

    # Get the type of the file and choose what to do.
    type=$(file --mime-type -b "$1")
    case $type in
        "text/x-shellscript")
            shellcheck "$1"
            ;;
        "text/x-python")
            isort -faas "$1"
            black -l 79 "$1"
            ;;
        *)
            echo "File $1 was not processed"
    esac
}

function lint_modified_file () {
    # This is called for every file modified in the current branch. We
    # may want to tone validations down a notch here in relation to
    # what we do with new files if we fear disturbing existing code or
    # forcing rewrites of code that's unrelated to the code being
    # changed.

    # Get the type of the file and choose what to do.
    type=$(file --mime-type -b "$1")
    case $type in
        "text/x-shellscript")
            shellcheck "$1"
            ;;
        "text/x-python")
            isort -faas "$1"
            black -l 79 "$1"
            ;;
        *)
            echo "File $1 was not processed"
    esac
}

# Iterate over the files added on this branch
# Sadly, it's not trivial to figure out what branch we branched from.
for file in $(git diff --name-only --diff-filter=A develop)
do
    lint_added_file "$file"
done

# Iterate over the files modified on this commit
for file in $(git diff --name-only --diff-filter=M HEAD)
do
    lint_modified_file "$file"
done