Use a custom diff view for binary files#737
Conversation
|
Nice work @Toilal :)
I wonder if it would make more sense to inverse the table, so |
package.json
Outdated
There was a problem hiding this comment.
We already depend on isbinaryfile, maybe reuse that?
There was a problem hiding this comment.
Ho yes, of course, i didn't see there was already this dependency.
There was a problem hiding this comment.
Problem is that isbinaryfile needs a file as input, and we only have a buffer in conflicter.js ... This could be refactored to have a file, but i'm not sure it make sense ...
There was a problem hiding this comment.
@Toilal Yeah, I guess it doesn't matter much dep wise. My only concern is that this one requires us to read in the whole file and then detect whether it's binary. isbinaryfile only read the required bytes and should be much faster.
There was a problem hiding this comment.
I would agre but file is fully read just after using isbinaryfile.
There was a problem hiding this comment.
@Toilal I don't know how the check works with that module. Maybe it needs to read until it hits some kind of needle. I haven't read their code.
There was a problem hiding this comment.
Currently, isbinaryfile is loads 512 first bytes in a buffer, and then make the check with this buffer. istextorbinary has a buffer as input, so it's up to the user to load the buffer, and it then reads the buffer until it reach a binary byte.
So it seems that using only 512 bytes of data could be enough, and would make isbinaryfile and istextorbinary quite equivalent ...
There was a problem hiding this comment.
As the conflicter handles buffers (he doesn't know of the actual template file), I'd drop isbinaryfileand only use istextorbinary.
6ed97bb to
0cd1d60
Compare
|
I've reverse the table ordering. I've kept Currently, var binary = isBinaryFile(source);
if (!binary) {
encoding = 'utf8';
}
body = fs.readFileSync(source, encoding);Could be replaced using body = fs.readFileSync(source);
if (istextorbinary.isTextSync(body)) {
body = body.toString();
}It would drop one dependency, and avoid the file to be read 2 times. I'll make another PR for this one. |
|
Another question : Should i use the extension feature of It can use the filename extension to check if it's a binary file, and it will then check the actual content of buffer if extension is unknown only. |
0cd1d60 to
9958860
Compare
9958860 to
e4b09fd
Compare
lib/util/conflicter.js
Outdated
There was a problem hiding this comment.
Maybe you could merge and release this one then : sindresorhus/read-chunk#2
e4b09fd to
82177ce
Compare
|
Should be OK now, using read-chunk 1.0.1. |
lib/util/conflicter.js
Outdated
There was a problem hiding this comment.
Move the content of the if statement to a helper function (e.g. diffBinary). It'll be easier to unit test, and easier to read.
|
We can do it after this is merged, but we should get all the logic around generating a diff console output to a standalone module. (Also, we want to stop using |
82177ce to
659d083
Compare
|
OK i've pushed a new version with your comments related changes @SBoudrias |
659d083 to
3238b09
Compare
|
I'll rebase |
3238b09 to
f71de02
Compare
|
LGTM, I'll let @sindresorhus go over it one last time. |
lib/util/binary-diff.js
Outdated
There was a problem hiding this comment.
The new file is still not created
There was a problem hiding this comment.
But maybe blank is better ?
There was a problem hiding this comment.
We only pass in the vinyl file.contents here, but if we were to pass in the file instead we could get the stat: dateFormat(newFile.stat.mtime)
There was a problem hiding this comment.
I agree, but it would change the conflicter checkForCollision function signature. Isn't this part of the API ?
If no problem, I can do the refactor.
There was a problem hiding this comment.
I'm not following. @SBoudrias
But isn't it just about passing file instead of file.contents here https://github.com/yeoman/generator/pull/737/files#diff-0765c740f9a715d47c62655d99f00ee6R171 ?
There was a problem hiding this comment.
Only watching this line may confuse ...
You have to read the real structure of file object, which is not a Vinyl file, here : https://github.com/Toilal/generator/blob/feat/binary-diff/lib/util/conflicter.js#L42-L45
file.path is the destination filepath (this is the existing file), and file.contents is the replacement content of the file (this is the new file).
|
I would also like to see a test if possible. |
f71de02 to
9693eea
Compare
|
I've written test checking display of right diff with binary and text files. I'm quite new in javascript, so I hope the mocks are correctly written using |
a409621 to
0f91850
Compare
|
Add |
0f91850 to
a7223ae
Compare
|
Sorry about that, and thanks for the tip. |
lib/test/adapter.js
Outdated
There was a problem hiding this comment.
Why did you changed that file?
These change are irrelevant to the current matter, and setup method that mutate the object state are pretty hard to maintain on the long run. Please revert those changes
There was a problem hiding this comment.
Yes sorry. It was used at some time in unit tests, but not required anymore.
a7223ae to
489db88
Compare
|
Alright, this LGTM! Thanks for the work you done on this one!! |
Use a custom diff view for binary files
|
👍 Awesome work on this one @Toilal 💃 |
Here the binary diff discussed in #736
Here's the overview.