forked from ossobv/vcutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifftac
executable file
·41 lines (38 loc) · 891 Bytes
/
difftac
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
#!/bin/sh
# difftac (part of ossobv/vcutil) // wdoekes/2014 // Public Domain
#
# Takes a unified diff and reverses it.
#
# Usage:
#
# difftac some/patch.diff > some/reversedpatch.diff
#
# Original author: Lie Ryan (2010)
# Source: http://stackoverflow.com/questions/3902388/
# permanently-reversing-a-patch-file/3902431#3902431
# Slightly improved by wdoekes.
#
swap() {
sed -e "s/^$1/PPP/;s/^$2/$1/;s/^PPP/$2/"
}
file_header() {
head -2 "$1" | tac | swap +++ ---
}
fix_chunk_header() {
sed -e 's/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ -\2 +\1 @@/'
}
fix_lines() {
swap + -
}
# Take filename or file from stdin
file="$1"
if test -z "$file"; then
file="`mktemp`"
cat > "$file"
fi
file_header "$file"
tail "$file" -n +3 | fix_chunk_header | fix_lines
# If we used stdin, we remove the tempfile
if test -z "$1"; then
rm "$file"
fi