From 1b9e519e05c853a21b658e628c028e9f9abcb833 Mon Sep 17 00:00:00 2001 From: jutty Date: Fri, 30 Aug 2024 09:49:38 -0300 Subject: [PATCH] Implement tree strategy for file merging --- src/check.sh | 2 +- src/file/file_merge.sh | 61 ++++++++++++++++++++++++++++++++++++++++++ src/file/merge_base.sh | 20 -------------- src/index.sh | 2 +- src/utility.sh | 4 +++ 5 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 src/file/file_merge.sh delete mode 100644 src/file/merge_base.sh diff --git a/src/check.sh b/src/check.sh index 598802a..b3923b7 100644 --- a/src/check.sh +++ b/src/check.sh @@ -6,5 +6,5 @@ check() { log debug "collected bkp files:\n$bkp_files" scan_packages - merge_base "$base_files" + merge_files "$base_files" } diff --git a/src/file/file_merge.sh b/src/file/file_merge.sh new file mode 100644 index 0000000..7bc4d0a --- /dev/null +++ b/src/file/file_merge.sh @@ -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 +} diff --git a/src/file/merge_base.sh b/src/file/merge_base.sh deleted file mode 100644 index 42f9d0e..0000000 --- a/src/file/merge_base.sh +++ /dev/null @@ -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 -} diff --git a/src/index.sh b/src/index.sh index 9899f9a..95c9f91 100644 --- a/src/index.sh +++ b/src/index.sh @@ -10,4 +10,4 @@ . "$TORI_ROOT/src/package/package_conflict_input_parser.sh" . "$TORI_ROOT/src/package/update_package_cache.sh" -. "$TORI_ROOT/src/file/merge_base.sh" +. "$TORI_ROOT/src/file/file_merge.sh" diff --git a/src/utility.sh b/src/utility.sh index 4ff19c4..f713db0 100644 --- a/src/utility.sh +++ b/src/utility.sh @@ -83,6 +83,10 @@ ask() { fi } +tildify() { + echo "$1" | sed "s*$HOME*~*" +} + set_opts() { local target="$1" local sign=