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  * to run this on old commits:
14  *
15  
16  
17   
18     git rev-list --since="last month"    --pretty=format:"%H %P" refs/heads/master \
19         | grep -v commit  \
20         | awk -v q='"' '{ print "echo " q $NF " " $1 " refs/heads/master" q " | /usr/bin/php /home/gitlive/web.mtrack/roo.php Hook/git post " } '                  \
21         | sh
22
23  
24  *
25  *
26  */
27
28
29
30 /* For licensing and copyright terms, see the file named LICENSE */
31 // called as:
32
33 // git-commit-hook what [mtrackconfig] > /tmp/gitlog
34 // the cwd is the repo path
35 // a list of "oldrev newrev refname" lines is presented to us on stdin
36
37  
38 require_once 'MTrackWeb.php';
39
40 class MTrackWeb_Hook_git extends MTrackWeb
41 {
42     
43     static $cli_desc = "Commit hook for git - see source for usage";
44     
45     function getAuth()
46     {
47         if (!HTML_FlexyFramework::get()->cli) {
48             die("NOT CLI!");
49         }
50         
51     }
52     
53     
54     
55      
56     function get($action)
57     {
58         try {
59             $repo = DB_DataObject::factory('mtrack_repos');
60             $repo->repopath = getcwd();
61             if (!$repo->find(true)) {
62                 $this->addEvent("HOOKERROR", false, "invalid repo:". getcwd() );
63                 die("INVALID REPO" . getcwd() );
64             }
65             echo "running hook";
66             
67             require_once 'MTrack/SCM/Git/CommitHookBridge.php';
68             
69             $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
70             
71             // for POST commit, we can use bridge->commits[0] to determine who..
72             $revs = $repo->history('.',1,'rev', $bridge->commits[0]);
73             
74             //print_R($revs);
75             
76             $who = $revs[0]->changebyToEmail();
77             
78             $this->authUser = DB_DataObject::factory('Person');
79             $this->authUser->get('email', $who); 
80             
81             $cfg = HTML_FlexyFramework::get()->MTrackWeb;
82             //print_R($cfg);
83             
84             
85             require_once 'MTrack/CommitChecker.php';
86             
87             
88             $checker = new MTrack_CommitChecker( array(
89                     'repo' => $repo,
90                     'checks' => $cfg['checks'],
91                     'authUser' => $this->authUser,
92             ));
93              
94              
95             switch ($action) {
96                 
97                 case 'pre':
98                     $checker->preCommit($bridge);
99                     break;
100                     
101                 default:
102                     $checker->postCommit($bridge);
103                     
104                     // at this point we have
105                     // checker->no_ticket (contains commits without tickets
106                     // print_r($checker);exit;
107                     
108                     DB_DataObject::DebugLevel(1);
109                     foreach($checker->no_ticket as $change_event) {
110                         $cg = DB_DataObject::factory('mtrack_change');
111                         $res= $cg->createFromCommit($change_event, $checker);
112                         if (!$res) {
113                             echo "Skip - commit already exists\n";
114                         }
115                         
116                     }
117                     foreach($checker->deferred as $ticket=> $info) {
118                         foreach($info['changes'] as $ev) {
119                             $cg = DB_DataObject::factory('mtrack_change');
120                             $res=  $cg->createFromCommit($ev, $checker, $info['ticket']);
121                             if (!$res) {
122                                 echo "Skip - commit already exists\n";
123                             }
124                             
125                         }
126                         
127                         
128                     }
129                     //print_r($checker);
130                   
131             }
132             //_log("SUCCESS");
133             exit(0);
134             
135             
136             
137         } catch (Exception $e) {
138              
139           fwrite(STDERR, "\n" . $e->getMessage() .
140             "\n\n" .
141             $e->getTraceAsString() .
142             "\n\n ** Commit failed [$action]\n");
143         
144           exit(1);
145         }
146         
147     }
148     
149 }
150