Skip to content

Switch to GitHub data API for files that are too large for the content API - #5

Merged
alex-e-leon merged 3 commits into
alex-e-leon:masterfrom
lyuzashi:master
Dec 21, 2017
Merged

alex-e-leon merged 3 commits into
alex-e-leon:masterfrom
lyuzashi:master

Conversation

@lyuzashi

Copy link
Copy Markdown
Contributor

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

@alex-e-leon alex-e-leon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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?

Comment thread index.js Outdated
owner,
repo,
sha
}).then(data => data.content);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

getContent also returns a base64 encoded content. The atob function is used in both cases to un-encode the content.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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

Comment thread index.js
return res.content;
}).catch((err) => {
try {
const apiError = JSON.parse(err);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

error handling is 👍

@lyuzashi

Copy link
Copy Markdown
Contributor Author

Hi @alex-e-leon, thanks for reviewing! I believe I've got the correct behaviour for decoding the base64, since both getContent and getBlob return base64 encoded content for files, so decoding is handled in each buildContent case.

You're right to question the use for binary files. From what I can tell the diff only includes file names, however the atob function assumed plain text. I've written in a case using the is-binary-buffer module to return the decoded buffer rather than a string for such files.

See additional commit for this routine. Let me know your thoughts :)

@alex-e-leon alex-e-leon left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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)

@lyuzashi

Copy link
Copy Markdown
Contributor Author

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 github-diff itself.

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.
githubDiff('lyuzashi/github-diff-sample', '1.0.0', '1.1.0')
> [ { filename: 'binary-data.zip',
    patch: undefined,
    status: 'added',
    header: 'diff --git a/binary-data.zip b/binary-data.zip\n--- a/binary-data.zip\n+++ b/binary-data.zip\n',
    fileB: <Buffer 50 4b 03 04 14 00 08 00 08 00 00 65 95 4b 00 00 00 00 00 00 00 00 00 00 00 00 18 00 10 00 70 65 78 65 6c73 2d 70 68 6f 74 6f 2d 34 35 31 34 39 30 2e ... > },
  { filename: 'file-that-is-renamed.txt',
    patch: undefined,
    status: 'renamed',
    header: 'diff --git a/file-that-is-renamed.txt b/file-to-be-renamed.txt\n--- a/file-that-is-renamed.txt\n+++ b/file-to-be-renamed.txt\n',
    previousFilename: 'file-to-be-renamed.txt',
    fileA: 'This file will be renamed.',
    fileB: 'This file will be renamed.' },
  { filename: 'file-to-remove.txt',
    patch: '@@ -1,4 +0,0 @@\n-This is a file\n-that will be\n-removed in the\n-next commit.',
    status: 'removed',
    header: 'diff --git a/file-to-remove.txt b/file-to-remove.txt\n--- a/file-to-remove.txt\n+++ b/file-to-remove.txt\n',
    fileA: 'This is a file\nthat will be\nremoved in the\nnext commit.\n' },
  { filename: 'readme.MD',
    patch: '@@ -7,4 +7,6 @@ This repository is a quick test for the following functions of [github-diff](htt\n * Modifying files\n * Renaming files\n \n-This file will be modified.\n+This file was modified.\n+\n+binary-data.zip was added.',
    status: 'modified',
    header: 'diff --git a/readme.MD b/readme.MD\n--- a/readme.MD\n+++ b/readme.MD\n',
    fileA: '# Sample Repository for GitHub Diff\n\nThis repository is a quick test for the following functions of [github-diff](https://github.com/alex-e-leon/github-diff)\n\n* Removing files\n* Adding files\n* Modifying files\n* Renaming files\n\nThis file will be modified.\n',
    fileB: '# Sample Repository for GitHub Diff\n\nThis repository is a quick test for the following functions of [github-diff](https://github.com/alex-e-leon/github-diff)\n\n* Removing files\n* Adding files\n* Modifying files\n* Renaming files\n\nThis file was modified.\n\nbinary-data.zip was added.\n' } ]

@alex-e-leon

Copy link
Copy Markdown
Owner

@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

@lyuzashi

Copy link
Copy Markdown
Contributor Author

I've checked the getBlob path is running. GitHub is returning

This API returns blobs up to 1 MB in size. The requested blob is too large to fetch via the API, but you can use the Git Data API to request blobs up to 100 MB in size.

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.

githubDiff('lyuzashi/github-diff-sample', '1.1.0', '1.2.0')
> [ { filename: 'binary-data.zip',
    patch: undefined,
    status: 'modified',
    header: 'diff --git a/binary-data.zip b/binary-data.zip\n--- a/binary-data.zip\n+++ b/binary-data.zip\n',
    fileA: <Buffer 50 4b 03 04 14 00 08 00 08 00 03 65 95 4b 00 00 00 00 00 00 00 00 00 00 00 00 18 00 10 00 70 65 78 65 6c 73 2d 70 68 6f 74 6f 2d 32 34 36 35 31 39 2e ... >,
    fileB: <Buffer 50 4b 03 04 14 00 08 00 08 00 03 65 95 4b 00 00 00 00 00 00 00 00 00 00 00 00 18 00 10 00 70 65 78 65 6c 73 2d 70 68 6f 74 6f 2d 32 34 36 35 31 39 2e ... > } ]

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.
The problem with this is that only the latest version of a large file can be retrieved.

Let me know your thoughts. I haven't come across another way to access large files from a particular commit.

@alex-e-leon

Copy link
Copy Markdown
Owner

Ah - sorry, my mistake I thought that the limit on getContents was 10Mb not 1Mb - I'll take a look with your test setup and see what happens. Thanks for putting in all this work so far!

@alex-e-leon

Copy link
Copy Markdown
Owner

@lyuzashi Ah yeap, since when we run compareCommits we only get the sha hash from the new file, not the old one.

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 undefined as well - most users will wonder what's going on!

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 undefined for those files while potentially consoling out that the file size was too large. That would probably be easiest, but I'm not sure whether that works for your use-case or not.

Thoughts?

@lyuzashi

Copy link
Copy Markdown
Contributor Author

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.

@alex-e-leon

Copy link
Copy Markdown
Owner

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!

@lyuzashi

Copy link
Copy Markdown
Contributor Author

Brilliant @alex-e-leon!
Added a repos.getCommit call for base and head which finds the file by path (since every other identifier changes) and grabs it with the getBlob API.

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.

githubDiff('lyuzashi/github-diff-sample', '1.1.0', '1.2.0')
> [ { filename: 'binary-data.zip',
    patch: undefined,
    status: 'modified',
    header: 'diff --git a/binary-data.zip b/binary-data.zip\n--- a/binary-data.zip\n+++ b/binary-data.zip\n',
    fileA: <Buffer 50 4b 03 04 14 00 08 00 08 00 00 65 95 4b 00 00 00 00 00 00 00 00 00 00 00 00 18 00 10 00 70 65 78 65 6c 73 2d 70 68 6f 74 6f 2d 34 35 31 34 39 30 2e ... >,
    fileB: <Buffer 50 4b 03 04 14 00 08 00 08 00 03 65 95 4b 00 00 00 00 00 00 00 00 00 00 00 00 18 00 10 00 70 65 78 65 6c 73 2d 70 68 6f 74 6f 2d 32 34 36 35 31 39 2e ... > } ]

But by checking the size difference (and double checking it was requesting different SHA) I think I've proved this works.

githubDiff('lyuzashi/github-diff-sample', '1.1.0', '1.2.0')
  .then(([patch]) => [patch.fileA.length, patch.fileB.length])
> [ 5878185, 16348647 ]

Hope this works for you 😃

@alex-e-leon
alex-e-leon merged commit 12938c9 into alex-e-leon:master Dec 21, 2017
@alex-e-leon

Copy link
Copy Markdown
Owner

@lyuzashi Looks great! I've just published it as a new version 1.3.0 Thanks for the fix!

@lyuzashi

Copy link
Copy Markdown
Contributor Author

Thanks so much for working through this and with me and releasing so quick @alex-e-leon!
Wish we'd had a chance to work together in person before your departure.

@lyuzashi

Copy link
Copy Markdown
Contributor Author

Oh no! I just came across another edge case for this. Will open another PR

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.

2 participants