Merger.php
[Pman.MTrack] / Merger.php
1 <?php
2 /**
3  * concept is to merge commits from a live or working tree
4  * into the 'release tree'
5  *
6  *
7  * step 1 = render the log of differences..
8  *
9  *
10  * First run issues:
11  *  -- our doc commit changes loads of files.. and makes the layout pointless.
12  *  -- ?? grouping by day??? // expand/collapse.. 
13  * 
14  *
15  */
16
17 require_once 'Pman.php';
18
19 class Pman_MTrack_Merger extends Pman {
20     
21     var $live = 'master';
22     var $release = 'github';
23     
24     
25     // function getAuth() - from log..
26     function getAuth()
27     {
28         parent::getAuth();
29         $au = $this->getAuthUser();
30         if (!$au || $au->company()->comptype != 'OWNER') {
31             $this->jautherr();
32         }
33         $this->authUser = $au;
34         return true;
35     }
36     
37     
38     function get($pi)
39     {
40         
41         $this->pi = 'default/roojs1'; // fixme..
42         
43         $this->repo = DB_DataObject::factory('mtrack_repos');
44         $this->filename = $this->repo->loadFromPath($this->pi);
45         
46         if (!$this->repo->id) {
47             $this->err("invalid repo");
48         };
49           
50         //if (!$this->projectPerm($this->repo->project_id, 'MTrack.Repos', 'S')) {
51         //    $this->err("no perm for this repo ");
52         //}
53         if (isset($_REQUEST['_tree'])) {
54             $this->tree();
55         }
56         
57         if (isset($_REQUEST['_changedFiles'])) {
58             $this->changedFiles($_REQUEST['_changedFiles']);
59         }
60         if (isset($_REQUEST['_preview'])) {
61             $this->preview($_REQUEST['_preview']);
62         }
63         
64         $this->jerr("invalid url");
65         exit;
66         
67     }
68     
69     
70     
71     function tree()
72     {
73         
74         $live = 'master';
75         $release = 'github';
76          
77         $this->repo->debug = 1;
78         $ar = $this->repo->history("/", null, "rev",  "{$this->release}..{$this->live}");
79         
80         // the point of this is to extract all the revisions, and group them.
81         
82         
83         
84         //echo '<PRE>';print_R($ar);
85         
86         // need to get a 2 dimensional array of
87         // files along top, and commints down.
88         $cfiles = array();
89         $rows = array();
90         foreach($ar as $commit) {
91             
92             $files = $commit->files;
93             $day = date("Y-m-d", strtotime($commit->ctime));
94             if (!isset($days[$day])) { 
95                 $days[$day] = array(
96                     'text' => $day,
97                     'rev' => $day,
98                     'children' => array()
99                 );
100             }
101             $time= date("H:i:s", strtotime($commit->ctime));
102             if (!isset($days[$day]['children'][$time])) { 
103                 $days[$day]['children'][$time] = array(
104                     'text' => $time,
105                     'rev' => $day . ' ' . $time,
106                     'commits' =>array(),
107                 );
108             }
109             $days[$day]['children'][$time]['children'][] = array(
110                 'text'=> $commit->changelog,
111                 'rev' => $commit->rev,
112                 'leaf' => true
113             );
114         }
115         $out = array();
116         
117         foreach($days as $d=>$do) {
118             $dcn = $do['children'];
119             $do['children'] = array();
120             foreach($dcn as $t=>$to) {
121                 $to['rev']  = $to['children'][0]['rev'];
122                 $do['children'][] = $to;
123             }
124             $do['rev'] = $do['children'][0]['rev'];
125             $out[] = $do;
126         }
127         
128         $this->jdata($out);
129         
130          
131         
132     }
133     
134     
135
136
137
138     function changedFiles($rev)
139     {
140         // list the files that have changed..
141         // in theory you could click a number of them and only merge those changes..
142         // need to do a git diff.. and just get a list of files..
143         
144         $rev = preg_replace('/[^a-z0-9]+/', '',$rev);
145         //$this->repo->impl()->debug = 1;
146         $fh = $this->repo->impl()->git('diff', '--numstat' ,  "{$this->release}..{$rev}", '--', "");
147         // returns ADDED \t REMOVED \t NAME (might include rename/etc. info)
148         $rows = array();
149         
150         while (false !== ($line = fgets($fh))) {
151             //var_Dump($line);
152             $ar = explode("\t", trim($line));
153             $rows[] = array(
154                 'added' => $ar[0],
155                 'removed' => $ar[1],
156                 'filename' => $ar[2],
157             );
158             
159             
160         }
161         fclose($fh);
162         $this->jdata($rows);
163          
164
165     }
166     
167     
168
169     function preview($rev, $files=array())
170     {
171         // list the files that have changed..
172         // in theory you could click a number of them and only merge those changes..
173         // need to do a git diff.. and just get a list of files..
174         
175         $rev = preg_replace('/[^a-z0-9]+/', '',$rev);
176         //$this->repo->impl()->debug = 1;
177         $fh = $this->repo->impl()->git('diff',  "{$this->release}..{$rev}", '--', "");
178         
179         echo '<PRE>' . htmlspecialchars(stream_get_contents($fh)) . '</PRE>';
180         fclose($fh);
181         exit;
182          
183
184     }
185     
186 }