Skip to content

Commit

Permalink
Allow file rename action for GitHub and Bitbucket
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dregad committed Jul 14, 2022
1 parent ca7f76a commit e374fec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
22 changes: 22 additions & 0 deletions Source/Source.API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );
Expand Down
11 changes: 8 additions & 3 deletions SourceBitBucket/SourceBitBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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";
}

Expand All @@ -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";
}

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions SourceGithub/SourceGithub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit e374fec

Please sign in to comment.