All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] post-commit hook to "fix" patches
@ 2009-02-11  9:48 Stefan Karpinski
  2009-02-11  9:56 ` Stefan Karpinski
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Karpinski @ 2009-02-11  9:48 UTC (permalink / raw)
  To: git

I just wrote the following post-commit hook that I thought others
might find interesting or useful. It's a non-intrusive, efficient and
IMO elegant solution to the problem that I was trying to solve here:

  http://thread.gmane.org/gmane.comp.version-control.git/98228

The post-commit hook calls a named "git-fix-patch", presumed to be in
the path somewhere, which munges a git diff to enforce whatever
stylistic rules a project might have (typically whitespace)---but only
in added lines. The hook creates a new commit with the same effect as
the HEAD commit, but without the added style errors.

That part's easy. What's much more interesting is making sure that the
workspace is left essentially the way it was, but reflecting the
changes wrought by the "enforcer." This involves generating reverse
patches, fixing them up, and then applying the fixed up patch in
reverse. I had to think about this for a long time (and draw a bunch
of commutative diagrams) to grok that this was what needed to be done,
but it seems to work quite nicely and generally.

Here's the post-commit hook:

== >8 ==
#!/bin/sh

if test -z "$GIT_POST_COMMIT_HOOK_RUNNING"
then
  export GIT_POST_COMMIT_HOOK_RUNNING=true
  x=$(git rev-parse HEAD)
  git reset -q $x^
  git diff -U0 $x^ $x | git-fix-patch | \
    git apply --unidiff-zero --cached
  if git diff --cached --quiet $x
  then
    git reset -q $x
  else
    echo "Automatically amending style errors..."
    git commit -q -C $x
    if git diff -R -U0 $x | git-fix-patch | \
      git apply -R --unidiff-zero --cached --check 2>/dev/null
    then
      git diff -R -U0 $x | git-fix-patch | \
        git apply -R --unidiff-zero --cached 2>/dev/null
      git checkout-index -a -f
      git reset -q
    else
      echo "WARNING: Failed patching uncommitted changes."
    fi
  fi
  unset GIT_POST_COMMIT_HOOK_RUNNING
fi
== >8 ==

I also wrote a sample git-fix-patch script that enforces the following
coding styles:
  1. no trailing whitespace
  2. all leading indentation with tabs (assumed to have width $t, below)
  3. indentation only with spaces after the initial indentation

== >8 ==
#!/usr/bin/env perl

$t=4;

while (<>) {
  if (m{^\+}) {
    s{\s*$}{\n};
    while (s{\t}{" "x($t-((length($`)-1)%$t))}e) {0}
    s{^\+((?: {$t})+)}{"+"."\t"x(length($1)/$t)}e;
  }
  print;
}
== >8 ==

I'd be really interested in reactions or thoughts about this. Maybe it
can even go into the collection of contrib scripts.

Stefan

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [RFC] post-commit hook to "fix" patches
  2009-02-11  9:48 [RFC] post-commit hook to "fix" patches Stefan Karpinski
@ 2009-02-11  9:56 ` Stefan Karpinski
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Karpinski @ 2009-02-11  9:56 UTC (permalink / raw)
  To: git
  Cc: Alex Riesen, Johannes Sixt, Jonathan del Strother,
	A Large Angry SCM, Marc Weber

I'm CC'ing a couple people who replied to the thread I referenced
earlier, in case they're interested and would otherwise miss this
message.

Especially Marc Weber, who expressed interest in having a good
solution to this problem. Does this address what you wanted? I like it
because it allows you to transparently and automatically enforce style
policy and:

  - it doesn't require modifying git,
  - it's efficient (no overhead except on commit),
  - it doesn't pretend to be a clean/smudge filter (as people pointed
out this isn't reversible),
  - it's portable: everything's written in sh and perl.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-02-11  9:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-11  9:48 [RFC] post-commit hook to "fix" patches Stefan Karpinski
2009-02-11  9:56 ` Stefan Karpinski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.