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 merge_base
{}; # merge base if we have another ref involved
16 field fetch_spec
{}; # refetch tracking branch if used?
17 field checkout
1; # actually checkout the branch?
18 field create
0; # create the branch if it doesn't exist?
20 field reset_ok
0; # did the user agree to reset?
21 field fetch_ok
0; # did the fetch succeed?
23 field readtree_d
{}; # buffered output from read-tree
24 field update_old
{}; # was the update-ref call deferred?
25 field reflog_msg
{}; # log message for the update-ref call
27 constructor new
{expr hash
{ref
{}}} {
35 method parent
{path
} {
36 set parent_w
[winfo toplevel $path]
39 method enable_merge
{type
} {
43 method enable_fetch
{spec
} {
47 method enable_checkout
{co
} {
51 method enable_create
{co
} {
56 if {$fetch_spec ne
{}} {
59 # We were asked to refresh a single tracking branch
60 # before we get to work. We should do that before we
61 # consider any ref updating.
64 set l_trck
[lindex $fetch_spec 0]
65 set remote
[lindex $fetch_spec 1]
66 set r_head
[lindex $fetch_spec 2]
67 regsub ^refs
/heads
/ $r_head {} r_name
69 _toplevel
$this {Refreshing Tracking Branch
}
70 set w_cons
[::console::embed \
72 "Fetching $r_name from $remote"]
73 pack $w.
console -fill both
-expand 1
75 [list git fetch
$remote +$r_head:$l_trck] \
78 bind $w <$M1B-Key
-w
> break
79 bind $w <$M1B-Key
-W
> break
80 bind $w <Visibility
> "
84 wm protocol
$w WM_DELETE_WINDOW
[cb _noop
]
94 # If we have a ref we need to update it before we can
95 # proceed with a checkout (if one was enabled).
97 if {![_update_ref
$this]} {
114 method _finish_fetch
{ok
} {
116 set l_trck
[lindex $fetch_spec 0]
117 if {[catch {set new_hash
[git rev-parse
--verify "$l_trck^0"]} err
]} {
119 $w_cons insert
"fatal: Cannot resolve $l_trck"
126 wm protocol
$w WM_DELETE_WINDOW
{}
132 button $w.
close -text Close
-command [list destroy $w]
133 pack $w.
close -side bottom
-anchor e
-padx 10 -pady 10
139 method _update_ref
{} {
140 global null_sha1 current_branch
147 set rn
[string length
$rh]
148 if {[string equal
-length $rn $rh $ref]} {
149 set newbranch
[string range
$ref $rn end
]
150 if {$current_branch eq
$newbranch} {
157 if {[catch {set cur
[git rev-parse
--verify "$ref^0"]}]} {
158 # Assume it does not exist, and that is what the error was.
161 _error
$this "Branch '$newbranch' does not exist."
165 set reflog_msg
"branch: Created from $new_expr"
167 } elseif
{$create && $merge_type eq
{none
}} {
168 # We were told to create it, but not do a merge.
169 # Bad. Name shouldn't have existed.
171 _error
$this "Branch '$newbranch' already exists."
173 } elseif
{!$create && $merge_type eq
{none
}} {
174 # We aren't creating, it exists and we don't merge.
175 # We are probably just a simple branch switch.
176 # Use whatever value we just read.
180 } elseif
{$new eq
$cur} {
181 # No merge would be required, don't compute anything.
184 catch {set merge_base
[git merge-base
$new $cur]}
185 if {$merge_base eq
$cur} {
186 # The current branch is older.
188 set reflog_msg
"merge $new_expr: Fast-forward"
190 switch -- $merge_type {
192 if {$merge_base eq
$new} {
193 # The current branch is actually newer.
197 _error
$this "Branch '$newbranch' already exists.\n\nIt cannot fast-forward to $new_expr.\nA merge is required."
202 # The current branch will lose things.
204 if {[_confirm_reset
$this $cur]} {
205 set reflog_msg
"reset $new_expr"
211 _error
$this "Only 'ff' and 'reset' merge is currently supported."
220 # No so fast. We should defer this in case
221 # we cannot update the working directory.
228 git update-ref
-m $reflog_msg $ref $new $cur
230 _error
$this "Failed to update '$newbranch'.\n\n$err"
238 method _checkout
{} {
239 if {[lock_index checkout_op
]} {
240 after idle
[cb _start_checkout
]
242 _error
$this "Index is already locked."
247 method _start_checkout
{} {
248 global HEAD commit_type
250 # -- Our in memory state should match the repository.
252 repository_state curType curHEAD curMERGE_HEAD
253 if {[string match amend
* $commit_type]
254 && $curType eq
{normal
}
255 && $curHEAD eq
$HEAD} {
256 } elseif
{$commit_type ne
$curType ||
$HEAD ne
$curHEAD} {
257 info_popup
{Last scanned state does not match repository state.
259 Another Git program has modified this repository since the last
scan. A rescan must be performed before the current branch can be changed.
261 The rescan will be automatically started now.
269 if {[is_config_true gui.trustmtime
]} {
272 ui_status
{Refreshing
file status...
}
273 set fd
[git_read update-index
\
279 fconfigure $fd -blocking 0 -translation binary
280 fileevent $fd readable
[cb _refresh_wait
$fd]
284 method _refresh_wait
{fd
} {
293 if {$new_ref eq
{}} {
294 return [string range
$new_hash 0 7]
298 set rn
[string length
$rh]
299 if {[string equal
-length $rn $rh $new_ref]} {
300 return [string range
$new_ref $rn end
]
306 method _readtree
{} {
310 $::main_status start
\
311 "Updating working directory to '[_name $this]'..." \
314 set fd
[git_read
--stderr read-tree
\
318 --exclude-per
-directory
=.gitignore
\
322 fconfigure $fd -blocking 0 -translation binary
323 fileevent $fd readable
[cb _readtree_wait
$fd]
326 method _readtree_wait
{fd
} {
327 global current_branch
330 $::main_status update_meter
$buf
331 append readtree_d
$buf
333 fconfigure $fd -blocking 1
335 fconfigure $fd -blocking 0
339 if {[catch {close $fd}]} {
341 regsub {^fatal
: } $err {} err
342 $::main_status stop
"Aborted checkout of '[_name $this]' (file level merging is required)."
343 warn_popup
"File level merge required.
347 Staying on branch '$current_branch'."
354 _after_readtree
$this
357 method _after_readtree
{} {
358 global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
359 global current_branch is_detached
362 set name
[_name
$this]
363 set log
"checkout: moving"
365 append log
" from $current_branch"
368 # -- Move/create HEAD as a symbolic ref. Core git does not
369 # even check for failure here, it Just Works(tm). If it
370 # doesn't we are in some really ugly state that is difficult
371 # to recover from within git-gui.
374 set rn
[string length
$rh]
375 if {[string equal
-length $rn $rh $new_ref]} {
376 set new_branch
[string range
$new_ref $rn end
]
377 append log
" to $new_branch"
380 git symbolic-ref
-m $log HEAD
$new_ref
384 set current_branch
$new_branch
387 append log
" to $new_expr"
390 _detach_HEAD
$log $new_hash
394 set current_branch HEAD
398 # -- We had to defer updating the branch itself until we
399 # knew the working directory would update. So now we
400 # need to finish that work. If it fails we're in big
403 if {$update_old ne
{}} {
416 info_popup
"You are no longer on a local branch.
418 If you wanted to be on a branch, create one now starting from 'This Detached Checkout'."
421 # -- Update our repository state. If we were previously in
422 # amend mode we need to toss the current buffer and do a
423 # full rescan to update our file lists. If we weren't in
424 # amend mode our file lists are accurate and we can avoid
428 set selected_commit_type new
429 if {[string match amend
* $commit_type]} {
430 $ui_comm delete
0.0 end
432 $ui_comm edit modified false
433 rescan
[list ui_status
"Checked out '$name'."]
435 repository_state commit_type HEAD MERGE_HEAD
437 ui_status
"Checked out '$name'."
442 git-version
proc _detach_HEAD
{log new
} {
444 git update-ref
--no-deref
-m $log HEAD
$new
450 fconfigure $fd -translation lf
-encoding utf-8
456 method _confirm_reset
{cur
} {
458 set name
[_name
$this]
459 set gitk
[list do_gitk
[list $cur ^
$new_hash]]
461 _toplevel
$this {Confirm Branch Reset
}
462 pack [label $w.msg1
\
465 -text "Resetting '$name' to $new_expr will lose the following commits:" \
475 -xscrollcommand [list $w.
list.sbx
set] \
476 -yscrollcommand [list $w.
list.sby
set]
477 scrollbar $w.
list.sbx
-orient h
-command [list $list xview
]
478 scrollbar $w.
list.sby
-orient v
-command [list $list yview
]
479 pack $w.
list.sbx
-fill x
-side bottom
480 pack $w.
list.sby
-fill y
-side right
481 pack $list -fill both
-expand 1
482 pack $w.
list -fill both
-expand 1 -padx 5 -pady 5
484 pack [label $w.msg2
\
487 -text {Recovering lost commits may not be easy.
} \
489 pack [label $w.msg3
\
492 -text "Reset '$name'?" \
496 button $w.buttons.visualize
\
499 pack $w.buttons.visualize
-side left
500 button $w.buttons.reset
\
506 pack $w.buttons.reset
-side right
507 button $w.buttons.cancel
\
510 -command [list destroy $w]
511 pack $w.buttons.cancel
-side right
-padx 5
512 pack $w.buttons
-side bottom
-fill x
-pady 10 -padx 10
514 set fd
[git_read rev-list
--pretty=oneline
$cur ^
$new_hash]
515 while {[gets $fd line
] > 0} {
516 set abbr
[string range
$line 0 7]
517 set subj
[string range
$line 41 end
]
518 $list insert end
"$abbr $subj\n"
521 $list configure
-state disabled
523 bind $w <Key-v
> $gitk
524 bind $w <Visibility
> "
526 focus $w.buttons.cancel
528 bind $w <Key-Return
> [list destroy $w]
529 bind $w <Key-Escape
> [list destroy $w]
534 method _error
{msg
} {
535 if {[winfo ismapped
$parent_w]} {
544 -title [wm title
$p] \
549 method _toplevel
{title
} {
550 regsub -all {::} $this {__
} w
553 if {[winfo ismapped
$parent_w]} {
561 wm geometry
$w "+[winfo rootx $p]+[winfo rooty $p]"
564 method _fatal
{err
} {
565 error_popup
"Failed to set current branch.
567 This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
569 This should not have occurred. [appname] will now close and give up.