-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff
executable file
·52 lines (46 loc) · 1.64 KB
/
diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /bin/bash -
# Returns the files that are unique among the dirs
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
>&2 printf "Usage: $0 <dir1> <dir2> ...\n"
exit 0
fi
dir="$(dirname $(readlink -f ${BASH_SOURCE[0]}))"
export LC_ALL=C # byte-wise sorting
export GREP_COLOR='32'
export look="$dir/look"
export sorted_filehashes="$dir/sorted_file_hashes.out"
tempHashes=$(mktemp -p /dev/shm)
tempOut=$(mktemp -p /dev/shm)
trap "rm $tempHashes $tempOut" EXIT
getFileHashes()(
path="$(readlink -f "$1")"
path="${path//\\/\\\\}"
path="${path//$'\n'/\\n}"
escaped=$(printf '%s' "$path" | \
sed -r 's/([\$\.\*\/\[\\^])/\\\1/g' | \
sed 's/[]]/\[]]/g')
replace=$(printf '%s' "$path" | \
sed -r 's/(["&\/\\])/\\\1/g')
modLine(){ sed "s|^$escaped|$replace/|g"; }
tempFile=$(mktemp -p /dev/shm)
trap "rm $tempFile" RETURN
"$look" "$path/" "$sorted_filehashes" | \
modLine > $tempFile
if [[ $? -ne 0 || ! -s $tempFile ]]; then
# fallback since look can not deal with special chars
text='\e[33mWarning:\e[0m No matches -> using slower grep for %s\n'
>&2 printf "$text" "$path"
grep -aF "$path" "$sorted_filehashes" | \
modLine > $tempFile
fi
if [[ ! -s $tempFile ]]; then
>&2 printf '\e[33mWarning:\e[0m No entries found for %s\n' "$path"
fi
cat $tempFile
)
export -f getFileHashes
printf '%s\0' "$@" | parallel --will-cite -0 -k getFileHashes | tee $tempOut | \
sed 's|^.*//||g' | sort | uniq -u > $tempHashes
ColorEsc=$(printf '\e[0m')
grep -aFf $tempHashes --color=always $tempOut | \
sed "s|//|/|g; s/,[^,]*,[^,]*$/$ColorEsc/g"