forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
·64 lines (55 loc) · 2.06 KB
/
Copy pathformat.sh
File metadata and controls
executable file
·64 lines (55 loc) · 2.06 KB
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
#!/bin/bash
############################################################################
# This script will format all C source files (.h and .c) that are not
# mentioned in .clangformatignore, and all CMake files (*.cmake and CMakeLists.txt)
# that are not mentioned in .cmakeformatignore.
# Note that the script has to be run from the same folder where
# the ignore files (.clangformatignore and .cmakeformatignore) are located.
###########################################################################
############################################################################
# Check if the file given as input has .h or .c extension
############################################################################
# Check if the file given as input has .h or .c extension
is_source_file() {
[[ "$1" == *.h || "$1" == *.c ]]
}
# Check if the file is a CMake file (.cmake or CMakeLists.txt)
is_cmake_file() {
case "$1" in
*.cmake) return 0 ;;
*/CMakeLists.txt|*\\CMakeLists.txt|CMakeLists.txt) return 0 ;;
*) return 1 ;;
esac
}
# Check if the file passed as argument is ignored or not by a given ignore file
is_not_ignored_in() {
local file="$1"
local ignore_file="$2"
if [[ ! -f "$ignore_file" ]]; then
return 0
fi
while IFS= read -r entry || [[ -n "$entry" ]]; do
# Skip empty lines and comments
[[ -z "$entry" || "$entry" =~ ^# ]] && continue
# If entry is a directory and file is inside it
if [[ -d "$entry" && "$file" == "$entry"* ]]; then
return 1
fi
# If entry matches the file exactly
if [[ "$file" == "$entry" ]]; then
return 1
fi
done < "$ignore_file"
return 0
}
format_all() {
git ls-tree -r --name-only HEAD | while read -r file; do
if is_source_file "$file" && is_not_ignored_in "$file" .clangformatignore; then
clang-format -i "$file"
fi
if is_cmake_file "$file" && is_not_ignored_in "$file" .cmakeformatignore; then
cmake-format -i "$file"
fi
done
}
format_all