Merge branch 'sb/send-email-use-to-from-input'
[git/kirr.git] / git-gui / lib / branch_rename.tcl
blob63988773ba0d02af3dc91474b4a1669ecda27c5c
1 # git-gui branch rename support
2 # Copyright (C) 2007 Shawn Pearce
4 class branch_rename {
6 field w
7 field oldname
8 field newname
10 constructor dialog {} {
11 global current_branch use_ttk NS
13 make_dialog top w
14 wm withdraw $w
15 wm title $top [append "[appname] ([reponame]): " [mc "Rename Branch"]]
16 if {$top ne {.}} {
17 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
20 set oldname $current_branch
21 set newname [get_config gui.newbranchtemplate]
23 ${NS}::label $w.header -text [mc "Rename Branch"]\
24 -font font_uibold -anchor center
25 pack $w.header -side top -fill x
27 ${NS}::frame $w.buttons
28 ${NS}::button $w.buttons.rename -text [mc Rename] \
29 -default active \
30 -command [cb _rename]
31 pack $w.buttons.rename -side right
32 ${NS}::button $w.buttons.cancel -text [mc Cancel] \
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
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]
46 ${NS}::label $w.rename.newname_l -text [mc "New Name:"]
47 ${NS}::entry $w.rename.newname_t \
48 -width 40 \
49 -textvariable @newname \
50 -validate key \
51 -validatecommand {
52 if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
53 return 1
56 grid $w.rename.oldname_l $w.rename.oldname_m -sticky w -padx {0 5}
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
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
68 wm deiconify $w
69 tkwait window $w
72 method _rename {} {
73 global current_branch
75 if {$oldname eq {}} {
76 tk_messageBox \
77 -icon error \
78 -type ok \
79 -title [wm title $w] \
80 -parent $w \
81 -message [mc "Please select a branch to rename."]
82 focus $w.rename.oldname_m
83 return
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 \
92 -message [mc "Please supply a branch name."]
93 focus $w.rename.newname_t
94 return
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 \
102 -message [mc "Branch '%s' already exists." $newname]
103 focus $w.rename.newname_t
104 return
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 \
112 -message [mc "'%s' is not an acceptable branch name." $newname]
113 focus $w.rename.newname_t
114 return
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 \
123 -message [strcat [mc "Failed to rename '%s'." $oldname] "\n\n$err"]
124 return
127 if {$current_branch eq $oldname} {
128 set current_branch $newname
131 destroy $w