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 update_old
{}; # was the update-ref call deferred?
23 field reflog_msg
{}; # log message for the update-ref call
25 constructor new
{expr hash
{ref
{}}} {
33 method parent
{path
} {
34 set parent_w
[winfo toplevel $path]
37 method enable_merge
{type
} {
41 method enable_fetch
{spec
} {
45 method enable_checkout
{co
} {
49 method enable_create
{co
} {
54 if {$fetch_spec ne
{}} {
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.
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
67 _toplevel
$this {Refreshing Tracking Branch
}
68 set w_cons
[::console::embed \
70 "Fetching $r_name from $remote"]
71 pack $w.
console -fill both
-expand 1
73 [list git fetch
$remote +$r_head:$l_trck] \
76 bind $w <$M1B-Key
-w
> break
77 bind $w <$M1B-Key
-W
> break
78 bind $w <Visibility
> "
82 wm protocol
$w WM_DELETE_WINDOW
[cb _noop
]
92 # If we have a ref we need to update it before we can
93 # proceed with a checkout (if one was enabled).
95 if {![_update_ref
$this]} {
112 method _finish_fetch
{ok
} {
114 set l_trck
[lindex $fetch_spec 0]
115 if {[catch {set new_hash
[git rev-parse
--verify "$l_trck^0"]} err
]} {
117 $w_cons insert
"fatal: Cannot resolve $l_trck"
124 wm protocol
$w WM_DELETE_WINDOW
{}
130 button $w.
close -text Close
-command [list destroy $w]
131 pack $w.
close -side bottom
-anchor e
-padx 10 -pady 10
137 method _update_ref
{} {
138 global null_sha1 current_branch
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} {
155 if {[catch {set cur
[git rev-parse
--verify "$ref^0"]}]} {
156 # Assume it does not exist, and that is what the error was.
159 _error
$this "Branch '$newbranch' does not exist."
163 set reflog_msg
"branch: Created from $new_expr"
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.
169 _error
$this "Branch '$newbranch' already exists."
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.
178 } elseif
{$new eq
$cur} {
179 # No merge would be required, don't compute anything.
183 catch {set mrb
[git merge-base
$new $cur]}
184 switch -- $merge_type {
187 # The current branch is actually newer.
190 } elseif
{$mrb eq
$cur} {
191 # The current branch is older.
193 set reflog_msg
"merge $new_expr: Fast-forward"
195 _error
$this "Branch '$newbranch' already exists.\n\nIt cannot fast-forward to $new_expr.\nA merge is required."
201 # The current branch is older.
203 set reflog_msg
"merge $new_expr: Fast-forward"
205 # The current branch will lose things.
207 if {[_confirm_reset
$this $cur]} {
208 set reflog_msg
"reset $new_expr"
215 _error
$this "Only 'ff' and 'reset' merge is currently supported."
223 # No so fast. We should defer this in case
224 # we cannot update the working directory.
231 git update-ref
-m $reflog_msg $ref $new $cur
233 _error
$this "Failed to update '$newbranch'.\n\n$err"
241 method _checkout
{} {
242 if {[lock_index checkout_op
]} {
243 after idle
[cb _start_checkout
]
245 _error
$this "Index is already locked."
250 method _start_checkout
{} {
251 global HEAD commit_type
253 # -- Our in memory state should match the repository.
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.
262 Another Git program has modified this repository since the last
scan. A rescan must be performed before the current branch can be changed.
264 The rescan will be automatically started now.
272 if {[is_config_true gui.trustmtime
]} {
275 ui_status
{Refreshing
file status...
}
276 set cmd
[list git update-index
]
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]
287 method _refresh_wait
{fd
} {
296 if {$new_ref eq
{}} {
297 return [string range
$new_hash 0 7]
301 set rn
[string length
$rh]
302 if {[string equal
-length $rn $rh $new_ref]} {
303 return [string range
$new_ref $rn end
]
309 method _readtree
{} {
312 ui_status
"Updating working directory to '[_name $this]'..."
313 set cmd
[list git read-tree
]
316 lappend cmd
--exclude-per
-directory
=.gitignore
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]
324 method _readtree_wait
{fd
} {
325 global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
326 global current_branch is_detached
329 # -- We never get interesting output on stdout; only stderr.
332 fconfigure $fd -blocking 1
334 fconfigure $fd -blocking 0
338 set name
[_name
$this]
340 # -- The working directory wasn't in sync with the index and
341 # we'd have to overwrite something to make the switch. A
344 if {[catch {close $fd} err
]} {
345 regsub {^fatal
: } $err {} err
346 warn_popup
"File level merge required.
350 Staying on branch '$current_branch'."
351 ui_status
"Aborted checkout of '$name' (file level merging is required)."
357 set log
"checkout: moving"
359 append log
" from $current_branch"
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.
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"
374 git symbolic-ref
-m $log HEAD
$new_ref
378 set current_branch
$new_branch
381 append log
" to $new_expr"
384 _detach_HEAD
$log $new_hash
388 set current_branch HEAD
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
397 if {$update_old ne
{}} {
410 info_popup
"You are no longer on a local branch.
412 If you wanted to be on a branch, create one now starting from 'This Detached Checkout'."
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
422 set selected_commit_type new
423 if {[string match amend
* $commit_type]} {
424 $ui_comm delete
0.0 end
426 $ui_comm edit modified false
427 rescan
[list ui_status
"Checked out '$name'."]
429 repository_state commit_type HEAD MERGE_HEAD
431 ui_status
"Checked out '$name'."
436 git-version
proc _detach_HEAD
{log new
} {
438 git update-ref
--no-deref
-m $log HEAD
$new
444 fconfigure $fd -translation lf
-encoding utf-8
450 method _confirm_reset
{cur
} {
452 set name
[_name
$this]
453 set gitk
[list do_gitk
[list $cur ^
$new_hash]]
455 _toplevel
$this {Confirm Branch Reset
}
456 pack [label $w.msg1
\
459 -text "Resetting '$name' to $new_expr will lose the following commits:" \
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
478 pack [label $w.msg2
\
481 -text {Recovering lost commits may not be easy.
} \
483 pack [label $w.msg3
\
486 -text "Reset '$name'?" \
490 button $w.buttons.visualize
\
493 pack $w.buttons.visualize
-side left
494 button $w.buttons.reset
\
500 pack $w.buttons.reset
-side right
501 button $w.buttons.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
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"
515 $list configure
-state disabled
517 bind $w <Key-v
> $gitk
518 bind $w <Visibility
> "
520 focus $w.buttons.cancel
522 bind $w <Key-Return
> [list destroy $w]
523 bind $w <Key-Escape
> [list destroy $w]
528 method _error
{msg
} {
529 if {[winfo ismapped
$parent_w]} {
538 -title [wm title
$p] \
543 method _toplevel
{title
} {
544 regsub -all {::} $this {__
} w
547 if {[winfo ismapped
$parent_w]} {
555 wm geometry
$w "+[winfo rootx $p]+[winfo rooty $p]"
558 method _fatal
{err
} {
559 error_popup
"Failed to set current branch.
561 This working directory is only partially switched. We successfully updated your files, but failed to update an internal Git file.
563 This should not have occurred. [appname] will now close and give up.