Code Diff
Fast AST based code differencing in Python
Software projects are constantly evolving to integrate new features or improve existing implementations. To keep track of this progress, it becomes important to track individual code changes. Code differencing provides a way to identify the smallest code change between two implementations.
code.diff provides a fast alternative to standard code differencing techniques with a focus on AST based code differencing. As part of this library, we include a fast reimplementation of the GumTree algorithm. However, by relying on a best-effort AST parser, we are able to generate AST code changes for individual code snippets. Many programming languages including Python, Java and JavaScript are supported!
Installation
The package is tested under Python 3. It can be installed via:
pip install code-diff
Usage
code.diff can compute a code difference for nearly any program code in a few lines of code:
import code_diff as cd
# Python
output = cd.difference(
'''
def my_func():
print("Hello World")
''',
'''
def say_helloworld():
print("Hello World")
''',
lang = "python")
# Output: my_func -> say_helloworld
output.edit_script()
# Output:
# [
# Update((identifier:my_func, line 1:12 - 1:19), say_helloworld)
#]
# Java
output = cd.difference(
'''
int x = x + 1;
''',
'''
int x = x / 2;
''',
lang = "java")
# Output: x + 1 -> x / 2
output.edit_script()
# Output: [
# Insert(/:/, (binary_operator, line 0:4 - 0:9), 1),
# Update((integer:1, line 0:8 - 0:9), 2),
# Delete((+:+, line 0:6 - 0:7))
#]
Language support
code.diff supports most programming languages where an AST can be computed. To parse an AST, the underlying parser employs
-
code.tokenize: A frontend for tree-sitter to effectively parse and tokenize program code in Python.
-
tree-sitter: A best-effort AST parser supporting many programming languages including Python, Java and JavaScript.
To decide whether your code can be handled by code.diff please review the libraries above.
GumTree: To compute an edit script between a source and target AST, we employ a Python reimplementation of the GumTree algorithm. Note however that the computed script are heavily dependent on the AST representation of the given code. Therefore, AST edit script computed with code.diff might significantly differ to the one computed by GumTree.
Release history
- 0.1.2
- Fix of the release information
- Fix bug in 0.1.1 release
- Package now useable by installing from PyPI
- 0.1.0
- Initial functionality
- Documentation
- SStuB Testing
Project Info
The goal of this project is to provide developer with easy access to AST-based code differencing. This is currently developed as a helper library for internal research projects. Therefore, it will only be updated as needed.
Feel free to open an issue if anything unexpected happens.
Cedric Richter - @cedricrichter - cedric.richter@uni-oldenburg.de
Distributed under the MIT license. See LICENSE for more information.
Metadata
Release files for code-diff 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| code_diff-0.1.3.tar.gz | 25.2 kB | Without provenance | Details for code_diff-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| code_diff-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for code_diff-0.1.3-py3-none-any.whl |
Total release size: 48.4 kB
Release files / code_diff-0.1.3.tar.gz
| Download URL | code_diff-0.1.3.tar.gz |
|---|---|
| Size | 25.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
02235e5419e1db87392bf742c33e1c3c65893d81cb61035cc9d7743e50809255
|
|
BLAKE2b-256 checksum How to use checksums |
55888ce5dc2ea13d63f989dd365984a8b4ecaf28ab328f63fe860d0848d455ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.10.14
|
Release files / code_diff-0.1.3-py3-none-any.whl
| Download URL | code_diff-0.1.3-py3-none-any.whl |
|---|---|
| Size | 23.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
25a470e3cc591ae92dcec5a084b93af4145f54fd6d7379d5c0bf5e8800b4334a
|
|
BLAKE2b-256 checksum How to use checksums |
99f63e4f598f6570f461e519b941333cce5db2db71635e00db1083e6aba32613
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.10.14
|
Security for code-diff 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.