Skip to main content

Wrappers for git diff

Project description

PyDiffChecker

Wrappers for git diff

LineShiftChecker

This tool provides an API for checking shifted but not modified lines in changed files of a Git diff. The tool will collect the lines where the content did not change into dict-like objects with source->destination line number mapping per changed file. File renames are detected as well.

The behavior is very similar to unified diff on Github or output of git-diffn. There are green (added), red (remove) and white (unmodified) lines. The return value of the API will contain these "white" lines with the source/destination line number for the whole file.

Example

Let's assume this is the output of git-diffn:

diff --git a/pydiffchecker/helper.py b/pydiffchecker/helper.py
index f9d4e08..6a6c214 100644
--- a/pydiffchecker/helper.py
+++ b/pydiffchecker/helper.py
@@ -3,14 +3,13 @@ from typing import Iterator
    3,   3:
    4,   4:
    5,   5: def subprocess_readlines(cmd, cwd=None) -> Iterator[str]:
-   6     :-    process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE)
+        6:+    process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, text=True)
    7,   7:
    8,   8:     for line in process.stdout:
-   9     :-        line = line.decode().rstrip(r'\n')
-  10     :-
+        9:+        line = line.rstrip('\n')
   11,  10:         yield line
   12,  11:
-  13     :-    process.wait()
+       12:+    process.communicate()
   14,  13:
   15,  14:     if process.returncode != 0:
   16,  15:         raise subprocess.CalledProcessError(process.returncode, cmd)

Here the lines 3-5, 7-8, 11-12 and 14-16 are unmodified, but some of them are shifted. Of course, "white" lines that are not shown above are also unmodified. The API fill return the following data (visualized for simplicity, see Usage how to access the data programatically):

* pydiffchecker/helper.py->pydiffchecker/helper.py:
    1->1
    2->2
    3->3
    4->4
    5->5
    6->None
    7->7
    8->8
    9->None
    10->None
    11->10
    12->11
    13->None
    14->13
    15->14
    16->15

Usage

from pydiffchecker.line_shift_checker import LineShiftChecker

line_shift_checker = LineShiftChecker('HEAD~', 'HEAD')
all_shifted_lines = line_shift_checker.get_all_shifted_lines()

# Iterate through shifted lines
for src_path, shifted_lines in all_shifted_lines.items():
    renamed_to = shifted_lines.dst_path
    for src_line_index, dst_line_index in shifted_lines:
        print(f'{src_line_index}->{dst_line_index}')

# Find if lines are shifted (and to where) in a file
if 'your/file.cpp' in all_shifted_lines:
    shifted_lines = all_shifted_lines['your/file.cpp']
    renamed_to = shifted_lines.dst_path
    if 12 in shifted_lines:
        # line 12 was shifted, to where?
        shifted_to = shifted_lines[12]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydiffchecker-0.1.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydiffchecker-0.1.1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file pydiffchecker-0.1.1.tar.gz.

File metadata

  • Download URL: pydiffchecker-0.1.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pydiffchecker-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b3a3a4aa10aa4aa9bc4b67ae65943f95f8fae7f48fdc7154b2da73a90512e7a4
MD5 21df5e95750899cee92a08eb74b9d3ee
BLAKE2b-256 c376fbe0339867425b0d7d9461ac9b8cb4072ed3256225af6de469bdb50b4a7a

See more details on using hashes here.

File details

Details for the file pydiffchecker-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pydiffchecker-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pydiffchecker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17df0ddbe8cff9055e4478a01271d52130b1a0dfd84382fb7c6337e2dc31ab2d
MD5 8e3ed9965c0c7e705312794fa877f87a
BLAKE2b-256 c73df49ab3a9aba350e4f4dad4112f6163a9a211278605a3640dadae081aa38d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page