Branch: Tag:

2010-09-25

2010-09-25 17:19:32 by Marcus Comstedt <marcus@mc.pp.se>

Check for removal of lines from .gitattributes.

79:    }    }    -  int check_ident(string filename) +  int find_expanded_ident(string data)    { -  string data = get_staged_file(filename); +     int p=0;    while ((p = search(data, "$Id", p))>=0) { -  if (data[p..p+3] != "$Id$") { +  if (data[p..p+3] != "$Id$") +  return 1; +  p += 4; +  } +  return 0; +  } +  +  int find_expanded_ident_in_staged_file(string filename) +  { +  return find_expanded_ident(get_staged_file(filename)); +  } +  +  int check_ident(string filename) +  { +  if (find_expanded_ident_in_staged_file(filename)) {    write("File %s contains an expanded ident.\n"    "Try 'git reset %s; git add %s', "    "or remove the ident manually.\n",    @({filename})*3);    return 1;    } -  p += 4; -  } +     return 0;    }   
127:    return 0;    }    +  int check_gitattributes_files(array(string) files_to_commit) +  { +  foreach(files_to_commit, string filename) +  if(filename == ".gitattributes" || +  has_suffix(filename, "/.gitattributes")) +  { +  string dir=filename[..sizeof(filename)-15]; +  string diff = run_git("diff", "-p", "-Sforeign_ident", "--cached", +  "--", filename); +  if (sizeof(diff)) { +  int pos = search(diff, "\n@@"); +  if (pos >= 0) +  diff = diff[pos+1..]; +  foreach(diff/"\n", string line) +  if(sizeof(line) && +  (line[0]=='+' || line[0]=='-')) { +  int code, len; +  string fn; +  if(sscanf(line, "%c/%s foreign_ident%n", code, fn, len) != 3 || +  len != sizeof(line)) { +  write("Unsupported change of foreign_ident in %s\n", +  filename); +  return 1; +  } +  fn = dir+fn; +  if (code=='-' && search(files_to_commit, fn)<0) { +  write("Removed foreign_ident from unstaged file %s\n", fn); +  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); +  check_blocker_attributes(files_to_commit) || +  check_gitattributes_files(files_to_commit);    }   }