-
Notifications
You must be signed in to change notification settings - Fork 0
/
v02gitdiff
executable file
·198 lines (167 loc) · 4.88 KB
/
v02gitdiff
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
#!/usr/bin/env bash
# set a default for the key file if none is given
if [[ -z "${V02ENC_KEY}" ]]
then
V02ENC_KEY="-"
fi
SCRIPT_DIR="$(/usr/bin/env dirname "$(/usr/bin/env realpath "$0")")"
# handle parameters
GIT_ROOT="${PWD}"
FILE1="${2}"
MODE1="${4}"
FILE2="${5}"
MODE2="${7}"
FILENAME="${1}"
# prepare the output
COMPARED="0"
EXITCODE="0"
OUTPUT=()
TEMPFILE1="${FILE1}"
TEMPFILE2="${FILE2}"
if [[ ${V02ENC_KEY} == "-" || -f "${V02ENC_KEY}" ]]
then
if [[ -f "${FILE1}" || -f "${FILE2}" ]]
then
# check if we are comparing v02enc encrypted files
if [[ "${FILE1: -7}" == ".v02enc" || "${FILE2: -7}" == ".v02enc" ]]
then
# try to read the encryption key
if [[ -z "${V02ENC_PASSPHRASE}" && "${V02ENC_KEY}" == "-" ]]
then
# check if there is a key file
if [[ -f ~/.v02enc ]]
then
# read the key file
V02ENC_PASSPHRASE="$(/usr/bin/env cat ~/.v02enc | /usr/bin/env xxd -p | /usr/bin/env tr -d "\n")"
else
# check if we are running macOS
OS="$(/usr/bin/env uname -s | /usr/bin/env tr "[:upper:]" "[:lower:]")"
if [[ "$OS" == "darwin" ]]
then
# try to read the default keychain value
TMP="$(/usr/bin/env security find-generic-password -a "$(/usr/bin/env whoami)" -s "v02enc" -w 2>/dev/null)"
if [[ "$?" -eq "0" ]]
then
V02ENC_PASSPHRASE="${TMP}"
fi
fi
fi
fi
# generate the temporary file names
TEMPFILE1="$(/usr/bin/env mktemp)"
TEMPFILE2="$(/usr/bin/env mktemp)"
# prepare exit codes
EXITCODE1="0"
EXITCODE2="0"
if [[ -f "${FILE1}" ]]
then
# decrypt the first file
echo -n "${V02ENC_PASSPHRASE}" | /usr/bin/env xxd -p -r | "${SCRIPT_DIR}/v02enc" --decrypt --key "${V02ENC_KEY}" --input "${FILE1}" --output "${TEMPFILE1}" 2>/dev/null
# store exit code
EXITCODE1="$?"
if [[ "${EXITCODE1}" -eq "0" ]]
then
# set restrictive temporary file access
/usr/bin/env chmod 400 "${TEMPFILE1}"
fi
fi
if [[ -f "${FILE2}" ]]
then
# decrypt the second file
echo -n "${V02ENC_PASSPHRASE}" | /usr/bin/env xxd -p -r | "${SCRIPT_DIR}/v02enc" --decrypt --key "${V02ENC_KEY}" --input "${FILE2}" --output "${TEMPFILE2}" 2>/dev/null
# store exit code
EXITCODE2="$?"
if [[ "${EXITCODE2}" -eq "0" ]]
then
# set restrictive temporary file access
/usr/bin/env chmod 400 "${TEMPFILE2}"
fi
fi
# check that both files could be decrypted
if [[ "${EXITCODE1}" -eq "0" && "${EXITCODE2}" -eq "0" ]]
then
# prevent normal file diff
COMPARED="1"
# compare decrypted files
IFS=$'\n' OUTPUT=($(/usr/bin/env diff --color=always -Npu "${TEMPFILE1}" "${TEMPFILE2}"))
# store exit code
EXITCODE="$?"
fi
if [[ -f "${FILE1}" ]]
then
# try to delete the temporary file either way
/usr/bin/env rm -f "${TEMPFILE1}"
fi
if [[ -f "${FILE2}" ]]
then
# try to delete the temporary file either way
/usr/bin/env rm -f "${TEMPFILE2}"
fi
fi
fi
fi
# handle as normal files
if [[ "${COMPARED}" -eq "0" ]]
then
# reset temporary file names
TEMPFILE1="${FILE1}"
TEMPFILE2="${FILE2}"
# just to be sure
COMPARED="1"
# compare decrypted files
IFS=$'\n' OUTPUT=($(/usr/bin/env diff --color=always -Npu "${TEMPFILE1}" "${TEMPFILE2}"))
# store exit code
EXITCODE="$?"
fi
# only handle output if there is any output
if [[ "${#OUTPUT[@]}" -gt "0" ]]
then
if [[ "${MODE1}" != "${MODE2}" ]]
then
echo "old mode ${MODE1}"
echo "new mode ${MODE2}"
fi
# check for the known exit code
if [[ "${EXITCODE}" -eq "1" ]]
then
# check if the output is a diff
if [[ "${OUTPUT[0]:0:3}" == "---" ]]
then
# prepare first output line
if [[ "${FILE1}" == "/dev/null" ]]
then
OUTPUT[0]="--- /dev/null"
else
OUTPUT[0]="--- a/${FILENAME}"
fi
# prepare second output line
if [[ "${FILE2}" == "/dev/null" || ! -f "${FILE2}" ]]
then
OUTPUT[1]="+++ /dev/null"
else
OUTPUT[1]="+++ b/${FILENAME}"
fi
printf "%s\n" "${OUTPUT[@]}" | /usr/bin/env colordiff
else
# prepare placeholders
PLACEHOLDER1=""
PLACEHOLDER2=""
if [[ "${FILE1}" == "/dev/null" ]]
then
PLACEHOLDER1="/dev/null"
else
PLACEHOLDER1="a/${FILENAME}"
fi
if [[ "${FILE2}" == "/dev/null" || ! -f "${FILE2}" ]]
then
PLACEHOLDER2="/dev/null"
else
PLACEHOLDER2="b/${FILENAME}"
fi
echo -e "Binary files ${PLACEHOLDER1} and ${PLACEHOLDER2} differ\n" | /usr/bin/env colordiff
fi
else
# just output the text
printf "%s\n" "${OUTPUT[@]}"
fi
fi