Eskil

Diff
Login

Differences From Artifact [b9fda6e1bb]:

To Artifact [394cdd7e7e]:


22
23
24
25
26
27
28

29




30
31




32
33
34
35
36
37
38
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







+

+
+
+
+


+
+
+
+







# $Revision$
#----------------------------------------------------------------------

# Top level dialog, for doing fourway diff
snit::widget FourWay {
    hulltype toplevel
    widgetclass Toplevel
    # Static
    variable fields
    # Gui
    variable filesGui
    variable revsGui
    # Working copy of Gui
    variable files
    variable revs
    # Working variables
    variable origfiles
    variable origrevs
    variable revtype
    variable doingLine1
    variable doingLine2

    constructor {args} {
        eskilRegisterToplevel $win

        wm title $win "Four Way Diff"
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
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







-
+















-
+
+


-
+
+







        ttk::label $win.l3 -text "Base 2"
        ttk::label $win.l4 -text "Changed 2"
        set txt1 {
            First diff is made from Base 1 to Changed 1.\n
            If a file is empty and have a revision, the other file name is used.
        }
        addBalloon $win.l1 -fmt $txt1
        addBalloon $win.l2 -fmt txt1
        addBalloon $win.l2 -fmt $txt1
        set txt2 [string map {1 2 First Second} $txt1]
        addBalloon $win.l3 -fmt $txt2
        addBalloon $win.l4 -fmt $txt2

        ttk::label $win.el -text "File path"
        ttk::label $win.rl -text "Rev"
        addBalloon $win.rl -fmt {
            If you want to use a revisioned controlled file
            instead of the one on disk, add a revision here.
            E.g. 0 can be used for latest commited revision.
        }

        set n 0
        foreach field $fields {
            incr n
            ttk::entryX $win.e$n -width 60 -textvariable [myvar files($field)]
            ttk::entryX $win.e$n -width 60 \
                    -textvariable [myvar filesGui($field)]
            ttk::button $win.b$n -text "Browse" \
                    -command [mymethod browseFile $field]
            ttk::entryX $win.r$n -width 8 -textvariable  [myvar revs($field)]
            ttk::entryX $win.r$n -width 8 \
                    -textvariable [myvar revsGui($field)]
        }

        ttk::button $win.bd -text "Diff" -command [mymethod doFourWayDiff] \
                -underline 0 -width 8
        bind $win <Alt-d> [list $win.bd invoke]

        grid x       $win.el x       $win.rl -sticky w  -padx 3 -pady 3
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
144
145


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
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
124
125
126
127
128
129
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
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







-
+















-
+







-
-
+
+





-
-
+
+








-
+



+
-
-
+
+

-
-
+
+



-
-
+
+

-
-
+
+




-
-
+
+



+
-
-
-
+
+
+



-
-
+
+

-
+


-
+

-
+



-
-
+
+

-
+

-
+


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












-
+







-
-
-
-
-
+
+
+
+
+









-
-
+
+







    # File drop using TkDnd
    method fileDrop {field filesDropped} {
        if {$field eq "any"} {
            # Dropped outside the entry widgets. Try to be clever.
            set todo {}
            # Drop in empty fields first
            foreach field $fields {
                if {$files($field) eq ""} {
                if {$filesGui($field) eq ""} {
                    lappend todo $field
                }
            }
            # Fill fields otherwise
            if {[llength $todo] == 0} {
                set todo $fields
            }
        } else {
            set todo [list $field]
        }
        foreach fn $filesDropped field $todo {
            # Loop until any list ends
            if {$fn eq "" || $field eq ""} break
            # Sanity check
            if {[file exists $fn]} {
                set files($field) $fn
                set filesGui($field) $fn
            }
        }
    }

    # Browse for file
    method browseFile {field} {
        set initDir [pwd]
        if {$files($field) ne ""} {
            set initDir [file dirname $files($field)]
        if {$filesGui($field) ne ""} {
            set initDir [file dirname $filesGui($field)]
        } else {
            # Pick default dir from other files
            foreach other [lreverse $fields] {
                if {$other eq $field} continue
                puts $other
                if {$files($other) ne ""} {
                    set initDir [file dirname $files($other)]
                if {$filesGui($other) ne ""} {
                    set initDir [file dirname $filesGui($other)]
                    puts $initDir
                    break
                }
            }
        }
        set apa [myOpenFile -title "Select file" -initialdir $initDir \
                         -parent $win]
        if {$apa != ""} {
            set files($field) $apa
            set filesGui($field) $apa
        }
    }

    # Fill in working copies of variables
    method doFourWayDiff {} {
        # Copy to local vars to be able to replace with defaults and parsed
    method PrepareFw {} {
        # Copy to work vars to be able to replace with defaults and parsed
        foreach field $fields {
            set filename($field) $files($field)
            set rev($field) [string trim $revs($field)]
            set files($field) $filesGui($field)
            set revs($field) [string trim $revsGui($field)]
        }
        # Fill in defaults, if only one file is given
        foreach {from to} $fields {
            if {$filename($to) eq ""} {
                set filename($to) $files($from)
            if {$files($to) eq ""} {
                set files($to) $filesGui($from)
            }
            if {$filename($from) eq ""} {
                set filename($from) $files($to)
            if {$files($from) eq ""} {
                set files($from) $filesGui($to)
            }
        }
        # Remember originals for display, they might be replaced below
        foreach field $fields {
            set origfile($field) $filename($field)
            set origrev($field)  $rev($field)
            set origfiles($field) $files($field)
            set origrevs($field)  $revs($field)
        }
        # Figure out any revisions
        foreach field $fields {
            set revtype($field) ""
            # TODO: Move this to helper function in rev.tcl
            if {$rev($field) ne ""} {
                set revtype($field) [detectRevSystem $filename($field)]
            # TODO: Move this to helper function in rev.tcl ?
            if {$revs($field) ne ""} {
                set revtype($field) [detectRevSystem $files($field)]
                if {$revtype($field) eq ""} {
                    tk_messageBox -icon error -title "Eskil Error" \
                            -parent $win -message \
                            "Could not detect version system for file $filename($field)"
                    return
                            "Could not detect version system for file $files($field)"
                    return -level 2
                    # TBD continue
                    set rev($field) ""
                    set revs($field) ""
                    continue
                }
                set revList [list $rev($field)]
                set revList [list $revs($field)]
                set revList [eskil::rev::$revtype($field)::ParseRevs \
                                     $filename($field) $revList]
                                     $files($field) $revList]
                if {[llength $revList] == 0} {
                    tk_messageBox -icon error -title "Eskil Error" \
                            -parent $win -message \
                            "Could not parse revision for file $filename($field)"
                    return
                            "Could not parse revision for file $files($field)"
                    return -level 2
                    # TBD continue
                    set rev($field) ""
                    set revs($field) ""
                } else {
                    set rev($field) [lindex $revList 0]
                    set revs($field) [lindex $revList 0]
                }
            }
        }
    }

    method doFourWayDiff {} {
        $self PrepareFw
            # Still a revision?
            if {$rev($field) ne ""} {
                set filename($field) [tmpFile]
                eskil::rev::$revtype($field)::get $origfile($field) \
                        $filename($field) $rev($field)
        # Extract revisions
        foreach field $fields {
            if {$revs($field) ne ""} {
                # Replace with checkout copy
                set files($field) [tmpFile]
                eskil::rev::$revtype($field)::get $origfiles($field) \
                        $files($field) $revs($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]
                    $files(base$side) $files(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))"
                set line "$str $origfiles($field)"
                if {$revs($field) ne ""} {
                    append line "  Revision $revs($field)"
                    if {$origrevs($field) ne $revs($field)} {
                        append line " ($origrevs($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 ch1 [open $files(base$side)]
            set ch2 [open $files(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