git-gui: Correctly handle CR vs. LF within the console of fetch.
[gitweb.git] / git-gui
diff --git a/git-gui b/git-gui
index bb89da43320ed53b819fa9f973c6cdaaf8783128..2645cd5d1a95ba3c40bf736810404aa34dd8d218 100755 (executable)
--- a/git-gui
+++ b/git-gui
@@ -92,7 +92,7 @@ proc update_status {{final Ready.}} {
        }
 
        if {![$ui_comm edit modified]
-           || [string trim [$ui_comm get 0.0 end]] == {}} {
+               || [string trim [$ui_comm get 0.0 end]] == {}} {
                if {[load_message GITGUI_MSG]} {
                } elseif {[load_message MERGE_MSG]} {
                } elseif {[load_message SQUASH_MSG]} {
@@ -266,6 +266,9 @@ proc show_diff {path} {
                $ui_diff conf -state normal
                $ui_diff insert end $content
                $ui_diff conf -state disabled
+               set diff_active 0
+               unlock_index
+               set ui_status_value {Ready.}
                return
        }
        }
@@ -482,8 +485,7 @@ A good commit message has the following format:
        set ui_status_value {Committing changes...}
 
        set fd_wt [open "| git write-tree" r]
-       fileevent $fd_wt readable \
-               [list commit_stage2 $fd_wt $curHEAD $msg]
+       fileevent $fd_wt readable [list commit_stage2 $fd_wt $curHEAD $msg]
 }
 
 proc commit_stage2 {fd_wt curHEAD msg} {
@@ -510,8 +512,8 @@ proc commit_stage2 {fd_wt curHEAD msg} {
        if {$commit_type == {merge}} {
                if {[catch {
                                set fd_mh [open [file join $gitdir MERGE_HEAD] r]
-                               while {[gets $fd_mh merge_head] > 0} {
-                                       lappend -p $merge_head
+                               while {[gets $fd_mh merge_head] >= 0} {
+                                       lappend cmd -p $merge_head
                                }
                                close $fd_mh
                        } err]} {
@@ -576,10 +578,34 @@ proc commit_stage2 {fd_wt curHEAD msg} {
 
        set commit_type {}
        set commit_active 0
+       set HEAD $cmt_id
+       set PARENT $cmt_id
        unlock_index
        update_status "Changes committed as $cmt_id."
 }
 
+######################################################################
+##
+## fetch pull push
+
+proc fetch_from {remote} {
+       set w [new_console "fetch $remote" \
+               "Fetching new changes from $remote"]
+       set cmd [list git fetch]
+       lappend cmd -v
+       lappend cmd $remote
+       console_exec $w $cmd
+}
+
+proc push_to {remote} {
+       set w [new_console "push $remote" \
+               "Pushing changes to $remote"]
+       set cmd [list git push]
+       lappend -v
+       lappend cmd $remote
+       console_exec $w $cmd
+}
+
 ######################################################################
 ##
 ## ui helpers
@@ -739,7 +765,7 @@ proc update_index {path} {
 }
 
 proc toggle_mode {path} {
-       global file_states
+       global file_states ui_fname_value
 
        set s $file_states($path)
        set m [lindex $s 0]
@@ -749,12 +775,51 @@ proc toggle_mode {path} {
        _O {set new A*}
        _M -
        MM {set new M*}
+       AD -
        _D {set new D*}
        default {return}
        }
 
        with_update_index {update_index $path}
        display_file $path $new
+       if {$ui_fname_value == $path} {
+               show_diff $path
+       }
+}
+
+######################################################################
+##
+## config (fetch push pull)
+
+proc load_all_remotes {} {
+       global gitdir all_remotes
+
+       set all_remotes [list]
+       set rm_dir [file join $gitdir remotes]
+       if {[file isdirectory $rm_dir]} {
+               set all_remotes [concat $all_remotes \
+                       [glob -types f -tails -directory $rm_dir * *]]
+       }
+
+       set fd_rc [open "| git repo-config --list" r]
+       while {[gets $fd_rc line] >= 0} {
+               if {[regexp ^remote\.(.*)\.url= $line line name]} {
+                       lappend all_remotes $name
+               }
+       }
+       close $fd_rc
+
+       set all_remotes [lsort -unique $all_remotes]
+}
+
+proc populate_remote_menu {m pfx op} {
+       global gitdir all_remotes mainfont
+
+       foreach remote $all_remotes {
+               $m add command -label "$pfx $remote..." \
+                       -command [list $op $remote] \
+                       -font $mainfont
+       }
 }
 
 ######################################################################
@@ -843,6 +908,7 @@ foreach i {
                {_O o plain    "Untracked"}
                {A_ o fulltick "Added"}
                {AM o parttick "Partially added"}
+               {AD o question  "Added (but now gone)"}
 
                {_D i question "Missing"}
                {D_ i removed  "Removed"}
@@ -873,12 +939,13 @@ proc error_popup {msg} {
 }
 
 proc show_msg {w top msg} {
-       global gitdir appname
+       global gitdir appname mainfont
 
        message $w.m -text $msg -justify left -aspect 400
        pack $w.m -side top -fill x -padx 5 -pady 10
        button $w.ok -text OK \
                -width 15 \
+               -font $mainfont \
                -command "destroy $top"
        pack $w.ok -side bottom
        bind $top <Visibility> "grab $top; focus $top"
@@ -922,6 +989,7 @@ proc hook_failed_popup {hook msg} {
 
        button $w.ok -text OK \
                -width 15 \
+               -font $mainfont \
                -command "destroy $w"
        pack $w.ok -side bottom
 
@@ -931,11 +999,109 @@ proc hook_failed_popup {hook msg} {
        tkwait window $w
 }
 
+set next_console_id 0
+
+proc new_console {short_title long_title} {
+       global next_console_id console_cr
+       global gitdir appname mainfont difffont
+
+       set w .console[incr next_console_id]
+       set console_cr($w) 1.0
+       toplevel $w
+       frame $w.m
+       label $w.m.l1 -text "$long_title:" \
+               -anchor w \
+               -justify left \
+               -font [concat $mainfont bold]
+       text $w.m.t \
+               -background white -borderwidth 1 \
+               -relief sunken \
+               -width 80 -height 10 \
+               -font $difffont \
+               -state disabled \
+               -yscrollcommand [list $w.m.sby set]
+       scrollbar $w.m.sby -command [list $w.m.t yview]
+       pack $w.m.l1 -side top -fill x
+       pack $w.m.sby -side right -fill y
+       pack $w.m.t -side left -fill both -expand 1
+       pack $w.m -side top -fill both -expand 1 -padx 5 -pady 10
+
+       button $w.ok -text {Running...} \
+               -width 15 \
+               -font $mainfont \
+               -state disabled \
+               -command "destroy $w"
+       pack $w.ok -side bottom
+
+       bind $w <Visibility> "focus $w"
+       bind $w <Destroy> break
+       wm title $w "$appname ([file dirname [file normalize [file dirname $gitdir]]]): $short_title"
+       return $w
+}
+
+proc console_exec {w cmd} {
+       global tcl_platform
+
+       # -- Windows tosses the enviroment when we exec our child.
+       #    But most users need that so we have to relogin. :-(
+       #
+       if {$tcl_platform(platform) == {windows}} {
+               set cmd [list sh --login -c "cd \"[pwd]\" && [join $cmd { }]"]
+       }
+
+       # -- Tcl won't let us redirect both stdout and stderr to
+       #    the same pipe.  So pass it through cat...
+       #
+       set cmd [concat | $cmd |& cat]
+
+       set fd_f [open $cmd r]
+       fconfigure $fd_f -blocking 0 -translation binary
+       fileevent $fd_f readable [list console_read $w $fd_f]
+}
+
+proc console_read {w fd} {
+       global console_cr
+
+       $w.m.t conf -state normal
+       set buf [read $fd]
+       set c 0
+       set n [string length $buf]
+       while {$c < $n} {
+               set cr [string first "\r" $buf $c]
+               set lf [string first "\n" $buf $c]
+               if {$cr < 0} {set cr [expr $n + 1]}
+               if {$lf < 0} {set lf [expr $n + 1]}
+
+               if {$lf < $cr} {
+                       $w.m.t insert end [string range $buf $c $lf]
+                       set console_cr($w) [$w.m.t index {end -1c}]
+                       set c $lf
+                       incr c
+               } else {
+                       $w.m.t delete $console_cr($w) end
+                       $w.m.t insert end "\n"
+                       $w.m.t insert end [string range $buf $c $cr]
+                       set c $cr
+                       incr c
+               }
+       }
+       $w.m.t conf -state disabled
+       $w.m.t see end
+
+       if {[eof $fd]} {
+               close $fd
+               $w.ok conf -text Close
+               $w.ok conf -state normal
+               array unset console_cr $w
+       }
+}
+
 ######################################################################
 ##
 ## ui commands
 
 set starting_gitk_msg {Please wait... Starting gitk...}
+
 proc do_gitk {} {
        global tcl_platform ui_status_value starting_gitk_msg
 
@@ -946,7 +1112,7 @@ proc do_gitk {} {
                }
        }
 
-    if {$tcl_platform(platform) == "windows"} {
+       if {$tcl_platform(platform) == {windows}} {
                exec sh -c gitk &
        } else {
                exec gitk &
@@ -1063,9 +1229,10 @@ set mainfont {Helvetica 10}
 set difffont {Courier 10}
 set maincursor [. cget -cursor]
 
-switch -- $tcl_platform(platform) {
-windows {set M1B Control; set M1T Ctrl}
-default {set M1B M1; set M1T M1}
+switch -glob -- "$tcl_platform(platform),$tcl_platform(os)" {
+windows,*   {set M1B Control; set M1T Ctrl}
+unix,Darwin {set M1B M1; set M1T Cmd}
+default     {set M1B M1; set M1T M1}
 }
 
 # -- Menu Bar
@@ -1074,6 +1241,7 @@ menu .mbar -tearoff 0
 .mbar add cascade -label Commit -menu .mbar.commit
 .mbar add cascade -label Fetch -menu .mbar.fetch
 .mbar add cascade -label Pull -menu .mbar.pull
+.mbar add cascade -label Push -menu .mbar.push
 . configure -menu .mbar
 
 # -- Project Menu
@@ -1122,6 +1290,9 @@ menu .mbar.fetch
 # -- Pull Menu
 menu .mbar.pull
 
+# -- Push Menu
+menu .mbar.push
+
 # -- Main Window Layout
 panedwindow .vpane -orient vertical
 panedwindow .vpane.files -orient horizontal
@@ -1261,10 +1432,18 @@ lappend disable_on_lock {.vpane.commarea.buttons.commit conf -state}
 # -- Commit Message Buffer
 frame .vpane.commarea.buffer
 set ui_comm .vpane.commarea.buffer.t
-label .vpane.commarea.buffer.l -text {Commit Message:} \
+set ui_coml .vpane.commarea.buffer.l
+label $ui_coml -text {Commit Message:} \
        -anchor w \
        -justify left \
        -font $mainfont
+trace add variable commit_type write {uplevel #0 {
+       switch -glob $commit_type \
+       initial {$ui_coml conf -text {Initial Commit Message:}} \
+       amend   {$ui_coml conf -text {Amended Commit Message:}} \
+       merge   {$ui_coml conf -text {Merge Commit Message:}} \
+       *       {$ui_coml conf -text {Commit Message:}}
+}}
 text $ui_comm -background white -borderwidth 1 \
        -relief sunken \
        -width 75 -height 10 -wrap none \
@@ -1272,7 +1451,7 @@ text $ui_comm -background white -borderwidth 1 \
        -yscrollcommand {.vpane.commarea.buffer.sby set} \
        -cursor $maincursor
 scrollbar .vpane.commarea.buffer.sby -command [list $ui_comm yview]
-pack .vpane.commarea.buffer.l -side top -fill x
+pack $ui_coml -side top -fill x
 pack .vpane.commarea.buffer.sby -side right -fill y
 pack $ui_comm -side left -fill y
 pack .vpane.commarea.buffer -side left -fill y
@@ -1311,16 +1490,30 @@ unset i M1B M1T
 ##
 ## main
 
+set appname [lindex [file split $argv0] end]
+set gitdir {}
+
+if {[catch {set cdup [exec git rev-parse --show-cdup]} err]} {
+       show_msg {} . "Cannot find the git directory: $err"
+       exit 1
+}
+if {$cdup != ""} {
+       cd $cdup
+}
+unset cdup
+
 if {[catch {set gitdir [exec git rev-parse --git-dir]} err]} {
        show_msg {} . "Cannot find the git directory: $err"
        exit 1
 }
 
-set appname [lindex [file split $argv0] end]
 if {$appname == {git-citool}} {
        set single_commit 1
 }
 
 wm title . "$appname ([file normalize [file dirname $gitdir]])"
 focus -force $ui_comm
+load_all_remotes
+populate_remote_menu .mbar.fetch From fetch_from
+populate_remote_menu .mbar.push To push_to
 update_status