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