Skip to content

Instantly share code, notes, and snippets.

@monarchmaisuriya
Created November 26, 2025 21:07
Show Gist options
  • Select an option

  • Save monarchmaisuriya/df63e1bcf318a08b74aeabacfb3f6908 to your computer and use it in GitHub Desktop.

Select an option

Save monarchmaisuriya/df63e1bcf318a08b74aeabacfb3f6908 to your computer and use it in GitHub Desktop.
Gemini Based Doc Generator
# ==============================================
# === Start of Gemini Diff-Based Docs Generator ===
# ==============================================
# ---- Documentation Generation Prompt ----
__gemini_docs_prompt=$(
cat <<'EOF'
You are generating technical documentation from code changes.
Produce clear, maintainable, developer-friendly documentation.
Requirements:
- Summarize purpose & behavior of the changed code.
- Explain new APIs, functions, parameters, return values.
- Highlight architecture interactions & dependencies.
- Provide examples or usage notes.
- If the change affects existing docs, state what should be updated.
Do NOT rewrite code. Do NOT propose speculative features.
EOF
)
# ---- State Tracking for Git ----
LAST_GIT_CMD=""
LAST_GIT_EXIT_CODE=""
git() {
if [[ "$1" == "commit" ]]; then
LAST_GIT_CMD="commit"
else
LAST_GIT_CMD=""
fi
command git "$@"
LAST_GIT_EXIT_CODE=$?
return $LAST_GIT_EXIT_CODE
}
# ======================================================
# === Modular file/folder finder for documentation ===
# ======================================================
# allowed search patterns (modular; easy to extend)
DOC_TARGETS=(
"README.md"
"docs"
"wiki"
)
# check if a doc target exists in the repo root
find_existing_doc_target() {
for target in "${DOC_TARGETS[@]}"; do
if [[ -e "$target" ]]; then
echo "$target"
return 0
fi
done
return 1
}
# prompt user for a manual doc target path
prompt_for_doc_target() {
echo "No documentation folder/file found."
read -r "?Enter a file or folder path where documentation should be written: " user_path
echo "$user_path"
}
# generate documentation target path
resolve_doc_target() {
local existing
existing=$(find_existing_doc_target)
if [[ $? -eq 0 ]]; then
echo "$existing"
else
prompt_for_doc_target
fi
}
# ==============================================
# === pre-cmd hook for auto docs-generation ===
# ==============================================
precmd() {
if [[ "${LAST_GIT_CMD:-}" == "commit" ]] && [[ "${LAST_GIT_EXIT_CODE:-1}" -eq 0 ]]; then
LAST_GIT_CMD=""
LAST_GIT_EXIT_CODE=""
echo
read -r "REPLY?Generate documentation for the committed changes? [y/n] "
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
return
fi
echo "Locating documentation target..."
DOC_TARGET=$(resolve_doc_target)
echo "Documentation will be generated into: $DOC_TARGET"
echo
# get changed files
if git rev-parse --verify HEAD~1 >/dev/null 2>&1; then
files=$(git diff --name-only HEAD~1 HEAD)
else
files=$(git show --name-only --pretty="" HEAD)
fi
if [[ -z "${files//[[:space:]]/}" ]]; then
echo "No files changed in the last commit."
return
fi
for file in $files; do
if [[ -f "$file" ]]; then
echo "Analyzing changes in: $file"
git diff HEAD~1 HEAD -- "$file" | \
gemini -p "$__gemini_docs_prompt" | \
tee -a "$DOC_TARGET/GENERATED_DOCS.md" >/dev/null
fi
done
echo "Documentation appended to: $DOC_TARGET/GENERATED_DOCS.md"
fi
}
# ==============================================
# === End of Gemini Diff-Based Docs Generator ===
# ==============================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment