Branch: Tag:

2010-09-26

2010-09-26 20:35:38 by Marcus Comstedt <marcus@mc.pp.se>

Improve code reuse yet a little more.

233:    GitAttributes attrs;       string get_file(string filename, int|void allow_empty); +  string get_old_file(string filename, int|void allow_empty);    int entry_is_new(string filename) { return 0; }       int find_expanded_ident(string data)
286:    }    return 0;    } +  +  int check_gitattributes_files() +  { +  foreach(files_to_commit, string filename) +  if(has_suffix(filename, "/.gitattributes")) { +  write(".gitattributes are not allowed in subdirectories\n"); +  return 1;    }    -  +  if(search(files_to_commit, ".gitattributes")>=0) { +  GitAttributes old_attrs = +  GitAttributes(get_old_file(".gitattributes", 1)); +  +  array(string) new_f_e = sort(attrs->findattr("foreign_ident")); +  array(string) old_f_e = sort(old_attrs->findattr("foreign_ident")); +  array(string) added_fe = new_f_e - old_f_e; +  array(string) removed_fe = old_f_e - new_f_e; +  +  foreach(added_fe, string path) { +  if(!has_prefix(path, "/") || search(path, "*")>=0) { +  write("Added unsupported foreign_ident: %s\n", path); +  return 1; +  } +  path = path[1..]; +  if (!entry_is_new(path)) { +  write("Added foreign_ident to file %s\n", path); +  return 1; +  } +  } +  +  foreach(removed_fe, string path) { +  if(has_prefix(path, "/")) +  path = path[1..]; +  if (search(files_to_commit, path)<0) { +  write("Removed foreign_ident from unchanged file %s\n", path); +  return 1; +  } +  } +  } +  return 0; +  } + } +    /* Checks run before editing a commit message */      class PreCommitHook
299:    return get_staged_file(filename, allow_empty);    }    +  string get_old_file(string filename, int|void allow_empty) +  { +  return get_committed_file("HEAD", filename, allow_empty); +  } +     int check_attributes_staged()    {    // We don't allow .gitattributes to differ between wt and index,
312:    }    }    -  int check_gitattributes_files() -  { -  foreach(files_to_commit, string filename) -  if(has_suffix(filename, "/.gitattributes")) { -  write(".gitattributes are not allowed in subdirectories\n"); -  return 1; -  } -  -  if(search(files_to_commit, ".gitattributes")>=0) { -  string diff = run_git("diff", "-p", "--cached", -  "--", ".gitattributes"); -  if (sizeof(diff)) { -  int pos = search(diff, "\n@@"); -  if (pos >= 0) -  diff = diff[pos+1..]; -  foreach(diff/"\n", string line) -  if(sizeof(line) && search(line, "foreign_ident")>=0 && -  search(line, "[attr]") != 1 && -  (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 .gitattributes\n"); -  return 1; -  } -  if (code=='-' && search(files_to_commit, fn)<0) { -  write("Removed foreign_ident from unstaged file %s\n", fn); -  return 1; -  } -  } -  } -  } -  return 0; -  } -  +     int hook()    {    files_to_commit =
373:    return get_committed_file(sha, filename, allow_empty);    }    +  string get_old_file(string filename, int|void allow_empty) +  { +  return get_committed_file(sha+"^", filename, allow_empty); +  } +     int entry_is_new(string filename)    {    return (!sizeof(run_git("ls-tree", sha+"^", "--", filename))) &&    sizeof(run_git("ls-tree", sha, "--", filename));    }    -  int check_gitattributes_files() -  { -  foreach(files_to_commit, string filename) -  if(has_suffix(filename, "/.gitattributes")) { -  write(".gitattributes are not allowed in subdirectories\n"); -  return 1; -  } -  -  if(search(files_to_commit, ".gitattributes")>=0) { -  GitAttributes old_attrs = -  GitAttributes(get_committed_file(sha+"^", ".gitattributes", 1)); -  -  array(string) new_f_e = sort(attrs->findattr("foreign_ident")); -  array(string) old_f_e = sort(old_attrs->findattr("foreign_ident")); -  array(string) added_fe = new_f_e - old_f_e; -  array(string) removed_fe = old_f_e - new_f_e; -  -  foreach(added_fe, string path) { -  if(!has_prefix(path, "/") || search(path, "*")>=0) { -  write("Added unsupported foreign_ident: %s\n", path); -  return 1; -  } -  path = path[1..]; -  if (sizeof(run_git("ls-tree", sha+"^", "--", path)) || -  !sizeof(run_git("ls-tree", sha, "--", path))) { -  write("Added foreign_ident to unadded file %s\n", path); -  return 1; -  } -  } -  -  foreach(removed_fe, string path) { -  if(has_prefix(path, "/")) -  path = path[1..]; -  if (search(files_to_commit, path)<0) { -  write("Removed foreign_ident from unchanged file %s\n", path); -  return 1; -  } -  } -  } -  return 0; -  } -  +     int check_commit(string sha)    {    write("Checking commit %s\n", sha);