Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rudimentary methods for writing data #166

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
17b0c5a
rudimentary methods for writing data
chris-kruining Mar 16, 2020
0c77a8d
fixed style issues
chris-kruining Mar 16, 2020
60af694
Update Repository.php
chris-kruining Mar 16, 2020
1f9f93c
Update Repository.php
chris-kruining Mar 16, 2020
2a6ea33
Update Repository.php
chris-kruining Mar 16, 2020
9389d3f
Update Repository.php
chris-kruining Mar 19, 2020
b2d7641
Update WorkingCopy.php
chris-kruining Mar 19, 2020
253a6c6
no clue
chris-kruining Mar 19, 2020
7da46a9
Update WorkingCopyTest.php
chris-kruining Mar 19, 2020
bb31f54
Update workingcopy.md
chris-kruining Mar 19, 2020
ec1c5b2
Update repository.md
chris-kruining Mar 19, 2020
a86d63e
Update WorkingCopyTest.php
chris-kruining Mar 19, 2020
cbb03c0
Update RepositoryTest.php
chris-kruining Mar 19, 2020
33962b1
Update Repository.php
chris-kruining Mar 19, 2020
4c27764
Update RepositoryTest.php
chris-kruining Mar 19, 2020
278389e
Update Repository.php
chris-kruining Mar 19, 2020
2bf59f3
Update workingcopy.md
chris-kruining Mar 19, 2020
e3e796f
Update Repository.php
chris-kruining Mar 19, 2020
45d538c
Update doc/repository.md
GrahamCampbell Mar 20, 2020
7264320
Update doc/repository.md
GrahamCampbell Mar 20, 2020
9749f07
Update doc/workingcopy.md
GrahamCampbell Mar 20, 2020
bb0ddeb
lets make the actual regex work...
chris-kruining Mar 20, 2020
8301907
Fixed signatures and doc
GrahamCampbell Mar 21, 2020
058e88e
Fixed phpdoc
GrahamCampbell Mar 21, 2020
522e378
Update workingcopy.md
GrahamCampbell Mar 21, 2020
57902ff
Actually support having a commit author as documented
GrahamCampbell Mar 21, 2020
12d2482
Update WorkingCopyTest.php
GrahamCampbell Mar 21, 2020
c198250
Fixed CS
GrahamCampbell Mar 21, 2020
2d5d922
Fixed tests
GrahamCampbell Mar 21, 2020
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
18 changes: 18 additions & 0 deletions doc/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,21 @@ It is possible to send environment variables to the `git` commands.
```php
$repository = new Gitonomy\Git\Repository('/tmp/foo', ['environment_variables' => ['GIT_']])
```

Pull
----

You can pull changes from remote by calling `pull()`:

```php
$repository->pull();
```

Push
----

You can push changes to remote by calling `push()`:

```php
$repository->push();
```
38 changes: 38 additions & 0 deletions doc/workingcopy.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,41 @@ You can get pending modifications on tracked files by calling method
```php
$diff = $wc->getDiffPending();
```

Staging file(s)
---------------

You can stage changed files by calling `stage(...files)`:

```php
$wc->stage('file1.txt', 'file2.txt');
```

Unstaging file(s)
-----------------

You can unstage files by calling `unstage(...files)`:

```php
$wc->unstage('file1.txt', 'file2.txt');
```

Discard file(s) changes
-----------------------

You can discard file(s) changed by calling `discard(...files)`:

```php
$wc->discard('file1.txt', 'file2.txt');
```

Committing
---------

You can commit changed files by calling `commit(message, author, ...files)`:

NOTE :: this will commit all staged files, the ...files argument simply stages those files before commiting, if you already have files staged by having called `stage(...files)` these files are included in the commit

```php
$wc->commit('summary', 'author', 'file1.txt', 'file2.txt');
```
24 changes: 24 additions & 0 deletions src/Gitonomy/Git/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,30 @@ public function setDescription($description)
return $this;
}

/**
* Executes pull command.
*
* @return Repository the current repository
*/
public function pull()
{
$result = $this->run('pull');

return preg_match('/\d (?:file|files) changed, \d insertion\(\+\), \d deletion\(\-\)/', $result) === 1;
alexandresalome marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Executes push command.
*
* @return Repository the current repository
*/
public function push()
{
$result = $this->run('push');

return strpos($result, 'Writing objects: 100%') !== false;
alexandresalome marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* This command is a facility command. You can run any command
* directly on git repository.
Expand Down
56 changes: 56 additions & 0 deletions src/Gitonomy/Git/WorkingCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,62 @@ public function checkout($revision, $branch = null)
return $this;
}

/**
* Stages the files provided by arguments.
*
* @return Repository the current repository
*/
public function stage(string ...$files)
{
foreach ($files as $file) {
$this->run('add', [$file]);
}

return $this;
}

/**
* Unstages the files provided by arguments.
*
* @return Repository the current repository
*/
public function unstage(string ...$files)
{
foreach ($files as $file) {
$this->run('restore', ['--staged', $file]);
}

return $this;
}

/**
* Discards file changed from files provided by arguments.
*
* @return Repository the current repository
*/
public function discard(string ...$files)
{
foreach ($files as $file) {
$this->run('checkout', ['--', $file]);
}

return $this;
}

/**
* Creates a commit with the message provided.
* Optionally stages files provided.
*
* @return Repository the current repository
*/
public function commit(string $message, string ...$files)
{
$this->stage(...$files);
$this->run('commit', ['-m', $message]);

return $this;
}

protected function run($command, array $args = [])
{
return $this->repository->run($command, $args);
Expand Down
16 changes: 16 additions & 0 deletions tests/Gitonomy/Git/Tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public function testGetDescription($repository)
$this->assertSame("Unnamed repository; edit this file 'description' to name the repository.\n", $repository->getDescription());
}

/*
* @dataProvider provideFoobar
*/
public function testPull($repository)
{
$this->assertIsBool($repository->pull());
}

/*
* @dataProvider provideFoobar
*/
public function testPush($repository)
{
$this->assertIsBool($repository->push());
}

/**
* @dataProvider provideFoobar
*/
Expand Down
62 changes: 62 additions & 0 deletions tests/Gitonomy/Git/Tests/WorkingCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,66 @@ public function testGetUntracked()

$this->assertContains('untracked.txt', $wc->getUntrackedFiles());
}

public function testStage()
{
$repository = self::createFoobarRepository(false);
$wc = $repository->getWorkingCopy();

$file = $repository->getWorkingDir().'/test.sh';
file_put_contents($file, 'test');

$this->assertContains('test.sh', $wc->getDiffPending());

$wc->stage('test.sh');

$this->assertContains('test.sh', $wc->getDiffStaged());
}

public function testUnstage()
{
$repository = self::createFoobarRepository(false);
$wc = $repository->getWorkingCopy();

$file = $repository->getWorkingDir().'/test.sh';
file_put_contents($file, 'test');

$wc->stage('test.sh');
$this->assertContains('test.sh', $wc->getDiffStaged());

$wc->unstage('test.sh');
$this->assertContains('test.sh', $wc->getDiffPending());
}

public function testDiscard()
{
$repository = self::createFoobarRepository(false);
$wc = $repository->getWorkingCopy();

$file = $repository->getWorkingDir().'/test.sh';
file_put_contents($file, 'test');

$this->assertContains('test.sh', $wc->getDiffPending());

$wc->discard('test.sh');

$diffPending = $wc->getDiffPending();
$this->assertCount(0, $diffPending->getFiles());
}

public function testCommit()
{
$repository = self::createFoobarRepository(false);
$wc = $repository->getWorkingCopy();

$file = $repository->getWorkingDir().'/test.sh';
file_put_contents($file, 'test');

$this->assertContains('test.sh', $wc->getDiffPending());

$wc->commit('this is the commit message', 'John Doe', 'test.sh');

$diffPending = $wc->getDiffPending();
$this->assertCount(0, $diffPending->getFiles());
}
}