Skip to content

Commit e374fec

Browse files
committed
Allow file rename action for GitHub and Bitbucket
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
1 parent ca7f76a commit e374fec

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

Source/Source.API.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,11 @@ class SourceFile {
13091309
const BINARY = 'bin';
13101310
const NA = 'n/a';
13111311

1312+
/**
1313+
* String used to separate old and new filename in RENAMED operations.
1314+
*/
1315+
const RENAMED_SEPARATOR = '';
1316+
13121317
var $id;
13131318
var $change_id;
13141319
var $revision;
@@ -1323,6 +1328,23 @@ function __construct( $p_change_id, $p_revision, $p_filename, $p_action='' ) {
13231328
$this->filename = $p_filename;
13241329
}
13251330

1331+
/**
1332+
* Return the File's name.
1333+
*
1334+
* In rename operations, filename is stored as 'old → new' so we need to
1335+
* conditionally split the string and return only the new filename to
1336+
* avoid problems when it's used e.g. in URLs.
1337+
*
1338+
* @return string
1339+
*/
1340+
public function getFilename() {
1341+
if ($this->action == SourceFile::RENAMED) {
1342+
$t_split = explode(SourceFile::RENAMED_SEPARATOR, $this->filename);
1343+
return isset( $t_split[1] ) ? $t_split[1] : $t_split[0];
1344+
}
1345+
return $this->filename;
1346+
}
1347+
13261348
function save() {
13271349
if ( 0 == $this->change_id ) {
13281350
error_parameters( $this->id );

SourceBitBucket/SourceBitBucket.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public function show_changeset( $p_repo, $p_changeset ) {
3838
}
3939

4040
public function show_file( $p_repo, $p_changeset, $p_file ) {
41-
return "$p_file->action - $p_file->filename";
41+
$t_filename = $p_file->getFilename();
42+
return "$p_file->action - $t_filename";
4243
}
4344

4445
public function url_repo( $p_repo, $p_changeset = null ) {
@@ -67,7 +68,7 @@ public function url_file( $p_repo, $p_changeset, $p_file ) {
6768
}
6869

6970
$t_ref = $p_changeset->revision;
70-
$t_filename = $p_file->filename;
71+
$t_filename = $p_file->getFilename();
7172
return $this->main_url . "$t_username/$t_reponame/src/$t_ref/$t_filename";
7273
}
7374

@@ -78,7 +79,7 @@ public function url_diff( $p_repo, $p_changeset, $p_file ) {
7879
}
7980

8081
$t_ref = $p_changeset->revision;
81-
$t_filename = $p_file->filename;
82+
$t_filename = $p_file->getFilename();
8283
return $this->main_url . "$t_username/$t_reponame/diff/$t_filename?diff2=$t_ref";
8384
}
8485

@@ -439,6 +440,10 @@ private function json_commit_changeset( $p_repo, $p_json, $p_branch = '' ) {
439440
$t_action = SourceFile::DELETED;
440441
$t_filename = $t_file->old->path;
441442
break;
443+
case 'renamed':
444+
$t_action = SourceFile::RENAMED;
445+
$t_filename = $t_file->old->path . SourceFile::RENAMED_SEPARATOR . $t_file->new->path;
446+
break;
442447
default:
443448
$t_action = SourceFile::UNKNOWN . ' ' . $t_file->status;
444449
}

SourceGithub/SourceGithub.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public function url_file( $p_repo, $p_changeset, $p_file ) {
248248
$t_username = $p_repo->info['hub_username'];
249249
$t_reponame = $p_repo->info['hub_reponame'];
250250
$t_ref = $p_changeset->revision;
251-
$t_filename = $p_file->filename;
251+
$t_filename = $p_file->getFilename();
252252

253-
return "https://github.com/$t_username/$t_reponame/tree/$t_ref/$t_filename";
253+
return "https://github.com/$t_username/$t_reponame/blob/$t_ref/$t_filename";
254254
}
255255

256256
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='' ) {
799799
case 'removed':
800800
$t_action = SourceFile::DELETED;
801801
break;
802+
case 'renamed':
803+
$t_action = SourceFile::RENAMED;
804+
$t_filename = $t_file->previous_filename . SourceFile::RENAMED_SEPARATOR . $t_file->filename;
805+
break;
802806
default:
803807
$t_action = SourceFile::UNKNOWN . ' ' . $t_file->status;
804808
}

0 commit comments

Comments
 (0)