Skip to content

Commit 501813b

Browse files
author
Matt Humphrey
committed
Added new methods for file manipulation and diffs
1 parent 2e4782a commit 501813b

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

lib/Gitlab/Api/Repositories.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public function commits($project_id, $page = 0, $per_page = self::PER_PAGE, $ref
2929
));
3030
}
3131

32+
public function commit($project_id, $sha)
33+
{
34+
return $this->get('projects/'.urlencode($project_id).'/repository/commits/'.urlencode($sha));
35+
}
36+
37+
public function diff($project_id, $sha)
38+
{
39+
return $this->get('projects/'.urlencode($project_id).'/repository/commits/'.urlencode($sha).'/diff');
40+
}
41+
3242
public function tree($project_id, array $params = array())
3343
{
3444
return $this->get('projects/'.urlencode($project_id).'/repository/tree', $params);
@@ -51,4 +61,33 @@ public function blob($project_id, $sha, $filepath)
5161
));
5262
}
5363

64+
public function createFile($project_id, $file_path, $content, $branch_name, $commit_message)
65+
{
66+
return $this->post('projects/'.urlencode($project_id).'/repository/files', array(
67+
'file_path' => $file_path,
68+
'branch_name' => $branch_name,
69+
'content' => $content,
70+
'commit_message' => $commit_message
71+
));
72+
}
73+
74+
public function updateFile($project_id, $file_path, $content, $branch_name, $commit_message)
75+
{
76+
return $this->put('projects/'.urlencode($project_id).'/repository/files', array(
77+
'file_path' => $file_path,
78+
'branch_name' => $branch_name,
79+
'content' => $content,
80+
'commit_message' => $commit_message
81+
));
82+
}
83+
84+
public function deleteFile($project_id, $file_path, $branch_name, $commit_message)
85+
{
86+
return $this->delete('projects/'.urlencode($project_id).'/repository/files', array(
87+
'file_path' => $file_path,
88+
'branch_name' => $branch_name,
89+
'commit_message' => $commit_message
90+
));
91+
}
92+
5493
}

lib/Gitlab/Model/Branch.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,25 @@ public function commits($page = 1, $per_page = Api::PER_PAGE)
5858
{
5959
return $this->project->commits($page, $per_page, $this->name);
6060
}
61+
62+
public function createFile($file_path, $content, $commit_message)
63+
{
64+
$data = $this->api('repo')->createFile($this->project->id, $file_path, $content, $this->name, $commit_message);
65+
66+
return File::fromArray($this->getClient(), $this, $data);
67+
}
68+
69+
public function updateFile($file_path, $content, $commit_message)
70+
{
71+
$data = $this->api('repo')->updateFile($this->project->id, $file_path, $content, $this->name, $commit_message);
72+
73+
return File::fromArray($this->getClient(), $this, $data);
74+
}
75+
76+
public function deleteFile($file_path, $commit_message)
77+
{
78+
$this->api('repo')->deleteFile($this->project->id, $file_path, $this->name, $commit_message);
79+
80+
return true;
81+
}
6182
}

lib/Gitlab/Model/File.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Gitlab\Model;
4+
5+
use Gitlab\Client;
6+
7+
class File extends AbstractModel
8+
{
9+
protected static $_properties = array(
10+
'project',
11+
'file_path',
12+
'branch_name'
13+
);
14+
15+
public static function fromArray(Client $client, Project $project, array $data)
16+
{
17+
$file = new File($project, $data['file_path'], $client);
18+
19+
return $file->hydrate($data);
20+
}
21+
22+
public function __construct(Project $project, $file_path = null, Client $client = null)
23+
{
24+
$this->setClient($client);
25+
26+
$this->project = $project;
27+
$this->file_path = $file_path;
28+
}
29+
}

lib/Gitlab/Model/Project.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ public function commits($page = 0, $per_page = Api::PER_PAGE, $ref_name = null)
245245
return $commits;
246246
}
247247

248+
public function commit($sha)
249+
{
250+
$data = $this->api('repo')->commit($this->id, $sha);
251+
252+
return Commit::fromArray($this->getClient(), $this, $data);
253+
}
254+
255+
public function diff($sha)
256+
{
257+
return $this->api('repo')->diff($this->id, $sha);
258+
}
259+
248260
public function tree(array $params = array())
249261
{
250262
$data = $this->api('repo')->tree($this->id, $params);
@@ -262,6 +274,27 @@ public function blob($sha, $filepath)
262274
return $this->api('repo')->blob($this->id, $sha, $filepath);
263275
}
264276

277+
public function createFile($file_path, $content, $branch_name, $commit_message)
278+
{
279+
$data = $this->api('repo')->createFile($this->id, $file_path, $content, $branch_name, $commit_message);
280+
281+
return File::fromArray($this->getClient(), $this, $data);
282+
}
283+
284+
public function updateFile($file_path, $content, $branch_name, $commit_message)
285+
{
286+
$data = $this->api('repo')->updateFile($this->id, $file_path, $content, $branch_name, $commit_message);
287+
288+
return File::fromArray($this->getClient(), $this, $data);
289+
}
290+
291+
public function deleteFile($file_path, $branch_name, $commit_message)
292+
{
293+
$this->api('repo')->deleteFile($this->id, $file_path, $branch_name, $commit_message);
294+
295+
return true;
296+
}
297+
265298
public function events()
266299
{
267300
$data = $this->api('projects')->events($this->id);

0 commit comments

Comments
 (0)