forked from SmartBear/lhdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.sh
executable file
·143 lines (124 loc) · 4.34 KB
/
history.sh
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
#!/usr/bin/env bash
#
# This script builds a sqlite database of lines and their unique ids.
#
set -e
IFS=$'\n'
NL=$'\n'
sqlite3 lines.db <<EOF
CREATE TABLE IF NOT EXISTS lines (
line_id TEXT NOT NULL,
file TEXT NOT NULL,
line_number INTEGER NOT NULL,
sha TEXT NOT NULL,
UNIQUE(file, line_number),
UNIQUE(line_id),
CHECK (length(line_id) > 0)
);
CREATE TABLE IF NOT EXISTS changes (
line_id TEXT NOT NULL,
file TEXT NOT NULL,
line_number INTEGER,
sha TEXT NOT NULL,
type TEXT NOT NULL,
UNIQUE(line_id, sha),
CHECK (length(line_id) > 0)
);
EOF
# Loop over all commits, starting with the oldest revision
for sha in $(git log --reverse --pretty=format:"%h"); do
# Loop over all files in the commit. We're interested in the files with one of the following formats:
# M file # Modified file.
# Rn oldfile newfile # Renamed file. The n represents the similarity. 100 means no content change.
for diffLine in $(git --no-pager diff --name-status --find-renames=50% $sha^1 $sha 2>/dev/null); do
type=$(echo $diffLine | awk '{ print substr($1,1,1) }')
oldFile=$(echo $diffLine | awk '{ print $2 }')
newFile=$(echo $diffLine | awk '{ print $3 }')
doLhdiff=false
if [ $type == "M" ]; then
newFile=$oldFile
doLhdiff=true
fi
if [ $type == "R" ]; then
doLhdiff=true
fi
# TODO: handle deleted files - we should delete all relevant rows
# if [[ $newFile != "go.mod" ]]; then
# continue
# fi
if [[ ${doLhdiff} = true ]]; then
# echo "----- ${sha}"
pairs=$(lhdiff --omit <(git show $sha^1:$oldFile) <(git show $sha:$newFile))
# echo "$pairs"
lineIdsToRemove="''"
insertStatements=""
for pair in $pairs; do
# echo "$pair"
oldLineNumber=$(echo $pair | cut -f1 -d,)
newLineNumber=$(echo $pair | cut -f2 -d,)
oldLineId=''
newLineId=''
lineId=''
# echo "${oldLineNumber} - ${newLineNumber}"
if [[ ${oldLineNumber} != "_" ]]; then
oldLineId=$(sqlite3 lines.db "SELECT line_id FROM lines WHERE file='${oldFile}' AND line_number=${oldLineNumber};")
if [[ ! -z ${oldLineId} ]]; then
lineIdsToRemove="${lineIdsToRemove},${NL} '${oldLineId}'"
fi
fi
if [[ ${newLineNumber} != "_" ]]; then
newLineId=$(sqlite3 lines.db "SELECT line_id FROM lines WHERE file='${newFile}' AND line_number=${newLineNumber};")
if [[ ! -z ${newLineId} ]]; then
lineIdsToRemove="${lineIdsToRemove},${NL} '${newLineId}'"
else
newLineId="${sha}:${newFile}:${newLineNumber}"
fi
fi
if [[ ${oldLineNumber} != "_" && ${newLineNumber} = '_' ]]; then
# Deleted
if [[ ! -z ${oldLineId} ]]; then
lineId="${oldLineId}"
fi
type="D"
else
# Modified or Added
if [[ ! -z ${oldLineId} ]]; then
lineId="${oldLineId}"
else
lineId="${sha}:${newFile}:${newLineNumber}"
fi
sql="INSERT INTO lines (line_id, file, line_number, sha) VALUES ('${lineId}', '${newFile}', ${newLineNumber}, '${sha}');"
insertStatements="${insertStatements}${NL}${sql}"
if [[ ${oldLineNumber} != "_" && ${newLineNumber} != "_" ]]; then
# Modified
type="M"
elif [[ ${oldLineNumber} = "_" && ${newLineNumber} != "_" ]]; then
# Added
type="A"
fi
fi
if [[ ! -z ${lineId} ]]; then
if [[ ${type} = 'D' ]]; then
lineNumber=NULL
else
lineNumber=${newLineNumber}
fi
sql="INSERT INTO changes (line_id, file, line_number, sha, type) VALUES ('${lineId}', '${newFile}', ${lineNumber}, '${sha}', '${type}');"
insertStatements="${insertStatements}${NL}${sql}"
fi
done
if [[ ${lineIdsToRemove} != "''" || ${insertStatements} != '' ]]; then
sql="BEGIN;${NL}"
if [[ ${lineIdsToRemove} != "''" ]]; then
sql="${sql}DELETE FROM lines where line_id IN (${lineIdsToRemove});${NL}"
fi
if [[ ${insertStatements} != '' ]]; then
sql="${sql}${insertStatements}${NL}"
fi
sql="${sql}COMMIT;${NL}"
echo "${sql}"
sqlite3 -echo lines.db "${sql}"
fi
fi
done
done