-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-undo-ws
More file actions
executable file
·56 lines (47 loc) · 1.28 KB
/
git-undo-ws
File metadata and controls
executable file
·56 lines (47 loc) · 1.28 KB
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
#!/bin/bash
fileName=""
dateStamp=""
backupFile=""
patchFile=""
function restoreBackup() {
cp "$backupFile" "$fileName"
}
function gitUndoWhitespace() {
cp -v "$fileName" "$backupFile"
if [[ $? -ne 0 ]]; then
echo "Failed to make backup; abort."
exit 2
fi
git diff -w "$fileName" > "$patchFile"
if [[ $? -ne 0 ]]; then
echo "Failed to create diff; abort"
exit 3
fi
git checkout HEAD "$fileName"
if [[ $? -ne 0 ]]; then
echo "Failed to checkout HEAD version; restoring backup"
restoreBackup
exit 4
fi
local pwdRelative=$(realpath --relative-to=$(git rev-parse --show-toplevel) "$PWD")
local stripParts=$(echo "$pwdRelative" | perl -F/ -ane 'print scalar @F;')
stripParts=$(( $stripParts + 1 ))
patch -p$stripParts < "$patchFile"
if [[ $? -ne 0 ]]; then
echo -e "Failed to patch HEAD version; leaving changes, original file is\n$backupFile"
exit 5
fi
echo "Removed whitespace changes from $fileName, backup of previous file is in $backupFile"
}
function setup() {
if [[ "x$1" = "x" || "$1" == "-h" || "$1" == "--help" ]]; then
echo -e "Git Undo Whitespace-only changes\nUsage: $0 <FILENAME>"
exit 1
fi
fileName="$1"
dateStamp="$(date +%s)"
backupFile="${fileName}_gitundows.${dateStamp}"
patchFile="${fileName}_${dateStamp}.patch"
}
setup "$@"
gitUndoWhitespace