From e374feca48ddfce87b7d34a628dc370b01e197ba Mon Sep 17 00:00:00 2001 From: Damien Regad Date: Thu, 14 Jul 2022 16:46:31 +0200 Subject: [PATCH] Allow file rename action for GitHub and Bitbucket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid a schema change introducing a new field for the old filename, we store both the old and new name in the existing field, separated by a delimiter ' → ' (defined as constant in SourceFile class). To ensure we generate valid URLs as target for the Diff and File buttons we also introduce a new SourceFile::getFilename() method, which is used in the url_diff() and url_file() methods to split the value and return the actual filename to use (i.e. the new one). Fixes #374 --- Source/Source.API.php | 22 ++++++++++++++++++++++ SourceBitBucket/SourceBitBucket.php | 11 ++++++++--- SourceGithub/SourceGithub.php | 8 ++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Source/Source.API.php b/Source/Source.API.php index 56fc6d7e7..9b8ba6ed1 100644 --- a/Source/Source.API.php +++ b/Source/Source.API.php @@ -1309,6 +1309,11 @@ class SourceFile { const BINARY = 'bin'; const NA = 'n/a'; + /** + * String used to separate old and new filename in RENAMED operations. + */ + const RENAMED_SEPARATOR = ' → '; + var $id; var $change_id; var $revision; @@ -1323,6 +1328,23 @@ function __construct( $p_change_id, $p_revision, $p_filename, $p_action='' ) { $this->filename = $p_filename; } + /** + * Return the File's name. + * + * In rename operations, filename is stored as 'old → new' so we need to + * conditionally split the string and return only the new filename to + * avoid problems when it's used e.g. in URLs. + * + * @return string + */ + public function getFilename() { + if ($this->action == SourceFile::RENAMED) { + $t_split = explode(SourceFile::RENAMED_SEPARATOR, $this->filename); + return isset( $t_split[1] ) ? $t_split[1] : $t_split[0]; + } + return $this->filename; + } + function save() { if ( 0 == $this->change_id ) { error_parameters( $this->id ); diff --git a/SourceBitBucket/SourceBitBucket.php b/SourceBitBucket/SourceBitBucket.php index 464ed2ca3..5e4fd08ad 100644 --- a/SourceBitBucket/SourceBitBucket.php +++ b/SourceBitBucket/SourceBitBucket.php @@ -38,7 +38,8 @@ public function show_changeset( $p_repo, $p_changeset ) { } public function show_file( $p_repo, $p_changeset, $p_file ) { - return "$p_file->action - $p_file->filename"; + $t_filename = $p_file->getFilename(); + return "$p_file->action - $t_filename"; } public function url_repo( $p_repo, $p_changeset = null ) { @@ -67,7 +68,7 @@ public function url_file( $p_repo, $p_changeset, $p_file ) { } $t_ref = $p_changeset->revision; - $t_filename = $p_file->filename; + $t_filename = $p_file->getFilename(); return $this->main_url . "$t_username/$t_reponame/src/$t_ref/$t_filename"; } @@ -78,7 +79,7 @@ public function url_diff( $p_repo, $p_changeset, $p_file ) { } $t_ref = $p_changeset->revision; - $t_filename = $p_file->filename; + $t_filename = $p_file->getFilename(); return $this->main_url . "$t_username/$t_reponame/diff/$t_filename?diff2=$t_ref"; } @@ -439,6 +440,10 @@ private function json_commit_changeset( $p_repo, $p_json, $p_branch = '' ) { $t_action = SourceFile::DELETED; $t_filename = $t_file->old->path; break; + case 'renamed': + $t_action = SourceFile::RENAMED; + $t_filename = $t_file->old->path . SourceFile::RENAMED_SEPARATOR . $t_file->new->path; + break; default: $t_action = SourceFile::UNKNOWN . ' ' . $t_file->status; } diff --git a/SourceGithub/SourceGithub.php b/SourceGithub/SourceGithub.php index 44aa7c1d1..00fe2f1d1 100644 --- a/SourceGithub/SourceGithub.php +++ b/SourceGithub/SourceGithub.php @@ -248,9 +248,9 @@ public function url_file( $p_repo, $p_changeset, $p_file ) { $t_username = $p_repo->info['hub_username']; $t_reponame = $p_repo->info['hub_reponame']; $t_ref = $p_changeset->revision; - $t_filename = $p_file->filename; + $t_filename = $p_file->getFilename(); - return "https://github.com/$t_username/$t_reponame/tree/$t_ref/$t_filename"; + return "https://github.com/$t_username/$t_reponame/blob/$t_ref/$t_filename"; } public function url_diff( $p_repo, $p_changeset, $p_file ) { @@ -799,6 +799,10 @@ private function json_commit_changeset( $p_repo, $p_json, $p_branch='' ) { case 'removed': $t_action = SourceFile::DELETED; break; + case 'renamed': + $t_action = SourceFile::RENAMED; + $t_filename = $t_file->previous_filename . SourceFile::RENAMED_SEPARATOR . $t_file->filename; + break; default: $t_action = SourceFile::UNKNOWN . ' ' . $t_file->status; }