Make author and committer available to pre-commit hook

Subsystems: the rest

4 messages, 3 authors, 2016-06-15 · open the first message on its own page

Make author and committer available to pre-commit hook

From: Gisle Aas <hidden>
Date: 2016-06-15 22:49:04

I would like to implement a pre-commit hook that validates the
--author set for the commit.  Our use case is a shared repository of
configuration info where different persons all commit as root; but we
want to make sure they override the --author to something sensible.

What would be the preferred mechanism to pass on this information?  It
could for instance be arguments to the hook script or via environment
variables.

I created the following patch to explore what it would take to pass on
this information as command line arguments.  It seems to do the trick
for me.  Any drawback with this approach?

Regards,
Gisle

diff --git a/builtin/commit.c b/builtin/commit.c
index c101f00..acb8dc2 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -557,8 +557,20 @@ static int prepare_to_commit(const char
*index_file, const char *prefix,
        const char *hook_arg2 = NULL;
        int ident_shown = 0;

-       if (!no_verify && run_hook(index_file, "pre-commit", NULL))
+       determine_author_info();
+       if (!no_verify) {
+           int rc;
+           hook_arg1 = xstrdup(fmt_name(author_name, author_email));
+           hook_arg2 = xstrdup(fmt_name(getenv("GIT_COMMITTER_NAME"),
+                                        getenv("GIT_COMMITTER_EMAIL")));
+           rc = run_hook(index_file, "pre-commit", hook_arg1, hook_arg2, NULL);
+           free((char*)hook_arg1);
+           free((char*)hook_arg2);
+           if (rc)
                return 0;
+           hook_arg1 = NULL;
+           hook_arg2 = NULL;
+       }

        if (message.len) {
                strbuf_addbuf(&sb, &message);
@@ -632,8 +644,6 @@ static int prepare_to_commit(const char
*index_file, const char *prefix,

        strbuf_release(&sb);

-       determine_author_info();
-
        /* This checks if committer ident is explicitly given */
        git_committer_info(0);
        if (use_editor && include_status) {

Re: Make author and committer available to pre-commit hook

From: Jeff King <hidden>
Date: 2016-06-15 22:49:04

On Mon, Jul 05, 2010 at 01:30:46PM +0200, Gisle Aas wrote:
I would like to implement a pre-commit hook that validates the
--author set for the commit.  Our use case is a shared repository of
configuration info where different persons all commit as root; but we
want to make sure they override the --author to something sensible.

What would be the preferred mechanism to pass on this information?  It
could for instance be arguments to the hook script or via environment
variables.
It would make sense to me for it to be passed in through the environment
using GIT_{AUTHOR,COMMITTER}_{NAME,EMAIL}. You could even pass
GIT_AUTHOR_DATE to detect if somebody is using "git commit -c" to get
the date from a previous commit.

-Peff

Re: Make author and committer available to pre-commit hook

From: Gisle Aas <hidden>
Date: 2016-06-15 22:49:05

On Mon, Jul 5, 2010 at 13:46, Jeff King [off-list ref] wrote:
On Mon, Jul 05, 2010 at 01:30:46PM +0200, Gisle Aas wrote:
quoted
I would like to implement a pre-commit hook that validates the
--author set for the commit.  Our use case is a shared repository of
configuration info where different persons all commit as root; but we
want to make sure they override the --author to something sensible.

What would be the preferred mechanism to pass on this information?  It
could for instance be arguments to the hook script or via environment
variables.
It would make sense to me for it to be passed in through the environment
using GIT_{AUTHOR,COMMITTER}_{NAME,EMAIL}. You could even pass
GIT_AUTHOR_DATE to detect if somebody is using "git commit -c" to get
the date from a previous commit.
I agree that would be a more natural interface.  Attached is a patch
that sets these environment variables before the hooks are invoked.
The patch also updates the documentation and adds some tests.

Regards,
Gisle

Re: Make author and committer available to pre-commit hook

From: Ævar Arnfjörð Bjarmason <hidden>
Date: 2016-06-15 22:49:05

On Mon, Jul 5, 2010 at 18:04, Gisle Aas [off-list ref] wrote:
I agree that would be a more natural interface.  Attached is a patch
that sets these environment variables before the hooks are invoked.
The patch also updates the documentation and adds some tests.
Hi, just a note that patches submitted for inclusion in Git should
have [PATCH] in the subject, be submitted inline and you should CC
Junio. See Documentation/SubmittingPatches for the instructions.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help