test
This commit is contained in:
parent
238dab668a
commit
b1b7898a5c
78
reddup.sh
78
reddup.sh
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# Author: ChatGPT
|
# Author: ChatGPT
|
||||||
@ -7,16 +8,19 @@
|
|||||||
# file in each non-hidden directory. The file is named "Folder Name - README.md".
|
# file in each non-hidden directory. The file is named "Folder Name - README.md".
|
||||||
#
|
#
|
||||||
# Each README includes:
|
# Each README includes:
|
||||||
# - A header with an author/date line.
|
# - An author/date header.
|
||||||
# - A header with the folder's name.
|
# - A header with the folder's name.
|
||||||
# - An alphabetically sorted list of immediate subdirectories, with links
|
# - An alphabetically sorted list of immediate subdirectories, with links
|
||||||
# to that subdirectory’s own README.
|
# to that subdirectory’s own README.
|
||||||
# - An alphabetically sorted list of immediate files (excluding the generated README).
|
# - An alphabetically sorted list of immediate files (excluding the generated README).
|
||||||
|
# - A "Summary" section with a short description generated by an LLM (using llama3.2:latest).
|
||||||
#
|
#
|
||||||
# Hidden directories (or any directory under a hidden parent such as .git) are skipped.
|
# Hidden directories (or any directory under a hidden parent such as .git) are skipped.
|
||||||
#
|
#
|
||||||
# The script only updates a README if the "body" (everything from line 3 on)
|
# The script only updates a README if the static content (header, subdirectories and files)
|
||||||
# has changed. It also prints a progress indicator like "[x/y] Updated: {dir}".
|
# changes. The LLM summary output is excluded from the diff check.
|
||||||
|
#
|
||||||
|
# Progress is printed as "[x/y] Updated: {dir}" or "[x/y] No change: {dir}".
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
# Set the base directory (change "." if necessary)
|
# Set the base directory (change "." if necessary)
|
||||||
@ -25,8 +29,8 @@ base_dir="."
|
|||||||
# Build an array of directories to process.
|
# Build an array of directories to process.
|
||||||
dirs=()
|
dirs=()
|
||||||
while IFS= read -r -d '' dir; do
|
while IFS= read -r -d '' dir; do
|
||||||
# Skip directories whose basename starts with a dot or whose path contains "/.git"
|
# Skip directories that are hidden or inside a hidden parent (like .git)
|
||||||
if [[ "$(basename "$dir")" == .* ]] || [[ "$dir" == *"/.git"* ]] || [[ "$dir" == *"/.obsidian"* ]] ; then
|
if [[ "$(basename "$dir")" == .* ]] || [[ "$dir" == *"/.git"* ]]; then
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
dirs+=( "$dir" )
|
dirs+=( "$dir" )
|
||||||
@ -35,10 +39,10 @@ done < <(find "$base_dir" -type d -print0)
|
|||||||
total=${#dirs[@]}
|
total=${#dirs[@]}
|
||||||
count=0
|
count=0
|
||||||
|
|
||||||
# Process each directory in the array.
|
|
||||||
for dir in "${dirs[@]}"; do
|
for dir in "${dirs[@]}"; do
|
||||||
count=$((count+1))
|
count=$((count+1))
|
||||||
# For the top-level directory, use the actual name (using $PWD); otherwise, use the directory's basename.
|
|
||||||
|
# For the top-level directory, use the actual name from $PWD; otherwise, use the directory's basename.
|
||||||
if [ "$dir" = "$base_dir" ]; then
|
if [ "$dir" = "$base_dir" ]; then
|
||||||
base=$(basename "$PWD")
|
base=$(basename "$PWD")
|
||||||
else
|
else
|
||||||
@ -48,16 +52,14 @@ for dir in "${dirs[@]}"; do
|
|||||||
# Define the path for the README file: "Folder Name - README.md"
|
# Define the path for the README file: "Folder Name - README.md"
|
||||||
readme="$dir/${base} - README.md"
|
readme="$dir/${base} - README.md"
|
||||||
|
|
||||||
# Create a temporary file to hold the new content.
|
# Create a temporary file to hold the new static content.
|
||||||
tmpfile=$(mktemp)
|
tmp_static=$(mktemp)
|
||||||
|
|
||||||
# Write the header into the temporary file.
|
|
||||||
{
|
{
|
||||||
echo "Generated by ChatGPT on $(date '+%Y-%m-%d')"
|
echo "Generated by ChatGPT on $(date '+%Y-%m-%d')"
|
||||||
echo ""
|
echo ""
|
||||||
echo "# Table of Contents for ${base}"
|
echo "# Table of Contents for ${base}"
|
||||||
echo ""
|
echo ""
|
||||||
} > "$tmpfile"
|
} > "$tmp_static"
|
||||||
|
|
||||||
#########################
|
#########################
|
||||||
# Process Subdirectories
|
# Process Subdirectories
|
||||||
@ -71,15 +73,14 @@ for dir in "${dirs[@]}"; do
|
|||||||
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d ! -name '.*' -print0)
|
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d ! -name '.*' -print0)
|
||||||
|
|
||||||
if [ ${#subdirs[@]} -gt 0 ]; then
|
if [ ${#subdirs[@]} -gt 0 ]; then
|
||||||
# Sort subdirectory names alphabetically.
|
|
||||||
mapfile -t sorted_subdirs < <(printf "%s\n" "${subdirs[@]}" | sort)
|
mapfile -t sorted_subdirs < <(printf "%s\n" "${subdirs[@]}" | sort)
|
||||||
echo "## Subdirectories" >> "$tmpfile"
|
echo "## Subdirectories" >> "$tmp_static"
|
||||||
for sub in "${sorted_subdirs[@]}"; do
|
for sub in "${sorted_subdirs[@]}"; do
|
||||||
# Link format: [[Subfolder/Subfolder - README]]
|
# Link format: [[Subfolder/Subfolder - README]]
|
||||||
line="- [[${sub}/${sub} - README]]"
|
line="- [[${sub}/${sub} - README]]"
|
||||||
printf "%s\n" "$line" >> "$tmpfile"
|
printf "%s\n" "$line" >> "$tmp_static"
|
||||||
done
|
done
|
||||||
echo "" >> "$tmpfile"
|
echo "" >> "$tmp_static"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#################
|
#################
|
||||||
@ -94,28 +95,49 @@ for dir in "${dirs[@]}"; do
|
|||||||
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type f ! -path "$readme" -print0)
|
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type f ! -path "$readme" -print0)
|
||||||
|
|
||||||
if [ ${#files[@]} -gt 0 ]; then
|
if [ ${#files[@]} -gt 0 ]; then
|
||||||
# Sort file names alphabetically.
|
|
||||||
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
|
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
|
||||||
echo "## Files" >> "$tmpfile"
|
echo "## Files" >> "$tmp_static"
|
||||||
for f in "${sorted_files[@]}"; do
|
for f in "${sorted_files[@]}"; do
|
||||||
line="- [[${f}]]"
|
line="- [[${f}]]"
|
||||||
printf "%s\n" "$line" >> "$tmpfile"
|
printf "%s\n" "$line" >> "$tmp_static"
|
||||||
done
|
done
|
||||||
echo "" >> "$tmpfile"
|
echo "" >> "$tmp_static"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If the README already exists, compare its body (from line 3 onward)
|
# Determine if the static content has changed.
|
||||||
# with the new content (ignoring the first two lines, which contain the date header).
|
static_changed=1
|
||||||
if [ -f "$readme" ]; then
|
if [ -f "$readme" ]; then
|
||||||
if diff -q <(tail -n +3 "$tmpfile") <(tail -n +3 "$readme") >/dev/null; then
|
# Extract the static portion from the existing README (everything before "## Summary")
|
||||||
rm "$tmpfile"
|
existing_static=$(sed '/^## Summary/ q' "$readme")
|
||||||
echo "[$count/$total] No change: $dir"
|
new_static=$(cat "$tmp_static")
|
||||||
continue
|
if [ "$existing_static" = "$new_static" ]; then
|
||||||
|
static_changed=0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Update (or create) the README.
|
if [ $static_changed -eq 0 ]; then
|
||||||
mv "$tmpfile" "$readme"
|
echo "[$count/$total] No change: $dir"
|
||||||
|
rm "$tmp_static"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
#################
|
||||||
|
# Query LLM for Summary
|
||||||
|
#################
|
||||||
|
prompt="Based on the following table of contents, provide a short summary description of what this directory is about:
|
||||||
|
$(cat "$tmp_static")"
|
||||||
|
# Query the model using the Ollama CLI with the llama3.2:latest model.
|
||||||
|
summary=$(ollama run llama3.2:latest --prompt "$prompt")
|
||||||
|
|
||||||
|
# Build the full new content: static portion + summary section.
|
||||||
|
{
|
||||||
|
cat "$tmp_static"
|
||||||
|
echo "## Summary"
|
||||||
|
echo "$summary"
|
||||||
|
echo ""
|
||||||
|
} > "$readme"
|
||||||
|
|
||||||
|
rm "$tmp_static"
|
||||||
echo "[$count/$total] Updated: $dir"
|
echo "[$count/$total] Updated: $dir"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user