-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-commit
executable file
·51 lines (41 loc) · 1.42 KB
/
pre-commit
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
#!/usr/bin/env bash
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
# add this pre-commit file into .git/hooks of your downloaded repo
# so that git automatically updates the lastUpdate field in the droneModels.json
# prior to each commit
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# Redirect output to stderr.
exec 1>&2
# Get the absolute path to the root of the Git repository
REPO_ROOT=$(git rev-parse --show-toplevel)
# Define the path to the file relative to the root of the repository
FILE="$REPO_ROOT/droneModels.json"
# Define the current date format
CURRENT_DATE=$(date)
# Determine OS type
OS_TYPE=$(uname)
# Use sed to replace the placeholder with the current date
# Adjust the sed command based on the operating system
if [[ "$OS_TYPE" == "Darwin" ]]; then
# macOS (BSD sed)
/usr/bin/sed -i '' "s|\"lastUpdate\": \".*\"|\"lastUpdate\": \"$CURRENT_DATE\"|" "$FILE"
else
# Linux (GNU sed)
/usr/bin/sed -i "s|\"lastUpdate\": \".*\"|\"lastUpdate\": \"$CURRENT_DATE\"|" "$FILE"
fi
# Add the file to the commit
git add $FILE
# End the script
exit 0