Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 1 | # git-gui branch rename support |
| 2 | # Copyright (C) 2007 Shawn Pearce |
| 3 | |
| 4 | class branch_rename { |
| 5 | |
| 6 | field w |
| 7 | field oldname |
| 8 | field newname |
| 9 | |
| 10 | constructor dialog {} { |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 11 | global current_branch |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 12 | |
| 13 | make_toplevel top w |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 14 | wm title $top [append "[appname] ([reponame]): " [mc "Rename Branch"]] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 15 | if {$top ne {.}} { |
| 16 | wm geometry $top "+[winfo rootx .]+[winfo rooty .]" |
| 17 | } |
| 18 | |
| 19 | set oldname $current_branch |
| 20 | set newname [get_config gui.newbranchtemplate] |
| 21 | |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 22 | label $w.header -text [mc "Rename Branch"] -font font_uibold |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 23 | pack $w.header -side top -fill x |
| 24 | |
| 25 | frame $w.buttons |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 26 | button $w.buttons.rename -text [mc Rename] \ |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 27 | -default active \ |
| 28 | -command [cb _rename] |
| 29 | pack $w.buttons.rename -side right |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 30 | button $w.buttons.cancel -text [mc Cancel] \ |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 31 | -command [list destroy $w] |
| 32 | pack $w.buttons.cancel -side right -padx 5 |
| 33 | pack $w.buttons -side bottom -fill x -pady 10 -padx 10 |
| 34 | |
| 35 | frame $w.rename |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 36 | label $w.rename.oldname_l -text [mc "Branch:"] |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 37 | eval tk_optionMenu $w.rename.oldname_m @oldname [load_all_heads] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 38 | |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 39 | label $w.rename.newname_l -text [mc "New Name:"] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 40 | entry $w.rename.newname_t \ |
| 41 | -borderwidth 1 \ |
| 42 | -relief sunken \ |
| 43 | -width 40 \ |
| 44 | -textvariable @newname \ |
| 45 | -validate key \ |
| 46 | -validatecommand { |
| 47 | if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0} |
| 48 | return 1 |
| 49 | } |
| 50 | |
| 51 | grid $w.rename.oldname_l $w.rename.oldname_m -sticky w -padx {0 5} |
| 52 | grid $w.rename.newname_l $w.rename.newname_t -sticky we -padx {0 5} |
| 53 | grid columnconfigure $w.rename 1 -weight 1 |
| 54 | pack $w.rename -anchor nw -fill x -pady 5 -padx 5 |
| 55 | |
| 56 | bind $w <Key-Return> [cb _rename] |
| 57 | bind $w <Key-Escape> [list destroy $w] |
| 58 | bind $w <Visibility> " |
| 59 | grab $w |
| 60 | $w.rename.newname_t icursor end |
| 61 | focus $w.rename.newname_t |
| 62 | " |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 63 | tkwait window $w |
| 64 | } |
| 65 | |
| 66 | method _rename {} { |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 67 | global current_branch |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 68 | |
| 69 | if {$oldname eq {}} { |
| 70 | tk_messageBox \ |
| 71 | -icon error \ |
| 72 | -type ok \ |
| 73 | -title [wm title $w] \ |
| 74 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 75 | -message [mc "Please select a branch to rename."] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 76 | focus $w.rename.oldname_m |
| 77 | return |
| 78 | } |
| 79 | if {$newname eq {} |
| 80 | || $newname eq [get_config gui.newbranchtemplate]} { |
| 81 | tk_messageBox \ |
| 82 | -icon error \ |
| 83 | -type ok \ |
| 84 | -title [wm title $w] \ |
| 85 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 86 | -message [mc "Please supply a branch name."] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 87 | focus $w.rename.newname_t |
| 88 | return |
| 89 | } |
| 90 | if {![catch {git show-ref --verify -- "refs/heads/$newname"}]} { |
| 91 | tk_messageBox \ |
| 92 | -icon error \ |
| 93 | -type ok \ |
| 94 | -title [wm title $w] \ |
| 95 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 96 | -message [mc "Branch '%s' already exists." $newname] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 97 | focus $w.rename.newname_t |
| 98 | return |
| 99 | } |
| 100 | if {[catch {git check-ref-format "heads/$newname"}]} { |
| 101 | tk_messageBox \ |
| 102 | -icon error \ |
| 103 | -type ok \ |
| 104 | -title [wm title $w] \ |
| 105 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 106 | -message [mc "'%s' is not an acceptable branch name." $newname] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 107 | focus $w.rename.newname_t |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | if {[catch {git branch -m $oldname $newname} err]} { |
| 112 | tk_messageBox \ |
| 113 | -icon error \ |
| 114 | -type ok \ |
| 115 | -title [wm title $w] \ |
| 116 | -parent $w \ |
Shawn O. Pearce | 31bb1d1 | 2007-09-14 01:50:09 -0400 | [diff] [blame] | 117 | -message [strcat [mc "Failed to rename '%s'." $oldname] "\n\n$err"] |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 118 | return |
| 119 | } |
| 120 | |
Shawn O. Pearce | 61f82ce | 2007-05-28 12:52:57 -0400 | [diff] [blame] | 121 | if {$current_branch eq $oldname} { |
| 122 | set current_branch $newname |
| 123 | } |
| 124 | |
| 125 | destroy $w |
| 126 | } |
| 127 | |
| 128 | } |