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 fd
[git_read update-index
\
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 fd
[git_read
--stderr read-tree
\
322 --exclude-per
-directory
=.gitignore
\
326 fconfigure $fd -blocking 0 -translation binary
327 fileevent $fd readable
[cb _readtree_wait
$fd]
330 method _readtree_wait
{fd
} {
331 global current_branch
334 $::main_status update_meter
$buf
335 append readtree_d
$buf
337 fconfigure $fd -blocking 1
339 fconfigure $fd -blocking 0
343 if {[catch {close $fd}]} {
345 regsub {^fatal
: } $err {} err
346 $::main_status stop
"Aborted checkout of '[_name $this]' (file level merging is required)."
347 warn_popup
"File level merge required.
351 Staying on branch '$current_branch'."
358 _after_readtree
$this
361 method _after_readtree
{} {
362 global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
363 global current_branch is_detached
366 set name
[_name
$this]
367 set log
"checkout: moving"
369 append log
" from $current_branch"
372 # -- Move/create HEAD as a symbolic ref. Core git does not
373 # even check for failure here, it Just Works(tm). If it
374 # doesn't we are in some really ugly state that is difficult
375 # to recover from within git-gui.
378 set rn
[string length
$rh]
379 if {[string equal
-length $rn $rh $new_ref]} {
380 set new_branch
[string range
$new_ref $rn end
]
381 append log
" to $new_branch"
384 git symbolic-ref
-m $log HEAD
$new_ref
388 set current_branch
$new_branch
391 append log
" to $new_expr"
394 _detach_HEAD
$log $new_hash
398 set current_branch HEAD
402 # -- We had to defer updating the branch itself until we
403 # knew the working directory would update. So now we
404 # need to finish that work. If it fails we're in big
407 if {$update_old ne
{}} {
420 info_popup
"You are no longer on a local branch.
422 If you wanted to be on a branch, create one now starting from 'This Detached Checkout'."
425 # -- Update our repository state. If we were previously in
426 # amend mode we need to toss the current buffer and do a
427 # full rescan to update our file lists. If we weren't in
428 # amend mode our file lists are accurate and we can avoid
432 set selected_commit_type new
433 if {[string match amend
* $commit_type]} {
434 $ui_comm delete
0.0 end
436 $ui_comm edit modified false
437 rescan
[list ui_status
"Checked out '$name'."]
439 repository_state commit_type HEAD MERGE_HEAD
441 ui_status
"Checked out '$name'."
446 git-version
proc _detach_HEAD
{log new
} {
448 git update-ref
--no-deref
-m $log HEAD
$new
454 fconfigure $fd -translation lf
-encoding utf-8
460 method _confirm_reset
{cur
} {
462 set name
[_name
$this]
463 set gitk
[list do_gitk
[list $cur ^
$new_hash]]
465 _toplevel
$this {Confirm Branch Reset
}
466 pack [label $w.msg1
\
469 -text "Resetting '$name' to $new_expr will lose the following commits:" \
479 -xscrollcommand [list $w.
list.sbx
set] \
480 -yscrollcommand [list $w.
list.sby
set]
481 scrollbar $w.
list.sbx
-orient h
-command [list $list xview
]
482 scrollbar $w.
list.sby
-orient v
-command [list $list yview
]
483 pack $w.
list.sbx
-fill x
-side bottom
484 pack $w.
list.sby
-fill y
-side right
485 pack $list -fill both
-expand 1
486 pack $w.
list -fill both
-expand 1 -padx 5 -pady 5
488 pack [label $w.msg2
\
491 -text {Recovering lost commits may not be easy.
} \
493 pack [label $w.msg3
\
496 -text "Reset '$name'?" \
500 button $w.buttons.visualize
\
503 pack $w.buttons.visualize
-side left
504 button $w.buttons.reset
\
510 pack $w.buttons.reset
-side right
511 button $w.buttons.cancel
\
514 -command [list destroy $w]
515 pack $w.buttons.cancel
-side right
-padx 5
516 pack $w.buttons
-side bottom
-fill x
-pady 10 -padx 10
518 set fd
[git_read rev-list
--pretty=oneline
$cur ^
$new_hash]
519 while {[gets $fd line
] > 0} {
520 set abbr
[string range
$line 0 7]
521 set subj
[string range
$line 41 end
]
522 $list insert end
"$abbr $subj\n"
525 $list configure
-state disabled
527 bind $w <Key-v
> $gitk
528 bind $w <Visibility
> "
530 focus $w.buttons.cancel
532 bind $w <Key-Return
> [list destroy $w]
533 bind $w <Key-Escape
> [list destroy $w]
538 method _error
{msg
} {
539 if {[winfo ismapped
$parent_w]} {
548 -title [wm title
$p] \
553 method _toplevel
{title
} {
554 regsub -all {::} $this {__
} w
557 if {[winfo ismapped
$parent_w]} {
565 wm geometry
$w "+[winfo rootx $p]+[winfo rooty $p]"
568 method _fatal
{err
} {
569 error_popup
"Failed to set current branch.
571 This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
573 This should not have occurred. [appname] will now close and give up.