-
Notifications
You must be signed in to change notification settings - Fork 0
/
eslintblame.fish
33 lines (33 loc) · 1.72 KB
/
eslintblame.fish
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
function eslintblame --description 'Parse eslint output into git-blame to unveil the offenders'
# Store eslint report from stdin, without the error/warning count
# See: https://github.com/fish-shell/fish-shell/issues/206#issuecomment-255232968
cat 1>| read -z report
# Get list of files with errors
set -l files (echo $report | grep $PWD)
for file in $files
# Escape slashes from file paths to use inside sed regular expression
set -l escaped (echo $file | sed -e 's#/#\\\/#g')
# Extract text, from file path to next empty line with sed (inclusive)
# and then remove file path and empty line with head and tail
set -l errors (echo $report | sed -n -e '/'$escaped'/,/^\s*$/p' | head -n -1 | tail -n +2)
# Print out filename
echo $file
# For each error in a given file, print the error with its author
for error in $errors
# Get error line number to be passed to git blame
set -l line (echo $error | cut -d : -f 1 | sed 's/\s//g')
# Store git blame result, using read -z to preserve new lines
# See: https://github.com/fish-shell/fish-shell/issues/159#issuecomment-240330354
git blame --porcelain -L $line,$line -- $file | read -z blame
# Extract commit hash
set -l shasum (echo $blame | head -1 | cut -d ' ' -f 1)
# Extract commit author
set -l author (echo $blame | grep 'author ' | cut -d ' ' -f2-)
# Print eslint error, using a tab to align with next line
echo -e '\t'(echo $error | sed 's/^\s*//')
# Then print commit hash and author
echo -e "\t$shasum\t$author\n"
end
echo
end
end