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