-
Notifications
You must be signed in to change notification settings - Fork 2
/
diffdir
executable file
·147 lines (126 loc) · 4.26 KB
/
diffdir
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# Author: Gokberk Cinbis, 2022
set -e
function __increment_argi__() # just increment argi variable
{
argi=$((argi+1))
}
function __decrement_argi__() # just decrement argi variable
{
argi=$((argi-1))
}
function __print_help__ ()
{
echo "diffdir [--nogit] [--vimdiff] <path1> <path2>"
echo "Compare two directories recursively, with handy options."
echo ""
echo "ARGUMENTS"
echo "--nogit Discard all results involving .git"
echo "--vim,-vimdiff vimdiff resulting files (prompts before vimdiff)."
# HIDDEN ARGUMENT: --debug
exit 1
}
function __main__()
{
local narg=$#
local i_=$((narg-1))
local path1="${!i_}" # second from the last
local path2="${!narg}" # the last argument
if [ $narg -lt 2 ]
then
__print_help__
fi
findargs=()
grepargs=()
debug=0
nogit=0
vimdiff_=0
argi=0 # argument reading index
for (( argi=1; argi<=$((narg-2)); argi++ )); do
if [ "${!argi}" == '--nogit' ]; then
nogit=1
elif [ "${!argi}" == '--help' ]; then
__print_help__
elif [ "${!argi}" == '--debug' ]; then
debug=1
elif [[ "${!argi}" == '--vim' ]]; then
vimdiff_=1
elif [[ "${!argi}" == '--vimdiff' ]]; then
vimdiff_=1
#elif [[ "${!argi}" == '-e' ]]; then
# __increment_argi__
# if [ $argi -eq $narg ]; then
# __print_help__ # we'd read <regexp> as <ext> incorrectly
# fi
# # doesnt work: findargs+=('-name' "\"*.${!argi}\"")
# findargs+=('-name' "*.${!argi}")
else
command echo "Unrecognized argument: ${!argi}"
__print_help__
fi
done
# check if folders exist
[ ! -d "$path1" ] && echo "$path1 does not exist or not a directory." && exit 2
[ ! -d "$path2" ] && echo "$path2 does not exist or not a directory." && exit 2
outfile=$(mktemp)
set +e # otherwise pipeline fails
if [ $nogit -eq 1 ]; then
command diff --recursive --brief --exclude=".git" "$path1" "$path2" > "$outfile"
else
command diff --recursive --brief "$path1" "$path2" > "$outfile"
fi
set -e
if [ $vimdiff_ -eq 1 ]; then
outfile_diffs=$(mktemp)
outfile_onlyins=$(mktemp)
tmp=$(mktemp)
tmp2=$(mktemp)
# https://stackoverflow.com/questions/5410757/how-to-delete-from-a-text-file-all-lines-that-contain-a-specific-string
command sed '/^Only in/d' "$outfile" > "$outfile_diffs"
command sed '/^Files /d' "$outfile" > "$outfile_onlyins"
# differing file pairs
command sed 's/^Files /vimdiff /g' "$outfile_diffs" > "$tmp"
command sed 's/ and / /g' "$tmp" > "$tmp2"
command sed 's/ differ$//g' "$tmp2" > "$tmp"
command echo "### Differing file pairs"
command cat "$tmp"
# Simply print one-sided files
command echo ""
command echo "### Files that appear only in one of the two directories (not opened in vimdiff)"
command cat "$outfile_onlyins"
# Create the final script: add "set -e" so that :cquit in vim stops the comparisons
# todo: errtrace / catch :cquit exit and notify
command echo "!/bin/bash" > "$tmp2"
command echo "set -e" >> "$tmp2"
command cat "$tmp" >> "$tmp2"
command chmod 700 "$tmp2"
if [ $debug -eq 1 ]; then
command echo "==== FINAL SCRIPT TO BE EXECUTED ===="
command cat "$tmp2"
fi
# vimdiff time
command echo ""
command read -p "Do you want to vimdiff each differing pair? (*** Use :cquit to stop diffs ***) [y/n]: " -n 1 -r # ask before as it is quite annoying when the # is very large
if [[ $REPLY =~ ^[Yy]$ ]]
then
"$tmp2" # run the script
else
command echo ""
command echo "Cancelled."
fi
# clean up
command rm -f "$outfile_onlyins" "$outfile_diffs" "$tmp" "$tmp2"
else
# simply print diff output
command cat "$outfile"
fi
# clean up
command rm -f "$outfile"
}
if [ $# -lt 2 ] # avoid segfault on macos
then
__print_help__
fi
# ok with multiple quoted arguments
__main__ "$@"
exit 0