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');
27 $this->pi = '/'. $pi . (strlen($pi) ? $this->bootLoader->ext : '');
28 $this->pi = rtrim($this->pi, '/');
30 $this->repo = MTrackSCM::factory($this->pi);
32 MTrackACL::requireAllRights("repo:{$this->repo->repoid}", 'read');
34 //$data = mtrack_cache('get_change_data', array($path), 864000);
35 $this->data = $this->get_change_data($this->pi);
37 if (!empty($_REQUEST['part'])) {
38 foreach($this->data->ent->files as $f) {
39 if ($f->name != $_REQUEST['part']) {
43 echo $this->diff($this->repo->diff($f, $this->data->ent->rev));
46 die("can not find part?");
50 $this->ent = $this->data->ent;
51 if ($this->ent === null) {
52 throw new Exception("invalid parameters");
55 //$rdata = mtrack_cache('get_change_data_relatives', array($path, $ent->rev));
56 $rdata = $this->get_change_data_relatives($this->pi, $this->ent->rev);
58 // print_R($rdata);exit;
59 if (isset($_GET['fmt']) && $_GET['fmt'] == 'diff') {
60 $this->downloadDiff();
62 $this->title = "Changeset " . $this->ent->rev;
69 function downloadDiff()
71 $filename = "$this->repo->shortname.$this->ent->rev.diff";
72 header("Content-Type: text/plain; name=\"$filename\"");
73 header("Content-Disposition: attachment; filename=\"$filename\"");
75 echo "Changeset: $repo->shortname $ent->rev\n";
76 echo "By: $ent->changeby\n";
77 echo "When: $ent->ctime\n";
79 echo $data->changelog . "\n\n";
81 if (is_array($ent->files) && count($ent->files)) {
82 foreach ($ent->files as $id => $file) {
83 echo "$file->status $file->name\n";
87 foreach ($ent->files as $id => $file) {
89 if ($fpath[0] != '/') $fpath = '/' . $fpath;
90 $diff = $repo->diff($file, $ent->rev);
91 if (is_resource($diff)) {
92 echo stream_get_contents($diff);
93 } elseif (is_array($diff)) {
94 echo join("\n", $diff);
103 function get_change_data($pi)
105 $ents = $this->repo->history(null, 1, 'rev', $pi);
106 $data = new stdclass;
113 // Determine project from the file list
114 $the_proj = $this->repo->projectFromPath($ent->files);
116 $proj = MTrackProject::loadById($the_proj);
117 $changelog = $proj->adjust_links($ent->changelog, true);
119 $changelog = $ent->changelog;
121 $data->changelog = $changelog;
122 $ent->files = is_array($ent->files) ? array_values($ent->files) : array();
124 // we should not do this here..
125 // it's to slow on huge commits..
127 //foreach ($ent->files as $file) {
128 // $file->diff = $this->diff($this->repo->diff($file, $ent->rev));
135 function get_change_data_relatives($pi, $rev)
138 $data = new stdclass;
139 list($data->parents, $data->kids) = $this->repo->getRelatedChanges($rev);
145 function diff($diffstr) // fixme... code should be in template..
149 if (is_resource($diffstr)) {
151 while (($line = fgets($diffstr)) !== false) {
152 $lines[] = rtrim($line, "\r\n");
157 if (is_string($diffstr)) {
158 $abase = md5($diffstr);
159 $diffstr = preg_split("/\r?\n/", $diffstr);
161 $abase = md5(join("\n", $diffstr));
164 // diffstr should now contain a string...
166 /* we could use toggle() below, but it is much faster to determine
167 * if we are hiding or showing based on a single variable than evaluating
168 * that for each possible cell */
170 <button class='togglediffcopy' type='button'>Toggle Diff Line Numbers</button>
171 <table class='code diff'>
173 //$html = "<pre class='code diff'>";
176 if (!count($diffstr)) {
179 $line = array_shift($diffstr);
181 if (!strncmp($line, '@@ ', 3)) {
182 /* done with preamble */
185 $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
186 $line = "<tr class='meta'><td class='lineno'></td><td class='lineno'></td><td class='lineno'></td><td width='100%'>$line</tr>";
187 $html .= $line . "\n";
190 $lines = array(0, 0);
195 if (preg_match("/^@@\s+-(\pN+)(?:,\pN+)?\s+\+(\pN+)(?:,\pN+)?\s*@@/",
197 $lines[0] = (int)$M[1] - 1;
198 $lines[1] = (int)$M[2] - 1;
201 } elseif (strlen($line)) {
202 if ($line[0] == '-') {
205 } elseif ($line[0] == '+') {
216 $row = "<tr class='$class";
220 if ($class != 'meta' && $first) {
228 $row .= "<td class='lineno'></td><td class='lineno'></td>";
231 $row .= "<td class='lineno'></td><td class='lineno'>" . $lines[1] . "</td>";
234 $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'></td>";
237 $row .= "<td class='lineno'>" . $lines[0] . "</td><td class='lineno'>" . $lines[1] . "</td>";
239 $anchor = $abase . '.' . $nlines;
240 $row .= "<td class='linelink'><a name='$anchor'></a><a href='#$anchor' title='link to this line'>#</a></td>";
242 $line = htmlspecialchars($line, ENT_QUOTES, 'utf-8');
243 $row .= "<td class='line' width='100%'>$line</td></tr>\n";
246 if (!count($diffstr)) {
249 $line = array_shift($diffstr);