This commit is contained in:
2026-02-25 16:39:22 +02:00
parent f84477f9b9
commit cf7f8609ac
8 changed files with 378 additions and 52 deletions

View File

@@ -3,13 +3,25 @@
set -euo pipefail
usage() {
echo "Usage: $(basename "$0") -c COMMENT_SYMBOL [-e EXCLUDE_PATTERN]... TARGET"
echo " -c Comment symbol (e.g., '#' or '//')"
echo "Usage: $(basename "$0") <command> [options] TARGET"
echo ""
echo "Commands:"
echo " add Add/update path comment in first 5 lines of each file"
echo " undo Remove any path comment from first 5 lines of each file"
echo ""
echo "Options:"
echo " -c Comment symbol (e.g., '#' or '//') [required for add]"
echo " -e Exclude pattern (can be specified multiple times)"
echo " TARGET File or directory to process"
exit 1
}
[[ $# -lt 1 ]] && usage
cmd="$1"
shift
[[ "$cmd" != "add" && "$cmd" != "undo" ]] && usage
comment_sym=""
excludes=()
@@ -24,66 +36,63 @@ done
shift $((OPTIND - 1))
[[ $# -ne 1 ]] && usage
[[ -z "$comment_sym" ]] && usage
if [[ "$cmd" == "add" ]]; then
[[ -z "$comment_sym" ]] && {
echo "Error: -c is required for add" >&2
usage
}
fi
target="$(realpath "$1")"
base_dir="$(pwd)"
strip_path_comment() {
local file="$1"
awk 'NR <= 5 && /path: / { next } { print }' "$file" >"$file.tmp"
mv "$file.tmp" "$file"
}
process_file() {
local file="$1"
# shellcheck disable=SC2295
local rel_path="${file#$base_dir/}"
local path_comment="$comment_sym path: $rel_path"
# Read first two lines
local line1 line2
IFS= read -r line1 <"$file" 2>/dev/null || line1=""
IFS= read -r line2 < <(tail -n +2 "$file") 2>/dev/null || line2=""
strip_path_comment "$file"
local line1
IFS= read -r line1 <"$file" 2>/dev/null || line1=""
# Handle shebang case
if [[ "$line1" =~ ^#! ]]; then
if [[ "$line2" == *"path: "* ]]; then
# Replace existing path comment on line 2
{
echo "$line1"
echo "$path_comment"
tail -n +3 "$file"
} >"$file.tmp"
else
# Insert new path comment after shebang
{
echo "$line1"
echo "$path_comment"
tail -n +2 "$file"
} >"$file.tmp"
fi
{
echo "$line1"
echo "$path_comment"
tail -n +2 "$file"
} >"$file.tmp"
else
if [[ "$line1" == *"path: "* ]]; then
# Replace existing path comment on line 1
{
echo "$path_comment"
tail -n +2 "$file"
} >"$file.tmp"
else
# Insert new path comment at top
{
echo "$path_comment"
cat "$file"
} >"$file.tmp"
fi
{
echo "$path_comment"
cat "$file"
} >"$file.tmp"
fi
mv "$file.tmp" "$file"
}
if [[ -f "$target" ]]; then
process_file "$target"
elif [[ -d "$target" ]]; then
find_cmd=(find "$target")
dispatch() {
local file="$1"
if [[ "$cmd" == "add" ]]; then
process_file "$file"
else
strip_path_comment "$file"
fi
}
# Always exclude hidden files and directories
find_cmd+=(\( -name ".*" -prune \))
if [[ -f "$target" ]]; then
dispatch "$target"
elif [[ -d "$target" ]]; then
find_cmd=(find "$target" \( -name ".*" -prune \))
if [[ ${#excludes[@]} -gt 0 ]]; then
find_cmd+=(-o \()
@@ -98,7 +107,7 @@ elif [[ -d "$target" ]]; then
find_cmd+=(-o -type f -print0)
while IFS= read -r -d '' file; do
process_file "$file"
dispatch "$file"
done < <("${find_cmd[@]}")
else
echo "Error: $target is not a file or directory" >&2