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         $this->jerr("invalid url");
61         exit;
62         
63     }
64     
65     
66     
67     function tree()
68     {
69         
70         $live = 'master';
71         $release = 'github';
72          
73         $this->repo->debug = 1;
74         $ar = $this->repo->history("/", null, "rev",  "{$this->release}..{$this->live}");
75         
76         // the point of this is to extract all the revisions, and group them.
77         
78         
79         
80         //echo '<PRE>';print_R($ar);
81         
82         // need to get a 2 dimensional array of
83         // files along top, and commints down.
84         $cfiles = array();
85         $rows = array();
86         foreach($ar as $commit) {
87             
88             $files = $commit->files;
89             $day = date("Y-m-d", strtotime($commit->ctime));
90             if (!isset($days[$day])) { 
91                 $days[$day] = array(
92                     'text' => $day,
93                     'rev' => $day,
94                     'children' => array()
95                 );
96             }
97             $time= date("H:i:s", strtotime($commit->ctime));
98             if (!isset($days[$day]['children'][$time])) { 
99                 $days[$day]['children'][$time] = array(
100                     'text' => $time,
101                     'rev' => $day . ' ' . $time,
102                     'commits' =>array(),
103                 );
104             }
105             $days[$day]['children'][$time]['children'][] = array(
106                 'text'=> $commit->changelog,
107                 'rev' => $commit->rev,
108                 'leaf' => true
109             );
110         }
111         $out = array();
112         
113         foreach($days as $d=>$do) {
114             $dcn = $do['children'];
115             $do['children'] = array();
116             foreach($dcn as $t=>$to) {
117                 $to['rev']  = $to['children'][0]['rev'];
118                 $do['children'][] = $to;
119             }
120             $do['rev'] = $do['children'][0]['rev'];
121             $out[] = $do;
122         }
123         
124         $this->jdata($out);
125         
126          
127         
128     }
129     
130     
131
132
133
134     function changedFiles($rev)
135     {
136         // list the files that have changed..
137         // in theory you could click a number of them and only merge those changes..
138         // need to do a git diff.. and just get a list of files..
139         
140         $rev = preg_replace('/[^a-z0-9]+/', '',$rev);
141         $this->repo->impl()->debug = 1;
142         $fh = $this->repo->impl()->git('diff', '--numstat' ,  "{$this->release}..{$rev}", '--', "");
143         // returns ADDED \t REMOVED \t NAME (might include rename/etc. info)
144         $rows = array();
145         
146         while (false !== ($line = fread($fh, 4096))) {
147             $ar = explode("\t", trim($line));
148             $rows[] = array(
149                 'added' => $ar[0],
150                 'removed' => $ar[1],
151                 'filename' => $ar[2],
152             );
153             
154             
155         }
156         fclose($fh);
157         $this->jdata($rows);
158         
159         
160      
161
162     }
163 }