Skip to content

Commit b6a70db

Browse files
bobfloatsroot
authored and
root
committed
Added more functional test.
Code clean up
1 parent 3ae88ef commit b6a70db

File tree

5 files changed

+193
-34
lines changed

5 files changed

+193
-34
lines changed

src/VersionControl/GitCommandBundle/GitCommands/Command/GitDiffCommand.php

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,49 @@
1616
use VersionControl\GitCommandBundle\Entity\Collections\GitCommitFileCollection;
1717

1818
/**
19-
* Description of GitFilesCommand.
19+
* Description of GitDiffCommand.
20+
*
21+
* From Git documentation (https://git-scm.com/docs/git-diff):
22+
* Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes between two blob objects, or changes between two files on disk.
23+
* git diff [--options] [--] [<path>…​]
24+
* This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t. You can stage these changes by using git-add[1].
25+
*
26+
* git diff --no-index [--options] [--] [<path>…​]
27+
* This form is to compare the given two paths on the filesystem. You can omit the --no-index option when running the command in a working tree controlled by Git and at least one of the paths points outside the working tree, or when running the command outside a working tree controlled by Git.
28+
* git diff [--options] --cached [<commit>] [--] [<path>…​]
29+
* This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. If HEAD does not exist (e.g. unborn branches) and <commit> is not given, it shows all staged changes. --staged is a synonym of --cached.
30+
*
31+
* git diff [--options] <commit> [--] [<path>…​]
32+
* This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.
33+
*
34+
* git diff [--options] <commit> <commit> [--] [<path>…​]
35+
* This is to view the changes between two arbitrary <commit>.
36+
*
37+
* git diff [--options] <commit>..<commit> [--] [<path>…​]
38+
* This is synonymous to the previous form. If <commit> on one side is omitted, it will have the same effect as using HEAD instead.
39+
*
40+
* git diff [--options] <commit>...<commit> [--] [<path>…​]
41+
* This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B". You can omit any one of <commit>, which has the same effect as using HEAD instead.
42+
*
43+
* Just in case if you are doing something exotic, it should be noted that all of the <commit> in the above description, except in the last two forms that use ".." notations, can be any <tree>.
44+
*
45+
* For a more complete list of ways to spell <commit>, see "SPECIFYING REVISIONS" section in gitrevisions[7]. However, "diff" is about comparing two endpoints, not ranges, and the range notations ("<commit>..<commit>" and "<commit>...<commit>") do not mean a range as defined in the "SPECIFYING RANGES" section in gitrevisions[7].
46+
*
47+
* git diff [options] <blob> <blob>
48+
* This form is to view the differences between the raw contents of two blob objects.
2049
*
2150
* @author Paul Schweppe <paulschweppe@gmail.com>
2251
*/
2352
class GitDiffCommand extends AbstractGitCommand
2453
{
25-
/**
26-
* Get diff based on Commit hash id.
27-
*
28-
* @return array()
29-
*/
30-
public function getCommitDiff($commitHash)
31-
{
32-
$diffString = $this->command->runCommand('git --no-pager show --oneline '.escapeshellarg($commitHash));
33-
$diffParser = new GitDiffParser($diffString);
34-
$diffs = $diffParser->parse();
35-
36-
return $diffs;
37-
}
3854

3955
/**
40-
* Get diff on a file.
41-
*
56+
* Get diff on a file, that has changes that are not committed.
57+
* git diff [--options] [--] [<path>…​]
58+
* This form is to view the changes you made relative to the index
59+
* (staging area for the next commit). In other words, the differences
60+
* are what you could tell Git to further add to the index but you still haven’t.
61+
*
4262
* @return array()
4363
*/
4464
public function getDiffFile($filename)
@@ -67,7 +87,7 @@ public function getDiffFileBetweenCommits($filename, $previousCommitHash, $commi
6787
/**
6888
* Returns a list of files effected by a commit.
6989
*
70-
* @return array() Array of file paths
90+
* @return array() Array of VersionControl\GitCommandBundle\Entity\GitCommitFile
7191
*/
7292
public function getFilesInCommit($commitHash)
7393
{
@@ -81,6 +101,13 @@ public function getFilesInCommit($commitHash)
81101
return $files;
82102
}
83103

104+
/**
105+
* Gets the previous Commit Hash by from a commit has and related to a file
106+
*
107+
* @param string $commitHash
108+
* @param string $fileName
109+
* @return string commit short hash
110+
*/
84111
public function getPreviousCommitHash($commitHash = 'HEAD', $fileName = false)
85112
{
86113
$previousCommitHash = '';
@@ -97,19 +124,4 @@ public function getPreviousCommitHash($commitHash = 'HEAD', $fileName = false)
97124
return $previousCommitHash;
98125
}
99126

100-
public function getAllPreviousCommitHash($commitHash = 'HEAD', $fileName = false)
101-
{
102-
$previousCommitHash = '';
103-
$command = " git log --pretty=format:'%h' -n 60 ".escapeshellarg($commitHash).'';
104-
if ($fileName !== false) {
105-
$command .= ' '.escapeshellarg($fileName);
106-
}
107-
$response = $this->command->runCommand($command);
108-
$responseLines = $this->splitOnNewLine($response);
109-
if (count($responseLines) == 2) {
110-
$previousCommitHash = trim($responseLines['1']);
111-
}
112-
113-
return $previousCommitHash;
114-
}
115127
}

src/VersionControl/GitCommandBundle/Tests/GitCommandTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function tearDown()
9191
{
9292
$fs = new Filesystem();
9393
$fs->remove($this->path);
94-
//m::close();
94+
9595
}
9696

9797
/**
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/*
3+
* This file is part of the GitCommandBundle package.
4+
*
5+
* (c) Paul Schweppe <paulschweppe@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace VersionControl\GitCommandBundle\Tests\GitCommands\Command;
12+
13+
use VersionControl\GitCommandBundle\Tests\GitCommandTestCase;
14+
15+
/**
16+
* Description of GitBranchCommandTest.
17+
*
18+
* @author fr_user
19+
*/
20+
class GitDiffCommandTest extends GitCommandTestCase
21+
{
22+
/**
23+
* setUp, called on every method.
24+
*/
25+
public function setUp()
26+
{
27+
$this->initGitCommandsLocal();
28+
$this->gitCommands->command('init')->initRepository();
29+
$this->addFile('test');
30+
$this->gitCommands->command('commit')->stageAll();
31+
$this->gitCommands->command('commit')->commit('first commit', 'Paul Schweppe <paulschweppe@gmail.com>');
32+
$this->updateFile('test', 'Test content some more data');
33+
$this->gitCommands->command('commit')->stageAll();
34+
$this->gitCommands->command('commit')->commit('Second commit', 'Paul Schweppe <paulschweppe@gmail.com>');
35+
$this->updateFile('test', 'Test Data has changed');
36+
}
37+
38+
public function testGetPreviousCommitHash(){
39+
40+
$previousCommitHash = $this->gitCommands->command('diff')->getPreviousCommitHash();
41+
$this->assertTrue((strlen($previousCommitHash) === 7),'Previous Commit hash does not equal 7 characters: '.$previousCommitHash);
42+
43+
}
44+
45+
/**
46+
* Test Getting commit files.
47+
*/
48+
public function testGetDiffFile()
49+
{
50+
51+
$diffs = $this->gitCommands->command('diff')->getDiffFile('test');
52+
53+
$this->assertCount(1, $diffs,'Diff count does not equal expected 17. Actual:'.count($diffs).print_r($diffs,true));
54+
foreach ($diffs as $diff) {
55+
$this->assertInstanceOf("\VersionControl\GitCommandBundle\Entity\GitDiff", $diff);
56+
}
57+
}
58+
59+
public function testGetDiffFileBetweenCommits(){
60+
61+
$previousCommitHash = $this->gitCommands->command('diff')->getPreviousCommitHash();
62+
63+
$diffs = $this->gitCommands->command('diff')->getDiffFileBetweenCommits('test',$previousCommitHash,'HEAD');
64+
foreach ($diffs as $diff) {
65+
$this->assertInstanceOf("\VersionControl\GitCommandBundle\Entity\GitDiff", $diff);
66+
}
67+
}
68+
69+
public function testGetFilesInCommit(){
70+
$filesCommitted = $this->gitCommands->command('diff')->getFilesInCommit('HEAD');
71+
72+
$this->assertCount(1, $filesCommitted,'Only one file should have been commited. Actual:'.count($filesCommitted));
73+
74+
foreach ($filesCommitted as $commitFile) {
75+
$this->assertInstanceOf("VersionControl\GitCommandBundle\Entity\GitCommitFile", $commitFile);
76+
}
77+
}
78+
79+
80+
81+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/*
3+
* This file is part of the GitCommandBundle package.
4+
*
5+
* (c) Paul Schweppe <paulschweppe@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace VersionControl\GitCommandBundle\Tests\GitCommands\Command;
12+
13+
use VersionControl\GitCommandBundle\Tests\GitCommandTestCase;
14+
use VersionControl\GitCommandBundle\GitCommands\GitDiffParser;
15+
/**
16+
* Description of GitBranchCommandTest.
17+
*
18+
* @author fr_user
19+
*/
20+
class GitDiffParserTest extends GitCommandTestCase
21+
{
22+
/**
23+
* setUp, called on every method.
24+
*/
25+
public function setUp()
26+
{
27+
28+
}
29+
30+
/**
31+
* Test Getting commit files.
32+
*/
33+
public function testGetCommitDiff()
34+
{
35+
$diffString =
36+
'diff --git a/src/VersionControl/GitControlBundle/Controller/ProjectHistoryController.php b/src/VersionControl/GitControlBundle/Controller/ProjectHistoryController.php
37+
index b9b5a2a..53b3b2d 100644
38+
--- a/src/VersionControl/GitControlBundle/Controller/ProjectHistoryController.php
39+
+++ b/src/VersionControl/GitControlBundle/Controller/ProjectHistoryController.php
40+
@@ -50,6 +50,7 @@ class ProjectHistoryController extends BaseProjectController
41+
42+
$currentPage = $request->query->get(\'page\', 1);
43+
$filter = false;
44+
+ $keyword = \'\';
45+
//Search
46+
/*$keyword = $request->query->get(\'keyword\', false);
47+
$filter= $request->query->get(\'filter\', false);
48+
@@ -103,7 +104,7 @@ class ProjectHistoryController extends BaseProjectController
49+
\'totalCount\' => $this->gitLogCommand->getTotalCount(),
50+
\'limit\' => $this->gitLogCommand->getLimit(),
51+
\'currentPage\' => $this->gitLogCommand->getPage()+1,
52+
- //\'keyword\' => $keyword,
53+
+ \'keyword\' => $keyword,
54+
\'filter\' => $filter,
55+
\'searchForm\' => $searchForm->createView()
56+
));';
57+
58+
$diffParser = new GitDiffParser($diffString);
59+
$diffs = $diffParser->parse();
60+
61+
$this->assertCount(1, $diffs,'Diff count does not equal expected 17. Actual:'.count($diffs).print_r($diffs,true));
62+
foreach ($diffs as $diff) {
63+
$this->assertInstanceOf("\VersionControl\GitCommandBundle\Entity\GitDiff", $diff);
64+
}
65+
66+
}
67+
}

src/VersionControl/GitControlBundle/Controller/ProjectHistoryController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ public function commitHistoryAction($id, $commitHash)
126126
$gitLog = $this->gitLogCommand->execute()->getFirstResult();
127127

128128
//Get git Diff
129-
//$gitDiffs = $gitDiffCommand->getCommitDiff($commitHash);
130129
$files = $gitDiffCommand->getFilesInCommit($commitHash);
131130

132131
return array_merge($this->viewVariables, array(

0 commit comments

Comments
 (0)