3f74fbb0764edf0260e292ec26cc8d6fb482edd0
   1#!/bin/sh
   2# Tcl ignores the next line -*- tcl -*- \
   3exec wish "$0" -- "$@"
   4
   5# Copyright (C) 2006 Shawn Pearce, Paul Mackerras.  All rights reserved.
   6# This program is free software; it may be used, copied, modified
   7# and distributed under the terms of the GNU General Public Licence,
   8# either version 2, or (at your option) any later version.
   9
  10set appname [lindex [file split $argv0] end]
  11set gitdir {}
  12
  13######################################################################
  14##
  15## config
  16
  17proc is_many_config {name} {
  18        switch -glob -- $name {
  19        remote.*.fetch -
  20        remote.*.push
  21                {return 1}
  22        *
  23                {return 0}
  24        }
  25}
  26
  27proc load_config {include_global} {
  28        global repo_config global_config default_config
  29
  30        array unset global_config
  31        if {$include_global} {
  32                catch {
  33                        set fd_rc [open "| git repo-config --global --list" r]
  34                        while {[gets $fd_rc line] >= 0} {
  35                                if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
  36                                        if {[is_many_config $name]} {
  37                                                lappend global_config($name) $value
  38                                        } else {
  39                                                set global_config($name) $value
  40                                        }
  41                                }
  42                        }
  43                        close $fd_rc
  44                }
  45        }
  46
  47        array unset repo_config
  48        catch {
  49                set fd_rc [open "| git repo-config --list" r]
  50                while {[gets $fd_rc line] >= 0} {
  51                        if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
  52                                if {[is_many_config $name]} {
  53                                        lappend repo_config($name) $value
  54                                } else {
  55                                        set repo_config($name) $value
  56                                }
  57                        }
  58                }
  59                close $fd_rc
  60        }
  61
  62        foreach name [array names default_config] {
  63                if {[catch {set v $global_config($name)}]} {
  64                        set global_config($name) $default_config($name)
  65                }
  66                if {[catch {set v $repo_config($name)}]} {
  67                        set repo_config($name) $default_config($name)
  68                }
  69        }
  70}
  71
  72proc save_config {} {
  73        global default_config font_descs
  74        global repo_config global_config
  75        global repo_config_new global_config_new
  76
  77        foreach option $font_descs {
  78                set name [lindex $option 0]
  79                set font [lindex $option 1]
  80                font configure $font \
  81                        -family $global_config_new(gui.$font^^family) \
  82                        -size $global_config_new(gui.$font^^size)
  83                font configure ${font}bold \
  84                        -family $global_config_new(gui.$font^^family) \
  85                        -size $global_config_new(gui.$font^^size)
  86                set global_config_new(gui.$name) [font configure $font]
  87                unset global_config_new(gui.$font^^family)
  88                unset global_config_new(gui.$font^^size)
  89        }
  90
  91        foreach name [array names default_config] {
  92                set value $global_config_new($name)
  93                if {$value ne $global_config($name)} {
  94                        if {$value eq $default_config($name)} {
  95                                catch {exec git repo-config --global --unset $name}
  96                        } else {
  97                                regsub -all "\[{}\]" $value {"} value
  98                                exec git repo-config --global $name $value
  99                        }
 100                        set global_config($name) $value
 101                        if {$value eq $repo_config($name)} {
 102                                catch {exec git repo-config --unset $name}
 103                                set repo_config($name) $value
 104                        }
 105                }
 106        }
 107
 108        foreach name [array names default_config] {
 109                set value $repo_config_new($name)
 110                if {$value ne $repo_config($name)} {
 111                        if {$value eq $global_config($name)} {
 112                                catch {exec git repo-config --unset $name}
 113                        } else {
 114                                regsub -all "\[{}\]" $value {"} value
 115                                exec git repo-config $name $value
 116                        }
 117                        set repo_config($name) $value
 118                }
 119        }
 120}
 121
 122proc error_popup {msg} {
 123        global gitdir appname
 124
 125        set title $appname
 126        if {$gitdir ne {}} {
 127                append title { (}
 128                append title [lindex \
 129                        [file split [file normalize [file dirname $gitdir]]] \
 130                        end]
 131                append title {)}
 132        }
 133        tk_messageBox \
 134                -parent . \
 135                -icon error \
 136                -type ok \
 137                -title "$title: error" \
 138                -message $msg
 139}
 140
 141proc info_popup {msg} {
 142        global gitdir appname
 143
 144        set title $appname
 145        if {$gitdir ne {}} {
 146                append title { (}
 147                append title [lindex \
 148                        [file split [file normalize [file dirname $gitdir]]] \
 149                        end]
 150                append title {)}
 151        }
 152        tk_messageBox \
 153                -parent . \
 154                -icon error \
 155                -type ok \
 156                -title $title \
 157                -message $msg
 158}
 159
 160######################################################################
 161##
 162## repository setup
 163
 164if {   [catch {set cdup [exec git rev-parse --show-cdup]} err]
 165        || [catch {set gitdir [exec git rev-parse --git-dir]} err]} {
 166        catch {wm withdraw .}
 167        error_popup "Cannot find the git directory:\n\n$err"
 168        exit 1
 169}
 170if {$cdup ne ""} {
 171        cd $cdup
 172}
 173unset cdup
 174
 175set single_commit 0
 176if {$appname eq {git-citool}} {
 177        set single_commit 1
 178}
 179
 180######################################################################
 181##
 182## task management
 183
 184set status_active 0
 185set diff_active 0
 186
 187set disable_on_lock [list]
 188set index_lock_type none
 189
 190set HEAD {}
 191set PARENT {}
 192set commit_type {}
 193
 194proc lock_index {type} {
 195        global index_lock_type disable_on_lock
 196
 197        if {$index_lock_type eq {none}} {
 198                set index_lock_type $type
 199                foreach w $disable_on_lock {
 200                        uplevel #0 $w disabled
 201                }
 202                return 1
 203        } elseif {$index_lock_type eq {begin-update} && $type eq {update}} {
 204                set index_lock_type $type
 205                return 1
 206        }
 207        return 0
 208}
 209
 210proc unlock_index {} {
 211        global index_lock_type disable_on_lock
 212
 213        set index_lock_type none
 214        foreach w $disable_on_lock {
 215                uplevel #0 $w normal
 216        }
 217}
 218
 219######################################################################
 220##
 221## status
 222
 223proc repository_state {hdvar ctvar} {
 224        global gitdir
 225        upvar $hdvar hd $ctvar ct
 226
 227        if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
 228                set ct initial
 229        } elseif {[file exists [file join $gitdir MERGE_HEAD]]} {
 230                set ct merge
 231        } else {
 232                set ct normal
 233        }
 234}
 235
 236proc update_status {{final Ready.}} {
 237        global HEAD PARENT commit_type
 238        global ui_index ui_other ui_status_value ui_comm
 239        global status_active file_states
 240        global repo_config
 241
 242        if {$status_active || ![lock_index read]} return
 243
 244        repository_state new_HEAD new_type
 245        if {$commit_type eq {amend}
 246                && $new_type eq {normal}
 247                && $new_HEAD eq $HEAD} {
 248        } else {
 249                set HEAD $new_HEAD
 250                set PARENT $new_HEAD
 251                set commit_type $new_type
 252        }
 253
 254        array unset file_states
 255
 256        if {![$ui_comm edit modified]
 257                || [string trim [$ui_comm get 0.0 end]] eq {}} {
 258                if {[load_message GITGUI_MSG]} {
 259                } elseif {[load_message MERGE_MSG]} {
 260                } elseif {[load_message SQUASH_MSG]} {
 261                }
 262                $ui_comm edit modified false
 263                $ui_comm edit reset
 264        }
 265
 266        if {$repo_config(gui.trustmtime) eq {true}} {
 267                update_status_stage2 {} $final
 268        } else {
 269                set status_active 1
 270                set ui_status_value {Refreshing file status...}
 271                set cmd [list git update-index]
 272                lappend cmd -q
 273                lappend cmd --unmerged
 274                lappend cmd --ignore-missing
 275                lappend cmd --refresh
 276                set fd_rf [open "| $cmd" r]
 277                fconfigure $fd_rf -blocking 0 -translation binary
 278                fileevent $fd_rf readable \
 279                        [list update_status_stage2 $fd_rf $final]
 280        }
 281}
 282
 283proc update_status_stage2 {fd final} {
 284        global gitdir PARENT commit_type
 285        global ui_index ui_other ui_status_value ui_comm
 286        global status_active
 287        global buf_rdi buf_rdf buf_rlo
 288
 289        if {$fd ne {}} {
 290                read $fd
 291                if {![eof $fd]} return
 292                close $fd
 293        }
 294
 295        set ls_others [list | git ls-files --others -z \
 296                --exclude-per-directory=.gitignore]
 297        set info_exclude [file join $gitdir info exclude]
 298        if {[file readable $info_exclude]} {
 299                lappend ls_others "--exclude-from=$info_exclude"
 300        }
 301
 302        set buf_rdi {}
 303        set buf_rdf {}
 304        set buf_rlo {}
 305
 306        set status_active 3
 307        set ui_status_value {Scanning for modified files ...}
 308        set fd_di [open "| git diff-index --cached -z $PARENT" r]
 309        set fd_df [open "| git diff-files -z" r]
 310        set fd_lo [open $ls_others r]
 311
 312        fconfigure $fd_di -blocking 0 -translation binary
 313        fconfigure $fd_df -blocking 0 -translation binary
 314        fconfigure $fd_lo -blocking 0 -translation binary
 315        fileevent $fd_di readable [list read_diff_index $fd_di $final]
 316        fileevent $fd_df readable [list read_diff_files $fd_df $final]
 317        fileevent $fd_lo readable [list read_ls_others $fd_lo $final]
 318}
 319
 320proc load_message {file} {
 321        global gitdir ui_comm
 322
 323        set f [file join $gitdir $file]
 324        if {[file isfile $f]} {
 325                if {[catch {set fd [open $f r]}]} {
 326                        return 0
 327                }
 328                set content [string trim [read $fd]]
 329                close $fd
 330                $ui_comm delete 0.0 end
 331                $ui_comm insert end $content
 332                return 1
 333        }
 334        return 0
 335}
 336
 337proc read_diff_index {fd final} {
 338        global buf_rdi
 339
 340        append buf_rdi [read $fd]
 341        set c 0
 342        set n [string length $buf_rdi]
 343        while {$c < $n} {
 344                set z1 [string first "\0" $buf_rdi $c]
 345                if {$z1 == -1} break
 346                incr z1
 347                set z2 [string first "\0" $buf_rdi $z1]
 348                if {$z2 == -1} break
 349
 350                set c $z2
 351                incr z2 -1
 352                display_file \
 353                        [string range $buf_rdi $z1 $z2] \
 354                        [string index $buf_rdi [expr $z1 - 2]]_
 355                incr c
 356        }
 357        if {$c < $n} {
 358                set buf_rdi [string range $buf_rdi $c end]
 359        } else {
 360                set buf_rdi {}
 361        }
 362
 363        status_eof $fd buf_rdi $final
 364}
 365
 366proc read_diff_files {fd final} {
 367        global buf_rdf
 368
 369        append buf_rdf [read $fd]
 370        set c 0
 371        set n [string length $buf_rdf]
 372        while {$c < $n} {
 373                set z1 [string first "\0" $buf_rdf $c]
 374                if {$z1 == -1} break
 375                incr z1
 376                set z2 [string first "\0" $buf_rdf $z1]
 377                if {$z2 == -1} break
 378
 379                set c $z2
 380                incr z2 -1
 381                display_file \
 382                        [string range $buf_rdf $z1 $z2] \
 383                        _[string index $buf_rdf [expr $z1 - 2]]
 384                incr c
 385        }
 386        if {$c < $n} {
 387                set buf_rdf [string range $buf_rdf $c end]
 388        } else {
 389                set buf_rdf {}
 390        }
 391
 392        status_eof $fd buf_rdf $final
 393}
 394
 395proc read_ls_others {fd final} {
 396        global buf_rlo
 397
 398        append buf_rlo [read $fd]
 399        set pck [split $buf_rlo "\0"]
 400        set buf_rlo [lindex $pck end]
 401        foreach p [lrange $pck 0 end-1] {
 402                display_file $p _O
 403        }
 404        status_eof $fd buf_rlo $final
 405}
 406
 407proc status_eof {fd buf final} {
 408        global status_active ui_status_value
 409        upvar $buf to_clear
 410
 411        if {[eof $fd]} {
 412                set to_clear {}
 413                close $fd
 414
 415                if {[incr status_active -1] == 0} {
 416                        display_all_files
 417                        unlock_index
 418                        reshow_diff
 419                        set ui_status_value $final
 420                }
 421        }
 422}
 423
 424######################################################################
 425##
 426## diff
 427
 428proc clear_diff {} {
 429        global ui_diff ui_fname_value ui_fstatus_value ui_index ui_other
 430
 431        $ui_diff conf -state normal
 432        $ui_diff delete 0.0 end
 433        $ui_diff conf -state disabled
 434
 435        set ui_fname_value {}
 436        set ui_fstatus_value {}
 437
 438        $ui_index tag remove in_diff 0.0 end
 439        $ui_other tag remove in_diff 0.0 end
 440}
 441
 442proc reshow_diff {} {
 443        global ui_fname_value ui_status_value file_states
 444
 445        if {$ui_fname_value eq {}
 446                || [catch {set s $file_states($ui_fname_value)}]} {
 447                clear_diff
 448        } else {
 449                show_diff $ui_fname_value
 450        }
 451}
 452
 453proc handle_empty_diff {} {
 454        global ui_fname_value file_states file_lists
 455
 456        set path $ui_fname_value
 457        set s $file_states($path)
 458        if {[lindex $s 0] ne {_M}} return
 459
 460        info_popup "No differences detected.
 461
 462[short_path $path] has no changes.
 463
 464The modification date of this file was updated by another
 465application and you currently have the Trust File Modification
 466Timestamps option enabled, so Git did not automatically detect
 467that there are no content differences in this file.
 468
 469This file will now be removed from the modified files list, to
 470prevent possible confusion.
 471"
 472        if {[catch {exec git update-index -- $path} err]} {
 473                error_popup "Failed to refresh index:\n\n$err"
 474        }
 475
 476        clear_diff
 477        set old_w [mapcol [lindex $file_states($path) 0] $path]
 478        set lno [lsearch -sorted $file_lists($old_w) $path]
 479        if {$lno >= 0} {
 480                set file_lists($old_w) \
 481                        [lreplace $file_lists($old_w) $lno $lno]
 482                incr lno
 483                $old_w conf -state normal
 484                $old_w delete $lno.0 [expr $lno + 1].0
 485                $old_w conf -state disabled
 486        }
 487}
 488
 489proc show_diff {path {w {}} {lno {}}} {
 490        global file_states file_lists
 491        global PARENT diff_3way diff_active repo_config
 492        global ui_diff ui_fname_value ui_fstatus_value ui_status_value
 493
 494        if {$diff_active || ![lock_index read]} return
 495
 496        clear_diff
 497        if {$w eq {} || $lno == {}} {
 498                foreach w [array names file_lists] {
 499                        set lno [lsearch -sorted $file_lists($w) $path]
 500                        if {$lno >= 0} {
 501                                incr lno
 502                                break
 503                        }
 504                }
 505        }
 506        if {$w ne {} && $lno >= 1} {
 507                $w tag add in_diff $lno.0 [expr $lno + 1].0
 508        }
 509
 510        set s $file_states($path)
 511        set m [lindex $s 0]
 512        set diff_3way 0
 513        set diff_active 1
 514        set ui_fname_value [escape_path $path]
 515        set ui_fstatus_value [mapdesc $m $path]
 516        set ui_status_value "Loading diff of [escape_path $path]..."
 517
 518        set cmd [list | git diff-index]
 519        lappend cmd --no-color
 520        if {$repo_config(gui.diffcontext) > 0} {
 521                lappend cmd "-U$repo_config(gui.diffcontext)"
 522        }
 523        lappend cmd -p
 524
 525        switch $m {
 526        MM {
 527                lappend cmd -c
 528        }
 529        _O {
 530                if {[catch {
 531                                set fd [open $path r]
 532                                set content [read $fd]
 533                                close $fd
 534                        } err ]} {
 535                        set diff_active 0
 536                        unlock_index
 537                        set ui_status_value "Unable to display [escape_path $path]"
 538                        error_popup "Error loading file:\n\n$err"
 539                        return
 540                }
 541                $ui_diff conf -state normal
 542                $ui_diff insert end $content
 543                $ui_diff conf -state disabled
 544                set diff_active 0
 545                unlock_index
 546                set ui_status_value {Ready.}
 547                return
 548        }
 549        }
 550
 551        lappend cmd $PARENT
 552        lappend cmd --
 553        lappend cmd $path
 554
 555        if {[catch {set fd [open $cmd r]} err]} {
 556                set diff_active 0
 557                unlock_index
 558                set ui_status_value "Unable to display [escape_path $path]"
 559                error_popup "Error loading diff:\n\n$err"
 560                return
 561        }
 562
 563        fconfigure $fd -blocking 0 -translation auto
 564        fileevent $fd readable [list read_diff $fd]
 565}
 566
 567proc read_diff {fd} {
 568        global ui_diff ui_status_value diff_3way diff_active
 569        global repo_config
 570
 571        while {[gets $fd line] >= 0} {
 572                if {[string match {diff --git *} $line]} continue
 573                if {[string match {diff --combined *} $line]} continue
 574                if {[string match {--- *} $line]} continue
 575                if {[string match {+++ *} $line]} continue
 576                if {[string match index* $line]} {
 577                        if {[string first , $line] >= 0} {
 578                                set diff_3way 1
 579                        }
 580                }
 581
 582                $ui_diff conf -state normal
 583                if {!$diff_3way} {
 584                        set x [string index $line 0]
 585                        switch -- $x {
 586                        "@" {set tags da}
 587                        "+" {set tags dp}
 588                        "-" {set tags dm}
 589                        default {set tags {}}
 590                        }
 591                } else {
 592                        set x [string range $line 0 1]
 593                        switch -- $x {
 594                        default {set tags {}}
 595                        "@@" {set tags da}
 596                        "++" {set tags dp; set x " +"}
 597                        " +" {set tags {di bold}; set x "++"}
 598                        "+ " {set tags dni; set x "-+"}
 599                        "--" {set tags dm; set x " -"}
 600                        " -" {set tags {dm bold}; set x "--"}
 601                        "- " {set tags di; set x "+-"}
 602                        default {set tags {}}
 603                        }
 604                        set line [string replace $line 0 1 $x]
 605                }
 606                $ui_diff insert end $line $tags
 607                $ui_diff insert end "\n"
 608                $ui_diff conf -state disabled
 609        }
 610
 611        if {[eof $fd]} {
 612                close $fd
 613                set diff_active 0
 614                unlock_index
 615                set ui_status_value {Ready.}
 616
 617                if {$repo_config(gui.trustmtime) eq {true}
 618                        && [$ui_diff index end] eq {2.0}} {
 619                        handle_empty_diff
 620                }
 621        }
 622}
 623
 624######################################################################
 625##
 626## commit
 627
 628proc load_last_commit {} {
 629        global HEAD PARENT commit_type ui_comm
 630
 631        if {$commit_type eq {amend}} return
 632        if {$commit_type ne {normal}} {
 633                error_popup "Can't amend a $commit_type commit."
 634                return
 635        }
 636
 637        set msg {}
 638        set parent {}
 639        set parent_count 0
 640        if {[catch {
 641                        set fd [open "| git cat-file commit $HEAD" r]
 642                        while {[gets $fd line] > 0} {
 643                                if {[string match {parent *} $line]} {
 644                                        set parent [string range $line 7 end]
 645                                        incr parent_count
 646                                }
 647                        }
 648                        set msg [string trim [read $fd]]
 649                        close $fd
 650                } err]} {
 651                error_popup "Error loading commit data for amend:\n\n$err"
 652                return
 653        }
 654
 655        if {$parent_count == 0} {
 656                set commit_type amend
 657                set HEAD {}
 658                set PARENT {}
 659                update_status
 660        } elseif {$parent_count == 1} {
 661                set commit_type amend
 662                set PARENT $parent
 663                $ui_comm delete 0.0 end
 664                $ui_comm insert end $msg
 665                $ui_comm edit modified false
 666                $ui_comm edit reset
 667                update_status
 668        } else {
 669                error_popup {You can't amend a merge commit.}
 670                return
 671        }
 672}
 673
 674proc commit_tree {} {
 675        global tcl_platform HEAD gitdir commit_type file_states
 676        global pch_error
 677        global ui_status_value ui_comm
 678
 679        if {![lock_index update]} return
 680
 681        # -- Our in memory state should match the repository.
 682        #
 683        repository_state curHEAD cur_type
 684        if {$commit_type eq {amend}
 685                && $cur_type eq {normal}
 686                && $curHEAD eq $HEAD} {
 687        } elseif {$commit_type ne $cur_type || $HEAD ne $curHEAD} {
 688                error_popup {Last scanned state does not match repository state.
 689
 690Its highly likely that another Git program modified the
 691repository since our last scan.  A rescan is required
 692before committing.
 693}
 694                unlock_index
 695                update_status
 696                return
 697        }
 698
 699        # -- At least one file should differ in the index.
 700        #
 701        set files_ready 0
 702        foreach path [array names file_states] {
 703                set s $file_states($path)
 704                switch -glob -- [lindex $s 0] {
 705                _? {continue}
 706                A? -
 707                D? -
 708                M? {set files_ready 1; break}
 709                U? {
 710                        error_popup "Unmerged files cannot be committed.
 711
 712File [short_path $path] has merge conflicts.
 713You must resolve them and include the file before committing.
 714"
 715                        unlock_index
 716                        return
 717                }
 718                default {
 719                        error_popup "Unknown file state [lindex $s 0] detected.
 720
 721File [short_path $path] cannot be committed by this program.
 722"
 723                }
 724                }
 725        }
 726        if {!$files_ready} {
 727                error_popup {No included files to commit.
 728
 729You must include at least 1 file before you can commit.
 730}
 731                unlock_index
 732                return
 733        }
 734
 735        # -- A message is required.
 736        #
 737        set msg [string trim [$ui_comm get 1.0 end]]
 738        if {$msg eq {}} {
 739                error_popup {Please supply a commit message.
 740
 741A good commit message has the following format:
 742
 743- First line: Describe in one sentance what you did.
 744- Second line: Blank
 745- Remaining lines: Describe why this change is good.
 746}
 747                unlock_index
 748                return
 749        }
 750
 751        # -- Ask the pre-commit hook for the go-ahead.
 752        #
 753        set pchook [file join $gitdir hooks pre-commit]
 754        if {$tcl_platform(platform) eq {windows} && [file isfile $pchook]} {
 755                set pchook [list sh -c [concat \
 756                        "if test -x \"$pchook\";" \
 757                        "then exec \"$pchook\" 2>&1;" \
 758                        "fi"]]
 759        } elseif {[file executable $pchook]} {
 760                set pchook [list $pchook |& cat]
 761        } else {
 762                set pchook {}
 763        }
 764        if {$pchook ne {}} {
 765                set ui_status_value {Calling pre-commit hook...}
 766                set pch_error {}
 767                set fd_ph [open "| $pchook" r]
 768                fconfigure $fd_ph -blocking 0 -translation binary
 769                fileevent $fd_ph readable \
 770                        [list commit_stage1 $fd_ph $curHEAD $msg]
 771        } else {
 772                commit_stage2 $curHEAD $msg
 773        }
 774}
 775
 776proc commit_stage1 {fd_ph curHEAD msg} {
 777        global pch_error ui_status_value
 778
 779        append pch_error [read $fd_ph]
 780        fconfigure $fd_ph -blocking 1
 781        if {[eof $fd_ph]} {
 782                if {[catch {close $fd_ph}]} {
 783                        set ui_status_value {Commit declined by pre-commit hook.}
 784                        hook_failed_popup pre-commit $pch_error
 785                        unlock_index
 786                } else {
 787                        commit_stage2 $curHEAD $msg
 788                }
 789                set pch_error {}
 790        } else {
 791                fconfigure $fd_ph -blocking 0
 792        }
 793}
 794
 795proc commit_stage2 {curHEAD msg} {
 796        global ui_status_value
 797
 798        # -- Write the tree in the background.
 799        #
 800        set ui_status_value {Committing changes...}
 801        set fd_wt [open "| git write-tree" r]
 802        fileevent $fd_wt readable [list commit_stage3 $fd_wt $curHEAD $msg]
 803}
 804
 805proc commit_stage3 {fd_wt curHEAD msg} {
 806        global single_commit gitdir HEAD PARENT commit_type tcl_platform
 807        global ui_status_value ui_comm
 808        global file_states
 809
 810        gets $fd_wt tree_id
 811        if {$tree_id eq {} || [catch {close $fd_wt} err]} {
 812                error_popup "write-tree failed:\n\n$err"
 813                set ui_status_value {Commit failed.}
 814                unlock_index
 815                return
 816        }
 817
 818        # -- Create the commit.
 819        #
 820        set cmd [list git commit-tree $tree_id]
 821        if {$PARENT ne {}} {
 822                lappend cmd -p $PARENT
 823        }
 824        if {$commit_type eq {merge}} {
 825                if {[catch {
 826                                set fd_mh [open [file join $gitdir MERGE_HEAD] r]
 827                                while {[gets $fd_mh merge_head] >= 0} {
 828                                        lappend cmd -p $merge_head
 829                                }
 830                                close $fd_mh
 831                        } err]} {
 832                        error_popup "Loading MERGE_HEAD failed:\n\n$err"
 833                        set ui_status_value {Commit failed.}
 834                        unlock_index
 835                        return
 836                }
 837        }
 838        if {$PARENT eq {}} {
 839                # git commit-tree writes to stderr during initial commit.
 840                lappend cmd 2>/dev/null
 841        }
 842        lappend cmd << $msg
 843        if {[catch {set cmt_id [eval exec $cmd]} err]} {
 844                error_popup "commit-tree failed:\n\n$err"
 845                set ui_status_value {Commit failed.}
 846                unlock_index
 847                return
 848        }
 849
 850        # -- Update the HEAD ref.
 851        #
 852        set reflogm commit
 853        if {$commit_type ne {normal}} {
 854                append reflogm " ($commit_type)"
 855        }
 856        set i [string first "\n" $msg]
 857        if {$i >= 0} {
 858                append reflogm {: } [string range $msg 0 [expr $i - 1]]
 859        } else {
 860                append reflogm {: } $msg
 861        }
 862        set cmd [list git update-ref -m $reflogm HEAD $cmt_id $curHEAD]
 863        if {[catch {eval exec $cmd} err]} {
 864                error_popup "update-ref failed:\n\n$err"
 865                set ui_status_value {Commit failed.}
 866                unlock_index
 867                return
 868        }
 869
 870        # -- Cleanup after ourselves.
 871        #
 872        catch {file delete [file join $gitdir MERGE_HEAD]}
 873        catch {file delete [file join $gitdir MERGE_MSG]}
 874        catch {file delete [file join $gitdir SQUASH_MSG]}
 875        catch {file delete [file join $gitdir GITGUI_MSG]}
 876
 877        # -- Let rerere do its thing.
 878        #
 879        if {[file isdirectory [file join $gitdir rr-cache]]} {
 880                catch {exec git rerere}
 881        }
 882
 883        # -- Run the post-commit hook.
 884        #
 885        set pchook [file join $gitdir hooks post-commit]
 886        if {$tcl_platform(platform) eq {windows} && [file isfile $pchook]} {
 887                set pchook [list sh -c [concat \
 888                        "if test -x \"$pchook\";" \
 889                        "then exec \"$pchook\";" \
 890                        "fi"]]
 891        } elseif {![file executable $pchook]} {
 892                set pchook {}
 893        }
 894        if {$pchook ne {}} {
 895                catch {exec $pchook &}
 896        }
 897
 898        $ui_comm delete 0.0 end
 899        $ui_comm edit modified false
 900        $ui_comm edit reset
 901
 902        if {$single_commit} do_quit
 903
 904        # -- Update status without invoking any git commands.
 905        #
 906        set commit_type normal
 907        set HEAD $cmt_id
 908        set PARENT $cmt_id
 909
 910        foreach path [array names file_states] {
 911                set s $file_states($path)
 912                set m [lindex $s 0]
 913                switch -glob -- $m {
 914                A? -
 915                M? -
 916                D? {set m _[string index $m 1]}
 917                }
 918
 919                if {$m eq {__}} {
 920                        unset file_states($path)
 921                } else {
 922                        lset file_states($path) 0 $m
 923                }
 924        }
 925
 926        display_all_files
 927        unlock_index
 928        reshow_diff
 929        set ui_status_value \
 930                "Changes committed as [string range $cmt_id 0 7]."
 931}
 932
 933######################################################################
 934##
 935## fetch pull push
 936
 937proc fetch_from {remote} {
 938        set w [new_console "fetch $remote" \
 939                "Fetching new changes from $remote"]
 940        set cmd [list git fetch]
 941        lappend cmd $remote
 942        console_exec $w $cmd
 943}
 944
 945proc pull_remote {remote branch} {
 946        global HEAD commit_type file_states repo_config
 947
 948        if {![lock_index update]} return
 949
 950        # -- Our in memory state should match the repository.
 951        #
 952        repository_state curHEAD cur_type
 953        if {$commit_type ne $cur_type || $HEAD ne $curHEAD} {
 954                error_popup {Last scanned state does not match repository state.
 955
 956Its highly likely that another Git program modified the
 957repository since our last scan.  A rescan is required
 958before a pull can be started.
 959}
 960                unlock_index
 961                update_status
 962                return
 963        }
 964
 965        # -- No differences should exist before a pull.
 966        #
 967        if {[array size file_states] != 0} {
 968                error_popup {Uncommitted but modified files are present.
 969
 970You should not perform a pull with unmodified files in your working
 971directory as Git would be unable to recover from an incorrect merge.
 972
 973Commit or throw away all changes before starting a pull operation.
 974}
 975                unlock_index
 976                return
 977        }
 978
 979        set w [new_console "pull $remote $branch" \
 980                "Pulling new changes from branch $branch in $remote"]
 981        set cmd [list git pull]
 982        if {$repo_config(gui.pullsummary) eq {false}} {
 983                lappend cmd --no-summary
 984        }
 985        lappend cmd $remote
 986        lappend cmd $branch
 987        console_exec $w $cmd [list post_pull_remote $remote $branch]
 988}
 989
 990proc post_pull_remote {remote branch success} {
 991        global HEAD PARENT commit_type
 992        global ui_status_value
 993
 994        unlock_index
 995        if {$success} {
 996                repository_state HEAD commit_type
 997                set PARENT $HEAD
 998                set $ui_status_value {Ready.}
 999        } else {
1000                update_status \
1001                        "Conflicts detected while pulling $branch from $remote."
1002        }
1003}
1004
1005proc push_to {remote} {
1006        set w [new_console "push $remote" \
1007                "Pushing changes to $remote"]
1008        set cmd [list git push]
1009        lappend cmd $remote
1010        console_exec $w $cmd
1011}
1012
1013######################################################################
1014##
1015## ui helpers
1016
1017proc mapcol {state path} {
1018        global all_cols ui_other
1019
1020        if {[catch {set r $all_cols($state)}]} {
1021                puts "error: no column for state={$state} $path"
1022                return $ui_other
1023        }
1024        return $r
1025}
1026
1027proc mapicon {state path} {
1028        global all_icons
1029
1030        if {[catch {set r $all_icons($state)}]} {
1031                puts "error: no icon for state={$state} $path"
1032                return file_plain
1033        }
1034        return $r
1035}
1036
1037proc mapdesc {state path} {
1038        global all_descs
1039
1040        if {[catch {set r $all_descs($state)}]} {
1041                puts "error: no desc for state={$state} $path"
1042                return $state
1043        }
1044        return $r
1045}
1046
1047proc escape_path {path} {
1048        regsub -all "\n" $path "\\n" path
1049        return $path
1050}
1051
1052proc short_path {path} {
1053        return [escape_path [lindex [file split $path] end]]
1054}
1055
1056set next_icon_id 0
1057
1058proc merge_state {path new_state} {
1059        global file_states next_icon_id
1060
1061        set s0 [string index $new_state 0]
1062        set s1 [string index $new_state 1]
1063
1064        if {[catch {set info $file_states($path)}]} {
1065                set state __
1066                set icon n[incr next_icon_id]
1067        } else {
1068                set state [lindex $info 0]
1069                set icon [lindex $info 1]
1070        }
1071
1072        if {$s0 eq {_}} {
1073                set s0 [string index $state 0]
1074        } elseif {$s0 eq {*}} {
1075                set s0 _
1076        }
1077
1078        if {$s1 eq {_}} {
1079                set s1 [string index $state 1]
1080        } elseif {$s1 eq {*}} {
1081                set s1 _
1082        }
1083
1084        set file_states($path) [list $s0$s1 $icon]
1085        return $state
1086}
1087
1088proc display_file {path state} {
1089        global file_states file_lists status_active
1090
1091        set old_m [merge_state $path $state]
1092        if {$status_active} return
1093
1094        set s $file_states($path)
1095        set new_m [lindex $s 0]
1096        set new_w [mapcol $new_m $path] 
1097        set old_w [mapcol $old_m $path]
1098        set new_icon [mapicon $new_m $path]
1099
1100        if {$new_w ne $old_w} {
1101                set lno [lsearch -sorted $file_lists($old_w) $path]
1102                if {$lno >= 0} {
1103                        incr lno
1104                        $old_w conf -state normal
1105                        $old_w delete $lno.0 [expr $lno + 1].0
1106                        $old_w conf -state disabled
1107                }
1108
1109                lappend file_lists($new_w) $path
1110                set file_lists($new_w) [lsort $file_lists($new_w)]
1111                set lno [lsearch -sorted $file_lists($new_w) $path]
1112                incr lno
1113                $new_w conf -state normal
1114                $new_w image create $lno.0 \
1115                        -align center -padx 5 -pady 1 \
1116                        -name [lindex $s 1] \
1117                        -image $new_icon
1118                $new_w insert $lno.1 "[escape_path $path]\n"
1119                $new_w conf -state disabled
1120        } elseif {$new_icon ne [mapicon $old_m $path]} {
1121                $new_w conf -state normal
1122                $new_w image conf [lindex $s 1] -image $new_icon
1123                $new_w conf -state disabled
1124        }
1125}
1126
1127proc display_all_files {} {
1128        global ui_index ui_other file_states file_lists
1129
1130        $ui_index conf -state normal
1131        $ui_other conf -state normal
1132
1133        $ui_index delete 0.0 end
1134        $ui_other delete 0.0 end
1135
1136        set file_lists($ui_index) [list]
1137        set file_lists($ui_other) [list]
1138
1139        foreach path [lsort [array names file_states]] {
1140                set s $file_states($path)
1141                set m [lindex $s 0]
1142                set w [mapcol $m $path]
1143                lappend file_lists($w) $path
1144                $w image create end \
1145                        -align center -padx 5 -pady 1 \
1146                        -name [lindex $s 1] \
1147                        -image [mapicon $m $path]
1148                $w insert end "[escape_path $path]\n"
1149        }
1150
1151        $ui_index conf -state disabled
1152        $ui_other conf -state disabled
1153}
1154
1155proc update_index {pathList} {
1156        global update_index_cp update_index_rsd ui_status_value
1157
1158        if {![lock_index update]} return
1159
1160        set update_index_cp 0
1161        set update_index_rsd 0
1162        set totalCnt [llength $pathList]
1163        set batch [expr {int($totalCnt * .01) + 1}]
1164        if {$batch > 25} {set batch 25}
1165
1166        set ui_status_value "Including files ... 0/$totalCnt 0%"
1167        set ui_status_value [format \
1168                "Including files ... %i/%i files (%.2f%%)" \
1169                $update_index_cp \
1170                $totalCnt \
1171                0.0]
1172        set fd [open "| git update-index --add --remove -z --stdin" w]
1173        fconfigure $fd -blocking 0 -translation binary
1174        fileevent $fd writable [list \
1175                write_update_index \
1176                $fd \
1177                $pathList \
1178                $totalCnt \
1179                $batch \
1180                ]
1181}
1182
1183proc write_update_index {fd pathList totalCnt batch} {
1184        global update_index_cp update_index_rsd ui_status_value
1185        global file_states ui_fname_value
1186
1187        if {$update_index_cp >= $totalCnt} {
1188                close $fd
1189                unlock_index
1190                if {$update_index_rsd} {
1191                        reshow_diff
1192                } else {
1193                        set ui_status_value {Ready.}
1194                }
1195                return
1196        }
1197
1198        for {set i $batch} \
1199                {$update_index_cp < $totalCnt && $i > 0} \
1200                {incr i -1} {
1201                set path [lindex $pathList $update_index_cp]
1202                incr update_index_cp
1203
1204                switch -- [lindex $file_states($path) 0] {
1205                AM -
1206                _O {set new A*}
1207                _M -
1208                MM {set new M*}
1209                AD -
1210                _D {set new D*}
1211                default {continue}
1212                }
1213
1214                puts -nonewline $fd $path
1215                puts -nonewline $fd "\0"
1216                display_file $path $new
1217                if {$ui_fname_value eq $path} {
1218                        set update_index_rsd 1
1219                }
1220        }
1221
1222        set ui_status_value [format \
1223                "Including files ... %i/%i files (%.2f%%)" \
1224                $update_index_cp \
1225                $totalCnt \
1226                [expr {100.0 * $update_index_cp / $totalCnt}]]
1227}
1228
1229######################################################################
1230##
1231## remote management
1232
1233proc load_all_remotes {} {
1234        global gitdir all_remotes repo_config
1235
1236        set all_remotes [list]
1237        set rm_dir [file join $gitdir remotes]
1238        if {[file isdirectory $rm_dir]} {
1239                set all_remotes [concat $all_remotes [glob \
1240                        -types f \
1241                        -tails \
1242                        -nocomplain \
1243                        -directory $rm_dir *]]
1244        }
1245
1246        foreach line [array names repo_config remote.*.url] {
1247                if {[regexp ^remote\.(.*)\.url\$ $line line name]} {
1248                        lappend all_remotes $name
1249                }
1250        }
1251
1252        set all_remotes [lsort -unique $all_remotes]
1253}
1254
1255proc populate_remote_menu {m pfx op} {
1256        global all_remotes
1257
1258        foreach remote $all_remotes {
1259                $m add command -label "$pfx $remote..." \
1260                        -command [list $op $remote] \
1261                        -font font_ui
1262        }
1263}
1264
1265proc populate_pull_menu {m} {
1266        global gitdir repo_config all_remotes disable_on_lock
1267
1268        foreach remote $all_remotes {
1269                set rb {}
1270                if {[array get repo_config remote.$remote.url] ne {}} {
1271                        if {[array get repo_config remote.$remote.fetch] ne {}} {
1272                                regexp {^([^:]+):} \
1273                                        [lindex $repo_config(remote.$remote.fetch) 0] \
1274                                        line rb
1275                        }
1276                } else {
1277                        catch {
1278                                set fd [open [file join $gitdir remotes $remote] r]
1279                                while {[gets $fd line] >= 0} {
1280                                        if {[regexp {^Pull:[ \t]*([^:]+):} $line line rb]} {
1281                                                break
1282                                        }
1283                                }
1284                                close $fd
1285                        }
1286                }
1287
1288                set rb_short $rb
1289                regsub ^refs/heads/ $rb {} rb_short
1290                if {$rb_short ne {}} {
1291                        $m add command \
1292                                -label "Branch $rb_short from $remote..." \
1293                                -command [list pull_remote $remote $rb] \
1294                                -font font_ui
1295                        lappend disable_on_lock \
1296                                [list $m entryconf [$m index last] -state]
1297                }
1298        }
1299}
1300
1301######################################################################
1302##
1303## icons
1304
1305set filemask {
1306#define mask_width 14
1307#define mask_height 15
1308static unsigned char mask_bits[] = {
1309   0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1310   0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1311   0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
1312}
1313
1314image create bitmap file_plain -background white -foreground black -data {
1315#define plain_width 14
1316#define plain_height 15
1317static unsigned char plain_bits[] = {
1318   0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1319   0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
1320   0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1321} -maskdata $filemask
1322
1323image create bitmap file_mod -background white -foreground blue -data {
1324#define mod_width 14
1325#define mod_height 15
1326static unsigned char mod_bits[] = {
1327   0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1328   0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1329   0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1330} -maskdata $filemask
1331
1332image create bitmap file_fulltick -background white -foreground "#007000" -data {
1333#define file_fulltick_width 14
1334#define file_fulltick_height 15
1335static unsigned char file_fulltick_bits[] = {
1336   0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
1337   0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
1338   0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1339} -maskdata $filemask
1340
1341image create bitmap file_parttick -background white -foreground "#005050" -data {
1342#define parttick_width 14
1343#define parttick_height 15
1344static unsigned char parttick_bits[] = {
1345   0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1346   0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
1347   0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1348} -maskdata $filemask
1349
1350image create bitmap file_question -background white -foreground black -data {
1351#define file_question_width 14
1352#define file_question_height 15
1353static unsigned char file_question_bits[] = {
1354   0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
1355   0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
1356   0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1357} -maskdata $filemask
1358
1359image create bitmap file_removed -background white -foreground red -data {
1360#define file_removed_width 14
1361#define file_removed_height 15
1362static unsigned char file_removed_bits[] = {
1363   0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1364   0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
1365   0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
1366} -maskdata $filemask
1367
1368image create bitmap file_merge -background white -foreground blue -data {
1369#define file_merge_width 14
1370#define file_merge_height 15
1371static unsigned char file_merge_bits[] = {
1372   0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
1373   0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1374   0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1375} -maskdata $filemask
1376
1377set ui_index .vpane.files.index.list
1378set ui_other .vpane.files.other.list
1379set max_status_desc 0
1380foreach i {
1381                {__ i plain    "Unmodified"}
1382                {_M i mod      "Modified"}
1383                {M_ i fulltick "Checked in"}
1384                {MM i parttick "Partially included"}
1385
1386                {_O o plain    "Untracked"}
1387                {A_ o fulltick "Added"}
1388                {AM o parttick "Partially added"}
1389                {AD o question "Added (but now gone)"}
1390
1391                {_D i question "Missing"}
1392                {D_ i removed  "Removed"}
1393                {DD i removed  "Removed"}
1394                {DO i removed  "Removed (still exists)"}
1395
1396                {UM i merge    "Merge conflicts"}
1397                {U_ i merge    "Merge conflicts"}
1398        } {
1399        if {$max_status_desc < [string length [lindex $i 3]]} {
1400                set max_status_desc [string length [lindex $i 3]]
1401        }
1402        if {[lindex $i 1] eq {i}} {
1403                set all_cols([lindex $i 0]) $ui_index
1404        } else {
1405                set all_cols([lindex $i 0]) $ui_other
1406        }
1407        set all_icons([lindex $i 0]) file_[lindex $i 2]
1408        set all_descs([lindex $i 0]) [lindex $i 3]
1409}
1410unset filemask i
1411
1412######################################################################
1413##
1414## util
1415
1416proc is_MacOSX {} {
1417        global tcl_platform tk_library
1418        if {$tcl_platform(platform) eq {unix}
1419                && $tcl_platform(os) eq {Darwin}
1420                && [string match /Library/Frameworks/* $tk_library]} {
1421                return 1
1422        }
1423        return 0
1424}
1425
1426proc bind_button3 {w cmd} {
1427        bind $w <Any-Button-3> $cmd
1428        if {[is_MacOSX]} {
1429                bind $w <Control-Button-1> $cmd
1430        }
1431}
1432
1433proc incr_font_size {font {amt 1}} {
1434        set sz [font configure $font -size]
1435        incr sz $amt
1436        font configure $font -size $sz
1437        font configure ${font}bold -size $sz
1438}
1439
1440proc hook_failed_popup {hook msg} {
1441        global gitdir appname
1442
1443        set w .hookfail
1444        toplevel $w
1445
1446        frame $w.m
1447        label $w.m.l1 -text "$hook hook failed:" \
1448                -anchor w \
1449                -justify left \
1450                -font font_uibold
1451        text $w.m.t \
1452                -background white -borderwidth 1 \
1453                -relief sunken \
1454                -width 80 -height 10 \
1455                -font font_diff \
1456                -yscrollcommand [list $w.m.sby set]
1457        label $w.m.l2 \
1458                -text {You must correct the above errors before committing.} \
1459                -anchor w \
1460                -justify left \
1461                -font font_uibold
1462        scrollbar $w.m.sby -command [list $w.m.t yview]
1463        pack $w.m.l1 -side top -fill x
1464        pack $w.m.l2 -side bottom -fill x
1465        pack $w.m.sby -side right -fill y
1466        pack $w.m.t -side left -fill both -expand 1
1467        pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
1468
1469        $w.m.t insert 1.0 $msg
1470        $w.m.t conf -state disabled
1471
1472        button $w.ok -text OK \
1473                -width 15 \
1474                -font font_ui \
1475                -command "destroy $w"
1476        pack $w.ok -side bottom
1477
1478        bind $w <Visibility> "grab $w; focus $w"
1479        bind $w <Key-Return> "destroy $w"
1480        wm title $w "$appname ([lindex [file split \
1481                [file normalize [file dirname $gitdir]]] \
1482                end]): error"
1483        tkwait window $w
1484}
1485
1486set next_console_id 0
1487
1488proc new_console {short_title long_title} {
1489        global next_console_id console_data
1490        set w .console[incr next_console_id]
1491        set console_data($w) [list $short_title $long_title]
1492        return [console_init $w]
1493}
1494
1495proc console_init {w} {
1496        global console_cr console_data
1497        global gitdir appname M1B
1498
1499        set console_cr($w) 1.0
1500        toplevel $w
1501        frame $w.m
1502        label $w.m.l1 -text "[lindex $console_data($w) 1]:" \
1503                -anchor w \
1504                -justify left \
1505                -font font_uibold
1506        text $w.m.t \
1507                -background white -borderwidth 1 \
1508                -relief sunken \
1509                -width 80 -height 10 \
1510                -font font_diff \
1511                -state disabled \
1512                -yscrollcommand [list $w.m.sby set]
1513        label $w.m.s -anchor w \
1514                -justify left \
1515                -font font_uibold
1516        scrollbar $w.m.sby -command [list $w.m.t yview]
1517        pack $w.m.l1 -side top -fill x
1518        pack $w.m.s -side bottom -fill x
1519        pack $w.m.sby -side right -fill y
1520        pack $w.m.t -side left -fill both -expand 1
1521        pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
1522
1523        menu $w.ctxm -tearoff 0
1524        $w.ctxm add command -label "Copy" \
1525                -font font_ui \
1526                -command "tk_textCopy $w.m.t"
1527        $w.ctxm add command -label "Select All" \
1528                -font font_ui \
1529                -command "$w.m.t tag add sel 0.0 end"
1530        $w.ctxm add command -label "Copy All" \
1531                -font font_ui \
1532                -command "
1533                        $w.m.t tag add sel 0.0 end
1534                        tk_textCopy $w.m.t
1535                        $w.m.t tag remove sel 0.0 end
1536                "
1537
1538        button $w.ok -text {Running...} \
1539                -width 15 \
1540                -font font_ui \
1541                -state disabled \
1542                -command "destroy $w"
1543        pack $w.ok -side bottom
1544
1545        bind_button3 $w.m.t "tk_popup $w.ctxm %X %Y"
1546        bind $w.m.t <$M1B-Key-a> "$w.m.t tag add sel 0.0 end;break"
1547        bind $w.m.t <$M1B-Key-A> "$w.m.t tag add sel 0.0 end;break"
1548        bind $w <Visibility> "focus $w"
1549        wm title $w "$appname ([lindex [file split \
1550                [file normalize [file dirname $gitdir]]] \
1551                end]): [lindex $console_data($w) 0]"
1552        return $w
1553}
1554
1555proc console_exec {w cmd {after {}}} {
1556        global tcl_platform
1557
1558        # -- Windows tosses the enviroment when we exec our child.
1559        #    But most users need that so we have to relogin. :-(
1560        #
1561        if {$tcl_platform(platform) eq {windows}} {
1562                set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
1563        }
1564
1565        # -- Tcl won't let us redirect both stdout and stderr to
1566        #    the same pipe.  So pass it through cat...
1567        #
1568        set cmd [concat | $cmd |& cat]
1569
1570        set fd_f [open $cmd r]
1571        fconfigure $fd_f -blocking 0 -translation binary
1572        fileevent $fd_f readable [list console_read $w $fd_f $after]
1573}
1574
1575proc console_read {w fd after} {
1576        global console_cr console_data
1577
1578        set buf [read $fd]
1579        if {$buf ne {}} {
1580                if {![winfo exists $w]} {console_init $w}
1581                $w.m.t conf -state normal
1582                set c 0
1583                set n [string length $buf]
1584                while {$c < $n} {
1585                        set cr [string first "\r" $buf $c]
1586                        set lf [string first "\n" $buf $c]
1587                        if {$cr < 0} {set cr [expr $n + 1]}
1588                        if {$lf < 0} {set lf [expr $n + 1]}
1589
1590                        if {$lf < $cr} {
1591                                $w.m.t insert end [string range $buf $c $lf]
1592                                set console_cr($w) [$w.m.t index {end -1c}]
1593                                set c $lf
1594                                incr c
1595                        } else {
1596                                $w.m.t delete $console_cr($w) end
1597                                $w.m.t insert end "\n"
1598                                $w.m.t insert end [string range $buf $c $cr]
1599                                set c $cr
1600                                incr c
1601                        }
1602                }
1603                $w.m.t conf -state disabled
1604                $w.m.t see end
1605        }
1606
1607        fconfigure $fd -blocking 1
1608        if {[eof $fd]} {
1609                if {[catch {close $fd}]} {
1610                        if {![winfo exists $w]} {console_init $w}
1611                        $w.m.s conf -background red -text {Error: Command Failed}
1612                        $w.ok conf -text Close
1613                        $w.ok conf -state normal
1614                        set ok 0
1615                } elseif {[winfo exists $w]} {
1616                        $w.m.s conf -background green -text {Success}
1617                        $w.ok conf -text Close
1618                        $w.ok conf -state normal
1619                        set ok 1
1620                }
1621                array unset console_cr $w
1622                array unset console_data $w
1623                if {$after ne {}} {
1624                        uplevel #0 $after $ok
1625                }
1626                return
1627        }
1628        fconfigure $fd -blocking 0
1629}
1630
1631######################################################################
1632##
1633## ui commands
1634
1635set starting_gitk_msg {Please wait... Starting gitk...}
1636
1637proc do_gitk {} {
1638        global tcl_platform ui_status_value starting_gitk_msg
1639
1640        set ui_status_value $starting_gitk_msg
1641        after 10000 {
1642                if {$ui_status_value eq $starting_gitk_msg} {
1643                        set ui_status_value {Ready.}
1644                }
1645        }
1646
1647        if {$tcl_platform(platform) eq {windows}} {
1648                exec sh -c gitk &
1649        } else {
1650                exec gitk &
1651        }
1652}
1653
1654proc do_repack {} {
1655        set w [new_console "repack" "Repacking the object database"]
1656        set cmd [list git repack]
1657        lappend cmd -a
1658        lappend cmd -d
1659        console_exec $w $cmd
1660}
1661
1662set is_quitting 0
1663
1664proc do_quit {} {
1665        global gitdir ui_comm is_quitting repo_config
1666
1667        if {$is_quitting} return
1668        set is_quitting 1
1669
1670        # -- Stash our current commit buffer.
1671        #
1672        set save [file join $gitdir GITGUI_MSG]
1673        set msg [string trim [$ui_comm get 0.0 end]]
1674        if {[$ui_comm edit modified] && $msg ne {}} {
1675                catch {
1676                        set fd [open $save w]
1677                        puts $fd [string trim [$ui_comm get 0.0 end]]
1678                        close $fd
1679                }
1680        } elseif {$msg eq {} && [file exists $save]} {
1681                file delete $save
1682        }
1683
1684        # -- Stash our current window geometry into this repository.
1685        #
1686        set cfg_geometry [list]
1687        lappend cfg_geometry [wm geometry .]
1688        lappend cfg_geometry [lindex [.vpane sash coord 0] 1]
1689        lappend cfg_geometry [lindex [.vpane.files sash coord 0] 0]
1690        if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
1691                set rc_geometry {}
1692        }
1693        if {$cfg_geometry ne $rc_geometry} {
1694                catch {exec git repo-config gui.geometry $cfg_geometry}
1695        }
1696
1697        destroy .
1698}
1699
1700proc do_rescan {} {
1701        update_status
1702}
1703
1704proc do_include_all {} {
1705        global file_states
1706
1707        if {![lock_index begin-update]} return
1708
1709        set pathList [list]
1710        foreach path [array names file_states] {
1711                set s $file_states($path)
1712                set m [lindex $s 0]
1713                switch -- $m {
1714                AM -
1715                MM -
1716                _M -
1717                _D {lappend pathList $path}
1718                }
1719        }
1720        if {$pathList eq {}} {
1721                unlock_index
1722        } else {
1723                update_index $pathList
1724        }
1725}
1726
1727set GIT_COMMITTER_IDENT {}
1728
1729proc do_signoff {} {
1730        global ui_comm GIT_COMMITTER_IDENT
1731
1732        if {$GIT_COMMITTER_IDENT eq {}} {
1733                if {[catch {set me [exec git var GIT_COMMITTER_IDENT]} err]} {
1734                        error_popup "Unable to obtain your identity:\n\n$err"
1735                        return
1736                }
1737                if {![regexp {^(.*) [0-9]+ [-+0-9]+$} \
1738                        $me me GIT_COMMITTER_IDENT]} {
1739                        error_popup "Invalid GIT_COMMITTER_IDENT:\n\n$me"
1740                        return
1741                }
1742        }
1743
1744        set sob "Signed-off-by: $GIT_COMMITTER_IDENT"
1745        set last [$ui_comm get {end -1c linestart} {end -1c}]
1746        if {$last ne $sob} {
1747                $ui_comm edit separator
1748                if {$last ne {}
1749                        && ![regexp {^[A-Z][A-Za-z]*-[A-Za-z-]+: *} $last]} {
1750                        $ui_comm insert end "\n"
1751                }
1752                $ui_comm insert end "\n$sob"
1753                $ui_comm edit separator
1754                $ui_comm see end
1755        }
1756}
1757
1758proc do_amend_last {} {
1759        load_last_commit
1760}
1761
1762proc do_commit {} {
1763        commit_tree
1764}
1765
1766proc do_options {} {
1767        global appname gitdir font_descs
1768        global repo_config global_config
1769        global repo_config_new global_config_new
1770
1771        array unset repo_config_new
1772        array unset global_config_new
1773        foreach name [array names repo_config] {
1774                set repo_config_new($name) $repo_config($name)
1775        }
1776        load_config 1
1777        foreach name [array names repo_config] {
1778                switch -- $name {
1779                gui.diffcontext {continue}
1780                }
1781                set repo_config_new($name) $repo_config($name)
1782        }
1783        foreach name [array names global_config] {
1784                set global_config_new($name) $global_config($name)
1785        }
1786        set reponame [lindex [file split \
1787                [file normalize [file dirname $gitdir]]] \
1788                end]
1789
1790        set w .options_editor
1791        toplevel $w
1792        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
1793
1794        label $w.header -text "$appname Options" \
1795                -font font_uibold
1796        pack $w.header -side top -fill x
1797
1798        frame $w.buttons
1799        button $w.buttons.restore -text {Restore Defaults} \
1800                -font font_ui \
1801                -command do_restore_defaults
1802        pack $w.buttons.restore -side left
1803        button $w.buttons.save -text Save \
1804                -font font_ui \
1805                -command [list do_save_config $w]
1806        pack $w.buttons.save -side right
1807        button $w.buttons.cancel -text {Cancel} \
1808                -font font_ui \
1809                -command [list destroy $w]
1810        pack $w.buttons.cancel -side right
1811        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
1812
1813        labelframe $w.repo -text "$reponame Repository" \
1814                -font font_ui \
1815                -relief raised -borderwidth 2
1816        labelframe $w.global -text {Global (All Repositories)} \
1817                -font font_ui \
1818                -relief raised -borderwidth 2
1819        pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
1820        pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
1821
1822        foreach option {
1823                {b pullsummary {Show Pull Summary}}
1824                {b trustmtime  {Trust File Modification Timestamps}}
1825                {i diffcontext {Number of Diff Context Lines}}
1826                } {
1827                set type [lindex $option 0]
1828                set name [lindex $option 1]
1829                set text [lindex $option 2]
1830                foreach f {repo global} {
1831                        switch $type {
1832                        b {
1833                                checkbutton $w.$f.$name -text $text \
1834                                        -variable ${f}_config_new(gui.$name) \
1835                                        -onvalue true \
1836                                        -offvalue false \
1837                                        -font font_ui
1838                                pack $w.$f.$name -side top -anchor w
1839                        }
1840                        i {
1841                                frame $w.$f.$name
1842                                label $w.$f.$name.l -text "$text:" -font font_ui
1843                                pack $w.$f.$name.l -side left -anchor w -fill x
1844                                spinbox $w.$f.$name.v \
1845                                        -textvariable ${f}_config_new(gui.$name) \
1846                                        -from 1 -to 99 -increment 1 \
1847                                        -width 3 \
1848                                        -font font_ui
1849                                pack $w.$f.$name.v -side right -anchor e
1850                                pack $w.$f.$name -side top -anchor w -fill x
1851                        }
1852                        }
1853                }
1854        }
1855
1856        set all_fonts [lsort [font families]]
1857        foreach option $font_descs {
1858                set name [lindex $option 0]
1859                set font [lindex $option 1]
1860                set text [lindex $option 2]
1861
1862                set global_config_new(gui.$font^^family) \
1863                        [font configure $font -family]
1864                set global_config_new(gui.$font^^size) \
1865                        [font configure $font -size]
1866
1867                frame $w.global.$name
1868                label $w.global.$name.l -text "$text:" -font font_ui
1869                pack $w.global.$name.l -side left -anchor w -fill x
1870                eval tk_optionMenu $w.global.$name.family \
1871                        global_config_new(gui.$font^^family) \
1872                        $all_fonts
1873                spinbox $w.global.$name.size \
1874                        -textvariable global_config_new(gui.$font^^size) \
1875                        -from 2 -to 80 -increment 1 \
1876                        -width 3 \
1877                        -font font_ui
1878                pack $w.global.$name.size -side right -anchor e
1879                pack $w.global.$name.family -side right -anchor e
1880                pack $w.global.$name -side top -anchor w -fill x
1881        }
1882
1883        bind $w <Visibility> "grab $w; focus $w"
1884        bind $w <Key-Escape> "destroy $w"
1885        wm title $w "$appname ($reponame): Options"
1886        tkwait window $w
1887}
1888
1889proc do_restore_defaults {} {
1890        global font_descs default_config repo_config
1891        global repo_config_new global_config_new
1892
1893        foreach name [array names default_config] {
1894                set repo_config_new($name) $default_config($name)
1895                set global_config_new($name) $default_config($name)
1896        }
1897
1898        foreach option $font_descs {
1899                set name [lindex $option 0]
1900                set repo_config(gui.$name) $default_config(gui.$name)
1901        }
1902        apply_config
1903
1904        foreach option $font_descs {
1905                set name [lindex $option 0]
1906                set font [lindex $option 1]
1907                set global_config_new(gui.$font^^family) \
1908                        [font configure $font -family]
1909                set global_config_new(gui.$font^^size) \
1910                        [font configure $font -size]
1911        }
1912}
1913
1914proc do_save_config {w} {
1915        if {[catch {save_config} err]} {
1916                error_popup "Failed to completely save options:\n\n$err"
1917        }
1918        reshow_diff
1919        destroy $w
1920}
1921
1922# shift == 1: left click
1923#          3: right click  
1924proc click {w x y shift wx wy} {
1925        global ui_index ui_other file_lists
1926
1927        set pos [split [$w index @$x,$y] .]
1928        set lno [lindex $pos 0]
1929        set col [lindex $pos 1]
1930        set path [lindex $file_lists($w) [expr $lno - 1]]
1931        if {$path eq {}} return
1932
1933        if {$col > 0 && $shift == 1} {
1934                show_diff $path $w $lno
1935        }
1936}
1937
1938proc unclick {w x y} {
1939        global file_lists
1940
1941        set pos [split [$w index @$x,$y] .]
1942        set lno [lindex $pos 0]
1943        set col [lindex $pos 1]
1944        set path [lindex $file_lists($w) [expr $lno - 1]]
1945        if {$path eq {}} return
1946
1947        if {$col == 0} {
1948                update_index [list $path]
1949        }
1950}
1951
1952######################################################################
1953##
1954## config defaults
1955
1956set cursor_ptr arrow
1957font create font_diff -family Courier -size 10
1958font create font_ui
1959catch {
1960        label .dummy
1961        eval font configure font_ui [font actual [.dummy cget -font]]
1962        destroy .dummy
1963}
1964
1965font create font_uibold
1966font create font_diffbold
1967
1968set M1B M1
1969set M1T M1
1970if {$tcl_platform(platform) eq {windows}} {
1971        set M1B Control
1972        set M1T Ctrl
1973} elseif {[is_MacOSX]} {
1974        set M1B M1
1975        set M1T Cmd
1976}
1977
1978proc apply_config {} {
1979        global repo_config font_descs
1980
1981        foreach option $font_descs {
1982                set name [lindex $option 0]
1983                set font [lindex $option 1]
1984                if {[catch {
1985                        foreach {cn cv} $repo_config(gui.$name) {
1986                                font configure $font $cn $cv
1987                        }
1988                        } err]} {
1989                        error_popup "Invalid font specified in gui.$name:\n\n$err"
1990                }
1991                foreach {cn cv} [font configure $font] {
1992                        font configure ${font}bold $cn $cv
1993                }
1994                font configure ${font}bold -weight bold
1995        }
1996}
1997
1998set default_config(gui.trustmtime) false
1999set default_config(gui.pullsummary) true
2000set default_config(gui.diffcontext) 5
2001set default_config(gui.fontui) [font configure font_ui]
2002set default_config(gui.fontdiff) [font configure font_diff]
2003set font_descs {
2004        {fontui   font_ui   {Main Font}}
2005        {fontdiff font_diff {Diff/Console Font}}
2006}
2007load_config 0
2008apply_config
2009
2010######################################################################
2011##
2012## ui construction
2013
2014# -- Menu Bar
2015menu .mbar -tearoff 0
2016.mbar add cascade -label Project -menu .mbar.project
2017.mbar add cascade -label Edit -menu .mbar.edit
2018.mbar add cascade -label Commit -menu .mbar.commit
2019if {!$single_commit} {
2020        .mbar add cascade -label Fetch -menu .mbar.fetch
2021        .mbar add cascade -label Pull -menu .mbar.pull
2022        .mbar add cascade -label Push -menu .mbar.push
2023}
2024. configure -menu .mbar
2025
2026# -- Project Menu
2027menu .mbar.project
2028.mbar.project add command -label Visualize \
2029        -command do_gitk \
2030        -font font_ui
2031if {!$single_commit} {
2032        .mbar.project add command -label {Repack Database} \
2033                -command do_repack \
2034                -font font_ui
2035}
2036.mbar.project add command -label Quit \
2037        -command do_quit \
2038        -accelerator $M1T-Q \
2039        -font font_ui
2040
2041# -- Edit Menu
2042#
2043menu .mbar.edit
2044.mbar.edit add command -label Undo \
2045        -command {catch {[focus] edit undo}} \
2046        -accelerator $M1T-Z \
2047        -font font_ui
2048.mbar.edit add command -label Redo \
2049        -command {catch {[focus] edit redo}} \
2050        -accelerator $M1T-Y \
2051        -font font_ui
2052.mbar.edit add separator
2053.mbar.edit add command -label Cut \
2054        -command {catch {tk_textCut [focus]}} \
2055        -accelerator $M1T-X \
2056        -font font_ui
2057.mbar.edit add command -label Copy \
2058        -command {catch {tk_textCopy [focus]}} \
2059        -accelerator $M1T-C \
2060        -font font_ui
2061.mbar.edit add command -label Paste \
2062        -command {catch {tk_textPaste [focus]; [focus] see insert}} \
2063        -accelerator $M1T-V \
2064        -font font_ui
2065.mbar.edit add command -label Delete \
2066        -command {catch {[focus] delete sel.first sel.last}} \
2067        -accelerator Del \
2068        -font font_ui
2069.mbar.edit add separator
2070.mbar.edit add command -label {Select All} \
2071        -command {catch {[focus] tag add sel 0.0 end}} \
2072        -accelerator $M1T-A \
2073        -font font_ui
2074.mbar.edit add separator
2075.mbar.edit add command -label {Options...} \
2076        -command do_options \
2077        -font font_ui
2078
2079# -- Commit Menu
2080menu .mbar.commit
2081.mbar.commit add command -label Rescan \
2082        -command do_rescan \
2083        -accelerator F5 \
2084        -font font_ui
2085lappend disable_on_lock \
2086        [list .mbar.commit entryconf [.mbar.commit index last] -state]
2087.mbar.commit add command -label {Amend Last Commit} \
2088        -command do_amend_last \
2089        -font font_ui
2090lappend disable_on_lock \
2091        [list .mbar.commit entryconf [.mbar.commit index last] -state]
2092.mbar.commit add command -label {Include All Files} \
2093        -command do_include_all \
2094        -accelerator $M1T-I \
2095        -font font_ui
2096lappend disable_on_lock \
2097        [list .mbar.commit entryconf [.mbar.commit index last] -state]
2098.mbar.commit add command -label {Sign Off} \
2099        -command do_signoff \
2100        -accelerator $M1T-S \
2101        -font font_ui
2102.mbar.commit add command -label Commit \
2103        -command do_commit \
2104        -accelerator $M1T-Return \
2105        -font font_ui
2106lappend disable_on_lock \
2107        [list .mbar.commit entryconf [.mbar.commit index last] -state]
2108
2109if {!$single_commit} {
2110        # -- Fetch Menu
2111        menu .mbar.fetch
2112
2113        # -- Pull Menu
2114        menu .mbar.pull
2115
2116        # -- Push Menu
2117        menu .mbar.push
2118}
2119
2120# -- Main Window Layout
2121panedwindow .vpane -orient vertical
2122panedwindow .vpane.files -orient horizontal
2123.vpane add .vpane.files -sticky nsew -height 100 -width 400
2124pack .vpane -anchor n -side top -fill both -expand 1
2125
2126# -- Index File List
2127frame .vpane.files.index -height 100 -width 400
2128label .vpane.files.index.title -text {Modified Files} \
2129        -background green \
2130        -font font_ui
2131text $ui_index -background white -borderwidth 0 \
2132        -width 40 -height 10 \
2133        -font font_ui \
2134        -cursor $cursor_ptr \
2135        -yscrollcommand {.vpane.files.index.sb set} \
2136        -state disabled
2137scrollbar .vpane.files.index.sb -command [list $ui_index yview]
2138pack .vpane.files.index.title -side top -fill x
2139pack .vpane.files.index.sb -side right -fill y
2140pack $ui_index -side left -fill both -expand 1
2141.vpane.files add .vpane.files.index -sticky nsew
2142
2143# -- Other (Add) File List
2144frame .vpane.files.other -height 100 -width 100
2145label .vpane.files.other.title -text {Untracked Files} \
2146        -background red \
2147        -font font_ui
2148text $ui_other -background white -borderwidth 0 \
2149        -width 40 -height 10 \
2150        -font font_ui \
2151        -cursor $cursor_ptr \
2152        -yscrollcommand {.vpane.files.other.sb set} \
2153        -state disabled
2154scrollbar .vpane.files.other.sb -command [list $ui_other yview]
2155pack .vpane.files.other.title -side top -fill x
2156pack .vpane.files.other.sb -side right -fill y
2157pack $ui_other -side left -fill both -expand 1
2158.vpane.files add .vpane.files.other -sticky nsew
2159
2160$ui_index tag conf in_diff -font font_uibold
2161$ui_other tag conf in_diff -font font_uibold
2162
2163# -- Diff and Commit Area
2164frame .vpane.lower -height 300 -width 400
2165frame .vpane.lower.commarea
2166frame .vpane.lower.diff -relief sunken -borderwidth 1
2167pack .vpane.lower.commarea -side top -fill x
2168pack .vpane.lower.diff -side bottom -fill both -expand 1
2169.vpane add .vpane.lower -stick nsew
2170
2171# -- Commit Area Buttons
2172frame .vpane.lower.commarea.buttons
2173label .vpane.lower.commarea.buttons.l -text {} \
2174        -anchor w \
2175        -justify left \
2176        -font font_ui
2177pack .vpane.lower.commarea.buttons.l -side top -fill x
2178pack .vpane.lower.commarea.buttons -side left -fill y
2179
2180button .vpane.lower.commarea.buttons.rescan -text {Rescan} \
2181        -command do_rescan \
2182        -font font_ui
2183pack .vpane.lower.commarea.buttons.rescan -side top -fill x
2184lappend disable_on_lock \
2185        {.vpane.lower.commarea.buttons.rescan conf -state}
2186
2187button .vpane.lower.commarea.buttons.amend -text {Amend Last} \
2188        -command do_amend_last \
2189        -font font_ui
2190pack .vpane.lower.commarea.buttons.amend -side top -fill x
2191lappend disable_on_lock \
2192        {.vpane.lower.commarea.buttons.amend conf -state}
2193
2194button .vpane.lower.commarea.buttons.incall -text {Include All} \
2195        -command do_include_all \
2196        -font font_ui
2197pack .vpane.lower.commarea.buttons.incall -side top -fill x
2198lappend disable_on_lock \
2199        {.vpane.lower.commarea.buttons.incall conf -state}
2200
2201button .vpane.lower.commarea.buttons.signoff -text {Sign Off} \
2202        -command do_signoff \
2203        -font font_ui
2204pack .vpane.lower.commarea.buttons.signoff -side top -fill x
2205
2206button .vpane.lower.commarea.buttons.commit -text {Commit} \
2207        -command do_commit \
2208        -font font_ui
2209pack .vpane.lower.commarea.buttons.commit -side top -fill x
2210lappend disable_on_lock \
2211        {.vpane.lower.commarea.buttons.commit conf -state}
2212
2213# -- Commit Message Buffer
2214frame .vpane.lower.commarea.buffer
2215set ui_comm .vpane.lower.commarea.buffer.t
2216set ui_coml .vpane.lower.commarea.buffer.l
2217label $ui_coml -text {Commit Message:} \
2218        -anchor w \
2219        -justify left \
2220        -font font_ui
2221trace add variable commit_type write {uplevel #0 {
2222        switch -glob $commit_type \
2223        initial {$ui_coml conf -text {Initial Commit Message:}} \
2224        amend   {$ui_coml conf -text {Amended Commit Message:}} \
2225        merge   {$ui_coml conf -text {Merge Commit Message:}} \
2226        *       {$ui_coml conf -text {Commit Message:}}
2227}}
2228text $ui_comm -background white -borderwidth 1 \
2229        -undo true \
2230        -maxundo 20 \
2231        -autoseparators true \
2232        -relief sunken \
2233        -width 75 -height 9 -wrap none \
2234        -font font_diff \
2235        -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
2236scrollbar .vpane.lower.commarea.buffer.sby \
2237        -command [list $ui_comm yview]
2238pack $ui_coml -side top -fill x
2239pack .vpane.lower.commarea.buffer.sby -side right -fill y
2240pack $ui_comm -side left -fill y
2241pack .vpane.lower.commarea.buffer -side left -fill y
2242
2243# -- Commit Message Buffer Context Menu
2244#
2245menu $ui_comm.ctxm -tearoff 0
2246$ui_comm.ctxm add command -label "Cut" \
2247        -font font_ui \
2248        -command "tk_textCut $ui_comm"
2249$ui_comm.ctxm add command -label "Copy" \
2250        -font font_ui \
2251        -command "tk_textCopy $ui_comm"
2252$ui_comm.ctxm add command -label "Paste" \
2253        -font font_ui \
2254        -command "tk_textPaste $ui_comm"
2255$ui_comm.ctxm add command -label "Delete" \
2256        -font font_ui \
2257        -command "$ui_comm delete sel.first sel.last"
2258$ui_comm.ctxm add separator
2259$ui_comm.ctxm add command -label "Select All" \
2260        -font font_ui \
2261        -command "$ui_comm tag add sel 0.0 end"
2262$ui_comm.ctxm add command -label "Copy All" \
2263        -font font_ui \
2264        -command "
2265                $ui_comm tag add sel 0.0 end
2266                tk_textCopy $ui_comm
2267                $ui_comm tag remove sel 0.0 end
2268        "
2269$ui_comm.ctxm add separator
2270$ui_comm.ctxm add command -label "Sign Off" \
2271        -font font_ui \
2272        -command do_signoff
2273bind_button3 $ui_comm "tk_popup $ui_comm.ctxm %X %Y"
2274
2275# -- Diff Header
2276set ui_fname_value {}
2277set ui_fstatus_value {}
2278frame .vpane.lower.diff.header -background orange
2279label .vpane.lower.diff.header.l1 -text {File:} \
2280        -background orange \
2281        -font font_ui
2282label .vpane.lower.diff.header.l2 -textvariable ui_fname_value \
2283        -background orange \
2284        -anchor w \
2285        -justify left \
2286        -font font_ui
2287label .vpane.lower.diff.header.l3 -text {Status:} \
2288        -background orange \
2289        -font font_ui
2290label .vpane.lower.diff.header.l4 -textvariable ui_fstatus_value \
2291        -background orange \
2292        -width $max_status_desc \
2293        -anchor w \
2294        -justify left \
2295        -font font_ui
2296pack .vpane.lower.diff.header.l1 -side left
2297pack .vpane.lower.diff.header.l2 -side left -fill x
2298pack .vpane.lower.diff.header.l4 -side right
2299pack .vpane.lower.diff.header.l3 -side right
2300
2301# -- Diff Body
2302frame .vpane.lower.diff.body
2303set ui_diff .vpane.lower.diff.body.t
2304text $ui_diff -background white -borderwidth 0 \
2305        -width 80 -height 15 -wrap none \
2306        -font font_diff \
2307        -xscrollcommand {.vpane.lower.diff.body.sbx set} \
2308        -yscrollcommand {.vpane.lower.diff.body.sby set} \
2309        -state disabled
2310scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
2311        -command [list $ui_diff xview]
2312scrollbar .vpane.lower.diff.body.sby -orient vertical \
2313        -command [list $ui_diff yview]
2314pack .vpane.lower.diff.body.sbx -side bottom -fill x
2315pack .vpane.lower.diff.body.sby -side right -fill y
2316pack $ui_diff -side left -fill both -expand 1
2317pack .vpane.lower.diff.header -side top -fill x
2318pack .vpane.lower.diff.body -side bottom -fill both -expand 1
2319
2320$ui_diff tag conf dm -foreground red
2321$ui_diff tag conf dp -foreground blue
2322$ui_diff tag conf di -foreground {#00a000}
2323$ui_diff tag conf dni -foreground {#a000a0}
2324$ui_diff tag conf da -font font_diffbold
2325$ui_diff tag conf bold -font font_diffbold
2326
2327# -- Diff Body Context Menu
2328#
2329menu $ui_diff.ctxm -tearoff 0
2330$ui_diff.ctxm add command -label "Copy" \
2331        -font font_ui \
2332        -command "tk_textCopy $ui_diff"
2333$ui_diff.ctxm add command -label "Select All" \
2334        -font font_ui \
2335        -command "$ui_diff tag add sel 0.0 end"
2336$ui_diff.ctxm add command -label "Copy All" \
2337        -font font_ui \
2338        -command "
2339                $ui_diff tag add sel 0.0 end
2340                tk_textCopy $ui_diff
2341                $ui_diff tag remove sel 0.0 end
2342        "
2343$ui_diff.ctxm add separator
2344$ui_diff.ctxm add command -label "Decrease Font Size" \
2345        -font font_ui \
2346        -command {incr_font_size font_diff -1}
2347$ui_diff.ctxm add command -label "Increase Font Size" \
2348        -font font_ui \
2349        -command {incr_font_size font_diff 1}
2350$ui_diff.ctxm add separator
2351$ui_diff.ctxm add command -label "Show Less Context" \
2352        -font font_ui \
2353        -command {if {$ui_fname_value ne {}
2354                && $repo_config(gui.diffcontext) >= 2} {
2355                incr repo_config(gui.diffcontext) -1
2356                reshow_diff
2357        }}
2358$ui_diff.ctxm add command -label "Show More Context" \
2359        -font font_ui \
2360        -command {if {$ui_fname_value ne {}} {
2361                incr repo_config(gui.diffcontext)
2362                reshow_diff
2363        }}
2364$ui_diff.ctxm add separator
2365$ui_diff.ctxm add command -label {Options...} \
2366        -font font_ui \
2367        -command do_options
2368bind_button3 $ui_diff "tk_popup $ui_diff.ctxm %X %Y"
2369
2370# -- Status Bar
2371set ui_status_value {Initializing...}
2372label .status -textvariable ui_status_value \
2373        -anchor w \
2374        -justify left \
2375        -borderwidth 1 \
2376        -relief sunken \
2377        -font font_ui
2378pack .status -anchor w -side bottom -fill x
2379
2380# -- Load geometry
2381catch {
2382set gm $repo_config(gui.geometry)
2383wm geometry . [lindex $gm 0]
2384.vpane sash place 0 \
2385        [lindex [.vpane sash coord 0] 0] \
2386        [lindex $gm 1]
2387.vpane.files sash place 0 \
2388        [lindex $gm 2] \
2389        [lindex [.vpane.files sash coord 0] 1]
2390unset gm
2391}
2392
2393# -- Key Bindings
2394bind $ui_comm <$M1B-Key-Return> {do_commit;break}
2395bind $ui_comm <$M1B-Key-i> {do_include_all;break}
2396bind $ui_comm <$M1B-Key-I> {do_include_all;break}
2397bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
2398bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
2399bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
2400bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
2401bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
2402bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
2403bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2404bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2405
2406bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
2407bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
2408bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
2409bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
2410bind $ui_diff <$M1B-Key-v> {break}
2411bind $ui_diff <$M1B-Key-V> {break}
2412bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2413bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2414bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
2415bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
2416bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
2417bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
2418
2419bind .   <Destroy> do_quit
2420bind all <Key-F5> do_rescan
2421bind all <$M1B-Key-r> do_rescan
2422bind all <$M1B-Key-R> do_rescan
2423bind .   <$M1B-Key-s> do_signoff
2424bind .   <$M1B-Key-S> do_signoff
2425bind .   <$M1B-Key-i> do_include_all
2426bind .   <$M1B-Key-I> do_include_all
2427bind .   <$M1B-Key-Return> do_commit
2428bind all <$M1B-Key-q> do_quit
2429bind all <$M1B-Key-Q> do_quit
2430bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2431bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
2432foreach i [list $ui_index $ui_other] {
2433        bind $i <Button-1> {click %W %x %y 1 %X %Y; break}
2434        bind $i <ButtonRelease-1> {unclick %W %x %y; break}
2435        bind_button3 $i {click %W %x %y 3 %X %Y; break}
2436}
2437unset i
2438
2439set file_lists($ui_index) [list]
2440set file_lists($ui_other) [list]
2441
2442wm title . "$appname ([file normalize [file dirname $gitdir]])"
2443focus -force $ui_comm
2444if {!$single_commit} {
2445        load_all_remotes
2446        populate_remote_menu .mbar.fetch From fetch_from
2447        populate_remote_menu .mbar.push To push_to
2448        populate_pull_menu .mbar.pull
2449}
2450after 1 update_status