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
11 var $template = 'changeset.html';
19 MTrackACL::requireAllRights('Browser', 'read');
26 $this->pi = empty($pi) ? '' : ($pi . $this->bootLoader->ext);
28 $this->repo = DB_DataObject::factory('repos');
29 $cs = $this->repo->loadFromPath($this->pi);
32 if (!$this->repo->repoid) {
33 return HTML_FlexyFramework::run('Browse');
37 MTrackACL::requireAllRights("repo:{$this->repo->repoid}", 'read');
39 //$data = mtrack_cache('get_change_data', array($path), 864000);
40 $this->data = $this->get_change_data($cs);
42 if (!empty($_REQUEST['part'])) {
43 foreach($this->data->ent->files as $f) {
44 if ($f->name != $_REQUEST['part']) {
48 echo $this->diff($this->repo->diff($f, $this->data->ent->rev));
51 die("can not find part?");
55 $this->ent = $this->data->ent;
56 if ($this->ent === null) {
57 throw new Exception("invalid parameters");
60 //$rdata = mtrack_cache('get_change_data_relatives', array($path, $ent->rev));
61 $rdata = $this->get_change_data_relatives($cs, $this->ent->rev);
63 // print_R($rdata);exit;
64 if (isset($_GET['fmt']) && $_GET['fmt'] == 'diff') {
65 $this->downloadDiff();
67 $this->title = "Changeset " . $this->ent->rev;
74 function downloadDiff()
76 $filename = "$this->repo->shortname.$this->ent->rev.diff";
77 header("Content-Type: text/plain; name=\"$filename\"");
78 header("Content-Disposition: attachment; filename=\"$filename\"");
80 echo "Changeset: $repo->shortname $ent->rev\n";
81 echo "By: $ent->changeby\n";
82 echo "When: $ent->ctime\n";
84 echo $data->changelog . "\n\n";
86 if (is_array($ent->files) && count($ent->files)) {
87 foreach ($ent->files as $id => $file) {
88 echo "$file->status $file->name\n";
92 foreach ($ent->files as $id => $file) {
94 if ($fpath[0] != '/') $fpath = '/' . $fpath;
95 $diff = $repo->diff($file, $ent->rev);
96 if (is_resource($diff)) {
97 echo stream_get_contents($diff);
98 } elseif (is_array($diff)) {
99 echo join("\n", $diff);
108 function get_change_data($pi)
110 $ents = $this->repo->history(null, 1, 'rev', $pi);
111 $data = new stdclass;
118 // Determine project from the file list
119 $the_proj = $this->repo->projectFromPath($ent->files);
121 $proj = MTrackProject::loadById($the_proj);
122 $changelog = $proj->adjust_links($ent->changelog, true);
124 $changelog = $ent->changelog;
126 $data->changelog = $changelog;
127 $ent->files = is_array($ent->files) ? array_values($ent->files) : array();
129 // we should not do this here..
130 // it's to slow on huge commits..
132 //foreach ($ent->files as $file) {
133 // $file->diff = $this->diff($this->repo->diff($file, $ent->rev));
140 function get_change_data_relatives($pi, $rev)
143 $data = new stdclass;
144 list($data->parents, $data->kids) = $this->repo->getRelatedChanges($rev);
150 function diff($diffstr) // fixme... code should be in template..
154 if (is_resource($diffstr)) {
156 while (($line = fgets($diffstr)) !== false) {
157 $lines[] = rtrim($line, "\r\n");
162 if (is_string($diffstr)) {
163 $abase = md5($diffstr);
164 $diffstr = preg_split("/\r?\n/", $diffstr);
166 $abase = md5(join("\n", $diffstr));
169 // diffstr should now contain a string...
171 /* we could use toggle() below, but it is much faster to determine
172 * if we are hiding or showing based on a single variable than evaluating
173 * that for each possible cell */
175 <button class='togglediffcopy' type='button'>Toggle Diff Line Numbers</button>
176 <table class='code diff'>
178 //$html = "<pre class='code diff'>";
181 if (!count($diffstr)) {
184 $line = array_shift($diffstr);
186 if (!strncmp($line, '@@ ', 3)) {
187 /* done with preamble */
190 $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
191 $line = "<tr class='meta'><td class='lineno'></td><td class='lineno'></td><td class='lineno'></td><td width='100%'>$line</tr>";
192 $html .= $line . "\n";
195 $lines = array(0, 0);
200 if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
202 $lines[0] = (int)$M[1] - 1;
203 $lines[1] = (int)$M[2] - 1;
206 } elseif (strlen($line)) {
207 if ($line[0] == '-') {
210 } elseif ($line[0] == '+') {
221 $row = "<tr class='$class";
225 if ($class != 'meta' && $first) {
233 $row .= "<td class='lineno'></td><td class='lineno'></td>";
236 $row .= "<td class='lineno'></td><td class='lineno'>" . $lines[1] . "</td>";
239 $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'></td>";
242 $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'>" . $lines[1] . "</td>";
244 $anchor = $abase . '.' . $nlines;
245 $row .= "<td class='linelink'><a name='$anchor'></a><a href='#$anchor' title='link to this line'>#</a></td>";
247 $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
248 $row .= "<td class='line' width='100%'>$line</td></tr>\n";
251 if (!count($diffstr)) {
254 $line = array_shift($diffstr);