Skip to content
Open
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
32 changes: 28 additions & 4 deletions lib/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function payload( WordPress_GitHub_Sync_Payload $payload ) {
*/
$error = false;

$result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ) );
$commits = $payload->get_commits();
$result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ), $commits );

if ( is_wp_error( $result ) ) {
$error = $result;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function payload( WordPress_GitHub_Sync_Payload $payload ) {
* @return string|WP_Error
*/
public function master() {
return $this->commit( $this->app->api()->fetch()->master() );
return $this->commit( $this->app->api()->fetch()->master(), null );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing in null here, we should not pass in anything and update the default value for this method.

}

/**
Expand All @@ -86,7 +87,20 @@ public function master() {
*
* @return string|WP_Error
*/
protected function commit( $commit ) {
protected function commit( $commit , $commits_array) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...in which case $commits_array = array() should go here.

$head_commit = array();
$mod_files = array();
$update_all = true;
foreach ( $commits_array as $commit_obj ) {
if ( $commit_obj != null ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we could remove this null check, unless there's a possibility of getting null from the API.

$next_commit = json_decode(json_encode($commit_obj), true);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can cast the object to an array with $next_commit = (array) $commit_obj; instead of the whole JSON thing.

if ( array_key_exists( 'modified', $next_commit ) ) {
$mod_files = array_merge($mod_files, $next_commit['modified']);
$update_all = false;
}
}
}

if ( is_wp_error( $commit ) ) {
return $commit;
}
Expand All @@ -103,10 +117,20 @@ protected function commit( $commit ) {
continue;
}

$posts[] = $post = $this->blob_to_post( $blob );
$post = $this->blob_to_post( $blob );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we're still iterating over the tree (see $commit->tree()->blobs() above), is the savings here primarily from making fewer DB calls?


$found = $update_all;
foreach ( $mod_files as $modified_file ) {
if ($blob->path() == $modified_file) {
$found = true;
}
}

if ( $post->is_new() ) {
$posts[] = $post;
$new[] = $post;
} elseif ( $found ) {
$posts[] = $post;
}
}

Expand Down