githelper.git / githelper.pike

version» Context lines:

githelper.git/githelper.pike:413:    {    write("Checking commit %s\n", sha);    this_program::sha = sha;    files_to_commit =    split_z(run_git("diff", "--name-only", "-z", sha, sha+"^"));    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) +  int check_tag_push(string old_sha, string new_sha, string ref_name)    { -  if(old_sha == "0"*40) { -  // New ref, maybe check if the name is allowed... +  string oldtag = +  String.trim_all_whites(run_git_ex(1, "rev-parse", "--verify", +  "-q", ref_name)); +  if (sizeof(oldtag) && oldtag != new_sha) { +  write("Tag %s already exists with value %s, will not move it\n", +  ref_name, oldtag); +  return 1; +  } +     return 0; -  +  } +  +  int check_branch_push(string old_sha, string new_sha, string ref_name) +  { +  if (old_sha == "0"*40) { +  // New branch, maybe check if the name is allowed... +  return 0;    } else { -  +  string merge_base = +  String.trim_all_whites(run_git("merge-base", old_sha, new_sha)); +  if (merge_base != old_sha) { +  write("Push to %s is not fast-forward.\n", ref_name); +  return 1; +  } +     foreach(split_lf(run_git("rev-list", old_sha+".."+new_sha)), string sha)    if(check_commit(sha))    return 1;    return 0;    }    }    -  +  int check_push(string old_sha, string new_sha, string ref_name) +  { +  if (has_prefix(ref_name, "refs/tags/")) { +  return check_tag_push(old_sha, new_sha, ref_name); +  } else if (has_prefix(ref_name, "refs/heads/")) { +  return check_branch_push(old_sha, new_sha, ref_name); +  } else { +  write("Trying to push a ref which is neither under refs/tags/ or refs/heads/...\n"); +  return 1; +  } +  } +     int hook()    {    foreach(split_lf(Stdio.stdin->read()), string line) {    array(string) args = line / " ";    if(sizeof(args) != 3)    fail("Unexpected input line to pre-receive hook: %s\n", line);    if(check_push(@args))    return 1;    }    return 0;