-#!/usr/bin/env php
-<?php # vim:ts=2:sw=2:et:ft=php:
-/* For licensing and copyright terms, see the file named LICENSE */
-// called as:
+<?php
-// git-commit-hook what [mtrackconfig] > /tmp/gitlog
-// the cwd is the repo path
-// a list of "oldrev newrev refname" lines is presented to us on stdin
-
-
-die("MAKE INTO CLASS");
+/****
+ *
+ * usage:
+ *
+ * #repo/hooks/post-receive
+ * /usr/bin/php {PATH TO APPLICATION}/index.php Hook/git post >> /tmp/githooklog
+ *
+ *
+ *
+ *
+ * to run this on old commits:
+ *
+
+ git rev-list --since="last month" --pretty=format:"%H %P" refs/heads/master \
+ | grep -v commit \
+ | awk -v q='"' '{ print "echo " q $NF " " $1 " refs/heads/master" q " | /usr/bin/php /home/gitlive/web.mtrack/roo.php Hook/git post " } ' \
+ | sh
-$action = $argv[1];
+
+ *
+ *
+ */
-include dirname(__FILE__) . '/../inc/common.php';
-require_once 'SCM/Git/CommitHookBridge.php';
-MTrackConfig::checkInitializing();
+/* For licensing and copyright terms, see the file named LICENSE */
+// called as:
-//MTrackCommitChecker::addCheck('NoEmptyLogMessage');
-//MTrackCommitChecker::addCheck('BlankLines');
-//MTrackCommitChecker::addCheck('UnixLineBreak');
-//MTrackCommitChecker::addCheck('SingleIssue');
+// git-commit-hook what [mtrackconfig] > /tmp/gitlog
+// the cwd is the repo path
+// a list of "oldrev newrev refname" lines is presented to us on stdin
-//ini_set('display_errors', true);
+require_once 'MTrackWeb.php';
-try {
+class MTrackWeb_Hook_git extends MTrackWeb
+{
- $repo = MTrackRepo::loadByLocation(getcwd());
- $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
+ static $cli_desc = "Commit hook for git - see source for usage";
- // WTF is this for.. it the hook should be telling who the user is.
- $author = 'www-data';//MTrackAuth::whoami();
- $author = mtrack_canon_username($author);
- MTrackAuth::su($author);
-
- $checker = new MTrackCommitChecker($repo);
-
- switch ($argv[1]) {
+ function getAuth()
+ {
+ if (!HTML_FlexyFramework::get()->cli) {
+ die("NOT CLI!");
+ }
- case 'pre':
- $checker->preCommit($bridge);
- break;
-
- default:
- $checker->postCommit($bridge);
-
}
- //_log("SUCCESS");
- exit(0);
-} catch (Exception $e) {
- fwrite(STDERR, "\n" . $e->getMessage() .
- "\n\n" .
- $e->getTraceAsString() .
- "\n\n ** Commit failed [$action]\n");
-
- exit(1);
+ function get($action)
+ {
+ try {
+ $repo = DB_DataObject::factory('mtrack_repos');
+ $repo->repopath = getcwd();
+ if (!$repo->find(true)) {
+ $this->addEvent("HOOKERROR", false, "invalid repo:". getcwd() );
+ die("INVALID REPO" . getcwd() );
+ }
+ echo "running hook";
+
+ require_once 'MTrack/SCM/Git/CommitHookBridge.php';
+
+ $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
+
+ // for POST commit, we can use bridge->commits[0] to determine who..
+ $revs = $repo->history('.',1,'rev', $bridge->commits[0]);
+
+ //print_R($revs);
+
+ $who = $revs[0]->changebyToEmail();
+
+ $this->authUser = DB_DataObject::factory('core_person');
+ $this->authUser->get('email', $who);
+
+ $cfg = HTML_FlexyFramework::get()->MTrackWeb;
+ //print_R($cfg);
+
+
+ require_once 'MTrack/CommitChecker.php';
+
+
+ $checker = new MTrack_CommitChecker( array(
+ 'repo' => $repo,
+ 'checks' => $cfg['checks'],
+ 'authUser' => $this->authUser,
+ ));
+
+
+ switch ($action) {
+
+ case 'pre':
+ $checker->preCommit($bridge);
+ break;
+
+ default:
+ $checker->postCommit($bridge);
+
+ // at this point we have
+ // checker->no_ticket (contains commits without tickets
+ // print_r($checker);exit;
+
+ //DB_DataObject::DebugLevel(1);
+ foreach($checker->no_ticket as $change_event) {
+ $cg = DB_DataObject::factory('mtrack_change');
+ $res= $cg->createFromCommit($change_event, $checker);
+ if (!$res) {
+ echo "Skip - commit already exists\n";
+ }
+
+ }
+ foreach($checker->deferred as $ticket=> $info) {
+ foreach($info['changes'] as $ev) {
+ $cg = DB_DataObject::factory('mtrack_change');
+ $res= $cg->createFromCommit($ev, $checker, $info['ticket']);
+ if (!$res) {
+ echo "Skip - commit already exists\n";
+ }
+
+ }
+
+
+ }
+ //print_r($checker);
+
+ }
+ //_log("SUCCESS");
+ exit(0);
+
+
+
+ } catch (Exception $e) {
+
+ fwrite(STDERR, "\n" . $e->getMessage() .
+ "\n\n" .
+ $e->getTraceAsString() .
+ "\n\n ** Commit failed [$action]\n");
+
+ exit(1);
+ }
+
+ }
+
}