Tuesday, February 20, 2024

Run tree command respecting .gitignore

#!/bin/bash
# /opt/scripts/tree-gitignore.sh
# Author: Nestor Urquiza
# Date: 20240220
# Description: A tree wrapper to show contents of a git project respecting .gitignore
# Usage: cd git-project && /opt/scripts/tree-gitignore.sh
cmd="tree -a -I '.git'"
# Read each line from .gitignore
while IFS= read -r line; do
# Skip empty lines and comments
if [[ "$line" != "" && "$line" != \#* ]]; then
# Remove leading "**/" from patterns, if present
pattern="${line/\*\*\//}"
# Remove trailing "/*" from patterns, if present
pattern="${pattern/\/\*/}"
# Append each cleaned pattern as an ignore option
cmd+=" -I '$pattern'"
fi
done < .gitignore
# Execute the constructed command
eval $cmd

No comments:

Followers