php8 fixes
[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     var $authUser;
43     
44     function getAuth()
45     {
46         if (!HTML_FlexyFramework::get()->cli) {
47             die("NOT CLI!");
48         }
49         
50     }
51     
52     
53     
54      
55     function get($action='')
56     {
57         try {
58             $repo = DB_DataObject::factory('mtrack_repos');
59             $repo->repopath = getcwd();
60             if (!$repo->find(true)) {
61                 $this->addEvent("HOOKERROR", false, "invalid repo:". getcwd() );
62                 die("INVALID REPO" . getcwd() );
63             }
64             echo "running hook";
65             
66             require_once 'MTrack/SCM/Git/CommitHookBridge.php';
67             
68             $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
69             
70             // for POST commit, we can use bridge->commits[0] to determine who..
71             $revs = $repo->history('.',1,'rev', $bridge->commits[0]);
72             
73             //print_R($revs);
74             
75             $who = $revs[0]->changebyToEmail();
76             
77             $this->authUser = DB_DataObject::factory('core_person');
78             $this->authUser->get('email', $who); 
79             
80             $cfg = HTML_FlexyFramework::get()->MTrackWeb;
81             //print_R($cfg);
82             
83             
84             require_once 'MTrack/CommitChecker.php';
85             
86             
87             $checker = new MTrack_CommitChecker( array(
88                     'repo' => $repo,
89                     'checks' => $cfg['checks'],
90                     'authUser' => $this->authUser,
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                     
115                     // in our system this happens when we merge normally.
116                     foreach($checker->deferred as $ticket=> $info) {
117                         foreach($info['changes'] as $ev) {
118                             $cg = DB_DataObject::factory('mtrack_change');
119                             $res=  $cg->createFromCommit($ev, $checker, $info['ticket']);
120                             if (!$res) {
121                                 echo "Skip - commit already exists\n";
122                             }
123                             
124                         }
125                         
126                         
127                     }
128                     //print_r($checker);
129                   
130             }
131             //_log("SUCCESS");
132             exit(0);
133             
134             
135             
136         } catch (Exception $e) {
137              
138           fwrite(STDERR, "\n" . $e->getMessage() .
139             "\n\n" .
140             $e->getTraceAsString() .
141             "\n\n ** Commit failed [$action]\n");
142         
143           exit(1);
144         }
145         
146     }
147     
148 }
149