fix/prdescribe-patch #8
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "fix/prdescribe-patch"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fix
git diffhandling inpr-describecommand and add mockable entry point for testsThis PR addresses an issue in the
pr-describecommand wheregit diffcalls were not properly handling non-zero exit codes (specifically exit code 1, which is normal when differences exist). It introduces a newgit.Difffunction to correctly capture diff output, makes it mockable for unit tests, and includes minor improvements for usability and robustness.Changes
internal/git/git.go:
Difffunction that runsgit diffusingexec.Command.CombinedOutput()and explicitly tolerates exit code 1 (indicating changes were found). This prevents treating legitimate diffs as errors.cmd/prdescribe.go:
gitDiffvariable assigned togit.Diff, similar to the existinggitRunpattern, to facilitate unit testing.--baseflag description to clarify the default value ("master").gitRuncalls withgitDifffor fetching diffs, preferring the local base branch first and falling back toorigin/<base>.strings.TrimSpaceto more accurately check for empty diffs (ignoring whitespace).cmd/run_test.go:
withMockGitDiffhelper function to inject mocks forgitDiff, mirroring the existingwithMockGitpattern.TestRunPRDescribeto usewithMockGitDiffinstead ofwithMockGit, ensuring they test the new diff logic.Motivation
The original implementation used
gitRunforgit diff, which relies oncmd.Output()and treats any non-zero exit code as a failure. However,git diffintentionally exits with code 1 when changes are present, leading to incorrect error handling and potential failures in generating PR descriptions. This PR introduces a dedicatedDifffunction to handle this Git-specific behavior correctly, improving reliability.Additionally, making
gitDiffmockable aligns with the project's testing patterns (e.g.,gitRun), allowing for cleaner and more isolated unit tests without affecting other Git operations. Minor tweaks like trimming whitespace and clarifying output messages enhance user experience and prevent edge cases (e.g., diffs with only whitespace being treated as non-empty).No breaking changes are introduced—this is fully backward-compatible.
Testing Notes
TestRunPRDescribeincmd/run_test.go. The tests now mockgitDiffto simulate various scenarios:grokkit pr-describeon branches with/without changes against "master" and custom bases.origin/<base>works when the local base is absent.client.Stream) is only invoked when a valid diff is present.go test ./...with no failures.If approved, this should be merged to the main branch. Let me know if additional tests or adjustments are needed!