@matheusbsbs / matheus-bagliano / commits / e734817db0
Fork 0

git-gui: Added revert changes command.

Users sometimes need to be able to throw away locally modified files in order to go back to the last committed version of that file. To perform a revert the user must first uninclude each file from the new commit as the working file must at least partially match the index, and we use git-checkout-index to update the working directory. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Nov 23, 2006 at 21:40 UTC e734817db0af028f2aea356b44c74b7ac51023cd
1 file changed +177 -7
git-gui
+177 -7
@@ -1277,9 +1277,26 @@ proc display_file {path state} {
1277 set old_w [mapcol $old_m $path]
1278 set new_icon [mapicon $new_m $path]
1279
1280 + if {$new_m eq {__}} {
1281 + set lno [lsearch -sorted $file_lists($old_w) $path]
1282 + if {$lno >= 0} {
1283 + set file_lists($old_w) \
1284 + [lreplace $file_lists($old_w) $lno $lno]
1285 + incr lno
1286 + $old_w conf -state normal
1287 + $old_w delete $lno.0 [expr {$lno + 1}].0
1288 + $old_w conf -state disabled
1289 + }
1290 + unset file_states($path)
1291 + catch {unset selected_paths($path)}
1292 + return
1293 + }
1294 +
1295 if {$new_w ne $old_w} {
1296 set lno [lsearch -sorted $file_lists($old_w) $path]
1297 if {$lno >= 0} {
1298 + set file_lists($old_w) \
1299 + [lreplace $file_lists($old_w) $lno $lno]
1300 incr lno
1301 $old_w conf -state normal
1302 $old_w delete $lno.0 [expr {$lno + 1}].0
@@ -1500,6 +1517,84 @@ proc write_update_index {fd pathList totalCnt batch msg after} {
1517 [expr {100.0 * $update_index_cp / $totalCnt}]]
1518 }
1519
1520 +proc checkout_index {msg pathList after} {
1521 + global update_index_cp ui_status_value
1522 +
1523 + if {![lock_index update]} return
1524 +
1525 + set update_index_cp 0
1526 + set pathList [lsort $pathList]
1527 + set totalCnt [llength $pathList]
1528 + set batch [expr {int($totalCnt * .01) + 1}]
1529 + if {$batch > 25} {set batch 25}
1530 +
1531 + set ui_status_value [format \
1532 + "$msg... %i/%i files (%.2f%%)" \
1533 + $update_index_cp \
1534 + $totalCnt \
1535 + 0.0]
1536 + set cmd [list git checkout-index]
1537 + lappend cmd --index
1538 + lappend cmd --quiet
1539 + lappend cmd --force
1540 + lappend cmd -z
1541 + lappend cmd --stdin
1542 + set fd [open "| $cmd " w]
1543 + fconfigure $fd \
1544 + -blocking 0 \
1545 + -buffering full \
1546 + -buffersize 512 \
1547 + -translation binary
1548 + fileevent $fd writable [list \
1549 + write_checkout_index \
1550 + $fd \
1551 + $pathList \
1552 + $totalCnt \
1553 + $batch \
1554 + $msg \
1555 + $after \
1556 + ]
1557 +}
1558 +
1559 +proc write_checkout_index {fd pathList totalCnt batch msg after} {
1560 + global update_index_cp ui_status_value
1561 + global file_states current_diff
1562 +
1563 + if {$update_index_cp >= $totalCnt} {
1564 + close $fd
1565 + unlock_index
1566 + uplevel #0 $after
1567 + return
1568 + }
1569 +
1570 + for {set i $batch} \
1571 + {$update_index_cp < $totalCnt && $i > 0} \
1572 + {incr i -1} {
1573 + set path [lindex $pathList $update_index_cp]
1574 + incr update_index_cp
1575 +
1576 + switch -glob -- [lindex $file_states($path) 0] {
1577 + AM -
1578 + AD {set new A_}
1579 + MM -
1580 + MD {set new M_}
1581 + _M -
1582 + _D {set new __}
1583 + ?? {continue}
1584 + }
1585 +
1586 + puts -nonewline $fd $path
1587 + puts -nonewline $fd "\0"
1588 + display_file $path $new
1589 + }
1590 +
1591 + set ui_status_value [format \
1592 + "$msg... %i/%i files (%.2f%%)" \
1593 + $update_index_cp \
1594 + $totalCnt \
1595 + [expr {100.0 * $update_index_cp / $totalCnt}]]
1596 +}
1597 +
1598 ######################################################################
1599 ##
1600 ## remote management
@@ -1708,11 +1803,12 @@ foreach i {
1803 {_M i mod "Modified"}
1804 {M_ i fulltick "Included in commit"}
1805 {MM i parttick "Partially included"}
1806 + {MD i question "Included (but gone)"}
1807
1808 {_O o plain "Untracked"}
1809 {A_ o fulltick "Added by commit"}
1810 {AM o parttick "Partially added"}
1715 - {AD o question "Added (but now gone)"}
1811 + {AD o question "Added (but gone)"}
1812
1813 {_D i question "Missing"}
1814 {DD i removed "Removed by commit"}
@@ -2159,6 +2255,74 @@ proc do_include_all {} {
2255 $paths
2256 }
2257
2258 +proc revert_helper {txt paths} {
2259 + global file_states current_diff
2260 +
2261 + if {![lock_index begin-update]} return
2262 +
2263 + set pathList [list]
2264 + set after {}
2265 + foreach path $paths {
2266 + switch -glob -- [lindex $file_states($path) 0] {
2267 + AM -
2268 + AD -
2269 + MM -
2270 + MD -
2271 + _M -
2272 + _D {
2273 + lappend pathList $path
2274 + if {$path eq $current_diff} {
2275 + set after {reshow_diff;}
2276 + }
2277 + }
2278 + }
2279 + }
2280 +
2281 + set n [llength $pathList]
2282 + if {$n == 0} {
2283 + unlock_index
2284 + return
2285 + } elseif {$n == 1} {
2286 + set s "[short_path [lindex $pathList]]"
2287 + } else {
2288 + set s "these $n files"
2289 + }
2290 +
2291 + set reply [tk_dialog \
2292 + .confirm_revert \
2293 + "title" \
2294 + "Revert unincluded changes in $s?
2295 +
2296 +Any unincluded changes will be permanently lost by the revert." \
2297 + questhead \
2298 + 1 \
2299 + {Do Nothing} \
2300 + {Revert Changes} \
2301 + ]
2302 + if {$reply == 1} {
2303 + checkout_index \
2304 + $txt \
2305 + $pathList \
2306 + [concat $after {set ui_status_value {Ready.}}]
2307 + } else {
2308 + unlock_index
2309 + }
2310 +}
2311 +
2312 +proc do_revert_selection {} {
2313 + global current_diff selected_paths
2314 +
2315 + if {[array size selected_paths] > 0} {
2316 + revert_helper \
2317 + {Reverting selected files} \
2318 + [array names selected_paths]
2319 + } elseif {$current_diff ne {}} {
2320 + revert_helper \
2321 + "Reverting [short_path $current_diff]" \
2322 + [list $current_diff]
2323 + }
2324 +}
2325 +
2326 proc do_signoff {} {
2327 global ui_comm
2328
@@ -2818,12 +2982,6 @@ lappend disable_on_lock \
2982 lappend disable_on_lock \
2983 [list .mbar.commit entryconf [.mbar.commit index last] -state]
2984
2821 -.mbar.commit add command -label {Remove From Commit} \
2822 - -command do_remove_selection \
2823 - -font font_ui
2824 -lappend disable_on_lock \
2825 - [list .mbar.commit entryconf [.mbar.commit index last] -state]
2826 -
2985 .mbar.commit add command -label {Include In Commit} \
2986 -command do_include_selection \
2987 -font font_ui
@@ -2837,6 +2995,18 @@ lappend disable_on_lock \
2995 lappend disable_on_lock \
2996 [list .mbar.commit entryconf [.mbar.commit index last] -state]
2997
2998 +.mbar.commit add command -label {Remove From Commit} \
2999 + -command do_remove_selection \
3000 + -font font_ui
3001 +lappend disable_on_lock \
3002 + [list .mbar.commit entryconf [.mbar.commit index last] -state]
3003 +
3004 +.mbar.commit add command -label {Revert Changes} \
3005 + -command do_revert_selection \
3006 + -font font_ui
3007 +lappend disable_on_lock \
3008 + [list .mbar.commit entryconf [.mbar.commit index last] -state]
3009 +
3010 .mbar.commit add separator
3011
3012 .mbar.commit add command -label {Sign Off} \