forked from ProfileManifests/ProfileManifests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·111 lines (91 loc) · 3.97 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
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
#!/bin/bash
# Verify the script is running inside the ProfileManifests repository and get the repository root path
repository_path=$( git rev-parse --show-toplevel 2> /dev/null );
if !( [[ $? -eq 0 ]] && [[ $(git remote -v) == *"ProfileManifests.git"* ]] ); then
printf "%s\n" "This script must be run inside the ProfileManifests repository."
exit 1
fi
cd $repository_path
index_path="${repository_path}/Manifests/index"
if ! [[ -f ${index_path} ]]; then
printf "%s\n" "This script requires an index file to be present at: ${index_path}"
exit 1
fi
# Get the latest commit to verify against
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
against=$( git rev-list --max-parents=0 HEAD )
fi
updated_file="false"
# Loop through all plist-files to be committed
for file in $( git diff --name-only --diff-filter=AM "${against}" | grep '\.plist$' ); do
# Get the file name
file_name=$( basename "${file}" )
# Get the file directory
file_directory=$(basename $( dirname "${file}" ))
# Get the value of the pfm_version key
file_version=$( /usr/libexec/PlistBuddy -c "Print pfm_version" "${file}" 2>/dev/null )
# Verify the value returned is valid
if [[ -z ${file_version} ]] && ! /usr/libexec/PlistBuddy -c "Add pfm_version integer 1" "${file}"; then
printf "%s\n" "${file_name}: Failed to add value: 1 for key: pfm_version"
exit 1
elif ! [[ ${file_version} =~ ^[0-9]+$ ]]; then
printf "%s\n" "${file_name}: Invalid or missing value: ${file_version} for key: pfm_version"
exit 1
fi
# Get the value of the pfm_domain key
file_domain=$( /usr/libexec/PlistBuddy -c "Print pfm_domain" "${file}" 2>/dev/null )
# Get the file hash
file_hash=$( plutil -convert binary1 -o - "${file}" | md5 -q )
# Get the saved file hash
file_hash_index=$( /usr/libexec/PlistBuddy -c "Print ${file_directory}:${file_domain}:hash" "${index_path}" 2>/dev/null )
if [[ ${file_hash} != ${file_hash_index} ]]; then
# Increment the current version number by 1
version_updated=$(( file_version + 1 ))
# Add the new value to the commit
if ! /usr/libexec/PlistBuddy -c "Set pfm_version ${version_updated}" "${file}" 2>/dev/null; then
printf "%s\n" "$( basename ${file} ): Failed to set value: ${version_updated} for key: pfm_version"
exit 1
fi
# Get the value of the pfm_last_modified key
file_last_modified=$( /usr/libexec/PlistBuddy -c "Print pfm_last_modified" "${file}" 2>/dev/null )
# Verify the value returned is valid
if [[ -z ${file_last_modified} ]] && ! /usr/libexec/PlistBuddy -c "Add pfm_last_modified date $( LANG=C /bin/date +%a" "%b" "%d" "%T" "%Z" "%Y )" "${file}"; then
printf "%s\n" "${file_name}: Failed to add value: $( LANG=C /bin/date +%a" "%b" "%d" "%T" "%Z" "%Y ) for key: pfm_last_modified"
exit 1
elif ! /usr/libexec/PlistBuddy -c "Set pfm_last_modified $( LANG=C /bin/date +%a" "%b" "%d" "%T" "%Z" "%Y )" "${file}"; then
printf "%s\n" "${file_name}: Failed to add value: $( LANG=C /bin/date +%a" "%b" "%d" "%T" "%Z" "%Y ) for key: pfm_last_modified"
exit 1
fi
updated_file="true"
fi
done
if [[ ${updated_file} == true ]]; then
updateIndex="${repository_path}/updateIndexManifests"
if [[ -f ${updateIndex} ]]; then
if ! "${updateIndex}"; then
printf "%s\n" "Failed to run updateIndex at ${updateIndex}"
exit 2
fi
else
printf "%s\n" "${updateIndex}: No such file or directory"
exit 2
fi
printf "%s\n" "Updated version and date information in committed manifests, please press commit again to include the added changes."
exit 3
fi
if $( git diff --name-only --diff-filter=AM "${against}" | grep '\.png$' ); then
updateIndex="${repository_path}/updateIndexIcons"
if [[ -f ${updateIndex} ]]; then
if ! "${updateIndex}"; then
printf "%s\n" "Failed to run updateIndex at ${updateIndex}"
exit 2
fi
else
printf "%s\n" "${updateIndex}: No such file or directory"
exit 2
fi
printf "%s\n" "Updated version and date information in committed icons, please press commit again to include the added changes."
exit 3
fi