feat: Enhance Git Patch Parsing and Refactor Core Logic #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)
ParseOptions
withparseDates
(to convert date strings toDate
objects) andstructuredDiff
(to convert diff strings into a structured object array -FileChange[]
). This allows for more flexible processing of patch data.ParsedCommit
type now dynamically adjusts the type ofdate
anddiff
fields based on these options, utilizing aSelectIfTrue
utility type for improved type safety.parseGitPatch
function now accepts these options to tailor the parsing output.Git Diff String Structurization (src/logics/parse/diffToStructured.ts, src/types.ts)
parseDiffToStructured
function to transform raw Git diff strings into an array ofFileChange
objects.FileChange
object detailsoldPath
,newPath
,additions
,deletions
, and an array ofhunks
.DiffHunk
contains aheader
string and an array ofDiffLine
objects, where each line is typed asaddition
,deletion
, orcontext
.Changed
Improved Type Safety and Modularity in Parsing Logic (src/consts.ts, src/types.ts, src/logics/parse/patch.ts)
HEADERS
,REGEX
) intosrc/consts.ts
for better organization.ParseOptions
andParsedCommit
using generics insrc/types.ts
to ensure stricter type checking.parseGitPatch
function to leverage these improved and more robust type definitions.Cross-Platform Build Clean Script (scripts/cleanDist.ts)
rm -rf dist
shell command in the build process with a new Node.js-based TypeScript script,scripts/cleanDist.ts
.node:fs/promises
'srm
function for deleting thedist
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)parseGitPatch
tests to a dedicated file:src/mocks/patch.ts
.tsconfig.json
to correctly include paths for these mock files, facilitating better test organization (Note:CHANGELOG.md
mentionstsconfig.json
update for include paths, but test files are typically run via a test runner likebun:test
which might handle module resolution differently. The primaryinclude
intsconfig.json
points tosrc/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)
src/logics/parse/patch.ts
and its test filesrc/logics/parse/patch.test.ts
) into thesrc/logics/parse/
directory.src/index.ts
andsrc/logics/index.ts
) for streamlined re-exporting of modules.src/types.ts
file.Style
src/logics/parse/patch.test.ts
file for consistency.