-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
refactor: fix #722 properly #1250
Conversation
@L11r what's the point of copying? Can't we just pass |
@zricethezav It's not ideal, but much better than current implementation in my opinion. |
Probably another option is to introduce some client := git.NewClient(opts...)
diffs := client.Log()
defer client.Close()
go func() {
for diff := range diffs { /* handle findings */ }
}()
if client.Err() != nil {} |
@zricethezav have pushed second refactor. It should look much better now, I didn't use copy this time. |
I will send PR with tests for |
@zricethezav any comments? I know you are probably busy, but still think it's good to fix this issue properly. |
@L11r these changes look good to me. I'm just running some memory profiles to make sure nothing is exploding |
@zricethezav sure! |
solid PR, thanks for the contribution @L11r |
Description:
This PR fixes #722 issue properly.
Currently
git
package is poorly written in my humble opinion. Probably this is whygit_test.go
commented out entirely, it just hard to test because of it.Main problem:
*exec.Cmd
requires you to runStart()
and thenWait()
to release resources. But bothgit.GitDiff()
andgit.GitLog()
return chan with git diff files, later consumer handles chan elements and parser (!) callsWait()
and closes stdout pipe. This is why you can't just defercmd.Wait()
in these functions, because otherwise you will get blocked, consumer don't consume diff files at this moment yet.In order to fix this we need to merge this: gitleaks/go-gitdiff#6
We shouldn't get parser know about our implementation, it should just read from somethings, that's it.
Then we can use
bytes.Buffer
to copy stdout and defer bothcmd.Wait()
andstdout.Close()
ingit
package directly. Also for some reason currently we don't even closestderr
, this PR also fixes it.To properly handle deferred
stderrErr
I use named return variables.I understand that it slightly increases memory footprint, but I am sure it's worth it.
Checklist: