aboutsummaryrefslogtreecommitdiff
blob: 9cdd9f61ea8a315fcd71c74826361e0e2ed94a88 (plain)
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
# -*-eselect-*-  vim: ft=eselect
# Copyright 2005-2012 Gentoo Foundation
# Distributed under the terms of the GNU GPL version 2 or later
# $Id$

inherit portage

DESCRIPTION="Handle configuration file merges"
MAINTAINER="eselect@gentoo.org"

show_extra_help_text() {
	cat <<ENDOFTEXT
This tool can be used to automatically or manually merge configuration files
which have been created by portage.

The 'interactive' action will enter interactive mode.

The 'list' action will show all configuration files which need updating.

The 'accept' action will accept the proposed changes to one or more
configuration files. It accepts one or more parameters, each of which can either
be the names of files to overwrite, a number corresponding to the value shown by
the 'list' action, or two numbers separated by a hyphen -- for example, "3-7" --
which is treated as a range.

The 'reject' action will discard (reject) the proposed changes to one or more
configuration files. It takes parameters in the same way as 'accept'.

The 'display' action shows (as a unified diff) the proposed update for one or
more files.

The 'accept-all' action accepts all proposed changes to all configuration files.
Similarly, 'reject-all' rejects all changes. Use with caution.
ENDOFTEXT
}

config_protect_dirs() {
	if [[ -z ${CONFIG_UPDATE_DIRS} ]] ; then
		export CONFIG_UPDATE_DIRS=$(envvar sys-devel/gcc CONFIG_PROTECT )
	fi
	echo ${CONFIG_UPDATE_DIRS}
}

find_targets() {
	local dir targets
	for dir in $(config_protect_dirs ) ; do
		[[ -d ${dir} ]] || continue
		find ${dir} -iname '._cfg????_*' | \
			sed -e 's,^\(.*\)\._cfg...._\(.*\),\1\2,g' | sort -u
	done
}

generic_handle_one_file() {
	local action_name="${1}" apply_cmd="${2}"
	shift 2

	local targets=( ) has_targets=0

	# check whether we've been given any parameters
	if [[ ${#@} < 1 ]] ; then
		die -q "You should give me at least one file to work with"
	else
		local file_raw file

		# for each parameter...
		for file_raw in "$@" ; do
			[[ -z ${file_raw} ]] && continue

			# do we have a number or a name?
			if is_number "${file_raw}" ; then
				# number. decode
				if [[ ${has_targets} == 0 ]] ; then
					targets=( $(find_targets ) )
					has_targets=1
				fi
				file=${targets[${file_raw} - 1]}
			else
				# name. don't decode
				file=${file_raw}
			fi

			# sanity checks
			[[ -z ${file} ]] && die -q "Sorry, ${file_raw} seems to be invalid"
			[[ -e ${file} ]] || die -q "Sorry, ${file} doesn't seem to exist"

			# find the files to discard
			local cfgname="._cfg????_$(basename "${file}")"
			local dirname="$(canonicalise -f $(dirname ${file} ))"
			local kfiles
			kfiles=( $(find "${dirname}" -maxdepth 1 -type f \
					-iname "${cfgname}") )

			if [[ -n ${kfiles[@]//[[:space:]]} ]] ; then
				# files found. work on them
				${apply_cmd} "${kfiles}"
			else
				# no files found
				write_error_msg "Couldn't find any files to ${action_name}."
				write_error_msg "I was looking in: ${dirname}"
				write_error_msg "For files named:  ${cfgname}"
				die -q "Can't ${action_name} ${file_raw}"
			fi
		done
	fi
}

generic_handle_all() {
	local verb="${1}" action="${2}"
	shift 2

	local targets target
	write_list_start "${verb} changes to configuration files:"
	targets=( $(find_targets ) )
	if [[ ${#targets[@]} > 0 ]] ; then
		for target in ${targets[@]} ; do
			write_kv_list_entry "${target}" ""
			do_action config "${action}" "${target}"
		done
	else
		write_kv_list_entry "No out of date files found"
	fi
}

### list action ###

describe_list() {
	echo "List files which need merging"
}

do_list() {
	targets=( $(find_targets ) )
	write_list_start "Configuration files needing action:"
	if [[ ${#targets[@]} > 0 ]] ; then
		write_numbered_list "${targets[@]}"
	else
		write_kv_list_entry "No out of date files found"
	fi
}

### reject action ###

describe_reject() {
	echo "Reject updates to the specified files"
}

do_reject() {
	generic_handle_one_file "reject" "reject_handler" "$@"
}

reject_handler() {
	rm --interactive "$@"
}

### accept action ###

describe_accept() {
	echo "Accept changes to the specified files"
}

do_accept() {
	generic_handle_one_file "accept" "accept_handler" "$@"
}

accept_handler() {
	local file
	for file in "$@" ; do
		local sfile="${file}" dfile="${file/._cfg????_}"
		echo "Replacing ${dfile} with ${sfile}..."
		mv --interactive "${sfile}" "${dfile}"
	done
}

### reject-all action ###

describe_reject-all() {
	echo "Reject all updates ($(highlight_warning dangerous))"
}

do_reject-all() {
	generic_handle_all "Rejecting" "reject"
}

### accept-all action ###

describe_accept-all() {
	echo "Accept all updates ($(highlight_warning dangerous))"
}

do_accept-all() {
	generic_handle_all "Accepting" "accept"
}

### merge action ###

describe_merge() {
	echo "Interactively merge changes to the specified files"
}

do_merge() {
	generic_handle_one_file "merge" "merge_handler" "$@"
}

merge_handler() {
	local file
	for file in "$@" ; do
		local sfile="${file}" dfile="${file/._cfg????_}"
		echo "Merging ${dfile} with ${sfile}..."
		merge_prog ${dfile} ${sfile}
	done
}

merge_prog() {
	if type vimdiff &>/dev/null ; then
		vimdiff "$@"
	else
		write_error_msg "Couldn't find a suitable merge program."
		write_error_msg "Currently I know how to use 'vimdiff', which you can"
		write_error_msg "get by installing app-editors/vim."
		die -q "No merge program found"
	fi

}

### merge-all action ###

describe_merge-all() {
	echo "Interactively merge all changes"
}

do_merge-all() {
	generic_handle_all "Merging" "merge"
}

### display action ###

describe_display() {
	echo "Display the proposed changes to the specified files"
}

do_display() {
	generic_handle_one_file "display" "display_handler" "$@"
}

display_handler() {
	local file
	for file in "$@" ; do
		local sfile="${file}" dfile="${file/._cfg????_}"
		echo "Proposed changes to ${dfile}:"
		diff_prog -u ${dfile} ${sfile}
	done
}

diff_prog() {
	if type colordiff &>/dev/null ; then
		colordiff "$@"
	else
		diff "$@"
	fi
}
### display-all action ###

describe_display-all() {
	echo "Display all proposed updates"
}

do_display-all() {
	generic_handle_all "Displaying" "display"
}

### interactive action ###

describe_interactive() {
	echo "Enter interactive mode"
}

do_interactive() {
	local targets command show_menu=yes show_list=yes

	config_protect_dirs 1>/dev/null
	while true ; do
		targets=( $(find_targets) )

		# Are we finished?
		if [[ ${#targets[@]} == 0 ]] ; then
			write_list_start "Nothing left to do."
			break
		fi

		# Should we show a file list?
		if [[ -n ${show_list} ]] ; then
			show_list=
			write_list_start "Configuration files needing action:"
			write_numbered_list "${targets[@]}"
			echo
		fi

		# Should we show a menu?
		if [[ -n ${show_menu} ]] ; then
			show_menu=
			write_list_start "Enter command:"
			write_kv_list_entry "<number>" "Work with the specified file"
			write_kv_list_entry "$(highlight d)isplay <number>" "Display proposed change"
			write_kv_list_entry "$(highlight a)ccept <number>"  "Accept proposed change"
			write_kv_list_entry "$(highlight r)eject <number>"  "Reject proposed change"
			write_kv_list_entry "$(highlight m)erge <number>"   "Merge proposed change"
			echo
			write_kv_list_entry "accept-all" \
				"Accept all changes ($(highlight_warning dangerous))"
			write_kv_list_entry "reject-all" \
				"Reject all changes ($(highlight_warning dangerous))"
			echo
			write_kv_list_entry "$(highlight l)ist" "Show file list"
			write_kv_list_entry "$(highlight h)elp" "Show this menu again"
			write_kv_list_entry "$(highlight q)uit" "Exit the program"
			echo
		else
			write_list_start "Enter command ($(highlight h)elp to show menu)"
		fi
		echo -n "> "
		read command

		case ${command%%[[:space:]]*} in
			q|qu|qui|quit)
				break;
				;;
			h|he|hel|help)
				show_menu=yes
				;;
			l|li|lis|list)
				show_list=yes
				;;
			d|di|dis|disp|displ|displa|display)
				local args=${command#*[[:space:]]}
				if [[ -z ${command//[^[:space:]]} ]] ; then
					write_error_msg "You didn't tell me which file to display"
				else
					do_action config display ${command#*[[:space:]]}
				fi
				;;
			a|ac|acc|acce|accep|accept)
				local args=${command#*[[:space:]]}
				if [[ -z ${command//[^[:space:]]} ]] ; then
					write_error_msg "You didn't tell me which file to accept"
				else
					do_action config accept ${command#*[[:space:]]}
				fi
				show_list=yes
				;;
			r|re|rej|reje|rejec|reject)
				local args=${command#*[[:space:]]}
				if [[ -z ${command//[^[:space:]]} ]] ; then
					write_error_msg "You didn't tell me which file to reject"
				else
					do_action config reject ${command#*[[:space:]]}
				fi
				show_list=yes
				;;
			m|me|mer|merg|merge)
				local args=${command#*[[:space:]]}
				if [[ -z ${command//[^[:space:]]} ]] ; then
					write_error_msg "You didn't tell me which file to merge"
				else
					do_action config merge ${command#*[[:space:]]}
				fi
				show_list=yes
				;;
			accept-all)
				do_action config accept-all
				show_list=yes
				;;
			reject-all)
				do_action config reject-all
				show_list=yes
				;;
			+([[:digit:]])*(+([[:space:]])+([[:digit:]])))
				while true ; do
					local item ok=
					for item in ${command} ; do
						[[ -z ${targets[${item}-1]} ]] && continue
						[[ -z ${ok} ]] && write_list_start "Selected files:"
						ok=yes
						write_numbered_list_entry ${item} ${targets[${item}-1]}
					done
					if [[ -z ${ok} ]] ; then
						write_error_msg "Sorry, ${command} not valid"
						show_list=yes
					else
						echo
						write_list_start "Enter subcommand:"
						write_kv_list_entry "$(highlight d)isplay" "Display proposed change"
						write_kv_list_entry "$(highlight a)ccept"  "Accept proposed change"
						write_kv_list_entry "$(highlight r)eject"  "Reject proposed change"
						write_kv_list_entry "$(highlight m)erge"   "Merge proposed change"
						write_kv_list_entry "$(highlight q)uit"    "Return to parent menu"
						echo
						echo -n "> "
						read subcommand
						case ${subcommand} in
							q|qu|qui|quit)
								break;
								;;
							d|di|dis|disp|displ|displa|display)
								do_action config display ${command}
								;;
							a|ac|acc|acce|accep|accept)
								do_action config accept ${command}
								show_list=yes
								break
								;;
							r|re|rej|reje|rejec|reject)
								do_action config reject ${command}
								show_list=yes
								break
								;;
							m|me|mer|merg|merge)
								do_action config reject ${command}
								show_list=yes
								break
								;;
							*)
								write_error_msg "Sorry, ${subcommand} not understood"
								;;
						esac
					fi
				done
				;;
			*)
				write_error_msg "Sorry, ${command} not understood"
				;;
		esac
	done
}