Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
fix-indents.sh
Go to the documentation of this file.
1#!/bin/bash
2
3trap 'echo "Interrupted."; exit 1' INT
4
5show_help() {
6 echo "Usage: $(basename "$0") [--changed] [-h|--help] [FILES_OR_DIRS...]"
7 echo
8 echo " --changed Only process modified files (master or MR target branch as reference)"
9 echo " -h, --help Show this help message"
10 echo " FILES_OR_DIRS List of files or directories to process"
11 exit 0
12}
13
14only_changed=false
15targets=()
16
17while [[ "$#" -gt 0 ]]; do
18 case "$1" in
19 --changed)
20 only_changed=true
21 ;;
22 -h|--help)
23 show_help
24 ;;
25 -*)
26 echo "Unknown option: $1" >&2
27 show_help
28 ;;
29 *)
30 targets+=("$1")
31 ;;
32 esac
33 shift
34done
35
36# file extensions to include
37extensions=(c cpp cc h hh hpp)
38
39# If --changed is given without files/dirs, use default targets
40if $only_changed && [[ "${#targets[@]}" -eq 0 ]]; then
41 targets=(software examples documentation benchmarks tests)
42fi
43
44if [[ "${#targets[@]}" -eq 0 ]]; then
45 echo "Error: No files or directories specified."
46 show_help
47fi
48
49target_branch="${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-master}"
50if $only_changed; then
51 echo "Checking connection to remote 'origin'..."
52 if git ls-remote --exit-code --heads origin "$target_branch" &>/dev/null; then
53 echo "Updating local sync of the target branch: $target_branch"
54 git fetch origin "$target_branch"
55 else
56 echo "Warning: Could not reach remote 'origin'. Skipping fetch (offline?)."
57 fi
58fi
59
60# Generate `find`-compatible args: ( -name "*.c" -o -name "*.cpp" ...)
61find_exts_args=()
62for ext in "${extensions[@]}"; do
63 find_exts_args+=(-name "*.${ext}" -o)
64done
65unset 'find_exts_args[${#find_exts_args[@]}-1]' # Remove trailing -o
66find_exts_args=( \‍( "${find_exts_args[@]}" \‍) )
67
68# Generate Bash regex: \.(c|cpp|cc|h|hh|hpp)$
69bash_regex="\.($(IFS='|'; echo "${extensions[*]}"))\$"
70
71modified=0
72
73for target in "${targets[@]}"; do
74 if [[ -d "$target" ]]; then
75 echo "Scanning directory: $target"
76 files=()
77 if $only_changed; then
78 changed_files=$( \
79 (git diff --name-only "origin/$target_branch...HEAD"; \
80 git diff --name-only --cached; \
81 git diff --name-only) | sort -u)
82 while IFS= read -r file; do
83 [[ -f "$file" && "$file" == $target/* && "$file" =~ $bash_regex ]] && files+=("$file")
84 done <<< "$changed_files"
85 else
86 while IFS= read -r line; do
87 files+=("$line")
88 done < <(find "$target" -type f "${find_exts_args[@]}")
89 fi
90 elif [[ -f "$target" && "$target" =~ $bash_regex ]]; then
91 files=("$target")
92 else
93 echo "Skipping unsupported or non-existent target: $target"
94 continue
95 fi
96
97 for file in "${files[@]}"; do
98 echo -n " $file: "
99 output=$(emacs --batch -q --no-site-file --no-site-lisp --eval='
100 (progn
101 (defun indent-whole-buffer ()
102 (let ((inhibit-message t))
103 (indent-region (point-min) (point-max))))
104 (let ((file (car command-line-args-left)))
105 (find-file file)
106 (normal-mode)
107 (setq indent-tabs-mode nil)
108 (setq tab-width 4)
109 (indent-whole-buffer)
110 (if (buffer-modified-p)
111 (progn
112 (save-buffer)
113 (message "fixed"))
114 (message "no changes")))
115 (kill-emacs 0))
116 ' "$file" 2>&1)
117 echo "$output"
118 if [[ "$output" == fixed ]]; then
119 modified=1
120 fi
121 done
122done
123
124if [[ "$modified" -eq 1 ]]; then
125 echo "One or more files were modified!"
126fi
127
128exit $modified