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?
19 field remote_source
{}; # same as fetch_spec, to setup tracking
21 field reset_ok
0; # did the user agree to reset?
22 field fetch_ok
0; # did the fetch succeed?
24 field readtree_d
{}; # buffered output from read-tree
25 field update_old
{}; # was the update-ref call deferred?
26 field reflog_msg
{}; # log message for the update-ref call
28 constructor new
{expr hash
{ref
{}}} {
36 method parent
{path
} {
37 set parent_w
[winfo toplevel $path]
40 method enable_merge
{type
} {
44 method enable_fetch
{spec
} {
48 method remote_source
{spec
} {
49 set remote_source
$spec
52 method enable_checkout
{co
} {
56 method enable_create
{co
} {
61 if {$fetch_spec ne
{}} {
64 # We were asked to refresh a single tracking branch
65 # before we get to work. We should do that before we
66 # consider any ref updating.
69 set l_trck
[lindex $fetch_spec 0]
70 set remote
[lindex $fetch_spec 1]
71 set r_head
[lindex $fetch_spec 2]
72 regsub ^refs
/heads
/ $r_head {} r_name
74 set cmd
[list git fetch
$remote]
76 lappend cmd
+$r_head:$l_trck
81 _toplevel
$this {Refreshing Tracking Branch
}
82 set w_cons
[::console::embed \
84 [mc
"Fetching %s from %s" $r_name $remote]]
85 pack $w.
console -fill both
-expand 1
86 $w_cons exec $cmd [cb _finish_fetch
]
88 bind $w <$M1B-Key
-w
> break
89 bind $w <$M1B-Key
-W
> break
90 bind $w <Visibility
> "
94 wm protocol
$w WM_DELETE_WINDOW
[cb _noop
]
103 if {$new_ref ne
{}} {
104 # If we have a ref we need to update it before we can
105 # proceed with a checkout (if one was enabled).
107 if {![_update_ref
$this]} {
124 method _finish_fetch
{ok
} {
126 set l_trck
[lindex $fetch_spec 0]
128 set l_trck FETCH_HEAD
130 if {[catch {set new_hash
[git rev-parse
--verify "$l_trck^0"]} err
]} {
132 $w_cons insert
[mc
"fatal: Cannot resolve %s" $l_trck]
139 wm protocol
$w WM_DELETE_WINDOW
{}
145 button $w.
close -text [mc Close
] -command [list destroy $w]
146 pack $w.
close -side bottom
-anchor e
-padx 10 -pady 10
152 method _update_ref
{} {
153 global null_sha1 current_branch repo_config
160 set rn
[string length
$rh]
161 if {[string equal
-length $rn $rh $ref]} {
162 set newbranch
[string range
$ref $rn end
]
163 if {$current_branch eq
$newbranch} {
170 if {[catch {set cur
[git rev-parse
--verify "$ref^0"]}]} {
171 # Assume it does not exist, and that is what the error was.
174 _error
$this [mc
"Branch '%s' does not exist." $newbranch]
178 set reflog_msg
"branch: Created from $new_expr"
181 if {($repo_config(branch.autosetupmerge
) eq
{true
}
182 ||
$repo_config(branch.autosetupmerge
) eq
{always
})
183 && $remote_source ne
{}
184 && "refs/heads/$newbranch" eq
$ref} {
186 set c_remote
[lindex $remote_source 1]
187 set c_merge
[lindex $remote_source 2]
189 git config branch.
$newbranch.remote
$c_remote
190 git config branch.
$newbranch.merge
$c_merge
192 _error
$this [strcat
\
193 [mc
"Failed to configure simplified git-pull for '%s'." $newbranch] \
197 } elseif
{$create && $merge_type eq
{none
}} {
198 # We were told to create it, but not do a merge.
199 # Bad. Name shouldn't have existed.
201 _error
$this [mc
"Branch '%s' already exists." $newbranch]
203 } elseif
{!$create && $merge_type eq
{none
}} {
204 # We aren't creating, it exists and we don't merge.
205 # We are probably just a simple branch switch.
206 # Use whatever value we just read.
210 } elseif
{$new eq
$cur} {
211 # No merge would be required, don't compute anything.
214 catch {set merge_base
[git merge-base
$new $cur]}
215 if {$merge_base eq
$cur} {
216 # The current branch is older.
218 set reflog_msg
"merge $new_expr: Fast-forward"
220 switch -- $merge_type {
222 if {$merge_base eq
$new} {
223 # The current branch is actually newer.
228 _error
$this [mc
"Branch '%s' already exists.\n\nIt cannot fast-forward to %s.\nA merge is required." $newbranch $new_expr]
233 # The current branch will lose things.
235 if {[_confirm_reset
$this $cur]} {
236 set reflog_msg
"reset $new_expr"
242 _error
$this [mc
"Merge strategy '%s' not supported." $merge_type]
251 # No so fast. We should defer this in case
252 # we cannot update the working directory.
259 git update-ref
-m $reflog_msg $ref $new $cur
261 _error
$this [strcat
[mc
"Failed to update '%s'." $newbranch] "\n\n$err"]
269 method _checkout
{} {
270 if {[lock_index checkout_op
]} {
271 after idle
[cb _start_checkout
]
273 _error
$this [mc
"Staging area (index) is already locked."]
278 method _start_checkout
{} {
279 global HEAD commit_type
281 # -- Our in memory state should match the repository.
283 repository_state curType curHEAD curMERGE_HEAD
284 if {[string match amend
* $commit_type]
285 && $curType eq
{normal
}
286 && $curHEAD eq
$HEAD} {
287 } elseif
{$commit_type ne
$curType ||
$HEAD ne
$curHEAD} {
288 info_popup
[mc
"Last scanned state does not match repository state.
290 Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed.
292 The rescan will be automatically started now.
300 if {$curHEAD eq
$new_hash} {
301 _after_readtree
$this
302 } elseif
{[is_config_true gui.trustmtime
]} {
305 ui_status
[mc
"Refreshing file status..."]
306 set fd
[git_read update-index
\
312 fconfigure $fd -blocking 0 -translation binary
313 fileevent $fd readable
[cb _refresh_wait
$fd]
317 method _refresh_wait
{fd
} {
326 if {$new_ref eq
{}} {
327 return [string range
$new_hash 0 7]
331 set rn
[string length
$rh]
332 if {[string equal
-length $rn $rh $new_ref]} {
333 return [string range
$new_ref $rn end
]
339 method _readtree
{} {
343 $::main_status start
\
344 [mc
"Updating working directory to '%s'..." [_name
$this]] \
345 [mc
"files checked out"]
347 set fd
[git_read
--stderr read-tree
\
351 --exclude-per
-directory
=.gitignore
\
355 fconfigure $fd -blocking 0 -translation binary
356 fileevent $fd readable
[cb _readtree_wait
$fd]
359 method _readtree_wait
{fd
} {
360 global current_branch
363 $::main_status update_meter
$buf
364 append readtree_d
$buf
366 fconfigure $fd -blocking 1
368 fconfigure $fd -blocking 0
372 if {[catch {close $fd}]} {
374 regsub {^fatal
: } $err {} err
375 $::main_status stop
[mc
"Aborted checkout of '%s' (file level merging is required)." [_name
$this]]
376 warn_popup
[strcat
[mc
"File level merge required."] "
380 " [mc
"Staying on branch '%s'." $current_branch]]
387 _after_readtree
$this
390 method _after_readtree
{} {
391 global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
392 global current_branch is_detached
395 set name
[_name
$this]
396 set log
"checkout: moving"
398 append log
" from $current_branch"
401 # -- Move/create HEAD as a symbolic ref. Core git does not
402 # even check for failure here, it Just Works(tm). If it
403 # doesn't we are in some really ugly state that is difficult
404 # to recover from within git-gui.
407 set rn
[string length
$rh]
408 if {[string equal
-length $rn $rh $new_ref]} {
409 set new_branch
[string range
$new_ref $rn end
]
410 if {$is_detached ||
$current_branch ne
$new_branch} {
411 append log
" to $new_branch"
413 git symbolic-ref
-m $log HEAD
$new_ref
417 set current_branch
$new_branch
421 if {!$is_detached ||
$new_hash ne
$HEAD} {
422 append log
" to $new_expr"
424 _detach_HEAD
$log $new_hash
429 set current_branch HEAD
433 # -- We had to defer updating the branch itself until we
434 # knew the working directory would update. So now we
435 # need to finish that work. If it fails we're in big
438 if {$update_old ne
{}} {
451 info_popup
[mc
"You are no longer on a local branch.
453 If you wanted to be on a branch, create one now starting from 'This Detached Checkout'."]
456 # -- Update our repository state. If we were previously in
457 # amend mode we need to toss the current buffer and do a
458 # full rescan to update our file lists. If we weren't in
459 # amend mode our file lists are accurate and we can avoid
463 set selected_commit_type new
464 if {[string match amend
* $commit_type]} {
465 $ui_comm delete
0.0 end
467 $ui_comm edit modified false
468 rescan
[list ui_status
[mc
"Checked out '%s'." $name]]
470 repository_state commit_type HEAD MERGE_HEAD
472 ui_status
[mc
"Checked out '%s'." $name]
477 git-version
proc _detach_HEAD
{log new
} {
479 git update-ref
--no-deref
-m $log HEAD
$new
485 fconfigure $fd -translation lf
-encoding utf-8
491 method _confirm_reset
{cur
} {
493 set name
[_name
$this]
494 set gitk
[list do_gitk
[list $cur ^
$new_hash]]
496 _toplevel
$this {Confirm Branch Reset
}
497 pack [label $w.msg1
\
500 -text [mc
"Resetting '%s' to '%s' will lose the following commits:" $name $new_expr]\
510 -xscrollcommand [list $w.
list.sbx
set] \
511 -yscrollcommand [list $w.
list.sby
set]
512 scrollbar $w.
list.sbx
-orient h
-command [list $list xview
]
513 scrollbar $w.
list.sby
-orient v
-command [list $list yview
]
514 pack $w.
list.sbx
-fill x
-side bottom
515 pack $w.
list.sby
-fill y
-side right
516 pack $list -fill both
-expand 1
517 pack $w.
list -fill both
-expand 1 -padx 5 -pady 5
519 pack [label $w.msg2
\
522 -text [mc
"Recovering lost commits may not be easy."] \
524 pack [label $w.msg3
\
527 -text [mc
"Reset '%s'?" $name] \
531 button $w.buttons.visualize
\
532 -text [mc Visualize
] \
534 pack $w.buttons.visualize
-side left
535 button $w.buttons.reset
\
541 pack $w.buttons.reset
-side right
542 button $w.buttons.cancel
\
545 -command [list destroy $w]
546 pack $w.buttons.cancel
-side right
-padx 5
547 pack $w.buttons
-side bottom
-fill x
-pady 10 -padx 10
549 set fd
[git_read rev-list
--pretty=oneline
$cur ^
$new_hash]
550 while {[gets $fd line
] > 0} {
551 set abbr
[string range
$line 0 7]
552 set subj
[string range
$line 41 end
]
553 $list insert end
"$abbr $subj\n"
556 $list configure
-state disabled
558 bind $w <Key-v
> $gitk
559 bind $w <Visibility
> "
561 focus $w.buttons.cancel
563 bind $w <Key-Return
> [list destroy $w]
564 bind $w <Key-Escape
> [list destroy $w]
569 method _error
{msg
} {
570 if {[winfo ismapped
$parent_w]} {
579 -title [wm title
$p] \
584 method _toplevel
{title
} {
585 regsub -all {::} $this {__
} w
588 if {[winfo ismapped
$parent_w]} {
596 wm geometry
$w "+[winfo rootx $p]+[winfo rooty $p]"
599 method _fatal
{err
} {
600 error_popup
[strcat
[mc
"Failed to set current branch.
602 This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
604 This should not have occurred. %s will now close and give up." [appname
]] "