Implement tree strategy for file merging

This commit is contained in:
Juno Takano 2024-08-30 09:49:38 -03:00
parent e0e1920b0b
commit 1b9e519e05
5 changed files with 67 additions and 22 deletions

View file

@ -6,5 +6,5 @@ check() {
log debug "collected bkp files:\n$bkp_files" log debug "collected bkp files:\n$bkp_files"
scan_packages scan_packages
merge_base "$base_files" merge_files "$base_files"
} }

61
src/file/file_merge.sh Normal file
View file

@ -0,0 +1,61 @@
merge_files() {
local base_files="$1"
local strategy="${2:-tree}"
if [ "$strategy" == tree ]; then
log info "[merge_files] Merging with $strategy strategy"
if ! file_scan_tree "$base_files"; then
file_merge_tree "$base_files"
log info "[merge_files] Recursing"
merge_files "$base_files"
fi
fi
}
file_scan_tree() {
local base_files="$1"
for file in $base_files; do
local absolute_path="$(echo "$file" | sed 's/^base//')"
local config_path="$CONFIG_ROOT/$file"
if ! diff "$absolute_path" "$config_path" > /dev/null; then
return 1
fi
done
return 0
}
# TODO Check if files exist before acting
file_merge_tree() {
local base_files="$1"
local strategy=
for file in $base_files; do
log debug "[merge_tree] Processing $file"
local absolute_path="$(echo "$file" | sed 's/^base//')"
log debug "[merge_tree] Absolute path: $absolute_path"
local config_path="$CONFIG_ROOT/$file"
log debug "[merge_tree] Config path: $config_path"
if diff "$absolute_path" "$config_path" > /dev/null; then
log debug "[merge_tree] Files match"
else
log debug "[merge_tree] Files differ"
strategy="$(ask "Differs: $(tildify "$absolute_path")" \
"Overwrite system,Overwrite configuration,Show difference")"
log debug "[merge_tree] Chosen strategy: $strategy"
if [ "$strategy" -eq 1 ]; then
cp -vi "$config_path" "$absolute_path"
elif [ "$strategy" -eq 2 ]; then
cp -vi "$absolute_path" "$config_path"
elif [ "$strategy" -eq 3 ]; then
echo "< $(tildify "$absolute_path") | $(echo "$config_path" | sed "s*$CONFIG_ROOT/**") >"
diff "$absolute_path" "$config_path" || return 0
else
log user 'Invalid choice'
fi
fi
done
}

View file

@ -1,20 +0,0 @@
merge_base() {
local base_files="$1"
local strategy=
for file in $base_files; do
log debug "[merge-base] Processing $file"
local absolute_path="$(echo "$file" | sed 's/^base//')"
log debug "[merge-base] Absolute path: $absolute_path"
local config_path="$CONFIG_ROOT/$file"
log debug "[merge-base] Config path: $config_path"
if diff "$absolute_path" "$config_path" > /dev/null; then
log debug "[merge-base] Files match"
else
log debug "[merge-base] Files differ"
strategy="$(ask "Configuration and system files differ" "Overwrite system,Overwrite config,Show difference")"
log debug "[merge-base] Chosen strategy: $strategy"
fi
done
}

View file

@ -10,4 +10,4 @@
. "$TORI_ROOT/src/package/package_conflict_input_parser.sh" . "$TORI_ROOT/src/package/package_conflict_input_parser.sh"
. "$TORI_ROOT/src/package/update_package_cache.sh" . "$TORI_ROOT/src/package/update_package_cache.sh"
. "$TORI_ROOT/src/file/merge_base.sh" . "$TORI_ROOT/src/file/file_merge.sh"

View file

@ -83,6 +83,10 @@ ask() {
fi fi
} }
tildify() {
echo "$1" | sed "s*$HOME*~*"
}
set_opts() { set_opts() {
local target="$1" local target="$1"
local sign= local sign=