Skip to content

Commit c949833

Browse files
jlehmannspearce
authored andcommitted
git-gui: run post-checkout hook on checkout
git-gui is using "git-read-tree -u" for checkout which doesn't invoke the post-checkout hook as a plain git-checkout would. So git-gui must call the hook itself. Signed-off-by: Jens Lehmann <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent f0d4eec commit c949833

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

lib/checkout_op.tcl

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ field w_cons {}; # embedded console window object
99
field new_expr ; # expression the user saw/thinks this is
1010
field new_hash ; # commit SHA-1 we are switching to
1111
field new_ref ; # ref we are updating/creating
12+
field old_hash ; # commit SHA-1 that was checked out when we started
1213

1314
field parent_w .; # window that started us
1415
field merge_type none; # type of merge to apply to existing branch
@@ -280,11 +281,11 @@ method _start_checkout {} {
280281

281282
# -- Our in memory state should match the repository.
282283
#
283-
repository_state curType curHEAD curMERGE_HEAD
284+
repository_state curType old_hash curMERGE_HEAD
284285
if {[string match amend* $commit_type]
285286
&& $curType eq {normal}
286-
&& $curHEAD eq $HEAD} {
287-
} elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
287+
&& $old_hash eq $HEAD} {
288+
} elseif {$commit_type ne $curType || $HEAD ne $old_hash} {
288289
info_popup [mc "Last scanned state does not match repository state.
289290
290291
Another Git program has modified this repository since the last scan. A rescan must be performed before the current branch can be changed.
@@ -297,7 +298,7 @@ The rescan will be automatically started now.
297298
return
298299
}
299300

300-
if {$curHEAD eq $new_hash} {
301+
if {$old_hash eq $new_hash} {
301302
_after_readtree $this
302303
} elseif {[is_config_true gui.trustmtime]} {
303304
_readtree $this
@@ -453,13 +454,47 @@ method _after_readtree {} {
453454
If you wanted to be on a branch, create one now starting from 'This Detached Checkout'."]
454455
}
455456

457+
# -- Run the post-checkout hook.
458+
#
459+
set fd_ph [githook_read post-checkout $old_hash $new_hash 1]
460+
if {$fd_ph ne {}} {
461+
global pch_error
462+
set pch_error {}
463+
fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
464+
fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
465+
} else {
466+
_update_repo_state $this
467+
}
468+
}
469+
470+
method _postcheckout_wait {fd_ph} {
471+
global pch_error
472+
473+
append pch_error [read $fd_ph]
474+
fconfigure $fd_ph -blocking 1
475+
if {[eof $fd_ph]} {
476+
if {[catch {close $fd_ph}]} {
477+
hook_failed_popup post-checkout $pch_error 0
478+
}
479+
unset pch_error
480+
_update_repo_state $this
481+
return
482+
}
483+
fconfigure $fd_ph -blocking 0
484+
}
485+
486+
method _update_repo_state {} {
456487
# -- Update our repository state. If we were previously in
457488
# amend mode we need to toss the current buffer and do a
458489
# full rescan to update our file lists. If we weren't in
459490
# amend mode our file lists are accurate and we can avoid
460491
# the rescan.
461492
#
493+
global selected_commit_type commit_type HEAD MERGE_HEAD PARENT
494+
global ui_comm
495+
462496
unlock_index
497+
set name [_name $this]
463498
set selected_commit_type new
464499
if {[string match amend* $commit_type]} {
465500
$ui_comm delete 0.0 end

0 commit comments

Comments
 (0)