Skip to content

Commit e7c34aa

Browse files
Add parser for unified diffs
1 parent 7f1f420 commit e7c34aa

File tree

7 files changed

+511
-1
lines changed

7 files changed

+511
-1
lines changed

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ The following two commands (which you may have to run as `root`) are all that is
2525

2626
### Usage
2727

28+
The `Differ` class can be used to generate a textual representation of the difference between two strings:
29+
2830
```php
2931
use SebastianBergmann\Diff\Differ;
3032

@@ -39,3 +41,97 @@ The code above yields the output below:
3941
@@ @@
4042
-foo
4143
+bar
44+
45+
The `Parser` class can be used to parse a unified diff into an object graph:
46+
47+
```php
48+
use SebastianBergmann\Diff\Parser;
49+
use SebastianBergmann\Git;
50+
51+
$git = new Git('/usr/local/src/money');
52+
53+
$diff = $git->getDiff(
54+
'948a1a07768d8edd10dcefa8315c1cbeffb31833',
55+
'c07a373d2399f3e686234c4f7f088d635eb9641b'
56+
);
57+
58+
$parser = new Parser;
59+
60+
print_r($parser->parse($diff));
61+
```
62+
63+
The code above yields the output below:
64+
65+
Array
66+
(
67+
[0] => SebastianBergmann\Diff\Diff Object
68+
(
69+
[from:SebastianBergmann\Diff\Diff:private] => a/tests/MoneyTest.php
70+
[to:SebastianBergmann\Diff\Diff:private] => b/tests/MoneyTest.php
71+
[chunks:SebastianBergmann\Diff\Diff:private] => Array
72+
(
73+
[0] => SebastianBergmann\Diff\Chunk Object
74+
(
75+
[start:SebastianBergmann\Diff\Chunk:private] => 87
76+
[startRange:SebastianBergmann\Diff\Chunk:private] => 7
77+
[end:SebastianBergmann\Diff\Chunk:private] => 87
78+
[endRange:SebastianBergmann\Diff\Chunk:private] => 7
79+
[lines:SebastianBergmann\Diff\Chunk:private] => Array
80+
(
81+
[0] => SebastianBergmann\Diff\Line Object
82+
(
83+
[type:SebastianBergmann\Diff\Line:private] => 3
84+
[content:SebastianBergmann\Diff\Line:private] => * @covers SebastianBergmann\Money\Money::add
85+
)
86+
87+
[1] => SebastianBergmann\Diff\Line Object
88+
(
89+
[type:SebastianBergmann\Diff\Line:private] => 3
90+
[content:SebastianBergmann\Diff\Line:private] => * @covers SebastianBergmann\Money\Money::newMoney
91+
)
92+
93+
[2] => SebastianBergmann\Diff\Line Object
94+
(
95+
[type:SebastianBergmann\Diff\Line:private] => 3
96+
[content:SebastianBergmann\Diff\Line:private] => */
97+
)
98+
99+
[3] => SebastianBergmann\Diff\Line Object
100+
(
101+
[type:SebastianBergmann\Diff\Line:private] => 2
102+
[content:SebastianBergmann\Diff\Line:private] => public function testAnotherMoneyWithSameCurrencyObjectCanBeAdded()
103+
)
104+
105+
[4] => SebastianBergmann\Diff\Line Object
106+
(
107+
[type:SebastianBergmann\Diff\Line:private] => 1
108+
[content:SebastianBergmann\Diff\Line:private] => public function testAnotherMoneyObjectWithSameCurrencyCanBeAdded()
109+
)
110+
111+
[5] => SebastianBergmann\Diff\Line Object
112+
(
113+
[type:SebastianBergmann\Diff\Line:private] => 3
114+
[content:SebastianBergmann\Diff\Line:private] => {
115+
)
116+
117+
[6] => SebastianBergmann\Diff\Line Object
118+
(
119+
[type:SebastianBergmann\Diff\Line:private] => 3
120+
[content:SebastianBergmann\Diff\Line:private] => $a = new Money(1, new Currency('EUR'));
121+
)
122+
123+
[7] => SebastianBergmann\Diff\Line Object
124+
(
125+
[type:SebastianBergmann\Diff\Line:private] => 3
126+
[content:SebastianBergmann\Diff\Line:private] => $b = new Money(2, new Currency('EUR'));
127+
)
128+
129+
)
130+
131+
)
132+
133+
)
134+
135+
)
136+
137+
)

build/package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
<dir name="SebastianBergmann">
3434
<dir name="Diff">
3535
<file baseinstalldir="/" name="autoload.php" role="php"/>
36+
<file baseinstalldir="/" name="Chunk.php" role="php"/>
3637
<file baseinstalldir="/" name="Differ.php" role="php"/>
38+
<file baseinstalldir="/" name="Diff.php" role="php"/>
39+
<file baseinstalldir="/" name="Line.php" role="php"/>
40+
<file baseinstalldir="/" name="Parser.php" role="php"/>
3741
</dir>
3842
</dir>
3943
<file baseinstalldir="/" name="LICENSE" role="doc"/>

src/Chunk.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Diff
4+
*
5+
* Copyright (c) 2001-2013, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @package Diff
38+
* @author Sebastian Bergmann <[email protected]>
39+
* @author Kore Nordmann <[email protected]>
40+
* @copyright 2001-2013 Sebastian Bergmann <[email protected]>
41+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
42+
* @link http://www.github.com/sebastianbergmann/diff
43+
*/
44+
45+
namespace SebastianBergmann\Diff;
46+
47+
/**
48+
* @package Diff
49+
* @author Sebastian Bergmann <[email protected]>
50+
* @author Kore Nordmann <[email protected]>
51+
* @copyright 2001-2013 Sebastian Bergmann <[email protected]>
52+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
53+
* @link http://www.github.com/sebastianbergmann/diff
54+
*/
55+
class Chunk
56+
{
57+
private $start;
58+
private $startRange;
59+
private $end;
60+
private $endRange;
61+
private $lines;
62+
63+
public function __construct($start = null, $startRange = 1, $end = null, $endRange = 1, array $lines = array())
64+
{
65+
$this->start = (int) $start;
66+
$this->startRange = (int) $startRange;
67+
$this->end = (int) $end;
68+
$this->endRange = (int) $endRange;
69+
$this->lines = $lines;
70+
}
71+
72+
public function getStart()
73+
{
74+
return $this->start;
75+
}
76+
77+
public function getStartRange()
78+
{
79+
return $this->startRange;
80+
}
81+
82+
public function getEnd()
83+
{
84+
return $this->end;
85+
}
86+
87+
public function getEndRange()
88+
{
89+
return $this->endRange;
90+
}
91+
92+
public function getLines()
93+
{
94+
return $this->lines;
95+
}
96+
97+
public function setLines(array $lines)
98+
{
99+
$this->lines = $lines;
100+
}
101+
}

src/Diff.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Diff
4+
*
5+
* Copyright (c) 2001-2013, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @package Diff
38+
* @author Sebastian Bergmann <[email protected]>
39+
* @author Kore Nordmann <[email protected]>
40+
* @copyright 2001-2013 Sebastian Bergmann <[email protected]>
41+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
42+
* @link http://www.github.com/sebastianbergmann/diff
43+
*/
44+
45+
namespace SebastianBergmann\Diff;
46+
47+
/**
48+
* @package Diff
49+
* @author Sebastian Bergmann <[email protected]>
50+
* @author Kore Nordmann <[email protected]>
51+
* @copyright 2001-2013 Sebastian Bergmann <[email protected]>
52+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
53+
* @link http://www.github.com/sebastianbergmann/diff
54+
*/
55+
class Diff
56+
{
57+
private $from;
58+
private $to;
59+
private $chunks;
60+
61+
public function __construct($from, $to, array $chunks = array())
62+
{
63+
$this->from = $from;
64+
$this->to = $to;
65+
$this->chunks = $chunks;
66+
}
67+
68+
public function getFrom()
69+
{
70+
return $this->from;
71+
}
72+
73+
public function getTo()
74+
{
75+
return $this->to;
76+
}
77+
78+
public function getChunks()
79+
{
80+
return $this->chunks;
81+
}
82+
83+
public function setChunks(array $chunks)
84+
{
85+
$this->chunks = $chunks;
86+
}
87+
}

src/Line.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Diff
4+
*
5+
* Copyright (c) 2001-2013, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @package Diff
38+
* @author Sebastian Bergmann <[email protected]>
39+
* @author Kore Nordmann <[email protected]>
40+
* @copyright 2001-2013 Sebastian Bergmann <[email protected]>
41+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
42+
* @link http://www.github.com/sebastianbergmann/diff
43+
*/
44+
45+
namespace SebastianBergmann\Diff;
46+
47+
/**
48+
* @package Diff
49+
* @author Sebastian Bergmann <[email protected]>
50+
* @author Kore Nordmann <[email protected]>
51+
* @copyright 2001-2013 Sebastian Bergmann <[email protected]>
52+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
53+
* @link http://www.github.com/sebastianbergmann/diff
54+
*/
55+
class Line
56+
{
57+
const ADDED = 1;
58+
const REMOVED = 2;
59+
const UNCHANGED = 3;
60+
61+
private $type;
62+
private $content;
63+
64+
public function __construct($type = self::UNCHANGED, $content = null)
65+
{
66+
$this->type = $type;
67+
$this->content = $content;
68+
}
69+
70+
public function getContent()
71+
{
72+
return $this->content;
73+
}
74+
75+
public function getType()
76+
{
77+
return $this->type;
78+
}
79+
}

0 commit comments

Comments
 (0)