-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_format.sh
executable file
·133 lines (120 loc) · 4.38 KB
/
auto_format.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
git checkout main
update_files=$(git diff HEAD^ HEAD --name-only --diff-filter=d)
del_files=$(git diff HEAD^ HEAD --diff-filter=D --name-only)
#set -x
# Determine .fileignore file if updated
if [[ $(echo $update_files | grep .fignore) ]]; then
echo ".fignore file updated"
# Get the hash of the last two commit that modified .fignore
latest_commit=$(git log -1 --pretty=format:%H)
previous_commit=$(git log -2 --pretty=format:%H | tail -n 1)
# If content has changed, check for specific changes
diff_output=$(git diff $latest_commit_hash $previous_commit .fignore)
if echo "$diff_output" | grep -E "^-[^-]" >/dev/null; then
del_lines=$(echo "$diff_output" |
grep "^-[^-]" |
sed '/^-/s/^-//' |
sed '/^#/d')
echo "File .fignore has deleted lines: $del_lines"
# Judge del_lines is file or dir
for line in $del_lines; do
# /dir/*
if [[ -f $line ]]; then
echo ".fignore del file: $line"
update_files+=($line)
# filename
else
u_files=$(find -name "$line.*")
update_files+=($u_files)
fi
done
fi
fi
#set +x
echo "Deleted files: "
echo "${del_files[@]}"
echo "---------------------"
if [[ $del_files ]]; then
for file in "${del_files[@]}"; do
echo "Deleting file: $file"
/bin/bash ../my_scripts/format.sh -d "$file"
done
fi
echo "Updated files: "
echo "${update_files[@]}"
echo "---------------------"
# Read the .fignore file
while read line; do
# Ignore lines that start with "#" or are empty
if [[ $line == \#* ]] || [[ -z $line ]]; then
continue
fi
if [[ $line =~ .*/\*$ ]]; then
for f in $line; do
f=$(echo $f)
ignore_list+=("$f")
done
continue
fi
# Ignore the file
ignore_list+=("$line")
done < .fignore
for ifile in "${ignore_list[@]}"; do
echo "ignore_list: $ifile"
done
sleep 1
files=$(echo "${update_files[@]}" | tr '\n' ' ')
IFS=' ' read -r -a files <<< "${files[@]}"
#set -x
for file in "${files[@]}";
do
# Ignore files that match any pattern in the ignore list
for pattern in "${ignore_list[@]}"; do
if [[ $file == $pattern ]]; then
echo "Ignoring file: $file"
continue 2
fi
name=$(basename "$file")
if [[ $name == $pattern ]]; then
echo "Ignoring file: $file"
continue 2
fi
done
echo "Formatting file: $file"
/bin/bash ../my_scripts/format.sh -f "$file"
done
# Itersate over all files in the repo
for file in $(find . -type f -not -path '*/\.*' -not -path './.fignore' -not -path './.git/*' -name flush);
do
echo "original file: $file"
echo "---------FLUSH-----------"
filename="${file#./}"
echo "Flush file: $filename"
content=$(cat $file)
if [[ $content != "flush" ]]; then
echo "Flush file content is not 1"
continue
fi
flush_dir=$(dirname $filename)
for f in $flush_dir/*; do
# Ignore files that match any pattern in the ignore list
for pattern in "${ignore_list[@]}"; do
if [[ "$f" == "$pattern" ]]; then
echo "Ignoring file: $f"
continue 2
fi
name=$(basename "$f")
echo "name: $name"
if [[ "$name" == "$pattern" ]]; then
echo "Ignoring file: $f"
continue 2
fi
done
echo "Formatting file: $f"
if [[ -f $f ]]; then
echo ''
/bin/bash ../my_scripts/format.sh -f "$f" --force
fi
done
done