Skip to content

Commit 31245d4

Browse files
author
Matt Humphrey
committed
Added compare() method to projects, along with Comparison and Diff objects
1 parent 8598bec commit 31245d4

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

lib/Gitlab/Model/AbstractModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ protected function setData($field, $value)
8080
return $this;
8181
}
8282

83+
/**
84+
* @return array
85+
*/
86+
public function getData()
87+
{
88+
return $this->data;
89+
}
90+
8391
/**
8492
* @param string $property
8593
* @param mixed $value

lib/Gitlab/Model/Comparison.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php namespace Gitlab\Model;
2+
3+
use Gitlab\Client;
4+
5+
/**
6+
* Class Comparison
7+
*
8+
* @property-read bool $compare_timeout
9+
* @property-read bool $compare_same_ref
10+
* @property-read Commit $commit
11+
* @property-read Commit[] $commits
12+
* @property-read Diff[] $diffs
13+
* @property-read Project $project
14+
*/
15+
class Comparison extends AbstractModel
16+
{
17+
/**
18+
* @var array
19+
*/
20+
protected static $properties = array(
21+
'commit',
22+
'commits',
23+
'diffs',
24+
'compare_timeout',
25+
'compare_same_ref',
26+
'project'
27+
);
28+
29+
/**
30+
* @param Client $client
31+
* @param Project $project
32+
* @param array $data
33+
* @return Comparison
34+
*/
35+
public static function fromArray(Client $client, Project $project, array $data)
36+
{
37+
$file = new static($project, $client);
38+
39+
if (isset($data['commit'])) {
40+
$data['commit'] = Commit::fromArray($client, $project, $data['commit']);
41+
}
42+
43+
if (isset($data['commits'])) {
44+
$commits = array();
45+
foreach ($data['commits'] as $commit) {
46+
$commits[] = Commit::fromArray($client, $project, $commit);
47+
}
48+
49+
$data['commits'] = $commits;
50+
}
51+
52+
if (isset($data['diffs'])) {
53+
$diffs = array();
54+
foreach ($data['diffs'] as $diff) {
55+
$diffs[] = Diff::fromArray($client, $project, $diff);
56+
}
57+
58+
$data['diffs'] = $diffs;
59+
}
60+
61+
return $file->hydrate($data);
62+
}
63+
64+
/**
65+
* @param Project $project
66+
* @param Client $client
67+
*/
68+
public function __construct(Project $project, Client $client = null)
69+
{
70+
$this->setClient($client);
71+
$this->setData('project', $project);
72+
}
73+
}

lib/Gitlab/Model/Diff.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php namespace Gitlab\Model;
2+
3+
use Gitlab\Client;
4+
5+
/**
6+
* Class Diff
7+
*
8+
* @property-read string $old_path
9+
* @property-read string $new_path
10+
* @property-read string $a_mode
11+
* @property-read string $b_mode
12+
* @property-read string $diff
13+
* @property-read bool $new_file
14+
* @property-read bool $renamed_file
15+
* @property-read bool $deleted_file
16+
* @property-read Project $project
17+
*/
18+
class Diff extends AbstractModel
19+
{
20+
/**
21+
* @var array
22+
*/
23+
protected static $properties = array(
24+
'old_path',
25+
'new_path',
26+
'a_mode',
27+
'b_mode',
28+
'diff',
29+
'new_file',
30+
'renamed_file',
31+
'deleted_file',
32+
'project'
33+
);
34+
35+
/**
36+
* @param Client $client
37+
* @param Project $project
38+
* @param array $data
39+
* @return Diff
40+
*/
41+
public static function fromArray(Client $client, Project $project, array $data)
42+
{
43+
$diff = new static($project, $client);
44+
45+
return $diff->hydrate($data);
46+
}
47+
48+
/**
49+
* @param Project $project
50+
* @param Client $client
51+
*/
52+
public function __construct(Project $project, Client $client = null)
53+
{
54+
$this->setClient($client);
55+
$this->setData('project', $project);
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function __toString()
62+
{
63+
return $this->diff;
64+
}
65+
}

lib/Gitlab/Model/Project.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,18 @@ public function diff($sha)
470470
return $this->api('repo')->diff($this->id, $sha);
471471
}
472472

473+
/**
474+
* @param string $from
475+
* @param string $to
476+
* @return Comparison
477+
*/
478+
public function compare($from, $to)
479+
{
480+
$data = $this->api('repo')->compare($this->id, $from, $to);
481+
482+
return Comparison::fromArray($this->getClient(), $this, $data);
483+
}
484+
473485
/**
474486
* @param array $params
475487
* @return Node[]

0 commit comments

Comments
 (0)