Skip to content

Commit 2a78530

Browse files
Working except for pathological diff splitting case
1 parent 8e2a505 commit 2a78530

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

apply.sql

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare
55
new_revision int;
66
hunk text[];
77
context text[]; -- only contains consecutive lines in LCS
8+
context_length int;
89
in_hunk boolean := FALSE;
910
hunk_lines_added int := 0;
1011
hunk_lines_deleted int := 0;
@@ -35,10 +36,11 @@ begin
3536
raise notice 'Determining longest common substring table...';
3637
C := lcs_length(X, Y);
3738
raise notice 'Longest common substring table determined.';
39+
raise notice 'context_len = %', context_len;
3840
LOOP -- moving backwards
3941
raise notice 'i = %, j = %', i, j;
40-
raise notice 'context = %', context;
4142
raise notice 'hunk = %', hunk;
43+
raise notice 'context here = %', context;
4244
if i = 1 or j = 1 then
4345
-- we're done!
4446
IF in_hunk THEN
@@ -75,18 +77,24 @@ begin
7577
context := (' ' || Xline) || context;
7678
-- hunk_lines_context := hunk_lines_context + 1;
7779
ELSE
78-
context := (' ' || Xline) || context[2:context_len];
80+
-- pull the last one off before you stick the new one in front
81+
raise notice 'sliced context = %', context[1:context_len-1];
82+
context := (' ' || Xline) || context[1:context_len-1];
7983
END IF;
8084
ELSE
8185
context := (' ' || Xline) || context;
86+
raise notice 'context whoa = %', context;
8287
-- hunk_lines_context := hunk_lines_context + 1;
8388
-- are we done with this hunk?
8489
IF array_length(context, 1) = context_len THEN
90+
-- prepend context to hunk
91+
hunk := context || hunk;
92+
hunk_lines_context = hunk_lines_context + context_len;
8593
-- write out the hunk
8694
INSERT INTO page_diff_hunk (page_id, revision, start,
8795
content, lines_added, lines_deleted, lines_context)
8896
VALUES
89-
(page_id, latest.revision, i, array_to_string(hunk, E'\n'),
97+
(page_id, latest.revision, i-1, array_to_string(hunk, E'\n'),
9098
hunk_lines_added, hunk_lines_deleted, hunk_lines_context);
9199
-- and reset
92100
hunk := array[]::text[];
@@ -102,18 +110,20 @@ begin
102110
continue; -- skip the rest of this function and go on
103111
end if;
104112
-- reset context array
113+
context_length := array_length(context, 1);
114+
raise notice 'context there = %', context;
105115
IF NOT in_hunk THEN
106116
-- start a new hunk
107117
hunk = context;
108118
in_hunk = TRUE;
109-
IF array_length(context, 1) IS NOT NULL THEN
110-
hunk_lines_context := hunk_lines_context + array_length(context, 1);
119+
IF context_length IS NOT NULL THEN
120+
hunk_lines_context := context_length;
111121
END IF;
112122
ELSE
113-
IF array_length(context, 1) IS NOT NULL THEN
123+
IF context_length IS NOT NULL THEN
114124
-- prepend context to hunk
115125
hunk := context || hunk;
116-
hunk_lines_context := hunk_lines_context + array_length(context, 1);
126+
hunk_lines_context := hunk_lines_context + context_length;
117127
END IF;
118128
END IF;
119129
context := array[]::text[];

test.sql

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
truncate page_latest, page_diff, page_diff_hunk;
12
INSERT INTO user(id, username, email) VALUES (1, 'foo', 'foo');
23
INSERT INTO page_latest(id, title, slug, num_lines, editor, language, content)
3-
VALUES (1, 'page', 'page', 5, 1, 'en-us',
4+
VALUES (1, 'page', 'page', 15, 1, 'en-us',
45
'a
56
b
67
c
@@ -10,7 +11,13 @@ f
1011
g
1112
h
1213
i
13-
j');
14+
j
15+
k
16+
l
17+
m
18+
n
19+
o
20+
p');
1421

1522

1623
SELECT update_page(1,
@@ -22,8 +29,15 @@ carrot
2229
f
2330
g
2431
h
25-
foo
26-
bogey
27-
k', 1);
32+
i
33+
j
34+
k
35+
l
36+
m
37+
stick
38+
o
39+
p', 1);
2840

29-
truncate page_latest, page_diff, page_diff_hunk;
41+
INSERT INTO page_latest(id, title, slug, num_lines, editor, language, content)
42+
VALUES (2, 'short', 'short', 3, 1, 'en-us', E'a\nb\nc');
43+
SELECT update_page(2, E'a\nx\nc\nd', 1);

0 commit comments

Comments
 (0)