git-gui.shon commit git-gui: Refactor git-config --list parsing (f00d504)
   1#!/bin/sh
   2# Tcl ignores the next line -*- tcl -*- \
   3 if test "z$*" = zversion \
   4 || test "z$*" = z--version; \
   5 then \
   6        echo 'git-gui version @@GITGUI_VERSION@@'; \
   7        exit; \
   8 fi; \
   9 argv0=$0; \
  10 exec wish "$argv0" -- "$@"
  11
  12set appvers {@@GITGUI_VERSION@@}
  13set copyright {
  14Copyright © 2006, 2007 Shawn Pearce, et. al.
  15
  16This program is free software; you can redistribute it and/or modify
  17it under the terms of the GNU General Public License as published by
  18the Free Software Foundation; either version 2 of the License, or
  19(at your option) any later version.
  20
  21This program is distributed in the hope that it will be useful,
  22but WITHOUT ANY WARRANTY; without even the implied warranty of
  23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24GNU General Public License for more details.
  25
  26You should have received a copy of the GNU General Public License
  27along with this program; if not, write to the Free Software
  28Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA}
  29
  30######################################################################
  31##
  32## Tcl/Tk sanity check
  33
  34if {[catch {package require Tcl 8.4} err]
  35 || [catch {package require Tk  8.4} err]
  36} {
  37        catch {wm withdraw .}
  38        tk_messageBox \
  39                -icon error \
  40                -type ok \
  41                -title [mc "git-gui: fatal error"] \
  42                -message $err
  43        exit 1
  44}
  45
  46catch {rename send {}} ; # What an evil concept...
  47
  48######################################################################
  49##
  50## locate our library
  51
  52set oguilib {@@GITGUI_LIBDIR@@}
  53set oguirel {@@GITGUI_RELATIVE@@}
  54if {$oguirel eq {1}} {
  55        set oguilib [file dirname [file dirname [file normalize $argv0]]]
  56        set oguilib [file join $oguilib share git-gui lib]
  57        set oguimsg [file join $oguilib msgs]
  58} elseif {[string match @@* $oguirel]} {
  59        set oguilib [file join [file dirname [file normalize $argv0]] lib]
  60        set oguimsg [file join [file dirname [file normalize $argv0]] po]
  61} else {
  62        set oguimsg [file join $oguilib msgs]
  63}
  64unset oguirel
  65
  66######################################################################
  67##
  68## enable verbose loading?
  69
  70if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
  71        unset _verbose
  72        rename auto_load real__auto_load
  73        proc auto_load {name args} {
  74                puts stderr "auto_load $name"
  75                return [uplevel 1 real__auto_load $name $args]
  76        }
  77        rename source real__source
  78        proc source {name} {
  79                puts stderr "source    $name"
  80                uplevel 1 real__source $name
  81        }
  82}
  83
  84######################################################################
  85##
  86## Internationalization (i18n) through msgcat and gettext. See
  87## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
  88
  89package require msgcat
  90
  91proc mc {fmt args} {
  92        set fmt [::msgcat::mc $fmt]
  93        set cmk [string first @@ $fmt]
  94        if {$cmk > 0} {
  95                set fmt [string range $fmt 0 [expr {$cmk - 1}]]
  96        }
  97        return [eval [list format $fmt] $args]
  98}
  99
 100proc strcat {args} {
 101        return [join $args {}]
 102}
 103
 104::msgcat::mcload $oguimsg
 105unset oguimsg
 106
 107######################################################################
 108##
 109## read only globals
 110
 111set _appname {Git Gui}
 112set _gitdir {}
 113set _gitexec {}
 114set _reponame {}
 115set _iscygwin {}
 116set _search_path {}
 117
 118proc appname {} {
 119        global _appname
 120        return $_appname
 121}
 122
 123proc gitdir {args} {
 124        global _gitdir
 125        if {$args eq {}} {
 126                return $_gitdir
 127        }
 128        return [eval [list file join $_gitdir] $args]
 129}
 130
 131proc gitexec {args} {
 132        global _gitexec
 133        if {$_gitexec eq {}} {
 134                if {[catch {set _gitexec [git --exec-path]} err]} {
 135                        error "Git not installed?\n\n$err"
 136                }
 137                if {[is_Cygwin]} {
 138                        set _gitexec [exec cygpath \
 139                                --windows \
 140                                --absolute \
 141                                $_gitexec]
 142                } else {
 143                        set _gitexec [file normalize $_gitexec]
 144                }
 145        }
 146        if {$args eq {}} {
 147                return $_gitexec
 148        }
 149        return [eval [list file join $_gitexec] $args]
 150}
 151
 152proc reponame {} {
 153        return $::_reponame
 154}
 155
 156proc is_MacOSX {} {
 157        if {[tk windowingsystem] eq {aqua}} {
 158                return 1
 159        }
 160        return 0
 161}
 162
 163proc is_Windows {} {
 164        if {$::tcl_platform(platform) eq {windows}} {
 165                return 1
 166        }
 167        return 0
 168}
 169
 170proc is_Cygwin {} {
 171        global _iscygwin
 172        if {$_iscygwin eq {}} {
 173                if {$::tcl_platform(platform) eq {windows}} {
 174                        if {[catch {set p [exec cygpath --windir]} err]} {
 175                                set _iscygwin 0
 176                        } else {
 177                                set _iscygwin 1
 178                        }
 179                } else {
 180                        set _iscygwin 0
 181                }
 182        }
 183        return $_iscygwin
 184}
 185
 186proc is_enabled {option} {
 187        global enabled_options
 188        if {[catch {set on $enabled_options($option)}]} {return 0}
 189        return $on
 190}
 191
 192proc enable_option {option} {
 193        global enabled_options
 194        set enabled_options($option) 1
 195}
 196
 197proc disable_option {option} {
 198        global enabled_options
 199        set enabled_options($option) 0
 200}
 201
 202######################################################################
 203##
 204## config
 205
 206proc is_many_config {name} {
 207        switch -glob -- $name {
 208        remote.*.fetch -
 209        remote.*.push
 210                {return 1}
 211        *
 212                {return 0}
 213        }
 214}
 215
 216proc is_config_true {name} {
 217        global repo_config
 218        if {[catch {set v $repo_config($name)}]} {
 219                return 0
 220        } elseif {$v eq {true} || $v eq {1} || $v eq {yes}} {
 221                return 1
 222        } else {
 223                return 0
 224        }
 225}
 226
 227proc get_config {name} {
 228        global repo_config
 229        if {[catch {set v $repo_config($name)}]} {
 230                return {}
 231        } else {
 232                return $v
 233        }
 234}
 235
 236######################################################################
 237##
 238## handy utils
 239
 240proc _git_cmd {name} {
 241        global _git_cmd_path
 242
 243        if {[catch {set v $_git_cmd_path($name)}]} {
 244                switch -- $name {
 245                  version   -
 246                --version   -
 247                --exec-path { return [list $::_git $name] }
 248                }
 249
 250                set p [gitexec git-$name$::_search_exe]
 251                if {[file exists $p]} {
 252                        set v [list $p]
 253                } elseif {[is_Windows] && [file exists [gitexec git-$name]]} {
 254                        # Try to determine what sort of magic will make
 255                        # git-$name go and do its thing, because native
 256                        # Tcl on Windows doesn't know it.
 257                        #
 258                        set p [gitexec git-$name]
 259                        set f [open $p r]
 260                        set s [gets $f]
 261                        close $f
 262
 263                        switch -glob -- [lindex $s 0] {
 264                        #!*sh     { set i sh     }
 265                        #!*perl   { set i perl   }
 266                        #!*python { set i python }
 267                        default   { error "git-$name is not supported: $s" }
 268                        }
 269
 270                        upvar #0 _$i interp
 271                        if {![info exists interp]} {
 272                                set interp [_which $i]
 273                        }
 274                        if {$interp eq {}} {
 275                                error "git-$name requires $i (not in PATH)"
 276                        }
 277                        set v [concat [list $interp] [lrange $s 1 end] [list $p]]
 278                } else {
 279                        # Assume it is builtin to git somehow and we
 280                        # aren't actually able to see a file for it.
 281                        #
 282                        set v [list $::_git $name]
 283                }
 284                set _git_cmd_path($name) $v
 285        }
 286        return $v
 287}
 288
 289proc _which {what} {
 290        global env _search_exe _search_path
 291
 292        if {$_search_path eq {}} {
 293                if {[is_Cygwin] && [regexp {^(/|\.:)} $env(PATH)]} {
 294                        set _search_path [split [exec cygpath \
 295                                --windows \
 296                                --path \
 297                                --absolute \
 298                                $env(PATH)] {;}]
 299                        set _search_exe .exe
 300                } elseif {[is_Windows]} {
 301                        set gitguidir [file dirname [info script]]
 302                        regsub -all ";" $gitguidir "\\;" gitguidir
 303                        set env(PATH) "$gitguidir;$env(PATH)"
 304                        set _search_path [split $env(PATH) {;}]
 305                        set _search_exe .exe
 306                } else {
 307                        set _search_path [split $env(PATH) :]
 308                        set _search_exe {}
 309                }
 310        }
 311
 312        foreach p $_search_path {
 313                set p [file join $p $what$_search_exe]
 314                if {[file exists $p]} {
 315                        return [file normalize $p]
 316                }
 317        }
 318        return {}
 319}
 320
 321proc _lappend_nice {cmd_var} {
 322        global _nice
 323        upvar $cmd_var cmd
 324
 325        if {![info exists _nice]} {
 326                set _nice [_which nice]
 327        }
 328        if {$_nice ne {}} {
 329                lappend cmd $_nice
 330        }
 331}
 332
 333proc git {args} {
 334        set opt [list exec]
 335
 336        while {1} {
 337                switch -- [lindex $args 0] {
 338                --nice {
 339                        _lappend_nice opt
 340                }
 341
 342                default {
 343                        break
 344                }
 345
 346                }
 347
 348                set args [lrange $args 1 end]
 349        }
 350
 351        set cmdp [_git_cmd [lindex $args 0]]
 352        set args [lrange $args 1 end]
 353
 354        return [eval $opt $cmdp $args]
 355}
 356
 357proc _open_stdout_stderr {cmd} {
 358        if {[catch {
 359                        set fd [open $cmd r]
 360                } err]} {
 361                if {   [lindex $cmd end] eq {2>@1}
 362                    && $err eq {can not find channel named "1"}
 363                        } {
 364                        # Older versions of Tcl 8.4 don't have this 2>@1 IO
 365                        # redirect operator.  Fallback to |& cat for those.
 366                        # The command was not actually started, so its safe
 367                        # to try to start it a second time.
 368                        #
 369                        set fd [open [concat \
 370                                [lrange $cmd 0 end-1] \
 371                                [list |& cat] \
 372                                ] r]
 373                } else {
 374                        error $err
 375                }
 376        }
 377        fconfigure $fd -eofchar {}
 378        return $fd
 379}
 380
 381proc git_read {args} {
 382        set opt [list |]
 383
 384        while {1} {
 385                switch -- [lindex $args 0] {
 386                --nice {
 387                        _lappend_nice opt
 388                }
 389
 390                --stderr {
 391                        lappend args 2>@1
 392                }
 393
 394                default {
 395                        break
 396                }
 397
 398                }
 399
 400                set args [lrange $args 1 end]
 401        }
 402
 403        set cmdp [_git_cmd [lindex $args 0]]
 404        set args [lrange $args 1 end]
 405
 406        return [_open_stdout_stderr [concat $opt $cmdp $args]]
 407}
 408
 409proc git_write {args} {
 410        set opt [list |]
 411
 412        while {1} {
 413                switch -- [lindex $args 0] {
 414                --nice {
 415                        _lappend_nice opt
 416                }
 417
 418                default {
 419                        break
 420                }
 421
 422                }
 423
 424                set args [lrange $args 1 end]
 425        }
 426
 427        set cmdp [_git_cmd [lindex $args 0]]
 428        set args [lrange $args 1 end]
 429
 430        return [open [concat $opt $cmdp $args] w]
 431}
 432
 433proc sq {value} {
 434        regsub -all ' $value "'\\''" value
 435        return "'$value'"
 436}
 437
 438proc load_current_branch {} {
 439        global current_branch is_detached
 440
 441        set fd [open [gitdir HEAD] r]
 442        if {[gets $fd ref] < 1} {
 443                set ref {}
 444        }
 445        close $fd
 446
 447        set pfx {ref: refs/heads/}
 448        set len [string length $pfx]
 449        if {[string equal -length $len $pfx $ref]} {
 450                # We're on a branch.  It might not exist.  But
 451                # HEAD looks good enough to be a branch.
 452                #
 453                set current_branch [string range $ref $len end]
 454                set is_detached 0
 455        } else {
 456                # Assume this is a detached head.
 457                #
 458                set current_branch HEAD
 459                set is_detached 1
 460        }
 461}
 462
 463auto_load tk_optionMenu
 464rename tk_optionMenu real__tkOptionMenu
 465proc tk_optionMenu {w varName args} {
 466        set m [eval real__tkOptionMenu $w $varName $args]
 467        $m configure -font font_ui
 468        $w configure -font font_ui
 469        return $m
 470}
 471
 472proc rmsel_tag {text} {
 473        $text tag conf sel \
 474                -background [$text cget -background] \
 475                -foreground [$text cget -foreground] \
 476                -borderwidth 0
 477        $text tag conf in_sel -background lightgray
 478        bind $text <Motion> break
 479        return $text
 480}
 481
 482set root_exists 0
 483bind . <Visibility> {
 484        bind . <Visibility> {}
 485        set root_exists 1
 486}
 487
 488if {[is_Windows]} {
 489        wm iconbitmap . -default $oguilib/git-gui.ico
 490}
 491
 492######################################################################
 493##
 494## config defaults
 495
 496set cursor_ptr arrow
 497font create font_diff -family Courier -size 10
 498font create font_ui
 499catch {
 500        label .dummy
 501        eval font configure font_ui [font actual [.dummy cget -font]]
 502        destroy .dummy
 503}
 504
 505font create font_uiitalic
 506font create font_uibold
 507font create font_diffbold
 508font create font_diffitalic
 509
 510foreach class {Button Checkbutton Entry Label
 511                Labelframe Listbox Menu Message
 512                Radiobutton Spinbox Text} {
 513        option add *$class.font font_ui
 514}
 515unset class
 516
 517if {[is_Windows] || [is_MacOSX]} {
 518        option add *Menu.tearOff 0
 519}
 520
 521if {[is_MacOSX]} {
 522        set M1B M1
 523        set M1T Cmd
 524} else {
 525        set M1B Control
 526        set M1T Ctrl
 527}
 528
 529proc bind_button3 {w cmd} {
 530        bind $w <Any-Button-3> $cmd
 531        if {[is_MacOSX]} {
 532                # Mac OS X sends Button-2 on right click through three-button mouse,
 533                # or through trackpad right-clicking (two-finger touch + click).
 534                bind $w <Any-Button-2> $cmd
 535                bind $w <Control-Button-1> $cmd
 536        }
 537}
 538
 539proc apply_config {} {
 540        global repo_config font_descs
 541
 542        foreach option $font_descs {
 543                set name [lindex $option 0]
 544                set font [lindex $option 1]
 545                if {[catch {
 546                        foreach {cn cv} $repo_config(gui.$name) {
 547                                font configure $font $cn $cv -weight normal
 548                        }
 549                        } err]} {
 550                        error_popup [strcat [mc "Invalid font specified in %s:" "gui.$name"] "\n\n$err"]
 551                }
 552                foreach {cn cv} [font configure $font] {
 553                        font configure ${font}bold $cn $cv
 554                        font configure ${font}italic $cn $cv
 555                }
 556                font configure ${font}bold -weight bold
 557                font configure ${font}italic -slant italic
 558        }
 559}
 560
 561set default_config(merge.diffstat) true
 562set default_config(merge.summary) false
 563set default_config(merge.verbosity) 2
 564set default_config(user.name) {}
 565set default_config(user.email) {}
 566
 567set default_config(gui.matchtrackingbranch) false
 568set default_config(gui.pruneduringfetch) false
 569set default_config(gui.trustmtime) false
 570set default_config(gui.diffcontext) 5
 571set default_config(gui.newbranchtemplate) {}
 572set default_config(gui.fontui) [font configure font_ui]
 573set default_config(gui.fontdiff) [font configure font_diff]
 574set font_descs {
 575        {fontui   font_ui   {mc "Main Font"}}
 576        {fontdiff font_diff {mc "Diff/Console Font"}}
 577}
 578
 579######################################################################
 580##
 581## find git
 582
 583set _git  [_which git]
 584if {$_git eq {}} {
 585        catch {wm withdraw .}
 586        tk_messageBox \
 587                -icon error \
 588                -type ok \
 589                -title [mc "git-gui: fatal error"] \
 590                -message [mc "Cannot find git in PATH."]
 591        exit 1
 592}
 593
 594######################################################################
 595##
 596## version check
 597
 598if {[catch {set _git_version [git --version]} err]} {
 599        catch {wm withdraw .}
 600        tk_messageBox \
 601                -icon error \
 602                -type ok \
 603                -title [mc "git-gui: fatal error"] \
 604                -message "Cannot determine Git version:
 605
 606$err
 607
 608[appname] requires Git 1.5.0 or later."
 609        exit 1
 610}
 611if {![regsub {^git version } $_git_version {} _git_version]} {
 612        catch {wm withdraw .}
 613        tk_messageBox \
 614                -icon error \
 615                -type ok \
 616                -title [mc "git-gui: fatal error"] \
 617                -message [strcat [mc "Cannot parse Git version string:"] "\n\n$_git_version"]
 618        exit 1
 619}
 620
 621set _real_git_version $_git_version
 622regsub -- {-dirty$} $_git_version {} _git_version
 623regsub {\.[0-9]+\.g[0-9a-f]+$} $_git_version {} _git_version
 624regsub {\.rc[0-9]+$} $_git_version {} _git_version
 625regsub {\.GIT$} $_git_version {} _git_version
 626regsub {\.[a-zA-Z]+\.[0-9]+$} $_git_version {} _git_version
 627
 628if {![regexp {^[1-9]+(\.[0-9]+)+$} $_git_version]} {
 629        catch {wm withdraw .}
 630        if {[tk_messageBox \
 631                -icon warning \
 632                -type yesno \
 633                -default no \
 634                -title "[appname]: warning" \
 635                 -message [mc "Git version cannot be determined.
 636
 637%s claims it is version '%s'.
 638
 639%s requires at least Git 1.5.0 or later.
 640
 641Assume '%s' is version 1.5.0?
 642" $_git $_real_git_version [appname] $_real_git_version]] eq {yes}} {
 643                set _git_version 1.5.0
 644        } else {
 645                exit 1
 646        }
 647}
 648unset _real_git_version
 649
 650proc git-version {args} {
 651        global _git_version
 652
 653        switch [llength $args] {
 654        0 {
 655                return $_git_version
 656        }
 657
 658        2 {
 659                set op [lindex $args 0]
 660                set vr [lindex $args 1]
 661                set cm [package vcompare $_git_version $vr]
 662                return [expr $cm $op 0]
 663        }
 664
 665        4 {
 666                set type [lindex $args 0]
 667                set name [lindex $args 1]
 668                set parm [lindex $args 2]
 669                set body [lindex $args 3]
 670
 671                if {($type ne {proc} && $type ne {method})} {
 672                        error "Invalid arguments to git-version"
 673                }
 674                if {[llength $body] < 2 || [lindex $body end-1] ne {default}} {
 675                        error "Last arm of $type $name must be default"
 676                }
 677
 678                foreach {op vr cb} [lrange $body 0 end-2] {
 679                        if {[git-version $op $vr]} {
 680                                return [uplevel [list $type $name $parm $cb]]
 681                        }
 682                }
 683
 684                return [uplevel [list $type $name $parm [lindex $body end]]]
 685        }
 686
 687        default {
 688                error "git-version >= x"
 689        }
 690
 691        }
 692}
 693
 694if {[git-version < 1.5]} {
 695        catch {wm withdraw .}
 696        tk_messageBox \
 697                -icon error \
 698                -type ok \
 699                -title [mc "git-gui: fatal error"] \
 700                -message "[appname] requires Git 1.5.0 or later.
 701
 702You are using [git-version]:
 703
 704[git --version]"
 705        exit 1
 706}
 707
 708######################################################################
 709##
 710## configure our library
 711
 712set idx [file join $oguilib tclIndex]
 713if {[catch {set fd [open $idx r]} err]} {
 714        catch {wm withdraw .}
 715        tk_messageBox \
 716                -icon error \
 717                -type ok \
 718                -title [mc "git-gui: fatal error"] \
 719                -message $err
 720        exit 1
 721}
 722if {[gets $fd] eq {# Autogenerated by git-gui Makefile}} {
 723        set idx [list]
 724        while {[gets $fd n] >= 0} {
 725                if {$n ne {} && ![string match #* $n]} {
 726                        lappend idx $n
 727                }
 728        }
 729} else {
 730        set idx {}
 731}
 732close $fd
 733
 734if {$idx ne {}} {
 735        set loaded [list]
 736        foreach p $idx {
 737                if {[lsearch -exact $loaded $p] >= 0} continue
 738                source [file join $oguilib $p]
 739                lappend loaded $p
 740        }
 741        unset loaded p
 742} else {
 743        set auto_path [concat [list $oguilib] $auto_path]
 744}
 745unset -nocomplain idx fd
 746
 747######################################################################
 748##
 749## config file parsing
 750
 751git-version proc _parse_config {arr_name args} {
 752        default {
 753                upvar $arr_name arr
 754                array unset arr
 755                catch {
 756                        set fd_rc [eval [list git_read config --list] $args]
 757                        while {[gets $fd_rc line] >= 0} {
 758                                if {[regexp {^([^=]+)=(.*)$} $line line name value]} {
 759                                        if {[is_many_config $name]} {
 760                                                lappend arr($name) $value
 761                                        } else {
 762                                                set arr($name) $value
 763                                        }
 764                                }
 765                        }
 766                        close $fd_rc
 767                }
 768        }
 769}
 770
 771proc load_config {include_global} {
 772        global repo_config global_config default_config
 773
 774        if {$include_global} {
 775                _parse_config global_config --global
 776        }
 777        _parse_config repo_config
 778
 779        foreach name [array names default_config] {
 780                if {[catch {set v $global_config($name)}]} {
 781                        set global_config($name) $default_config($name)
 782                }
 783                if {[catch {set v $repo_config($name)}]} {
 784                        set repo_config($name) $default_config($name)
 785                }
 786        }
 787}
 788
 789######################################################################
 790##
 791## feature option selection
 792
 793if {[regexp {^git-(.+)$} [file tail $argv0] _junk subcommand]} {
 794        unset _junk
 795} else {
 796        set subcommand gui
 797}
 798if {$subcommand eq {gui.sh}} {
 799        set subcommand gui
 800}
 801if {$subcommand eq {gui} && [llength $argv] > 0} {
 802        set subcommand [lindex $argv 0]
 803        set argv [lrange $argv 1 end]
 804}
 805
 806enable_option multicommit
 807enable_option branch
 808enable_option transport
 809disable_option bare
 810
 811switch -- $subcommand {
 812browser -
 813blame {
 814        enable_option bare
 815
 816        disable_option multicommit
 817        disable_option branch
 818        disable_option transport
 819}
 820citool {
 821        enable_option singlecommit
 822
 823        disable_option multicommit
 824        disable_option branch
 825        disable_option transport
 826}
 827}
 828
 829######################################################################
 830##
 831## repository setup
 832
 833if {[catch {
 834                set _gitdir $env(GIT_DIR)
 835                set _prefix {}
 836                }]
 837        && [catch {
 838                set _gitdir [git rev-parse --git-dir]
 839                set _prefix [git rev-parse --show-prefix]
 840        } err]} {
 841        load_config 1
 842        apply_config
 843        choose_repository::pick
 844}
 845if {![file isdirectory $_gitdir] && [is_Cygwin]} {
 846        catch {set _gitdir [exec cygpath --windows $_gitdir]}
 847}
 848if {![file isdirectory $_gitdir]} {
 849        catch {wm withdraw .}
 850        error_popup [strcat [mc "Git directory not found:"] "\n\n$_gitdir"]
 851        exit 1
 852}
 853if {$_prefix ne {}} {
 854        regsub -all {[^/]+/} $_prefix ../ cdup
 855        if {[catch {cd $cdup} err]} {
 856                catch {wm withdraw .}
 857                error_popup [strcat [mc "Cannot move to top of working directory:"] "\n\n$err"]
 858                exit 1
 859        }
 860        unset cdup
 861} elseif {![is_enabled bare]} {
 862        if {[lindex [file split $_gitdir] end] ne {.git}} {
 863                catch {wm withdraw .}
 864                error_popup [strcat [mc "Cannot use funny .git directory:"] "\n\n$_gitdir"]
 865                exit 1
 866        }
 867        if {[catch {cd [file dirname $_gitdir]} err]} {
 868                catch {wm withdraw .}
 869                error_popup [strcat [mc "No working directory"] " [file dirname $_gitdir]:\n\n$err"]
 870                exit 1
 871        }
 872}
 873set _reponame [file split [file normalize $_gitdir]]
 874if {[lindex $_reponame end] eq {.git}} {
 875        set _reponame [lindex $_reponame end-1]
 876} else {
 877        set _reponame [lindex $_reponame end]
 878}
 879
 880######################################################################
 881##
 882## global init
 883
 884set current_diff_path {}
 885set current_diff_side {}
 886set diff_actions [list]
 887
 888set HEAD {}
 889set PARENT {}
 890set MERGE_HEAD [list]
 891set commit_type {}
 892set empty_tree {}
 893set current_branch {}
 894set is_detached 0
 895set current_diff_path {}
 896set is_3way_diff 0
 897set selected_commit_type new
 898
 899######################################################################
 900##
 901## task management
 902
 903set rescan_active 0
 904set diff_active 0
 905set last_clicked {}
 906
 907set disable_on_lock [list]
 908set index_lock_type none
 909
 910proc lock_index {type} {
 911        global index_lock_type disable_on_lock
 912
 913        if {$index_lock_type eq {none}} {
 914                set index_lock_type $type
 915                foreach w $disable_on_lock {
 916                        uplevel #0 $w disabled
 917                }
 918                return 1
 919        } elseif {$index_lock_type eq "begin-$type"} {
 920                set index_lock_type $type
 921                return 1
 922        }
 923        return 0
 924}
 925
 926proc unlock_index {} {
 927        global index_lock_type disable_on_lock
 928
 929        set index_lock_type none
 930        foreach w $disable_on_lock {
 931                uplevel #0 $w normal
 932        }
 933}
 934
 935######################################################################
 936##
 937## status
 938
 939proc repository_state {ctvar hdvar mhvar} {
 940        global current_branch
 941        upvar $ctvar ct $hdvar hd $mhvar mh
 942
 943        set mh [list]
 944
 945        load_current_branch
 946        if {[catch {set hd [git rev-parse --verify HEAD]}]} {
 947                set hd {}
 948                set ct initial
 949                return
 950        }
 951
 952        set merge_head [gitdir MERGE_HEAD]
 953        if {[file exists $merge_head]} {
 954                set ct merge
 955                set fd_mh [open $merge_head r]
 956                while {[gets $fd_mh line] >= 0} {
 957                        lappend mh $line
 958                }
 959                close $fd_mh
 960                return
 961        }
 962
 963        set ct normal
 964}
 965
 966proc PARENT {} {
 967        global PARENT empty_tree
 968
 969        set p [lindex $PARENT 0]
 970        if {$p ne {}} {
 971                return $p
 972        }
 973        if {$empty_tree eq {}} {
 974                set empty_tree [git mktree << {}]
 975        }
 976        return $empty_tree
 977}
 978
 979proc rescan {after {honor_trustmtime 1}} {
 980        global HEAD PARENT MERGE_HEAD commit_type
 981        global ui_index ui_workdir ui_comm
 982        global rescan_active file_states
 983        global repo_config
 984
 985        if {$rescan_active > 0 || ![lock_index read]} return
 986
 987        repository_state newType newHEAD newMERGE_HEAD
 988        if {[string match amend* $commit_type]
 989                && $newType eq {normal}
 990                && $newHEAD eq $HEAD} {
 991        } else {
 992                set HEAD $newHEAD
 993                set PARENT $newHEAD
 994                set MERGE_HEAD $newMERGE_HEAD
 995                set commit_type $newType
 996        }
 997
 998        array unset file_states
 999
1000        if {!$::GITGUI_BCK_exists &&
1001                (![$ui_comm edit modified]
1002                || [string trim [$ui_comm get 0.0 end]] eq {})} {
1003                if {[string match amend* $commit_type]} {
1004                } elseif {[load_message GITGUI_MSG]} {
1005                } elseif {[load_message MERGE_MSG]} {
1006                } elseif {[load_message SQUASH_MSG]} {
1007                }
1008                $ui_comm edit reset
1009                $ui_comm edit modified false
1010        }
1011
1012        if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
1013                rescan_stage2 {} $after
1014        } else {
1015                set rescan_active 1
1016                ui_status [mc "Refreshing file status..."]
1017                set fd_rf [git_read update-index \
1018                        -q \
1019                        --unmerged \
1020                        --ignore-missing \
1021                        --refresh \
1022                        ]
1023                fconfigure $fd_rf -blocking 0 -translation binary
1024                fileevent $fd_rf readable \
1025                        [list rescan_stage2 $fd_rf $after]
1026        }
1027}
1028
1029if {[is_Cygwin]} {
1030        set is_git_info_link {}
1031        set is_git_info_exclude {}
1032        proc have_info_exclude {} {
1033                global is_git_info_link is_git_info_exclude
1034
1035                if {$is_git_info_link eq {}} {
1036                        set is_git_info_link [file isfile [gitdir info.lnk]]
1037                }
1038
1039                if {$is_git_info_link} {
1040                        if {$is_git_info_exclude eq {}} {
1041                                if {[catch {exec test -f [gitdir info exclude]}]} {
1042                                        set is_git_info_exclude 0
1043                                } else {
1044                                        set is_git_info_exclude 1
1045                                }
1046                        }
1047                        return $is_git_info_exclude
1048                } else {
1049                        return [file readable [gitdir info exclude]]
1050                }
1051        }
1052} else {
1053        proc have_info_exclude {} {
1054                return [file readable [gitdir info exclude]]
1055        }
1056}
1057
1058proc rescan_stage2 {fd after} {
1059        global rescan_active buf_rdi buf_rdf buf_rlo
1060
1061        if {$fd ne {}} {
1062                read $fd
1063                if {![eof $fd]} return
1064                close $fd
1065        }
1066
1067        set ls_others [list --exclude-per-directory=.gitignore]
1068        if {[have_info_exclude]} {
1069                lappend ls_others "--exclude-from=[gitdir info exclude]"
1070        }
1071        set user_exclude [get_config core.excludesfile]
1072        if {$user_exclude ne {} && [file readable $user_exclude]} {
1073                lappend ls_others "--exclude-from=$user_exclude"
1074        }
1075
1076        set buf_rdi {}
1077        set buf_rdf {}
1078        set buf_rlo {}
1079
1080        set rescan_active 3
1081        ui_status [mc "Scanning for modified files ..."]
1082        set fd_di [git_read diff-index --cached -z [PARENT]]
1083        set fd_df [git_read diff-files -z]
1084        set fd_lo [eval git_read ls-files --others -z $ls_others]
1085
1086        fconfigure $fd_di -blocking 0 -translation binary -encoding binary
1087        fconfigure $fd_df -blocking 0 -translation binary -encoding binary
1088        fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
1089        fileevent $fd_di readable [list read_diff_index $fd_di $after]
1090        fileevent $fd_df readable [list read_diff_files $fd_df $after]
1091        fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
1092}
1093
1094proc load_message {file} {
1095        global ui_comm
1096
1097        set f [gitdir $file]
1098        if {[file isfile $f]} {
1099                if {[catch {set fd [open $f r]}]} {
1100                        return 0
1101                }
1102                fconfigure $fd -eofchar {}
1103                set content [string trim [read $fd]]
1104                close $fd
1105                regsub -all -line {[ \r\t]+$} $content {} content
1106                $ui_comm delete 0.0 end
1107                $ui_comm insert end $content
1108                return 1
1109        }
1110        return 0
1111}
1112
1113proc read_diff_index {fd after} {
1114        global buf_rdi
1115
1116        append buf_rdi [read $fd]
1117        set c 0
1118        set n [string length $buf_rdi]
1119        while {$c < $n} {
1120                set z1 [string first "\0" $buf_rdi $c]
1121                if {$z1 == -1} break
1122                incr z1
1123                set z2 [string first "\0" $buf_rdi $z1]
1124                if {$z2 == -1} break
1125
1126                incr c
1127                set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
1128                set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
1129                merge_state \
1130                        [encoding convertfrom $p] \
1131                        [lindex $i 4]? \
1132                        [list [lindex $i 0] [lindex $i 2]] \
1133                        [list]
1134                set c $z2
1135                incr c
1136        }
1137        if {$c < $n} {
1138                set buf_rdi [string range $buf_rdi $c end]
1139        } else {
1140                set buf_rdi {}
1141        }
1142
1143        rescan_done $fd buf_rdi $after
1144}
1145
1146proc read_diff_files {fd after} {
1147        global buf_rdf
1148
1149        append buf_rdf [read $fd]
1150        set c 0
1151        set n [string length $buf_rdf]
1152        while {$c < $n} {
1153                set z1 [string first "\0" $buf_rdf $c]
1154                if {$z1 == -1} break
1155                incr z1
1156                set z2 [string first "\0" $buf_rdf $z1]
1157                if {$z2 == -1} break
1158
1159                incr c
1160                set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
1161                set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
1162                merge_state \
1163                        [encoding convertfrom $p] \
1164                        ?[lindex $i 4] \
1165                        [list] \
1166                        [list [lindex $i 0] [lindex $i 2]]
1167                set c $z2
1168                incr c
1169        }
1170        if {$c < $n} {
1171                set buf_rdf [string range $buf_rdf $c end]
1172        } else {
1173                set buf_rdf {}
1174        }
1175
1176        rescan_done $fd buf_rdf $after
1177}
1178
1179proc read_ls_others {fd after} {
1180        global buf_rlo
1181
1182        append buf_rlo [read $fd]
1183        set pck [split $buf_rlo "\0"]
1184        set buf_rlo [lindex $pck end]
1185        foreach p [lrange $pck 0 end-1] {
1186                set p [encoding convertfrom $p]
1187                if {[string index $p end] eq {/}} {
1188                        set p [string range $p 0 end-1]
1189                }
1190                merge_state $p ?O
1191        }
1192        rescan_done $fd buf_rlo $after
1193}
1194
1195proc rescan_done {fd buf after} {
1196        global rescan_active current_diff_path
1197        global file_states repo_config
1198        upvar $buf to_clear
1199
1200        if {![eof $fd]} return
1201        set to_clear {}
1202        close $fd
1203        if {[incr rescan_active -1] > 0} return
1204
1205        prune_selection
1206        unlock_index
1207        display_all_files
1208        if {$current_diff_path ne {}} reshow_diff
1209        uplevel #0 $after
1210}
1211
1212proc prune_selection {} {
1213        global file_states selected_paths
1214
1215        foreach path [array names selected_paths] {
1216                if {[catch {set still_here $file_states($path)}]} {
1217                        unset selected_paths($path)
1218                }
1219        }
1220}
1221
1222######################################################################
1223##
1224## ui helpers
1225
1226proc mapicon {w state path} {
1227        global all_icons
1228
1229        if {[catch {set r $all_icons($state$w)}]} {
1230                puts "error: no icon for $w state={$state} $path"
1231                return file_plain
1232        }
1233        return $r
1234}
1235
1236proc mapdesc {state path} {
1237        global all_descs
1238
1239        if {[catch {set r $all_descs($state)}]} {
1240                puts "error: no desc for state={$state} $path"
1241                return $state
1242        }
1243        return $r
1244}
1245
1246proc ui_status {msg} {
1247        global main_status
1248        if {[info exists main_status]} {
1249                $main_status show $msg
1250        }
1251}
1252
1253proc ui_ready {{test {}}} {
1254        global main_status
1255        if {[info exists main_status]} {
1256                $main_status show [mc "Ready."] $test
1257        }
1258}
1259
1260proc escape_path {path} {
1261        regsub -all {\\} $path "\\\\" path
1262        regsub -all "\n" $path "\\n" path
1263        return $path
1264}
1265
1266proc short_path {path} {
1267        return [escape_path [lindex [file split $path] end]]
1268}
1269
1270set next_icon_id 0
1271set null_sha1 [string repeat 0 40]
1272
1273proc merge_state {path new_state {head_info {}} {index_info {}}} {
1274        global file_states next_icon_id null_sha1
1275
1276        set s0 [string index $new_state 0]
1277        set s1 [string index $new_state 1]
1278
1279        if {[catch {set info $file_states($path)}]} {
1280                set state __
1281                set icon n[incr next_icon_id]
1282        } else {
1283                set state [lindex $info 0]
1284                set icon [lindex $info 1]
1285                if {$head_info eq {}}  {set head_info  [lindex $info 2]}
1286                if {$index_info eq {}} {set index_info [lindex $info 3]}
1287        }
1288
1289        if     {$s0 eq {?}} {set s0 [string index $state 0]} \
1290        elseif {$s0 eq {_}} {set s0 _}
1291
1292        if     {$s1 eq {?}} {set s1 [string index $state 1]} \
1293        elseif {$s1 eq {_}} {set s1 _}
1294
1295        if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
1296                set head_info [list 0 $null_sha1]
1297        } elseif {$s0 ne {_} && [string index $state 0] eq {_}
1298                && $head_info eq {}} {
1299                set head_info $index_info
1300        }
1301
1302        set file_states($path) [list $s0$s1 $icon \
1303                $head_info $index_info \
1304                ]
1305        return $state
1306}
1307
1308proc display_file_helper {w path icon_name old_m new_m} {
1309        global file_lists
1310
1311        if {$new_m eq {_}} {
1312                set lno [lsearch -sorted -exact $file_lists($w) $path]
1313                if {$lno >= 0} {
1314                        set file_lists($w) [lreplace $file_lists($w) $lno $lno]
1315                        incr lno
1316                        $w conf -state normal
1317                        $w delete $lno.0 [expr {$lno + 1}].0
1318                        $w conf -state disabled
1319                }
1320        } elseif {$old_m eq {_} && $new_m ne {_}} {
1321                lappend file_lists($w) $path
1322                set file_lists($w) [lsort -unique $file_lists($w)]
1323                set lno [lsearch -sorted -exact $file_lists($w) $path]
1324                incr lno
1325                $w conf -state normal
1326                $w image create $lno.0 \
1327                        -align center -padx 5 -pady 1 \
1328                        -name $icon_name \
1329                        -image [mapicon $w $new_m $path]
1330                $w insert $lno.1 "[escape_path $path]\n"
1331                $w conf -state disabled
1332        } elseif {$old_m ne $new_m} {
1333                $w conf -state normal
1334                $w image conf $icon_name -image [mapicon $w $new_m $path]
1335                $w conf -state disabled
1336        }
1337}
1338
1339proc display_file {path state} {
1340        global file_states selected_paths
1341        global ui_index ui_workdir
1342
1343        set old_m [merge_state $path $state]
1344        set s $file_states($path)
1345        set new_m [lindex $s 0]
1346        set icon_name [lindex $s 1]
1347
1348        set o [string index $old_m 0]
1349        set n [string index $new_m 0]
1350        if {$o eq {U}} {
1351                set o _
1352        }
1353        if {$n eq {U}} {
1354                set n _
1355        }
1356        display_file_helper     $ui_index $path $icon_name $o $n
1357
1358        if {[string index $old_m 0] eq {U}} {
1359                set o U
1360        } else {
1361                set o [string index $old_m 1]
1362        }
1363        if {[string index $new_m 0] eq {U}} {
1364                set n U
1365        } else {
1366                set n [string index $new_m 1]
1367        }
1368        display_file_helper     $ui_workdir $path $icon_name $o $n
1369
1370        if {$new_m eq {__}} {
1371                unset file_states($path)
1372                catch {unset selected_paths($path)}
1373        }
1374}
1375
1376proc display_all_files_helper {w path icon_name m} {
1377        global file_lists
1378
1379        lappend file_lists($w) $path
1380        set lno [expr {[lindex [split [$w index end] .] 0] - 1}]
1381        $w image create end \
1382                -align center -padx 5 -pady 1 \
1383                -name $icon_name \
1384                -image [mapicon $w $m $path]
1385        $w insert end "[escape_path $path]\n"
1386}
1387
1388proc display_all_files {} {
1389        global ui_index ui_workdir
1390        global file_states file_lists
1391        global last_clicked
1392
1393        $ui_index conf -state normal
1394        $ui_workdir conf -state normal
1395
1396        $ui_index delete 0.0 end
1397        $ui_workdir delete 0.0 end
1398        set last_clicked {}
1399
1400        set file_lists($ui_index) [list]
1401        set file_lists($ui_workdir) [list]
1402
1403        foreach path [lsort [array names file_states]] {
1404                set s $file_states($path)
1405                set m [lindex $s 0]
1406                set icon_name [lindex $s 1]
1407
1408                set s [string index $m 0]
1409                if {$s ne {U} && $s ne {_}} {
1410                        display_all_files_helper $ui_index $path \
1411                                $icon_name $s
1412                }
1413
1414                if {[string index $m 0] eq {U}} {
1415                        set s U
1416                } else {
1417                        set s [string index $m 1]
1418                }
1419                if {$s ne {_}} {
1420                        display_all_files_helper $ui_workdir $path \
1421                                $icon_name $s
1422                }
1423        }
1424
1425        $ui_index conf -state disabled
1426        $ui_workdir conf -state disabled
1427}
1428
1429######################################################################
1430##
1431## icons
1432
1433set filemask {
1434#define mask_width 14
1435#define mask_height 15
1436static unsigned char mask_bits[] = {
1437   0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1438   0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f,
1439   0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f};
1440}
1441
1442image create bitmap file_plain -background white -foreground black -data {
1443#define plain_width 14
1444#define plain_height 15
1445static unsigned char plain_bits[] = {
1446   0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1447   0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x10,
1448   0x02, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1449} -maskdata $filemask
1450
1451image create bitmap file_mod -background white -foreground blue -data {
1452#define mod_width 14
1453#define mod_height 15
1454static unsigned char mod_bits[] = {
1455   0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1456   0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1457   0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1458} -maskdata $filemask
1459
1460image create bitmap file_fulltick -background white -foreground "#007000" -data {
1461#define file_fulltick_width 14
1462#define file_fulltick_height 15
1463static unsigned char file_fulltick_bits[] = {
1464   0xfe, 0x01, 0x02, 0x1a, 0x02, 0x0c, 0x02, 0x0c, 0x02, 0x16, 0x02, 0x16,
1465   0x02, 0x13, 0x00, 0x13, 0x86, 0x11, 0x8c, 0x11, 0xd8, 0x10, 0xf2, 0x10,
1466   0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1467} -maskdata $filemask
1468
1469image create bitmap file_parttick -background white -foreground "#005050" -data {
1470#define parttick_width 14
1471#define parttick_height 15
1472static unsigned char parttick_bits[] = {
1473   0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
1474   0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
1475   0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1476} -maskdata $filemask
1477
1478image create bitmap file_question -background white -foreground black -data {
1479#define file_question_width 14
1480#define file_question_height 15
1481static unsigned char file_question_bits[] = {
1482   0xfe, 0x01, 0x02, 0x02, 0xe2, 0x04, 0xf2, 0x09, 0x1a, 0x1b, 0x0a, 0x13,
1483   0x82, 0x11, 0xc2, 0x10, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10, 0x62, 0x10,
1484   0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
1485} -maskdata $filemask
1486
1487image create bitmap file_removed -background white -foreground red -data {
1488#define file_removed_width 14
1489#define file_removed_height 15
1490static unsigned char file_removed_bits[] = {
1491   0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x02, 0x10,
1492   0x1a, 0x16, 0x32, 0x13, 0xe2, 0x11, 0xc2, 0x10, 0xe2, 0x11, 0x32, 0x13,
1493   0x1a, 0x16, 0x02, 0x10, 0xfe, 0x1f};
1494} -maskdata $filemask
1495
1496image create bitmap file_merge -background white -foreground blue -data {
1497#define file_merge_width 14
1498#define file_merge_height 15
1499static unsigned char file_merge_bits[] = {
1500   0xfe, 0x01, 0x02, 0x03, 0x62, 0x05, 0x62, 0x09, 0x62, 0x1f, 0x62, 0x10,
1501   0xfa, 0x11, 0xf2, 0x10, 0x62, 0x10, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
1502   0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
1503} -maskdata $filemask
1504
1505set ui_index .vpane.files.index.list
1506set ui_workdir .vpane.files.workdir.list
1507
1508set all_icons(_$ui_index)   file_plain
1509set all_icons(A$ui_index)   file_fulltick
1510set all_icons(M$ui_index)   file_fulltick
1511set all_icons(D$ui_index)   file_removed
1512set all_icons(U$ui_index)   file_merge
1513
1514set all_icons(_$ui_workdir) file_plain
1515set all_icons(M$ui_workdir) file_mod
1516set all_icons(D$ui_workdir) file_question
1517set all_icons(U$ui_workdir) file_merge
1518set all_icons(O$ui_workdir) file_plain
1519
1520set max_status_desc 0
1521foreach i {
1522                {__ {mc "Unmodified"}}
1523
1524                {_M {mc "Modified, not staged"}}
1525                {M_ {mc "Staged for commit"}}
1526                {MM {mc "Portions staged for commit"}}
1527                {MD {mc "Staged for commit, missing"}}
1528
1529                {_O {mc "Untracked, not staged"}}
1530                {A_ {mc "Staged for commit"}}
1531                {AM {mc "Portions staged for commit"}}
1532                {AD {mc "Staged for commit, missing"}}
1533
1534                {_D {mc "Missing"}}
1535                {D_ {mc "Staged for removal"}}
1536                {DO {mc "Staged for removal, still present"}}
1537
1538                {U_ {mc "Requires merge resolution"}}
1539                {UU {mc "Requires merge resolution"}}
1540                {UM {mc "Requires merge resolution"}}
1541                {UD {mc "Requires merge resolution"}}
1542        } {
1543        set text [eval [lindex $i 1]]
1544        if {$max_status_desc < [string length $text]} {
1545                set max_status_desc [string length $text]
1546        }
1547        set all_descs([lindex $i 0]) $text
1548}
1549unset i
1550
1551######################################################################
1552##
1553## util
1554
1555proc scrollbar2many {list mode args} {
1556        foreach w $list {eval $w $mode $args}
1557}
1558
1559proc many2scrollbar {list mode sb top bottom} {
1560        $sb set $top $bottom
1561        foreach w $list {$w $mode moveto $top}
1562}
1563
1564proc incr_font_size {font {amt 1}} {
1565        set sz [font configure $font -size]
1566        incr sz $amt
1567        font configure $font -size $sz
1568        font configure ${font}bold -size $sz
1569        font configure ${font}italic -size $sz
1570}
1571
1572######################################################################
1573##
1574## ui commands
1575
1576set starting_gitk_msg [mc "Starting gitk... please wait..."]
1577
1578proc do_gitk {revs} {
1579        # -- Always start gitk through whatever we were loaded with.  This
1580        #    lets us bypass using shell process on Windows systems.
1581        #
1582        set exe [file join [file dirname $::_git] gitk]
1583        set cmd [list [info nameofexecutable] $exe]
1584        if {! [file exists $exe]} {
1585                error_popup [mc "Unable to start gitk:\n\n%s does not exist" $exe]
1586        } else {
1587                global env
1588
1589                if {[info exists env(GIT_DIR)]} {
1590                        set old_GIT_DIR $env(GIT_DIR)
1591                } else {
1592                        set old_GIT_DIR {}
1593                }
1594
1595                set pwd [pwd]
1596                cd [file dirname [gitdir]]
1597                set env(GIT_DIR) [file tail [gitdir]]
1598
1599                eval exec $cmd $revs &
1600
1601                if {$old_GIT_DIR eq {}} {
1602                        unset env(GIT_DIR)
1603                } else {
1604                        set env(GIT_DIR) $old_GIT_DIR
1605                }
1606                cd $pwd
1607
1608                ui_status $::starting_gitk_msg
1609                after 10000 {
1610                        ui_ready $starting_gitk_msg
1611                }
1612        }
1613}
1614
1615set is_quitting 0
1616
1617proc do_quit {} {
1618        global ui_comm is_quitting repo_config commit_type
1619        global GITGUI_BCK_exists GITGUI_BCK_i
1620
1621        if {$is_quitting} return
1622        set is_quitting 1
1623
1624        if {[winfo exists $ui_comm]} {
1625                # -- Stash our current commit buffer.
1626                #
1627                set save [gitdir GITGUI_MSG]
1628                if {$GITGUI_BCK_exists && ![$ui_comm edit modified]} {
1629                        file rename -force [gitdir GITGUI_BCK] $save
1630                        set GITGUI_BCK_exists 0
1631                } else {
1632                        set msg [string trim [$ui_comm get 0.0 end]]
1633                        regsub -all -line {[ \r\t]+$} $msg {} msg
1634                        if {(![string match amend* $commit_type]
1635                                || [$ui_comm edit modified])
1636                                && $msg ne {}} {
1637                                catch {
1638                                        set fd [open $save w]
1639                                        puts -nonewline $fd $msg
1640                                        close $fd
1641                                }
1642                        } else {
1643                                catch {file delete $save}
1644                        }
1645                }
1646
1647                # -- Remove our editor backup, its not needed.
1648                #
1649                after cancel $GITGUI_BCK_i
1650                if {$GITGUI_BCK_exists} {
1651                        catch {file delete [gitdir GITGUI_BCK]}
1652                }
1653
1654                # -- Stash our current window geometry into this repository.
1655                #
1656                set cfg_geometry [list]
1657                lappend cfg_geometry [wm geometry .]
1658                lappend cfg_geometry [lindex [.vpane sash coord 0] 0]
1659                lappend cfg_geometry [lindex [.vpane.files sash coord 0] 1]
1660                if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
1661                        set rc_geometry {}
1662                }
1663                if {$cfg_geometry ne $rc_geometry} {
1664                        catch {git config gui.geometry $cfg_geometry}
1665                }
1666        }
1667
1668        destroy .
1669}
1670
1671proc do_rescan {} {
1672        rescan ui_ready
1673}
1674
1675proc do_commit {} {
1676        commit_tree
1677}
1678
1679proc toggle_or_diff {w x y} {
1680        global file_states file_lists current_diff_path ui_index ui_workdir
1681        global last_clicked selected_paths
1682
1683        set pos [split [$w index @$x,$y] .]
1684        set lno [lindex $pos 0]
1685        set col [lindex $pos 1]
1686        set path [lindex $file_lists($w) [expr {$lno - 1}]]
1687        if {$path eq {}} {
1688                set last_clicked {}
1689                return
1690        }
1691
1692        set last_clicked [list $w $lno]
1693        array unset selected_paths
1694        $ui_index tag remove in_sel 0.0 end
1695        $ui_workdir tag remove in_sel 0.0 end
1696
1697        if {$col == 0} {
1698                if {$current_diff_path eq $path} {
1699                        set after {reshow_diff;}
1700                } else {
1701                        set after {}
1702                }
1703                if {$w eq $ui_index} {
1704                        update_indexinfo \
1705                                "Unstaging [short_path $path] from commit" \
1706                                [list $path] \
1707                                [concat $after [list ui_ready]]
1708                } elseif {$w eq $ui_workdir} {
1709                        update_index \
1710                                "Adding [short_path $path]" \
1711                                [list $path] \
1712                                [concat $after [list ui_ready]]
1713                }
1714        } else {
1715                show_diff $path $w $lno
1716        }
1717}
1718
1719proc add_one_to_selection {w x y} {
1720        global file_lists last_clicked selected_paths
1721
1722        set lno [lindex [split [$w index @$x,$y] .] 0]
1723        set path [lindex $file_lists($w) [expr {$lno - 1}]]
1724        if {$path eq {}} {
1725                set last_clicked {}
1726                return
1727        }
1728
1729        if {$last_clicked ne {}
1730                && [lindex $last_clicked 0] ne $w} {
1731                array unset selected_paths
1732                [lindex $last_clicked 0] tag remove in_sel 0.0 end
1733        }
1734
1735        set last_clicked [list $w $lno]
1736        if {[catch {set in_sel $selected_paths($path)}]} {
1737                set in_sel 0
1738        }
1739        if {$in_sel} {
1740                unset selected_paths($path)
1741                $w tag remove in_sel $lno.0 [expr {$lno + 1}].0
1742        } else {
1743                set selected_paths($path) 1
1744                $w tag add in_sel $lno.0 [expr {$lno + 1}].0
1745        }
1746}
1747
1748proc add_range_to_selection {w x y} {
1749        global file_lists last_clicked selected_paths
1750
1751        if {[lindex $last_clicked 0] ne $w} {
1752                toggle_or_diff $w $x $y
1753                return
1754        }
1755
1756        set lno [lindex [split [$w index @$x,$y] .] 0]
1757        set lc [lindex $last_clicked 1]
1758        if {$lc < $lno} {
1759                set begin $lc
1760                set end $lno
1761        } else {
1762                set begin $lno
1763                set end $lc
1764        }
1765
1766        foreach path [lrange $file_lists($w) \
1767                [expr {$begin - 1}] \
1768                [expr {$end - 1}]] {
1769                set selected_paths($path) 1
1770        }
1771        $w tag add in_sel $begin.0 [expr {$end + 1}].0
1772}
1773
1774######################################################################
1775##
1776## ui construction
1777
1778load_config 0
1779apply_config
1780set ui_comm {}
1781
1782# -- Menu Bar
1783#
1784menu .mbar -tearoff 0
1785.mbar add cascade -label [mc Repository] -menu .mbar.repository
1786.mbar add cascade -label [mc Edit] -menu .mbar.edit
1787if {[is_enabled branch]} {
1788        .mbar add cascade -label [mc Branch] -menu .mbar.branch
1789}
1790if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1791        .mbar add cascade -label [mc Commit@@noun] -menu .mbar.commit
1792}
1793if {[is_enabled transport]} {
1794        .mbar add cascade -label [mc Merge] -menu .mbar.merge
1795        .mbar add cascade -label [mc Remote] -menu .mbar.remote
1796}
1797. configure -menu .mbar
1798
1799# -- Repository Menu
1800#
1801menu .mbar.repository
1802
1803.mbar.repository add command \
1804        -label [mc "Browse Current Branch's Files"] \
1805        -command {browser::new $current_branch}
1806set ui_browse_current [.mbar.repository index last]
1807.mbar.repository add command \
1808        -label [mc "Browse Branch Files..."] \
1809        -command browser_open::dialog
1810.mbar.repository add separator
1811
1812.mbar.repository add command \
1813        -label [mc "Visualize Current Branch's History"] \
1814        -command {do_gitk $current_branch}
1815set ui_visualize_current [.mbar.repository index last]
1816.mbar.repository add command \
1817        -label [mc "Visualize All Branch History"] \
1818        -command {do_gitk --all}
1819.mbar.repository add separator
1820
1821proc current_branch_write {args} {
1822        global current_branch
1823        .mbar.repository entryconf $::ui_browse_current \
1824                -label [mc "Browse %s's Files" $current_branch]
1825        .mbar.repository entryconf $::ui_visualize_current \
1826                -label [mc "Visualize %s's History" $current_branch]
1827}
1828trace add variable current_branch write current_branch_write
1829
1830if {[is_enabled multicommit]} {
1831        .mbar.repository add command -label [mc "Database Statistics"] \
1832                -command do_stats
1833
1834        .mbar.repository add command -label [mc "Compress Database"] \
1835                -command do_gc
1836
1837        .mbar.repository add command -label [mc "Verify Database"] \
1838                -command do_fsck_objects
1839
1840        .mbar.repository add separator
1841
1842        if {[is_Cygwin]} {
1843                .mbar.repository add command \
1844                        -label [mc "Create Desktop Icon"] \
1845                        -command do_cygwin_shortcut
1846        } elseif {[is_Windows]} {
1847                .mbar.repository add command \
1848                        -label [mc "Create Desktop Icon"] \
1849                        -command do_windows_shortcut
1850        } elseif {[is_MacOSX]} {
1851                .mbar.repository add command \
1852                        -label [mc "Create Desktop Icon"] \
1853                        -command do_macosx_app
1854        }
1855}
1856
1857.mbar.repository add command -label [mc Quit] \
1858        -command do_quit \
1859        -accelerator $M1T-Q
1860
1861# -- Edit Menu
1862#
1863menu .mbar.edit
1864.mbar.edit add command -label [mc Undo] \
1865        -command {catch {[focus] edit undo}} \
1866        -accelerator $M1T-Z
1867.mbar.edit add command -label [mc Redo] \
1868        -command {catch {[focus] edit redo}} \
1869        -accelerator $M1T-Y
1870.mbar.edit add separator
1871.mbar.edit add command -label [mc Cut] \
1872        -command {catch {tk_textCut [focus]}} \
1873        -accelerator $M1T-X
1874.mbar.edit add command -label [mc Copy] \
1875        -command {catch {tk_textCopy [focus]}} \
1876        -accelerator $M1T-C
1877.mbar.edit add command -label [mc Paste] \
1878        -command {catch {tk_textPaste [focus]; [focus] see insert}} \
1879        -accelerator $M1T-V
1880.mbar.edit add command -label [mc Delete] \
1881        -command {catch {[focus] delete sel.first sel.last}} \
1882        -accelerator Del
1883.mbar.edit add separator
1884.mbar.edit add command -label [mc "Select All"] \
1885        -command {catch {[focus] tag add sel 0.0 end}} \
1886        -accelerator $M1T-A
1887
1888# -- Branch Menu
1889#
1890if {[is_enabled branch]} {
1891        menu .mbar.branch
1892
1893        .mbar.branch add command -label [mc "Create..."] \
1894                -command branch_create::dialog \
1895                -accelerator $M1T-N
1896        lappend disable_on_lock [list .mbar.branch entryconf \
1897                [.mbar.branch index last] -state]
1898
1899        .mbar.branch add command -label [mc "Checkout..."] \
1900                -command branch_checkout::dialog \
1901                -accelerator $M1T-O
1902        lappend disable_on_lock [list .mbar.branch entryconf \
1903                [.mbar.branch index last] -state]
1904
1905        .mbar.branch add command -label [mc "Rename..."] \
1906                -command branch_rename::dialog
1907        lappend disable_on_lock [list .mbar.branch entryconf \
1908                [.mbar.branch index last] -state]
1909
1910        .mbar.branch add command -label [mc "Delete..."] \
1911                -command branch_delete::dialog
1912        lappend disable_on_lock [list .mbar.branch entryconf \
1913                [.mbar.branch index last] -state]
1914
1915        .mbar.branch add command -label [mc "Reset..."] \
1916                -command merge::reset_hard
1917        lappend disable_on_lock [list .mbar.branch entryconf \
1918                [.mbar.branch index last] -state]
1919}
1920
1921# -- Commit Menu
1922#
1923if {[is_enabled multicommit] || [is_enabled singlecommit]} {
1924        menu .mbar.commit
1925
1926        .mbar.commit add radiobutton \
1927                -label [mc "New Commit"] \
1928                -command do_select_commit_type \
1929                -variable selected_commit_type \
1930                -value new
1931        lappend disable_on_lock \
1932                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1933
1934        .mbar.commit add radiobutton \
1935                -label [mc "Amend Last Commit"] \
1936                -command do_select_commit_type \
1937                -variable selected_commit_type \
1938                -value amend
1939        lappend disable_on_lock \
1940                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1941
1942        .mbar.commit add separator
1943
1944        .mbar.commit add command -label [mc Rescan] \
1945                -command do_rescan \
1946                -accelerator F5
1947        lappend disable_on_lock \
1948                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1949
1950        .mbar.commit add command -label [mc "Stage To Commit"] \
1951                -command do_add_selection
1952        lappend disable_on_lock \
1953                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1954
1955        .mbar.commit add command -label [mc "Stage Changed Files To Commit"] \
1956                -command do_add_all \
1957                -accelerator $M1T-I
1958        lappend disable_on_lock \
1959                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1960
1961        .mbar.commit add command -label [mc "Unstage From Commit"] \
1962                -command do_unstage_selection
1963        lappend disable_on_lock \
1964                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1965
1966        .mbar.commit add command -label [mc "Revert Changes"] \
1967                -command do_revert_selection
1968        lappend disable_on_lock \
1969                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1970
1971        .mbar.commit add separator
1972
1973        .mbar.commit add command -label [mc "Sign Off"] \
1974                -command do_signoff \
1975                -accelerator $M1T-S
1976
1977        .mbar.commit add command -label [mc Commit@@verb] \
1978                -command do_commit \
1979                -accelerator $M1T-Return
1980        lappend disable_on_lock \
1981                [list .mbar.commit entryconf [.mbar.commit index last] -state]
1982}
1983
1984# -- Merge Menu
1985#
1986if {[is_enabled branch]} {
1987        menu .mbar.merge
1988        .mbar.merge add command -label [mc "Local Merge..."] \
1989                -command merge::dialog \
1990                -accelerator $M1T-M
1991        lappend disable_on_lock \
1992                [list .mbar.merge entryconf [.mbar.merge index last] -state]
1993        .mbar.merge add command -label [mc "Abort Merge..."] \
1994                -command merge::reset_hard
1995        lappend disable_on_lock \
1996                [list .mbar.merge entryconf [.mbar.merge index last] -state]
1997}
1998
1999# -- Transport Menu
2000#
2001if {[is_enabled transport]} {
2002        menu .mbar.remote
2003
2004        .mbar.remote add command \
2005                -label [mc "Push..."] \
2006                -command do_push_anywhere \
2007                -accelerator $M1T-P
2008        .mbar.remote add command \
2009                -label [mc "Delete..."] \
2010                -command remote_branch_delete::dialog
2011}
2012
2013if {[is_MacOSX]} {
2014        # -- Apple Menu (Mac OS X only)
2015        #
2016        .mbar add cascade -label [mc Apple] -menu .mbar.apple
2017        menu .mbar.apple
2018
2019        .mbar.apple add command -label [mc "About %s" [appname]] \
2020                -command do_about
2021        .mbar.apple add separator
2022        .mbar.apple add command \
2023                -label [mc "Preferences..."] \
2024                -command do_options \
2025                -accelerator $M1T-,
2026        bind . <$M1B-,> do_options
2027} else {
2028        # -- Edit Menu
2029        #
2030        .mbar.edit add separator
2031        .mbar.edit add command -label [mc "Options..."] \
2032                -command do_options
2033}
2034
2035# -- Help Menu
2036#
2037.mbar add cascade -label [mc Help] -menu .mbar.help
2038menu .mbar.help
2039
2040if {![is_MacOSX]} {
2041        .mbar.help add command -label [mc "About %s" [appname]] \
2042                -command do_about
2043}
2044
2045set browser {}
2046catch {set browser $repo_config(instaweb.browser)}
2047set doc_path [file dirname [gitexec]]
2048set doc_path [file join $doc_path Documentation index.html]
2049
2050if {[is_Cygwin]} {
2051        set doc_path [exec cygpath --mixed $doc_path]
2052}
2053
2054if {$browser eq {}} {
2055        if {[is_MacOSX]} {
2056                set browser open
2057        } elseif {[is_Cygwin]} {
2058                set program_files [file dirname [exec cygpath --windir]]
2059                set program_files [file join $program_files {Program Files}]
2060                set firefox [file join $program_files {Mozilla Firefox} firefox.exe]
2061                set ie [file join $program_files {Internet Explorer} IEXPLORE.EXE]
2062                if {[file exists $firefox]} {
2063                        set browser $firefox
2064                } elseif {[file exists $ie]} {
2065                        set browser $ie
2066                }
2067                unset program_files firefox ie
2068        }
2069}
2070
2071if {[file isfile $doc_path]} {
2072        set doc_url "file:$doc_path"
2073} else {
2074        set doc_url {http://www.kernel.org/pub/software/scm/git/docs/}
2075}
2076
2077if {$browser ne {}} {
2078        .mbar.help add command -label [mc "Online Documentation"] \
2079                -command [list exec $browser $doc_url &]
2080}
2081unset browser doc_path doc_url
2082
2083# -- Standard bindings
2084#
2085wm protocol . WM_DELETE_WINDOW do_quit
2086bind all <$M1B-Key-q> do_quit
2087bind all <$M1B-Key-Q> do_quit
2088bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2089bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
2090
2091set subcommand_args {}
2092proc usage {} {
2093        puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
2094        exit 1
2095}
2096
2097# -- Not a normal commit type invocation?  Do that instead!
2098#
2099switch -- $subcommand {
2100browser -
2101blame {
2102        set subcommand_args {rev? path}
2103        if {$argv eq {}} usage
2104        set head {}
2105        set path {}
2106        set is_path 0
2107        foreach a $argv {
2108                if {$is_path || [file exists $_prefix$a]} {
2109                        if {$path ne {}} usage
2110                        set path $_prefix$a
2111                        break
2112                } elseif {$a eq {--}} {
2113                        if {$path ne {}} {
2114                                if {$head ne {}} usage
2115                                set head $path
2116                                set path {}
2117                        }
2118                        set is_path 1
2119                } elseif {$head eq {}} {
2120                        if {$head ne {}} usage
2121                        set head $a
2122                        set is_path 1
2123                } else {
2124                        usage
2125                }
2126        }
2127        unset is_path
2128
2129        if {$head ne {} && $path eq {}} {
2130                set path $_prefix$head
2131                set head {}
2132        }
2133
2134        if {$head eq {}} {
2135                load_current_branch
2136        } else {
2137                if {[regexp {^[0-9a-f]{1,39}$} $head]} {
2138                        if {[catch {
2139                                        set head [git rev-parse --verify $head]
2140                                } err]} {
2141                                puts stderr $err
2142                                exit 1
2143                        }
2144                }
2145                set current_branch $head
2146        }
2147
2148        switch -- $subcommand {
2149        browser {
2150                if {$head eq {}} {
2151                        if {$path ne {} && [file isdirectory $path]} {
2152                                set head $current_branch
2153                        } else {
2154                                set head $path
2155                                set path {}
2156                        }
2157                }
2158                browser::new $head $path
2159        }
2160        blame   {
2161                if {$head eq {} && ![file exists $path]} {
2162                        puts stderr [mc "fatal: cannot stat path %s: No such file or directory" $path]
2163                        exit 1
2164                }
2165                blame::new $head $path
2166        }
2167        }
2168        return
2169}
2170citool -
2171gui {
2172        if {[llength $argv] != 0} {
2173                puts -nonewline stderr "usage: $argv0"
2174                if {$subcommand ne {gui}
2175                        && [file tail $argv0] ne "git-$subcommand"} {
2176                        puts -nonewline stderr " $subcommand"
2177                }
2178                puts stderr {}
2179                exit 1
2180        }
2181        # fall through to setup UI for commits
2182}
2183default {
2184        puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
2185        exit 1
2186}
2187}
2188
2189# -- Branch Control
2190#
2191frame .branch \
2192        -borderwidth 1 \
2193        -relief sunken
2194label .branch.l1 \
2195        -text [mc "Current Branch:"] \
2196        -anchor w \
2197        -justify left
2198label .branch.cb \
2199        -textvariable current_branch \
2200        -anchor w \
2201        -justify left
2202pack .branch.l1 -side left
2203pack .branch.cb -side left -fill x
2204pack .branch -side top -fill x
2205
2206# -- Main Window Layout
2207#
2208panedwindow .vpane -orient horizontal
2209panedwindow .vpane.files -orient vertical
2210.vpane add .vpane.files -sticky nsew -height 100 -width 200
2211pack .vpane -anchor n -side top -fill both -expand 1
2212
2213# -- Index File List
2214#
2215frame .vpane.files.index -height 100 -width 200
2216label .vpane.files.index.title -text [mc "Staged Changes (Will Commit)"] \
2217        -background lightgreen
2218text $ui_index -background white -borderwidth 0 \
2219        -width 20 -height 10 \
2220        -wrap none \
2221        -cursor $cursor_ptr \
2222        -xscrollcommand {.vpane.files.index.sx set} \
2223        -yscrollcommand {.vpane.files.index.sy set} \
2224        -state disabled
2225scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
2226scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
2227pack .vpane.files.index.title -side top -fill x
2228pack .vpane.files.index.sx -side bottom -fill x
2229pack .vpane.files.index.sy -side right -fill y
2230pack $ui_index -side left -fill both -expand 1
2231
2232# -- Working Directory File List
2233#
2234frame .vpane.files.workdir -height 100 -width 200
2235label .vpane.files.workdir.title -text [mc "Unstaged Changes"] \
2236        -background lightsalmon
2237text $ui_workdir -background white -borderwidth 0 \
2238        -width 20 -height 10 \
2239        -wrap none \
2240        -cursor $cursor_ptr \
2241        -xscrollcommand {.vpane.files.workdir.sx set} \
2242        -yscrollcommand {.vpane.files.workdir.sy set} \
2243        -state disabled
2244scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
2245scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
2246pack .vpane.files.workdir.title -side top -fill x
2247pack .vpane.files.workdir.sx -side bottom -fill x
2248pack .vpane.files.workdir.sy -side right -fill y
2249pack $ui_workdir -side left -fill both -expand 1
2250
2251.vpane.files add .vpane.files.workdir -sticky nsew
2252.vpane.files add .vpane.files.index -sticky nsew
2253
2254foreach i [list $ui_index $ui_workdir] {
2255        rmsel_tag $i
2256        $i tag conf in_diff -background [$i tag cget in_sel -background]
2257}
2258unset i
2259
2260# -- Diff and Commit Area
2261#
2262frame .vpane.lower -height 300 -width 400
2263frame .vpane.lower.commarea
2264frame .vpane.lower.diff -relief sunken -borderwidth 1
2265pack .vpane.lower.diff -fill both -expand 1
2266pack .vpane.lower.commarea -side bottom -fill x
2267.vpane add .vpane.lower -sticky nsew
2268
2269# -- Commit Area Buttons
2270#
2271frame .vpane.lower.commarea.buttons
2272label .vpane.lower.commarea.buttons.l -text {} \
2273        -anchor w \
2274        -justify left
2275pack .vpane.lower.commarea.buttons.l -side top -fill x
2276pack .vpane.lower.commarea.buttons -side left -fill y
2277
2278button .vpane.lower.commarea.buttons.rescan -text [mc Rescan] \
2279        -command do_rescan
2280pack .vpane.lower.commarea.buttons.rescan -side top -fill x
2281lappend disable_on_lock \
2282        {.vpane.lower.commarea.buttons.rescan conf -state}
2283
2284button .vpane.lower.commarea.buttons.incall -text [mc "Stage Changed"] \
2285        -command do_add_all
2286pack .vpane.lower.commarea.buttons.incall -side top -fill x
2287lappend disable_on_lock \
2288        {.vpane.lower.commarea.buttons.incall conf -state}
2289
2290button .vpane.lower.commarea.buttons.signoff -text [mc "Sign Off"] \
2291        -command do_signoff
2292pack .vpane.lower.commarea.buttons.signoff -side top -fill x
2293
2294button .vpane.lower.commarea.buttons.commit -text [mc Commit@@verb] \
2295        -command do_commit
2296pack .vpane.lower.commarea.buttons.commit -side top -fill x
2297lappend disable_on_lock \
2298        {.vpane.lower.commarea.buttons.commit conf -state}
2299
2300button .vpane.lower.commarea.buttons.push -text [mc Push] \
2301        -command do_push_anywhere
2302pack .vpane.lower.commarea.buttons.push -side top -fill x
2303
2304# -- Commit Message Buffer
2305#
2306frame .vpane.lower.commarea.buffer
2307frame .vpane.lower.commarea.buffer.header
2308set ui_comm .vpane.lower.commarea.buffer.t
2309set ui_coml .vpane.lower.commarea.buffer.header.l
2310radiobutton .vpane.lower.commarea.buffer.header.new \
2311        -text [mc "New Commit"] \
2312        -command do_select_commit_type \
2313        -variable selected_commit_type \
2314        -value new
2315lappend disable_on_lock \
2316        [list .vpane.lower.commarea.buffer.header.new conf -state]
2317radiobutton .vpane.lower.commarea.buffer.header.amend \
2318        -text [mc "Amend Last Commit"] \
2319        -command do_select_commit_type \
2320        -variable selected_commit_type \
2321        -value amend
2322lappend disable_on_lock \
2323        [list .vpane.lower.commarea.buffer.header.amend conf -state]
2324label $ui_coml \
2325        -anchor w \
2326        -justify left
2327proc trace_commit_type {varname args} {
2328        global ui_coml commit_type
2329        switch -glob -- $commit_type {
2330        initial       {set txt [mc "Initial Commit Message:"]}
2331        amend         {set txt [mc "Amended Commit Message:"]}
2332        amend-initial {set txt [mc "Amended Initial Commit Message:"]}
2333        amend-merge   {set txt [mc "Amended Merge Commit Message:"]}
2334        merge         {set txt [mc "Merge Commit Message:"]}
2335        *             {set txt [mc "Commit Message:"]}
2336        }
2337        $ui_coml conf -text $txt
2338}
2339trace add variable commit_type write trace_commit_type
2340pack $ui_coml -side left -fill x
2341pack .vpane.lower.commarea.buffer.header.amend -side right
2342pack .vpane.lower.commarea.buffer.header.new -side right
2343
2344text $ui_comm -background white -borderwidth 1 \
2345        -undo true \
2346        -maxundo 20 \
2347        -autoseparators true \
2348        -relief sunken \
2349        -width 75 -height 9 -wrap none \
2350        -font font_diff \
2351        -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
2352scrollbar .vpane.lower.commarea.buffer.sby \
2353        -command [list $ui_comm yview]
2354pack .vpane.lower.commarea.buffer.header -side top -fill x
2355pack .vpane.lower.commarea.buffer.sby -side right -fill y
2356pack $ui_comm -side left -fill y
2357pack .vpane.lower.commarea.buffer -side left -fill y
2358
2359# -- Commit Message Buffer Context Menu
2360#
2361set ctxm .vpane.lower.commarea.buffer.ctxm
2362menu $ctxm -tearoff 0
2363$ctxm add command \
2364        -label [mc Cut] \
2365        -command {tk_textCut $ui_comm}
2366$ctxm add command \
2367        -label [mc Copy] \
2368        -command {tk_textCopy $ui_comm}
2369$ctxm add command \
2370        -label [mc Paste] \
2371        -command {tk_textPaste $ui_comm}
2372$ctxm add command \
2373        -label [mc Delete] \
2374        -command {$ui_comm delete sel.first sel.last}
2375$ctxm add separator
2376$ctxm add command \
2377        -label [mc "Select All"] \
2378        -command {focus $ui_comm;$ui_comm tag add sel 0.0 end}
2379$ctxm add command \
2380        -label [mc "Copy All"] \
2381        -command {
2382                $ui_comm tag add sel 0.0 end
2383                tk_textCopy $ui_comm
2384                $ui_comm tag remove sel 0.0 end
2385        }
2386$ctxm add separator
2387$ctxm add command \
2388        -label [mc "Sign Off"] \
2389        -command do_signoff
2390bind_button3 $ui_comm "tk_popup $ctxm %X %Y"
2391
2392# -- Diff Header
2393#
2394proc trace_current_diff_path {varname args} {
2395        global current_diff_path diff_actions file_states
2396        if {$current_diff_path eq {}} {
2397                set s {}
2398                set f {}
2399                set p {}
2400                set o disabled
2401        } else {
2402                set p $current_diff_path
2403                set s [mapdesc [lindex $file_states($p) 0] $p]
2404                set f [mc "File:"]
2405                set p [escape_path $p]
2406                set o normal
2407        }
2408
2409        .vpane.lower.diff.header.status configure -text $s
2410        .vpane.lower.diff.header.file configure -text $f
2411        .vpane.lower.diff.header.path configure -text $p
2412        foreach w $diff_actions {
2413                uplevel #0 $w $o
2414        }
2415}
2416trace add variable current_diff_path write trace_current_diff_path
2417
2418frame .vpane.lower.diff.header -background gold
2419label .vpane.lower.diff.header.status \
2420        -background gold \
2421        -width $max_status_desc \
2422        -anchor w \
2423        -justify left
2424label .vpane.lower.diff.header.file \
2425        -background gold \
2426        -anchor w \
2427        -justify left
2428label .vpane.lower.diff.header.path \
2429        -background gold \
2430        -anchor w \
2431        -justify left
2432pack .vpane.lower.diff.header.status -side left
2433pack .vpane.lower.diff.header.file -side left
2434pack .vpane.lower.diff.header.path -fill x
2435set ctxm .vpane.lower.diff.header.ctxm
2436menu $ctxm -tearoff 0
2437$ctxm add command \
2438        -label [mc Copy] \
2439        -command {
2440                clipboard clear
2441                clipboard append \
2442                        -format STRING \
2443                        -type STRING \
2444                        -- $current_diff_path
2445        }
2446lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2447bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
2448
2449# -- Diff Body
2450#
2451frame .vpane.lower.diff.body
2452set ui_diff .vpane.lower.diff.body.t
2453text $ui_diff -background white -borderwidth 0 \
2454        -width 80 -height 15 -wrap none \
2455        -font font_diff \
2456        -xscrollcommand {.vpane.lower.diff.body.sbx set} \
2457        -yscrollcommand {.vpane.lower.diff.body.sby set} \
2458        -state disabled
2459scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
2460        -command [list $ui_diff xview]
2461scrollbar .vpane.lower.diff.body.sby -orient vertical \
2462        -command [list $ui_diff yview]
2463pack .vpane.lower.diff.body.sbx -side bottom -fill x
2464pack .vpane.lower.diff.body.sby -side right -fill y
2465pack $ui_diff -side left -fill both -expand 1
2466pack .vpane.lower.diff.header -side top -fill x
2467pack .vpane.lower.diff.body -side bottom -fill both -expand 1
2468
2469$ui_diff tag conf d_cr -elide true
2470$ui_diff tag conf d_@ -foreground blue -font font_diffbold
2471$ui_diff tag conf d_+ -foreground {#00a000}
2472$ui_diff tag conf d_- -foreground red
2473
2474$ui_diff tag conf d_++ -foreground {#00a000}
2475$ui_diff tag conf d_-- -foreground red
2476$ui_diff tag conf d_+s \
2477        -foreground {#00a000} \
2478        -background {#e2effa}
2479$ui_diff tag conf d_-s \
2480        -foreground red \
2481        -background {#e2effa}
2482$ui_diff tag conf d_s+ \
2483        -foreground {#00a000} \
2484        -background ivory1
2485$ui_diff tag conf d_s- \
2486        -foreground red \
2487        -background ivory1
2488
2489$ui_diff tag conf d<<<<<<< \
2490        -foreground orange \
2491        -font font_diffbold
2492$ui_diff tag conf d======= \
2493        -foreground orange \
2494        -font font_diffbold
2495$ui_diff tag conf d>>>>>>> \
2496        -foreground orange \
2497        -font font_diffbold
2498
2499$ui_diff tag raise sel
2500
2501# -- Diff Body Context Menu
2502#
2503set ctxm .vpane.lower.diff.body.ctxm
2504menu $ctxm -tearoff 0
2505$ctxm add command \
2506        -label [mc Refresh] \
2507        -command reshow_diff
2508lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2509$ctxm add command \
2510        -label [mc Copy] \
2511        -command {tk_textCopy $ui_diff}
2512lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2513$ctxm add command \
2514        -label [mc "Select All"] \
2515        -command {focus $ui_diff;$ui_diff tag add sel 0.0 end}
2516lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2517$ctxm add command \
2518        -label [mc "Copy All"] \
2519        -command {
2520                $ui_diff tag add sel 0.0 end
2521                tk_textCopy $ui_diff
2522                $ui_diff tag remove sel 0.0 end
2523        }
2524lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2525$ctxm add separator
2526$ctxm add command \
2527        -label [mc "Apply/Reverse Hunk"] \
2528        -command {apply_hunk $cursorX $cursorY}
2529set ui_diff_applyhunk [$ctxm index last]
2530lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
2531$ctxm add separator
2532$ctxm add command \
2533        -label [mc "Decrease Font Size"] \
2534        -command {incr_font_size font_diff -1}
2535lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2536$ctxm add command \
2537        -label [mc "Increase Font Size"] \
2538        -command {incr_font_size font_diff 1}
2539lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2540$ctxm add separator
2541$ctxm add command \
2542        -label [mc "Show Less Context"] \
2543        -command {if {$repo_config(gui.diffcontext) >= 1} {
2544                incr repo_config(gui.diffcontext) -1
2545                reshow_diff
2546        }}
2547lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2548$ctxm add command \
2549        -label [mc "Show More Context"] \
2550        -command {if {$repo_config(gui.diffcontext) < 99} {
2551                incr repo_config(gui.diffcontext)
2552                reshow_diff
2553        }}
2554lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state]
2555$ctxm add separator
2556$ctxm add command -label [mc "Options..."] \
2557        -command do_options
2558proc popup_diff_menu {ctxm x y X Y} {
2559        global current_diff_path file_states
2560        set ::cursorX $x
2561        set ::cursorY $y
2562        if {$::ui_index eq $::current_diff_side} {
2563                set l [mc "Unstage Hunk From Commit"]
2564        } else {
2565                set l [mc "Stage Hunk For Commit"]
2566        }
2567        if {$::is_3way_diff
2568                || $current_diff_path eq {}
2569                || ![info exists file_states($current_diff_path)]
2570                || {_O} eq [lindex $file_states($current_diff_path) 0]} {
2571                set s disabled
2572        } else {
2573                set s normal
2574        }
2575        $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
2576        tk_popup $ctxm $X $Y
2577}
2578bind_button3 $ui_diff [list popup_diff_menu $ctxm %x %y %X %Y]
2579
2580# -- Status Bar
2581#
2582set main_status [::status_bar::new .status]
2583pack .status -anchor w -side bottom -fill x
2584$main_status show [mc "Initializing..."]
2585
2586# -- Load geometry
2587#
2588catch {
2589set gm $repo_config(gui.geometry)
2590wm geometry . [lindex $gm 0]
2591.vpane sash place 0 \
2592        [lindex $gm 1] \
2593        [lindex [.vpane sash coord 0] 1]
2594.vpane.files sash place 0 \
2595        [lindex [.vpane.files sash coord 0] 0] \
2596        [lindex $gm 2]
2597unset gm
2598}
2599
2600# -- Key Bindings
2601#
2602bind $ui_comm <$M1B-Key-Return> {do_commit;break}
2603bind $ui_comm <$M1B-Key-i> {do_add_all;break}
2604bind $ui_comm <$M1B-Key-I> {do_add_all;break}
2605bind $ui_comm <$M1B-Key-x> {tk_textCut %W;break}
2606bind $ui_comm <$M1B-Key-X> {tk_textCut %W;break}
2607bind $ui_comm <$M1B-Key-c> {tk_textCopy %W;break}
2608bind $ui_comm <$M1B-Key-C> {tk_textCopy %W;break}
2609bind $ui_comm <$M1B-Key-v> {tk_textPaste %W; %W see insert; break}
2610bind $ui_comm <$M1B-Key-V> {tk_textPaste %W; %W see insert; break}
2611bind $ui_comm <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2612bind $ui_comm <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2613
2614bind $ui_diff <$M1B-Key-x> {tk_textCopy %W;break}
2615bind $ui_diff <$M1B-Key-X> {tk_textCopy %W;break}
2616bind $ui_diff <$M1B-Key-c> {tk_textCopy %W;break}
2617bind $ui_diff <$M1B-Key-C> {tk_textCopy %W;break}
2618bind $ui_diff <$M1B-Key-v> {break}
2619bind $ui_diff <$M1B-Key-V> {break}
2620bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
2621bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
2622bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
2623bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
2624bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
2625bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
2626bind $ui_diff <Key-k>         {catch {%W yview scroll -1 units};break}
2627bind $ui_diff <Key-j>         {catch {%W yview scroll  1 units};break}
2628bind $ui_diff <Key-h>         {catch {%W xview scroll -1 units};break}
2629bind $ui_diff <Key-l>         {catch {%W xview scroll  1 units};break}
2630bind $ui_diff <Control-Key-b> {catch {%W yview scroll -1 pages};break}
2631bind $ui_diff <Control-Key-f> {catch {%W yview scroll  1 pages};break}
2632bind $ui_diff <Button-1>   {focus %W}
2633
2634if {[is_enabled branch]} {
2635        bind . <$M1B-Key-n> branch_create::dialog
2636        bind . <$M1B-Key-N> branch_create::dialog
2637        bind . <$M1B-Key-o> branch_checkout::dialog
2638        bind . <$M1B-Key-O> branch_checkout::dialog
2639        bind . <$M1B-Key-m> merge::dialog
2640        bind . <$M1B-Key-M> merge::dialog
2641}
2642if {[is_enabled transport]} {
2643        bind . <$M1B-Key-p> do_push_anywhere
2644        bind . <$M1B-Key-P> do_push_anywhere
2645}
2646
2647bind .   <Key-F5>     do_rescan
2648bind .   <$M1B-Key-r> do_rescan
2649bind .   <$M1B-Key-R> do_rescan
2650bind .   <$M1B-Key-s> do_signoff
2651bind .   <$M1B-Key-S> do_signoff
2652bind .   <$M1B-Key-i> do_add_all
2653bind .   <$M1B-Key-I> do_add_all
2654bind .   <$M1B-Key-Return> do_commit
2655foreach i [list $ui_index $ui_workdir] {
2656        bind $i <Button-1>       "toggle_or_diff         $i %x %y; break"
2657        bind $i <$M1B-Button-1>  "add_one_to_selection   $i %x %y; break"
2658        bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
2659}
2660unset i
2661
2662set file_lists($ui_index) [list]
2663set file_lists($ui_workdir) [list]
2664
2665wm title . "[appname] ([reponame]) [file normalize [file dirname [gitdir]]]"
2666focus -force $ui_comm
2667
2668# -- Warn the user about environmental problems.  Cygwin's Tcl
2669#    does *not* pass its env array onto any processes it spawns.
2670#    This means that git processes get none of our environment.
2671#
2672if {[is_Cygwin]} {
2673        set ignored_env 0
2674        set suggest_user {}
2675        set msg [mc "Possible environment issues exist.
2676
2677The following environment variables are probably
2678going to be ignored by any Git subprocess run
2679by %s:
2680
2681" [appname]]
2682        foreach name [array names env] {
2683                switch -regexp -- $name {
2684                {^GIT_INDEX_FILE$} -
2685                {^GIT_OBJECT_DIRECTORY$} -
2686                {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
2687                {^GIT_DIFF_OPTS$} -
2688                {^GIT_EXTERNAL_DIFF$} -
2689                {^GIT_PAGER$} -
2690                {^GIT_TRACE$} -
2691                {^GIT_CONFIG$} -
2692                {^GIT_CONFIG_LOCAL$} -
2693                {^GIT_(AUTHOR|COMMITTER)_DATE$} {
2694                        append msg " - $name\n"
2695                        incr ignored_env
2696                }
2697                {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
2698                        append msg " - $name\n"
2699                        incr ignored_env
2700                        set suggest_user $name
2701                }
2702                }
2703        }
2704        if {$ignored_env > 0} {
2705                append msg [mc "
2706This is due to a known issue with the
2707Tcl binary distributed by Cygwin."]
2708
2709                if {$suggest_user ne {}} {
2710                        append msg [mc "
2711
2712A good replacement for %s
2713is placing values for the user.name and
2714user.email settings into your personal
2715~/.gitconfig file.
2716" $suggest_user]
2717                }
2718                warn_popup $msg
2719        }
2720        unset ignored_env msg suggest_user name
2721}
2722
2723# -- Only initialize complex UI if we are going to stay running.
2724#
2725if {[is_enabled transport]} {
2726        load_all_remotes
2727
2728        set n [.mbar.remote index end]
2729        populate_push_menu
2730        populate_fetch_menu
2731        set n [expr {[.mbar.remote index end] - $n}]
2732        if {$n > 0} {
2733                .mbar.remote insert $n separator
2734        }
2735        unset n
2736}
2737
2738if {[winfo exists $ui_comm]} {
2739        set GITGUI_BCK_exists [load_message GITGUI_BCK]
2740
2741        # -- If both our backup and message files exist use the
2742        #    newer of the two files to initialize the buffer.
2743        #
2744        if {$GITGUI_BCK_exists} {
2745                set m [gitdir GITGUI_MSG]
2746                if {[file isfile $m]} {
2747                        if {[file mtime [gitdir GITGUI_BCK]] > [file mtime $m]} {
2748                                catch {file delete [gitdir GITGUI_MSG]}
2749                        } else {
2750                                $ui_comm delete 0.0 end
2751                                $ui_comm edit reset
2752                                $ui_comm edit modified false
2753                                catch {file delete [gitdir GITGUI_BCK]}
2754                                set GITGUI_BCK_exists 0
2755                        }
2756                }
2757                unset m
2758        }
2759
2760        proc backup_commit_buffer {} {
2761                global ui_comm GITGUI_BCK_exists
2762
2763                set m [$ui_comm edit modified]
2764                if {$m || $GITGUI_BCK_exists} {
2765                        set msg [string trim [$ui_comm get 0.0 end]]
2766                        regsub -all -line {[ \r\t]+$} $msg {} msg
2767
2768                        if {$msg eq {}} {
2769                                if {$GITGUI_BCK_exists} {
2770                                        catch {file delete [gitdir GITGUI_BCK]}
2771                                        set GITGUI_BCK_exists 0
2772                                }
2773                        } elseif {$m} {
2774                                catch {
2775                                        set fd [open [gitdir GITGUI_BCK] w]
2776                                        puts -nonewline $fd $msg
2777                                        close $fd
2778                                        set GITGUI_BCK_exists 1
2779                                }
2780                        }
2781
2782                        $ui_comm edit modified false
2783                }
2784
2785                set ::GITGUI_BCK_i [after 2000 backup_commit_buffer]
2786        }
2787
2788        backup_commit_buffer
2789}
2790
2791lock_index begin-read
2792if {![winfo ismapped .]} {
2793        wm deiconify .
2794}
2795after 1 do_rescan
2796if {[is_enabled multicommit]} {
2797        after 1000 hint_gc
2798}