Eskil

Check-in [2f4c54f97a]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Refactor fourway code
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2f4c54f97a931f7907bd590d7861196849c30b9692e68ef86dd34f11ae943066
User & Date: peter 2018-05-13 07:48:57.516
Context
2018-05-13
08:02
Refactor fourway code check-in: 97e70bc905 user: peter tags: trunk
07:48
Refactor fourway code check-in: 2f4c54f97a user: peter tags: trunk
06:45
Use feature from psballoon to cleanup check-in: f867bb4b2d user: peter tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/eskil.syntax.
124
125
126
127
128
129
130
131
132
# This is the annotation needed for this object definition

##nagelfar syntax FourWay dc=_obj,FourWay p*
###nagelfar option FourWay
##nagelfar return FourWay _obj,FourWay
###nagelfar subcmd+ _obj,FourWay text newLine

##nagelfar implicitvarns snit::widget::FourWay self\ _obj,FourWay fields files revs doingLine1 doingLine2 win hull








|

124
125
126
127
128
129
130
131
132
# This is the annotation needed for this object definition

##nagelfar syntax FourWay dc=_obj,FourWay p*
###nagelfar option FourWay
##nagelfar return FourWay _obj,FourWay
###nagelfar subcmd+ _obj,FourWay text newLine

##nagelfar implicitvarns snit::widget::FourWay self\ _obj,FourWay fields files filesGui revs revsGui origfiles origrevs revtype doingLine1 doingLine2 win hull

Changes to src/fourway.tcl.
22
23
24
25
26
27
28

29




30
31




32
33
34
35
36
37
38
# $Revision$
#----------------------------------------------------------------------

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







>

>
>
>
>


>
>
>
>







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
        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
        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::button $win.b$n -text "Browse" \
                    -command [mymethod browseFile $field]
            ttk::entryX $win.r$n -width 8 -textvariable  [myvar revs($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







|















|
>


|
>







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
        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 filesGui($field)]
            ttk::button $win.b$n -text "Browse" \
                    -command [mymethod browseFile $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
    # 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 ""} {
                    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
            }
        }
    }

    # Browse for file
    method browseFile {field} {
        set initDir [pwd]
        if {$files($field) ne ""} {
            set initDir [file dirname $files($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)]
                    puts $initDir
                    break
                }
            }
        }
        set apa [myOpenFile -title "Select file" -initialdir $initDir \
                         -parent $win]
        if {$apa != ""} {
            set files($field) $apa
        }
    }


    method doFourWayDiff {} {
        # Copy to local 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)]
        }
        # Fill in defaults, if only one file is given
        foreach {from to} $fields {
            if {$filename($to) eq ""} {
                set filename($to) $files($from)
            }
            if {$filename($from) eq ""} {
                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 ""} {
                    tk_messageBox -icon error -title "Eskil Error" \
                            -parent $win -message \
                            "Could not detect version system for file $filename($field)"
                    return
                    # TBD continue
                    set rev($field) ""
                    continue
                }
                set revList [list $rev($field)]
                set revList [eskil::rev::$revtype($field)::ParseRevs \
                                     $filename($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
                    # TBD continue
                    set rev($field) ""
                } else {
                    set rev($field) [lindex $revList 0]
                }
            }





            # Still a revision?

            if {$rev($field) ne ""} {

                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







|















|







|
|





|
|








|



>
|
|

|
|



|
|

|
|




|
|



>
|
|
|



|
|

|


|

|



|
|

|

|


>
>
>
>
>
|
>
|
>
|
|
|












|







|
|
|
|
|









|
|







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 {$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 filesGui($field) $fn
            }
        }
    }

    # Browse for file
    method browseFile {field} {
        set initDir [pwd]
        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 {$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 filesGui($field) $apa
        }
    }

    # Fill in working copies of variables
    method PrepareFw {} {
        # Copy to work vars to be able to replace with defaults and parsed
        foreach field $fields {
            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 {$files($to) eq ""} {
                set files($to) $filesGui($from)
            }
            if {$files($from) eq ""} {
                set files($from) $filesGui($to)
            }
        }
        # Remember originals for display, they might be replaced below
        foreach field $fields {
            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 {$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 $files($field)"
                    return -level 2
                    # TBD continue
                    set revs($field) ""
                    continue
                }
                set revList [list $revs($field)]
                set revList [eskil::rev::$revtype($field)::ParseRevs \
                                     $files($field) $revList]
                if {[llength $revList] == 0} {
                    tk_messageBox -icon error -title "Eskil Error" \
                            -parent $win -message \
                            "Could not parse revision for file $files($field)"
                    return -level 2
                    # TBD continue
                    set revs($field) ""
                } else {
                    set revs($field) [lindex $revList 0]
                }
            }
        }
    }

    method doFourWayDiff {} {
        $self PrepareFw
        # 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 \
                    $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 $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 $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