forked from gitwatch/gitwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·98 lines (74 loc) · 2.57 KB
/
entrypoint.sh
File metadata and controls
executable file
·98 lines (74 loc) · 2.57 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
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
#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# --- Environment Variable Configuration with Defaults ---
# Target directory to watch
GIT_WATCH_DIR=${GIT_WATCH_DIR:-/app/watched-repo}
GIT_DIR="${GIT_DIR:-}"
# Git options
GIT_REMOTE=${GIT_REMOTE:-origin}
GIT_BRANCH=${GIT_BRANCH:-main}
# Gitwatch behavior
SLEEP_TIME=${SLEEP_TIME:-2}
COMMIT_MSG=${COMMIT_MSG:-"Scripted auto-commit on change (%d) by gitwatch.sh"}
DATE_FMT=${DATE_FMT:-"+%Y-%m-%d %H:%M:%S"}
# Read the user-friendly pattern
USER_EXCLUDE_PATTERN=${EXCLUDE_PATTERN:-""}
EVENTS=${EVENTS:-""}
# Boolean flags (set to "true" to enable)
PULL_BEFORE_PUSH=${PULL_BEFORE_PUSH:-false}
SKIP_IF_MERGING=${SKIP_IF_MERGING:-false}
VERBOSE=${VERBOSE:-false}
COMMIT_ON_START=${COMMIT_ON_START:-false}
# --- Command Construction ---
# Use a bash array to safely build the command and its arguments
cmd=( "/app/gitwatch.sh" )
# Add options with arguments
cmd+=( -r "${GIT_REMOTE}" )
cmd+=( -b "${GIT_BRANCH}" )
cmd+=( -s "${SLEEP_TIME}" )
cmd+=( -m "${COMMIT_MSG}" )
cmd+=( -d "${DATE_FMT}" )
if [ -n "${GIT_DIR}" ]; then
cmd+=( -g "${GIT_DIR}" )
fi
# --- Convert User-Friendly Exclude Pattern to Regex ---
if [ -n "${USER_EXCLUDE_PATTERN}" ]; then
# 1. Replace commas with spaces to treat as separate words.
PATTERNS_AS_WORDS=${USER_EXCLUDE_PATTERN//,/ }
# 2. Use an array to store and automatically trim whitespace from each pattern.
read -r -a PATTERN_ARRAY <<< "$PATTERNS_AS_WORDS"
# 3. Join the array elements with the regex OR pipe `|`.
PROCESSED_PATTERN=$(IFS=\|; echo "${PATTERN_ARRAY[*]}")
# 4. Escape periods to treat them as literal dots in regex
PROCESSED_PATTERN=${PROCESSED_PATTERN//./\\.}
# 5. Convert glob stars `*` into the regex equivalent `.*`
PROCESSED_PATTERN=${PROCESSED_PATTERN//\*/\.\*}
cmd+=( -x "${PROCESSED_PATTERN}" )
fi
if [ -n "${EVENTS}" ]; then
cmd+=( -e "${EVENTS}" )
fi
# Add boolean flags if they are set to "true"
if [ "${PULL_BEFORE_PUSH}" = "true" ]; then
cmd+=( -R )
fi
if [ "${SKIP_IF_MERGING}" = "true" ]; then
cmd+=( -M )
fi
if [ "${VERBOSE}" = "true" ]; then
cmd+=( -v )
fi
if [ "${COMMIT_ON_START}" = "true" ]; then
cmd+=( -f )
fi
# The final argument is the directory to watch
cmd+=( "${GIT_WATCH_DIR}" )
# --- Execution ---
echo "Starting gitwatch with the following arguments:"
# Use printf with %q to safely quote the arguments for display
printf "%q " "${cmd[@]}"
echo # Add a newline for cleaner logging
echo "-------------------------------------------------"
# Use exec to replace the current shell process with gitwatch
exec "${cmd[@]}"