Skip to content

Commit

Permalink
added unstage() method
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosister committed Jul 3, 2013
1 parent 63597cd commit cfbc33e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
3 changes: 2 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ todo
* better status handling with --porcelain DONE
* CommitCollection instead of countable
* tag messages and signed tags
* named exceptions
* named exceptions DONE
* unstage DONE

next
* git blame
Expand Down
20 changes: 19 additions & 1 deletion src/GitElephant/Command/MainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MainCommand extends BaseCommand
const GIT_CHECKOUT = 'checkout';
const GIT_MOVE = 'mv';
const GIT_REMOVE = 'rm';
const GIT_RESET = 'reset';

/**
* @return MainCommand
Expand Down Expand Up @@ -76,7 +77,7 @@ public function status($porcelain = false)
}

/**
* Add a node to the repository
* Add a node to the stage
*
* @param string $what what should be added to the repository
*
Expand All @@ -91,6 +92,23 @@ public function add($what = '.')
return $this->getCommand();
}

/**
* Remove a node from the stage and put in the working tree
*
* @param string $what what should be removed from the stage
*
* @return string
*/
public function unstage($what)
{
$this->clearAll();
$this->addCommandName(self::GIT_RESET);
$this->addCommandArgument('HEAD');
$this->addPath($what);

return $this->getCommand();
}

/**
* Commit
*
Expand Down
17 changes: 9 additions & 8 deletions src/GitElephant/Objects/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function createFromOutputLine(Repository $repository, $outputLine)
$name = substr($fullPath, $pos + 1);
}

return new self($slices['permissions'], $slices['type'], $slices['sha'], $slices['size'], $name, $path);
return new self($repository, $slices['permissions'], $slices['type'], $slices['sha'], $slices['size'], $name, $path);
}

/**
Expand Down Expand Up @@ -140,14 +140,15 @@ public static function getLineSlices($line)
/**
* Class constructor
*
* @param string $permissions node permissions
* @param string $type node type
* @param string $sha node sha
* @param string $size node size in bytes
* @param string $name node name
* @param string $path node path
* @param \GitElephant\Repository $repository repository instance
* @param string $permissions node permissions
* @param string $type node type
* @param string $sha node sha
* @param string $size node size in bytes
* @param string $name node name
* @param string $path node path
*/
public function __construct($permissions, $type, $sha, $size, $name, $path)
public function __construct(Repository $repository, $permissions, $type, $sha, $size, $name, $path)
{
$this->permissions = $permissions;
$this->type = $type;
Expand Down
10 changes: 9 additions & 1 deletion src/GitElephant/Objects/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,15 @@ private function parseLine($line)
}
if (!in_array($name, $this->pathChildren)) {
$path = rtrim(rtrim($slices['fullPath'], $name), '/');
$treeObject = new Object($slices['permissions'], $slices['type'], $slices['sha'], $slices['size'], $name, $path);
$treeObject = new Object(
$this->repository,
$slices['permissions'],
$slices['type'],
$slices['sha'],
$slices['size'],
$name,
$path
);
$this->children[] = $treeObject;
$this->pathChildren[] = $name;
}
Expand Down
14 changes: 14 additions & 0 deletions src/GitElephant/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ public function stage($path = '.')
return $this;
}

/**
* Unstage a tree content
*
* @param string|Object $path the path to unstage
*
* @return Repository
*/
public function unstage($path)
{
$this->caller->execute(MainCommand::getInstance()->unstage($path));

return $this;
}

/**
* Move a file/directory
*
Expand Down
8 changes: 8 additions & 0 deletions tests/GitElephant/Command/MainCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public function testAdd()
$this->assertEquals(MainCommand::GIT_ADD." 'foo'", $this->mainCommand->add('foo'));
}

/**
* unstage test
*/
public function testUnstage()
{
$this->assertEquals(MainCommand::GIT_CHECKOUT." -- 'foo'", $this->mainCommand->unstage('foo'));
}

/**
* commit test
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/GitElephant/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ public function testStage()
$this->assertTrue($match, 'stageAll error, git status should give Changes to be committed');
}

/**
* @covers GitElephant\Repository::unstage
*/
public function testUnstage()
{
$this->getRepository()->init();
$this->addFile('test');
$this->assertCount(1, $this->getRepository()->getStatus()->untracked());
$this->assertCount(0, $this->getRepository()->getStatus()->added());
$this->getRepository()->stage('test');
$this->assertCount(0, $this->getRepository()->getStatus()->untracked());
$this->assertCount(1, $this->getRepository()->getStatus()->added());
$this->getRepository()->unstage('test');
$this->assertCount(1, $this->getRepository()->getStatus()->untracked());
$this->assertCount(0, $this->getRepository()->getStatus()->added());
}

/**
* @covers GitElephant\Repository::commit
* @covers GitElephant\Repository::getStatusOutput
Expand Down

0 comments on commit cfbc33e

Please sign in to comment.