-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt
executable file
·113 lines (102 loc) · 2.65 KB
/
fmt
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash
# Helper script for formatting source code in this repo.
set -euf
is_alias="${GIT_PREFIX+y}"
pwd="${GIT_PREFIX:-$(pwd)}"
haskell_fmt_cmd=("fourmolu" "--mode" "inplace" "-q")
haskell_files=()
git_since=""
untracked_changes=()
function usage () {
echo
if [ "$is_alias" == "y" ] ; then
echo "Usage: git fmt [-h|--help] [--since <commit|branch|HEAD>] [<files..>]"
else
echo "Usage: $0 [-h|--help] [--since <commit|branch|HEAD>] [<files..>]"
fi
echo
echo "Runs formatter on specified files."
echo
echo "If no arguments are given, --since HEAD is assumed, which just formats"
echo "files modified since the most recent commit."
echo
echo "Modified files whose changes aren't tracked will not be formatted,"
echo "unless explicitly specified. 'git add' your files before running this."
echo
echo "Arguments:"
echo
echo " -h. --help Show this help menu."
echo " --since <commit> Format all files modified since <commit>. Can"
echo " also be a branch name or HEAD."
echo
echo "But if you just really want to just format all files in this repo:"
echo
echo " fourmolu -i ."
}
cd "$(git rev-parse --show-toplevel)"
if [ $# -gt 0 ]; then
while [ $# -gt 0 ] ; do
case "$1" in
"-h" | "--help" )
usage
exit 0
;;
"--since")
shift
git_since="$1"
:
;;
*".hs")
haskell_files+=("$pwd/$1")
;;
-* )
echo "Warn: unknown argument: $1. Ignoring."
;;
*)
echo "Warn: unknown file type: $1. Refusing to format."
;;
esac
shift
done
else
git_since="HEAD"
fi
if [ -n "$git_since" ] ; then
while read -r file ; do
case "$file" in
*".hs")
if ! git status --porcelain "$file" | awk '/^\?\?|^.M/ { exit 1 }' ; then
echo "Warn: '$file' has untracked changes, so it will not be formatted."
untracked_changes+=("$file")
:
else
haskell_files+=("$file")
fi
;;
esac
done < <(git diff "$git_since" --name-only)
fi
if [ "${#untracked_changes[@]}" -gt 0 ] ; then
echo
echo "To format files with untracked changes, git add them:"
echo
echo " git add " "${untracked_changes[@]}"
fi
if [ "${#haskell_files[@]}" -eq 0 ] ; then
echo
echo "Nothing to format."
fi
if ! which fourmolu >/dev/null 2>/dev/null ; then
echo
echo "Cannot find fourmolu on PATH."
echo
echo "Please install with:"
echo
echo " stack install fourmolu"
exit 1
fi
for file in "${haskell_files[@]}" ; do
echo -n "Formatting $file ... "
"${haskell_fmt_cmd[@]}" "$file"
echo "Formatted."
done