Skip to content

Commit 5511bb5

Browse files
author
Pascal FLuck
committed
Implements commit
1 parent ebee5bf commit 5511bb5

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

inc/prune

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# Initialization
1212
TOLERANCE=0.5
13+
PRUNE_HISTORY_FILENAME=".prune"
1314

1415
# prune usage
1516
display_prune_usage() {
@@ -101,13 +102,11 @@ if [[ ! -f "$SOURCE" ]]; then
101102
fi
102103

103104
# Execute prune command
104-
verbose "Launch prune in $SOURCE."
105-
106-
pruneCount=1
107-
similarCount=0
108-
109105
if [[ ! $COMMIT ]]; then
110-
verbose "Start to find similar images."
106+
pruneCount=1
107+
similarCount=0
108+
verbose "Start to find similar images in $SOURCE."
109+
111110
find "$SOURCE" -maxdepth $MAXDEPTH -type f -print0 | sort -z | while IFS= read -r -d '' f; do
112111
if [[ -f $f ]]; then
113112
debug "$f"
@@ -120,29 +119,36 @@ if [[ ! $COMMIT ]]; then
120119
previousFilenameWithExt=$(basename "$previousImage")
121120
debug "$f"
122121
difference=$(compare -quiet -metric NCC "$previousImage" "$f" null: 2>&1)
122+
123123
re="^[0-9]\.{0,1}[0-9]*$"
124124
if [[ $difference =~ $re ]]; then
125125
debug "$difference >= $TOLERANCE"
126126
isDifferent=$(echo "$difference >= $TOLERANCE" | bc -l)
127+
127128
if [[ $isDifferent == 1 ]]; then
128129
verbose "'$previousImage' seems similar to '$f' ($difference)."
129130
stillSimilar=1
130-
similarCount=$(( $similarCount + 1 ))
131131
pruneDir="$TARGET/$pruneCount"
132132
debug "pruneDir = $pruneDir"
133+
133134
mkdir -p "$pruneDir"
134-
if [[ ! -f "$pruneDir/$previousFilenameWithExt" ]]; then
135-
ln -s "$previousImage" "$pruneDir/$previousFilenameWithExt";
135+
if [[ ! -f "$pruneDir/$previousFilenameWithExt" ]]; then
136+
if [[ $similarCount == 0 ]]; then
137+
ln -s "$previousImage" "$pruneDir/$previousFilenameWithExt";
138+
echo "$previousImage" >> "$pruneDir/$PRUNE_HISTORY_FILENAME"
139+
fi
136140
fi
137141
ln -s "$f" "$pruneDir/$filenameWithExt"
138-
echo "$f" >> "$pruneDir/.prune"
142+
echo "$f" >> "$pruneDir/$PRUNE_HISTORY_FILENAME"
143+
144+
similarCount=$(( $similarCount + 1 ))
139145
verbose "Add image '$f' to prune directory '$pruneDir'."
140146
else
141147
debug "'$previousImage' seems different to '$f' ($difference)."
142148

143149
if [[ $stillSimilar > 0 ]]; then
144150
similarCount=$(( $similarCount + 1 ))
145-
mv "$pruneDir" "$pruneDir_$similarCount"
151+
mv "$pruneDir" "$pruneDir ($similarCount)"
146152
pruneCount=$(( $pruneCount + 1 ));
147153
fi
148154
stillSimilar=0
@@ -160,4 +166,19 @@ if [[ ! $COMMIT ]]; then
160166
else
161167
verbose "Start to commit prune selections."
162168

169+
find "$SOURCE" -type f -print0 -name "$PRUNE_HISTORY_FILENAME" | sort -z | while IFS= read -r -d '' f; do
170+
pruneDir=$(dirname "${f}")
171+
debug "Reading prune file file '$f' to find deleted files."
172+
while read p; do
173+
debug "p = $p"
174+
filenameWithExt=$(basename "$p")
175+
linked="$pruneDir/$filenameWithExt"
176+
debug "Check if link '$linked' still exists."
177+
if [[ ! -f "$linked" ]]; then
178+
debug "'$linked' has been deleted, so delete the original file '$p'."
179+
rm -f "$p"
180+
verbose "'$p' has been pruned (deleted)."
181+
fi
182+
done <"$f"
183+
done
163184
fi

0 commit comments

Comments
 (0)