@matheusbsbs / matheus-bagliano / commits / d41b43eb4c
Fork 0

git-gui: Refactor branch switch to support detached head

This is a major rewrite of the way we perform switching between branches and the subsequent update of the working directory. Like core Git we now use a single code path to perform all changes: our new checkout_op class. We also use it for branch creation/update as it integrates the tracking branch fetch process along with a very basic merge (fast-forward and reset only currently). Because some users have literally hundreds of local branches we use the standard revision picker (with its branch filtering tool) to select the local branch, rather than keeping all of the local branches in the Branch menu. The branch menu listing out all of the available branches is simply not sane for those types of huge repositories. Users can now checkout a detached head by ticking off the option in the checkout dialog. This option is off by default for the obvious reason, but it can be easily enabled for any local branch by simply checking it. We also detach the head if any non local branch was selected, or if a revision expression was entered. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jul 8, 2007 at 18:40 UTC d41b43eb4c73044d0fff2057211fc78e4e7b2094
11 files changed +746 -449
git-gui.sh
+19 -13
@@ -284,7 +284,9 @@ proc git {args} {
284 return [eval exec git $args]
285 }
286
287 -proc current-branch {} {
287 +proc load_current_branch {} {
288 + global current_branch is_detached
289 +
290 set fd [open [gitdir HEAD] r]
291 if {[gets $fd ref] < 1} {
292 set ref {}
@@ -297,11 +299,13 @@ proc current-branch {} {
299 # We're on a branch. It might not exist. But
300 # HEAD looks good enough to be a branch.
301 #
300 - return [string range $ref $len end]
302 + set current_branch [string range $ref $len end]
303 + set is_detached 0
304 } else {
305 # Assume this is a detached head.
306 #
304 - return HEAD
307 + set current_branch HEAD
308 + set is_detached 1
309 }
310 }
311
@@ -442,6 +446,7 @@ set MERGE_HEAD [list]
446 set commit_type {}
447 set empty_tree {}
448 set current_branch {}
449 +set is_detached 0
450 set current_diff_path {}
451 set selected_commit_type new
452
@@ -491,7 +496,7 @@ proc repository_state {ctvar hdvar mhvar} {
496
497 set mh [list]
498
494 - set current_branch [current-branch]
499 + load_current_branch
500 if {[catch {set hd [git rev-parse --verify HEAD]}]} {
501 set hd {}
502 set ct initial
@@ -557,11 +562,6 @@ proc rescan {after {honor_trustmtime 1}} {
562 $ui_comm edit modified false
563 }
564
560 - if {[is_enabled branch]} {
561 - load_all_heads
562 - populate_branch_menu
563 - }
564 -
565 if {$honor_trustmtime && $repo_config(gui.trustmtime) eq {true}} {
566 rescan_stage2 {} $after
567 } else {
@@ -1519,6 +1519,12 @@ if {[is_enabled branch]} {
1519 lappend disable_on_lock [list .mbar.branch entryconf \
1520 [.mbar.branch index last] -state]
1521
1522 + .mbar.branch add command -label {Checkout...} \
1523 + -command branch_checkout::dialog \
1524 + -accelerator $M1T-O
1525 + lappend disable_on_lock [list .mbar.branch entryconf \
1526 + [.mbar.branch index last] -state]
1527 +
1528 .mbar.branch add command -label {Rename...} \
1529 -command branch_rename::dialog
1530 lappend disable_on_lock [list .mbar.branch entryconf \
@@ -1739,7 +1745,7 @@ switch -- $subcommand {
1745 browser {
1746 set subcommand_args {rev?}
1747 switch [llength $argv] {
1742 - 0 { set current_branch [current-branch] }
1748 + 0 { load_current_branch }
1749 1 { set current_branch [lindex $argv 0] }
1750 default usage
1751 }
@@ -1773,7 +1779,7 @@ blame {
1779 unset is_path
1780
1781 if {$head eq {}} {
1776 - set current_branch [current-branch]
1782 + load_current_branch
1783 } else {
1784 set current_branch $head
1785 }
@@ -2240,6 +2246,8 @@ bind $ui_diff <Button-1> {focus %W}
2246 if {[is_enabled branch]} {
2247 bind . <$M1B-Key-n> branch_create::dialog
2248 bind . <$M1B-Key-N> branch_create::dialog
2249 + bind . <$M1B-Key-o> branch_checkout::dialog
2250 + bind . <$M1B-Key-O> branch_checkout::dialog
2251 }
2252 if {[is_enabled transport]} {
2253 bind . <$M1B-Key-p> do_push_anywhere
@@ -2326,9 +2334,7 @@ user.email settings into your personal
2334 #
2335 if {[is_enabled transport]} {
2336 load_all_remotes
2329 - load_all_heads
2337
2331 - populate_branch_menu
2338 populate_fetch_menu
2339 populate_push_menu
2340 }
lib/branch.tcl
+1 -168
@@ -2,7 +2,6 @@
2 # Copyright (C) 2006, 2007 Shawn Pearce
3
4 proc load_all_heads {} {
5 - global all_heads
5 global some_heads_tracking
6
7 set rh refs/heads
@@ -16,7 +15,7 @@ proc load_all_heads {} {
15 }
16 close $fd
17
19 - set all_heads [lsort $all_heads]
18 + return [lsort $all_heads]
19 }
20
21 proc load_all_tags {} {
@@ -30,173 +29,7 @@ proc load_all_tags {} {
29 return $all_tags
30 }
31
33 -proc populate_branch_menu {} {
34 - global all_heads disable_on_lock
35 -
36 - set m .mbar.branch
37 - set last [$m index last]
38 - for {set i 0} {$i <= $last} {incr i} {
39 - if {[$m type $i] eq {separator}} {
40 - $m delete $i last
41 - set new_dol [list]
42 - foreach a $disable_on_lock {
43 - if {[lindex $a 0] ne $m || [lindex $a 2] < $i} {
44 - lappend new_dol $a
45 - }
46 - }
47 - set disable_on_lock $new_dol
48 - break
49 - }
50 - }
51 -
52 - if {$all_heads ne {}} {
53 - $m add separator
54 - }
55 - foreach b $all_heads {
56 - $m add radiobutton \
57 - -label $b \
58 - -command [list switch_branch $b] \
59 - -variable current_branch \
60 - -value $b
61 - lappend disable_on_lock \
62 - [list $m entryconf [$m index last] -state]
63 - }
64 -}
65 -
32 proc radio_selector {varname value args} {
33 upvar #0 $varname var
34 set var $value
35 }
70 -
71 -proc switch_branch {new_branch} {
72 - global HEAD commit_type current_branch repo_config
73 -
74 - if {![lock_index switch]} return
75 -
76 - # -- Our in memory state should match the repository.
77 - #
78 - repository_state curType curHEAD curMERGE_HEAD
79 - if {[string match amend* $commit_type]
80 - && $curType eq {normal}
81 - && $curHEAD eq $HEAD} {
82 - } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
83 - info_popup {Last scanned state does not match repository state.
84 -
85 -Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed.
86 -
87 -The rescan will be automatically started now.
88 -}
89 - unlock_index
90 - rescan {set ui_status_value {Ready.}}
91 - return
92 - }
93 -
94 - # -- Don't do a pointless switch.
95 - #
96 - if {$current_branch eq $new_branch} {
97 - unlock_index
98 - return
99 - }
100 -
101 - if {$repo_config(gui.trustmtime) eq {true}} {
102 - switch_branch_stage2 {} $new_branch
103 - } else {
104 - set ui_status_value {Refreshing file status...}
105 - set cmd [list git update-index]
106 - lappend cmd -q
107 - lappend cmd --unmerged
108 - lappend cmd --ignore-missing
109 - lappend cmd --refresh
110 - set fd_rf [open "| $cmd" r]
111 - fconfigure $fd_rf -blocking 0 -translation binary
112 - fileevent $fd_rf readable \
113 - [list switch_branch_stage2 $fd_rf $new_branch]
114 - }
115 -}
116 -
117 -proc switch_branch_stage2 {fd_rf new_branch} {
118 - global ui_status_value HEAD
119 -
120 - if {$fd_rf ne {}} {
121 - read $fd_rf
122 - if {![eof $fd_rf]} return
123 - close $fd_rf
124 - }
125 -
126 - set ui_status_value "Updating working directory to '$new_branch'..."
127 - set cmd [list git read-tree]
128 - lappend cmd -m
129 - lappend cmd -u
130 - lappend cmd --exclude-per-directory=.gitignore
131 - lappend cmd $HEAD
132 - lappend cmd $new_branch
133 - set fd_rt [open "| $cmd" r]
134 - fconfigure $fd_rt -blocking 0 -translation binary
135 - fileevent $fd_rt readable \
136 - [list switch_branch_readtree_wait $fd_rt $new_branch]
137 -}
138 -
139 -proc switch_branch_readtree_wait {fd_rt new_branch} {
140 - global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
141 - global current_branch
142 - global ui_comm ui_status_value
143 -
144 - # -- We never get interesting output on stdout; only stderr.
145 - #
146 - read $fd_rt
147 - fconfigure $fd_rt -blocking 1
148 - if {![eof $fd_rt]} {
149 - fconfigure $fd_rt -blocking 0
150 - return
151 - }
152 -
153 - # -- The working directory wasn't in sync with the index and
154 - # we'd have to overwrite something to make the switch. A
155 - # merge is required.
156 - #
157 - if {[catch {close $fd_rt} err]} {
158 - regsub {^fatal: } $err {} err
159 - warn_popup "File level merge required.
160 -
161 -$err
162 -
163 -Staying on branch '$current_branch'."
164 - set ui_status_value "Aborted checkout of '$new_branch' (file level merging is required)."
165 - unlock_index
166 - return
167 - }
168 -
169 - # -- Update the symbolic ref. Core git doesn't even check for failure
170 - # here, it Just Works(tm). If it doesn't we are in some really ugly
171 - # state that is difficult to recover from within git-gui.
172 - #
173 - if {[catch {git symbolic-ref HEAD "refs/heads/$new_branch"} err]} {
174 - error_popup "Failed to set current branch.
175 -
176 -This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
177 -
178 -This should not have occurred. [appname] will now close and give up.
179 -
180 -$err"
181 - do_quit
182 - return
183 - }
184 -
185 - # -- Update our repository state. If we were previously in amend mode
186 - # we need to toss the current buffer and do a full rescan to update
187 - # our file lists. If we weren't in amend mode our file lists are
188 - # accurate and we can avoid the rescan.
189 - #
190 - unlock_index
191 - set selected_commit_type new
192 - if {[string match amend* $commit_type]} {
193 - $ui_comm delete 0.0 end
194 - $ui_comm edit reset
195 - $ui_comm edit modified false
196 - rescan {set ui_status_value "Checked out branch '$current_branch'."}
197 - } else {
198 - repository_state commit_type HEAD MERGE_HEAD
199 - set PARENT $HEAD
200 - set ui_status_value "Checked out branch '$current_branch'."
201 - }
202 -}
lib/branch_checkout.tcl new
+88
@@ -0,0 +1,88 @@
1 +# git-gui branch checkout support
2 +# Copyright (C) 2007 Shawn Pearce
3 +
4 +class branch_checkout {
5 +
6 +field w ; # widget path
7 +field w_rev ; # mega-widget to pick the initial revision
8 +
9 +field opt_fetch 1; # refetch tracking branch if used?
10 +field opt_detach 0; # force a detached head case?
11 +
12 +constructor dialog {} {
13 + make_toplevel top w
14 + wm title $top "[appname] ([reponame]): Checkout Branch"
15 + if {$top ne {.}} {
16 + wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
17 + }
18 +
19 + label $w.header -text {Checkout Branch} -font font_uibold
20 + pack $w.header -side top -fill x
21 +
22 + frame $w.buttons
23 + button $w.buttons.create -text Checkout \
24 + -default active \
25 + -command [cb _checkout]
26 + pack $w.buttons.create -side right
27 + button $w.buttons.cancel -text {Cancel} \
28 + -command [list destroy $w]
29 + pack $w.buttons.cancel -side right -padx 5
30 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
31 +
32 + set w_rev [::choose_rev::new $w.rev {Revision}]
33 + pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
34 +
35 + labelframe $w.options -text {Options}
36 +
37 + checkbutton $w.options.fetch \
38 + -text {Fetch Tracking Branch} \
39 + -variable @opt_fetch
40 + pack $w.options.fetch -anchor nw
41 +
42 + checkbutton $w.options.detach \
43 + -text {Detach From Local Branch} \
44 + -variable @opt_detach
45 + pack $w.options.detach -anchor nw
46 +
47 + pack $w.options -anchor nw -fill x -pady 5 -padx 5
48 +
49 + bind $w <Visibility> [cb _visible]
50 + bind $w <Key-Escape> [list destroy $w]
51 + bind $w <Key-Return> [cb _checkout]\;break
52 + tkwait window $w
53 +}
54 +
55 +method _checkout {} {
56 + set spec [$w_rev get_tracking_branch]
57 + if {$spec ne {} && $opt_fetch} {
58 + set new {}
59 + } elseif {[catch {set new [$w_rev commit_or_die]}]} {
60 + return
61 + }
62 +
63 + if {$opt_detach} {
64 + set ref {}
65 + } else {
66 + set ref [$w_rev get_local_branch]
67 + }
68 +
69 + set co [::checkout_op::new [$w_rev get] $new $ref]
70 + $co parent $w
71 + $co enable_checkout 1
72 + if {$spec ne {} && $opt_fetch} {
73 + $co enable_fetch $spec
74 + }
75 +
76 + if {[$co run]} {
77 + destroy $w
78 + } else {
79 + $w_rev focus_filter
80 + }
81 +}
82 +
83 +method _visible {} {
84 + grab $w
85 + $w_rev focus_filter
86 +}
87 +
88 +}
lib/branch_create.tcl
+18 -229
@@ -73,7 +73,7 @@ constructor dialog {} {
73 pack $w.options.merge.l -side left
74 radiobutton $w.options.merge.no \
75 -text No \
76 - -value no \
76 + -value none \
77 -variable @opt_merge
78 pack $w.options.merge.no -side left
79 radiobutton $w.options.merge.ff \
@@ -113,7 +113,7 @@ constructor dialog {} {
113 }
114
115 method _create {} {
116 - global repo_config current_branch
116 + global repo_config
117 global M1B
118
119 set spec [$w_rev get_tracking_branch]
@@ -155,17 +155,6 @@ method _create {} {
155 return
156 }
157
158 - if {$newbranch eq $current_branch} {
159 - tk_messageBox \
160 - -icon error \
161 - -type ok \
162 - -title [wm title $w] \
163 - -parent $w \
164 - -message "'$newbranch' already exists and is the current branch."
165 - focus $w_name
166 - return
167 - }
168 -
158 if {[catch {git check-ref-format "heads/$newbranch"}]} {
159 tk_messageBox \
160 -icon error \
@@ -178,228 +167,28 @@ method _create {} {
167 }
168
169 if {$spec ne {} && $opt_fetch} {
181 - set l_trck [lindex $spec 0]
182 - set remote [lindex $spec 1]
183 - set r_head [lindex $spec 2]
184 - regsub ^refs/heads/ $r_head {} r_head
185 -
186 - set c $w.fetch_trck
187 - toplevel $c
188 - wm title $c "Refreshing Tracking Branch"
189 - wm geometry $c "+[winfo rootx $w]+[winfo rooty $w]"
190 -
191 - set e [::console::embed \
192 - $c.console \
193 - "Fetching $r_head from $remote"]
194 - pack $c.console -fill both -expand 1
195 - $e exec \
196 - [list git fetch $remote +$r_head:$l_trck] \
197 - [cb _finish_fetch $newbranch $c $e]
198 -
199 - bind $c <Visibility> [list grab $c]
200 - bind $c <$M1B-Key-w> break
201 - bind $c <$M1B-Key-W> break
202 - wm protocol $c WM_DELETE_WINDOW [cb _noop]
203 - } else {
204 - _finish_create $this $newbranch
205 - }
206 -}
207 -
208 -method _noop {} {}
209 -
210 -method _finish_fetch {newbranch c e ok} {
211 - wm protocol $c WM_DELETE_WINDOW {}
212 -
213 - if {$ok} {
214 - destroy $c
215 - _finish_create $this $newbranch
216 - } else {
217 - $e done $ok
218 - button $c.close -text Close -command [list destroy $c]
219 - pack $c.close -side bottom -anchor e -padx 10 -pady 10
220 - }
221 -}
222 -
223 -method _finish_create {newbranch} {
224 - global null_sha1 all_heads
225 -
226 - if {[catch {set new [$w_rev commit_or_die]}]} {
170 + set new {}
171 + } elseif {[catch {set new [$w_rev commit_or_die]}]} {
172 return
173 }
174
230 - set ref refs/heads/$newbranch
231 - if {[catch {set cur [git rev-parse --verify "$ref^0"]}]} {
232 - # Assume it does not exist, and that is what the error was.
233 - #
234 - set reflog_msg "branch: Created from [$w_rev get]"
235 - set cur $null_sha1
236 - } elseif {$opt_merge eq {no}} {
237 - tk_messageBox \
238 - -icon error \
239 - -type ok \
240 - -title [wm title $w] \
241 - -parent $w \
242 - -message "Branch '$newbranch' already exists."
243 - focus $w_name
244 - return
245 - } else {
246 - set mrb {}
247 - catch {set mrb [git merge-base $new $cur]}
248 - switch -- $opt_merge {
249 - ff {
250 - if {$mrb eq $new} {
251 - # The current branch is actually newer.
252 - #
253 - set new $cur
254 - } elseif {$mrb eq $cur} {
255 - # The current branch is older.
256 - #
257 - set reflog_msg "merge [$w_rev get]: Fast-forward"
258 - } else {
259 - tk_messageBox \
260 - -icon error \
261 - -type ok \
262 - -title [wm title $w] \
263 - -parent $w \
264 - -message "Branch '$newbranch' already exists.\n\nIt cannot fast-forward to [$w_rev get].\nA merge is required."
265 - focus $w_name
266 - return
267 - }
268 - }
269 - reset {
270 - if {$mrb eq $cur} {
271 - # The current branch is older.
272 - #
273 - set reflog_msg "merge [$w_rev get]: Fast-forward"
274 - } else {
275 - # The current branch will lose things.
276 - #
277 - if {[_confirm_reset $this $newbranch $cur $new]} {
278 - set reflog_msg "reset [$w_rev get]"
279 - } else {
280 - return
281 - }
282 - }
283 - }
284 - default {
285 - tk_messageBox \
286 - -icon error \
287 - -type ok \
288 - -title [wm title $w] \
289 - -parent $w \
290 - -message "Branch '$newbranch' already exists."
291 - focus $w_name
292 - return
293 - }
294 - }
295 - }
296 -
297 - if {$new ne $cur} {
298 - if {[catch {
299 - git update-ref -m $reflog_msg $ref $new $cur
300 - } err]} {
301 - tk_messageBox \
302 - -icon error \
303 - -type ok \
304 - -title [wm title $w] \
305 - -parent $w \
306 - -message "Failed to create '$newbranch'.\n\n$err"
307 - return
308 - }
309 - }
310 -
311 - if {$cur eq $null_sha1} {
312 - lappend all_heads $newbranch
313 - set all_heads [lsort -uniq $all_heads]
314 - populate_branch_menu
315 - }
316 -
317 - destroy $w
318 - if {$opt_checkout} {
319 - switch_branch $newbranch
175 + set co [::checkout_op::new \
176 + [$w_rev get] \
177 + $new \
178 + refs/heads/$newbranch]
179 + $co parent $w
180 + $co enable_create 1
181 + $co enable_merge $opt_merge
182 + $co enable_checkout $opt_checkout
183 + if {$spec ne {} && $opt_fetch} {
184 + $co enable_fetch $spec
185 }
321 -}
322 -
323 -method _confirm_reset {newbranch cur new} {
324 - set reset_ok 0
325 - set gitk [list do_gitk [list $cur ^$new]]
326 -
327 - set c $w.confirm_reset
328 - toplevel $c
329 - wm title $c "Confirm Branch Reset"
330 - wm geometry $c "+[winfo rootx $w]+[winfo rooty $w]"
186
332 - pack [label $c.msg1 \
333 - -anchor w \
334 - -justify left \
335 - -text "Resetting '$newbranch' to [$w_rev get] will lose the following commits:" \
336 - ] -anchor w
337 -
338 - set list $c.list.l
339 - frame $c.list
340 - text $list \
341 - -font font_diff \
342 - -width 80 \
343 - -height 10 \
344 - -wrap none \
345 - -xscrollcommand [list $c.list.sbx set] \
346 - -yscrollcommand [list $c.list.sby set]
347 - scrollbar $c.list.sbx -orient h -command [list $list xview]
348 - scrollbar $c.list.sby -orient v -command [list $list yview]
349 - pack $c.list.sbx -fill x -side bottom
350 - pack $c.list.sby -fill y -side right
351 - pack $list -fill both -expand 1
352 - pack $c.list -fill both -expand 1 -padx 5 -pady 5
353 -
354 - pack [label $c.msg2 \
355 - -anchor w \
356 - -justify left \
357 - -text "Recovering lost commits may not be easy." \
358 - ]
359 - pack [label $c.msg3 \
360 - -anchor w \
361 - -justify left \
362 - -text "Reset '$newbranch'?" \
363 - ]
364 -
365 - frame $c.buttons
366 - button $c.buttons.visualize \
367 - -text Visualize \
368 - -command $gitk
369 - pack $c.buttons.visualize -side left
370 - button $c.buttons.reset \
371 - -text Reset \
372 - -command "
373 - set @reset_ok 1
374 - destroy $c
375 - "
376 - pack $c.buttons.reset -side right
377 - button $c.buttons.cancel \
378 - -default active \
379 - -text Cancel \
380 - -command [list destroy $c]
381 - pack $c.buttons.cancel -side right -padx 5
382 - pack $c.buttons -side bottom -fill x -pady 10 -padx 10
383 -
384 - set fd [open "| git rev-list --pretty=oneline $cur ^$new" r]
385 - while {[gets $fd line] > 0} {
386 - set abbr [string range $line 0 7]
387 - set subj [string range $line 41 end]
388 - $list insert end "$abbr $subj\n"
187 + if {[$co run]} {
188 + destroy $w
189 + } else {
190 + focus $w_name
191 }
390 - close $fd
391 - $list configure -state disabled
392 -
393 - bind $c <Key-v> $gitk
394 -
395 - bind $c <Visibility> "
396 - grab $c
397 - focus $c.buttons.cancel
398 - "
399 - bind $c <Key-Return> [list destroy $c]
400 - bind $c <Key-Escape> [list destroy $c]
401 - tkwait window $c
402 - return $reset_ok
192 }
193
194 method _validate {d S} {
lib/branch_delete.tcl
+2 -11
@@ -9,7 +9,7 @@ field w_check ; # revision picker for merge test
9 field w_delete ; # delete button
10
11 constructor dialog {} {
12 - global all_heads current_branch
12 + global current_branch
13
14 make_toplevel top w
15 wm title $top "[appname] ([reponame]): Delete Branch"
@@ -54,7 +54,7 @@ constructor dialog {} {
54 $w_check none {Always (Do not perform merge test.)}
55 pack $w.check -anchor nw -fill x -pady 5 -padx 5
56
57 - foreach h $all_heads {
57 + foreach h [load_all_heads] {
58 if {$h ne $current_branch} {
59 $w_heads insert end $h
60 }
@@ -79,8 +79,6 @@ method _select {} {
79 }
80
81 method _delete {} {
82 - global all_heads
83 -
82 if {[catch {set check_cmt [$w_check commit_or_die]}]} {
83 return
84 }
@@ -133,11 +131,6 @@ Delete the selected branches?}
131 set o [lindex $i 1]
132 if {[catch {git update-ref -d "refs/heads/$b" $o} err]} {
133 append failed " - $b: $err\n"
136 - } else {
137 - set x [lsearch -sorted -exact $all_heads $b]
138 - if {$x >= 0} {
139 - set all_heads [lreplace $all_heads $x $x]
140 - }
134 }
135 }
136
@@ -150,8 +143,6 @@ Delete the selected branches?}
143 -message "Failed to delete branches:\n$failed"
144 }
145
153 - set all_heads [lsort $all_heads]
154 - populate_branch_menu
146 destroy $w
147 }
148
lib/branch_rename.tcl
+3 -11
@@ -8,7 +8,7 @@ field oldname
8 field newname
9
10 constructor dialog {} {
11 - global all_heads current_branch
11 + global current_branch
12
13 make_toplevel top w
14 wm title $top "[appname] ([reponame]): Rename Branch"
@@ -34,7 +34,7 @@ constructor dialog {} {
34
35 frame $w.rename
36 label $w.rename.oldname_l -text {Branch:}
37 - eval tk_optionMenu $w.rename.oldname_m @oldname $all_heads
37 + eval tk_optionMenu $w.rename.oldname_m @oldname [load_all_heads]
38
39 label $w.rename.newname_l -text {New Name:}
40 entry $w.rename.newname_t \
@@ -64,7 +64,7 @@ constructor dialog {} {
64 }
65
66 method _rename {} {
67 - global all_heads current_branch
67 + global current_branch
68
69 if {$oldname eq {}} {
70 tk_messageBox \
@@ -118,14 +118,6 @@ method _rename {} {
118 return
119 }
120
121 - set oldidx [lsearch -exact -sorted $all_heads $oldname]
122 - if {$oldidx >= 0} {
123 - set all_heads [lreplace $all_heads $oldidx $oldidx]
124 - }
125 - lappend all_heads $newname
126 - set all_heads [lsort $all_heads]
127 - populate_branch_menu
128 -
121 if {$current_branch eq $oldname} {
122 set current_branch $newname
123 }
lib/checkout_op.tcl new
+569
@@ -0,0 +1,569 @@
1 +# git-gui commit checkout support
2 +# Copyright (C) 2007 Shawn Pearce
3 +
4 +class checkout_op {
5 +
6 +field w {}; # our window (if we have one)
7 +field w_cons {}; # embedded console window object
8 +
9 +field new_expr ; # expression the user saw/thinks this is
10 +field new_hash ; # commit SHA-1 we are switching to
11 +field new_ref ; # ref we are updating/creating
12 +
13 +field parent_w .; # window that started us
14 +field merge_type none; # type of merge to apply to existing branch
15 +field fetch_spec {}; # refetch tracking branch if used?
16 +field checkout 1; # actually checkout the branch?
17 +field create 0; # create the branch if it doesn't exist?
18 +
19 +field reset_ok 0; # did the user agree to reset?
20 +field fetch_ok 0; # did the fetch succeed?
21 +
22 +field update_old {}; # was the update-ref call deferred?
23 +field reflog_msg {}; # log message for the update-ref call
24 +
25 +constructor new {expr hash {ref {}}} {
26 + set new_expr $expr
27 + set new_hash $hash
28 + set new_ref $ref
29 +
30 + return $this
31 +}
32 +
33 +method parent {path} {
34 + set parent_w [winfo toplevel $path]
35 +}
36 +
37 +method enable_merge {type} {
38 + set merge_type $type
39 +}
40 +
41 +method enable_fetch {spec} {
42 + set fetch_spec $spec
43 +}
44 +
45 +method enable_checkout {co} {
46 + set checkout $co
47 +}
48 +
49 +method enable_create {co} {
50 + set create $co
51 +}
52 +
53 +method run {} {
54 + if {$fetch_spec ne {}} {
55 + global M1B
56 +
57 + # We were asked to refresh a single tracking branch
58 + # before we get to work. We should do that before we
59 + # consider any ref updating.
60 + #
61 + set fetch_ok 0
62 + set l_trck [lindex $fetch_spec 0]
63 + set remote [lindex $fetch_spec 1]
64 + set r_head [lindex $fetch_spec 2]
65 + regsub ^refs/heads/ $r_head {} r_name
66 +
67 + _toplevel $this {Refreshing Tracking Branch}
68 + set w_cons [::console::embed \
69 + $w.console \
70 + "Fetching $r_name from $remote"]
71 + pack $w.console -fill both -expand 1
72 + $w_cons exec \
73 + [list git fetch $remote +$r_head:$l_trck] \
74 + [cb _finish_fetch]
75 +
76 + bind $w <$M1B-Key-w> break
77 + bind $w <$M1B-Key-W> break
78 + bind $w <Visibility> "
79 + [list grab $w]
80 + [list focus $w]
81 + "
82 + wm protocol $w WM_DELETE_WINDOW [cb _noop]
83 + tkwait window $w
84 +
85 + if {!$fetch_ok} {
86 + delete_this
87 + return 0
88 + }
89 + }
90 +
91 + if {$new_ref ne {}} {
92 + # If we have a ref we need to update it before we can
93 + # proceed with a checkout (if one was enabled).
94 + #
95 + if {![_update_ref $this]} {
96 + delete_this
97 + return 0
98 + }
99 + }
100 +
101 + if {$checkout} {
102 + _checkout $this
103 + return 1
104 + }
105 +
106 + delete_this
107 + return 1
108 +}
109 +
110 +method _noop {} {}
111 +
112 +method _finish_fetch {ok} {
113 + if {$ok} {
114 + set l_trck [lindex $fetch_spec 0]
115 + if {[catch {set new_hash [git rev-parse --verify "$l_trck^0"]} err]} {
116 + set ok 0
117 + $w_cons insert "fatal: Cannot resolve $l_trck"
118 + $w_cons insert $err
119 + }
120 + }
121 +
122 + $w_cons done $ok
123 + set w_cons {}
124 + wm protocol $w WM_DELETE_WINDOW {}
125 +
126 + if {$ok} {
127 + destroy $w
128 + set w {}
129 + } else {
130 + button $w.close -text Close -command [list destroy $w]
131 + pack $w.close -side bottom -anchor e -padx 10 -pady 10
132 + }
133 +
134 + set fetch_ok $ok
135 +}
136 +
137 +method _update_ref {} {
138 + global null_sha1 current_branch
139 +
140 + set ref $new_ref
141 + set new $new_hash
142 +
143 + set is_current 0
144 + set rh refs/heads/
145 + set rn [string length $rh]
146 + if {[string equal -length $rn $rh $ref]} {
147 + set newbranch [string range $ref $rn end]
148 + if {$current_branch eq $newbranch} {
149 + set is_current 1
150 + }
151 + } else {
152 + set newbranch $ref
153 + }
154 +
155 + if {[catch {set cur [git rev-parse --verify "$ref^0"]}]} {
156 + # Assume it does not exist, and that is what the error was.
157 + #
158 + if {!$create} {
159 + _error $this "Branch '$newbranch' does not exist."
160 + return 0
161 + }
162 +
163 + set reflog_msg "branch: Created from $new_expr"
164 + set cur $null_sha1
165 + } elseif {$create && $merge_type eq {none}} {
166 + # We were told to create it, but not do a merge.
167 + # Bad. Name shouldn't have existed.
168 + #
169 + _error $this "Branch '$newbranch' already exists."
170 + return 0
171 + } elseif {!$create && $merge_type eq {none}} {
172 + # We aren't creating, it exists and we don't merge.
173 + # We are probably just a simple branch switch.
174 + # Use whatever value we just read.
175 + #
176 + set new $cur
177 + set new_hash $cur
178 + } elseif {$new eq $cur} {
179 + # No merge would be required, don't compute anything.
180 + #
181 + } else {
182 + set mrb {}
183 + catch {set mrb [git merge-base $new $cur]}
184 + switch -- $merge_type {
185 + ff {
186 + if {$mrb eq $new} {
187 + # The current branch is actually newer.
188 + #
189 + set new $cur
190 + } elseif {$mrb eq $cur} {
191 + # The current branch is older.
192 + #
193 + set reflog_msg "merge $new_expr: Fast-forward"
194 + } else {
195 + _error $this "Branch '$newbranch' already exists.\n\nIt cannot fast-forward to $new_expr.\nA merge is required."
196 + return 0
197 + }
198 + }
199 + reset {
200 + if {$mrb eq $cur} {
201 + # The current branch is older.
202 + #
203 + set reflog_msg "merge $new_expr: Fast-forward"
204 + } else {
205 + # The current branch will lose things.
206 + #
207 + if {[_confirm_reset $this $cur]} {
208 + set reflog_msg "reset $new_expr"
209 + } else {
210 + return 0
211 + }
212 + }
213 + }
214 + default {
215 + _error $this "Only 'ff' and 'reset' merge is currently supported."
216 + return 0
217 + }
218 + }
219 + }
220 +
221 + if {$new ne $cur} {
222 + if {$is_current} {
223 + # No so fast. We should defer this in case
224 + # we cannot update the working directory.
225 + #
226 + set update_old $cur
227 + return 1
228 + }
229 +
230 + if {[catch {
231 + git update-ref -m $reflog_msg $ref $new $cur
232 + } err]} {
233 + _error $this "Failed to update '$newbranch'.\n\n$err"
234 + return 0
235 + }
236 + }
237 +
238 + return 1
239 +}
240 +
241 +method _checkout {} {
242 + if {[lock_index checkout_op]} {
243 + after idle [cb _start_checkout]
244 + } else {
245 + _error $this "Index is already locked."
246 + delete_this
247 + }
248 +}
249 +
250 +method _start_checkout {} {
251 + global HEAD commit_type
252 +
253 + # -- Our in memory state should match the repository.
254 + #
255 + repository_state curType curHEAD curMERGE_HEAD
256 + if {[string match amend* $commit_type]
257 + && $curType eq {normal}
258 + && $curHEAD eq $HEAD} {
259 + } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
260 + info_popup {Last scanned state does not match repository state.
261 +
262 +Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed.
263 +
264 +The rescan will be automatically started now.
265 +}
266 + unlock_index
267 + rescan ui_ready
268 + delete_this
269 + return
270 + }
271 +
272 + if {[is_config_true gui.trustmtime]} {
273 + _readtree $this
274 + } else {
275 + ui_status {Refreshing file status...}
276 + set cmd [list git update-index]
277 + lappend cmd -q
278 + lappend cmd --unmerged
279 + lappend cmd --ignore-missing
280 + lappend cmd --refresh
281 + set fd [open "| $cmd" r]
282 + fconfigure $fd -blocking 0 -translation binary
283 + fileevent $fd readable [cb _refresh_wait $fd]
284 + }
285 +}
286 +
287 +method _refresh_wait {fd} {
288 + read $fd
289 + if {[eof $fd]} {
290 + close $fd
291 + _readtree $this
292 + }
293 +}
294 +
295 +method _name {} {
296 + if {$new_ref eq {}} {
297 + return [string range $new_hash 0 7]
298 + }
299 +
300 + set rh refs/heads/
301 + set rn [string length $rh]
302 + if {[string equal -length $rn $rh $new_ref]} {
303 + return [string range $new_ref $rn end]
304 + } else {
305 + return $new_ref
306 + }
307 +}
308 +
309 +method _readtree {} {
310 + global HEAD
311 +
312 + ui_status "Updating working directory to '[_name $this]'..."
313 + set cmd [list git read-tree]
314 + lappend cmd -m
315 + lappend cmd -u
316 + lappend cmd --exclude-per-directory=.gitignore
317 + lappend cmd $HEAD
318 + lappend cmd $new_hash
319 + set fd [open "| $cmd" r]
320 + fconfigure $fd -blocking 0 -translation binary
321 + fileevent $fd readable [cb _readtree_wait $fd]
322 +}
323 +
324 +method _readtree_wait {fd} {
325 + global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
326 + global current_branch is_detached
327 + global ui_comm
328 +
329 + # -- We never get interesting output on stdout; only stderr.
330 + #
331 + read $fd
332 + fconfigure $fd -blocking 1
333 + if {![eof $fd]} {
334 + fconfigure $fd -blocking 0
335 + return
336 + }
337 +
338 + set name [_name $this]
339 +
340 + # -- The working directory wasn't in sync with the index and
341 + # we'd have to overwrite something to make the switch. A
342 + # merge is required.
343 + #
344 + if {[catch {close $fd} err]} {
345 + regsub {^fatal: } $err {} err
346 + warn_popup "File level merge required.
347 +
348 +$err
349 +
350 +Staying on branch '$current_branch'."
351 + ui_status "Aborted checkout of '$name' (file level merging is required)."
352 + unlock_index
353 + delete_this
354 + return
355 + }
356 +
357 + set log "checkout: moving"
358 + if {!$is_detached} {
359 + append log " from $current_branch"
360 + }
361 +
362 + # -- Move/create HEAD as a symbolic ref. Core git does not
363 + # even check for failure here, it Just Works(tm). If it
364 + # doesn't we are in some really ugly state that is difficult
365 + # to recover from within git-gui.
366 + #
367 + set rh refs/heads/
368 + set rn [string length $rh]
369 + if {[string equal -length $rn $rh $new_ref]} {
370 + set new_branch [string range $new_ref $rn end]
371 + append log " to $new_branch"
372 +
373 + if {[catch {
374 + git symbolic-ref -m $log HEAD $new_ref
375 + } err]} {
376 + _fatal $this $err
377 + }
378 + set current_branch $new_branch
379 + set is_detached 0
380 + } else {
381 + append log " to $new_expr"
382 +
383 + if {[catch {
384 + _detach_HEAD $log $new_hash
385 + } err]} {
386 + _fatal $this $err
387 + }
388 + set current_branch HEAD
389 + set is_detached 1
390 + }
391 +
392 + # -- We had to defer updating the branch itself until we
393 + # knew the working directory would update. So now we
394 + # need to finish that work. If it fails we're in big
395 + # trouble.
396 + #
397 + if {$update_old ne {}} {
398 + if {[catch {
399 + git update-ref \
400 + -m $reflog_msg \
401 + $new_ref \
402 + $new_hash \
403 + $update_old
404 + } err]} {
405 + _fatal $this $err
406 + }
407 + }
408 +
409 + if {$is_detached} {
410 + info_popup "You are no longer on a local branch.
411 +
412 +If you wanted to be on a branch, create one now starting from 'This Detached Checkout'."
413 + }
414 +
415 + # -- Update our repository state. If we were previously in
416 + # amend mode we need to toss the current buffer and do a
417 + # full rescan to update our file lists. If we weren't in
418 + # amend mode our file lists are accurate and we can avoid
419 + # the rescan.
420 + #
421 + unlock_index
422 + set selected_commit_type new
423 + if {[string match amend* $commit_type]} {
424 + $ui_comm delete 0.0 end
425 + $ui_comm edit reset
426 + $ui_comm edit modified false
427 + rescan [list ui_status "Checked out '$name'."]
428 + } else {
429 + repository_state commit_type HEAD MERGE_HEAD
430 + set PARENT $HEAD
431 + ui_status "Checked out '$name'."
432 + }
433 + delete_this
434 +}
435 +
436 +git-version proc _detach_HEAD {log new} {
437 + >= 1.5.3 {
438 + git update-ref --no-deref -m $log HEAD $new
439 + }
440 + default {
441 + set p [gitdir HEAD]
442 + file delete $p
443 + set fd [open $p w]
444 + fconfigure $fd -translation lf -encoding utf-8
445 + puts $fd $new
446 + close $fd
447 + }
448 +}
449 +
450 +method _confirm_reset {cur} {
451 + set reset_ok 0
452 + set name [_name $this]
453 + set gitk [list do_gitk [list $cur ^$new_hash]]
454 +
455 + _toplevel $this {Confirm Branch Reset}
456 + pack [label $w.msg1 \
457 + -anchor w \
458 + -justify left \
459 + -text "Resetting '$name' to $new_expr will lose the following commits:" \
460 + ] -anchor w
461 +
462 + set list $w.list.l
463 + frame $w.list
464 + text $list \
465 + -font font_diff \
466 + -width 80 \
467 + -height 10 \
468 + -wrap none \
469 + -xscrollcommand [list $w.list.sbx set] \
470 + -yscrollcommand [list $w.list.sby set]
471 + scrollbar $w.list.sbx -orient h -command [list $list xview]
472 + scrollbar $w.list.sby -orient v -command [list $list yview]
473 + pack $w.list.sbx -fill x -side bottom
474 + pack $w.list.sby -fill y -side right
475 + pack $list -fill both -expand 1
476 + pack $w.list -fill both -expand 1 -padx 5 -pady 5
477 +
478 + pack [label $w.msg2 \
479 + -anchor w \
480 + -justify left \
481 + -text {Recovering lost commits may not be easy.} \
482 + ]
483 + pack [label $w.msg3 \
484 + -anchor w \
485 + -justify left \
486 + -text "Reset '$name'?" \
487 + ]
488 +
489 + frame $w.buttons
490 + button $w.buttons.visualize \
491 + -text Visualize \
492 + -command $gitk
493 + pack $w.buttons.visualize -side left
494 + button $w.buttons.reset \
495 + -text Reset \
496 + -command "
497 + set @reset_ok 1
498 + destroy $w
499 + "
500 + pack $w.buttons.reset -side right
501 + button $w.buttons.cancel \
502 + -default active \
503 + -text Cancel \
504 + -command [list destroy $w]
505 + pack $w.buttons.cancel -side right -padx 5
506 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
507 +
508 + set fd [open "| git rev-list --pretty=oneline $cur ^$new_hash" r]
509 + while {[gets $fd line] > 0} {
510 + set abbr [string range $line 0 7]
511 + set subj [string range $line 41 end]
512 + $list insert end "$abbr $subj\n"
513 + }
514 + close $fd
515 + $list configure -state disabled
516 +
517 + bind $w <Key-v> $gitk
518 + bind $w <Visibility> "
519 + grab $w
520 + focus $w.buttons.cancel
521 + "
522 + bind $w <Key-Return> [list destroy $w]
523 + bind $w <Key-Escape> [list destroy $w]
524 + tkwait window $w
525 + return $reset_ok
526 +}
527 +
528 +method _error {msg} {
529 + if {[winfo ismapped $parent_w]} {
530 + set p $parent_w
531 + } else {
532 + set p .
533 + }
534 +
535 + tk_messageBox \
536 + -icon error \
537 + -type ok \
538 + -title [wm title $p] \
539 + -parent $p \
540 + -message $msg
541 +}
542 +
543 +method _toplevel {title} {
544 + regsub -all {::} $this {__} w
545 + set w .$w
546 +
547 + if {[winfo ismapped $parent_w]} {
548 + set p $parent_w
549 + } else {
550 + set p .
551 + }
552 +
553 + toplevel $w
554 + wm title $w $title
555 + wm geometry $w "+[winfo rootx $p]+[winfo rooty $p]"
556 +}
557 +
558 +method _fatal {err} {
559 + error_popup "Failed to set current branch.
560 +
561 +This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
562 +
563 +This should not have occurred. [appname] will now close and give up.
564 +
565 +$err"
566 + exit 1
567 +}
568 +
569 +}
lib/choose_rev.tcl
+36 -7
@@ -18,7 +18,7 @@ field spec_trck ; # list of all tracking branch specs
18 field spec_tag ; # list of all tag specs
19
20 constructor new {path {title {}}} {
21 - global all_heads current_branch
21 + global current_branch is_detached
22
23 set w $path
24
@@ -29,6 +29,15 @@ constructor new {path {title {}}} {
29 }
30 bind $w <Destroy> [cb _delete %W]
31
32 + if {$is_detached} {
33 + radiobutton $w.detachedhead_r \
34 + -anchor w \
35 + -text {This Detached Checkout} \
36 + -value HEAD \
37 + -variable @revtype
38 + grid $w.detachedhead_r -sticky we -padx {0 5} -columnspan 2
39 + }
40 +
41 radiobutton $w.expr_r \
42 -text {Revision Expression:} \
43 -value expr \
@@ -86,14 +95,18 @@ constructor new {path {title {}}} {
95 grid $w.list -sticky nswe -padx {20 5} -columnspan 2
96
97 grid columnconfigure $w 1 -weight 1
89 - grid rowconfigure $w 2 -weight 1
98 + if {$is_detached} {
99 + grid rowconfigure $w 3 -weight 1
100 + } else {
101 + grid rowconfigure $w 2 -weight 1
102 + }
103
104 trace add variable @revtype write [cb _select]
105 bind $w_filter <Key-Return> [list focus $w_list]\;break
106 bind $w_filter <Key-Down> [list focus $w_list]
107
108 set spec_head [list]
96 - foreach name $all_heads {
109 + foreach name [load_all_heads] {
110 lappend spec_head [list $name refs/heads/$name]
111 }
112
@@ -109,7 +122,8 @@ constructor new {path {title {}}} {
122 lappend spec_tag [list $name refs/tags/$name]
123 }
124
112 - if {[llength $spec_head] > 0} { set revtype head
125 + if {$is_detached} { set revtype HEAD
126 + } elseif {[llength $spec_head] > 0} { set revtype head
127 } elseif {[llength $spec_trck] > 0} { set revtype trck
128 } elseif {[llength $spec_tag ] > 0} { set revtype tag
129 } else { set revtype expr
@@ -153,6 +167,7 @@ method get {} {
167 }
168 }
169
170 + HEAD { return HEAD }
171 expr { return $c_expr }
172 none { return {} }
173 default { error "unknown type of revision" }
@@ -163,6 +178,20 @@ method pick_tracking_branch {} {
178 set revtype trck
179 }
180
181 +method focus_filter {} {
182 + if {[$w_filter cget -state] eq {normal}} {
183 + focus $w_filter
184 + }
185 +}
186 +
187 +method get_local_branch {} {
188 + if {$revtype eq {head}} {
189 + return [_expr $this]
190 + } else {
191 + return {}
192 + }
193 +}
194 +
195 method get_tracking_branch {} {
196 set i [$w_list curselection]
197 if {$i eq {} || $revtype ne {trck}} {
@@ -222,6 +251,7 @@ method _expr {} {
251 error "Revision expression is empty."
252 }
253 }
254 + HEAD { return HEAD }
255 none { return {} }
256 default { error "unknown type of revision" }
257 }
@@ -249,9 +279,7 @@ method _filter {P} {
279
280 method _select {args} {
281 _rebuild $this $filter
252 - if {[$w_filter cget -state] eq {normal}} {
253 - focus $w_filter
254 - }
282 + focus_filter $this
283 }
284
285 method _rebuild {pat} {
@@ -261,6 +289,7 @@ method _rebuild {pat} {
289 trck { set new $spec_trck }
290 tag { set new $spec_tag }
291 expr -
292 + HEAD -
293 none {
294 set new [list]
295 set ste disabled
lib/commit.tcl
-8
@@ -359,14 +359,6 @@ A rescan will be automatically started now.
359
360 if {[is_enabled singlecommit]} do_quit
361
362 - # -- Make sure our current branch exists.
363 - #
364 - if {$commit_type eq {initial}} {
365 - lappend all_heads $current_branch
366 - set all_heads [lsort -unique $all_heads]
367 - populate_branch_menu
368 - }
369 -
362 # -- Update in memory status
363 #
364 set selected_commit_type new
lib/console.tcl
+8
@@ -173,6 +173,14 @@ method chain {cmdlist {ok 1}} {
173 }
174 }
175
176 +method insert {txt} {
177 + if {![winfo exists $w.m.t]} {_init $this}
178 + $w.m.t conf -state normal
179 + $w.m.t insert end "$txt\n"
180 + set console_cr [$w.m.t index {end -1c}]
181 + $w.m.t conf -state disabled
182 +}
183 +
184 method done {ok} {
185 if {$ok} {
186 if {[winfo exists $w.m.s]} {
lib/transport.tcl
+2 -2
@@ -74,7 +74,7 @@ trace add variable push_remote write \
74 [list radio_selector push_urltype remote]
75
76 proc do_push_anywhere {} {
77 - global all_heads all_remotes current_branch
77 + global all_remotes current_branch
78 global push_urltype push_remote push_url push_thin push_tags
79
80 set w .push_setup
@@ -101,7 +101,7 @@ proc do_push_anywhere {} {
101 -width 70 \
102 -selectmode extended \
103 -yscrollcommand [list $w.source.sby set]
104 - foreach h $all_heads {
104 + foreach h [load_all_heads] {
105 $w.source.l insert end $h
106 if {$h eq $current_branch} {
107 $w.source.l select set end