Subject: [PATCH] git-gui: run post-checkout hook on checkout

From: Jens Lehmann <[email-masked]>
Date: Wed, 25 Mar 2009 23:48:33 +0100

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-masked]>
---

It's been a while since i deeply touched tcl/tk code ... i basically
adapted the code for calling the post-commit hook here.

I renamed the former local variable "curHEAD" from the method
_start_checkout to a field named "old_hash" to be able to remember
the hash of the tree *before* the checkout started. This hash has
to be given as first argument to the post-checkout hook *after* the
checkout, where it is not the *current* HEAD anymore.

 git-gui/lib/checkout_op.tcl |   39 ++++++++++++++++++++++++++++++++++-----
 1 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/git-gui/lib/checkout_op.tcl b/git-gui/lib/checkout_op.tcl
index caca888..31863a5 100644
--- a/git-gui/lib/checkout_op.tcl
+++ b/git-gui/lib/checkout_op.tcl
@@ -9,6 +9,7 @@ field w_cons   {}; # embedded console window object
 field new_expr   ; # expression the user saw/thinks this is
 field new_hash   ; # commit SHA-1 we are switching to
 field new_ref    ; # ref we are updating/creating
+field old_hash   ; # commit SHA-1 that was checked when we started
 
 field parent_w      .; # window that started us
 field merge_type none; # type of merge to apply to existing branch
@@ -280,11 +281,11 @@ method _start_checkout {} {
 
 	# -- Our in memory state should match the repository.
 	#
-	repository_state curType curHEAD curMERGE_HEAD
+	repository_state curType old_hash curMERGE_HEAD
 	if {[string match amend* $commit_type]
 		&& $curType eq {normal}
-		&& $curHEAD eq $HEAD} {
-	} elseif {$commit_type ne $curType || $HEAD ne $curHEAD} {
+		&& $old_hash eq $HEAD} {
+	} elseif {$commit_type ne $curType || $HEAD ne $old_hash} {
 		info_popup [mc "Last scanned state does not match repository state.
 
 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.
 		return
 	}
 
-	if {$curHEAD eq $new_hash} {
+	if {$old_hash eq $new_hash} {
 		_after_readtree $this
 	} elseif {[is_config_true gui.trustmtime]} {
 		_readtree $this
@@ -471,7 +472,19 @@ If you wanted to be on a branch, create one now starting from 'This Detached Che
 		set PARENT $HEAD
 		ui_status [mc "Checked out '%s'." $name]
 	}
-	delete_this
+
+	# -- Run the post-checkout hook.
+	#
+	set fd_ph [githook_read post-checkout $old_hash $new_hash 1]
+	if {$fd_ph ne {}} {
+		upvar #0 pch_error pc_err
+		set pc_err {}
+		fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
+		fileevent $fd_ph readable \
+			[list checkout_postcheckout_wait $fd_ph $this]
+	} else {
+		delete_this
+	}
 }
 
 git-version proc _detach_HEAD {log new} {
@@ -608,3 +621,19 @@ $err"]
 }
 
 }
+
+proc checkout_postcheckout_wait {fd_ph t} {
+	upvar #0 pch_error pch_error
+
+	append pch_error [read $fd_ph]
+	fconfigure $fd_ph -blocking 1
+	if {[eof $fd_ph]} {
+		if {[catch {close $fd_ph}]} {
+			hook_failed_popup post-checkout $pch_error 0
+		}
+		unset pch_error
+		delete_this $t
+		return
+	}
+	fconfigure $fd_ph -blocking 0
+}
-- 
1.6.2.1.307.g91408