-
Notifications
You must be signed in to change notification settings - Fork 10
/
difftac
executable file
·53 lines (49 loc) · 1.23 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/sh
# difftac (part of ossobv/vcutil) // wdoekes/2014,2017 // Public Domain
#
# Takes a unified diff and reverses it.
#
# Usage:
#
# difftac some/patch.diff > some/reversedpatch.diff
#
# Bugs:
# - only works on patches that operate on a single file
# - does not cope with leading "garbage"/comments before the patch
#
# 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
# Check whether our bugs might affect it
if tail -n +2 "$file" | grep -q '^--- '; then
echo >&2
echo '*** Leading data or multiple files found. ***' >&2
echo '*** Diff may become corrupt! ***' >&2
echo >&2
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