githelper.git
/
githelper.pike
version
»
Context lines:
10
20
40
80
file
none
3
githelper.git/githelper.pike:226:
/* Hooks */ class CommitHookUtils { static array(string) files_to_commit; 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) { int p=0; while ((p = search(data, "$Id", p))>=0) { if (data[p..p+3] != "$Id$") return 1; p += 4; }
githelper.git/githelper.pike:279:
} } 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()
+
{
+
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 { inherit CommitHookUtils; string get_file(string filename, int|void allow_empty) { 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, // because that could mean the committed stuff ends up with different // attributes than they have right now... if (sizeof(run_git("diff", "--name-only", ".gitattributes"))) { write("You have unstaged changes to .gitattributes.\n" "Please add or stash them before commit.\n"); return 1; } }
-
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 = split_z(run_git("diff", "--staged", "--name-only", "-z")); attrs = GitAttributes(get_file(".gitattributes", 1)); return check_attributes_staged() || check_blocker_attributes() || check_gitattributes_files(); }
githelper.git/githelper.pike:366:
{ inherit CommitHookUtils; static string sha; string get_file(string filename, int|void allow_empty) { 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); 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(); }