Eskil

Diff
Login

Differences From Artifact [06a7954d30]:

To Artifact [83859129c4]:


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
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







+
+


















+
+
+
+







# Top level dialog, for doing fourway diff
snit::widget FourWay {
    hulltype toplevel
    widgetclass Toplevel
    variable fields
    variable files
    variable revs
    variable doingLine1
    variable doingLine2

    constructor {args} {
        eskilRegisterToplevel $win

        wm title $win "Four Way Diff"
        wm protocol $win WM_DELETE_WINDOW "cleanupAndExit $win"
        $hull configure -padx 3 -pady 3

        menu $win.m
        $hull configure -menu $win.m
        $win.m add cascade -menu $win.m.mf -label "File" -underline 0
        menu $win.m.mf
        $win.m.mf add command -label "Close" -underline 0 \
                -command [list cleanupAndExit $win]
        $win.m.mf add separator
        $win.m.mf add command -label "Quit" -underline 0 \
                -command [list cleanupAndExit all]

        if {$::eskil(debug) == 1} {
            AddDebugMenu $win
        }
 
        # Four files, with optional revision
        set fields {base1 change1 base2 change2}
    
        ttk::label $win.l1 -text "Base 1"
        ttk::label $win.l2 -text "Changed 1"
        ttk::label $win.l3 -text "Base 2"
        ttk::label $win.l4 -text "Changed 2"
146
147
148
149
150
151
152
153

154
155

156
157
158
159
160
161
162
163
164
165



























166






































































































167
168
169
170
171
172
173
174
175
176
177
178
152
153
154
155
156
157
158

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313







-
+


+










+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+












                         -parent $win]
        if {$apa != ""} {
            set files($field) $apa
        }
    }

    method doFourWayDiff {} {
        # Copy to local vars to be able to replace with defaults
        # Copy to local vars to be able to replace with defaults and parsed
        foreach field $fields {
            set filename($field) $files($field)
            set rev($field) $revs($field)
        }
        # Fill in defaults, if revisions are given for empty files
        foreach {from to} $fields {
            if {$filename($to) eq "" && $revs($to) ne ""} {
                set filename($to) $files($from)
            }
            if {$filename($from) eq "" && $revs($from) ne ""} {
                set filename($from) $files($to)
            }
        }
        # Remember originals for display, they might be replaced below
        foreach field $fields {
            set origfile($field) $filename($field)
            set origrev($field)  $rev($field)
        }
        # Figure out any revisions
        foreach field $fields {
            # TODO: Move this to helper function in rev.tcl
            if {$rev($field) ne ""} {
                set revtype($field) [detectRevSystem $filename($field)]
                if {$revtype($field) eq ""} {
                    # TODO error
                    set rev($field) ""
                    continue
                }
                set revList [list $rev($field)]
                set revList [eskil::rev::$revtype($field)::ParseRevs \
                                     $filename($field) $revList]
                if {[llength $revList] == 0} {
                    # TODO error
                    set rev($field) ""
                } else {
                    set rev($field) [lindex $revList 0]
                }
            }
            # Still a revision?
            if {$rev($field) ne ""} {
        parray filename
                set filename($field) [tmpFile]
                eskil::rev::$revtype($field)::get $origfile($field) \
                        $filename($field) $rev($field)
            }
        }

        # Handle at least base options
        set opts $::Pref(ignore)
        if {$::Pref(nocase)} {lappend opts -nocase}
        if {$::Pref(noempty)} {lappend opts -noempty}
        if {$::Pref(pivot) > 0} {lappend opts -pivot $::Pref(pivot)}

        # Do compare of files, to generate patches
        foreach side {1 2} {
            set differr [catch {DiffUtil::diffFiles {*}$opts \
                                        $filename(base$side) $filename(change$side)} diffres]
            if {$differr != 0} {
                # TODO error
                return
            }
            set outfile($side) [tmpFile]
            set ch [open $outfile($side) w]
            foreach str {From To} field "base$side change$side" {
                set line "$str $origfile($field)"
                if {$rev($field) ne ""} {
                    append line "  Revision $rev($field)"
                    if {$origrev($field) ne $rev($field)} {
                        append line " ($origrev($field))"
                    }
                }
                puts $ch $line
            }
            puts $ch [string repeat "-" 78]
            if {[llength $diffres] == 0} {
            }
            set doingLine1 1
            set doingLine2 1
            set ch1 [open $filename(base$side)]
            set ch2 [open $filename(change$side)]
            set t 0
            foreach i $diffres {
                lassign $i line1 n1 line2 n2
                $self doText $ch $ch1 $ch2 $n1 $n2 $line1 $line2
            }
            $self doText $ch $ch1 $ch2 0 0 0 0
            close $ch1
            close $ch2
            close $ch
        }

        # Now run a diff window with the patch files
        set top [newDiff $outfile(1) $outfile(2)]

    }

    # See dotext in eskil.tcl for more info since this is similar
    method doText {ch ch1 ch2 n1 n2 line1 line2} {
        if {$n1 == 0 && $n2 == 0} {
            # All blocks have been processed. Continue until end of file.
            # TBD context
            return
        }
        set limit 3
        if {($line1 - $doingLine1 < (2 * $limit + 2))} {
            set limit -1
        }

        # Fill in context before change block

        if {$doingLine1 == 1} {
            set allowStartFill 0
        } else {
            set allowStartFill 1
        }
        set t 0
        while {$doingLine1 < $line1} {
            gets $ch1 apa
            gets $ch2 bepa
            if {$limit < 0 || ($t < $limit && $allowStartFill) || \
                        ($line1 - $doingLine1) <= $limit} {
                # Both sides are supposed to be equal, use one of them
                puts $ch "  $apa"
            } elseif {$t == $limit && $allowStartFill} {
                # TBD empty instead?
                puts $ch [string repeat "-" 78]
            }
            incr doingLine1
            incr doingLine2
            incr t
        }
 
        # Output diff
        for {set t 0} {$t < $n1} {incr t} {
            gets $ch1 apa
            puts $ch "- $apa"
            incr doingLine1
         }
        for {set t 0} {$t < $n2} {incr t} {
            gets $ch2 apa
            puts $ch "+ $apa"
            incr doingLine2
         }
    }
}

proc makeFourWayWin {} {
    set t 1
    set top .fourway$t
    while {[winfo exists $top]} {
        incr t
        set top .fourway$t
    }
    FourWay $top
}