Fix #6661 - modify hooks to update timeshetet
[web.mtrack] / MTrackWeb / Hook / git.php
index 4638d46..ce00629 100755 (executable)
@@ -1,5 +1,30 @@
-#!/usr/bin/env php
-<?php # vim:ts=2:sw=2:et:ft=php:
+<?php  
+
+/****
+ *
+ *  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
+
+ *
+ *
+ */
+
+
+
 /* For licensing and copyright terms, see the file named LICENSE */
 // called as:
 
 // the cwd is the repo path
 // a list of "oldrev newrev refname" lines is presented to us on stdin
 
-
-die("MAKE INTO CLASS");
 require_once 'MTrackWeb.php';
 
-class MTrackWeb_Hook_Git extents MTrackWeb
+class MTrackWeb_Hook_git extends MTrackWeb
 {
+    
+    static $cli_desc = "Commit hook for git - see source for usage";
+    
     function getAuth()
     {
         if (!HTML_FlexyFramework::get()->cli) {
@@ -20,28 +47,49 @@ class MTrackWeb_Hook_Git extents MTrackWeb
         }
         
     }
+    
+    
+    
+     
     function get($action)
     {
-        
-
         try {
-            $repo = DB_DataObject::factory('mtrack_repo');
+            $repo = DB_DataObject::factory('mtrack_repos');
             $repo->repopath = getcwd();
             if (!$repo->find(true)) {
                 $this->addEvent("HOOKERROR", false, "invalid repo:". getcwd() );
-                die("INVALID REPO");
+                die("INVALID REPO" . getcwd() );
             }
+            echo "running hook";
+            
+            require_once 'MTrack/SCM/Git/CommitHookBridge.php';
             
             $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
             
-            // 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);
+            // 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';
+            
             
-            switch ($argv[1]) {
+            $checker = new MTrack_CommitChecker( array(
+                    'repo' => $repo,
+                    'checks' => $cfg['checks'],
+                    'authUser' => $this->authUser,
+            ));
+             
+            switch ($action) {
                 
                 case 'pre':
                     $checker->preCommit($bridge);
@@ -49,6 +97,34 @@ class MTrackWeb_Hook_Git extents MTrackWeb
                     
                 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";
+                        }
+                    }
+                    
+                    // in our system this happens when we merge normally.
+                    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");
@@ -69,58 +145,4 @@ class MTrackWeb_Hook_Git extents MTrackWeb
     }
     
 }
-
-
-$action = $argv[1]; 
-
-include dirname(__FILE__) . '/../inc/common.php';
-
-require_once 'SCM/Git/CommitHookBridge.php';
-
-MTrackConfig::checkInitializing();
-
-//MTrackCommitChecker::addCheck('NoEmptyLogMessage');
-//MTrackCommitChecker::addCheck('BlankLines');
-//MTrackCommitChecker::addCheck('UnixLineBreak');
-//MTrackCommitChecker::addCheck('SingleIssue'); 
-
-//ini_set('display_errors', true);
-
-try {
-    
-    $repo = MTrack_Repo::loadByLocation(getcwd());
-    $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
-    
-    // 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]) {
-        
-        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);
-}