-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-ini
More file actions
executable file
·465 lines (416 loc) · 12.9 KB
/
diff-ini
File metadata and controls
executable file
·465 lines (416 loc) · 12.9 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env bash
# -*- mode: sh; sh-shell: bash; indent-tabs-mode: nil; tab-width: 2 -*-
# vim: ft=bash:et:ts=2:sts=2:sw=2
# code: language=bash insertSpaces=true tabSize=2
# shellcheck shell=bash
#
# Semantic INI diff and merge helper.
declare -r _SCRIPT_PATH="$(cd "$(dirname "$0")"; pwd -P)"
declare -r _SCRIPT_FILE="$(basename "$0")"
# include commons lib
for f in {$_SCRIPT_PATH/,$_SCRIPT_PATH/lib,$HOME/lib}/commons.sh; do
if [[ -f "$f" ]]; then
source "$f"
break
fi
done
if ! command -v "cl::cmd_p" >& /dev/null; then
printf "commons lib not found, exiting ..\n" >&2
exit 1
fi
# set options
set -o errtrace
set -o errexit
set -o pipefail
set -o nounset
(( ${DEBUG_LVL:-0} >= 2 )) && set -o xtrace
IFS=$'\t\n\0'
# traps
trap '_rc=$?; \
printf "ERROR(%s) in %s:%s\n -> %s\n -> %s\n" "${_rc}" \
"${0:-N/A}" "${LINENO:-N/A}" "${FUNCNAME[@]:-N/A}" \
"${BASH_COMMAND:-N/A}"; \
exit $_rc' ERR
trap 'printf "\nINTERRUPT\n"; exit 1' SIGINT SIGTERM
# constants
declare -r _USAGE="${_SCRIPT_FILE} [OPTIONS] FILE1 FILE2"
declare _HELP
! IFS='' read -r -d '' _HELP <<EOF
Usage: $_USAGE
$(cl::fx b)About:$(cl::fx r)
Semantic INI file diff & merge tool. Compares two INI files by section
and key, ignoring order of sections and keys. Numeric values are
normalized for comparison (e.g. 1.6000 == 1.6). Comments and blank
lines are stripped.
$(cl::fx b)Modes:$(cl::fx r)
$(cl::fx b)-d, --diff$(cl::fx r) Show only differences (default)
< = in file1 only / file1 value
> = in file2 only / file2 value
$(cl::fx b)-m, --merge$(cl::fx r) Output file1 as base, enriched with file2 changes
Unchanged keys are output as-is
Changed/added keys marked with < / >
$(cl::fx b)Options:$(cl::fx r)
-i, --inline Inline style for changed values (both on one line)
| key = val1 ;; = val2
Useful for bulk search-and-replace editing
-e, --equal-prefix Prefix unchanged keys with = in merge mode
= key = value (same in both files)
-c, --color Colorize output (< red, > green, | yellow, = dim, sections bold)
-h, --help Show this help
$(cl::fx b)Examples:$(cl::fx r)
${_SCRIPT_FILE} config1.ini config2.ini
${_SCRIPT_FILE} --merge -c base.ini changes.ini
${_SCRIPT_FILE} --merge base.ini changes.ini > merged.ini
EOF
declare -r _HELP
# arguments
declare _mode="diff"
declare _inline=false
declare _equal_prefix=false
declare _color=false
declare _file1=""
declare _file2=""
# parsed data (prefix 1/2 for file1/file2)
declare -A _sections1 _sections2
declare -A _data1 _data2
declare -A _keys1 _keys2
_parse_args() {
if [[ -z "${1:-}" ]]; then
cl::p_usg "${_USAGE}"
exit 1
fi
while [[ -n "${1:-}" ]]; do
case $1 in
-d|--diff)
_mode="diff"
shift
;;
-m|--merge)
_mode="merge"
shift
;;
-i|--inline)
_inline=true
shift
;;
-e|--equal-prefix)
_equal_prefix=true
shift
;;
-c|--color)
_color=true
shift
;;
-h|--help)
printf "%s" "${_HELP}"
exit 0
;;
-*)
cl::p_err "unknown option: $1"
exit 1
;;
*)
break
;;
esac
done
if [[ -z "${1:-}" ]]; then
cl::p_err "missing FILE1 argument"
exit 1
fi
_file1="$1"
shift
if [[ -z "${1:-}" ]]; then
cl::p_err "missing FILE2 argument"
exit 1
fi
_file2="$1"
shift
if [[ -n "${1:-}" ]]; then
cl::p_err "too many arguments"
exit 1
fi
if [[ "$_equal_prefix" == true && "$_mode" != "merge" ]]; then
cl::p_err "--equal-prefix can only be used with --merge mode"
exit 1
fi
if [[ ! -f "$_file1" ]]; then
cl::p_err "file not found: $_file1"
exit 1
fi
if [[ ! -f "$_file2" ]]; then
cl::p_err "file not found: $_file2"
exit 1
fi
}
# Parse an INI file into associative arrays
# Args: $1=file, $2=sections_var_name, $3=data_var_name, $4=keys_var_name
_parse_ini() {
local file="$1"
local -n sections_ref="$2"
local -n data_ref="$3"
local -n keys_ref="$4"
local current_section=""
local line key value
while IFS='' read -r line || [[ -n "$line" ]]; do
# strip leading/trailing whitespace
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
# skip blank lines and comments
[[ -z "$line" ]] && continue
[[ "$line" == \#* || "$line" == \;* ]] && continue
# section header
if [[ "$line" == \[*\] ]]; then
current_section="${line#[}"
current_section="${current_section%]}"
# trim whitespace from section name
current_section="${current_section#"${current_section%%[![:space:]]*}"}"
current_section="${current_section%"${current_section##*[![:space:]]}"}"
sections_ref["$current_section"]=1
continue
fi
# key=value pair (skip lines before any section)
if [[ -z "$current_section" ]]; then
continue
fi
if [[ "$line" == *=* ]]; then
key="${line%%=*}"
value="${line#*=}"
# trim whitespace from key and value
key="${key#"${key%%[![:space:]]*}"}"
key="${key%"${key##*[![:space:]]}"}"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
data_ref["${current_section}${_SEP}${key}"]="$value"
# append key to keys list (newline-separated)
if [[ -n "${keys_ref["$current_section"]+x}" ]]; then
keys_ref["$current_section"]+=$'\n'"$key"
else
keys_ref["$current_section"]="$key"
fi
fi
done < "$file"
}
# Normalize a value for comparison purposes
# Strips trailing zeros from decimal numbers
_normalize_value() {
local val="$1"
# check if it looks like a decimal number (optional sign, digits, dot, digits)
if [[ "$val" =~ ^[+-]?[0-9]+\.[0-9]+$ ]]; then
# strip trailing zeros after decimal point using sed
val="$(printf '%s' "$val" | sed 's/\.\{0,1\}0*$//')"
fi
printf '%s' "$val"
}
# Get sorted unique keys for a section
_sorted_keys() {
local keys_str="$1"
if [[ -z "$keys_str" ]]; then
return
fi
printf '%s\n' "$keys_str" | sort -u
}
# Print a line with optional color
# Args: $1=prefix ("<", ">", ""), $2=text
_print_line() {
local prefix="$1"
local text="$2"
if [[ "$_color" == true ]]; then
case "$prefix" in
"<") printf '%s%s %s%s\n' "$(cl::fx red)" "<" "$text" "$(cl::fx r)" ;;
">") printf '%s%s %s%s\n' "$(cl::fx green)" ">" "$text" "$(cl::fx r)" ;;
"|") printf '%s%s %s%s\n' "$(cl::fx yellow)" "|" "$text" "$(cl::fx r)" ;;
"=") printf '%s%s %s%s\n' "$(cl::fx d)" "=" "$text" "$(cl::fx r)" ;;
*) printf '%s\n' "$text" ;;
esac
else
if [[ -n "$prefix" ]]; then
printf '%s %s\n' "$prefix" "$text"
else
printf '%s\n' "$text"
fi
fi
}
# Print a section header with optional prefix and color
# Args: $1=prefix ("<", ">", ""), $2=section_name
_print_section() {
local prefix="$1"
local section="$2"
if [[ "$_color" == true ]]; then
local bold_section
bold_section="$(cl::fx b)[${section}]$(cl::fx r)"
case "$prefix" in
"<") printf '%s%s %s%s\n' "$(cl::fx red)" "<" "$bold_section" "$(cl::fx r)" ;;
">") printf '%s%s %s%s\n' "$(cl::fx green)" ">" "$bold_section" "$(cl::fx r)" ;;
"=") printf '%s%s %s%s\n' "$(cl::fx d)" "=" "$bold_section" "$(cl::fx r)" ;;
*) printf '%s\n' "$bold_section" ;;
esac
else
if [[ -n "$prefix" ]]; then
printf '%s [%s]\n' "$prefix" "$section"
else
printf '[%s]\n' "$section"
fi
fi
}
# separator for composite keys in associative arrays
declare -r _SEP=$'\x1f'
_diff_ini() {
# collect all section names (union), sorted
local -A all_sections
local section
for section in "${!_sections1[@]}"; do
all_sections["$section"]=1
done
for section in "${!_sections2[@]}"; do
all_sections["$section"]=1
done
local -a sorted_sections
IFS=$'\n' read -r -d '' -a sorted_sections < <(
printf '%s\n' "${!all_sections[@]}" | sort
printf '\0'
) || true
local first_section=true
local key val1 val2 norm1 norm2
local in1 in2
for section in "${sorted_sections[@]}"; do
in1="${_sections1["$section"]+1}"
in2="${_sections2["$section"]+1}"
if [[ -n "$in1" && -z "$in2" ]]; then
# section only in file1
if [[ "$_mode" == "diff" ]]; then
[[ "$first_section" == true ]] || printf '\n'
first_section=false
_print_section "<" "$section"
local keys1_str="${_keys1["$section"]:-}"
while IFS='' read -r key; do
[[ -z "$key" ]] && continue
_print_line "<" "$key = ${_data1["${section}${_SEP}${key}"]}"
done < <(_sorted_keys "$keys1_str")
elif [[ "$_mode" == "merge" ]]; then
# in merge mode, file1-only sections are output as-is (it's the base)
[[ "$first_section" == true ]] || printf '\n'
first_section=false
_print_section "" "$section"
local keys1_str="${_keys1["$section"]:-}"
while IFS='' read -r key; do
[[ -z "$key" ]] && continue
_print_line "" "$key = ${_data1["${section}${_SEP}${key}"]}"
done < <(_sorted_keys "$keys1_str")
fi
continue
fi
if [[ -z "$in1" && -n "$in2" ]]; then
# section only in file2
[[ "$first_section" == true ]] || printf '\n'
first_section=false
_print_section ">" "$section"
local keys2_str="${_keys2["$section"]:-}"
while IFS='' read -r key; do
[[ -z "$key" ]] && continue
_print_line ">" "$key = ${_data2["${section}${_SEP}${key}"]}"
done < <(_sorted_keys "$keys2_str")
continue
fi
# section in both files - compare keys
local -A section_keys
local keys1_str="${_keys1["$section"]:-}"
local keys2_str="${_keys2["$section"]:-}"
# collect all keys for this section
section_keys=()
if [[ -n "$keys1_str" ]]; then
while IFS='' read -r key; do
[[ -z "$key" ]] && continue
section_keys["$key"]=1
done <<< "$keys1_str"
fi
if [[ -n "$keys2_str" ]]; then
while IFS='' read -r key; do
[[ -z "$key" ]] && continue
section_keys["$key"]=1
done <<< "$keys2_str"
fi
local -a sorted_keys
IFS=$'\n' read -r -d '' -a sorted_keys < <(
printf '%s\n' "${!section_keys[@]}" | sort -u
printf '\0'
) || true
# build output for this section
local section_output=""
local has_diff=false
for key in "${sorted_keys[@]}"; do
local has1="${_data1["${section}${_SEP}${key}"]+1}"
local has2="${_data2["${section}${_SEP}${key}"]+1}"
if [[ -n "$has1" && -n "$has2" ]]; then
val1="${_data1["${section}${_SEP}${key}"]}"
val2="${_data2["${section}${_SEP}${key}"]}"
norm1="$(_normalize_value "$val1")"
norm2="$(_normalize_value "$val2")"
if [[ "$norm1" == "$norm2" ]]; then
# same value
if [[ "$_mode" == "merge" ]]; then
section_output+="=$key = $val1"$'\n'
fi
else
# different values
has_diff=true
if [[ "$_inline" == true ]]; then
section_output+="|$key = $val1 ;; = $val2"$'\n'
else
section_output+="<$key = $val1"$'\n'
section_output+=">$key = $val2"$'\n'
fi
fi
elif [[ -n "$has1" && -z "$has2" ]]; then
# key only in file1
has_diff=true
val1="${_data1["${section}${_SEP}${key}"]}"
section_output+="<$key = $val1"$'\n'
else
# key only in file2
has_diff=true
val2="${_data2["${section}${_SEP}${key}"]}"
section_output+=">$key = $val2"$'\n'
fi
done
# output section if there are differences (diff mode) or always (merge mode)
if [[ "$_mode" == "diff" && "$has_diff" == false ]]; then
continue
fi
if [[ "$_mode" == "merge" || "$has_diff" == true ]]; then
[[ "$first_section" == true ]] || printf '\n'
first_section=false
local section_prefix=""
if [[ "$_equal_prefix" == true && "$has_diff" == false ]]; then
section_prefix="="
fi
_print_section "$section_prefix" "$section"
# print buffered lines
while IFS='' read -r line; do
[[ -z "$line" ]] && continue
local prefix="${line:0:1}"
local content="${line:1}"
case "$prefix" in
"=") if [[ "$_equal_prefix" == true ]]; then
_print_line "=" "$content"
else
_print_line "" "$content"
fi ;;
"<") _print_line "<" "$content" ;;
">") _print_line ">" "$content" ;;
"|") _print_line "|" "$content" ;;
esac
done <<< "$section_output"
fi
done
}
_main() {
_parse_args "$@"
# parse both files
_parse_ini "$_file1" _sections1 _data1 _keys1
_parse_ini "$_file2" _sections2 _data2 _keys2
# run diff/merge
_diff_ini
}
_main "$@"
exit 0