githelper.git
/
githelper.pike
version
»
Context lines:
10
20
40
80
file
none
3
githelper.git/githelper.pike:220:
return type=='O' && sprintf("GitAttributes(%O)\n", attrs); } } /* Hooks */ class CommitHookUtils {
+
static array(string) files_to_commit;
+
+
string get_file(string filename);
+
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; } 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 */ class PreCommitHook { 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, // 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_
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" - ({""}), 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) {
githelper.git/githelper.pike:304:
break; if (check_ident(filename)) return 1; break; } } } 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")) { 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");
githelper.git/githelper.pike:342:
return 1; } } } } return 0; } 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();
} } /* Checks run before accepting a push */ class PreReceiveHook { 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); 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; } }
githelper.git/githelper.pike:388:
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(
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")) { 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));
githelper.git/githelper.pike:441:
return 1; } } } return 0; } 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) { if(old_sha == "0"*40) { // New ref, maybe check if the name is allowed... return 0; } else { foreach(run_git("rev-list", old_sha+".."+new_sha)/"\n", string sha) if(sizeof(sha) && check_commit(sha))