-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_unmodified.sh
executable file
·199 lines (177 loc) · 4.69 KB
/
check_unmodified.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
# Global settings (based on environment variables)
TARGET_PATH="${UNMO_PATH:-.}"
EXT="${UNMO_EXT:-md}"
DAYS="${UNMO_DAYS:-365}"
SHOW_UNTRACKED="${SHOW_UNTRACKED:-false}"
SKIP_GIT_CHECK="${SKIP_GIT_CHECK:-false}"
QUIET="${UNMO_QUIET:-false}"
VERBOSE="${UNMO_VERBOSE:-false}"
DEBUG="${UNMO_DEBUG:-false}"
# also capture if --debug is passed anywhere in the args
if [[ "$DEBUG" = "true" || "$*" == *--debug* ]]; then
echo "Debugging output enabled."
set -x
fi
# Functions
show_help() {
cat <<EOF
Usage: ./check_unmodified.sh [OPTIONS] [DIRECTORY]
Directory defaults to the current directory if not specified.
Options:
--help Display this help and exit
--ext EXT Specify file extension (default: md)
--days DAYS Specify the timespan to check (default: 365)
--untracked Show files not tracked by Git
--skip-git-check Skip the Git check
--quiet Suppress normal output
--verbose Output more information
--debug Enable Bash debugging output
EOF
}
message() {
local level="$1"
local string
if [[ "$level" == "info" || "$level" == "warn" || "$level" == "error" || "$level" == "debug" ]]; then
string="$2"
shift
else
level="info"
string="$1"
fi
case "$level" in
"info")
if [[ "$QUIET" = "false" ]]; then
echo "INFO: $string"
fi
;;
"warn")
if [[ "$QUIET" = "false" ]]; then
echo "WARNING: $string" >&2
fi
;;
"error")
echo "ERROR: $string" >&2
;;
"debug")
if [[ "$DEBUG" = "true" || "$VERBOSE" = "true" ]]; then
echo "DEBUG: $string"
fi
;;
*)
echo "$2"
;;
esac
}
check_for_git() {
if ! command -v git &>/dev/null; then
echo "Error: Git is not installed." >&2
echo "Disable Git check with --skip-git-check"
exit 1
fi
# check for .git repository
if [[ ! -d .git ]]; then
echo "Error: Not a Git repository."
exit 1
fi
}
last_commit_date() {
local file="$1"
git log -1 --format="%ai" -- "$(realpath --relative-to="$(git rev-parse --show-toplevel)" "$file")" 2>/dev/null | cut -d ' ' -f 1
}
# Function to establish a local scope for main processing
main() {
local target_path="$TARGET_PATH"
local ext="$EXT"
local days="$DAYS"
local show_untracked="$SHOW_UNTRACKED"
local skip_git_check="$SKIP_GIT_CHECK"
local cutoff_date
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
--help)
show_help
exit 0
;;
--ext)
ext="$2"
shift 2
;;
--days)
days="$2"
shift 2
;;
--untracked)
show_untracked="true"
shift
;;
--skip-git-check)
skip_git_check="true"
shift
;;
--quiet)
QUIET="true"
shift
;;
--verbose)
VERBOSE="true"
shift
;;
--debug)
DEBUG="true"
shift
;;
*)
if [[ "$1" == -* ]]; then
echo "Error: Unknown option: $1" >&2
exit 1
fi
target_path="$1"
break
;;
esac
done
# Ensure the directory exists
if [[ ! -d "$target_path" ]]; then
message "error" "Directory '$target_path' does not exist."
exit 1
fi
# If $target_path is outside the current path, we need to navigate to it for the duration of the script
if [[ "$target_path" != "." && "$target_path" != "$(pwd)" ]]; then
cd "$target_path" || exit 1
target_path="."
fi
if [[ ! "$skip_git_check" = "true" ]]; then
check_for_git
fi
# Calculate the cutoff date in YYYY-MM-DD format
cutoff_date=$(date -d "$days days ago" +%Y-%m-%d)
# Determine the list of files to process
local files
if [[ "$show_untracked" = "true" ]]; then
mssg="Searching for *.$ext files in $target_path, including untracked files,"
files=$(find "$target_path" -type f -name "*.$ext")
else
mssg="Searching for *.$ext files in $target_path tracked by Git"
files=$(git ls-files -- "$target_path" | grep -E "\.${ext}$")
fi
message "debug" "$mssg last committed before $cutoff_date..."
# Check if no files are found
if [[ -z "$files" || $(echo "$files" | wc -l) -eq 0 ]]; then
message "info" "No files found."
exit 0
fi
# Process the files
while IFS= read -r file; do
local last_commit_date_value
last_commit_date_value=$(last_commit_date "$file")
if [[ -z "$last_commit_date_value" && "$show_untracked" = "true" ]]; then
echo "Untracked: $file"
elif [[ "$last_commit_date_value" < "$cutoff_date" ]]; then
echo "$file (Last committed: $last_commit_date_value)"
fi
done <<< "$files"
}
# Call the main function
main "$@"