Repository navigation
Switch to GitHub data API for files that are too large for the content API - #5
Conversation
alex-e-leon
left a comment
There was a problem hiding this comment.
Hey @lyuzashi - thanks for contributing back! I had one question about the return value of blob.contents - but otherwise everything looks good.
However I also wonder whether it even makes sense to return a diff for large binary files like zip? Thoughts?
| owner, | ||
| repo, | ||
| sha | ||
| }).then(data => data.content); |
There was a problem hiding this comment.
It looks like blob content is always returned as base64 encoded: https://developer.github.com/v3/git/blobs/#get-a-blob
Shouldn't we be un-encoding it here so that the diff can work properly?
There was a problem hiding this comment.
getContent also returns a base64 encoded content. The atob function is used in both cases to un-encode the content.
There was a problem hiding this comment.
good catch - it's obviously been a while since I worked on this. the repo could really use some tests - in fact if you have some time... 😛
| return res.content; | ||
| }).catch((err) => { | ||
| try { | ||
| const apiError = JSON.parse(err); |
|
Hi @alex-e-leon, thanks for reviewing! I believe I've got the correct behaviour for decoding the base64, since both You're right to question the use for binary files. From what I can tell the diff only includes file names, however the See additional commit for this routine. Let me know your thoughts :) |
alex-e-leon
left a comment
There was a problem hiding this comment.
Looks good to me @lyuzashi - thanks for your work! Since there's no unit tests yet I'd like to run it against a repo to check the output before I merge it though.
Do you have access to a publicly accessible repo with some large files in it, or if not, are you able to print the output when you run the cli against a private repo (assuming there's no sensitive info in the output)
|
My pleasure @alex-e-leon. Would be great to add unit tests, I agree. During writing these commits, I've tried the code against a private repo, which is now succeeding, however the output does not provide enough insights into I've put up a very simple test case to check existing and new features. Hope this helps. $ node cli.js lyuzashi/github-diff-sample 1.0.0 1.1.0
diff --git a/binary-data.zip b/binary-data.zip
--- a/binary-data.zip
+++ b/binary-data.zip
undefined
diff --git a/file-that-is-renamed.txt b/file-to-be-renamed.txt
--- a/file-that-is-renamed.txt
+++ b/file-to-be-renamed.txt
undefined
diff --git a/file-to-remove.txt b/file-to-remove.txt
--- a/file-to-remove.txt
+++ b/file-to-remove.txt
@@ -1,4 +0,0 @@
-This is a file
-that will be
-removed in the
-next commit.
diff --git a/readme.MD b/readme.MD
--- a/readme.MD
+++ b/readme.MD
@@ -7,4 +7,6 @@ This repository is a quick test for the following functions of [github-diff](htt
* Modifying files
* Renaming files
-This file will be modified.
+This file was modified.
+
+binary-data.zip was added. |
|
@lyuzashi Thanks for that - there's no patch info on the zip file though since it was added instead of changed between versions, would you be able to make another commit that changes the zip file? Also note that the zip file that was uploaded isn't > 10mb, so I think in this example it still only tests the old code paths? If you change the zip to be >10mb though that should test it in the second getContent call |
|
I've checked the
which is caught and a second call is made to return the file content. Just to be sure, I've both modified the zip file and increased its size to +10MB. In doing this, I've noticed that GitHub API does not have version specific SHA for each file (but it's obvious looking back at the code) which means that the A and B blobs are identical. Since a patch is not provided either, I'd think that indicating the file is modified and providing a completely new file would be enough to handle this case. Let me know your thoughts. I haven't come across another way to access large files from a particular commit. |
|
Ah - sorry, my mistake I thought that the limit on getContents was |
|
@lyuzashi Ah yeap, since when we run After thinking about it for a bit, I think that the solution we have so far would be unexpected behaviour for most users. If the file is over 1MB you get back fileA and fileB but they are actually the same file and the patch is If we did the extra work and fetched the sha for the old blob, so at least the file contents being returned were correct I'd be ok with that, but that does slow the script down as it's an extra network request for each file (unless we move some code around, in which case we could drop the extra work and only fetch the extra sha when necessary) However, another option would just be to avoid fetching the large data at all and just returning Thoughts? |
|
Thanks for putting so much thought into this @alex-e-leon. It's definitely a tricky one. I'd be happy with a few extra network requests (only for large files) to get the old SHA. I'm looking into implementing this now. I'd prefer the library did everything it could to return a complete diff for my use case, but perhaps we could include an option to disable or warning somewhere when several round trips are required for such files. Of course, this is all just for an edge case, since it's rare to have files over 1MB checked in. |
|
not at all @lyuzashi - thanks for doing all the work! Supporting edge cases is what good software is supposed to do 😄 - I'm happier with the lib working as expected, so let me know when you get the old sha working and we can get this merged in! |
|
Brilliant @alex-e-leon! In doing this, I no longer needed to pass the original SHA around either, which cleans up the code a bit for the merge. Hilariously, I thought I failed at first because my zip file test is identical for the first few dozen bytes. But by checking the size difference (and double checking it was requesting different SHA) I think I've proved this works. Hope this works for you 😃 |
|
@lyuzashi Looks great! I've just published it as a new version |
|
Thanks so much for working through this and with me and releasing so quick @alex-e-leon! |
|
Oh no! I just came across another edge case for this. Will open another PR |
In rare cases, large files (such as .zip) are added to a repository.
GitHub API restricts their content API to 1MB, but offer a data blob API able to retrieve up to 100MB.
To enable
github-diffto successfully perform a diff on large files, this PR includes the data blob API as a fallback for that specific error.The SHA for each file must be passed around to allow this.