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