Permalink
Browse files

Fix a number of bugs, support listing files modified

Ton Plomp contributed a patch in June 2010 to resolve a bug whereby
automatic additions of changesets were not working. Another bug with
incorrect timezone offsets was also resolved. Finally, Ton's patch also
allows for the author email address to be unspecified on a commit.

In October 2010 Marcel Bennett reviewed Ton's patch and made a few minor
changes/corrections. Marcel has also added the ability for the
SourceHgWeb plugin to retrieve a list of files modified in a commit.

I have modified the patch to escape variables before outputting them so
that this plugin is safe from potential XSS vulnerabilities.

Co-authored-by: Ton Plomp <tcplomp@gmail.com>
Co-authored-by: Marcel Bennett <marcel@iamserious.com>
  • Loading branch information...
3 people authored and dregad committed Oct 22, 2010
1 parent 42824e2 commit 3ce2224be422d0ddf2fa057114aa98d019156043
Showing with 66 additions and 29 deletions.
  1. +66 −29 SourceHgWeb/SourceHgWeb.php
@@ -1,5 +1,5 @@
<?php
-# Copyright (C) 2010 David Hicks
+# Copyright (C) 2010 David Hicks, Ton Plomp, Marcel Bennett
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
@@ -22,7 +22,7 @@ function register() {
$this->name = plugin_lang_get( 'title' );
$this->description = plugin_lang_get( 'description' );
- $this->version = '0.14';
+ $this->version = '0.15';
$this->requires = array(
'MantisCore' => '1.2.0',
'Source' => '0.14',
@@ -90,20 +90,20 @@ public function update_repo_form( $p_repo ) {
if ( isset( $p_repo->info['master_branch'] ) ) {
$t_master_branch = $p_repo->info['master_branch'];
} else {
- $t_master_branch = 'master';
+ $t_master_branch = 'default';
}
?>
<tr <?php echo helper_alternate_class() ?>>
-<td class="category"><?php echo plugin_lang_get( 'hgweb_root' ) ?></td>
-<td><input name="hgweb_root" maxlength="250" size="40" value="<?php echo string_attribute( $t_hgweb_root ) ?>"/></td>
+ <td class="category"><?php echo plugin_lang_get( 'hgweb_root' ) ?></td>
+ <td><input name="hgweb_root" maxlength="250" size="40" value="<?php echo string_attribute( $t_hgweb_root ) ?>"/></td>
</tr>
<tr <?php echo helper_alternate_class() ?>>
-<td class="category"><?php echo plugin_lang_get( 'hgweb_project' ) ?></td>
-<td><input name="hgweb_project" maxlength="250" size="40" value="<?php echo string_attribute( $t_hgweb_project ) ?>"/></td>
+ <td class="category"><?php echo plugin_lang_get( 'hgweb_project' ) ?></td>
+ <td><input name="hgweb_project" maxlength="250" size="40" value="<?php echo string_attribute( $t_hgweb_project ) ?>"/></td>
</tr>
<tr <?php echo helper_alternate_class() ?>>
-<td class="category"><?php echo plugin_lang_get( 'master_branch' ) ?></td>
-<td><input name="master_branch" maxlength="250" size="40" value="<?php echo string_attribute( $t_master_branch ) ?>"/></td>
+ <td class="category"><?php echo plugin_lang_get( 'master_branch' ) ?></td>
+ <td><input name="master_branch" maxlength="250" size="40" value="<?php echo string_attribute( $t_master_branch ) ?>"/></td>
</tr>
<?php
}
@@ -132,9 +132,6 @@ public function commit( $p_repo, $p_data ) {
# i.e. DATA=`echo $INPUT | sed -e 's/ /+/g'`
list ( , $t_commit_id, $t_branch ) = split( ' ', $p_data );
list ( , , $t_branch ) = split( '/', $t_branch );
- if ( $t_branch != $p_repo->info['master_branch'] ) {
- return;
- }
return $this->import_commits($p_repo, null, $t_commit_id, $t_branch);
}
@@ -144,7 +141,7 @@ public function import_full( $p_repo ) {
$t_branch = $p_repo->info['master_branch'];
if ( is_blank( $t_branch ) ) {
- $t_branch = 'master';
+ $t_branch = 'default';
}
$t_branches = map( 'trim', explode( ',', $t_branch ) );
@@ -162,7 +159,7 @@ public function import_full( $p_repo ) {
if ( db_num_rows( $t_result ) > 0 ) {
$t_parent = db_result( $t_result );
- echo "Oldest '$t_branch' branch parent: '$t_parent'\n";
+ echo 'Oldest \'' . string_display_line( $t_branch ) . '\' branch parent: \'' . string_display_line( $t_parent ) . "'\n";
if ( !empty( $t_parent ) ) {
$t_commits[] = $t_parent;
@@ -196,7 +193,7 @@ private function import_commits( $p_repo, $p_uri_base, $p_commit_ids, $p_branch=
while( count( $s_parents ) > 0 && $s_counter < 200 ) {
$t_commit_id = array_shift( $s_parents );
- echo "Retrieving $t_commit_id ... ";
+ echo 'Retrieving ' . string_display_line( $t_commit_id ) . '... ';
$t_commit_url = $this->uri_base( $p_repo ) . 'raw-rev/' . $t_commit_id;
$t_input = url_get( $t_commit_url );
@@ -228,40 +225,80 @@ private function commit_changeset( $p_repo, $p_input, $p_branch='' ) {
if( strpos( $t_line, '#' ) === 0 ) {
if( !isset( $t_commit['revision'] ) && preg_match( '@^# Node ID +([a-f0-9]+)@', $t_line, $t_matches ) ) {
$t_commit['revision'] = $t_matches[1];
- echo "Processing $t_commit[revision] ... ";
+ echo 'Processing ' . string_display_line( $t_commit[revision] ) . '... ';
if ( SourceChangeset::exists( $p_repo->id, $t_commit['revision'] ) ) {
echo "already exists.\n";
return array( null, array() );
}
- } else if( !isset( $t_commit['author'] ) && preg_match( '@^# User +([^<>]*) <([^<>]*)>@', $t_line, $t_matches ) ) {
- $t_commit['author'] = $t_matches[1];
+ } else if( !isset( $t_commit['author'] ) && preg_match( '@^# User ([^<>]*)(?(?=(?=<))<([^<>]*)>|.*)@', $t_line, $t_matches ) ) {
+ $t_commit['author'] = trim($t_matches[1]);
$t_commit['author_email'] = $t_matches[2];
} else if( !isset( $t_commit['date'] ) && preg_match( '@^# Date +(\d+) (-?\d+)@', $t_line, $t_matches ) ) {
- $t_timestamp_gmt = $t_matches[1] + (int)$t_matches[2];
+ $t_timestamp_gmt = $t_matches[1] - (int)$t_matches[2];
$t_commit['date'] = gmdate( 'Y-m-d H:i:s', $t_timestamp_gmt );
} else if( !isset( $t_commit['parent'] ) && preg_match( '@^# Parent +([a-f0-9]+)@', $t_line, $t_matches ) ) {
$t_parents[] = $t_matches[1];
$t_commit['parent'] = $t_matches[1];
}
} else if( isset( $t_commit['revision'] ) ) {
- if ( preg_match( '@^--- @', $t_line, $t_matches ) ) {
+ if ( preg_match( '@^diff @', $t_line, $t_matches ) ) {
break;
}
$t_message[] = $t_line;
}
}
- $t_commit['message'] = implode( "\n", $t_message );
+ if ( !SourceChangeset::exists( $p_repo->id, $t_commit['revision'] ) ) {
+ $t_commit['message'] = implode( "\n", $t_message );
- $t_changeset = new SourceChangeset( $p_repo->id, $t_commit['revision'],
- $p_branch, $t_commit['date'], $t_commit['author'],
- $t_commit['message'], 0,
- isset( $t_commit['parent'] ) ? $t_commit['parent'] : ''
+ $t_changeset = new SourceChangeset( $p_repo->id, $t_commit['revision'],
+ $p_branch, $t_commit['date'], $t_commit['author'],
+ $t_commit['message'], 0,
+ (isset( $t_commit['parent'] ) ? $t_commit['parent'] : '')
);
- $t_changeset->author_email = $t_commit['author_email'];
- $t_changeset->save();
- echo "saved.\n";
- return array( $t_changeset, $t_parents );
+ $t_changeset->author_email = empty($t_commit['author_email'])? '': $t_commit['author_email'];
+
+ preg_match_all('#diff[\s]*-r[\s]([^\s]*)[\s]*-r[\s]([^\s]*)[\s]([^\n]*)\n(Binary file[\s]([^\s]*)[\s]has changed|\-{3}[\s](/dev/null)?[^\t]*[^\n]*\n\+{3}[\s](/dev/null)?[^\t]*\t[^\n]*)#', $p_input, $t_matches, PREG_SET_ORDER);
+
+ $t_commit['files'] = array();
+
+ foreach( $t_matches as $t_file_matches ) {
+ $t_file = array();
+ $t_file['filename'] = $t_file_matches[3];
+ $t_file['revision'] = $t_commit['revision'];
+
+ if(!empty($t_file_matches[3])) {
+ if(!empty($t_file_matches[5])) {
+ $t_file['action'] = 'bin';
+ }
+ else if ("/dev/null" == $t_file_matches[7]) {
+ $t_file['action'] = 'rm';
+ }
+ else if ("/dev/null" == $t_file_matches[6]) {
+ $t_file['action'] = 'add';
+ }
+ else if ("/dev/null" == $t_file_matches[7] && "/dev/null" == $t_file_matches[6]) {
+ $t_file['action'] = 'n/a';
+ }
+ else if (empty($t_file_matches[5]) && empty($t_file_matches[6]) && empty($t_file_matches[7])) {
+ $t_file['action'] = 'mod';
+ }
+ }
+ $t_commit['files'][] = $t_file;
+ }
+
+ foreach( $t_commit['files'] as $t_file ) {
+ $t_changeset->files[] = new SourceFile( 0, $t_file['revision'], $t_file['filename'], $t_file['action'] );
+ }
+
+ $t_changeset->save();
+
+ echo "saved.\n";
+ return array( $t_changeset, $t_parents );
+ } else {
+ echo "already exists.\n";
+ return array( null, array() );
+ }
}
}

0 comments on commit 3ce2224

Please sign in to comment.