From: Thomas Walker Lynch Date: Wed, 20 Nov 2024 08:49:22 +0000 (+0000) Subject: directory structure adds .githolder to empty directories X-Git-Url: https://git.reasoningtechnology.com/style/static/git-logo.png?a=commitdiff_plain;h=c710b73ca517209b490b0db1c3f54e81cdd95708;p=subu directory structure adds .githolder to empty directories --- diff --git a/tool_shared/bespoke/git_holder b/tool_shared/bespoke/git_holder deleted file mode 100755 index 7c6a3a9..0000000 --- a/tool_shared/bespoke/git_holder +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/env /bin/bash - -# Description: Finds all empty directories and adds a `.githolder` file to ensure Git tracking. - -# Function to add .githolder to empty directories -add_githolders() { - echo "Searching for empty directories..." - - # Find all empty directories and loop through them - find . -type d -empty | while read -r dir; do - # Add .githolder file in each empty directory - echo "Adding .githolder to $dir" - touch "$dir/.githolder" - done - - echo "Done! Empty directories are now tracked by Git with .githolder files." -} - -# Run the function -add_githolders diff --git a/tool_shared/bespoke/githolder b/tool_shared/bespoke/githolder new file mode 100755 index 0000000..49fb12b --- /dev/null +++ b/tool_shared/bespoke/githolder @@ -0,0 +1,63 @@ +#!/bin/env /bin/bash + +# Description: Descends from $1, or pwd, looking for empty directories and adds a `.githolder` to them. +# does not descend into hidden directories. + +# examples: +# > git_holder +# > git_holder --dry-run + +set -e + +find_empty_dirs() { + local dir="$1" + local dry_run="$2" + + # Skip `.git` specifically + if [[ "$(basename "$dir")" == ".git" ]]; then + return + fi + + # Check if the directory is empty (including hidden files, excluding `.` and `..`) + if [[ -z $(find "$dir" -mindepth 1 -maxdepth 1 -print -quit) ]]; then + if [[ "$dry_run" == "true" ]]; then + echo "Dry-run: Would add .githolder in $dir" + else + echo "Adding .githolder to $dir" + touch "$dir/.githolder" + fi + else + # Recurse into subdirectories + for subdir in "$dir"/*/ "$dir"/.[!.]/; do + if [[ -d "$subdir" && "$subdir" != "$dir/.[!.]/" ]]; then + find_empty_dirs "$subdir" "$dry_run" + fi + done + fi +} + +# Default parameters +dry_run="false" +target_dir="." + +# Parse arguments +while [[ $# -gt 0 ]]; do + case "$1" in + --dry-run) + dry_run="true" + shift + ;; + *) + if [[ -d "$1" ]]; then + target_dir="$1" + shift + else + echo "Invalid argument: $1 is not a directory" + exit 1 + fi + ;; + esac +done + +# Run the function +find_empty_dirs "$target_dir" "$dry_run"