diff --git a/module/mr/control.php b/module/mr/control.php index 0d5f0591d1..b6e953866a 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -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(); } } diff --git a/module/mr/model.php b/module/mr/model.php index 5236b4f7d6..754d4eba14 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -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)); + } +} \ No newline at end of file diff --git a/module/mr/view/diff.html.php b/module/mr/view/diff.html.php index a4ff5ab6f9..ac5361274a 100644 --- a/module/mr/view/diff.html.php +++ b/module/mr/view/diff.html.php @@ -17,7 +17,7 @@ $backURI = $this->session->mrView ? $this->session->mrView : $this->session->mrList; if($backURI) { - echo html::a($backURI, " " . $lang->goback, '', "class='btn btn-link' data-app='{$app->openApp}'"); + echo html::a($backURI, " " . $lang->goback, '', "class='btn btn-link' data-app='{$app->tab}'"); } else { @@ -47,7 +47,7 @@ - + @@ -76,7 +76,6 @@ type != 'new') echo $line->oldlc?> type != 'old') echo $line->newlc?> line = $repo->SCM == 'Subversion' ? htmlspecialchars($line->line) : $line->line; echo $line->type == 'old' ? preg_replace('/^\-/', '–', $line->line) : ($line->type == 'new' ? $line->line : ' ' . $line->line); ?> @@ -105,13 +104,11 @@ type?> type == 'custom') echo "line-old"?> code'>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('/^\-/', '–', $content->old[$oldlc]) : ' ' . $content->old[$oldlc]; ?> type?> type == 'custom') echo "line-new"?> code'>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]; ?> -