Branch: Tag:

2010-09-26

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

Improve code reuse a little.

227:      class CommitHookUtils   { +  static array(string) files_to_commit; +  +  string get_file(string filename); +     int find_expanded_ident(string data)    {    int p=0;
238:    return 0;    }    -  int find_expanded_ident_in_staged_file(string filename) +  int check_ident(string filename)    { -  return find_expanded_ident(get_staged_file(filename)); +  if (find_expanded_ident(get_file(filename))) { +  write("File %s contains an expanded ident.\n", filename); +  if(this_program == PreCommitHook) { +  write("Try 'git reset %s; git add %s', " +  "or remove the ident manually.\n", +  @({filename})*2);;    } -  -  int find_expanded_ident_in_committed_file(string sha, string filename) -  { -  return find_expanded_ident(get_committed_file(sha, filename)); +  return 1;    } -  +  return 0;    } -  + }      /* Checks run before editing a commit message */   
255:   {    inherit CommitHookUtils;    +  string get_file(string filename) +  { +  return get_staged_file(filename); +  } +     int check_attributes_staged()    {    // We don't allow .gitattributes to differ between wt and index,
268:    }    }    -  int check_ident(string filename) +  int check_blocker_attributes()    { -  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; -  } -  return 0; -  } -  -  int check_blocker_attributes(array(string) files_to_commit) -  { +     constant attrs_to_check = ({ "foreign_ident", "block_commit", "ident" });    foreach(run_git("check-attr", @attrs_to_check,    "--", @files_to_commit) / "\n" - ({""}),
311:    return 0;    }    -  int check_gitattributes_files(array(string) files_to_commit) +  int check_gitattributes_files()    {    foreach(files_to_commit, string filename)    if(has_suffix(filename, "/.gitattributes")) {
349:       int hook()    { -  array(string) files_to_commit = +  files_to_commit =    split_z(run_git("diff", "--staged", "--name-only", "-z"));    return    check_attributes_staged() || -  check_blocker_attributes(files_to_commit) || -  check_gitattributes_files(files_to_commit); +  check_blocker_attributes() || +  check_gitattributes_files();    }   }   
364:   {    inherit CommitHookUtils;    -  int check_ident(string sha, string filename) +  static string sha; +  +  string get_file(string filename)    { -  if (find_expanded_ident_in_committed_file(sha, filename)) { -  write("File %s contains an expanded ident.\n", filename); -  return 1; +  return get_committed_file(sha, filename);    } -  return 0; -  } +     -  int check_blocker_attributes(string sha, GitAttributes attrs, array(string) files_to_commit) +  int check_blocker_attributes(GitAttributes attrs)    {    foreach(files_to_commit, string filename) {    mapping(string:string|int) a = attrs->checkattr(filename);
395:    }    if(a->ident && a->ident != GitAttributes.ATTR_FALSE &&    a->ident != GitAttributes.ATTR_UNSET) { -  if (check_ident(sha, filename)) +  if (check_ident(filename))    return 1;    }    }    return 0;    }    -  int check_gitattributes_files(string sha, array(string) files_to_commit, -  GitAttributes attrs) +  int check_gitattributes_files(GitAttributes attrs)    {    foreach(files_to_commit, string filename)    if(has_suffix(filename, "/.gitattributes")) {
448:    int check_commit(string sha)    {    write("Checking commit %s\n", sha); -  array(string) committed_files = +  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(sha, attrs, committed_files) || -  check_gitattributes_files(sha, committed_files, attrs); +  return check_blocker_attributes(attrs) || +  check_gitattributes_files(attrs);    }       int check_push(string old_sha, string new_sha, string ref_name)