]>
Commit | Line | Data |
---|---|---|
61f82ce7 SP |
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 {} { | |
c80d7be5 | 11 | global current_branch use_ttk NS |
61f82ce7 | 12 | |
c80d7be5 PT |
13 | make_dialog top w |
14 | wm withdraw $w | |
a3d97afa | 15 | wm title $top [mc "%s (%s): Rename Branch" [appname] [reponame]] |
61f82ce7 SP |
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 | ||
c80d7be5 PT |
23 | ${NS}::label $w.header -text [mc "Rename Branch"]\ |
24 | -font font_uibold -anchor center | |
61f82ce7 SP |
25 | pack $w.header -side top -fill x |
26 | ||
c80d7be5 PT |
27 | ${NS}::frame $w.buttons |
28 | ${NS}::button $w.buttons.rename -text [mc Rename] \ | |
61f82ce7 SP |
29 | -default active \ |
30 | -command [cb _rename] | |
31 | pack $w.buttons.rename -side right | |
c80d7be5 | 32 | ${NS}::button $w.buttons.cancel -text [mc Cancel] \ |
61f82ce7 SP |
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 | ||
c80d7be5 PT |
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 | } | |
61f82ce7 | 45 | |
c80d7be5 PT |
46 | ${NS}::label $w.rename.newname_l -text [mc "New Name:"] |
47 | ${NS}::entry $w.rename.newname_t \ | |
61f82ce7 SP |
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 | ||
4c56d1dd | 56 | grid $w.rename.oldname_l $w.rename.oldname_m -sticky we -padx {0 5} |
61f82ce7 SP |
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 | " | |
c80d7be5 | 68 | wm deiconify $w |
61f82ce7 SP |
69 | tkwait window $w |
70 | } | |
71 | ||
72 | method _rename {} { | |
d41b43eb | 73 | global current_branch |
61f82ce7 SP |
74 | |
75 | if {$oldname eq {}} { | |
76 | tk_messageBox \ | |
77 | -icon error \ | |
78 | -type ok \ | |
79 | -title [wm title $w] \ | |
80 | -parent $w \ | |
1ac17950 | 81 | -message [mc "Please select a branch to rename."] |
61f82ce7 SP |
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 \ | |
1ac17950 | 92 | -message [mc "Please supply a branch name."] |
61f82ce7 SP |
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 \ | |
1ac17950 | 102 | -message [mc "Branch '%s' already exists." $newname] |
61f82ce7 SP |
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 \ | |
1ac17950 | 112 | -message [mc "'%s' is not an acceptable branch name." $newname] |
61f82ce7 SP |
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 \ | |
31bb1d1b | 123 | -message [strcat [mc "Failed to rename '%s'." $oldname] "\n\n$err"] |
61f82ce7 SP |
124 | return |
125 | } | |
126 | ||
61f82ce7 SP |
127 | if {$current_branch eq $oldname} { |
128 | set current_branch $newname | |
129 | } | |
130 | ||
131 | destroy $w | |
132 | } | |
133 | ||
134 | } |