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

Re: How to use git attributes to configure server-side checks?

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 21, 2011, 20:17 UTC
Message-ID
<7vy5xh1whq.fsf@alter.siamese.dyndns.org>
In-Reply-To
<4E7A3BDE.3040301@alum.mit.edu>
Michael Haggerty <mhagger@alum.mit.edu> writes:
Show 24 quoted lines
> I was thinking of using git attributes to configure a server-side
> "update" hook that does some basic sanity checking before accepting a
> push.  I thought I could do something like
>
> ~/.gitattributes:
>     *.c whitespace
>
> ~/crappy-vendor-code/.gitattributes:
>     # This code doesn't conform to our standards; disable check:
>     *.c -whitespace
>
> This would allow fine-grained specification of which checks are applied
> to which files, and ensure that the hook configuration is kept
> synchronized with changes to the content.
>
> What I can't figure out is how a server-side update hook can inquire
> about the gitattributes that were associated with a file *in a
> particular commit*, as opposed to in the current working tree.  I would
> like to ask questions like "was the "whitespace" attribute set on file F
> in commit C?"
>
> I see that there is an (undocumented) API involving
> git_attr_set_direction() that seems to let gitattributes to be read out
> of the index instead of the working tree.  But I am still confused:

The words "server side" automatically mean that there should be no working tree, and where there is no working tree there should be no index, so the direction should not make any difference. The attributes that are used to help whitespace checks should come from project.git/info/attributes in such a case [*1*].

As to the actual checking of the pushed contents, your pre-receive hook is called after all the objects received are placed in the object store, but before the refs are updated to conclude the push, and you can veto the push by exiting with non-zero status from the hook. Your hook will get which ref is being updated from what old commit to what new commit, so you can either

 (1) grab the new commits introduced to the project using rev-list, and
     invoke "git show --check" on each and every one of them; or
 (2) check only the endpoints, by running "git diff --check" between the
     old and new commits. A pushed series may introduce a breakage early
     in the series which is corrected later in the series and you would
     not catch such a case if you used this method.
[Footnote]

*1* granted, that there are people who make a checkout from their post update hook, perhaps so that a build robot can be told to work on it or the web server can deliver individual files. But that is merely a crude substitute of having a proper "install" procedure. As far as the "server-side" Git that accepts "git push" is concerned, such a working tree does not exist.

Previous: Jay SoffianNext: Michael Haggerty
Message 3 of 31 in “How to use git attributes to configure server-side checks?”
  1. Michael HaggertySep 21, 2011
  2. Jay SoffianSep 21, 2011
  3. Junio C HamanoSep 21, 2011
  4. Michael HaggertySep 22, 2011
  5. Jay SoffianSep 22, 2011
  6. Jeff KingSep 22, 2011
  7. Jay SoffianSep 22, 2011
  8. Junio C HamanoSep 22, 2011
  9. Jeff KingSep 22, 2011
  10. Jeff KingSep 22, 2011
  11. Michael HaggertySep 23, 2011
  12. Jeff KingSep 23, 2011
  13. Junio C HamanoSep 23, 2011
  14. Jeff KingSep 23, 2011
  15. Michael HaggertySep 24, 2011
  16. Jeff KingSep 24, 2011
  17. Michael HaggertySep 24, 2011
  18. Junio C HamanoSep 26, 2011
  19. Michael HaggertySep 26, 2011
  20. Jeff KingSep 26, 2011
  21. Jakub NarebskiSep 26, 2011
  22. Michael HaggertySep 26, 2011
  23. Junio C HamanoSep 22, 2011
  24. Michael HaggertySep 23, 2011
  25. Stephen BashSep 23, 2011
  26. Michael HaggertySep 23, 2011
  27. Jakub NarebskiSep 22, 2011
  28. Michael HaggertySep 23, 2011
  29. Michael HaggertyFeb 17, 2012
  30. Junio C HamanoFeb 17, 2012
  31. Junio C HamanoFeb 17, 2012

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.