git-gui: allow Ctrl+T to toggle multiple paths
[git/git.git] / lib / branch_create.tcl
CommitLineData
b1fa2bff
SP
1# git-gui branch create support
2# Copyright (C) 2006, 2007 Shawn Pearce
3
4class branch_create {
5
6field w ; # widget path
7field w_rev ; # mega-widget to pick the initial revision
8field w_name ; # new branch name widget
9
10field name {}; # name of the branch the user has chosen
dd87efc8
SP
11field name_type user; # type of branch name to use
12
774173aa 13field opt_merge ff; # type of merge to apply to existing branch
b1fa2bff 14field opt_checkout 1; # automatically checkout the new branch?
ba1964be 15field opt_fetch 1; # refetch tracking branch if used?
774173aa 16field reset_ok 0; # did the user agree to reset?
b1fa2bff
SP
17
18constructor dialog {} {
c80d7be5 19 global repo_config use_ttk NS
b1fa2bff 20
c80d7be5
PT
21 make_dialog top w
22 wm withdraw $w
a3d97afa 23 wm title $top [mc "%s (%s): Create Branch" [appname] [reponame]]
b1fa2bff
SP
24 if {$top ne {.}} {
25 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
26 }
27
c80d7be5
PT
28 ${NS}::label $w.header -text [mc "Create New Branch"] \
29 -font font_uibold -anchor center
b1fa2bff
SP
30 pack $w.header -side top -fill x
31
c80d7be5
PT
32 ${NS}::frame $w.buttons
33 ${NS}::button $w.buttons.create -text [mc Create] \
b1fa2bff
SP
34 -default active \
35 -command [cb _create]
36 pack $w.buttons.create -side right
c80d7be5 37 ${NS}::button $w.buttons.cancel -text [mc Cancel] \
b1fa2bff
SP
38 -command [list destroy $w]
39 pack $w.buttons.cancel -side right -padx 5
40 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
41
c80d7be5
PT
42 ${NS}::labelframe $w.desc -text [mc "Branch Name"]
43 ${NS}::radiobutton $w.desc.name_r \
1ac17950 44 -text [mc "Name:"] \
dd87efc8
SP
45 -value user \
46 -variable @name_type
c80d7be5 47 if {!$use_ttk} {$w.desc.name_r configure -anchor w}
b1fa2bff 48 set w_name $w.desc.name_t
c80d7be5 49 ${NS}::entry $w_name \
b1fa2bff
SP
50 -width 40 \
51 -textvariable @name \
52 -validate key \
53 -validatecommand [cb _validate %d %S]
54 grid $w.desc.name_r $w_name -sticky we -padx {0 5}
55
c80d7be5 56 ${NS}::radiobutton $w.desc.match_r \
1ac17950 57 -text [mc "Match Tracking Branch Name"] \
dd87efc8
SP
58 -value match \
59 -variable @name_type
c80d7be5 60 if {!$use_ttk} {$w.desc.match_r configure -anchor w}
dd87efc8
SP
61 grid $w.desc.match_r -sticky we -padx {0 5} -columnspan 2
62
b1fa2bff
SP
63 grid columnconfigure $w.desc 1 -weight 1
64 pack $w.desc -anchor nw -fill x -pady 5 -padx 5
65
1ac17950 66 set w_rev [::choose_rev::new $w.rev [mc "Starting Revision"]]
7618e6b1 67 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
b1fa2bff 68
c80d7be5 69 ${NS}::labelframe $w.options -text [mc Options]
774173aa 70
c80d7be5
PT
71 ${NS}::frame $w.options.merge
72 ${NS}::label $w.options.merge.l -text [mc "Update Existing Branch:"]
774173aa 73 pack $w.options.merge.l -side left
c80d7be5 74 ${NS}::radiobutton $w.options.merge.no \
1ac17950 75 -text [mc No] \
d41b43eb 76 -value none \
774173aa
SP
77 -variable @opt_merge
78 pack $w.options.merge.no -side left
c80d7be5 79 ${NS}::radiobutton $w.options.merge.ff \
1ac17950 80 -text [mc "Fast Forward Only"] \
774173aa
SP
81 -value ff \
82 -variable @opt_merge
83 pack $w.options.merge.ff -side left
c80d7be5 84 ${NS}::radiobutton $w.options.merge.reset \
1ac17950 85 -text [mc Reset] \
774173aa
SP
86 -value reset \
87 -variable @opt_merge
88 pack $w.options.merge.reset -side left
89 pack $w.options.merge -anchor nw
90
c80d7be5 91 ${NS}::checkbutton $w.options.fetch \
1ac17950 92 -text [mc "Fetch Tracking Branch"] \
ba1964be
SP
93 -variable @opt_fetch
94 pack $w.options.fetch -anchor nw
95
c80d7be5 96 ${NS}::checkbutton $w.options.checkout \
1ac17950 97 -text [mc "Checkout After Creation"] \
b1fa2bff 98 -variable @opt_checkout
774173aa
SP
99 pack $w.options.checkout -anchor nw
100 pack $w.options -anchor nw -fill x -pady 5 -padx 5
b1fa2bff 101
7cf04426
SP
102 trace add variable @name_type write [cb _select]
103
b1fa2bff 104 set name $repo_config(gui.newbranchtemplate)
7cf04426
SP
105 if {[is_config_true gui.matchtrackingbranch]} {
106 set name_type match
107 }
b1fa2bff 108
7cf04426 109 bind $w <Visibility> [cb _visible]
b1fa2bff
SP
110 bind $w <Key-Escape> [list destroy $w]
111 bind $w <Key-Return> [cb _create]\;break
c80d7be5 112 wm deiconify $w
b1fa2bff
SP
113 tkwait window $w
114}
115
116method _create {} {
d41b43eb 117 global repo_config
ba1964be 118 global M1B
b1fa2bff 119
ba1964be 120 set spec [$w_rev get_tracking_branch]
dd87efc8
SP
121 switch -- $name_type {
122 user {
123 set newbranch $name
124 }
125 match {
dd87efc8
SP
126 if {$spec eq {}} {
127 tk_messageBox \
128 -icon error \
129 -type ok \
130 -title [wm title $w] \
131 -parent $w \
1ac17950 132 -message [mc "Please select a tracking branch."]
dd87efc8
SP
133 return
134 }
135 if {![regsub ^refs/heads/ [lindex $spec 2] {} newbranch]} {
136 tk_messageBox \
137 -icon error \
138 -type ok \
139 -title [wm title $w] \
140 -parent $w \
1ac17950 141 -message [mc "Tracking branch %s is not a branch in the remote repository." [$w get]]
dd87efc8
SP
142 return
143 }
144 }
145 }
146
b1fa2bff
SP
147 if {$newbranch eq {}
148 || $newbranch eq $repo_config(gui.newbranchtemplate)} {
149 tk_messageBox \
150 -icon error \
151 -type ok \
152 -title [wm title $w] \
153 -parent $w \
1ac17950 154 -message [mc "Please supply a branch name."]
b1fa2bff
SP
155 focus $w_name
156 return
157 }
774173aa 158
b1fa2bff
SP
159 if {[catch {git check-ref-format "heads/$newbranch"}]} {
160 tk_messageBox \
161 -icon error \
162 -type ok \
163 -title [wm title $w] \
164 -parent $w \
1ac17950 165 -message [mc "'%s' is not an acceptable branch name." $newbranch]
b1fa2bff
SP
166 focus $w_name
167 return
168 }
169
ba1964be 170 if {$spec ne {} && $opt_fetch} {
d41b43eb
SP
171 set new {}
172 } elseif {[catch {set new [$w_rev commit_or_die]}]} {
b1fa2bff
SP
173 return
174 }
774173aa 175
d41b43eb
SP
176 set co [::checkout_op::new \
177 [$w_rev get] \
178 $new \
179 refs/heads/$newbranch]
180 $co parent $w
181 $co enable_create 1
182 $co enable_merge $opt_merge
183 $co enable_checkout $opt_checkout
184 if {$spec ne {} && $opt_fetch} {
185 $co enable_fetch $spec
b1fa2bff 186 }
fe70225d
SP
187 if {$spec ne {}} {
188 $co remote_source $spec
189 }
774173aa 190
d41b43eb
SP
191 if {[$co run]} {
192 destroy $w
193 } else {
194 focus $w_name
774173aa 195 }
774173aa
SP
196}
197
b1fa2bff
SP
198method _validate {d S} {
199 if {$d == 1} {
200 if {[regexp {[~^:?*\[\0- ]} $S]} {
201 return 0
202 }
203 if {[string length $S] > 0} {
204 set name_type user
205 }
206 }
207 return 1
208}
209
7cf04426
SP
210method _select {args} {
211 if {$name_type eq {match}} {
212 $w_rev pick_tracking_branch
213 }
214}
215
216method _visible {} {
217 grab $w
218 if {$name_type eq {user}} {
219 $w_name icursor end
220 focus $w_name
221 }
222}
223
b1fa2bff 224}