php8 fixes
[web.mtrack] / MTrack / SCM / Git / Event.php
1 <?php
2
3 require_once 'MTrack/SCMEvent.php';
4 class MTrack_SCM_Git_Event extends MTrackSCMEvent 
5 {
6     
7     public $repo;
8     /** Revision or changeset identifier for this particular file */
9     public $rev;
10
11     /** commit message associated with this revision */
12     public $changelog;
13
14     /** who committed this revision */
15     public $changeby;
16     
17     /** branch for this revision */
18     public $branch;
19     /** when this revision was committed */
20     public $ctime;
21
22     /** files affected in this event; may be null, but otherwise
23     * will be an array of MTrackSCMFileEvent */
24     public $files;
25
26     public $commit;
27     var $tags;
28     var $files_array;
29     /**
30      * this is based on the output from git --raw --numstat
31      */
32     static function newFromCommit($commit, $repo)
33     {
34         
35         $ent = new MTrack_SCM_Git_Event;
36         $ent->commit = $commit;
37         $ent->repo = $repo;
38         $lines = explode("\n", $commit);
39         $line = array_shift($lines);
40
41         if (!preg_match("/^commit\s+(\S+)\s+(\S+)$/", $line, $M)) {
42             return false;
43         }
44         $ent->rev = $M[1];
45         $ent->branch = $M[2] ;
46         $ent->tags = array(); // FIXME
47         $ent->files = array();
48
49         while (count($lines)) {
50             $line = array_shift($lines);
51             if (!strlen($line)) {
52               break;
53             }
54             if (preg_match("/^(\S+):\s+(.*)\s*$/", $line, $M)) {
55                 $k = $M[1];
56                 $v = $M[2];
57
58                 switch ($k) {
59                     case 'Author':
60                       $ent->changeby = $v;
61                       break;
62                     case 'Date':
63                       $ts = strtotime($v);
64                       $ent->ctime = MTrackDB::unixtime($ts);
65                       break;
66                 }
67             }
68         }
69
70         $ent->changelog = "";
71
72         if ($lines[0] == '') {
73             array_shift($lines);
74         }
75
76         while (count($lines)) {
77             $line = array_shift($lines);
78             if (strncmp($line, '    ', 4)) {
79               array_unshift($lines, $line);
80               break;
81             }
82             $line = substr($line, 4);
83             $ent->changelog .= $line . "\n";
84         }
85
86         if ($lines[0] == '') {
87             array_shift($lines);
88         }
89         // this should only be the last set of lines..
90         
91         foreach ($lines as $line) {
92             if (!strlen($line)) {
93                 continue;
94             }
95               
96             if (preg_match('#^:#', $line)) {
97                 // it's our stat line..:
98                 // :100755 100755 fde93abd1a71accd3aa7e97b29c1eecfb43095d7 
99                 // 3d71edf6512035846d8164c3b28818de0062335a M      web/MTrackWeb/DataObjects/Changes.php
100                 $info = preg_split('#\s+#', substr($line ,1), 6);
101                // print_r($info);
102                 $f = new MTrackSCMFileEvent; //generic..
103                 $f->name = $info[5];
104                 $f->oldperm = $info[0];
105                 $f->newperm = $info[1];
106                 $f->oldver = $info[2];
107                 $f->newver = $info[3];
108                 $f->status = $info[4];
109                 $ent->files[$f->name] = $f;
110                 continue;
111             }
112          
113             $info = preg_split('#\s+#', substr($line ,1), 3);
114            //print_r($info);
115             $name = $info[2];
116             if (!isset($ent->files[$name])) {
117                 $ent->files[$name] = new stdClass; // ??
118             }
119             
120             $ent->files[$name]->added = $info[0];            
121             $ent->files[$name]->removed = $info[1];
122         }
123         // fixme..
124         //if (!count($ent->branches)) {
125         //    $ent->branches[] = $this->branch; //'master';
126         //}
127         $ent->files_array = array_values($ent->files);
128         return $ent;
129     }
130  
131     
132 }
133