git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Make author and committer available to pre-commit hook

From
Gisle Aas <gisle@aas.no>
Date
Jul 5, 2010, 11:30 UTC
Message-ID
<AANLkTinpCDRf_Yhuj2-tdZwmvHk8yna1Xjdtbrmx35CB@mail.gmail.com>

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) {
Next: Jeff King
Message 1 of 8 in “Make author and committer available to pre-commit hook”
  1. Gisle AasJul 5, 2010
  2. Jeff KingJul 5, 2010
  3. Gisle AasJul 5, 2010
  4. Ævar Arnfjörð BjarmasonJul 5, 2010
  5. Junio C HamanoJul 6, 2010
  6. Jeff KingJul 6, 2010
  7. Gisle AasJul 6, 2010
  8. Junio C HamanoJul 6, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.