1- #! /bin/bash
1+ #! /usr/ bin/env bash
22# merge-overlay: Interactive tool to review and merge overlayfs changes back to host
33#
44# Usage:
@@ -43,6 +43,50 @@ should_ignore() {
4343 return 1
4444}
4545
46+ is_git_ignored () {
47+ (cd " $REPO_DIR " && git check-ignore -q " $1 " ) 2> /dev/null
48+ }
49+
50+ GIT_IGNORED_CHOICE=" "
51+
52+ git_ignored_should_discard () {
53+ local rel_path=" $1 "
54+
55+ # If user already chose to keep, skip checking (optimization)
56+ [[ " $GIT_IGNORED_CHOICE " == " keep" ]] && return 1
57+
58+ # Check if file is git-ignored
59+ is_git_ignored " $rel_path " || return 1
60+
61+ # If already chose to discard, return true
62+ [[ " $GIT_IGNORED_CHOICE " == " discard" ]] && return 0
63+
64+ # First git-ignored file - ask user
65+ echo " "
66+ echo -e " ${YELLOW} ⚠ Git-ignored file detected:${NC} $rel_path "
67+ echo " "
68+ while true ; do
69+ read -p " How should git-ignored files be handled? [a/A=ask (review), D=discard all]? " choice
70+ case " $choice " in
71+ a|A)
72+ echo " → Git-ignored files will be treated like regular changes"
73+ echo " "
74+ GIT_IGNORED_CHOICE=" keep"
75+ return 1
76+ ;;
77+ D)
78+ echo " → All git-ignored files will be discarded"
79+ echo " "
80+ GIT_IGNORED_CHOICE=" discard"
81+ return 0
82+ ;;
83+ * )
84+ echo " Invalid choice. Please enter a, A, or D"
85+ ;;
86+ esac
87+ done
88+ }
89+
4690# Validate directories
4791if [[ ! -d " $REPO_DIR " ]]; then
4892 echo " Error: Repository directory does not exist: $REPO_DIR "
@@ -299,16 +343,22 @@ echo ""
299343# We need to handle whiteout files (both .wh. prefix and char devices 0/0) and regular files
300344FILES=()
301345IGNORED=()
302- while IFS= read -r -d ' ' file; do
346+ GIT_IGNORED_COUNT=0
347+
348+ # Read from fd 4 (connected to find output) so stdin remains available for user input
349+ while IFS= read -u 4 -r -d ' ' file; do
303350 rel_path=" ${file# $OVERLAY_UPPER / } "
304351 if should_ignore " $rel_path " ; then
305352 IGNORED+=(" $rel_path " )
306353 # Auto-discard ignored files
307354 rm -f " $file "
355+ elif git_ignored_should_discard " $rel_path " ; then
356+ rm -f " $file "
357+ (( GIT_IGNORED_COUNT++ ))
308358 else
309359 FILES+=(" $rel_path " )
310360 fi
311- done < <( find " $OVERLAY_UPPER " \( -type f -o -type c \) -print0 | sort -z )
361+ done 4 < <( find " $OVERLAY_UPPER " \( -type f -o -type c \) -print0)
312362
313363# Report ignored files
314364if [[ ${# IGNORED[@]} -gt 0 ]]; then
@@ -319,6 +369,12 @@ if [[ ${#IGNORED[@]} -gt 0 ]]; then
319369 echo " "
320370fi
321371
372+ # Report git-ignored files that were discarded
373+ if [[ $GIT_IGNORED_COUNT -gt 0 ]]; then
374+ echo " Discarded $GIT_IGNORED_COUNT git-ignored file(s)"
375+ echo " "
376+ fi
377+
322378TOTAL=${# FILES[@]}
323379
324380# Exit if no files remain after filtering
0 commit comments