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

Re: Syntax check via update hook?

From
Jeff King <peff@peff.net>
Date
May 9, 2009, 14:47 UTC
Message-ID
<20090509144711.GA3926@sigill.intra.peff.net>
In-Reply-To
<wsprejtm71.fsf@usg.edu>
On Fri, May 08, 2009 at 03:43:30PM -0400, Mark Plaksin wrote:
Show 14 quoted lines
> What's the best way to make the update hook do a syntax check?
> 
> We want to switch our Puppet [1] config repository from SVN to Git.  Our
> SVN repository has a pre-commit hook that does a syntax check.  The hook
> runs Puppet to check the syntax of the file(s) being committed and if the
> check fails, the commit fails.  With SVN that hook runs on the server so
> it's easy to have (the correct version of) the puppet binary there for
> the hook to use.
> 
> Once Puppet config changes are committed to to our SVN repository they
> are automatically pushed into our production Puppet config.  We want to
> do something similar with Git--once commits are successfully pushed to
> the master repository they are automatically pulled and become our
> production Puppet config.

That sounds like a reasonable goal. In the hook itself, you can do one of:

  1. Look at the diff to the original version to make sure you're not
     introducing anything bogus. This is sufficient for syntax issues on
     single lines, etc, and is obviously pretty efficient. But it won't
     cover "does this whole thing build?" types of checks, which I think
     is what you want.
  2. Check out the whole tree to a temp directory and run your full
     syntax check there. The simplest way would be by using "git
     archive" to generate the tree. You can make this a bit more
     efficient by keeping the temp directory between runs and using
     git to just update the changed files. Off the top of my head, that
     would look something like:
        $ cat <<EOF >.git/hooks/update
        #!/bin/sh
        GIT_INDEX_FILE=/path/to/tempdir/index; export GIT_INDEX_FILE
        cd /path/to/tempdir/tree &&
        mkdir -p tree &&
        git read-tree "$3" &&
        git checkout-index &&
        your_syntax_check
        EOF
        $ chmod +x .git/hooks/update
-Peff
Previous: Mark PlaksinNext: Mark Plaksin
Message 2 of 8 in “Syntax check via update hook?”
  1. Mark PlaksinMay 8, 2009
  2. Jeff KingMay 9, 2009
  3. Mark PlaksinMay 12, 2009
  4. Jeff KingMay 14, 2009
  5. Mark PlaksinMay 14, 2009
  6. Mark PlaksinMay 20, 2009
  7. Jeff KingMay 20, 2009
  8. Mark PlaksinMay 20, 2009

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.