* Implement view diffs in MR module.

This commit is contained in:
dingguodong
2021-11-03 17:53:14 +08:00
parent 074d13aa20
commit bdcf454239
3 changed files with 69 additions and 29 deletions
+9 -5
View File
@@ -258,10 +258,13 @@ class mr extends control
* @access public
* @return void
*/
public function diff($MRID)
public function diff($MRID, $encoding= '')
{
$encoding = empty($encoding) ? 'utf-8' : $encoding;
$encoding = strtolower(str_replace('_', '-', $encoding)); /* Revert $config->requestFix in $encoding. */
$MR = $this->mr->getByID($MRID);
$diffs = $this->mr->getDiffs($MR);
$diffs = $this->mr->getDiffs($MR, $encoding = '');
$arrange = $this->cookie->arrange ? $this->cookie->arrange : 'inline';
if($this->server->request_method == 'POST')
@@ -294,9 +297,10 @@ class mr extends control
}
}
$this->view->title = $this->lang->mr->viewDiff;
$this->view->diffs = $diffs;
$this->view->arrange = $arrange;
$this->view->title = $this->lang->mr->viewDiff;
$this->view->diffs = $diffs;
$this->view->encoding = $encoding;
$this->view->arrange = $arrange;
$this->display();
}
}
+58 -5
View File
@@ -462,11 +462,13 @@ class mrModel extends model
*
* @docs https://docs.gitlab.com/ee/api/merge_requests.html#get-mr-diff-versions
* @param object $MR
* @param string $encoding
* @access public
* @return object
*/
public function getDiffs($MR)
public function getDiffs($MR, $encoding = '')
{
$diffVersions = $this->apiGetDiffVersions($MR->gitlabID, $MR->targetProject, $MR->mriid);
$gitlab = $this->gitlab->getByID($MR->gitlabID);
$this->loadModel('repo');
@@ -477,12 +479,32 @@ class mrModel extends model
$repo->path = sprintf($this->config->repo->gitlab->apiPath, $gitlab->url, $MR->targetProject);
$repo->client = $gitlab->url;
$repo->password = $gitlab->token;
$repo->account = '';
$repo->encoding = $encoding;
$lines = array();
foreach ($diffVersions as $diffVersion)
{
$singleDiff = $this->apiGetSingleDiffVersion($MR->gitlabID, $MR->targetProject, $MR->mriid, $diffVersion->id);
if ($singleDiff->state == 'empty') continue;
$diffs = $singleDiff->diffs;
foreach ($diffs as $diff)
{
/* Here use $index to make sure $index is unique in $lines. */
$index = sprintf("index %s ... %s %s ", $singleDiff->head_commit_sha, $singleDiff->base_commit_sha, $diff->b_mode);
if(in_array($index, $lines)) continue;
$lines[] = sprintf("diff --git a/%s b/%s", $diff->old_path, $diff->new_path);
$lines[] = $index;
$lines[] = sprintf("--a/%s", $diff->old_path);
$lines[] = sprintf("--b/%s", $diff->new_path);
$diffLines = explode("\n", $diff->diff);
foreach ($diffLines as $diffLine) $lines[] = $diffLine;
}
}
$scm = $this->app->loadClass('scm');
$scm->setEngine($repo);
/* TODO fix this malformed function. */
return $scm->diff('', $MR->sourceBranch, $MR->targetBranch, $parse = true, $MR->sourceProject);
$diff = $scm->engine->parseDiff($lines);
return $diff;
}
/**
@@ -540,4 +562,35 @@ class mrModel extends model
$url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/todo");
return json_decode(commonModel::http($url, $data = null, $options = array(CURLOPT_CUSTOMREQUEST => 'POST')));
}
}
/**
* Get diff versions of MR from GitLab API.
*
* @param int $gitlabID
* @param int $projectID
* @param int $MRID
* @access public
* @return object
*/
public function apiGetDiffVersions($gitlabID, $projectID, $MRID)
{
$url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/versions");
return json_decode(commonModel::http($url));
}
/**
* Get a single diff version of MR from GitLab API.
*
* @param int $gitlabID
* @param int $projectID
* @param int $MRID
* @param int $versionID
* @access public
* @return object
*/
public function apiGetSingleDiffVersion($gitlabID, $projectID, $MRID, $versionID)
{
$url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/versions/$versionID");
return json_decode(commonModel::http($url));
}
}
+2 -19
View File
@@ -17,7 +17,7 @@
$backURI = $this->session->mrView ? $this->session->mrView : $this->session->mrList;
if($backURI)
{
echo html::a($backURI, "<i class='icon icon-back icon-sm'></i> " . $lang->goback, '', "class='btn btn-link' data-app='{$app->openApp}'");
echo html::a($backURI, "<i class='icon icon-back icon-sm'></i> " . $lang->goback, '', "class='btn btn-link' data-app='{$app->tab}'");
}
else
{
@@ -47,7 +47,7 @@
</div>
</div>
</div>
<?php echo html::hidden('arrange', $arrange) . html::hidden('encoding', $encoding) . html::hidden('revision[]', $newRevision) . html::hidden('revision[]', $oldRevision)?>
<?php echo html::hidden('arrange', $arrange) . html::hidden('encoding', $encoding); ?>
</form>
</div>
<?php foreach($diffs as $diffFile):?>
@@ -76,7 +76,6 @@
<th class='w-num text-right'><?php if($line->type != 'new') echo $line->oldlc?></th>
<th class='w-num text-left'><?php if($line->type != 'old') echo $line->newlc?></th>
<td class='line-<?php echo $line->type?> code'><?php
$line->line = $repo->SCM == 'Subversion' ? htmlspecialchars($line->line) : $line->line;
echo $line->type == 'old' ? preg_replace('/^\-/', '&ndash;', $line->line) : ($line->type == 'new' ? $line->line : ' ' . $line->line);
?></td>
</tr>
@@ -105,13 +104,11 @@
<th class='w-num text-right'><?php echo $oldlc?></th>
<td class='w-code line-<?php if($line->type != 'new')echo $line->type?> <?php if($line->type == 'custom') echo "line-old"?> code'><?php
if(!isset($content->old[$oldlc])) $content->old[$oldlc] = '';
$content->old[$oldlc] = $repo->SCM == 'Subversion' ? htmlspecialchars($content->old[$oldlc]) : $content->old[$oldlc];
if(!empty($oldlc)) echo $line->type != 'all' ? preg_replace('/^\-/', '&ndash;', $content->old[$oldlc]) : ' ' . $content->old[$oldlc];
?></td>
<th class='w-num text-right'><?php echo $newlc?></th>
<td class='w-code line-<?php if($line->type != 'old') echo $line->type?> <?php if($line->type == 'custom') echo "line-new"?> code'><?php
if(!isset($content->new[$newlc])) $content->new[$newlc] = '';
$content->new[$newlc] = $repo->SCM == 'Subversion' ? htmlspecialchars($content->new[$newlc]) : $content->new[$newlc];
if(!empty($newlc)) echo $line->type != 'all' ? $content->new[$newlc] : ' ' . $content->new[$newlc];
?></td>
<?php
@@ -126,20 +123,6 @@
</div>
<?php endforeach?>
</div>
<div class='revisions hidden'>
<?php
if(strpos($repo->SCM, 'Subversion') === false)
{
$oldRevision = $oldRevision == '^' ? "$newRevision" : $oldRevision;
echo " <span class='label label-info'>" . substr($oldRevision, 0, 10) . " : " . substr($newRevision, 0, 10) . ' (' . $historys[$oldRevision] . ' : ' . $historys[$newRevision] . ')</span>';
}
else
{
$oldRevision = $oldRevision == '^' ? $newRevision - 1 : $oldRevision;
echo " <span class='label label-info'>$oldRevision : $newRevision</span>";
}
?>
</div>
<form method="post" id="exchange" class="hidden">
<input type="hidden" name="revision[]" value="<?php echo $oldRevision;?>"/>
<input type="hidden" name="revision[]" value="<?php echo $newRevision;?>"/>