git-gui: Display the current branch.
[gitweb.git] / git-gui
diff --git a/git-gui b/git-gui
index 3e53fbd623f14446de6aabb79614ce34ef6e0cfe..dd2d750ab17f9c1831ffea70459d07726bfcfaec 100755 (executable)
--- a/git-gui
+++ b/git-gui
@@ -145,6 +145,28 @@ proc error_popup {msg} {
        eval $cmd
 }
 
+proc warn_popup {msg} {
+       global gitdir appname
+
+       set title $appname
+       if {$gitdir ne {}} {
+               append title { (}
+               append title [lindex \
+                       [file split [file normalize [file dirname $gitdir]]] \
+                       end]
+               append title {)}
+       }
+       set cmd [list tk_messageBox \
+               -icon warning \
+               -type ok \
+               -title "$title: warning" \
+               -message $msg]
+       if {[winfo ismapped .]} {
+               lappend cmd -parent .
+       }
+       eval $cmd
+}
+
 proc info_popup {msg} {
        global gitdir appname
 
@@ -158,7 +180,7 @@ proc info_popup {msg} {
        }
        tk_messageBox \
                -parent . \
-               -icon error \
+               -icon info \
                -type ok \
                -title $title \
                -message $msg
@@ -236,11 +258,20 @@ proc unlock_index {} {
 ## status
 
 proc repository_state {ctvar hdvar mhvar} {
-       global gitdir
+       global gitdir current_branch
        upvar $ctvar ct $hdvar hd $mhvar mh
 
        set mh [list]
 
+       if {[catch {set current_branch [exec git symbolic-ref HEAD]}]} {
+               set current_branch {}
+       } else {
+               regsub ^refs/(heads|tags)/ \
+                       $current_branch \
+                       {} \
+                       current_branch
+       }
+
        if {[catch {set hd [exec git rev-parse --verify HEAD]}]} {
                set hd {}
                set ct initial
@@ -1100,11 +1131,13 @@ proc pull_remote {remote branch} {
        #
        repository_state curType curHEAD curMERGE_HEAD
        if {$commit_type ne $curType || $HEAD ne $curHEAD} {
-               error_popup {Last scanned state does not match repository state.
+               info_popup {Last scanned state does not match repository state.
+
+Another Git program has modified this repository
+since the last scan.  A rescan must be performed
+before a pull operation can be started.
 
-Its highly likely that another Git program modified the
-repository since our last scan.  A rescan is required
-before a pull can be started.
+The rescan will be automatically started now.
 }
                unlock_index
                rescan {set ui_status_value {Ready.}}
@@ -1116,10 +1149,12 @@ before a pull can be started.
        if {[array size file_states] != 0} {
                error_popup {Uncommitted but modified files are present.
 
-You should not perform a pull with unmodified files in your working
-directory as Git would be unable to recover from an incorrect merge.
+You should not perform a pull with unmodified
+files in your working directory as Git will be
+unable to recover from an incorrect merge.
 
-Commit or throw away all changes before starting a pull operation.
+You should commit or revert all changes before
+starting a pull operation.
 }
                unlock_index
                return
@@ -1251,9 +1286,26 @@ proc display_file {path state} {
        set old_w [mapcol $old_m $path]
        set new_icon [mapicon $new_m $path]
 
+       if {$new_m eq {__}} {
+               set lno [lsearch -sorted $file_lists($old_w) $path]
+               if {$lno >= 0} {
+                       set file_lists($old_w) \
+                               [lreplace $file_lists($old_w) $lno $lno]
+                       incr lno
+                       $old_w conf -state normal
+                       $old_w delete $lno.0 [expr {$lno + 1}].0
+                       $old_w conf -state disabled
+               }
+               unset file_states($path)
+               catch {unset selected_paths($path)}
+               return
+       }
+
        if {$new_w ne $old_w} {
                set lno [lsearch -sorted $file_lists($old_w) $path]
                if {$lno >= 0} {
+                       set file_lists($old_w) \
+                               [lreplace $file_lists($old_w) $lno $lno]
                        incr lno
                        $old_w conf -state normal
                        $old_w delete $lno.0 [expr {$lno + 1}].0
@@ -1474,6 +1526,84 @@ proc write_update_index {fd pathList totalCnt batch msg after} {
                [expr {100.0 * $update_index_cp / $totalCnt}]]
 }
 
+proc checkout_index {msg pathList after} {
+       global update_index_cp ui_status_value
+
+       if {![lock_index update]} return
+
+       set update_index_cp 0
+       set pathList [lsort $pathList]
+       set totalCnt [llength $pathList]
+       set batch [expr {int($totalCnt * .01) + 1}]
+       if {$batch > 25} {set batch 25}
+
+       set ui_status_value [format \
+               "$msg... %i/%i files (%.2f%%)" \
+               $update_index_cp \
+               $totalCnt \
+               0.0]
+       set cmd [list git checkout-index]
+       lappend cmd --index
+       lappend cmd --quiet
+       lappend cmd --force
+       lappend cmd -z
+       lappend cmd --stdin
+       set fd [open "| $cmd " w]
+       fconfigure $fd \
+               -blocking 0 \
+               -buffering full \
+               -buffersize 512 \
+               -translation binary
+       fileevent $fd writable [list \
+               write_checkout_index \
+               $fd \
+               $pathList \
+               $totalCnt \
+               $batch \
+               $msg \
+               $after \
+               ]
+}
+
+proc write_checkout_index {fd pathList totalCnt batch msg after} {
+       global update_index_cp ui_status_value
+       global file_states current_diff
+
+       if {$update_index_cp >= $totalCnt} {
+               close $fd
+               unlock_index
+               uplevel #0 $after
+               return
+       }
+
+       for {set i $batch} \
+               {$update_index_cp < $totalCnt && $i > 0} \
+               {incr i -1} {
+               set path [lindex $pathList $update_index_cp]
+               incr update_index_cp
+
+               switch -glob -- [lindex $file_states($path) 0] {
+               AM -
+               AD {set new A_}
+               MM -
+               MD {set new M_}
+               _M -
+               _D {set new __}
+               ?? {continue}
+               }
+
+               puts -nonewline $fd $path
+               puts -nonewline $fd "\0"
+               display_file $path $new
+       }
+
+       set ui_status_value [format \
+               "$msg... %i/%i files (%.2f%%)" \
+               $update_index_cp \
+               $totalCnt \
+               [expr {100.0 * $update_index_cp / $totalCnt}]]
+}
+
 ######################################################################
 ##
 ## remote management
@@ -1682,11 +1812,12 @@ foreach i {
                {_M i mod      "Modified"}
                {M_ i fulltick "Included in commit"}
                {MM i parttick "Partially included"}
+               {MD i question "Included (but gone)"}
 
                {_O o plain    "Untracked"}
                {A_ o fulltick "Added by commit"}
                {AM o parttick "Partially added"}
-               {AD o question "Added (but now gone)"}
+               {AD o question "Added (but gone)"}
 
                {_D i question "Missing"}
                {DD i removed  "Removed by commit"}
@@ -1716,9 +1847,7 @@ unset filemask i
 
 proc is_MacOSX {} {
        global tcl_platform tk_library
-       if {$tcl_platform(platform) eq {unix}
-               && $tcl_platform(os) eq {Darwin}
-               && [string match /Library/Frameworks/* $tk_library]} {
+       if {[tk windowingsystem] eq {aqua}} {
                return 1
        }
        return 0
@@ -1939,20 +2068,28 @@ proc console_read {w fd after} {
 
 set starting_gitk_msg {Please wait... Starting gitk...}
 
-proc do_gitk {} {
+proc do_gitk {revs} {
        global ui_status_value starting_gitk_msg
 
-       set ui_status_value $starting_gitk_msg
-       after 10000 {
-               if {$ui_status_value eq $starting_gitk_msg} {
-                       set ui_status_value {Ready.}
-               }
+       set cmd gitk
+       if {$revs ne {}} {
+               append cmd { }
+               append cmd $revs
        }
-
        if {[is_Windows]} {
-               exec sh -c gitk &
+               set cmd "sh -c \"exec $cmd\""
+       }
+       append cmd { &}
+
+       if {[catch {eval exec $cmd} err]} {
+               error_popup "Failed to start gitk:\n\n$err"
        } else {
-               exec gitk &
+               set ui_status_value $starting_gitk_msg
+               after 10000 {
+                       if {$ui_status_value eq $starting_gitk_msg} {
+                               set ui_status_value {Ready.}
+                       }
+               }
        }
 }
 
@@ -2127,6 +2264,74 @@ proc do_include_all {} {
                $paths
 }
 
+proc revert_helper {txt paths} {
+       global file_states current_diff
+
+       if {![lock_index begin-update]} return
+
+       set pathList [list]
+       set after {}
+       foreach path $paths {
+               switch -glob -- [lindex $file_states($path) 0] {
+               AM -
+               AD -
+               MM -
+               MD -
+               _M -
+               _D {
+                       lappend pathList $path
+                       if {$path eq $current_diff} {
+                               set after {reshow_diff;}
+                       }
+               }
+               }
+       }
+
+       set n [llength $pathList]
+       if {$n == 0} {
+               unlock_index
+               return
+       } elseif {$n == 1} {
+               set s "[short_path [lindex $pathList]]"
+       } else {
+               set s "these $n files"
+       }
+
+       set reply [tk_dialog \
+               .confirm_revert \
+               "title" \
+               "Revert unincluded changes in $s?
+
+Any unincluded changes will be permanently lost by the revert." \
+               questhead \
+               1 \
+               {Do Nothing} \
+               {Revert Changes} \
+               ]
+       if {$reply == 1} {
+               checkout_index \
+                       $txt \
+                       $pathList \
+                       [concat $after {set ui_status_value {Ready.}}]
+       } else {
+               unlock_index
+       }
+}
+
+proc do_revert_selection {} {
+       global current_diff selected_paths
+
+       if {[array size selected_paths] > 0} {
+               revert_helper \
+                       {Reverting selected files} \
+                       [array names selected_paths]
+       } elseif {$current_diff ne {}} {
+               revert_helper \
+                       "Reverting [short_path $current_diff]" \
+                       [list $current_diff]
+       }
+}
+
 proc do_signoff {} {
        global ui_comm
 
@@ -2617,14 +2822,15 @@ catch {
 font create font_uibold
 font create font_diffbold
 
-set M1B M1
-set M1T M1
 if {[is_Windows]} {
        set M1B Control
        set M1T Ctrl
 } elseif {[is_MacOSX]} {
        set M1B M1
        set M1T Cmd
+} else {
+       set M1B M1
+       set M1T M1
 }
 
 proc apply_config {} {
@@ -2680,12 +2886,19 @@ if {!$single_commit} {
 # -- Repository Menu
 #
 menu .mbar.repository
-.mbar.repository add command -label Visualize \
-       -command do_gitk \
+.mbar.repository add command \
+       -label {Visualize Current Branch} \
+       -command {do_gitk {}} \
        -font font_ui
-if {!$single_commit} {
-       .mbar.repository add separator
+if {![is_MacOSX]} {
+       .mbar.repository add command \
+               -label {Visualize All Branches} \
+               -command {do_gitk {--all}} \
+               -font font_ui
+}
+.mbar.repository add separator
 
+if {!$single_commit} {
        .mbar.repository add command -label {Repack Database} \
                -command do_repack \
                -font font_ui
@@ -2778,12 +2991,6 @@ lappend disable_on_lock \
 lappend disable_on_lock \
        [list .mbar.commit entryconf [.mbar.commit index last] -state]
 
-.mbar.commit add command -label {Remove From Commit} \
-       -command do_remove_selection \
-       -font font_ui
-lappend disable_on_lock \
-       [list .mbar.commit entryconf [.mbar.commit index last] -state]
-
 .mbar.commit add command -label {Include In Commit} \
        -command do_include_selection \
        -font font_ui
@@ -2797,6 +3004,18 @@ lappend disable_on_lock \
 lappend disable_on_lock \
        [list .mbar.commit entryconf [.mbar.commit index last] -state]
 
+.mbar.commit add command -label {Remove From Commit} \
+       -command do_remove_selection \
+       -font font_ui
+lappend disable_on_lock \
+       [list .mbar.commit entryconf [.mbar.commit index last] -state]
+
+.mbar.commit add command -label {Revert Changes} \
+       -command do_revert_selection \
+       -font font_ui
+lappend disable_on_lock \
+       [list .mbar.commit entryconf [.mbar.commit index last] -state]
+
 .mbar.commit add separator
 
 .mbar.commit add command -label {Sign Off} \
@@ -2850,6 +3069,25 @@ if {[is_MacOSX]} {
 }
 
 
+# -- Branch Control
+#
+frame .branch \
+       -borderwidth 1 \
+       -relief sunken
+label .branch.l1 \
+       -text {Current Branch:} \
+       -anchor w \
+       -justify left \
+       -font font_ui
+label .branch.cb \
+       -textvariable current_branch \
+       -anchor w \
+       -justify left \
+       -font font_ui
+pack .branch.l1 -side left
+pack .branch.cb -side left -fill x
+pack .branch -side top -fill x
+
 # -- Main Window Layout
 #
 panedwindow .vpane -orient vertical
@@ -3276,11 +3514,70 @@ set PARENT {}
 set MERGE_HEAD [list]
 set commit_type {}
 set empty_tree {}
+set current_branch {}
 set current_diff {}
 set selected_commit_type new
 
 wm title . "$appname ([file normalize [file dirname $gitdir]])"
 focus -force $ui_comm
+
+# -- Warn the user about environmental problems.
+#    Cygwin's Tcl does *not* pass its env array
+#    onto any processes it spawns.  This means
+#    that the git processes get none of our
+#    environment.  That may not work...
+#
+if {[is_Windows]} {
+       set ignored_env 0
+       set suggest_user {}
+       set msg "Possible environment issues exist.
+
+The following environment variables are probably
+going to be ignored by any Git subprocess run
+by $appname:
+
+"
+       foreach name [array names env] {
+               switch -regexp -- $name {
+               {^GIT_INDEX_FILE$} -
+               {^GIT_OBJECT_DIRECTORY$} -
+               {^GIT_ALTERNATE_OBJECT_DIRECTORIES$} -
+               {^GIT_DIFF_OPTS$} -
+               {^GIT_EXTERNAL_DIFF$} -
+               {^GIT_PAGER$} -
+               {^GIT_TRACE$} -
+               {^GIT_CONFIG$} -
+               {^GIT_CONFIG_LOCAL$} -
+               {^GIT_(AUTHOR|COMMITTER)_DATE$} {
+                       append msg " - $name\n"
+                       incr ignored_env
+               }
+               {^GIT_(AUTHOR|COMMITTER)_(NAME|EMAIL)$} {
+                       append msg " - $name\n"
+                       incr ignored_env
+                       set suggest_user $name
+               }
+               }
+       }
+       if {$ignored_env > 0} {
+               append msg "
+This is due to a known issue with the
+Tcl binary distributed by Cygwin."
+
+               if {$suggest_user ne {}} {
+                       append msg "
+
+A good replacement for $suggest_user
+is placing values for the user.name and
+user.email settings into your personal
+~/.gitconfig file.
+"
+               }
+               warn_popup $msg
+       }
+       unset ignored_env msg suggest_user name
+}
+
 if {!$single_commit} {
        load_all_remotes
        populate_fetch_menu .mbar.fetch