threads / patch / 15556

patchcall prepare-commit-msg hook

Subject: [GIT-GUI PATCH] call prepare-commit-msg hook

## tl;dr

5 messages between Sep 16, 2008 and Sep 24, 2008. Diffs are folded; open one to read it.

replies: 4people: 2as markdown or json

Joshua Williams· Sep 16, 2008, 21:12 UTC · lore

This patch adds support to git-gui for calling out to the prepare-commit-msg hook.

Signed-off-by: Joshua Williams <joshua.williams@qlogic.com>
---
  git-gui/git-gui.sh |   66 
++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 files changed, 64 insertions(+), 2 deletions(-)
Show changes to git-gui/git-gui.sh +64 −2
diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh
index 10d8a44..1e269de 100755
--- a/git-gui/git-gui.sh
+++ b/git-gui/git-gui.sh
@@ -1131,8 +1131,7 @@ proc rescan {after {honor_trustmtime 1}} {
  		|| [string trim [$ui_comm get 0.0 end]] eq {})} {
  		if {[string match amend* $commit_type]} {
  		} elseif {[load_message GITGUI_MSG]} {
-		} elseif {[load_message MERGE_MSG]} {
-		} elseif {[load_message SQUASH_MSG]} {
+		} elseif {[run_prepare_commit_msg_hook]} {
  		}
  		$ui_comm edit reset
  		$ui_comm edit modified false
@@ -1230,6 +1229,69 @@ proc load_message {file} {
  	return 0
  }

+proc run_prepare_commit_msg_hook {} {
+	global pch_error
+
+	# prepare-commit-msg requires PREPARE_COMMIT_MSG exist.  From git-gui
+	# it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
+	# empty file but existant file.
+
+	set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
+
+	if {[file isfile [gitdir MERGE_MSG]]} {
+		set pcm_source "merge"
+		set fd_mm [open [gitdir MERGE_MSG] r]
+		puts -nonewline $fd_pcm [read $fd_mm]
+		close $fd_mm
+	} elseif {[file isfile [gitdir SQUASH_MSG]]} {
+		set pcm_source "squash"
+		set fd_sm [open [gitdir SQUASH_MSG] r]
+		puts -nonewline $fd_pcm [read $fd_sm]
+		close $fd_sm
+	} else {
+		set pcm_source ""
+	}
+
+	close $fd_pcm
+
+	set fd_ph [githook_read prepare-commit-msg \
+			[gitdir PREPARE_COMMIT_MSG] $pcm_source]
+	if {$fd_ph eq {}} {
+		catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+		return 0;
+	}
+
+	ui_status [mc "Calling prepare-commit-msg hook..."]
+	set pch_error {}
+
+	fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
+	fileevent $fd_ph readable \
+		[list prepare_commit_msg_hook_wait $fd_ph]
+
+	return 1;
+}
+
+proc prepare_commit_msg_hook_wait {fd_ph} {
+	global pch_error
+
+	append pch_error [read $fd_ph]
+	fconfigure $fd_ph -blocking 1
+	if {[eof $fd_ph]} {
+		if {[catch {close $fd_ph}]} {
+			ui_status [mc "Commit declined by prepare-commit-msg hook."]
+			hook_failed_popup prepare-commit-msg $pch_error
+			exit 1
+		} else {
+			load_message PREPARE_COMMIT_MSG
+		}
+		set pch_error {}
+		catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+		return
+        }
+	fconfigure $fd_ph -blocking 0
+	catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+}
+
  proc read_diff_index {fd after} {
  	global buf_rdi
Joshua Williams· Sep 16, 2008, 22:05 UTC · re: Joshua Williams · lore

Re: [GIT-GUI PATCH] call prepare-commit-msg hook

Small change. Still need to load MERGE_MSG and SQUASH_MSG if there's no prepare-commit-msg hook. We now process the hook first and if it's not there, load MERGE_MSG or SQUASH_MSG.

Signed-off-by: Joshua Williams <joshua.williams@qlogic.com>
---
  git-gui/git-gui.sh |   64 
++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 64 insertions(+), 0 deletions(-)
Show changes to git-gui/git-gui.sh +64 −0
diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh
index 10d8a44..8d06cd1 100755
--- a/git-gui/git-gui.sh
+++ b/git-gui/git-gui.sh
@@ -1131,6 +1131,7 @@ proc rescan {after {honor_trustmtime 1}} {
  		|| [string trim [$ui_comm get 0.0 end]] eq {})} {
  		if {[string match amend* $commit_type]} {
  		} elseif {[load_message GITGUI_MSG]} {
+		} elseif {[run_prepare_commit_msg_hook]} {
  		} elseif {[load_message MERGE_MSG]} {
  		} elseif {[load_message SQUASH_MSG]} {
  		}
@@ -1230,6 +1231,69 @@ proc load_message {file} {
  	return 0
  }

+proc run_prepare_commit_msg_hook {} {
+	global pch_error
+
+	# prepare-commit-msg requires PREPARE_COMMIT_MSG exist.  From git-gui
+	# it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
+	# empty file but existant file.
+
+	set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
+
+	if {[file isfile [gitdir MERGE_MSG]]} {
+		set pcm_source "merge"
+		set fd_mm [open [gitdir MERGE_MSG] r]
+		puts -nonewline $fd_pcm [read $fd_mm]
+		close $fd_mm
+	} elseif {[file isfile [gitdir SQUASH_MSG]]} {
+		set pcm_source "squash"
+		set fd_sm [open [gitdir SQUASH_MSG] r]
+		puts -nonewline $fd_pcm [read $fd_sm]
+		close $fd_sm
+	} else {
+		set pcm_source ""
+	}
+
+	close $fd_pcm
+
+	set fd_ph [githook_read prepare-commit-msg \
+			[gitdir PREPARE_COMMIT_MSG] $pcm_source]
+	if {$fd_ph eq {}} {
+		catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+		return 0;
+	}
+
+	ui_status [mc "Calling prepare-commit-msg hook..."]
+	set pch_error {}
+
+	fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
+	fileevent $fd_ph readable \
+		[list prepare_commit_msg_hook_wait $fd_ph]
+
+	return 1;
+}
+
+proc prepare_commit_msg_hook_wait {fd_ph} {
+	global pch_error
+
+	append pch_error [read $fd_ph]
+	fconfigure $fd_ph -blocking 1
+	if {[eof $fd_ph]} {
+		if {[catch {close $fd_ph}]} {
+			ui_status [mc "Commit declined by prepare-commit-msg hook."]
+			hook_failed_popup prepare-commit-msg $pch_error
+			exit 1
+		} else {
+			load_message PREPARE_COMMIT_MSG
+		}
+		set pch_error {}
+		catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+		return
+        }
+	fconfigure $fd_ph -blocking 0
+	catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+}
+
  proc read_diff_index {fd after} {
  	global buf_rdi
Shawn O. Pearce· Sep 24, 2008, 17:04 UTC · re: Joshua Williams · lore

Re: [GIT-GUI PATCH] call prepare-commit-msg hook

Joshua Williams <joshua.williams@qlogic.com> wrote:
Show 5 quoted lines
> Small change.  Still need to load MERGE_MSG and SQUASH_MSG
> if there's no prepare-commit-msg hook.  We now process the hook
> first and if it's not there, load MERGE_MSG or SQUASH_MSG.
>
> Signed-off-by: Joshua Williams <joshua.williams@qlogic.com>

This patch looks reasonable. My only comment is maybe we want to delete PREPARE_COMMIT_MSG file when we "exit 1" when the hook has declined.

> diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh
> index 10d8a44..8d06cd1 100755
> --- a/git-gui/git-gui.sh
> +++ b/git-gui/git-gui.sh

Can you please try to put this onto my current git-gui tree? I don't have the base 10d8a44 object so I can't 3-way merge this in, and the patch doesn't apply cleanly anymore.

-- 
Shawn.
Joshua Williams· Sep 24, 2008, 19:11 UTC · re: Shawn O. Pearce · lore

Re: [GIT-GUI PATCH] call prepare-commit-msg hook

Shawn O. Pearce wrote:
Show 10 quoted lines
> Joshua Williams <joshua.williams@qlogic.com> wrote:
>> Small change.  Still need to load MERGE_MSG and SQUASH_MSG
>> if there's no prepare-commit-msg hook.  We now process the hook
>> first and if it's not there, load MERGE_MSG or SQUASH_MSG.
>>
>> Signed-off-by: Joshua Williams <joshua.williams@qlogic.com>
> 
> This patch looks reasonable.  My only comment is maybe we want
> to delete PREPARE_COMMIT_MSG file when we "exit 1" when the hook
> has declined.
Good catch.  I've added that to the latest patch.
Show 8 quoted lines
>> diff --git a/git-gui/git-gui.sh b/git-gui/git-gui.sh
>> index 10d8a44..8d06cd1 100755
>> --- a/git-gui/git-gui.sh
>> +++ b/git-gui/git-gui.sh
> 
> Can you please try to put this onto my current git-gui tree?
> I don't have the base 10d8a44 object so I can't 3-way merge
> this in, and the patch doesn't apply cleanly anymore.
Absolutely.  Here it is:
Add support to git-gui for calling out to the prepare-commit-msg hook.
Signed-off-by: Joshua Williams <joshua.williams@qlogic.com>
---
Show changes to git-gui.sh +65 −0
diff --git a/git-gui.sh b/git-gui.sh
index 1939001..cb0fcac 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -1170,6 +1170,7 @@ proc rescan {after {honor_trustmtime 1}} {
  		|| [string trim [$ui_comm get 0.0 end]] eq {})} {
  		if {[string match amend* $commit_type]} {
  		} elseif {[load_message GITGUI_MSG]} {
+		} elseif {[run_prepare_commit_msg_hook]} {
  		} elseif {[load_message MERGE_MSG]} {
  		} elseif {[load_message SQUASH_MSG]} {
  		}
@@ -1269,6 +1270,70 @@ proc load_message {file} {
  	return 0
  }

+proc run_prepare_commit_msg_hook {} {
+	global pch_error
+
+	# prepare-commit-msg requires PREPARE_COMMIT_MSG exist.  From git-gui
+	# it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
+	# empty file but existant file.
+
+	set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
+
+	if {[file isfile [gitdir MERGE_MSG]]} {
+		set pcm_source "merge"
+		set fd_mm [open [gitdir MERGE_MSG] r]
+		puts -nonewline $fd_pcm [read $fd_mm]
+		close $fd_mm
+	} elseif {[file isfile [gitdir SQUASH_MSG]]} {
+		set pcm_source "squash"
+		set fd_sm [open [gitdir SQUASH_MSG] r]
+		puts -nonewline $fd_pcm [read $fd_sm]
+		close $fd_sm
+	} else {
+		set pcm_source ""
+	}
+
+	close $fd_pcm
+
+	set fd_ph [githook_read prepare-commit-msg \
+			[gitdir PREPARE_COMMIT_MSG] $pcm_source]
+	if {$fd_ph eq {}} {
+		catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+		return 0;
+	}
+
+	ui_status [mc "Calling prepare-commit-msg hook..."]
+	set pch_error {}
+
+	fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
+	fileevent $fd_ph readable \
+		[list prepare_commit_msg_hook_wait $fd_ph]
+
+	return 1;
+}
+
+proc prepare_commit_msg_hook_wait {fd_ph} {
+	global pch_error
+
+	append pch_error [read $fd_ph]
+	fconfigure $fd_ph -blocking 1
+	if {[eof $fd_ph]} {
+		if {[catch {close $fd_ph}]} {
+			ui_status [mc "Commit declined by prepare-commit-msg hook."]
+			hook_failed_popup prepare-commit-msg $pch_error
+			catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+			exit 1
+		} else {
+			load_message PREPARE_COMMIT_MSG
+		}
+		set pch_error {}
+		catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+		return
+        }
+	fconfigure $fd_ph -blocking 0
+	catch {file delete [gitdir PREPARE_COMMIT_MSG]}
+}
+
  proc read_diff_index {fd after} {
  	global buf_rdi
Shawn O. Pearce· Sep 24, 2008, 19:46 UTC · re: Joshua Williams · lore

Re: [GIT-GUI PATCH] call prepare-commit-msg hook

Joshua Williams <joshua.williams@qlogic.com> wrote:
Show 6 quoted lines
>>
>> Can you please try to put this onto my current git-gui tree?
>> I don't have the base 10d8a44 object so I can't 3-way merge
>> this in, and the patch doesn't apply cleanly anymore.
>
> Absolutely.  Here it is:

I'm not sure what happened, but any line of context got an extra space inserted at the front of it. I had to go rip out the extra spaces from every line of context to get git-apply to process the patch.

Anyway, its applied now.  Thanks.
-- 
Shawn.

← back to recent threads