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     function post($pi) {
70         return $this->get($pi);
71     }
72     
73     function tree()
74     {
75         
76         $live = 'master';
77         $release = 'github';
78          
79         $this->repo->debug = 1;
80         $ar = $this->repo->history("/", null, "rev",  "{$this->release}..{$this->live}");
81         
82         // the point of this is to extract all the revisions, and group them.
83         
84         
85         
86         //echo '<PRE>';print_R($ar);
87         
88         // need to get a 2 dimensional array of
89         // files along top, and commints down.
90         $cfiles = array();
91         $rows = array();
92         foreach($ar as $commit) {
93             
94             $files = $commit->files;
95             $day = date("Y-m-d", strtotime($commit->ctime));
96             if (!isset($days[$day])) { 
97                 $days[$day] = array(
98                     'text' => $day,
99                     'rev' => $day,
100                     'children' => array()
101                 );
102             }
103             $time= date("H:i:s", strtotime($commit->ctime));
104             if (!isset($days[$day]['children'][$time])) { 
105                 $days[$day]['children'][$time] = array(
106                     'text' => $time,
107                     'rev' => $day . ' ' . $time,
108                     'commits' =>array(),
109                 );
110             }
111             $days[$day]['children'][$time]['children'][] = array(
112                 'text'=> $commit->changelog,
113                 'rev' => $commit->rev,
114                 'leaf' => true
115             );
116         }
117         $out = array();
118         
119         foreach($days as $d=>$do) {
120             $dcn = $do['children'];
121             $do['children'] = array();
122             foreach($dcn as $t=>$to) {
123                 $to['rev']  = $to['children'][0]['rev'];
124                 $do['children'][] = $to;
125             }
126             $do['rev'] = $do['children'][0]['rev'];
127             $out[] = $do;
128         }
129         
130         $this->jdata($out);
131         
132          
133         
134     }
135     
136     
137
138
139
140     function changedFiles($rev)
141     {
142         // list the files that have changed..
143         // in theory you could click a number of them and only merge those changes..
144         // need to do a git diff.. and just get a list of files..
145         
146         $rev = preg_replace('/[^a-z0-9]+/', '',$rev);
147         //$this->repo->impl()->debug = 1;
148         $fh = $this->repo->impl()->git('diff', '--numstat' ,  "{$this->release}..{$rev}", '--', "");
149         // returns ADDED \t REMOVED \t NAME (might include rename/etc. info)
150         $rows = array();
151         
152         while (false !== ($line = fgets($fh))) {
153             //var_Dump($line);
154             $ar = explode("\t", trim($line));
155             $rows[] = array(
156                 'added' => $ar[0],
157                 'removed' => $ar[1],
158                 'filename' => $ar[2],
159             );
160             
161             
162         }
163         fclose($fh);
164         $this->jdata($rows);
165          
166
167     }
168     
169     
170
171     function preview($rev)
172     {
173         // list the files that have changed..
174         // in theory you could click a number of them and only merge those changes..
175         // need to do a git diff.. and just get a list of files..
176         
177         $rev = preg_replace('/[^a-z0-9]+/', '',$rev);
178         //$this->repo->impl()->debug = 1;
179         $files = '';
180         if (!empty($_POST['files'])) {
181             $files = json_decode($_POST['files']);
182         }
183         
184         
185         $fh = $this->repo->impl()->git('diff', "-w", "{$this->release}..{$rev}", '--', $files);
186         
187         echo '<PRE>' . htmlspecialchars(stream_get_contents($fh)) . '</PRE>';
188         fclose($fh);
189         exit;
190          
191
192     }
193     
194     
195     function merge($rev)
196     {
197         $wd = $this->repo->getWorkingCopy();
198        // $wd->checkout($this->release);
199         // generate the patch
200         // apply the patch
201         // commit with message..
202         // push
203         
204         
205         
206         
207         
208         
209         
210         
211         
212     }
213     
214     
215     
216     
217 }