Skip to content

feat: Enhance Git Patch Parsing and Refactor Core Logic #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

zenyr
Copy link

@zenyr zenyr commented May 17, 2025

Hi!

I've been working on improving how we handle Git patches. As we've started looking at commits in more detail, we realized the old code could be more flexible and easier to work with.

This PR refactors some key parts to make the code cleaner, safer (with better type checking!), and generally more solid. The goal is to make it simpler for all of us to maintain and build upon in the future. Also all the test cases are still valid.

Let me know what you think!


This pull request enhances the Git patch parsing capabilities, introduces structured diff representation, improves type safety, and refactors the core parsing logic for better modularity and maintainability.

Detailed Changes

Added

  • Enhanced Patch Parsing Logic with Flexible Configuration (src/logics/parse/patch.ts, src/types.ts)

    • Introduced ParseOptions with parseDates (to convert date strings to Date objects) and structuredDiff (to convert diff strings into a structured object array - FileChange[]). This allows for more flexible processing of patch data.
    • The ParsedCommit type now dynamically adjusts the type of date and diff fields based on these options, utilizing a SelectIfTrue utility type for improved type safety.
    • The main parseGitPatch function now accepts these options to tailor the parsing output.
  • Git Diff String Structurization (src/logics/parse/diffToStructured.ts, src/types.ts)

    • Implemented parseDiffToStructured function to transform raw Git diff strings into an array of FileChange objects.
    • Each FileChange object details oldPath, newPath, additions, deletions, and an array of hunks.
    • Each DiffHunk contains a header string and an array of DiffLine objects, where each line is typed as addition, deletion, or context.

Changed

  • Improved Type Safety and Modularity in Parsing Logic (src/consts.ts, src/types.ts, src/logics/parse/patch.ts)

    • Centralized parsing-related constants (e.g., HEADERS, REGEX) into src/consts.ts for better organization.
    • Enhanced type definitions for ParseOptions and ParsedCommit using generics in src/types.ts to ensure stricter type checking.
    • Updated the parseGitPatch function to leverage these improved and more robust type definitions.
  • Cross-Platform Build Clean Script (scripts/cleanDist.ts)

    • Replaced the rm -rf dist shell command in the build process with a new Node.js-based TypeScript script, scripts/cleanDist.ts.
    • This script utilizes node:fs/promises's rm function for deleting the dist directory, ensuring cross-platform compatibility for the clean-up task.
  • Enhanced Readability and Organization of parseGitPatch Tests (src/mocks/patch.ts, src/logics/parse/patch.test.ts, tsconfig.json)

    • Relocated mock patch data used in parseGitPatch tests to a dedicated file: src/mocks/patch.ts.
    • Updated tsconfig.json to correctly include paths for these mock files, facilitating better test organization (Note: CHANGELOG.md mentions tsconfig.json update for include paths, but test files are typically run via a test runner like bun:test which might handle module resolution differently. The primary include in tsconfig.json points to src/index.ts).
  • Refactored Core Parsing Logic File Structure (src/logics/parse/patch.ts, src/logics/parse/patch.test.ts, src/index.ts, src/logics/index.ts, src/types.ts)

    • Reorganized the core parsing logic by moving relevant files (src/logics/parse/patch.ts and its test file src/logics/parse/patch.test.ts) into the src/logics/parse/ directory.
    • Introduced new index files (src/index.ts and src/logics/index.ts) for streamlined re-exporting of modules.
    • Consolidated primary type definitions into a new src/types.ts file.

Style

  • Adjusted Import Order in Tests (src/logics/parse/patch.test.ts)
    • Standardized the import order within the src/logics/parse/patch.test.ts file for consistency.

zenyr added 8 commits May 17, 2025 17:08
Moved core parsing logic files (index.ts and index.test.ts) to the src/logics/parse/ directory.
Created new index files in src/ and src/logics/ directories for re-exporting.
Added src/types.ts file for type definitions.
Move mock data to a separate `src/mocks/patch.ts` file to improve readability and organization of the `parseGitPatch` test.
This change also includes updating the include paths in `tsconfig.json` for bundling.
Implements a function to parse Git diff strings into a structured array of objects, including file changes, each hunk, and line types (additions, deletions, context).

Includes a comprehensive test suite that covers various diff scenarios, such as single file changes, multiple file changes, file additions/deletions/renames, multiple hunks, and indicators for lines without trailing newlines.
Refactors the git patch parsing logic to improve type safety and code organization. Key improvements include:

- Centralizing parsing-related constants.
- Introducing generic types for ParseOptions and ParsedCommit to allow dynamic type inference based on options, leading to more precise type checking.
- Updating the parseGitPatch function to leverage these enhanced types for better accuracy.
Enhanced the patch parsing logic to provide more flexible configuration options. This includes adjustments to how dates and diff structures are processed based on user-defined settings.

Corresponding test suites have been expanded to cover these new option variations, ensuring robustness. Type definitions related to parsed commit data have also been updated to accurately represent the structured diff output when that option is enabled.
…-rf with a Node.js script

This commit replaces the `rm -rf dist` command in the build script
with a cross-platform Node.js script (`scripts/cleanDist.ts`).
This enhances compatibility, particularly for Windows environments
where `rm -rf` is not natively available.
The new script uses `node:fs/promises` to delete the 'dist' directory.
This commit updates the import order in the `patch.test.ts` file.
This change is likely due to automated linting or code style adjustments and does not affect the logic of the tests.
- Updated CHANGELOG.md with recent changes.
@zenyr zenyr force-pushed the refactor/relocate-modules branch from 4e6f9d9 to 6239454 Compare May 17, 2025 10:36
Updates the `parseGitPatch` function signature in the README.md file.

- Adds an `options` parameter to selectively enable date parsing and structured diff return.
- Changes the `date` return type to `string | Date`.
- Changes the `diff` return type to `string | FileChange[]`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant