1 # git-gui commit checkout support
2 # Copyright (C) 2007 Shawn Pearce
6 field w {}; # our window (if we have one)
7 field w_cons {}; # embedded console window object
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
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?
19 field reset_ok 0; # did the user agree to reset?
20 field fetch_ok 0; # did the fetch succeed?
22 field readtree_d {}; # buffered output from read-tree
23 field update_old {}; # was the update-ref call deferred?
24 field reflog_msg {}; # log message for the update-ref call
26 constructor new {expr hash {ref {}}} {
34 method parent {path} {
35 set parent_w [winfo toplevel $path]
38 method enable_merge {type} {
42 method enable_fetch {spec} {
46 method enable_checkout {co} {
50 method enable_create {co} {
55 if {$fetch_spec ne {}} {
58 # We were asked to refresh a single tracking branch
59 # before we get to work. We should do that before we
60 # consider any ref updating.
63 set l_trck [lindex $fetch_spec 0]
64 set remote [lindex $fetch_spec 1]
65 set r_head [lindex $fetch_spec 2]
66 regsub ^refs/heads/ $r_head {} r_name
68 _toplevel $this {Refreshing Tracking Branch}
69 set w_cons [::console::embed \
71 "Fetching $r_name from $remote"]
72 pack $w.console -fill both -expand 1
74 [list git fetch $remote +$r_head:$l_trck] \
77 bind $w <$M1B-Key-w> break
78 bind $w <$M1B-Key-W> break
79 bind $w <Visibility> "
83 wm protocol $w WM_DELETE_WINDOW [cb _noop]
93 # If we have a ref we need to update it before we can
94 # proceed with a checkout (if one was enabled).
96 if {![_update_ref $this]} {
113 method _finish_fetch {ok} {
115 set l_trck [lindex $fetch_spec 0]
116 if {[catch {set new_hash [git rev-parse --verify "$l_trck^0"]} err]} {
118 $w_cons insert "fatal: Cannot resolve $l_trck"
125 wm protocol $w WM_DELETE_WINDOW {}
131 button $w.close -text Close -command [list destroy $w]
132 pack $w.close -side bottom -anchor e -padx 10 -pady 10
138 method _update_ref {} {
139 global null_sha1 current_branch
146 set rn [string length $rh]
147 if {[string equal -length $rn $rh $ref]} {
148 set newbranch [string range $ref $rn end]
149 if {$current_branch eq $newbranch} {
156 if {[catch {set cur [git rev-parse --verify "$ref^0"]}]} {
157 # Assume it does not exist, and that is what the error was.
160 _error $this "Branch '$newbranch' does not exist."
164 set reflog_msg "branch: Created from $new_expr"
166 } elseif {$create && $merge_type eq {none}} {
167 # We were told to create it, but not do a merge.
168 # Bad. Name shouldn't have existed.
170 _error $this "Branch '$newbranch' already exists."
172 } elseif {!$create && $merge_type eq {none}} {
173 # We aren't creating, it exists and we don't merge.
174 # We are probably just a simple branch switch.
175 # Use whatever value we just read.
179 } elseif {$new eq $cur} {
180 # No merge would be required, don't compute anything.
184 catch {set mrb [git merge-base $new $cur]}
185 switch -- $merge_type {
188 # The current branch is actually newer.
191 } elseif {$mrb eq $cur} {
192 # The current branch is older.
194 set reflog_msg "merge $new_expr: Fast-forward"
196 _error $this "Branch '$newbranch' already exists.\n\nIt cannot fast-forward to $new_expr.\nA merge is required."
202 # The current branch is older.
204 set reflog_msg "merge $new_expr: Fast-forward"
206 # The current branch will lose things.
208 if {[_confirm_reset $this $cur]} {
209 set reflog_msg "reset $new_expr"
216 _error $this "Only 'ff' and 'reset' merge is currently supported."
224 # No so fast. We should defer this in case
225 # we cannot update the working directory.
232 git update-ref -m $reflog_msg $ref $new $cur
234 _error $this "Failed to update '$newbranch'.\n\n$err"
242 method _checkout {} {
243 if {[lock_index checkout_op]} {
244 after idle [cb _start_checkout]
246 _error $this "Index is already locked."
251 method _start_checkout {} {
252 global HEAD commit_type
254 # -- Our in memory state should match the repository.
256 repository_state curType curHEAD curMERGE_HEAD
257 if {[string match amend* $commit_type]
258 && $curType eq {normal}
259 && $curHEAD eq $HEAD} {
260 } elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
261 info_popup {Last scanned state does not match repository state.
263 Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed.
265 The rescan will be automatically started now.
273 if {[is_config_true gui.trustmtime]} {
276 ui_status {Refreshing file status...}
277 set cmd [list git update-index]
279 lappend cmd --unmerged
280 lappend cmd --ignore-missing
281 lappend cmd --refresh
282 set fd [open "| $cmd" r]
283 fconfigure $fd -blocking 0 -translation binary
284 fileevent $fd readable [cb _refresh_wait $fd]
288 method _refresh_wait {fd} {
297 if {$new_ref eq {}} {
298 return [string range $new_hash 0 7]
302 set rn [string length $rh]
303 if {[string equal -length $rn $rh $new_ref]} {
304 return [string range $new_ref $rn end]
310 method _readtree {} {
314 $::main_status start \
315 "Updating working directory to '[_name $this]'..." \
318 set cmd [list git read-tree]
322 lappend cmd --exclude-per-directory=.gitignore
324 lappend cmd $new_hash
327 set fd [open "| $cmd 2>@1" r]
329 # Older versions of Tcl 8.4 don't have this 2>@1 IO
330 # redirect operator. Fallback to |& cat for those.
332 set fd [open "| $cmd |& cat" r]
335 fconfigure $fd -blocking 0 -translation binary
336 fileevent $fd readable [cb _readtree_wait $fd]
339 method _readtree_wait {fd} {
340 global current_branch
343 $::main_status update_meter $buf
344 append readtree_d $buf
346 fconfigure $fd -blocking 1
348 fconfigure $fd -blocking 0
352 if {[catch {close $fd}]} {
354 regsub {^fatal: } $err {} err
355 $::main_status stop "Aborted checkout of '[_name $this]' (file level merging is required)."
356 warn_popup "File level merge required.
360 Staying on branch '$current_branch'."
367 _after_readtree $this
370 method _after_readtree {} {
371 global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
372 global current_branch is_detached
375 set name [_name $this]
376 set log "checkout: moving"
378 append log " from $current_branch"
381 # -- Move/create HEAD as a symbolic ref. Core git does not
382 # even check for failure here, it Just Works(tm). If it
383 # doesn't we are in some really ugly state that is difficult
384 # to recover from within git-gui.
387 set rn [string length $rh]
388 if {[string equal -length $rn $rh $new_ref]} {
389 set new_branch [string range $new_ref $rn end]
390 append log " to $new_branch"
393 git symbolic-ref -m $log HEAD $new_ref
397 set current_branch $new_branch
400 append log " to $new_expr"
403 _detach_HEAD $log $new_hash
407 set current_branch HEAD
411 # -- We had to defer updating the branch itself until we
412 # knew the working directory would update. So now we
413 # need to finish that work. If it fails we're in big
416 if {$update_old ne {}} {
429 info_popup "You are no longer on a local branch.
431 If you wanted to be on a branch, create one now starting from 'This Detached Checkout'."
434 # -- Update our repository state. If we were previously in
435 # amend mode we need to toss the current buffer and do a
436 # full rescan to update our file lists. If we weren't in
437 # amend mode our file lists are accurate and we can avoid
441 set selected_commit_type new
442 if {[string match amend* $commit_type]} {
443 $ui_comm delete 0.0 end
445 $ui_comm edit modified false
446 rescan [list ui_status "Checked out '$name'."]
448 repository_state commit_type HEAD MERGE_HEAD
450 ui_status "Checked out '$name'."
455 git-version proc _detach_HEAD {log new} {
457 git update-ref --no-deref -m $log HEAD $new
463 fconfigure $fd -translation lf -encoding utf-8
469 method _confirm_reset {cur} {
471 set name [_name $this]
472 set gitk [list do_gitk [list $cur ^$new_hash]]
474 _toplevel $this {Confirm Branch Reset}
475 pack [label $w.msg1 \
478 -text "Resetting '$name' to $new_expr will lose the following commits:" \
488 -xscrollcommand [list $w.list.sbx set] \
489 -yscrollcommand [list $w.list.sby set]
490 scrollbar $w.list.sbx -orient h -command [list $list xview]
491 scrollbar $w.list.sby -orient v -command [list $list yview]
492 pack $w.list.sbx -fill x -side bottom
493 pack $w.list.sby -fill y -side right
494 pack $list -fill both -expand 1
495 pack $w.list -fill both -expand 1 -padx 5 -pady 5
497 pack [label $w.msg2 \
500 -text {Recovering lost commits may not be easy.} \
502 pack [label $w.msg3 \
505 -text "Reset '$name'?" \
509 button $w.buttons.visualize \
512 pack $w.buttons.visualize -side left
513 button $w.buttons.reset \
519 pack $w.buttons.reset -side right
520 button $w.buttons.cancel \
523 -command [list destroy $w]
524 pack $w.buttons.cancel -side right -padx 5
525 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
527 set fd [open "| git rev-list --pretty=oneline $cur ^$new_hash" r]
528 while {[gets $fd line] > 0} {
529 set abbr [string range $line 0 7]
530 set subj [string range $line 41 end]
531 $list insert end "$abbr $subj\n"
534 $list configure -state disabled
536 bind $w <Key-v> $gitk
537 bind $w <Visibility> "
539 focus $w.buttons.cancel
541 bind $w <Key-Return> [list destroy $w]
542 bind $w <Key-Escape> [list destroy $w]
547 method _error {msg} {
548 if {[winfo ismapped $parent_w]} {
557 -title [wm title $p] \
562 method _toplevel {title} {
563 regsub -all {::} $this {__} w
566 if {[winfo ismapped $parent_w]} {
574 wm geometry $w "+[winfo rootx $p]+[winfo rooty $p]"
577 method _fatal {err} {
578 error_popup "Failed to set current branch.
580 This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
582 This should not have occurred. [appname] will now close and give up.