1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 // Browse.php - only for rendering the body..
5 // Tree.php - the actually tree..
7 require_once 'MTrackWeb.php';
9 class MTrackWeb_Changeset extends MTrackWeb
12 var $cs = ''; // the changeset.
24 $this->pi = $pi . (strlen($pi) ? $this->bootLoader->ext : '');
26 if (!isset($_REQUEST['ajax_body'])) {
27 $this->title = "Changeset: " . $this->pi;
30 $this->masterTemplate = 'changeset.html';
31 $this->repo = DB_DataObject::factory('mtrack_repos');
32 $this->cs = $this->repo->loadFromPath($this->pi);
36 if (!$this->repo->id) {
37 return HTML_FlexyFramework::run('Browse');
40 if (!$this->projectPerm($this->repo->project_id, 'MTrack.Repos', 'S')) {
41 return HTML_FlexyFramework::run('Noperm'); // noperm = loggedin -> need more perms / not.. try loggin in..
44 //$data = mtrack_cache('get_change_data', array($path), 864000);
45 $this->data = $this->get_change_data($this->cs);
47 if (!empty($_REQUEST['part'])) {
48 foreach($this->data->ent->files as $f) {
49 if ($f->name != $_REQUEST['part']) {
53 echo $this->diff($this->repo->diff($f, $this->data->ent->rev));
56 die("can not find part?");
60 $this->ent = $this->data->ent;
61 if ($this->ent === null) {
62 throw new Exception("invalid parameters");
65 //$rdata = mtrack_cache('get_change_data_relatives', array($path, $ent->rev));
66 $rdata = $this->get_change_data_relatives($this->cs, $this->ent->rev);
68 // print_R($rdata);exit;
69 if (isset($_GET['fmt']) && $_GET['fmt'] == 'diff') {
70 $this->downloadDiff();
72 $this->title = "Changeset " . $this->ent->rev;
79 function downloadDiff()
81 $filename = "$this->repo->shortname.$this->ent->rev.diff";
82 header("Content-Type: text/plain; name=\"$filename\"");
83 header("Content-Disposition: attachment; filename=\"$filename\"");
85 echo "Changeset: $repo->shortname $ent->rev\n";
86 echo "By: $ent->changeby\n";
87 echo "When: $ent->ctime\n";
89 echo $data->changelog . "\n\n";
91 if (is_array($ent->files) && count($ent->files)) {
92 foreach ($ent->files as $id => $file) {
93 echo "$file->status $file->name\n";
97 foreach ($ent->files as $id => $file) {
99 if ($fpath[0] != '/') $fpath = '/' . $fpath;
100 $diff = $repo->diff($file, $ent->rev);
101 if (is_resource($diff)) {
102 echo stream_get_contents($diff);
103 } elseif (is_array($diff)) {
104 echo join("\n", $diff);
113 function get_change_data($pi)
115 $ents = $this->repo->history(null, 1, 'rev', $pi);
116 $data = new stdclass;
123 // Determine project from the file list
124 $changelog = $ent->changelog;
125 /*$the_proj = $this->repo->projectFromPath($ent->files);
127 $proj = MTrackProject::loadById($the_proj);
128 $changelog = $proj->adjust_links($ent->changelog, true);
131 $data->changelog = $changelog;
132 $ent->files = is_array($ent->files) ? array_values($ent->files) : array();
134 // we should not do this here..
135 // it's to slow on huge commits..
137 //foreach ($ent->files as $file) {
138 // $file->diff = $this->diff($this->repo->diff($file, $ent->rev));
145 function get_change_data_relatives($pi, $rev)
148 $data = new stdclass;
149 list($data->parents, $data->kids) = $this->repo->getRelatedChanges($rev);
155 function diff($diffstr) // fixme... code should be in template..
159 if (is_resource($diffstr)) {
161 while (($line = fgets($diffstr)) !== false) {
162 $lines[] = rtrim($line, "\r\n");
167 if (is_string($diffstr)) {
168 $abase = md5($diffstr);
169 $diffstr = preg_split("/\r?\n/", $diffstr);
171 $abase = md5(join("\n", $diffstr));
174 // diffstr should now contain a string...
176 /* we could use toggle() below, but it is much faster to determine
177 * if we are hiding or showing based on a single variable than evaluating
178 * that for each possible cell */
180 <button class='togglediffcopy' type='button'>Toggle Diff Line Numbers</button>
181 <table class='code diff'>
183 //$html = "<pre class='code diff'>";
186 if (!count($diffstr)) {
189 $line = array_shift($diffstr);
191 if (!strncmp($line, '@@ ', 3)) {
192 /* done with preamble */
195 $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
196 $line = "<tr class='meta'><td class='lineno'></td><td class='lineno'></td><td class='lineno'></td><td width='100%'>$line</tr>";
197 $html .= $line . "\n";
200 $lines = array(0, 0);
205 if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
207 $lines[0] = (int)$M[1] - 1;
208 $lines[1] = (int)$M[2] - 1;
211 } elseif (strlen($line)) {
212 if ($line[0] == '-') {
215 } elseif ($line[0] == '+') {
226 $row = "<tr class='$class";
230 if ($class != 'meta' && $first) {
238 $row .= "<td class='lineno'></td><td class='lineno'></td>";
241 $row .= "<td class='lineno'></td><td class='lineno'>" . $lines[1] . "</td>";
244 $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'></td>";
247 $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'>" . $lines[1] . "</td>";
249 $anchor = $abase . '.' . $nlines;
250 $row .= "<td class='linelink'><a name='$anchor'></a><a href='#$anchor' title='link to this line'>#</a></td>";
252 $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
253 $row .= "<td class='line' width='100%'>$line</td></tr>\n";
256 if (!count($diffstr)) {
259 $line = array_shift($diffstr);