Branch: Tag:

2010-09-25

2010-09-25 13:48:09 by Marcus Comstedt <marcus@mc.pp.se>

Check for blocker attributes on commit.

1:   #! /usr/bin/env pike    - constant hooks = ([ + mapping(string:program) hooks = ([    "pre-commit" : PreCommitHook,   ]);    - constant filters = ([ + mapping(string:program) filters = ([    "nice_ident" : NiceIdentFilter,   ]);   
28:    iofailn(errno(), msg, @args);   }    + array(string) split_z(string data) + { +  array(string) a = data / "\0"; +  if (sizeof(a) && a[-1] == "") +  a = a[..sizeof(a)-2]; +  return a; + } +    string run_git_ex(int max_exitcode, string ... args)   {    mapping res = Process.run(({"git"})+args);
49:      class PreCommitHook   { -  int hook() +  int check_attributes_staged()    { -  write("pre-commit\n"); +  // We don't allow .gitattributes to differ between wt and index, +  // because that could mean the committed stuff ends up with different +  // attributes than they have right now... +  +  if (sizeof(run_git("diff", "--name-only", ".gitattributes"))) { +  write("You have unstaged changes to .gitattributes.\n" +  "Please add or stash them before commit.\n"); +  return 1; +  } +  } +  +  int check_blocker_attributes(array(string) files_to_commit) +  { +  constant attrs_to_check = ({ "foreign_ident", "block_commit" }); +  foreach(run_git("check-attr", @attrs_to_check, +  "--", @files_to_commit) / "\n" - ({""}), +  string line) { +  array(string) parts = line / ": "; +  if (sizeof(parts) != 3) +  fail("Unexpected output from git check-attr, please fix check_blocker_attributes()\n"); +  [string filename, string attribute, string value] = parts; +  if (value != "unspecified") { +  switch (attribute) { +  case "foreign_ident": +  write("File %s has the foreign_ident attribute. Please remove it before commit.\n", filename); +  return 1; +  case "block_commit": +  write("File %s is blocked from committing: %s\n", filename, +  replace(value, "-", " ")); +  return 1; +  } +  } +  }    return 0;    } -  +  +  int hook() +  { +  array(string) files_to_commit = +  split_z(run_git("diff", "--staged", "--name-only", "-z")); +  return +  check_attributes_staged() || +  check_blocker_attributes(files_to_commit);    } -  + }      /* Filters */