├── .LICENSE_HEADER.hh.txt ├── .gitattributes ├── .github └── workflows │ ├── build-and-test.yml │ ├── hhvm.gpg.key │ └── install-composer.sh ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── clidiff.png ├── composer.json ├── hh_autoload.json ├── hhast-lint.json ├── src ├── CLIColoredUnifiedDiff.php ├── ColoredUnifiedDiff.php ├── Diff.php ├── DiffDeleteOp.php ├── DiffInsertOp.php ├── DiffKeepOp.php ├── DiffOp.php ├── StringDiff.php └── cluster.php └── udiff.png /.LICENSE_HEADER.hh.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017-present, Facebook, Inc. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the MIT license found in the 6 | * LICENSE file in the root directory of this source tree. 7 | * 8 | */ 9 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | tests/ export-ignore 2 | .hhconfig export-ignore 3 | .hhvmconfig.hdf export-ignore 4 | -------------------------------------------------------------------------------- /.github/workflows/build-and-test.yml: -------------------------------------------------------------------------------- 1 | name: Continuous Integration 2 | on: 3 | push: 4 | pull_request: 5 | schedule: 6 | - cron: '42 15 * * *' 7 | jobs: 8 | build: 9 | name: HHVM ${{matrix.hhvm}} - ${{matrix.os}} 10 | strategy: 11 | # Run tests on all OS's and HHVM versions, even if one fails 12 | fail-fast: false 13 | matrix: 14 | os: [ ubuntu ] 15 | hhvm: 16 | - '4.128' 17 | - latest 18 | - nightly 19 | runs-on: ${{matrix.os}}-latest 20 | steps: 21 | - uses: actions/checkout@v2 22 | - name: Create branch for version alias 23 | run: git checkout -b CI_current_pull_request 24 | - uses: hhvm/actions/hack-lint-test@master 25 | with: 26 | hhvm: ${{matrix.hhvm}} 27 | -------------------------------------------------------------------------------- /.github/workflows/hhvm.gpg.key: -------------------------------------------------------------------------------- 1 | -----BEGIN PGP PUBLIC KEY BLOCK----- 2 | Version: GnuPG v1 3 | 4 | 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 35 | =3DTP 36 | -----END PGP PUBLIC KEY BLOCK----- 37 | -------------------------------------------------------------------------------- /.github/workflows/install-composer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # From https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md 4 | 5 | EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)" 6 | php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 7 | ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")" 8 | 9 | if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ] 10 | then 11 | >&2 echo 'ERROR: Invalid installer checksum' 12 | rm composer-setup.php 13 | exit 1 14 | fi 15 | 16 | php composer-setup.php "$@" 17 | RESULT=$? 18 | rm composer-setup.php 19 | exit $RESULT 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | tests/examples/*.out 3 | *.swp 4 | *.swo 5 | .DS_Store 6 | composer.lock 7 | .*.hhast.*cache 8 | .var/ 9 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies within all project spaces, and it also applies when 49 | an individual is representing the project or its community in public spaces. 50 | Examples of representing a project or community include using an official 51 | project e-mail address, posting via an official social media account, or acting 52 | as an appointed representative at an online or offline event. Representation of 53 | a project may be further defined and clarified by project maintainers. 54 | 55 | This Code of Conduct also applies outside the project spaces when there is a 56 | reasonable belief that an individual's behavior may have a negative impact on 57 | the project or its community. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported by contacting the project team at . All 63 | complaints will be reviewed and investigated and will result in a response that 64 | is deemed necessary and appropriate to the circumstances. The project team is 65 | obligated to maintain confidentiality with regard to the reporter of an incident. 66 | Further details of specific enforcement policies may be posted separately. 67 | 68 | Project maintainers who do not follow or enforce the Code of Conduct in good 69 | faith may face temporary or permanent repercussions as determined by other 70 | members of the project's leadership. 71 | 72 | ## Attribution 73 | 74 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 75 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 76 | 77 | [homepage]: https://www.contributor-covenant.org 78 | 79 | For answers to common questions about this code of conduct, see 80 | https://www.contributor-covenant.org/faq 81 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to DiffLib 2 | 3 | We want to make contributing to this project as easy and transparent as 4 | possible. 5 | 6 | ## Code of Conduct 7 | The code of conduct is described in [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md). 8 | 9 | ## Our Development Process 10 | 11 | We develop on main, and make releases on an as-needed basis - usually 12 | to support new HHVM releases. 13 | 14 | ## Pull Requests 15 | We actively welcome your pull requests. 16 | 17 | 1. Fork the repo and create your branch from `main`. 18 | 2. If you've added code that should be tested, add tests. 19 | 3. If you've changed APIs, update the documentation. 20 | 4. Ensure the test suite passes. 21 | 5. Make sure your code lints. 22 | 6. If you haven't already, complete the Contributor License Agreement ("CLA"). 23 | 24 | ## Contributor License Agreement ("CLA") 25 | In order to accept your pull request, we need you to submit a CLA. You only need 26 | to do this once to work on any of Facebook's open source projects. 27 | 28 | Complete your CLA here: 29 | 30 | ## Issues 31 | We use GitHub issues to track public bugs. Please ensure your description is 32 | clear and has sufficient instructions to be able to reproduce the issue. 33 | 34 | Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe 35 | disclosure of security bugs. In those cases, please go through the process 36 | outlined on that page and do not file a public issue. 37 | 38 | ## Coding Style 39 | * 2 spaces for indentation rather than tabs 40 | * 80 character line length 41 | * Match hackfmt output 42 | * Pass the configured HHAST linters 43 | 44 | ## License 45 | By contributing to DiffLib, you agree that your contributions will be licensed 46 | under the LICENSE file in the root directory of this source tree. 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-present, Facebook, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DiffLib 2 | 3 | [![Continuous Integration](https://github.com/hhvm/difflib/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/hhvm/difflib/actions/workflows/build-and-test.yml) 4 | 5 | DiffLib is a Hack library for creating and parsing diffs. Diffs can be created 6 | between any two sequences of items. Additional helpers (such as support for 7 | unified diffs, and colored diffs) are provided for string diffs. 8 | 9 | Diffs are represented as a sequence of `DiffOp` operations; these can 'keep' an 10 | element (i.e. it's unchanged in both sequences), insert an element, or delete an 11 | element. For example, in a unified diff, these would be: 12 | 13 | ```diff 14 | DiffKeepOp 15 | -DiffDeleteOp 16 | +DiffInsertOp 17 | ``` 18 | 19 | String diffs are typically represented as a sequence of lines, but can also be 20 | represented as a sequence of characters, allowing intra-line diffs. 21 | 22 | ## Diff output formats 23 | 24 | difflib can create standard unified diffs: 25 | 26 | ![@@ -1 +1@@ -Foo Bat Baz +Foo Bar Baz](udiff.png) 27 | 28 | difflib can also create colored diffs, with intra-line edits highlighted: 29 | 30 | ![same diff as above, but with 'Bat' highlighted red, 'Bar' highlighted 31 | green](clidiff.png) 32 | 33 | A concrete implementation is provided for standard CLI terminals, and an 34 | abstract base class for other implementations, such as HTML or XHP. 35 | 36 | ## Examples 37 | 38 | ```Hack 39 | use namespace Facebook\DiffLib; 40 | 41 | function create_unified_diff(string $from, string $to): string { 42 | return DiffLib\StringDiff::lines($from, $to)->getUnifiedDiff(); 43 | } 44 | 45 | function create_colored_diff(string $from, string $to): string { 46 | return DiffLib\CLIColoredUnifiedDiff::create($from, $to); 47 | } 48 | 49 | final class IntDiff extends DiffLib\Diff { 50 | const type TContent = int; 51 | } 52 | 53 | function dump_int_diff(vec $from, vec $to): void { 54 | $diff = (new IntDiff($from, $to))->getDiff(); 55 | foreach ($diff as $op) { 56 | if ($op is DiffLib\DiffKeepOp<_>) { 57 | \printf(" %d\n", $op->getContent()); 58 | } else if ($op is DiffLib\DiffDeleteOp<_>) { 59 | \printf("- %d\n", $op->getContent()); 60 | } else { 61 | $op = $op as DiffLib\DiffInsertOp<_>; 62 | printf("+ %d\n", $op->getContent()); 63 | } 64 | } 65 | } 66 | 67 | ``` 68 | 69 | `dump_int_diff(vec[1, 3, 9], vec[1, 4, 9])` will produce this output: 70 | 71 | ``` 72 | 1 73 | - 3 74 | + 4 75 | 9 76 | ``` 77 | 78 | ## Requirements 79 | 80 | * The current release version of HHVM 81 | 82 | ## Installing DiffLib 83 | 84 | ``` 85 | composer require facebook/difflib 86 | ``` 87 | 88 | ## How DiffLib works 89 | 90 | Diffs are created using Myers' diff algorithm: 91 | Myers, E.W. Algorithmica (1986) 1: 251. https://doi.org/10.1007/BF01840446 92 | 93 | For more details, see [the commented implementation](src/Diff.php). 94 | 95 | ## Full documentation 96 | 97 | - `DiffLib\Diff`: abstract base class. Needs to be subclassed to operate on 98 | any particular type. 99 | - `DiffLib\StringDiff`: final class for diffing strings, and adds support 100 | for creating unified diffs 101 | - `DiffLib\ColoredUnifiedDiff`: abstract class for rendering unified diffs. 102 | Output may be any type - for example, strings or XHP. 103 | - `DiffLib\CLIColoredUnifiedDiff`: abstract final class for rendering unified 104 | diffs to a terminal that supports color escape sequences. Provides intra-line 105 | highlighting. 106 | - `DiffLib\DiffOp`: abstract class for a diff operation. Sealed to 107 | `DiffInsertOp`, `DiffKeepOp`, and `DiffDeleteOp`. 108 | - `DiffLib\cluster()`: utility function to group together sequential operations 109 | of the same kind. Converts `vec>` to a generally shorter number of 110 | `vec>>` 111 | 112 | ## License 113 | DiffLib is MIT licensed, as found in the LICENSE file. 114 | -------------------------------------------------------------------------------- /clidiff.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hhvm/difflib/35575b4d645c81840a42ba013ef759baf12f0789/clidiff.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "facebook/difflib", 3 | "license": "MIT", 4 | "extra": { 5 | "branch-alias": { 6 | "dev-master": "1.x-dev", 7 | "dev-main": "1.x-dev", 8 | "dev-CI_current_pull_request": "1.x-dev" 9 | } 10 | }, 11 | "require": { 12 | "hhvm": "^4.128" 13 | }, 14 | "require-dev": { 15 | "hhvm/hhast": "^4.0", 16 | "hhvm/hacktest": "^2.0", 17 | "facebook/fbexpect": "^2.1.2", 18 | "hhvm/hhvm-autoload": "^2.0|^3.0" 19 | }, 20 | "config": { 21 | "allow-plugins": { 22 | "hhvm/hhvm-autoload": true 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /hh_autoload.json: -------------------------------------------------------------------------------- 1 | { 2 | "roots": ["src/"], 3 | "devRoots": ["tests/"], 4 | "devFailureHandler": "Facebook\\AutoloadMap\\HHClientFallbackHandler", 5 | "useFactsIfAvailable": true 6 | } 7 | -------------------------------------------------------------------------------- /hhast-lint.json: -------------------------------------------------------------------------------- 1 | { 2 | "roots": [ "src/", "tests/" ], 3 | "builtinLinters": "all" 4 | } 5 | -------------------------------------------------------------------------------- /src/CLIColoredUnifiedDiff.php: -------------------------------------------------------------------------------- 1 | getUnifiedDiff()` 23 | * instead. 24 | */ 25 | final abstract class CLIColoredUnifiedDiff extends ColoredUnifiedDiff { 26 | const string RESET = "\e[0m"; 27 | const string BLACK = "\e[30m"; 28 | const string BLUE = "\e[36m"; 29 | const string RED = "\e[31m"; 30 | const string GREEN = "\e[32m"; 31 | 32 | /** Swap foreground and background colors */ 33 | const string INVERTED = "\e[7m"; 34 | 35 | const string HEADER_COLOR = self::BLUE; 36 | const string KEEP_COLOR = self::RESET; 37 | const string DELETE_COLOR = self::RED; 38 | const string INSERT_COLOR = self::GREEN; 39 | const string INTRALINE_DELETE_COLOR = self::INVERTED; 40 | const string INTRALINE_INSERT_COLOR = self::INVERTED; 41 | 42 | <<__Override>> 43 | protected static function colorHeaderLine(string $line): string { 44 | return self::HEADER_COLOR.$line.self::RESET; 45 | } 46 | 47 | <<__Override>> 48 | protected static function colorKeepLine(string $line): string { 49 | return self::KEEP_COLOR.$line.self::RESET; 50 | } 51 | 52 | <<__Override>> 53 | protected static function colorDeleteLine(string $line): string { 54 | return self::DELETE_COLOR.$line.self::RESET; 55 | } 56 | 57 | <<__Override>> 58 | protected static function colorInsertLine(string $line): string { 59 | return self::INSERT_COLOR.$line.self::RESET; 60 | } 61 | 62 | <<__Override>> 63 | protected static function join(vec $lines): string { 64 | return Str\join($lines, "\n")."\n"; 65 | } 66 | 67 | <<__Override>> 68 | protected static function colorDeleteLineWithIntralineEdits( 69 | vec> $ops, 70 | ): string { 71 | $line = self::DELETE_COLOR.'-'; 72 | foreach ($ops as $op) { 73 | if ($op->isKeepOp()) { 74 | $line .= $op->getContent(); 75 | continue; 76 | } 77 | if ($op->isDeleteOp()) { 78 | $line .= self::INTRALINE_DELETE_COLOR. 79 | $op->getContent(). 80 | self::RESET. 81 | self::DELETE_COLOR; 82 | continue; 83 | } 84 | } 85 | return $line.self::RESET; 86 | } 87 | 88 | <<__Override>> 89 | protected static function colorInsertLineWithIntralineEdits( 90 | vec> $ops, 91 | ): string { 92 | $line = self::INSERT_COLOR.'+'; 93 | foreach ($ops as $op) { 94 | if ($op->isKeepOp()) { 95 | $line .= $op->getContent(); 96 | continue; 97 | } 98 | if ($op->isInsertOp()) { 99 | $line .= self::INTRALINE_INSERT_COLOR. 100 | $op->getContent(). 101 | self::RESET. 102 | self::INSERT_COLOR; 103 | continue; 104 | } 105 | } 106 | return $line.self::RESET; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/ColoredUnifiedDiff.php: -------------------------------------------------------------------------------- 1 | { 22 | abstract protected static function colorHeaderLine(string $line): TOut; 23 | abstract protected static function colorKeepLine(string $line): TOut; 24 | abstract protected static function colorDeleteLine(string $line): TOut; 25 | abstract protected static function colorInsertLine(string $line): TOut; 26 | 27 | /** Render a deletion line from character keep/delete operations. 28 | * 29 | * If we have `- foo` `+ bar` where `foo` and `bar` are very similar, this 30 | * allows highlighting the changed characters in addition to/instead of the 31 | * whole line. 32 | * 33 | * Note that the input does not include the `- `, and you may want to re-add 34 | * it. 35 | * 36 | * The default implementation prepends `- ` then delegates to 37 | * `colorDeleteLine()` without doing any special handling. 38 | */ 39 | protected static function colorDeleteLineWithIntralineEdits( 40 | vec> $ops, 41 | ): TOut { 42 | return static::colorDeleteLine( 43 | '- '.(Vec\map($ops, $op ==> $op->getContent()) |> Str\join($$, '')), 44 | ); 45 | } 46 | 47 | /** Render an insertion line from character keep/insert operations. 48 | * 49 | * If we have `- foo` `+ bar` where `foo` and `bar` are very similar, this 50 | * allows highlighting the changed characters in addition to/instead of the 51 | * whole line. 52 | * 53 | * Note that the input does not include the `+ `, and you may want to re-add 54 | * it. 55 | * 56 | * The default implementation prepends `+ ` then delegates to 57 | * `colorInsertLine()` without doing any special handling. 58 | */ 59 | protected static function colorInsertLineWithIntralineEdits( 60 | vec> $ops, 61 | ): TOut { 62 | return static::colorInsertLine( 63 | '+ '.(Vec\map($ops, $op ==> $op->getContent()) |> Str\join($$, '')), 64 | ); 65 | } 66 | 67 | abstract protected static function join(vec $lines): TOut; 68 | 69 | final public static function create( 70 | string $a, 71 | string $b, 72 | int $context = 3, 73 | ): TOut { 74 | $diff = StringDiff::lines($a, $b)->getUnifiedDiff($context); 75 | 76 | $out = vec[]; 77 | $lines = Str\split($diff, "\n"); 78 | while (!C\is_empty($lines)) { 79 | $line = C\firstx($lines); 80 | $lines = Vec\drop($lines, 1); 81 | 82 | if ($line === '') { 83 | invariant(C\is_empty($lines), 'Blank line was not last line'); 84 | break; 85 | } 86 | if ($line[0] === '@') { 87 | $out[] = static::colorHeaderLine($line); 88 | continue; 89 | } 90 | if ($line[0] === ' ') { 91 | $out[] = static::colorKeepLine($line); 92 | continue; 93 | } 94 | if ($line[0] === '+') { 95 | $out[] = static::colorInsertLine($line); 96 | continue; 97 | } 98 | if ($line[0] === '-') { 99 | $next = C\first($lines); 100 | if ( 101 | $next !== null && $next !== '' && $next[0] === '+' 102 | // Levenshtein function throws for strings with length > 255 103 | // Don't need intra word diffs for long lines 104 | && Str\length($line) < 256 && Str\length($next) < 256 105 | // -2 to deal with the prefix 106 | /* HH_FIXME[4107] using directly because this is open source */ 107 | /* HH_FIXME[2049] using directly because this is open source */ 108 | && \levenshtein($line, $next) - 2 <= (0.5 * (Str\length($line) - 2)) 109 | ) { 110 | // Drop the prefix 111 | $line = Str\slice($line, 1); 112 | $next = Str\slice($next, 1); 113 | $lines = Vec\drop($lines, 1); 114 | 115 | $words_line = vec(\preg_split( 116 | '/([^a-zA-Z0-9_]+)/', 117 | $line, 118 | -1, 119 | \PREG_SPLIT_DELIM_CAPTURE, 120 | )); 121 | $words_next = vec(\preg_split( 122 | '/([^a-zA-Z0-9_]+)/', 123 | $next, 124 | -1, 125 | \PREG_SPLIT_DELIM_CAPTURE, 126 | )); 127 | $intraline = (new StringDiff($words_line, $words_next))->getDiff(); 128 | $out[] = $intraline 129 | |> Vec\filter($$, $op ==> !$op->isInsertOp()) 130 | |> static::colorDeleteLineWithIntralineEdits($$); 131 | $out[] = $intraline 132 | |> Vec\filter($$, $op ==> !$op->isDeleteOp()) 133 | |> static::colorInsertLineWithIntralineEdits($$); 134 | continue; 135 | } 136 | $out[] = static::colorDeleteLine($line); 137 | continue; 138 | } 139 | invariant_violation('unhandled line prefix: %s', $line[0]); 140 | } 141 | 142 | return static::join($out); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/Diff.php: -------------------------------------------------------------------------------- 1 | getDiff()` 26 | */ 27 | abstract class Diff { 28 | /** The type of individual elements in the sequence */ 29 | abstract const type TContent; 30 | /** An element in the sequence, and metadata */ 31 | const type TElem = shape('content' => this::TContent, 'pos' => int); 32 | /** Internal: ((from_x, from_y), (to_x, to_y) 33 | * 34 | * Diffs are represented as a 2D graph (see `getMoves()` for details)`; this 35 | * type represents a move within the graph. There are three valid moves: 36 | * - `(x+1, y)` is 'delete from a' 37 | * - `(x, y+1)` is 'insert to a from b' 38 | * - `(x+1, y+1)` is 'keep' 39 | */ 40 | const type TMove = ((int, int), (int, int)); 41 | 42 | private vec $a; 43 | private vec $b; 44 | 45 | public function __construct(vec $a, vec $b) { 46 | $this->a = 47 | Vec\map_with_key($a, ($k, $v) ==> shape('content' => $v, 'pos' => $k)); 48 | $this->b = 49 | Vec\map_with_key($b, ($k, $v) ==> shape('content' => $v, 'pos' => $k)); 50 | } 51 | 52 | /** The definition of identity for this differ. 53 | * 54 | * This defaults to `$a === $b`; subclasses may override this. 55 | */ 56 | protected function areSame(this::TContent $a, this::TContent $b): bool { 57 | return $a === $b; 58 | } 59 | 60 | /** Get the diff as a sequence of operations to transform from A to B. 61 | * 62 | * If you're dealing with strings, you might want 63 | * `StringDiff::getUnifiedDiff()` 64 | */ 65 | <<__Memoize>> 66 | final public function getDiff(): vec> { 67 | $a = $this->a; 68 | $b = $this->b; 69 | 70 | $moves = Vec\reverse($this->getMoves()); 71 | $diff = vec[]; 72 | 73 | $prev = tuple(0, 0); 74 | foreach ($moves as list($from, $to)) { 75 | invariant($from === $prev, 'Missed a step'); 76 | list($x, $y) = $from; 77 | $prev = $to; 78 | if ($to === tuple($x + 1, $y + 1)) { 79 | $diff[] = 80 | new DiffKeepOp($a[$x]['pos'], $b[$y]['pos'], $a[$x]['content']); 81 | continue; 82 | } 83 | 84 | if ($to === tuple($x + 1, $y)) { 85 | $diff[] = new DiffDeleteOp($a[$x]['pos'], $a[$x]['content']); 86 | continue; 87 | } 88 | 89 | if ($to === tuple($x, $y + 1)) { 90 | $diff[] = new DiffInsertOp($b[$y]['pos'], $b[$y]['content']); 91 | continue; 92 | } 93 | 94 | invariant_violation( 95 | 'invalid move: (%d, %d) => (%d, %d)', 96 | $from[0], 97 | $from[1], 98 | $to[0], 99 | $to[1], 100 | ); 101 | } 102 | return $diff; 103 | } 104 | 105 | /** Implementation of Myers' diff algorithm, as in the paper 106 | * 107 | * @See Myers (1986) An O(ND) difference algorithm and its variations. 108 | * Algorithmica, 1(1-4), p251-266. doi: 10.1007/BF01840446 109 | * 110 | * In short: 111 | * - think of the problem as an edit graph: the X axis is the original 112 | * sequence and the Y axis is the desired sequence 113 | * - increasing X is deleting an item (cost 1) 114 | * - increasing Y is inserting an item from the target sequence (cost 1) 115 | * - if the items are the same, increase both X and Y simultanously (cost 0) 116 | * - diffing is now graph traversal problem: find the path with the 117 | * lowest-cost cost path from (0, 0) to (len(a), len(b)) 118 | * 119 | * The paper first defines an edit distance function, then explains how to 120 | * convert this function into a diff algorithm; in summary: 121 | * 122 | * 1) create an edit distance function 123 | * 2) change it to log all what edits it makes 124 | * 3) when it's found the minimum edit distance, stop 125 | * 4) post-process the log into a set of moves 126 | * 127 | * The final step is handled by `backtrackPath()` 128 | */ 129 | private function getMoves(): vec { 130 | /* If you're comparing this to the paper: 131 | * `$max_x` is N 132 | * `$max_y` is M 133 | * `$max_cost` is MAX 134 | * `$cost` is D 135 | * `$diagonal` is k 136 | * `$best_points` is V 137 | * `$best_points_at_cost` is Vd 138 | * 139 | * 'cost' isn't quite the same as 'depth': taking a diagonal path through 140 | * the space is an increase in depth, but not cost. 141 | */ 142 | 143 | $a = $this->a; 144 | $b = $this->b; 145 | $max_x = C\count($a); 146 | $max_y = C\count($b); 147 | // Absolute worst case: delete everything from a + insert everything from b 148 | $max_cost = $max_x + $max_y; 149 | 150 | /* Find the shortest path. 151 | * 152 | * $x: offset into a: deletions 153 | * $y: offset into b: insertions 154 | * $diagonal: an identifier representing the line where $y = $x - $diagonal 155 | * 156 | * From the paper: 157 | * 1. For any path of cost $cost: -$cost <= $diagonal <= $cost 158 | * 2. The furthest-reaching 0-cost path ends at (x,x), where x = 159 | * min(x) where $a[x] !== $b[x] or $x >= $max_x or $x >= $max_y 160 | * 3. The furthest-reaching ($cost)-cost path is composed of either: 161 | * 1. 1. a furthest-reaching ($cost-1)-path on diagonal ($diagonal - 1) 162 | * 2. a horizontal edge (deletion) 163 | * 3. any number (including 0) of diagonal edges (keeps) 164 | * 2. 1. a furthest-reaching ($cost-1)-path on diagonal ($diagonal + 1) 165 | * 2. a vertical edge (insertion) 166 | * 3. any number (including 0) of diagonal edges (keeps) 167 | * 168 | * So, for each cost, find the furthest-reaching point on each diagonal. 169 | * Stop when we leave the search space, or reach the endpoint. 170 | */ 171 | 172 | // A map from $diagonal to $x. This is effectively a map from $diagonal to 173 | // points, as $y = $x - $diagonal. 174 | // 175 | // To handle $cost = 0, we take advantage of the fact that: 176 | // - we will only be looking at $diagonal === 0 177 | // - the implementation takes the ($diagonal + 1) path if $cost === 178 | // $diagonal 179 | // So, we set the best-$x for ($diagonal + 1) to 0, as that gets us 180 | // $y = $x + $diagonal = 0 + 0 = 0, so ($x = 0, $y = 0) 181 | $best_points = dict[1 => 0]; 182 | $best_points_at_cost = vec[]; 183 | 184 | for ($cost = 0; $cost <= $max_cost; $cost++) { 185 | $best_points_at_cost[] = $best_points; 186 | // Add 2 each time: any point with cost 0 is on diagonal 0; any point 187 | // with cost 1 is either on diagonal 1 or -1, any point with cost 2 188 | // is on diagonal -2, 0, or 2. This can be generalized to 3.x.1 above: 189 | // - Any even cost point is on an even diagonal 190 | // - Any odd cost point is on an odd diagonal 191 | // ... so if we have an even cost, skip the odd diagonals, and if we have 192 | // an odd cost, skip the even diagonals 193 | for ($diagonal = -$cost; $diagonal <= $cost; $diagonal += 2) { // Use 1. 194 | // Use 3: The furthest-reaching path on this diagonal is a continuation 195 | // of a ($cost-1) path on either ($diagonal-1) or ($diagonal+1). 196 | // 197 | // Decide which option we're taking for this diagonal 198 | if ( 199 | // if ===, can't be -1 as $diagonal >= -$cost 200 | $diagonal === -$cost || 201 | ( 202 | // if ===, must be -1 as $diagonal <= $cost 203 | $diagonal !== $cost 204 | // Okay, we can choose. 205 | // prefer to keep the ($cost-1) path with higher $x - i.e. prefer to 206 | // delete. Not needed for correctness or efficiency, but: 207 | // -foo 208 | // +foof 209 | // is generally expected, and considered more readable than: 210 | // +foof 211 | // -foo 212 | && 213 | $best_points[$diagonal - 1] < $best_points[$diagonal + 1] 214 | ) 215 | ) { 216 | // keep x, increase $y (add a insertion) 217 | $x = $best_points[$diagonal + 1]; 218 | } else { 219 | // increase x, keep y (add a deletion) 220 | $x = $best_points[$diagonal - 1] + 1; 221 | } 222 | $y = $x - $diagonal; 223 | 224 | // Use 2: Can we move along the diagonal (keep/unchanged elem)? 225 | while ( 226 | $x < $max_x && 227 | $y < $max_y && 228 | $this->areSame($a[$x]['content'], $b[$y]['content']) 229 | ) { 230 | $x++; 231 | $y++; 232 | } 233 | 234 | $best_points[$diagonal] = $x; 235 | 236 | if ($x >= $max_x && $y >= $max_y) { 237 | return $this->backtrackPath($best_points_at_cost); 238 | } 239 | } 240 | } 241 | invariant_violation( 242 | 'Shortest path is greater than the maximum possible length', 243 | ); 244 | } 245 | 246 | /** Backtracking algorithm in the paper. 247 | * 248 | * Returns a reversed list of moves. 249 | */ 250 | private function backtrackPath( 251 | vec> $best_points_at_cost, 252 | ): vec { 253 | /* Start at the final position, and backtrack to (0, 0). 254 | * 255 | * We know the final (x, y), and that the final cost is 256 | * C\count($best_points_at_cost). 257 | * 258 | */ 259 | $moves = vec[]; 260 | $to_x = C\count($this->a); 261 | $to_y = C\count($this->b); 262 | 263 | /* Work backwards, finding the (x, y) at ($cost - 1), that gets us to 264 | * our target at ($cost) */ 265 | foreach (Dict\reverse($best_points_at_cost) as $cost => $best_points) { 266 | $diagonal = $to_x - $to_y; 267 | 268 | /* Use point 3. again - it's either on ($diagonal -1) with ($x-1, $y) 269 | * or on ($diagonal + 1) with ($x, $y - 1), plus any number of 270 | * diagonal moves. Which? 271 | */ 272 | if ( 273 | $diagonal === -$cost || 274 | ( 275 | $diagonal !== $cost && 276 | $best_points[$diagonal - 1] < $best_points[$diagonal + 1] 277 | ) 278 | ) { 279 | $diagonal = $diagonal + 1; 280 | } else { 281 | $diagonal = $diagonal - 1; 282 | } 283 | $from_x = $best_points[$diagonal]; 284 | $from_y = $from_x - $diagonal; 285 | 286 | // We found the move that cost us - now try to fill in free diagonal 287 | // moves 288 | while ($to_x > $from_x && $to_y > $from_y) { 289 | $moves[] = tuple(tuple($to_x - 1, $to_y - 1), tuple($to_x, $to_y)); 290 | $to_x--; 291 | $to_y--; 292 | } 293 | 294 | if ($cost === 0) { 295 | // If cost is 0, there were only diagonals, which we just dealt with 296 | return $moves; 297 | } 298 | 299 | $moves[] = tuple(tuple($from_x, $from_y), tuple($to_x, $to_y)); 300 | 301 | $to_x = $from_x; 302 | $to_y = $from_y; 303 | } 304 | invariant_violation('unreachable'); 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /src/DiffDeleteOp.php: -------------------------------------------------------------------------------- 1 | extends DiffOp { 15 | public function __construct(private int $oldPos, private TContent $content) { 16 | } 17 | 18 | public function getOldPos(): int { 19 | return $this->oldPos; 20 | } 21 | 22 | <<__Override>> 23 | public function getContent(): TContent { 24 | return $this->content; 25 | } 26 | 27 | <<__Override>> 28 | public function isDeleteOp(): bool { 29 | return true; 30 | } 31 | 32 | <<__Override>> 33 | public function asDeleteOp(): DiffDeleteOp { 34 | return $this; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/DiffInsertOp.php: -------------------------------------------------------------------------------- 1 | extends DiffOp { 15 | public function __construct(private int $newPos, private TContent $content) { 16 | } 17 | 18 | public function getNewPos(): int { 19 | return $this->newPos; 20 | } 21 | 22 | <<__Override>> 23 | public function getContent(): TContent { 24 | return $this->content; 25 | } 26 | 27 | <<__Override>> 28 | public function isInsertOp(): bool { 29 | return true; 30 | } 31 | 32 | <<__Override>> 33 | public function asInsertOp(): DiffInsertOp { 34 | return $this; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/DiffKeepOp.php: -------------------------------------------------------------------------------- 1 | extends DiffOp { 15 | public function __construct( 16 | private int $oldPos, 17 | private int $newPos, 18 | private TContent $content, 19 | ) { 20 | } 21 | 22 | public function getOldPos(): int { 23 | return $this->oldPos; 24 | } 25 | 26 | public function getNewPos(): int { 27 | return $this->newPos; 28 | } 29 | 30 | <<__Override>> 31 | public function getContent(): TContent { 32 | return $this->content; 33 | } 34 | 35 | <<__Override>> 36 | public function isKeepOp(): bool { 37 | return true; 38 | } 39 | 40 | <<__Override>> 41 | public function asKeepOp(): DiffKeepOp { 42 | return $this; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/DiffOp.php: -------------------------------------------------------------------------------- 1 | > 18 | abstract class DiffOp { 19 | abstract public function getContent(): TContent; 20 | 21 | public function isDeleteOp(): bool { 22 | return false; 23 | } 24 | 25 | public function isInsertOp(): bool { 26 | return false; 27 | } 28 | 29 | public function isKeepOp(): bool { 30 | return false; 31 | } 32 | 33 | public function asDeleteOp(): DiffDeleteOp { 34 | invariant_violation('not a deletion'); 35 | } 36 | 37 | public function asInsertOp(): DiffInsertOp { 38 | invariant_violation('not an insertion'); 39 | } 40 | 41 | public function asKeepOp(): DiffKeepOp { 42 | invariant_violation('not a keep'); 43 | } 44 | 45 | final public function getDiffOpClass(): classname> { 46 | return static::class; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/StringDiff.php: -------------------------------------------------------------------------------- 1 | >` 21 | * @see `getUnifiedDiff()` to get a diff suitable for `patch` 22 | * @see `CLIColoredUnifiedDiff` if you want to provide a human-readable diff on 23 | * a terminal. 24 | */ 25 | final class StringDiff extends Diff { 26 | const type TContent = string; 27 | 28 | public static function lines(string $a, string $b): this { 29 | return new self(Str\split($a, "\n"), Str\split($b, "\n")); 30 | } 31 | 32 | public static function characters(string $a, string $b): this { 33 | return new self(Str\split($a, ''), Str\split($b, '')); 34 | } 35 | 36 | public static function commaSeparatedValues(string $a, string $b): this { 37 | return new self(Str\split($a, ','), Str\split($b, ',')); 38 | } 39 | 40 | public function getHunks(int $context): vec>> { 41 | $hunks = vec[]; 42 | 43 | $remaining = $this->getDiff(); 44 | $last = C\lastx($remaining); 45 | // diff -u ignores trailing newlines 46 | if ($last->isKeepOp() && $last->getContent() === '') { 47 | $remaining = Vec\slice($remaining, 0, C\count($remaining) - 1); 48 | } 49 | 50 | while (!C\is_empty($remaining)) { 51 | $not_keep = C\find_key($remaining, $row ==> !$row->isKeepOp()); 52 | if ($not_keep === null) { 53 | break; 54 | } 55 | $start = ($not_keep > $context) ? ($not_keep - $context) : 0; 56 | 57 | $remaining = Vec\drop($remaining, $start); 58 | $count = C\count($remaining); 59 | 60 | $end = $count; 61 | $run_start = null; 62 | for ($i = $context; $i < $count; ++$i) { 63 | if ($remaining[$i]->isKeepOp()) { 64 | $run_start ??= $i; 65 | continue; 66 | } 67 | 68 | if ($run_start === null) { 69 | continue; 70 | } 71 | 72 | if ($i >= $run_start + (2 * $context)) { 73 | $end = $run_start + $context; 74 | break; 75 | } 76 | } 77 | if ($run_start !== null) { 78 | $end = $run_start + $context; 79 | } 80 | $hunks[] = Vec\take($remaining, $end); 81 | $remaining = Vec\drop($remaining, $end); 82 | } 83 | return $hunks; 84 | } 85 | 86 | public function getUnifiedDiff(int $context = 3): string { 87 | $hunks = $this->getHunks($context); 88 | return Vec\map($hunks, $hunk ==> $this->getUnifiedDiffHunk($hunk)) 89 | |> Vec\filter_nulls($$) 90 | |> Str\join($$, ''); 91 | } 92 | 93 | private function getUnifiedDiffHunk(vec> $hunk): ?string { 94 | if (C\is_empty($hunk)) { 95 | return null; 96 | } 97 | $old_start = null; 98 | $new_start = null; 99 | $old_lines = 0; 100 | $new_lines = 0; 101 | 102 | $lines = vec[]; 103 | 104 | foreach ($hunk as $op) { 105 | if ($op->isKeepOp()) { 106 | $op = $op->asKeepOp(); 107 | $lines[] = ' '.$op->getContent(); 108 | $old_start ??= $op->getOldPos(); 109 | $new_start ??= $op->getNewPos(); 110 | ++$old_lines; 111 | ++$new_lines; 112 | continue; 113 | } 114 | 115 | if ($op->isDeleteOp()) { 116 | $op = $op->asDeleteOp(); 117 | $lines[] = '-'.$op->getContent(); 118 | $old_start ??= $op->getOldPos(); 119 | $new_start ??= $op->getOldPos(); 120 | ++$old_lines; 121 | continue; 122 | } 123 | 124 | if ($op->isInsertOp()) { 125 | $op = $op->asInsertOp(); 126 | $lines[] = '+'.$op->getContent(); 127 | $old_start ??= $op->getNewPos(); 128 | $new_start ??= $op->getNewPos(); 129 | ++$new_lines; 130 | continue; 131 | } 132 | 133 | invariant_violation('Unsupported diff op: %s', \get_class($op)); 134 | } 135 | invariant($old_start !== null, 'failed to find an old pos'); 136 | invariant($new_start !== null, 'failed to find a new pos'); 137 | 138 | $format = (int $start, int $lines) ==> ($start === 1 && $lines === 1) 139 | ? '1' 140 | : Str\format('%d,%d', $start, $lines); 141 | 142 | return Str\format( 143 | "@@ -%s +%s @@\n", 144 | $format($old_start + 1, $old_lines), 145 | $format($new_start + 1, $new_lines), 146 | ). 147 | Str\join($lines, "\n"). 148 | "\n"; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/cluster.php: -------------------------------------------------------------------------------- 1 | (vec> $diff): vec>> { 21 | $clusters = vec[]; 22 | while (!C\is_empty($diff)) { 23 | $class = $diff[0]->getDiffOpClass(); 24 | 25 | $next = C\find_key($diff, $op ==> $op->getDiffOpClass() !== $class); 26 | if ($next === null) { 27 | $clusters[] = $diff; 28 | break; 29 | } 30 | 31 | $clusters[] = Vec\take($diff, $next); 32 | $diff = Vec\drop($diff, $next); 33 | } 34 | 35 | return Vec\map( 36 | $clusters, 37 | $cluster ==> { 38 | $first = C\firstx($cluster); 39 | 40 | if ($first->isDeleteOp()) { 41 | return new DiffDeleteOp( 42 | $first->asDeleteOp()->getOldPos(), 43 | Vec\map($cluster, $op ==> $op->asDeleteOp()->getContent()), 44 | ); 45 | } 46 | 47 | if ($first->isInsertOp()) { 48 | return new DiffInsertOp( 49 | $first->asInsertOp()->getNewPos(), 50 | Vec\map($cluster, $op ==> $op->asInsertOp()->getContent()), 51 | ); 52 | } 53 | 54 | if ($first->isKeepOp()) { 55 | $first = $first->asKeepOp(); 56 | return new DiffKeepOp( 57 | $first->getOldPos(), 58 | $first->getNewPos(), 59 | Vec\map($cluster, $op ==> $op->asKeepOp()->getContent()), 60 | ); 61 | } 62 | 63 | invariant_violation('invalid op kind: %s', \get_class($first)); 64 | }, 65 | ); 66 | } 67 | -------------------------------------------------------------------------------- /udiff.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hhvm/difflib/35575b4d645c81840a42ba013ef759baf12f0789/udiff.png --------------------------------------------------------------------------------