@matheusbsbs / matheus-bagliano / commits / 4578c5cb69
Fork 0

git-gui: Automatically backup the user's commit buffer

A few users have been seeing crashes in Tk when using the undo key binding to undo the last few keystroke events in the commit buffer. Unfortunately that means the user loses their commit message and must start over from scratch when the user restarts the process. git-gui now saves the user's commit message buffer every couple of seconds to a temporary file under .git (specifically .git/GITGUI_BCK). At exit time we rename this file to .git/GITGUI_MSG if there is a message, the file exists, and it is currently synchronized with the Tk buffer. Otherwise we do our usual routine of saving the Tk buffer to .git/GITGUI_MSG and delete .git/GITGUI_BCK, if it exists. During startup we favor .git/GITGUI_BCK over .git/GITGUI_MSG. This way a crash doesn't take out the user's message buffer but instead will cause the user to lose only a few keystrokes. Most people do not type more than 200 WPM, and with 30 possible saves per minute we are unlikely to lose more than 7 words. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jul 21, 2007 at 04:57 UTC 4578c5cb690df98d0d5fbea043114b4b7dec8f57
2 files changed +81 -11
git-gui.sh
+77 -11
@@ -1417,6 +1417,7 @@ set is_quitting 0
1417
1418 proc do_quit {} {
1419 global ui_comm is_quitting repo_config commit_type
1420 + global GITGUI_BCK_exists GITGUI_BCK_i
1421
1422 if {$is_quitting} return
1423 set is_quitting 1
@@ -1425,18 +1426,30 @@ proc do_quit {} {
1426 # -- Stash our current commit buffer.
1427 #
1428 set save [gitdir GITGUI_MSG]
1428 - set msg [string trim [$ui_comm get 0.0 end]]
1429 - regsub -all -line {[ \r\t]+$} $msg {} msg
1430 - if {(![string match amend* $commit_type]
1431 - || [$ui_comm edit modified])
1432 - && $msg ne {}} {
1433 - catch {
1434 - set fd [open $save w]
1435 - puts -nonewline $fd $msg
1436 - close $fd
1437 - }
1429 + if {$GITGUI_BCK_exists && ![$ui_comm edit modified]} {
1430 + file rename -force [gitdir GITGUI_BCK] $save
1431 + set GITGUI_BCK_exists 0
1432 } else {
1439 - catch {file delete $save}
1433 + set msg [string trim [$ui_comm get 0.0 end]]
1434 + regsub -all -line {[ \r\t]+$} $msg {} msg
1435 + if {(![string match amend* $commit_type]
1436 + || [$ui_comm edit modified])
1437 + && $msg ne {}} {
1438 + catch {
1439 + set fd [open $save w]
1440 + puts -nonewline $fd $msg
1441 + close $fd
1442 + }
1443 + } else {
1444 + catch {file delete $save}
1445 + }
1446 + }
1447 +
1448 + # -- Remove our editor backup, its not needed.
1449 + #
1450 + after cancel $GITGUI_BCK_i
1451 + if {$GITGUI_BCK_exists} {
1452 + catch {file delete [gitdir GITGUI_BCK]}
1453 }
1454
1455 # -- Stash our current window geometry into this repository.
@@ -2598,6 +2611,59 @@ if {[is_enabled transport]} {
2611 populate_push_menu
2612 }
2613
2614 +if {[winfo exists $ui_comm]} {
2615 + set GITGUI_BCK_exists [load_message GITGUI_BCK]
2616 +
2617 + # -- If both our backup and message files exist use the
2618 + # newer of the two files to initialize the buffer.
2619 + #
2620 + if {$GITGUI_BCK_exists} {
2621 + set m [gitdir GITGUI_MSG]
2622 + if {[file isfile $m]} {
2623 + if {[file mtime [gitdir GITGUI_BCK]] > [file mtime $m]} {
2624 + catch {file delete [gitdir GITGUI_MSG]}
2625 + } else {
2626 + $ui_comm delete 0.0 end
2627 + $ui_comm edit reset
2628 + $ui_comm edit modified false
2629 + catch {file delete [gitdir GITGUI_BCK]}
2630 + set GITGUI_BCK_exists 0
2631 + }
2632 + }
2633 + unset m
2634 + }
2635 +
2636 + proc backup_commit_buffer {} {
2637 + global ui_comm GITGUI_BCK_exists
2638 +
2639 + set m [$ui_comm edit modified]
2640 + if {$m || $GITGUI_BCK_exists} {
2641 + set msg [string trim [$ui_comm get 0.0 end]]
2642 + regsub -all -line {[ \r\t]+$} $msg {} msg
2643 +
2644 + if {$msg eq {}} {
2645 + if {$GITGUI_BCK_exists} {
2646 + catch {file delete [gitdir GITGUI_BCK]}
2647 + set GITGUI_BCK_exists 0
2648 + }
2649 + } elseif {$m} {
2650 + catch {
2651 + set fd [open [gitdir GITGUI_BCK] w]
2652 + puts -nonewline $fd $msg
2653 + close $fd
2654 + set GITGUI_BCK_exists 1
2655 + }
2656 + }
2657 +
2658 + $ui_comm edit modified false
2659 + }
2660 +
2661 + set ::GITGUI_BCK_i [after 2000 backup_commit_buffer]
2662 + }
2663 +
2664 + backup_commit_buffer
2665 +}
2666 +
2667 lock_index begin-read
2668 if {![winfo ismapped .]} {
2669 wm deiconify .
lib/commit.tcl
+4
@@ -379,6 +379,10 @@ A rescan will be automatically started now.
379 $ui_comm delete 0.0 end
380 $ui_comm edit reset
381 $ui_comm edit modified false
382 + if {$::GITGUI_BCK_exists} {
383 + catch {file delete [gitdir GITGUI_BCK]}
384 + set $::GITGUI_BCK_exists 0
385 + }
386
387 if {[is_enabled singlecommit]} do_quit
388