php8
[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     var $utime;
31     var $commitday;
32     var $committime;
33     var $commitby;
34     
35     /**
36      * this is based on the output from git --raw --numstat
37      */
38     static function newFromCommit($commit, $repo)
39     {
40         
41         $ent = new MTrack_SCM_Git_Event;
42         $ent->commit = $commit;
43         $ent->repo = $repo;
44         $lines = explode("\n", $commit);
45         $line = array_shift($lines);
46
47         if (!preg_match("/^commit\s+(\S+)\s+(\S+)$/", $line, $M)) {
48             return false;
49         }
50         $ent->rev = $M[1];
51         $ent->branch = $M[2] ;
52         $ent->tags = array(); // FIXME
53         $ent->files = array();
54
55         while (count($lines)) {
56             $line = array_shift($lines);
57             if (!strlen($line)) {
58               break;
59             }
60             if (preg_match("/^(\S+):\s+(.*)\s*$/", $line, $M)) {
61                 $k = $M[1];
62                 $v = $M[2];
63
64                 switch ($k) {
65                     case 'Author':
66                       $ent->changeby = $v;
67                       break;
68                     case 'Date':
69                       $ts = strtotime($v);
70                       $ent->ctime = MTrackDB::unixtime($ts);
71                       break;
72                 }
73             }
74         }
75
76         $ent->changelog = "";
77
78         if ($lines[0] == '') {
79             array_shift($lines);
80         }
81
82         while (count($lines)) {
83             $line = array_shift($lines);
84             if (strncmp($line, '    ', 4)) {
85               array_unshift($lines, $line);
86               break;
87             }
88             $line = substr($line, 4);
89             $ent->changelog .= $line . "\n";
90         }
91
92         if ($lines[0] == '') {
93             array_shift($lines);
94         }
95         // this should only be the last set of lines..
96         
97         foreach ($lines as $line) {
98             if (!strlen($line)) {
99                 continue;
100             }
101               
102             if (preg_match('#^:#', $line)) {
103                 // it's our stat line..:
104                 // :100755 100755 fde93abd1a71accd3aa7e97b29c1eecfb43095d7 
105                 // 3d71edf6512035846d8164c3b28818de0062335a M      web/MTrackWeb/DataObjects/Changes.php
106                 $info = preg_split('#\s+#', substr($line ,1), 6);
107                // print_r($info);
108                 $f = new MTrackSCMFileEvent; //generic..
109                 $f->name = $info[5];
110                 $f->oldperm = $info[0];
111                 $f->newperm = $info[1];
112                 $f->oldver = $info[2];
113                 $f->newver = $info[3];
114                 $f->status = $info[4];
115                 $ent->files[$f->name] = $f;
116                 continue;
117             }
118          
119             $info = preg_split('#\s+#', substr($line ,1), 3);
120            //print_r($info);
121             $name = $info[2];
122             if (!isset($ent->files[$name])) {
123                 $ent->files[$name] = new stdClass; // ??
124             }
125             
126             $ent->files[$name]->added = $info[0];            
127             $ent->files[$name]->removed = $info[1];
128         }
129         // fixme..
130         //if (!count($ent->branches)) {
131         //    $ent->branches[] = $this->branch; //'master';
132         //}
133         $ent->files_array = array_values($ent->files);
134         return $ent;
135     }
136  
137     
138 }
139