diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/extract-elm.sh | 44 | ||||
-rwxr-xr-x | src/extract-nix.sh | 44 | ||||
-rwxr-xr-x | src/extract.sh | 58 |
3 files changed, 146 insertions, 0 deletions
diff --git a/src/extract-elm.sh b/src/extract-elm.sh new file mode 100755 index 0000000..375d8a8 --- /dev/null +++ b/src/extract-elm.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +set -efu + +input_file=${1-/dev/stdin} + +tree-grepper \ + --query elm '([(line_comment) (block_comment)]+)' \ + --format json $input_file | + jq 'sort_by(.file)' | + jq '.[]' --indent 0 | + while read -r line; do + file=$(echo "$line" | jq -r .file) + file_type=$(echo "$line" | jq -r .file_type) + items=$(echo "$line" | + jq '.matches[] | { file: $file, file_type: $file_type, match: . }' --arg file "$file" --arg file_type "$file_type" | + jq '. | select(.match.text | test("TODO .+"))' + ) + if test -z "$items"; then + continue + fi + + echo "$items" | jq --slurp '.[]' --indent 0 | + while read -r item; do + start_row=$(echo "$item" | jq '.match.start.row') + end_row=$(echo "$item" | jq '.match.end.row') + + last_commit=$(git --no-pager blame -L "$start_row,$start_row" "$file" -p | + head -n 1 | + cut -d ' ' -f 1 + ) + + text=$(echo "$item" | + jq .match.text -r | + sed 's/^-- *TODO *//' | + sed 's/^{-|\? *TODO *//' | + sed 's/ *-}$//') + + echo "$item" | jq '. + {"text": $text, "last_commit": $last_commit}' \ + --arg text "$text" \ + --arg last_commit "$last_commit" \ + -c + done + done diff --git a/src/extract-nix.sh b/src/extract-nix.sh new file mode 100755 index 0000000..e092db2 --- /dev/null +++ b/src/extract-nix.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +set -efu + +input_file=${1-/dev/stdin} + +tree-grepper \ + --query nix '((comment)+)' \ + --format json $input_file | + jq 'sort_by(.file)' | + jq '.[]' --indent 0 | + while read -r line; do + file=$(echo "$line" | jq -r .file) + file_type=$(echo "$line" | jq -r .file_type) + items=$(echo "$line" | + jq '.matches[] | { file: $file, file_type: $file_type, match: . }' --arg file "$file" --arg file_type "$file_type" | + jq '. | select(.match.text | test("TODO .+"))' + ) + if test -z "$items"; then + continue + fi + + echo "$items" | jq --slurp '.[]' --indent 0 | + while read -r item; do + start_row=$(echo "$item" | jq '.match.start.row') + end_row=$(echo "$item" | jq '.match.end.row') + + last_commit=$(git --no-pager blame -L "$start_row,$start_row" "$file" -p | + head -n 1 | + cut -d ' ' -f 1 + ) + + text=$(echo "$item" | + jq .match.text -r | + sed 's/^# *TODO *//' | + sed 's/^\/\* *TODO *//' | + sed 's/ *\*\/$//') + + echo "$item" | jq '. + {"text": $text, "last_commit": $last_commit}' \ + --arg text "$text" \ + --arg last_commit "$last_commit" \ + -c + done + done diff --git a/src/extract.sh b/src/extract.sh new file mode 100755 index 0000000..cc7dfc6 --- /dev/null +++ b/src/extract.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +set -efu + +input_dir=$1 + +find "$input_dir" -type f | + while read -r input_file; do + ext= + case $input_file in + *.elm) ext="elm" ;; + *.nix) ext="nix" ;; + *) ;; + esac + + if test -z $ext; then + echo "warning, file $input_file ignored" >&2 + continue + fi + + "$(dirname "$0")"/extract-$ext.sh "$input_file" + done | + while read -r item; do + start_row=$(echo "$item" | jq '.match.start.row') + end_row=$(echo "$item" | jq '.match.end.row') + + last_commit=$(echo "$item" | jq '.last_commit' -r) + text=$(echo "$item" | jq '.text' -r) + file=$(echo "$item" | jq '.file' -r) + + first_commit=$(git --no-pager log --reverse -S"$text" --format=%H | \ + head -n 1) + + created_at=$(git show $first_commit --no-patch --format=%ad) + heading=$(echo "$text" | sed '/^$/Q' | sed 's/.*TODO //') + body=$(echo "$text" | tail -n +$(($(echo "$heading" | wc -l) + 2)) | \ + awk -F '[^ ]' ' + NR == 1 {n = length($1)} + {sub("^ {1,"n"}", ""); print}' + ) + + GREEN='\033[0;32m' + BOLD=$(tput bold) + NORMAL=$(tput sgr0) + NC='\033[0m' + echo -e "$GREEN--- $created_at --- $file$NC" + echo + echo "$BOLD$heading$NORMAL" | fold -s + if test -n "$body"; then + echo + echo "$body" | fold -s + echo + fi + echo + cat "$file" | nl -w 4 -s "| " -p -d '' -b a| tail -n +$(($start_row - 2)) | head -n $(($end_row - $start_row + 7)) + echo + echo + done |