aboutsummaryrefslogtreecommitdiffstats
path: root/extract-nix.sh
diff options
context:
space:
mode:
authorLibravatar Fabian Kirchner <kirchner@posteo.de>2023-09-05 13:48:06 +0200
committerLibravatar Fabian Kirchner <kirchner@posteo.de>2023-09-05 13:54:54 +0200
commit208e15c62d32c1604f2a70b4a6f630397330b869 (patch)
tree6546e18ed74f17fbcca2b815593ecd34dbb86605 /extract-nix.sh
parente99de30fdab8f13d6ea105ac8141a0624f7766da (diff)
add nix extract script
Diffstat (limited to 'extract-nix.sh')
-rwxr-xr-xextract-nix.sh44
1 files changed, 44 insertions, 0 deletions
diff --git a/extract-nix.sh b/extract-nix.sh
new file mode 100755
index 0000000..e092db2
--- /dev/null
+++ b/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