e17b5d658c161f0dcff429a0f6f66939cf1a2962
[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              
93             switch ($action) {
94                 
95                 case 'pre':
96                     $checker->preCommit($bridge);
97                     break;
98                     
99                 default:
100                     $checker->postCommit($bridge);
101                     
102                     // at this point we have
103                     // checker->no_ticket (contains commits without tickets
104                     // print_r($checker);exit;
105                     
106                     //DB_DataObject::DebugLevel(1);
107                     foreach($checker->no_ticket as $change_event) {
108                         $cg = DB_DataObject::factory('mtrack_change');
109                         $res= $cg->createFromCommit($change_event, $checker);
110                         if (!$res) {
111                             echo "Skip - commit already exists\n";
112                         }
113                     }
114                     foreach($checker->deferred as $ticket=> $info) {
115                         foreach($info['changes'] as $ev) {
116                             $cg = DB_DataObject::factory('mtrack_change');
117                             $res=  $cg->createFromCommit($ev, $checker, $info['ticket']);
118                             if (!$res) {
119                                 echo "Skip - commit already exists\n";
120                             }
121                             
122                         }
123                         
124                         
125                     }
126                     //print_r($checker);
127                   
128             }
129             //_log("SUCCESS");
130             exit(0);
131             
132             
133             
134         } catch (Exception $e) {
135              
136           fwrite(STDERR, "\n" . $e->getMessage() .
137             "\n\n" .
138             $e->getTraceAsString() .
139             "\n\n ** Commit failed [$action]\n");
140         
141           exit(1);
142         }
143         
144     }
145     
146 }
147