Branch: Tag:

2010-09-26

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

Improve code reuse a little more.

54:    return run_git_ex(0, @args);   }    - string get_staged_file(string filename) + string get_staged_file(string filename, int|void allow_empty)   { -  string sha; -  if (2 != sscanf(run_git("ls-files", "--stage", "--", filename), -  "%*o %s ", sha)) +  string blob; +  string treeentry = run_git("ls-files", "--stage", "--", filename); +  if (allow_empty && !sizeof(treeentry)) +  return ""; +  if (2 != sscanf(treeentry, "%*o %s ", blob))    fail("Unable to parse output from git ls-files...\n"); -  return run_git("cat-file", "blob", sha); +  return run_git("cat-file", "blob", blob);   }      string get_committed_file(string sha, string filename, int|void allow_empty)   {    string blob; -  string attrentry = run_git("ls-tree", sha, "--", filename); -  if (allow_empty && !sizeof(attrentry)) +  string treeentry = run_git("ls-tree", sha, "--", filename); +  if (allow_empty && !sizeof(treeentry))    return ""; -  if (2 != sscanf(attrentry, "%*o blob %s\t", blob)) +  if (2 != sscanf(treeentry, "%*o blob %s\t", blob))    fail("Unexpected output from git ls-tree\n");    return run_git("cat-file", "blob", blob);   }
228:   class CommitHookUtils   {    static array(string) files_to_commit; +  GitAttributes attrs;    -  string get_file(string filename); +  string get_file(string filename, int|void allow_empty); +  int entry_is_new(string filename) { return 0; }       int find_expanded_ident(string data)    {
255:    }    return 0;    } +  +  int check_blocker_attributes() +  { +  foreach(files_to_commit, string filename) { +  mapping(string:string|int) a = attrs->checkattr(filename); +  if(a->foreign_ident == GitAttributes.ATTR_TRUE) { +  if (!entry_is_new(filename)) { +  write("File %s has the foreign_ident attribute. Please remove it before commit.\n", filename); +  return 1;    } -  +  } +  if(stringp(a->block_commit) || a->block_commit == GitAttributes.ATTR_TRUE) { +  if (!entry_is_new(filename)) { +  write("File %s is blocked from committing: %s\n", filename, +  replace((stringp(a->block_commit)? a->block_commit : +  "no explanation given"), "-", " ")); +  return 1; +  } +  } +  if(a->ident && a->ident != GitAttributes.ATTR_FALSE && +  a->ident != GitAttributes.ATTR_UNSET) { +  if (check_ident(filename)) +  return 1; +  } +  } +  return 0; +  } + }      /* Checks run before editing a commit message */   
263:   {    inherit CommitHookUtils;    -  string get_file(string filename) +  string get_file(string filename, int|void allow_empty)    { -  return get_staged_file(filename); +  return get_staged_file(filename, allow_empty);    }       int check_attributes_staged()
281:    }    }    -  int check_blocker_attributes() -  { -  constant attrs_to_check = ({ "foreign_ident", "block_commit", "ident" }); -  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; -  case "ident": -  if (value == "unset") -  break; -  if (check_ident(filename)) -  return 1; -  break; -  } -  } -  } -  return 0; -  } -  +     int check_gitattributes_files()    {    foreach(files_to_commit, string filename)
352:    {    files_to_commit =    split_z(run_git("diff", "--staged", "--name-only", "-z")); +  attrs = GitAttributes(get_file(".gitattributes", 1));    return    check_attributes_staged() ||    check_blocker_attributes() ||
367:       static string sha;    -  string get_file(string filename) +  string get_file(string filename, int|void allow_empty)    { -  return get_committed_file(sha, filename); +  return get_committed_file(sha, filename, allow_empty);    }    -  int check_blocker_attributes(GitAttributes attrs) +  int entry_is_new(string filename)    { -  foreach(files_to_commit, string filename) { -  mapping(string:string|int) a = attrs->checkattr(filename); -  if(a->foreign_ident == GitAttributes.ATTR_TRUE) { -  if (sizeof(run_git("ls-tree", sha+"^", "--", filename)) || -  !sizeof(run_git("ls-tree", sha, "--", filename))) { -  write("File %s has the foreign_ident attribute. Please remove it before commit.\n", filename); -  return 1; +  return (!sizeof(run_git("ls-tree", sha+"^", "--", filename))) && +  sizeof(run_git("ls-tree", sha, "--", filename));    } -  } -  if(stringp(a->block_commit) || a->block_commit == GitAttributes.ATTR_TRUE) { -  if (sizeof(run_git("ls-tree", sha+"^", "--", filename)) || -  !sizeof(run_git("ls-tree", sha, "--", filename))) { -  write("File %s is blocked from committing: %s\n", filename, -  replace((stringp(a->block_commit)? a->block_commit : -  "no explanation given"), "-", " ")); -  return 1; -  } -  } -  if(a->ident && a->ident != GitAttributes.ATTR_FALSE && -  a->ident != GitAttributes.ATTR_UNSET) { -  if (check_ident(filename)) -  return 1; -  } -  } -  return 0; -  } +     -  int check_gitattributes_files(GitAttributes attrs) +  int check_gitattributes_files()    {    foreach(files_to_commit, string filename)    if(has_suffix(filename, "/.gitattributes")) {
449:    this_program::sha = sha;    files_to_commit =    split_z(run_git("diff", "--name-only", "-z", sha, sha+"^")); -  string attrtext = get_committed_file(sha, ".gitattributes", 1); -  GitAttributes attrs = GitAttributes(attrtext); -  return check_blocker_attributes(attrs) || -  check_gitattributes_files(attrs); +  attrs = GitAttributes(get_file(".gitattributes", 1)); +  return check_blocker_attributes() || +  check_gitattributes_files();    }       int check_push(string old_sha, string new_sha, string ref_name)