forked from leomercier/arok-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat_files.sh
executable file
·95 lines (82 loc) · 3.01 KB
/
concat_files.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# Get current timestamp
timestamp=$(date +"%Y%m%d_%H%M%S")
output_file="./projects/project-${timestamp}.txt"
script_name=$(basename "$0")
# Create projects directory if it doesn't exist
mkdir -p ./projects
# Function to check if a file matches gitignore patterns
is_ignored() {
local file="$1"
local pattern
# Remove leading ./ from the file path for matching
file="${file#./}"
while IFS= read -r pattern || [ -n "$pattern" ]; do
# Skip empty lines and comments
[[ -z "$pattern" || "$pattern" =~ ^[[:space:]]*# ]] && continue
# Trim whitespace
pattern="$(echo "$pattern" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
[ -z "$pattern" ] && continue
# Convert pattern to regex
# Handle specific patterns we know exist in the gitignore
case "$pattern" in
"node_modules/"*)
if [[ "$file" == node_modules/* ]]; then
return 0
fi
;;
"node_modules")
if [[ "$file" == node_modules/* || "$file" == "node_modules" ]]; then
return 0
fi
;;
*.*)
# Handle file extensions and wildcard patterns
pattern="$(echo "$pattern" | sed 's/\./\\./g' | sed 's/\*/.*/')"
if [[ "$file" =~ ^${pattern}$ || "$file" =~ ^.*/${pattern}$ ]]; then
return 0
fi
;;
*)
# Handle directory patterns
if [[ "$pattern" == */ ]]; then
if [[ "$file" == "$pattern"* || "$file" == */"$pattern"* ]]; then
return 0
fi
else
if [[ "$file" == "$pattern" || "$file" == */"$pattern" ]]; then
return 0
fi
fi
;;
esac
done < ".gitignore"
return 1
}
# Initialize output file
echo "Starting concatenation at $(date)" > "$output_file"
# Find all files and process them
find . -type f -not -path "./logs/*" -not -path "./node_modules/*" -not -path "./projects/*" -not -path "./.git/*" -not -name "$script_name" | sort | while IFS= read -r file; do
# Skip if file matches gitignore patterns
if [ -f ".gitignore" ] && is_ignored "$file"; then
echo "Skipping ignored file: $file"
continue
fi
# Check if file is readable before attempting to process
if [ -r "$file" ]; then
echo "Processing: ${file#./}"
{
echo -e "\n=== File: ${file#./} ==="
cat "$file"
} >> "$output_file"
else
echo "Warning: Cannot read file $file"
fi
done
echo "Concatenation complete. Output written to: $output_file"
# Verify the output file was created and has content
if [ -s "$output_file" ]; then
echo "Success! Output file created with $(wc -l < "$output_file") lines"
else
echo "Warning: Output file is empty"
fi