Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions SourceGitweb/SourceGitweb.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,21 @@ public function precommit( ) {
}

public function commit( $p_repo, $p_data ) {
# Handle branch names with '+' character
$p_data = str_replace('_plus_', '+', $p_data);

# The -d option from curl requires you to encode your own data.
# Once it reaches here it is decoded. Hence we split by a space
# were as the curl command uses a '+' character instead.
# i.e. DATA=`echo $INPUT | sed -e 's/ /+/g'`
list ( , $t_commit_id, $t_branch) = explode(' ', $p_data);
list ( , , $t_branch) = explode('/', $t_branch);
list ( , , $t_branch) = explode('/', $t_branch, 3);

# master_branch contains comma-separated list of branches
$t_branches = explode(',', $p_repo->info['master_branch']);
if (!in_array($t_branch, $t_branches))
if (!in_array('*', $t_branches) and !in_array($t_branch, $t_branches))
{
return;
return;
}

return $this->import_commits($p_repo, null, $t_commit_id, $t_branch);
Expand Down Expand Up @@ -218,11 +222,14 @@ private function import_commits( $p_repo, $p_uri_base, $p_commit_ids, $p_branch=

echo "Retrieving $t_commit_id ... ";

$t_commit_url = $this->uri_base( $p_repo ) . 'a=commit;h=' . $t_commit_id;
# Handle branch names with '+' character
$t_fixed_id = str_replace('+', '%2B', $t_commit_id);
$t_commit_url = $this->uri_base( $p_repo ) . 'a=commit;h=' . $t_fixed_id;
$t_input = url_get( $t_commit_url );

if ( false === $t_input ) {
echo "failed.\n";
echo "$t_commit_url\n"; # DEBUG
continue;
}

Expand All @@ -232,6 +239,7 @@ private function import_commits( $p_repo, $p_uri_base, $p_commit_ids, $p_branch=
}

$s_parents = array_merge( $s_parents, $t_commit_parents );
$s_counter += 1;
}

$s_counter = 0;
Expand Down Expand Up @@ -274,37 +282,34 @@ private function commit_changeset( $p_repo, $p_input, $p_branch='' ) {
if ( !SourceChangeset::exists( $p_repo->id, $t_commit['revision'] ) ) {

# Parse for commit data
preg_match( '#<tr><td>author</td><td>(?:<a[^>]*>)?([^<>]*)(?:</a>)? *(?:<a[^>]*>)?<([^<>]*)>(?:</a>)?</td>(?:<[^<>]*>\s*)*?</tr>\n<tr><td></td><td><span class="datetime">\w*, (\d* \w* \d* \d*:\d*:\d*)#', $t_gitweb_data, $t_matches );
preg_match( '#authored by ([^"]*).*?authored by ([^"]*).*?>([^<]*\d*:\d*:\d*[^(<]*)'
. '.*?committed by ([^"]*).*?committed by ([^"]*).*?page_body">(.*?)</div>#',
$t_gitweb_data, $t_matches );
$t_commit['author'] = $t_matches[1];
$t_commit['author_email'] = $t_matches[2];
$t_commit['date'] = date( 'Y-m-d H:i:s', strtotime( $t_matches[3] ) );

if( preg_match( '#<tr><td>committer</td><td>(?:<a[^>]*>)?([^<>]*)(?:</a>)? *(?:<a[^>]*>)?<([^<>]*)>(?:</a>)?</td>(?:<[^<>]*>\s*)*?</tr>#', $t_gitweb_data, $t_matches ) ) {
$t_commit['committer'] = $t_matches[1];
$t_commit['committer_email'] = $t_matches[2];
}
$t_commit['committer'] = $t_matches[4];
$t_commit['committer_email'] = $t_matches[5];
$t_commit['message'] = trim( str_replace( '<br/>', PHP_EOL, $t_matches[6] ) );

$t_parents = array();
if( preg_match_all( '#<tr><td>parent</td><td class="sha1"><a [^<>]*>([a-f0-9]*)</a></td>#', $t_gitweb_data, $t_matches ) ) {
if ( preg_match_all( '#parent</td><td class="sha1"><[^>]*h=([0-9a-f]*)#', $t_gitweb_data, $t_matches ) ) {
foreach( $t_matches[1] as $t_match ) {
$t_parents[] = $t_commit['parent'] = $t_match;
}
}

preg_match( '#<div class="page_body">\n(.*)\n</div>#', $t_gitweb_data, $t_matches );
$t_commit['message'] = trim( str_replace( '<br/>', PHP_EOL, $t_matches[1] ) );

# Strip ref links and signoff spans from commit message
$t_commit['message'] = preg_replace( array(
'@<a[^>]*>([^<]*)<\/a>@',
'@<span[^>]*>([^<]*<[^>]*>[^<]*)<\/span>@', #finds <span..>signed-off by <email></span>
), '$1', $t_commit['message'] );
$t_commit['message'] = preg_replace( array( '#<a[^>]*>([^<]*)</a>#', '#<span[^>]*>(.*?)</span>#' ),
'$1', $t_commit['message'] );

# Prepend a # sign to mantis number
$t_commit['message'] = preg_replace( '#(mantis)\s+(\d+)#i', '$1 #$2',$t_commit['message'] );

# Parse for changed file data
$t_commit['files'] = array();

preg_match_all( '#<tr class="(?:light|dark)">\n<td><a class="list" href="[^"]*;h=(\w+);[^"]*">([^<>]+)</a></td>'.
'\n<td>(?:<span class="file_status (\w+)">[^<>]*</span>)?</td>#',
preg_match_all( '#class="list".*?h=(\w*)[^>]*>([^<]*)</a>(?:(?:</td><td><span class="file_status|[^%]*%) (\w*))?#',
$t_gitweb_files, $t_matches, PREG_SET_ORDER );

foreach( $t_matches as $t_file_matches ) {
Expand All @@ -313,10 +318,12 @@ private function commit_changeset( $p_repo, $p_input, $p_branch='' ) {
$t_file['revision'] = $t_file_matches[1];

if ( isset( $t_file_matches[3] ) ) {
if ( 'new' == $t_file_matches[3] ) {
if ( $t_file_matches[3] == 'new' or $t_file_matches[3] == 'moved' ) {
$t_file['action'] = 'add';
} else if ( 'deleted' == $t_file_matches[3] ) {
} else if ( $t_file_matches[3] == 'deleted' or $t_file_matches[3] == 'similarity' ) {
$t_file['action'] = 'rm';
} else {
$t_file['action'] = 'mod';
}
} else {
$t_file['action'] = 'mod';
Expand Down
7 changes: 6 additions & 1 deletion SourceGitweb/post-receive.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
# Licensed under the MIT license

read LINE

# Handle branch names with '+' character
LINE=`echo $LINE | sed -e 's/+/_plus_/g'`

LINE=`echo $LINE | sed -e 's/ /+/g'`

URL="http://localhost/mantisbt/plugin.php?page=Source/checkin"
PROJECT="test"
API_KEY="test"

CURL=/usr/bin/curl

echo "Updating Changeset to Mantis Bug Tracker"
${CURL} -s -S -d "repo_name=${PROJECT}" -d "data=${LINE}" ${URL}
${CURL} -sS -d "api_key=${API_KEY}" -d "repo_name=${PROJECT}" -d "data=${LINE}" ${URL}