threads / bug / 40820

[BUG] pre-applypatch can no longer add to staging area

Subject: [BUG] pre-applypatch can no longer add to staging area

## tl;dr

One message between Nov 18, 2015 and Nov 18, 2015.

replies: 0people: 1as markdown or json

Chris Packham· Nov 18, 2015, 02:15 UTC · lore
Hi,

At $dayjob we've been using a pre-applypatch hook to keep an eye on some software metrics as sub-area maintainers receive changes from developers. When a particular metric goes up we inform the maintainer that they should reject the patch and stop 'git am' from continuing. When the metric goes down automatically update the stored metric count and include that change in the commit. Prior to git 2.6.3 this was working, its probably prior to 2.6.0 the last version I know worked was 2.5.3

Consider the following pre-applypatch hook to stop people adding more uses of strcpy() to an existing code base.

  #/bin/sh
  current=$(cat .count)
  new=$(git grep -c strcpy)
  if test "$new" -gt "$current"; then
    echo "Bad patch"
    exit 1
  fi
  if  test "$new" -lt "$current"; then
     echo "Awesome patch"
     echo "$new" >.count
     git add .count
  fi
  exit 0

When a maintainer runs 'git am patch.patch' the hook would run and record the new metric value in the staging area before the commit is created.

Based on my reading of the pre-applypatch documentation I think this is a valid use-case. The hook is invoked with the changes applied but before they are committed so it should be OK to add additional changes to the staging area as part of the hook. Interestingly with the newer git version the change is not even staged.

Thanks, Chris

← back to recent threads