From 63614da1dd709d27a272a68ff62fed4f1f8fee93 Mon Sep 17 00:00:00 2001 From: H0mire Date: Fri, 3 May 2024 18:46:03 +0200 Subject: [PATCH 01/26] added updatescript --- update-dots.sh | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 update-dots.sh diff --git a/update-dots.sh b/update-dots.sh new file mode 100644 index 000000000..2b6395e0f --- /dev/null +++ b/update-dots.sh @@ -0,0 +1,86 @@ +#!/bin/bash +# This script updates the dotfiles by fetching the latest version from the Git repository and then replacing files +# that have not been modified by the user to preserve changes. The remaining files will be replaced with the new ones. + +cd "$(dirname "$0")" +export base="$(pwd)" + +function get_checksum() { + # Get the checksum of a specific file + md5sum "$1" | awk '{print $1}' +} + + +# Define the folders to update +folders=(".config" ".local") + +# Then check which files have been modified since the last update +modified_files=() + +# Find all files in the specified folders and their subfolders +while IFS= read -r -d '' file; do + # Calculate checksums + base_checksum=$(get_checksum "$base/$file") + home_checksum=$(get_checksum "$HOME/$file") + # Compare checksums and add to modified_files if necessary + if [[ $base_checksum != $home_checksum ]]; then + modified_files+=("$file") + fi +done < <(find "${folders[@]}" -type f -print0) + + +echo "Modified files: ${modified_files[@]}" + +# Output all modified files +if [[ ${#modified_files[@]} -gt 0 ]]; then + echo "The following files have been modified since the last update:" + for file in "${modified_files[@]}"; do + echo "$file" + done +else + echo "No files have been modified since the last update. All files will be replaced." +fi + +# Ask if the user wants to keep them +read -p "Do you want to keep these files untouched? [y/N] " -n 1 -r +echo +if [[ $REPLY =~ ^[Yy]$ ]]; then + echo "Keeping modified files." +else + echo "Replacing all files." + modified_files=() +fi + +# Then update the repository +git pull + +# Now only replace the files that are not modified by the user +for folder in "${folders[@]}"; do + # Find all files (including those in subdirectories) and copy them + find "$folder" -type f -print0 | while IFS= read -r -d '' file; do + if [[ -f "$file" ]]; then + if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then + # Construct the destination path + destination="$HOME/$file" + # Copy the file + cp -rf "$base/$file" "$destination" + fi + fi + done +done + +# Add the new files, because maybe the update added new files +for folder in "${folders[@]}"; do + # Find all files (including those in subdirectories) and copy them + find "$folder" -type f -print0 | while IFS= read -r -d '' file; do + if [[ ! -f "$HOME/$file" ]]; then + echo "Adding new file: $file" + # Construct the destination path + destination="$HOME/$file" + # Copy the file + cp -rf "$base/$file" "$destination" + fi + done +done + + From e75021b473e20f84ac34e12214629fdb5a8ca37d Mon Sep 17 00:00:00 2001 From: H0mire Date: Fri, 3 May 2024 18:57:36 +0200 Subject: [PATCH 02/26] made it more secure --- update-dots.sh | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 2b6395e0f..824eda9c5 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -10,14 +10,20 @@ function get_checksum() { md5sum "$1" | awk '{print $1}' } - # Define the folders to update folders=(".config" ".local") +exclude_folders=("/home/janik/.config/hypr/custom") + +# Build the exclude string for find command +exclude_string="" +for folder in "${exclude_folders[@]}"; do + exclude_string+="! -path $folder -prune -o " +done # Then check which files have been modified since the last update modified_files=() -# Find all files in the specified folders and their subfolders +# Find all files in the specified folders and their subfolders, excluding specified folders while IFS= read -r -d '' file; do # Calculate checksums base_checksum=$(get_checksum "$base/$file") @@ -26,8 +32,7 @@ while IFS= read -r -d '' file; do if [[ $base_checksum != $home_checksum ]]; then modified_files+=("$file") fi -done < <(find "${folders[@]}" -type f -print0) - +done < <(eval find "${folders[@]}" -type f $exclude_string -print0) echo "Modified files: ${modified_files[@]}" @@ -38,7 +43,12 @@ if [[ ${#modified_files[@]} -gt 0 ]]; then echo "$file" done else - echo "No files have been modified since the last update. All files will be replaced." + echo "No files found that have been modified since the last installation/update. All files will be replaced. Are you sure you want to continue? [y/N] " + read -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + exit 1 + fi fi # Ask if the user wants to keep them @@ -57,14 +67,12 @@ git pull # Now only replace the files that are not modified by the user for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them - find "$folder" -type f -print0 | while IFS= read -r -d '' file; do - if [[ -f "$file" ]]; then - if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then - # Construct the destination path - destination="$HOME/$file" - # Copy the file - cp -rf "$base/$file" "$destination" - fi + find "$folder" -type f $exclude_string -print0 | while IFS= read -r -d '' file; do + if [[ -f "$file" && ! " ${modified_files[@]} " =~ " ${file} " ]]; then + # Construct the destination path + destination="$HOME/$file" + # Copy the file + cp -rf "$base/$file" "$destination" fi done done @@ -72,7 +80,7 @@ done # Add the new files, because maybe the update added new files for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them - find "$folder" -type f -print0 | while IFS= read -r -d '' file; do + find "$folder" -type f $exclude_string -print0 | while IFS= read -r -d '' file; do if [[ ! -f "$HOME/$file" ]]; then echo "Adding new file: $file" # Construct the destination path @@ -82,5 +90,3 @@ for folder in "${folders[@]}"; do fi done done - - From 8805155fa8423982bc692019c0903ed921b1e85d Mon Sep 17 00:00:00 2001 From: H0mire Date: Fri, 3 May 2024 19:05:17 +0200 Subject: [PATCH 03/26] more save script --- update-dots.sh | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 824eda9c5..770f122ed 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -10,20 +10,15 @@ function get_checksum() { md5sum "$1" | awk '{print $1}' } + # Define the folders to update folders=(".config" ".local") -exclude_folders=("/home/janik/.config/hypr/custom") - -# Build the exclude string for find command -exclude_string="" -for folder in "${exclude_folders[@]}"; do - exclude_string+="! -path $folder -prune -o " -done +# exclude_folders=("/home/janik/.config/hypr/custom") TODO: Implement exclude folders # Then check which files have been modified since the last update modified_files=() -# Find all files in the specified folders and their subfolders, excluding specified folders +# Find all files in the specified folders and their subfolders while IFS= read -r -d '' file; do # Calculate checksums base_checksum=$(get_checksum "$base/$file") @@ -32,7 +27,8 @@ while IFS= read -r -d '' file; do if [[ $base_checksum != $home_checksum ]]; then modified_files+=("$file") fi -done < <(eval find "${folders[@]}" -type f $exclude_string -print0) +done < <(find "${folders[@]}" -type f -print0) + echo "Modified files: ${modified_files[@]}" @@ -43,18 +39,19 @@ if [[ ${#modified_files[@]} -gt 0 ]]; then echo "$file" done else - echo "No files found that have been modified since the last installation/update. All files will be replaced. Are you sure you want to continue? [y/N] " + echo "No files found that have been modified since the last update. All files will be replaced. Are you sure you want to continue? [Y/n] " read -n 1 -r echo - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - exit 1 + if [[ ! $REPLY =~ ^[Nn]$ ]]; then + echo "Exiting." + exit 0 fi fi # Ask if the user wants to keep them -read -p "Do you want to keep these files untouched? [y/N] " -n 1 -r +read -p "Do you want to keep these files untouched? [Y/n] " -n 1 -r echo -if [[ $REPLY =~ ^[Yy]$ ]]; then +if [[ ! $REPLY =~ ^[Nn]$ ]]; then echo "Keeping modified files." else echo "Replacing all files." @@ -67,12 +64,14 @@ git pull # Now only replace the files that are not modified by the user for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them - find "$folder" -type f $exclude_string -print0 | while IFS= read -r -d '' file; do - if [[ -f "$file" && ! " ${modified_files[@]} " =~ " ${file} " ]]; then - # Construct the destination path - destination="$HOME/$file" - # Copy the file - cp -rf "$base/$file" "$destination" + find "$folder" -type f -print0 | while IFS= read -r -d '' file; do + if [[ -f "$file" ]]; then + if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then + # Construct the destination path + destination="$HOME/$file" + # Copy the file + cp -rf "$base/$file" "$destination" + fi fi done done @@ -80,7 +79,7 @@ done # Add the new files, because maybe the update added new files for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them - find "$folder" -type f $exclude_string -print0 | while IFS= read -r -d '' file; do + find "$folder" -type f -print0 | while IFS= read -r -d '' file; do if [[ ! -f "$HOME/$file" ]]; then echo "Adding new file: $file" # Construct the destination path @@ -90,3 +89,5 @@ for folder in "${folders[@]}"; do fi done done + + From 336e867d06476b73a794ec9eefa94c0f7e5197a7 Mon Sep 17 00:00:00 2001 From: H0mire Date: Fri, 3 May 2024 19:06:49 +0200 Subject: [PATCH 04/26] besser --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index 770f122ed..99dcb002c 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -13,7 +13,7 @@ function get_checksum() { # Define the folders to update folders=(".config" ".local") -# exclude_folders=("/home/janik/.config/hypr/custom") TODO: Implement exclude folders +# exclude_folders=(".config/hypr/custom") TODO: Implement exclude folders # Then check which files have been modified since the last update modified_files=() From a6ef4352f2e1429213cf062498cfda569eac3a77 Mon Sep 17 00:00:00 2001 From: H0mire Date: Fri, 3 May 2024 19:15:35 +0200 Subject: [PATCH 05/26] now with exclude folders --- update-dots.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 99dcb002c..e23090c12 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -5,15 +5,28 @@ cd "$(dirname "$0")" export base="$(pwd)" + +# Define the folders to update +folders=(".config" ".local") +exclude_folders=(".config/hypr/custom") + function get_checksum() { # Get the checksum of a specific file md5sum "$1" | awk '{print $1}' } +function file_in_exclude_folders() { + # Check if a file is in the exclude_folders + for exclude_folder in "${exclude_folders[@]}"; do + if [[ $1 == $exclude_folder* ]]; then + return 0 + fi + done + return 1 +} + + -# Define the folders to update -folders=(".config" ".local") -# exclude_folders=(".config/hypr/custom") TODO: Implement exclude folders # Then check which files have been modified since the last update modified_files=() @@ -65,7 +78,7 @@ git pull for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them find "$folder" -type f -print0 | while IFS= read -r -d '' file; do - if [[ -f "$file" ]]; then + if [[ -f "$file" ]] && ! file_in_exclude_folders "$file"; then if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path destination="$HOME/$file" @@ -80,7 +93,7 @@ done for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them find "$folder" -type f -print0 | while IFS= read -r -d '' file; do - if [[ ! -f "$HOME/$file" ]]; then + if [[ ! -f "$HOME/$file" ]] && ! file_in_exclude_folders "$file"; then echo "Adding new file: $file" # Construct the destination path destination="$HOME/$file" From 8dd89d24d7d502b111ebd67ba10c5c26aeeab1e5 Mon Sep 17 00:00:00 2001 From: H0mire Date: Fri, 3 May 2024 19:42:13 +0200 Subject: [PATCH 06/26] removed unnecessary loop --- update-dots.sh | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index e23090c12..b637f0dbe 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -89,18 +89,3 @@ for folder in "${folders[@]}"; do done done -# Add the new files, because maybe the update added new files -for folder in "${folders[@]}"; do - # Find all files (including those in subdirectories) and copy them - find "$folder" -type f -print0 | while IFS= read -r -d '' file; do - if [[ ! -f "$HOME/$file" ]] && ! file_in_exclude_folders "$file"; then - echo "Adding new file: $file" - # Construct the destination path - destination="$HOME/$file" - # Copy the file - cp -rf "$base/$file" "$destination" - fi - done -done - - From cce845ad49aa0b5e3e9921a16cb33d456505bb8b Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 09:37:32 +0200 Subject: [PATCH 07/26] update based on https://github.com/end-4/dots-hyprland/pull/473#issuecomment-2094060995 --- update-dots.sh | 77 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index b637f0dbe..07feab80b 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -2,23 +2,24 @@ # This script updates the dotfiles by fetching the latest version from the Git repository and then replacing files # that have not been modified by the user to preserve changes. The remaining files will be replaced with the new ones. +set -e cd "$(dirname "$0")" export base="$(pwd)" -# Define the folders to update +# Define paths to update folders=(".config" ".local") -exclude_folders=(".config/hypr/custom") +excludes=(".config/hypr/custom", ".config/ags/user_options.js", ".config/hypr/hyprland.conf") function get_checksum() { # Get the checksum of a specific file md5sum "$1" | awk '{print $1}' } -function file_in_exclude_folders() { +function file_in_excludes() { # Check if a file is in the exclude_folders - for exclude_folder in "${exclude_folders[@]}"; do - if [[ $1 == $exclude_folder* ]]; then + for exc in "${excludes[@]}"; do + if [[ $1 == $exc* ]]; then return 0 fi done @@ -28,7 +29,7 @@ function file_in_exclude_folders() { -# Then check which files have been modified since the last update +# Then check which files have been modified by the user since the last update to preserve user configurations modified_files=() # Find all files in the specified folders and their subfolders @@ -61,29 +62,79 @@ else fi fi +echo "Do you want to keep these files untouched?" +echo "[y] Yes, keep them." +echo "[n] No, replace them." +echo "[i] Check the files individually." # Ask if the user wants to keep them -read -p "Do you want to keep these files untouched? [Y/n] " -n 1 -r +read -p "Answer: " -n 1 -r echo -if [[ ! $REPLY =~ ^[Nn]$ ]]; then - echo "Keeping modified files." -else +if [[ $REPLY =~ ^[Nn]$ ]]; then echo "Replacing all files." modified_files=() +elif [[ $REPLY =~ ^[Ii]$ ]]; then + for file in "${modified_files[@]}"; do + echo "Do you want to keep $file untouched?" + echo "[y] Yes, keep it." + echo "[n] No, replace it." + read -p "Answer: " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Nn]$ ]]; then + echo "Keeping $file." + else + modified_files=("${modified_files[@]/$file}") + fi + done +else + echo "Keeping every modified file" fi # Then update the repository -git pull +if git pull; then + echo "Git pull successful." +else + echo "Git pull failed. Consider recloning the project or resolving conflicts manually." + echo "Should I clone the repository to a temporary folder in chache and copy the files from there? [Y/n] " + read -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo "Exiting..." + exit 1 + fi + + temp_folder=$(mktemp -d) + git clone https://github.com/end-4/dots-hyprland/ "$temp_folder" + # Replace the existing dotfiles with the new ones + for folder in "${folders[@]}"; do + # Find all files (including those in subdirectories) and copy them + find "$temp_folder/$folder" -type f -print0 | while IFS= read -r -d '' file; do + if [[ -f "$file" ]] && ! file_in_excludes "$file"; then + # Construct the destination path + destination="$HOME/$file" + # Create the destination folder if it doesn't exist + mkdir -p "$(dirname "$destination")" + # Copy the file + cp -f "$file" "$destination" + fi + done + done + echo "New dotfiles have been copied. Cleaning up temporary folder." + rm -rf "$temp_folder" +fi + # Now only replace the files that are not modified by the user for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them find "$folder" -type f -print0 | while IFS= read -r -d '' file; do - if [[ -f "$file" ]] && ! file_in_exclude_folders "$file"; then + if [[ -f "$file" ]] && ! file_in_excludes "$file"; then if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path destination="$HOME/$file" # Copy the file - cp -rf "$base/$file" "$destination" + # Create the destination folder if it doesn't exist + mkdir -p "$(dirname "$destination")" + cp -f "$base/$file" "$destination" fi fi done From 6e1a822fc1191fedd67954792348789fadda71ad Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 09:40:46 +0200 Subject: [PATCH 08/26] changed tempfolder path --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index 07feab80b..b2679708b 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -102,7 +102,7 @@ else exit 1 fi - temp_folder=$(mktemp -d) + temp_folder=$(mktemp -d -p /cache) git clone https://github.com/end-4/dots-hyprland/ "$temp_folder" # Replace the existing dotfiles with the new ones for folder in "${folders[@]}"; do From b8c5c7a09921dfe30338564bb17901790282f1d4 Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 10:16:43 +0200 Subject: [PATCH 09/26] now adds new folders --- update-dots.sh | 57 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index b2679708b..dd25d1e9f 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -26,17 +26,21 @@ function file_in_excludes() { return 1 } - - - # Then check which files have been modified by the user since the last update to preserve user configurations modified_files=() # Find all files in the specified folders and their subfolders while IFS= read -r -d '' file; do + # If the file is not in the home directory, skip it + if [[ ! -f "$HOME/$file" ]]; then + continue + fi + # Calculate checksums base_checksum=$(get_checksum "$base/$file") home_checksum=$(get_checksum "$HOME/$file") + + # Compare checksums and add to modified_files if necessary if [[ $base_checksum != $home_checksum ]]; then modified_files+=("$file") @@ -45,6 +49,7 @@ done < <(find "${folders[@]}" -type f -print0) echo "Modified files: ${modified_files[@]}" +echo "Excluded files and folders: ${excludes[@]}" # Output all modified files if [[ ${#modified_files[@]} -gt 0 ]]; then @@ -54,7 +59,7 @@ if [[ ${#modified_files[@]} -gt 0 ]]; then done else echo "No files found that have been modified since the last update. All files will be replaced. Are you sure you want to continue? [Y/n] " - read -n 1 -r + read -r echo if [[ ! $REPLY =~ ^[Nn]$ ]]; then echo "Exiting." @@ -67,12 +72,14 @@ echo "[y] Yes, keep them." echo "[n] No, replace them." echo "[i] Check the files individually." # Ask if the user wants to keep them -read -p "Answer: " -n 1 -r +read -p "Answer: " -r echo if [[ $REPLY =~ ^[Nn]$ ]]; then echo "Replacing all files." modified_files=() elif [[ $REPLY =~ ^[Ii]$ ]]; then + new_modified_files=() + replaced_files=() for file in "${modified_files[@]}"; do echo "Do you want to keep $file untouched?" echo "[y] Yes, keep it." @@ -81,10 +88,33 @@ elif [[ $REPLY =~ ^[Ii]$ ]]; then echo if [[ ! $REPLY =~ ^[Nn]$ ]]; then echo "Keeping $file." + new_modified_files+=("$file") else - modified_files=("${modified_files[@]/$file}") + replaced_files+=("$file") fi done + modified_files=("${new_modified_files[@]}") + # verify the files that will be kept + echo "_____________________________________________________" + echo "These User configured/modifed files will be kept:" + for file in "${modified_files[@]}"; do + echo "$file" + done + echo "_____________________________________________________" + echo "These User configured/modifed files will be replaced:" + for file in "${replaced_files[@]}"; do + echo "$file" + done + echo "_____________________________________________________" + echo "Do you want to continue?" + echo "[y] Yes, continue." + echo "[n] No, exit." + read -p "Answer: " -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo "Exiting..." + exit 0 + fi else echo "Keeping every modified file" fi @@ -93,16 +123,18 @@ fi if git pull; then echo "Git pull successful." else + # If the pull failed, clone the repository to a temporary folder and copy the files from there echo "Git pull failed. Consider recloning the project or resolving conflicts manually." - echo "Should I clone the repository to a temporary folder in chache and copy the files from there? [Y/n] " - read -n 1 -r + echo "Should I clone the repository to a temporary folder in cache and copy the files from there? [y/N] " + read -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Exiting..." exit 1 fi - temp_folder=$(mktemp -d -p /cache) + mkdir -p ./cache + temp_folder=$(mktemp -d -p ./cache) git clone https://github.com/end-4/dots-hyprland/ "$temp_folder" # Replace the existing dotfiles with the new ones for folder in "${folders[@]}"; do @@ -126,7 +158,12 @@ fi # Now only replace the files that are not modified by the user for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them - find "$folder" -type f -print0 | while IFS= read -r -d '' file; do + find "$folder" -print0 | while IFS= read -r -d '' file; do + # if the file is a directory, ensure it exists in the home directory + if [[ -d "$file" ]]; then + mkdir -p "$HOME/$file" + fi + # Check if the file is a regular file and not in the exclude_folders if [[ -f "$file" ]] && ! file_in_excludes "$file"; then if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path From fececd14a2ef4cd3d0f0b19cd5fb3252230a1331 Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 10:22:31 +0200 Subject: [PATCH 10/26] marked Y as default --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index dd25d1e9f..ecb059e79 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -68,7 +68,7 @@ else fi echo "Do you want to keep these files untouched?" -echo "[y] Yes, keep them." +echo "[Y] Yes, keep them." echo "[n] No, replace them." echo "[i] Check the files individually." # Ask if the user wants to keep them From 05a220bd7ae05d5dddf2838911ef820ea9ec4426 Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 10:24:16 +0200 Subject: [PATCH 11/26] again --- update-dots.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index ecb059e79..e12d65ac0 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -82,7 +82,7 @@ elif [[ $REPLY =~ ^[Ii]$ ]]; then replaced_files=() for file in "${modified_files[@]}"; do echo "Do you want to keep $file untouched?" - echo "[y] Yes, keep it." + echo "[Y] Yes, keep it." echo "[n] No, replace it." read -p "Answer: " -n 1 -r echo @@ -108,7 +108,7 @@ elif [[ $REPLY =~ ^[Ii]$ ]]; then echo "_____________________________________________________" echo "Do you want to continue?" echo "[y] Yes, continue." - echo "[n] No, exit." + echo "[N] No, exit." read -p "Answer: " -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then From 328013fd8fc04d695990c0bea5581bf3e4cde2c7 Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 11:10:00 +0200 Subject: [PATCH 12/26] made so it checks if repo is already up to date since, no checksum cache is implemented yet --- update-dots.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/update-dots.sh b/update-dots.sh index e12d65ac0..3a4218ea8 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -26,6 +26,19 @@ function file_in_excludes() { return 1 } + +# fetch the latest version of the repository +if ! git fetch; then + echo "Failed to fetch the latest version of the repository. Exiting." + exit 1 +fi + +# Check if there are any changes +if [[ $(git rev-list HEAD...origin/main --count) -eq 0 ]]; then + echo "No changes found. Exiting." + exit 0 +fi + # Then check which files have been modified by the user since the last update to preserve user configurations modified_files=() From f9854e5400fd1235b3717ac3d7a1b4f74dd9335c Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 11:13:07 +0200 Subject: [PATCH 13/26] now with current branch --- update-dots.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 3a4218ea8..ddb3eea8f 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -26,6 +26,7 @@ function file_in_excludes() { return 1 } +current_branch=$(git rev-parse --abbrev-ref HEAD) # fetch the latest version of the repository if ! git fetch; then @@ -34,7 +35,7 @@ if ! git fetch; then fi # Check if there are any changes -if [[ $(git rev-list HEAD...origin/main --count) -eq 0 ]]; then +if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then echo "No changes found. Exiting." exit 0 fi @@ -188,5 +189,4 @@ for folder in "${folders[@]}"; do fi fi done -done - +done \ No newline at end of file From e505ec6c071c046a5cdb62ce696da2b2184d1c9a Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 11:14:30 +0200 Subject: [PATCH 14/26] changed messsage --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index ddb3eea8f..fd564e88d 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -36,7 +36,7 @@ fi # Check if there are any changes if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then - echo "No changes found. Exiting." + echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi From 3b4c53ed22d88e03be805535d1cdb3ed363ecaf1 Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 16:31:28 +0200 Subject: [PATCH 15/26] Update based on https://github.com/end-4/dots-hyprland/pull/473#issuecomment-2094147560 --- update-dots.sh | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index fd564e88d..113628ba4 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -35,7 +35,7 @@ if ! git fetch; then fi # Check if there are any changes -if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then +if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 && 1 != 1 ]]; then echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi @@ -134,7 +134,7 @@ else fi # Then update the repository -if git pull; then +if ! git pull; then echo "Git pull successful." else # If the pull failed, clone the repository to a temporary folder and copy the files from there @@ -149,14 +149,18 @@ else mkdir -p ./cache temp_folder=$(mktemp -d -p ./cache) - git clone https://github.com/end-4/dots-hyprland/ "$temp_folder" + git clone https://github.com/end-4/dots-hyprland/ --depth=1 "$temp_folder" # Replace the existing dotfiles with the new ones for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them find "$temp_folder/$folder" -type f -print0 | while IFS= read -r -d '' file; do - if [[ -f "$file" ]] && ! file_in_excludes "$file"; then + file="${file/$temp_folder<\//}" + if [[ -f "$temp_folder/$file" && ! $(file_in_excludes "$file") && ! " ${modified_files[@]} " =~ " ${file} " ]]; then + # Construct the destination path + # Remove the temporary folder path destination="$HOME/$file" + echo "$destination" # Create the destination folder if it doesn't exist mkdir -p "$(dirname "$destination")" # Copy the file @@ -166,6 +170,7 @@ else done echo "New dotfiles have been copied. Cleaning up temporary folder." rm -rf "$temp_folder" + exit 0 fi @@ -178,15 +183,14 @@ for folder in "${folders[@]}"; do mkdir -p "$HOME/$file" fi # Check if the file is a regular file and not in the exclude_folders - if [[ -f "$file" ]] && ! file_in_excludes "$file"; then - if [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then - # Construct the destination path - destination="$HOME/$file" - # Copy the file - # Create the destination folder if it doesn't exist - mkdir -p "$(dirname "$destination")" - cp -f "$base/$file" "$destination" - fi + if [[ -f "$file" && ! $(file_in_excludes "$file") && ! " ${modified_files[@]} " =~ " ${file} " ]]; then + # Construct the destination path + destination="$HOME/$file" + echo "$destination" + # Copy the file + # Create the destination folder if it doesn't exist + mkdir -p "$(dirname "$destination")" + cp -f "$base/$file" "$destination" fi done done \ No newline at end of file From 4917a1f64ea2ed674d29fa8e452cbb5f959ea393 Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 16:46:18 +0200 Subject: [PATCH 16/26] added greeting --- update-dots.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/update-dots.sh b/update-dots.sh index 113628ba4..19e6631bf 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -26,6 +26,29 @@ function file_in_excludes() { return 1 } + +# Greetings! +echo "###################################################################################################" +echo "| |" +echo "| Welcome to the dotfiles updater script! |" +echo "| |" +echo "| This script will update your dotfiles (.config, .local, etc) by retrieving the latest version |" +echo "| from the Git repository and then replacing files that have not been modified |" +echo "| by you to preserve changes. |" +echo "| |" +echo "###################################################################################################" + +echo "Do you want to continue? [Y/n] " +read -r +echo +if [[ $REPLY =~ ^[Nn]$ ]]; then + echo "Exiting." + exit 0 +fi + +# End of Greetings + + current_branch=$(git rev-parse --abbrev-ref HEAD) # fetch the latest version of the repository From 273c356e0fe1f4891316fa1f831292dcfeb39db0 Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 16:48:48 +0200 Subject: [PATCH 17/26] now git pull working again --- update-dots.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 19e6631bf..916f187b9 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -58,7 +58,7 @@ if ! git fetch; then fi # Check if there are any changes -if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 && 1 != 1 ]]; then +if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi @@ -85,8 +85,8 @@ while IFS= read -r -d '' file; do done < <(find "${folders[@]}" -type f -print0) -echo "Modified files: ${modified_files[@]}" echo "Excluded files and folders: ${excludes[@]}" +echo # Output all modified files if [[ ${#modified_files[@]} -gt 0 ]]; then @@ -157,7 +157,7 @@ else fi # Then update the repository -if ! git pull; then +if git pull; then echo "Git pull successful." else # If the pull failed, clone the repository to a temporary folder and copy the files from there From dc0c113270f81c5c58262dd59db24416ad2d562a Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 17:07:53 +0200 Subject: [PATCH 18/26] Now with better logging --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index 916f187b9..8981deaa7 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -209,7 +209,7 @@ for folder in "${folders[@]}"; do if [[ -f "$file" && ! $(file_in_excludes "$file") && ! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path destination="$HOME/$file" - echo "$destination" + echo "Replacing \"$destination\" ..." # Copy the file # Create the destination folder if it doesn't exist mkdir -p "$(dirname "$destination")" From 3fa60152ed017ca5169cf8bdd895e4b4c2e9c8ee Mon Sep 17 00:00:00 2001 From: H0mire Date: Sat, 4 May 2024 17:18:26 +0200 Subject: [PATCH 19/26] bugfix --- update-dots.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 8981deaa7..bbd6e1990 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -176,18 +176,22 @@ else # Replace the existing dotfiles with the new ones for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them - find "$temp_folder/$folder" -type f -print0 | while IFS= read -r -d '' file; do + find "$temp_folder/$folder" -print0 | while IFS= read -r -d '' file; do file="${file/$temp_folder<\//}" + if [[ -d "$temp_folder/$file" ]]; then + mkdir -p "$HOME/$file" + fi if [[ -f "$temp_folder/$file" && ! $(file_in_excludes "$file") && ! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path # Remove the temporary folder path destination="$HOME/$file" - echo "$destination" + echo "Replacing $destination ..." # Create the destination folder if it doesn't exist mkdir -p "$(dirname "$destination")" # Copy the file - cp -f "$file" "$destination" + + cp -f "$temp_folder/$file" "$destination" fi done done From 9d19cd2b3c87b61814d8093a6c8fac8a703d01a0 Mon Sep 17 00:00:00 2001 From: H0mire Date: Mon, 6 May 2024 21:55:28 +0200 Subject: [PATCH 20/26] bugfix excluding mechanism now works --- update-dots.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index bbd6e1990..5c465a585 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -9,7 +9,7 @@ export base="$(pwd)" # Define paths to update folders=(".config" ".local") -excludes=(".config/hypr/custom", ".config/ags/user_options.js", ".config/hypr/hyprland.conf") +excludes=(".config/hypr/custom" ".config/ags/user_options.js" ".config/hypr/hyprland.conf") function get_checksum() { # Get the checksum of a specific file @@ -58,7 +58,7 @@ if ! git fetch; then fi # Check if there are any changes -if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then +if [[ ! $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi @@ -69,7 +69,8 @@ modified_files=() # Find all files in the specified folders and their subfolders while IFS= read -r -d '' file; do # If the file is not in the home directory, skip it - if [[ ! -f "$HOME/$file" ]]; then + if [[ ! -f "$HOME/$file" ]] || file_in_excludes "$file" ; then + echo "Skipping $file" continue fi @@ -84,7 +85,7 @@ while IFS= read -r -d '' file; do fi done < <(find "${folders[@]}" -type f -print0) - +echo echo "Excluded files and folders: ${excludes[@]}" echo @@ -181,7 +182,7 @@ else if [[ -d "$temp_folder/$file" ]]; then mkdir -p "$HOME/$file" fi - if [[ -f "$temp_folder/$file" && ! $(file_in_excludes "$file") && ! " ${modified_files[@]} " =~ " ${file} " ]]; then + if [[ -f "$temp_folder/$file" ]] && ! file_in_excludes "$file" && [[! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path # Remove the temporary folder path @@ -210,7 +211,7 @@ for folder in "${folders[@]}"; do mkdir -p "$HOME/$file" fi # Check if the file is a regular file and not in the exclude_folders - if [[ -f "$file" && ! $(file_in_excludes "$file") && ! " ${modified_files[@]} " =~ " ${file} " ]]; then + if [[ -f "$temp_folder/$file" ]] && ! file_in_excludes "$file" && [[! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path destination="$HOME/$file" echo "Replacing \"$destination\" ..." From f37789d2a42d37390cff6829e11bf2c4e807b7c5 Mon Sep 17 00:00:00 2001 From: H0mire Date: Mon, 6 May 2024 21:57:47 +0200 Subject: [PATCH 21/26] Changed Message --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index 5c465a585..77daa97e2 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -62,6 +62,7 @@ if [[ ! $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi +echo "Excluding files and folders: ${excludes[@]}" # Then check which files have been modified by the user since the last update to preserve user configurations modified_files=() @@ -86,7 +87,6 @@ while IFS= read -r -d '' file; do done < <(find "${folders[@]}" -type f -print0) echo -echo "Excluded files and folders: ${excludes[@]}" echo # Output all modified files From 313f0ed178dfcf6c0cbaef62a88827439f380f0e Mon Sep 17 00:00:00 2001 From: H0mire Date: Mon, 6 May 2024 22:07:25 +0200 Subject: [PATCH 22/26] removed debugging exclamation --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index 77daa97e2..5179198ea 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -58,7 +58,7 @@ if ! git fetch; then fi # Check if there are any changes -if [[ ! $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then +if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi From c860974e8e3fe4b5c659782d3abfa4dd1605d147 Mon Sep 17 00:00:00 2001 From: H0mire Date: Mon, 6 May 2024 22:20:23 +0200 Subject: [PATCH 23/26] fixed git clone paths --- update-dots.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 5179198ea..40a1875b4 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -58,7 +58,7 @@ if ! git fetch; then fi # Check if there are any changes -if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then +if [[ ! $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi @@ -158,7 +158,7 @@ else fi # Then update the repository -if git pull; then +if ! git pull; then echo "Git pull successful." else # If the pull failed, clone the repository to a temporary folder and copy the files from there @@ -178,11 +178,11 @@ else for folder in "${folders[@]}"; do # Find all files (including those in subdirectories) and copy them find "$temp_folder/$folder" -print0 | while IFS= read -r -d '' file; do - file="${file/$temp_folder<\//}" + file=${file//$temp_folder\//} if [[ -d "$temp_folder/$file" ]]; then mkdir -p "$HOME/$file" fi - if [[ -f "$temp_folder/$file" ]] && ! file_in_excludes "$file" && [[! " ${modified_files[@]} " =~ " ${file} " ]]; then + if [[ -f "$temp_folder/$file" ]] && ! file_in_excludes "$file" && [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path # Remove the temporary folder path From 6138f0b0feecc987e0df9487d65a575d3d562382 Mon Sep 17 00:00:00 2001 From: H0mire Date: Mon, 6 May 2024 22:21:35 +0200 Subject: [PATCH 24/26] removed debugging stuff --- update-dots.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index 40a1875b4..ba67ad32f 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -58,7 +58,7 @@ if ! git fetch; then fi # Check if there are any changes -if [[ ! $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then +if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." exit 0 fi @@ -158,7 +158,7 @@ else fi # Then update the repository -if ! git pull; then +if git pull; then echo "Git pull successful." else # If the pull failed, clone the repository to a temporary folder and copy the files from there From a4434dd709601ca8c793fb4996c1d88175262607 Mon Sep 17 00:00:00 2001 From: H0mire Date: Mon, 6 May 2024 22:27:35 +0200 Subject: [PATCH 25/26] fix number 2 --- update-dots.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-dots.sh b/update-dots.sh index ba67ad32f..dd88a4a22 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -211,7 +211,7 @@ for folder in "${folders[@]}"; do mkdir -p "$HOME/$file" fi # Check if the file is a regular file and not in the exclude_folders - if [[ -f "$temp_folder/$file" ]] && ! file_in_excludes "$file" && [[! " ${modified_files[@]} " =~ " ${file} " ]]; then + if [[ -f "$file" ]] && ! file_in_excludes "$file" && [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then # Construct the destination path destination="$HOME/$file" echo "Replacing \"$destination\" ..." From 207c81bd556ed625107a7c021e48337c27ecdadc Mon Sep 17 00:00:00 2001 From: H0mire Date: Thu, 16 May 2024 19:53:42 +0200 Subject: [PATCH 26/26] cleaned up code for readability, added colours --- update-dots.sh | 235 +++++++++++++++++++++++-------------------------- 1 file changed, 111 insertions(+), 124 deletions(-) diff --git a/update-dots.sh b/update-dots.sh index dd88a4a22..ecc4225ab 100644 --- a/update-dots.sh +++ b/update-dots.sh @@ -2,67 +2,77 @@ # This script updates the dotfiles by fetching the latest version from the Git repository and then replacing files # that have not been modified by the user to preserve changes. The remaining files will be replaced with the new ones. -set -e +set -euo pipefail cd "$(dirname "$0")" export base="$(pwd)" +# Define colors +GREEN="\033[0;32m" +RED="\033[0;31m" +BLUE="\033[0;34m" +CYAN="\033[0;36m" +YELLOW="\033[1;33m" +MAGENTA="\033[0;35m" +RESET="\033[0m" # Define paths to update folders=(".config" ".local") excludes=(".config/hypr/custom" ".config/ags/user_options.js" ".config/hypr/hyprland.conf") -function get_checksum() { - # Get the checksum of a specific file - md5sum "$1" | awk '{print $1}' +get_checksum() { + # Get the checksum of a specific file + local file="$1" + md5sum "$file" | awk '{print $1}' } -function file_in_excludes() { - # Check if a file is in the exclude_folders - for exc in "${excludes[@]}"; do - if [[ $1 == $exc* ]]; then - return 0 - fi - done - return 1 +file_in_excludes() { + # Check if a file is in the exclude_folders + local file="$1" + for exc in "${excludes[@]}"; do + if [[ $file == "$exc"* ]]; then + return 0 + fi + done + return 1 } +# Greetings! +cat << 'EOF' +################################################################################################### +| | +| Hi there! | +| | +| This script will update your dotfiles (.config, .local, etc) by retrieving the latest version | +| from the Git repository and then replacing the old config files with the updated ones. | +| To preserve your customizations, it will ask you if you wanna keep some modified | +| files untouched. | +| | +################################################################################################### +EOF -# Greetings! -echo "###################################################################################################" -echo "| |" -echo "| Welcome to the dotfiles updater script! |" -echo "| |" -echo "| This script will update your dotfiles (.config, .local, etc) by retrieving the latest version |" -echo "| from the Git repository and then replacing files that have not been modified |" -echo "| by you to preserve changes. |" -echo "| |" -echo "###################################################################################################" - -echo "Do you want to continue? [Y/n] " -read -r +read -rp "Do you want to continue? [Y/n] " REPLY echo if [[ $REPLY =~ ^[Nn]$ ]]; then - echo "Exiting." + echo -e "${RED}Exiting.${RESET}" exit 0 fi # End of Greetings - current_branch=$(git rev-parse --abbrev-ref HEAD) # fetch the latest version of the repository if ! git fetch; then - echo "Failed to fetch the latest version of the repository. Exiting." + echo -e "${RED}Failed to fetch the latest version of the repository. Exiting.${RESET}" exit 1 fi # Check if there are any changes -if [[ $(git rev-list HEAD...origin/$current_branch --count) -eq 0 ]]; then - echo "Repository is already up-to-date. Do not run git pull before this script. Exiting." +if [[ $(git rev-list HEAD...origin/"$current_branch" --count) -eq 0 ]]; then + echo -e "${GREEN}Repository is already up-to-date. Do not run git pull before this script. Exiting.${RESET}" exit 0 fi -echo "Excluding files and folders: ${excludes[@]}" +echo -e "${CYAN}Excluding files and folders: ${excludes[@]}${RESET}" # Then check which files have been modified by the user since the last update to preserve user configurations modified_files=() @@ -70,16 +80,15 @@ modified_files=() # Find all files in the specified folders and their subfolders while IFS= read -r -d '' file; do # If the file is not in the home directory, skip it - if [[ ! -f "$HOME/$file" ]] || file_in_excludes "$file" ; then - echo "Skipping $file" + if [[ ! -f "$HOME/$file" ]] || file_in_excludes "$file"; then + echo -e "${YELLOW}Skipping $file${RESET}" continue fi - + # Calculate checksums base_checksum=$(get_checksum "$base/$file") home_checksum=$(get_checksum "$HOME/$file") - - + # Compare checksums and add to modified_files if necessary if [[ $base_checksum != $home_checksum ]]; then modified_files+=("$file") @@ -87,138 +96,116 @@ while IFS= read -r -d '' file; do done < <(find "${folders[@]}" -type f -print0) echo -echo # Output all modified files if [[ ${#modified_files[@]} -gt 0 ]]; then - echo "The following files have been modified since the last update:" + echo -e "${MAGENTA}The following files have been modified since the last update:${RESET}" for file in "${modified_files[@]}"; do - echo "$file" + echo -e "${BLUE}$file${RESET}" done else - echo "No files found that have been modified since the last update. All files will be replaced. Are you sure you want to continue? [Y/n] " - read -r + read -rp "No files found that have been modified since the last update. All files will be replaced. Are you sure you want to continue? [Y/n] " REPLY echo - if [[ ! $REPLY =~ ^[Nn]$ ]]; then - echo "Exiting." + if [[ $REPLY =~ ^[Nn]$ ]]; then + echo -e "${RED}Exiting.${RESET}" exit 0 fi fi -echo "Do you want to keep these files untouched?" -echo "[Y] Yes, keep them." -echo "[n] No, replace them." -echo "[i] Check the files individually." -# Ask if the user wants to keep them -read -p "Answer: " -r +cat << 'EOF' +Do you want to keep these files untouched? +[Y] Yes, keep them. +[n] No, replace them. +[i] Check the files individually. +EOF +read -rp "Answer: " REPLY echo -if [[ $REPLY =~ ^[Nn]$ ]]; then - echo "Replacing all files." - modified_files=() -elif [[ $REPLY =~ ^[Ii]$ ]]; then - new_modified_files=() - replaced_files=() - for file in "${modified_files[@]}"; do - echo "Do you want to keep $file untouched?" - echo "[Y] Yes, keep it." - echo "[n] No, replace it." - read -p "Answer: " -n 1 -r - echo - if [[ ! $REPLY =~ ^[Nn]$ ]]; then - echo "Keeping $file." - new_modified_files+=("$file") - else - replaced_files+=("$file") - fi - done - modified_files=("${new_modified_files[@]}") - # verify the files that will be kept - echo "_____________________________________________________" - echo "These User configured/modifed files will be kept:" - for file in "${modified_files[@]}"; do - echo "$file" - done - echo "_____________________________________________________" - echo "These User configured/modifed files will be replaced:" - for file in "${replaced_files[@]}"; do - echo "$file" - done - echo "_____________________________________________________" - echo "Do you want to continue?" - echo "[y] Yes, continue." - echo "[N] No, exit." - read -p "Answer: " -r - echo - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - echo "Exiting..." - exit 0 - fi -else - echo "Keeping every modified file" -fi -# Then update the repository -if git pull; then - echo "Git pull successful." -else - # If the pull failed, clone the repository to a temporary folder and copy the files from there - echo "Git pull failed. Consider recloning the project or resolving conflicts manually." - echo "Should I clone the repository to a temporary folder in cache and copy the files from there? [y/N] " - read -r +case $REPLY in + [Nn]) + echo -e "${RED}Replacing all files.${RESET}" + modified_files=() + ;; + [Ii]) + new_modified_files=() + replaced_files=() + for file in "${modified_files[@]}"; do + read -rp "Do you want to keep $file untouched? [Y/n] " REPLY + echo + if [[ $REPLY =~ ^[Nn]$ ]]; then + replaced_files+=("$file") + else + new_modified_files+=("$file") + fi + done + modified_files=("${new_modified_files[@]}") + echo -e "${CYAN}_____________________________________________________${RESET}" + echo -e "${MAGENTA}These User configured/modified files will be kept:${RESET}" + for file in "${modified_files[@]}"; do + echo -e "${BLUE}$file${RESET}" + done + echo -e "${CYAN}_____________________________________________________${RESET}" + echo -e "${MAGENTA}These User configured/modified files will be replaced:${RESET}" + for file in "${replaced_files[@]}"; do + echo -e "${BLUE}$file${RESET}" + done + echo -e "${CYAN}_____________________________________________________${RESET}" + read -rp "Do you want to continue? [y/N] " REPLY + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo -e "${RED}Exiting...${RESET}" + exit 0 + fi + ;; + *) + echo -e "${GREEN}Keeping every modified file${RESET}" + ;; +esac + +# Update the repository +if ! git pull; then + echo -e "${RED}Git pull failed. Consider recloning the project or resolving conflicts manually.${RESET}" + read -rp "Should I clone the repository to a temporary folder in cache and copy the files from there? [y/N] " REPLY echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then - echo "Exiting..." + echo -e "${RED}Exiting...${RESET}" exit 1 fi - + mkdir -p ./cache temp_folder=$(mktemp -d -p ./cache) git clone https://github.com/end-4/dots-hyprland/ --depth=1 "$temp_folder" # Replace the existing dotfiles with the new ones for folder in "${folders[@]}"; do - # Find all files (including those in subdirectories) and copy them find "$temp_folder/$folder" -print0 | while IFS= read -r -d '' file; do file=${file//$temp_folder\//} if [[ -d "$temp_folder/$file" ]]; then mkdir -p "$HOME/$file" fi - if [[ -f "$temp_folder/$file" ]] && ! file_in_excludes "$file" && [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then - - # Construct the destination path - # Remove the temporary folder path + if [[ -f "$temp_folder/$file" ]] && ! file_in_excludes "$file" && [[ ! " ${modified_files[*]} " =~ " $file " ]]; then destination="$HOME/$file" - echo "Replacing $destination ..." - # Create the destination folder if it doesn't exist + echo -e "${BLUE}Replacing $destination ...${RESET}" mkdir -p "$(dirname "$destination")" - # Copy the file - cp -f "$temp_folder/$file" "$destination" fi done done - echo "New dotfiles have been copied. Cleaning up temporary folder." + echo -e "${GREEN}New dotfiles have been copied. Cleaning up temporary folder.${RESET}" rm -rf "$temp_folder" exit 0 fi - -# Now only replace the files that are not modified by the user +# Replace unmodified files for folder in "${folders[@]}"; do - # Find all files (including those in subdirectories) and copy them find "$folder" -print0 | while IFS= read -r -d '' file; do - # if the file is a directory, ensure it exists in the home directory if [[ -d "$file" ]]; then mkdir -p "$HOME/$file" fi - # Check if the file is a regular file and not in the exclude_folders - if [[ -f "$file" ]] && ! file_in_excludes "$file" && [[ ! " ${modified_files[@]} " =~ " ${file} " ]]; then - # Construct the destination path + if [[ -f "$file" ]] && ! file_in_excludes "$file" && [[ ! " ${modified_files[*]} " =~ " $file " ]]; then destination="$HOME/$file" - echo "Replacing \"$destination\" ..." - # Copy the file - # Create the destination folder if it doesn't exist + echo -e "${BLUE}Replacing \"$destination\" ...${RESET}" mkdir -p "$(dirname "$destination")" cp -f "$base/$file" "$destination" fi done -done \ No newline at end of file +done