Skip to content

Use a custom diff view for binary files#737

Merged
SBoudrias merged 1 commit intoyeoman:masterfrom
Toilal:feat/binary-diff
Jan 25, 2015
Merged

Use a custom diff view for binary files#737
SBoudrias merged 1 commit intoyeoman:masterfrom
Toilal:feat/binary-diff

Conversation

@Toilal
Copy link
Contributor

@Toilal Toilal commented Jan 17, 2015

Here the binary diff discussed in #736

Here's the overview.

 conflict gradle-wrapper.jar
? Overwrite gradle-wrapper.jar? show the differences between the old and the new
┌───────────────┬──────────────────────────┬─────────────┬──────┐
│               │ Existing                 │ Replacement │ Diff │
├───────────────┼──────────────────────────┼─────────────┼──────┤
│ Size          │ 51.01 kB                 │ 51.01 kB    │ -2 B │
├───────────────┼──────────────────────────┼─────────────┼──────┤
│ Last modified │ Sat Jan 17 2015 10:03:33 │ N/A         │ N/A  │
└───────────────┴──────────────────────────┴─────────────┴──────┘
? Overwrite gradle-wrapper.jar? (Ynaxdh)

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.52%) when pulling 6ed97bb on Toilal:feat/binary-diff into 03c0ca3 on yeoman:master.

@sindresorhus
Copy link
Member

Nice work @Toilal :)

mtime => Last modified

I wonder if it would make more sense to inverse the table, so Size, Modified is on the left, and Existing, Future, Diff is on the top? Thoughts?

package.json Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already depend on isbinaryfile, maybe reuse that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ho yes, of course, i didn't see there was already this dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agre but file is fully read just after using isbinaryfile.

@see #737 (comment).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, then go for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the conflicter handles buffers (he doesn't know of the actual template file), I'd drop isbinaryfileand only use istextorbinary.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.52%) when pulling 0cd1d60 on Toilal:feat/binary-diff into 03c0ca3 on yeoman:master.

@Toilal
Copy link
Contributor Author

Toilal commented Jan 17, 2015

I've reverse the table ordering.

I've kept istextorbinary because it handle buffer as input, where isbinaryfile handle file as input.

Currently, isbinaryfile is only used in actions.js.

  var binary = isBinaryFile(source);
  if (!binary) {
    encoding = 'utf8';
  }

  body = fs.readFileSync(source, encoding);

Could be replaced using istextorbinary

  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.

@Toilal
Copy link
Contributor Author

Toilal commented Jan 17, 2015

Another question : Should i use the extension feature of istextorbinary ?

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.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.52%) when pulling 9958860 on Toilal:feat/binary-diff into 03c0ca3 on yeoman:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.21%) when pulling e4b09fd on Toilal:feat/binary-diff into 03c0ca3 on yeoman:master.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use read-chunk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could merge and release this one then : sindresorhus/read-chunk#2

@Toilal
Copy link
Contributor Author

Toilal commented Jan 17, 2015

Should be OK now, using read-chunk 1.0.1.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.6%) when pulling 82177ce on Toilal:feat/binary-diff into 03c0ca3 on yeoman:master.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@SBoudrias
Copy link
Member

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 adapter.diff() method)

@Toilal
Copy link
Contributor Author

Toilal commented Jan 17, 2015

OK i've pushed a new version with your comments related changes @SBoudrias

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.11%) when pulling 659d083 on Toilal:feat/binary-diff into 45bb7a5 on yeoman:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.2%) when pulling 3238b09 on Toilal:feat/binary-diff into 45bb7a5 on yeoman:master.

@Toilal
Copy link
Contributor Author

Toilal commented Jan 18, 2015

I'll rebase

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.2%) when pulling f71de02 on Toilal:feat/binary-diff into 697ecb2 on yeoman:master.

@SBoudrias
Copy link
Member

LGTM, I'll let @sindresorhus go over it one last time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this N/A?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new file is still not created

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But maybe blank is better ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@sindresorhus
Copy link
Member

I would also like to see a test if possible.

@Toilal
Copy link
Contributor Author

Toilal commented Jan 20, 2015

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 sinon.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.2%) when pulling 9693eea on Toilal:feat/binary-diff into b611741 on yeoman:master.

@Toilal Toilal force-pushed the feat/binary-diff branch 2 times, most recently from a409621 to 0f91850 Compare January 20, 2015 15:34
@kevva
Copy link
Member

kevva commented Jan 20, 2015

Add .idea to your global .gitignore (in HOME).

@Toilal
Copy link
Contributor Author

Toilal commented Jan 20, 2015

Sorry about that, and thanks for the tip.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.15%) when pulling a7223ae on Toilal:feat/binary-diff into b611741 on yeoman:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.15%) when pulling a7223ae on Toilal:feat/binary-diff into b611741 on yeoman:master.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sorry. It was used at some time in unit tests, but not required anymore.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.15%) when pulling 489db88 on Toilal:feat/binary-diff into b611741 on yeoman:master.

@SBoudrias
Copy link
Member

Alright, this LGTM! Thanks for the work you done on this one!!

SBoudrias added a commit that referenced this pull request Jan 25, 2015
Use a custom diff view for binary files
@SBoudrias SBoudrias merged commit 79e882e into yeoman:master Jan 25, 2015
@sindresorhus
Copy link
Member

👍 Awesome work on this one @Toilal 💃

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.

5 participants