MTrackWeb/Hook/git.php
[web.mtrack] / MTrackWeb / Hook / git.php
1 <?php  
2
3 /****
4  *
5  *  usage:
6  *
7  *  #repo/hooks/post-receive
8  *  /usr/bin/php {PATH TO APPLICATION}/index.php Hook/git post  >> /tmp/githooklog
9  *
10  *
11  *
12  */
13
14
15
16 /* For licensing and copyright terms, see the file named LICENSE */
17 // called as:
18
19 // git-commit-hook what [mtrackconfig] > /tmp/gitlog
20 // the cwd is the repo path
21 // a list of "oldrev newrev refname" lines is presented to us on stdin
22
23  
24 require_once 'MTrackWeb.php';
25
26 class MTrackWeb_Hook_git extends MTrackWeb
27 {
28     function getAuth()
29     {
30         if (!HTML_FlexyFramework::get()->cli) {
31             die("NOT CLI!");
32         }
33         
34     }
35      
36     function get($action)
37     {
38         try {
39             $repo = DB_DataObject::factory('mtrack_repos');
40             $repo->repopath = getcwd();
41             if (!$repo->find(true)) {
42                 $this->addEvent("HOOKERROR", false, "invalid repo:". getcwd() );
43                 die("INVALID REPO" . getcwd() );
44             }
45             echo "running hook";
46             
47             require_once 'MTrack/SCM/Git/CommitHookBridge.php';
48             
49             $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
50             
51             // for POST commit, we can use bridge->commits[0] to determine who..
52             $revs = $repo->history('.',1,'rev', $bridge->commits[0]);
53             
54             //print_R($revs);
55             
56             $who = $revs[0]->changebyToEmail();
57             
58             $this->authUser = DB_DataObject::factory('Person');
59             $this->authUser->get('email', $who); 
60             
61             $cfg = HTML_FlexyFramework::get()->MTrackWeb;
62             //print_R($cfg);
63             
64             
65             require_once 'MTrack/CommitChecker.php';
66             
67             $checker = new MTrack_CommitChecker( array(
68                     'repo' => $repo,
69                     'checks' => $cfg['checks'],
70                     'authUser' => $this->authUser,
71             ));
72              
73              
74             switch ($action) {
75                 
76                 case 'pre':
77                     $checker->preCommit($bridge);
78                     break;
79                     
80                 default:
81                     $checker->postCommit($bridge);
82                     print_r($checker);
83                   
84             }
85             //_log("SUCCESS");
86             exit(0);
87             
88             
89             
90         } catch (Exception $e) {
91              
92           fwrite(STDERR, "\n" . $e->getMessage() .
93             "\n\n" .
94             $e->getTraceAsString() .
95             "\n\n ** Commit failed [$action]\n");
96         
97           exit(1);
98         }
99         
100     }
101     
102 }
103