Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Lint clean, has boilerplate and license information, and has diff
creation code from HHAST
  • Loading branch information
fredemmott committed Sep 5, 2018
0 parents commit 6c31326
Show file tree
Hide file tree
Showing 22 changed files with 1,333 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .LICENSE_HEADER.hh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
*.swp
*.swo
.DS_Store
1 change: 1 addition & 0 deletions .hhconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assume_php=false
10 changes: 10 additions & 0 deletions .travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
set -ex
hhvm --version

composer install --ignore-platform-reqs

hh_client

#hhvm vendor/bin/phpunit
hhvm vendor/bin/hhast-lint
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: php
php:
- hhvm
- hhvm-nightly
matrix:
allow_failures:
- php: hhvm-nightly
script:
- ./.travis.sh
5 changes: 5 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Code of Conduct

Facebook has adopted a Code of Conduct that we expect project participants to adhere to.
Please read the [full text](https://code.fb.com/codeofconduct/)
so that you can understand what actions will and will not be tolerated.
46 changes: 46 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Contributing to DiffLib

We want to make contributing to this project as easy and transparent as
possible.

## Code of Conduct
The code of conduct is described in [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md).

## Our Development Process

We develop on master, and make releases on an as-needed basic - usually
to support new HHVM releases.

## Pull Requests
We actively welcome your pull requests.

1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. If you haven't already, complete the Contributor License Agreement ("CLA").

## Contributor License Agreement ("CLA")
In order to accept your pull request, we need you to submit a CLA. You only need
to do this once to work on any of Facebook's open source projects.

Complete your CLA here: <https://code.facebook.com/cla>

## Issues
We use GitHub issues to track public bugs. Please ensure your description is
clear and has sufficient instructions to be able to reproduce the issue.

Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe
disclosure of security bugs. In those cases, please go through the process
outlined on that page and do not file a public issue.

## Coding Style
* 2 spaces for indentation rather than tabs
* 80 character line length
* Match hackfmt output
* Pass the configured HHAST linters

## License
By contributing to DiffLib, you agree that your contributions will be licensed
under the LICENSE file in the root directory of this source tree.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017-present, Facebook, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# DiffLib

DiffLib is a Hack library for creating and parsing diffs. Diffs can be created
between any two sequences of items. Additional helpers (such as support for
unified diffs, and colored diffs) are provided for string diffs.

Diffs are represented as a sequence of `DiffOp` operations; these can 'keep' an
element (i.e. it's unchanged in both sequences), insert an element, or delete an
element. For example, in a unified diff, these would be:

```diff
DiffKeepOp
- DiffDeleteOp
+ DiffInsertOp
```

String diffs are typically represented as a sequence of lines, but can also be
represented as a sequence of characters, allowing intra-line diffs.

## Examples

```Hack
use namespace Facebook\DiffLib;
function create_unified_diff(string $from, string $to): string {
return DiffLib\StringDiff::lines($from, $to)->getUnifiedDiff();
}
function create_colored_diff(string $from, string $to): string {
return DiffLib\CLIColoredUnifiedDiff::create($from, $to);
}
final class IntDiff extends DiffLib\Diff {
const type TContent = int;
}
function dump_int_diff(vec<int> $from, vec<int> $to): void {
$diff = (new IntDiff($from, $to))->getDiff();
foreach ($diff as $op) {
if ($op is DiffLib\DiffKeepOp<_>) {
\printf(" %d\n", $op->getContent());
} else if ($op is DiffLib\DiffDeleteOp<_>) {
\printf("- %d\n", $op->getContent());
} else {
$op = $op as DiffLib\DiffInsertOp<_>;
printf("+ %d\n", $op->getContent());
}
}
}
```

`dump_int_diff(vec[1, 3, 9], vec[1, 4, 9])` will produce this output:

```
1
- 3
+ 4
9
```

## Requirements

* The current release version of HHVM

## Installing DiffLib

```
composer require facebook/difflib
```

## How DiffLib works

Diffs are created using Myers' diff algorithm:
Myers, E.W. Algorithmica (1986) 1: 251. https://doi.org/10.1007/BF01840446

For more details, see [the commented implementation](src/Diff.php).

## Full documentation

- `DiffLib\Diff`: abstract base class. Needs to be subclassed to operate on
any particular type.
- `DiffLib\StringDiff`: final class for diffing strings, and adds support
for creating unified diffs
- `DiffLib\ColoredUnifiedDiff`: abstract class for rendering unified diffs.
Output may be any type - for example, strings or XHP.
- `DiffLib\CLIColoredUnifiedDiff`: abstract final class for rendering unified
diffs to a terminal that supports color escape sequences. Provides intra-line
highlighting.
- `DiffLib\DiffOp`: abstract class for a diff operation. Sealed to
`DiffInsertOp`, `DiffKeepOp`, and `DiffDeleteOp`.
- `DiffLib\cluster()`: utility function to group together sequential operations
of the same kind. Converts `vec<DiffOp<T>>` to a generally shorter number of
`vec<DiffOp<vec<T>>>`

## License
DiffLib is MIT licensed, as found in the LICENSE file.
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "facebook/difflib",
"license": "MIT",
"require": {
"hhvm/hsl": "^3.28"
},
"require-dev": {
"hhvm/hhast": "^3.28"
}
}
Loading

0 comments on commit 6c31326

Please sign in to comment.