githelper.git
/
githelper.pike
version
»
Context lines:
10
20
40
80
file
none
3
githelper.git/githelper.pike:1:
#! /usr/bin/env pike #define DOLLAR "$" constant unexpanded_id = DOLLAR"Id"DOLLAR; mapping(string:program) hooks = ([ "pre-commit" : PreCommitHook, "pre-receive" : PreReceiveHook,
+
"post-commit" : PostCommitHook,
]); mapping(string:program) filters = ([ #if 0 "nice_ident" : NiceIdentFilter, #endif ]);
githelper.git/githelper.pike:436:
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; } }
+
/* Do housekeeping after a commit */
+
+
class PostCommitHook
+
{
+
void cleanup(string filename, mapping(string:string|int) attr)
+
{
+
if(attr->ident && attr->ident != GitAttributes.ATTR_FALSE &&
+
attr->ident != GitAttributes.ATTR_UNSET &&
+
search(get_committed_file("HEAD", filename, 1), unexpanded_id)>=0)
+
if(sizeof(run_git("diff", "--name-only", "--", filename))) {
+
write("NOTICE: The file %s has a stale ident,\n but I won't touch it since you have unstaged changes.\n", filename);
+
} else {
+
// write("Checking out %s, to fix stale ident...\n", filename);
+
run_git("checkout", "HEAD", "--", filename);
+
}
+
}
+
+
int hook()
+
{
+
array(string) committed_files =
+
split_z(run_git("diff", "--name-only", "-z", "HEAD", "HEAD^"));
+
GitAttributes attrs =
+
GitAttributes(get_committed_file("HEAD", ".gitattributes", 1));
+
foreach(committed_files, string filename)
+
cleanup(filename, attrs->checkattr(filename));
+
return 0;
+
}
+
}
+
/* 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, DOLLAR"Id", p)) >= 0) { int p2 = search(f, DOLLAR, p+3), p3 = search(f, "\n", p+3);