Fossil SCM

fossil-scm / src / merge.tcl
Blame History Raw 646 lines
1
# Show details of a 3-way merge operation. The left-most column is the
2
# common ancestor. The next two columns are edits of that common ancestor.
3
# The right-most column is the result of the merge.
4
#
5
# Several variables will have been initialized:
6
#
7
# ncontext The number of lines of context to show on each change
8
#
9
# fossilexe Pathname of the fossil program
10
#
11
# filelist A list of "merge-type filename" pairs.
12
#
13
# darkmode Boolean. True for dark mode
14
#
15
# debug Boolean. True for debugging output
16
#
17
# If the "filelist" global variable is defined, then it is a list of
18
# alternating "merge-type names" (ex: UPDATE, MERGE, CONFLICT, ERROR) and
19
# filenames. In that case, the initial display shows the changes for
20
# the first pair on the list and there is a optionmenu that allows the
21
# user to select other fiels on the list.
22
#
23
# This header comment is stripped off by the "mkbuiltin.c" program.
24
#
25
package require Tk
26
27
array set CFG_light {
28
TITLE {Fossil Merge}
29
LN_COL_BG #dddddd
30
LN_COL_FG #444444
31
TXT_COL_BG #ffffff
32
TXT_COL_FG #000000
33
MKR_COL_BG #444444
34
MKR_COL_FG #dddddd
35
CHNG_BG #d0d070
36
ADD_BG #c0ffc0
37
RM_BG #ffc0c0
38
HR_FG #444444
39
HR_PAD_TOP 4
40
HR_PAD_BTM 8
41
FN_BG #444444
42
FN_FG #ffffff
43
FN_PAD 5
44
ERR_FG #ee0000
45
PADX 5
46
WIDTH 80
47
HEIGHT 45
48
LB_HEIGHT 25
49
}
50
51
array set CFG_dark {
52
TITLE {Fossil Merge}
53
LN_COL_BG #dddddd
54
LN_COL_FG #444444
55
TXT_COL_BG #3f3f3f
56
TXT_COL_FG #dcdccc
57
MKR_COL_BG #444444
58
MKR_COL_FG #dddddd
59
CHNG_BG #6a6a00
60
ADD_BG #57934c
61
RM_BG #ef6767
62
HR_FG #444444
63
HR_PAD_TOP 4
64
HR_PAD_BTM 8
65
FN_BG #5e5e5e
66
FN_FG #ffffff
67
FN_PAD 5
68
ERR_FG #ee0000
69
PADX 5
70
WIDTH 80
71
HEIGHT 45
72
LB_HEIGHT 25
73
}
74
75
array set CFG_arr {
76
0 CFG_light
77
1 CFG_dark
78
}
79
80
array set CFG [array get $CFG_arr($darkmode)]
81
82
if {![namespace exists ttk]} {
83
interp alias {} ::ttk::scrollbar {} ::scrollbar
84
interp alias {} ::ttk::menubutton {} ::menubutton
85
}
86
87
proc dehtml {x} {
88
set x [regsub -all {<[^>]*>} $x {}]
89
return [string map {&amp; & &lt; < &gt; > &#39; ' &quot; \"} $x]
90
}
91
92
proc cols {} {
93
return [list .lnA .txtA .lnB .txtB .lnC .txtC .lnD .txtD]
94
}
95
96
proc colType {c} {
97
regexp {[a-z]+} $c type
98
return $type
99
}
100
101
proc readMerge {args} {
102
global fossilexe ncontext current_file debug
103
if {$ncontext=="All"} {
104
set cmd "| $fossilexe merge-info -c -1"
105
} else {
106
set cmd "| $fossilexe merge-info -c $ncontext"
107
}
108
if {[info exists current_file]} {
109
regsub {^[A-Z]+ } $current_file {} fn
110
lappend cmd -tcl $fn
111
}
112
if {$debug} {
113
regsub {^\| +} $cmd {} cmd2
114
puts $cmd2
115
flush stdout
116
}
117
if {[catch {
118
set in [open $cmd r]
119
fconfigure $in -encoding utf-8
120
set mergetxt [read $in]
121
close $in
122
} msg]} {
123
tk_messageBox -message "Unable to run command: \"$cmd\""
124
set mergetxt {}
125
}
126
foreach c [cols] {
127
$c config -state normal
128
$c delete 1.0 end
129
}
130
set lnA 1
131
set lnB 1
132
set lnC 1
133
set lnD 1
134
foreach {A B C D} $mergetxt {
135
set key1 [string index $A 0]
136
if {$key1=="S"} {
137
scan [string range $A 1 end] "%d %d %d %d" nA nB nC nD
138
foreach x {A B C D} {
139
set N [set n$x]
140
incr ln$x $N
141
if {$N>0} {
142
.ln$x insert end ...\n hrln
143
.txt$x insert end [string repeat . 30]\n hrtxt
144
} else {
145
.ln$x insert end \n hrln
146
.txt$x insert end \n hrtxt
147
}
148
}
149
continue
150
}
151
set key2 [string index $B 0]
152
set key3 [string index $C 0]
153
set key4 [string index $D 0]
154
if {$key1=="."} {
155
.lnA insert end \n -
156
.txtA insert end \n -
157
} elseif {$key1=="N"} {
158
.nameA config -text [string range $A 1 end]
159
} else {
160
.lnA insert end $lnA\n -
161
incr lnA
162
if {$key1=="X"} {
163
.txtA insert end [string range $A 1 end]\n rm
164
} else {
165
.txtA insert end [string range $A 1 end]\n -
166
}
167
}
168
if {$key2=="."} {
169
.lnB insert end \n -
170
.txtB insert end \n -
171
} elseif {$key2=="N"} {
172
.nameB config -text [string range $B 1 end]
173
} else {
174
.lnB insert end $lnB\n -
175
incr lnB
176
if {$key4=="2"} {set tag chng} {set tag -}
177
if {$key2=="1"} {
178
.txtB insert end [string range $A 1 end]\n $tag
179
} elseif {$key2=="X"} {
180
.txtB insert end [string range $B 1 end]\n rm
181
} else {
182
.txtB insert end [string range $B 1 end]\n $tag
183
}
184
}
185
if {$key3=="."} {
186
.lnC insert end \n -
187
.txtC insert end \n -
188
} elseif {$key3=="N"} {
189
.nameC config -text [string range $C 1 end]
190
} else {
191
.lnC insert end $lnC\n -
192
incr lnC
193
if {$key4=="3"} {set tag add} {set tag -}
194
if {$key3=="1"} {
195
.txtC insert end [string range $A 1 end]\n $tag
196
} elseif {$key3=="2"} {
197
.txtC insert end [string range $B 1 end]\n chng
198
} elseif {$key3=="X"} {
199
.txtC insert end [string range $C 1 end]\n rm
200
} else {
201
.txtC insert end [string range $C 1 end]\n $tag
202
}
203
}
204
if {$key4=="."} {
205
.lnD insert end \n -
206
.txtD insert end \n -
207
} elseif {$key4=="N"} {
208
.nameD config -text [string range $D 1 end]
209
} else {
210
.lnD insert end $lnD\n -
211
incr lnD
212
if {$key4=="1"} {
213
.txtD insert end [string range $A 1 end]\n -
214
} elseif {$key4=="2"} {
215
.txtD insert end [string range $B 1 end]\n chng
216
} elseif {$key4=="3"} {
217
.txtD insert end [string range $C 1 end]\n add
218
} elseif {$key4=="X"} {
219
.txtD insert end [string range $D 1 end]\n rm
220
} else {
221
.txtD insert end [string range $D 1 end]\n -
222
}
223
}
224
}
225
foreach c [cols] {
226
set type [colType $c]
227
if {$type ne "txt"} {
228
$c config -width 6; # $widths($type)
229
}
230
$c config -state disabled
231
}
232
set mx $lnA
233
if {$lnB>$mx} {set mx $lnB}
234
if {$lnC>$mx} {set mx $lnC}
235
if {$lnD>$mx} {set mx $lnD}
236
global lnWidth
237
set lnWidth [string length [format +%d $mx]]
238
.lnA config -width $lnWidth
239
.lnB config -width $lnWidth
240
.lnC config -width $lnWidth
241
.lnD config -width $lnWidth
242
grid columnconfig . {0 2 4 6} -minsize $lnWidth
243
}
244
245
proc viewDiff {idx} {
246
.txtA yview $idx
247
.txtA xview moveto 0
248
}
249
250
proc cycleDiffs {{reverse 0}} {
251
if {$reverse} {
252
set range [.txtA tag prevrange fn @0,0 1.0]
253
if {$range eq ""} {
254
viewDiff {fn.last -1c}
255
} else {
256
viewDiff [lindex $range 0]
257
}
258
} else {
259
set range [.txtA tag nextrange fn {@0,0 +1c} end]
260
if {$range eq "" || [lindex [.txtA yview] 1] == 1} {
261
viewDiff fn.first
262
} else {
263
viewDiff [lindex $range 0]
264
}
265
}
266
}
267
268
proc xvis {col} {
269
set view [$col xview]
270
return [expr {[lindex $view 1]-[lindex $view 0]}]
271
}
272
273
proc scroll-x {args} {
274
set c .txt[expr {[xvis .txtA] < [xvis .txtB] ? "A" : "B"}]
275
eval $c xview $args
276
}
277
278
interp alias {} scroll-y {} .txtA yview
279
280
proc noop {args} {}
281
282
proc enableSync {axis} {
283
update idletasks
284
interp alias {} sync-$axis {}
285
rename _sync-$axis sync-$axis
286
}
287
288
proc disableSync {axis} {
289
rename sync-$axis _sync-$axis
290
interp alias {} sync-$axis {} noop
291
}
292
293
proc sync-y {first last} {
294
disableSync y
295
foreach c [cols] {
296
$c yview moveto $first
297
}
298
if {$first > 0 || $last < 1} {
299
grid .sby
300
.sby set $first $last
301
} else {
302
grid remove .sby
303
}
304
enableSync y
305
}
306
307
wm withdraw .
308
wm title . $CFG(TITLE)
309
wm iconname . $CFG(TITLE)
310
# Keystroke bindings for on the top-level window for navigation and
311
# control also fire when those same keystrokes are pressed in the
312
# Search entry box. Disable them, to prevent the diff screen from
313
# disappearing abruptly and unexpectedly when searching for "q".
314
#
315
bind . <Control-q> exit
316
bind . <Control-p> {catch searchPrev; break}
317
bind . <Control-n> {catch searchNext; break}
318
bind . <Escape><Escape> exit
319
bind . <Destroy> {after 0 exit}
320
bind . <Tab> {cycleDiffs; break}
321
bind . <<PrevWindow>> {cycleDiffs 1; break}
322
bind . <Control-f> {searchOnOff; break}
323
bind . <Control-g> {catch searchNext; break}
324
bind . <Return> {
325
event generate .bb.files <1>
326
event generate .bb.files <ButtonRelease-1>
327
break
328
}
329
foreach {key axis args} {
330
Up y {scroll -5 units}
331
k y {scroll -5 units}
332
Down y {scroll 5 units}
333
j y {scroll 5 units}
334
Left x {scroll -5 units}
335
h x {scroll -5 units}
336
Right x {scroll 5 units}
337
l x {scroll 5 units}
338
Prior y {scroll -1 page}
339
b y {scroll -1 page}
340
Next y {scroll 1 page}
341
space y {scroll 1 page}
342
Home y {moveto 0}
343
g y {moveto 0}
344
End y {moveto 1}
345
} {
346
bind . <$key> "scroll-$axis $args; break"
347
bind . <Shift-$key> continue
348
}
349
350
frame .bb
351
::ttk::menubutton .bb.diff2 -text {2-way diff} -menu .bb.diff2.m
352
menu .bb.diff2.m -tearoff 0
353
.bb.diff2.m add command -label {baseline vs local} -command {two-way 12}
354
.bb.diff2.m add command -label {baseline vs merge-in} -command {two-way 13}
355
.bb.diff2.m add command -label {baseline vs current} -command {two-way 14}
356
.bb.diff2.m add command -label {local vs merge-in} -command {two-way 23}
357
.bb.diff2.m add command -label {local vs current} -command {two-way 24}
358
.bb.diff2.m add command -label {merge-in vs current} -command {two-way 34}
359
360
# Bring up a separate two-way diff between a pair of columns
361
# the argument is one of:
362
# 12 Baseline versus Local
363
# 13 Baseline versus Merge-in
364
# 23 Local versus Merge-in
365
#
366
proc two-way {mode} {
367
global current_file fossilexe debug darkmode ncontext
368
regsub {^[A-Z]+ } $current_file {} fn
369
set cmd $fossilexe
370
lappend cmd merge-info --diff$mode $fn -c $ncontext
371
if {$darkmode} {
372
lappend cmd --dark
373
}
374
if {$debug} {
375
lappend cmd --tkdebug
376
puts $cmd
377
flush stdout
378
}
379
exec {*}$cmd &
380
}
381
382
set useOptionMenu 1
383
if {[info exists filelist]} {
384
set current_file "[lindex $filelist 0] [lindex $filelist 1]"
385
if {[llength $filelist]>2} {
386
trace add variable current_file write readMerge
387
388
if {$tcl_platform(os)=="Darwin" || [llength $filelist]<30} {
389
set fnlist {}
390
foreach {op fn} $filelist {lappend fnlist "$op $fn"}
391
tk_optionMenu .bb.files current_file {*}$fnlist
392
} else {
393
set useOptionMenu 0
394
::ttk::menubutton .bb.files -text $current_file
395
if {[tk windowingsystem] eq "win32"} {
396
::ttk::style theme use winnative
397
.bb.files configure -padding {20 1 10 2}
398
}
399
toplevel .wfiles
400
wm withdraw .wfiles
401
update idletasks
402
wm transient .wfiles .
403
wm overrideredirect .wfiles 1
404
set ht [expr {[llength $filelist]/2}]
405
if {$ht>$CFG(LB_HEIGHT)} {set ht $CFG(LB_HEIGHT)}
406
listbox .wfiles.lb -width 0 -height $ht -activestyle none \
407
-yscroll {.wfiles.sb set}
408
set mx 1
409
foreach {op fn} $filelist {
410
set n [string length $fn]
411
if {$n>$mx} {set mx $n}
412
.wfiles.lb insert end "$op $fn"
413
}
414
.bb.files config -width $mx
415
::ttk::scrollbar .wfiles.sb -command {.wfiles.lb yview}
416
grid .wfiles.lb .wfiles.sb -sticky ns
417
bind .bb.files <1> {
418
set x [winfo rootx %W]
419
set y [expr {[winfo rooty %W]+[winfo height %W]}]
420
wm geometry .wfiles +$x+$y
421
wm deiconify .wfiles
422
focus .wfiles.lb
423
}
424
bind .wfiles <FocusOut> {wm withdraw .wfiles}
425
bind .wfiles <Escape> {focus .}
426
foreach evt {1 Return} {
427
bind .wfiles.lb <$evt> {
428
set ii [%W curselection]
429
set ::current_file [%W get $ii]
430
.bb.files config -text $::current_file
431
focus .
432
break
433
}
434
}
435
bind .wfiles.lb <Motion> {
436
%W selection clear 0 end
437
%W selection set @%x,%y
438
}
439
}
440
}
441
}
442
443
label .bb.ctxtag -text "Context:"
444
set context_choices {3 6 12 25 50 100 All}
445
if {$ncontext<0} {set ncontext All}
446
trace add variable ncontext write readMerge
447
if {$tcl_platform(os)=="Darwin" || $useOptionMenu} {
448
tk_optionMenu .bb.ctx ncontext {*}$context_choices
449
} else {
450
::ttk::menubutton .bb.ctx -text $ncontext
451
if {[tk windowingsystem] eq "win32"} {
452
::ttk::style theme use winnative
453
.bb.ctx configure -padding {20 1 10 2}
454
}
455
toplevel .wctx
456
wm withdraw .wctx
457
update idletasks
458
wm transient .wctx .
459
wm overrideredirect .wctx 1
460
listbox .wctx.lb -width 0 -height 7 -activestyle none
461
.wctx.lb insert end {*}$context_choices
462
pack .wctx.lb
463
bind .bb.ctx <1> {
464
set x [winfo rootx %W]
465
set y [expr {[winfo rooty %W]+[winfo height %W]}]
466
wm geometry .wctx +$x+$y
467
wm deiconify .wctx
468
focus .wctx.lb
469
}
470
bind .wctx <FocusOut> {wm withdraw .wctx}
471
bind .wctx <Escape> {focus .}
472
foreach evt {1 Return} {
473
bind .wctx.lb <$evt> {
474
set ::ncontext [lindex $::context_choices [%W curselection]]
475
.bb.ctx config -text $::ncontext
476
focus .
477
break
478
}
479
}
480
bind .wctx.lb <Motion> {
481
%W selection clear 0 end
482
%W selection set @%x,%y
483
}
484
}
485
486
foreach {side syncCol} {A .txtA B .txtB C .txtC D .txtD} {
487
set ln .ln$side
488
text $ln -width 6
489
$ln tag config - -justify right
490
491
set txt .txt$side
492
text $txt -width $CFG(WIDTH) -height $CFG(HEIGHT) -wrap none \
493
-xscroll ".sbx$side set"
494
catch {$txt config -tabstyle wordprocessor} ;# Required for Tk>=8.5
495
foreach tag {add rm chng} {
496
$txt tag config $tag -background $CFG([string toupper $tag]_BG)
497
$txt tag lower $tag
498
}
499
$txt tag config fn -background $CFG(FN_BG) -foreground $CFG(FN_FG) \
500
-justify center
501
$txt tag config err -foreground $CFG(ERR_FG)
502
}
503
text .mkr
504
505
set mxwidth [lindex [wm maxsize .] 0]
506
while {$CFG(WIDTH)>=40} {
507
set wanted [expr {([winfo reqwidth .lnA]+[winfo reqwidth .txtA])*4+30}]
508
if {$wanted<=$mxwidth} break
509
incr CFG(WIDTH) -10
510
.txtA config -width $CFG(WIDTH)
511
.txtB config -width $CFG(WIDTH)
512
.txtC config -width $CFG(WIDTH)
513
.txtD config -width $CFG(WIDTH)
514
}
515
516
foreach c [cols] {
517
set keyPrefix [string toupper [colType $c]]_COL_
518
if {[tk windowingsystem] eq "win32"} {$c config -font {courier 9}}
519
$c config -bg $CFG(${keyPrefix}BG) -fg $CFG(${keyPrefix}FG) -borderwidth 0 \
520
-padx $CFG(PADX) -yscroll sync-y
521
$c tag config hrln -spacing1 $CFG(HR_PAD_TOP) -spacing3 $CFG(HR_PAD_BTM) \
522
-foreground $CFG(HR_FG) -justify right
523
$c tag config hrtxt -spacing1 $CFG(HR_PAD_TOP) -spacing3 $CFG(HR_PAD_BTM) \
524
-foreground $CFG(HR_FG) -justify center
525
$c tag config fn -spacing1 $CFG(FN_PAD) -spacing3 $CFG(FN_PAD)
526
bindtags $c ". $c Text all"
527
bind $c <1> {focus %W}
528
}
529
530
label .nameA
531
label .nameB
532
label .nameC
533
label .nameD -text {Merge Result}
534
::ttk::scrollbar .sby -command {.txtA yview} -orient vertical
535
::ttk::scrollbar .sbxA -command {.txtA xview} -orient horizontal
536
::ttk::scrollbar .sbxB -command {.txtB xview} -orient horizontal
537
::ttk::scrollbar .sbxC -command {.txtC xview} -orient horizontal
538
::ttk::scrollbar .sbxD -command {.txtD xview} -orient horizontal
539
frame .spacer
540
541
update idletasks
542
543
proc searchOnOff {} {
544
if {[info exists ::search]} {
545
unset ::search
546
.txtA tag remove search 1.0 end
547
.txtB tag remove search 1.0 end
548
.txtC tag remove search 1.0 end
549
.txtD tag remove search 1.0 end
550
pack forget .bb.sframe
551
focus .
552
} else {
553
set ::search .txtA
554
if {![winfo exists .bb.sframe]} {
555
frame .bb.sframe
556
::ttk::entry .bb.sframe.e -width 10
557
pack .bb.sframe.e -side left -fill y -expand 1
558
bind .bb.sframe.e <Return> {searchNext; break}
559
::ttk::button .bb.sframe.nx -text \u2193 -width 1 -command searchNext
560
::ttk::button .bb.sframe.pv -text \u2191 -width 1 -command searchPrev
561
tk_optionMenu .bb.sframe.typ ::search_type \
562
Exact {No Case} {RegExp} {Whole Word}
563
.bb.sframe.typ config -width 10
564
set ::search_type Exact
565
pack .bb.sframe.nx .bb.sframe.pv .bb.sframe.typ -side left
566
}
567
pack .bb.sframe -side left
568
after idle {focus .bb.sframe.e}
569
}
570
}
571
proc searchNext {} {searchStep -forwards +1 1.0 end}
572
proc searchPrev {} {searchStep -backwards -1 end 1.0}
573
proc searchStep {direction incr start stop} {
574
set pattern [.bb.sframe.e get]
575
if {$pattern==""} return
576
set count 0
577
set w $::search
578
switch $w {
579
.txtA {set other .txtB}
580
.txtB {set other .txtC}
581
.txtC {set other .txtD}
582
default {set other .txtA}
583
}
584
if {[lsearch [$w mark names] search]<0} {
585
$w mark set search $start
586
}
587
switch $::search_type {
588
Exact {set st -exact}
589
{No Case} {set st -nocase}
590
{RegExp} {set st -regexp}
591
{Whole Word} {set st -regexp; set pattern \\y$pattern\\y}
592
}
593
set idx [$w search -count count $direction $st -- \
594
$pattern "search $incr chars" $stop]
595
if {"$idx"==""} {
596
set idx [$other search -count count $direction $st -- $pattern $start $stop]
597
if {"$idx"!=""} {
598
set this $w
599
set w $other
600
set other $this
601
} else {
602
set idx [$w search -count count $direction $st -- $pattern $start $stop]
603
}
604
}
605
$w tag remove search 1.0 end
606
$w mark unset search
607
$other tag remove search 1.0 end
608
$other mark unset search
609
if {"$idx"!=""} {
610
$w mark set search $idx
611
$w yview -pickplace $idx
612
$w tag add search search "$idx +$count chars"
613
$w tag config search -background {#fcc000}
614
}
615
set ::search $w
616
}
617
::ttk::button .bb.quit -text {Quit} -command exit
618
::ttk::button .bb.search -text {Search} -command searchOnOff
619
pack .bb.quit -side left -fill y
620
pack .bb.diff2 -side left -fill y
621
if {[winfo exists .bb.files]} {
622
pack .bb.files -side left -fill y
623
}
624
pack .bb.ctxtag .bb.ctx -side left -fill y
625
pack .bb.search -side left -fill y
626
grid rowconfigure . 1 -weight 1 -minsize [winfo reqheight .nameA]
627
grid rowconfigure . 2 -weight 100
628
readMerge
629
grid .bb -row 0 -columnspan 8
630
grid .nameA -row 1 -column 1 -sticky ew
631
grid .nameB -row 1 -column 3 -sticky ew
632
grid .nameC -row 1 -column 5 -sticky ew
633
grid .nameD -row 1 -column 7 -sticky ew
634
eval grid [cols] -row 2 -sticky nsew
635
grid .sby -row 2 -column 8 -sticky ns
636
grid .sbxA -row 3 -column 1 -sticky ew
637
grid .sbxB -row 3 -column 3 -sticky ew
638
grid .sbxC -row 3 -column 5 -sticky ew
639
grid .sbxD -row 3 -column 7 -sticky ew
640
grid columnconfigure . {0 2 4 6} \
641
-weight 1 -uniform a -minsize [winfo reqwidth .lnA]
642
grid columnconfigure . {1 3 5 7} -weight 100 -uniform b
643
644
.spacer config -height [winfo height .sbxA]
645
wm deiconify .
646

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button