githelper.git
/
githelper.pike
version
»
Context lines:
10
20
40
80
file
none
3
githelper.git/githelper.pike:1:
#! /usr/bin/env pike
-
constant
hooks = ([
+
mapping(string:program)
hooks = ([
"pre-commit" : PreCommitHook, ]);
-
constant
filters = ([
+
mapping(string:program)
filters = ([
"nice_ident" : NiceIdentFilter, ]); constant filterops = ({ "clean", "smudge" }); void fail(string msg, mixed ... args) { werror(msg, @args);
githelper.git/githelper.pike:21:
void iofailn(int errno, string msg, mixed ... args) { fail(msg+": %s\n", @args, strerror(errno)); } void iofail(string msg, mixed ... args) { iofailn(errno(), msg, @args); }
+
array(string) split_z(string data)
+
{
+
array(string) a = data / "\0";
+
if (sizeof(a) && a[-1] == "")
+
a = a[..sizeof(a)-2];
+
return a;
+
}
+
string run_git_ex(int max_exitcode, string ... args) { mapping res = Process.run(({"git"})+args); if (res->exitcode > max_exitcode) { werror(res->stderr); fail("git exited with code %d\n", res->exitcode); } return res->stdout; }
githelper.git/githelper.pike:42:
{ return run_git_ex(0, @args); } /* Hooks */ /* Checks run before editing a commit message */ class PreCommitHook {
-
int
hook
()
+
int
check_attributes_staged
()
{
-
write("
pre
-
commit\n
");
+
// 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_blocker_attributes(array(string) files_to_commit)
+
{
+
constant attrs_to_check = ({ "foreign_ident", "block_commit" });
+
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;
+
}
+
}
+
}
return 0; }
-
+
+
int hook()
+
{
+
array(string) files_to_commit =
+
split_z(run_git("diff", "--staged", "--name-only", "-z"));
+
return
+
check_attributes_staged() ||
+
check_blocker_attributes(files_to_commit);
}
-
+
}
/* Filters */ /* A sample filter, not really useful... */ class NiceIdentFilter { static string replace_id(string f, function(string:string) replace) { int p=0; while((p=search(f, "$Id", p)) >= 0) {