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