18

The GitHub file browser lists a file's name and info about the last commit:

GitHub file browser

Is there a way to add each file's size to these listings?

Sphinxxx
  • 11,400
  • 4
  • 49
  • 78

3 Answers3

37

If there is no official way to do this, here is a bookmarklet which uses GitHub's API to add size info (won't work in IE):

javascript:(function(){function f(a){var g=document.querySelector('div[role="rowheader"] a[title="'+a.name+'"]').closest('div[role="row"]').lastElementChild,c=document.createElement("div");c.style.width="5em";"file"===a.type&&(c.textContent=(a.size/1024).toLocaleString("en-US",{minimumFractionDigits:1,maximumFractionDigits:1})+" KB",c.style.textAlign="right",c.style.whiteSpace="nowrap");g.insertAdjacentElement("beforebegin",c)}var b=window.location.pathname.split("/"),d=b[1],h=b[2],e=b[4];b=b.slice(5);d=["https://api.github.com/repos",d,h,"contents"].concat(b||[]).join("/")+(e?"?ref="+e:"");console.log(d);fetch(d).then(function(a){return a.json()}).then(function(a){return Array.isArray(a)?a.forEach(f):console.warn(a)})})();

File sizes added

Unminified source:

(function () {
    "use strict";

    //Parse the current GitHub repo url. Examples:
    //  Repo root:           /Sphinxxxx/vanilla-picker
    //  Subfolder:           /Sphinxxxx/vanilla-picker/tree/master/src/css
    //  Subfolder at commit: /Sphinxxxx/vanilla-picker/tree/382231756aac75a49f046ccee1b04263196f9a22/src/css
    //  Subfolder at tag:    /Sphinxxxx/vanilla-picker/tree/v2.2.0/src/css
    //
    //If applicable, the name of the commit/branch/tag is always the 4th element in the url path.
    //Here, we put that in the "ref" variable:
    const [/* Leading slash */, owner, repo, /* "tree" */, ref, ...path] = window.location.pathname.split('/');

    //Create the URL to query GitHub's API: https://developer.github.com/v3/repos/contents/#get-contents
    //Example:
    //  https://api.github.com/repos/Sphinxxxx/vanilla-picker/contents/src/css?ref=382231756aac75a49f046ccee1b04263196f9a22
    const query = ['https://api.github.com/repos', owner, repo, 'contents'].concat(path || []),
          url = query.join('/') + (ref ? '?ref=' + ref : '');
    console.log(url);

    //https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
    fetch(url).then(r => r.json())
              .then(j => Array.isArray(j) ? j.forEach(handleFileInfo) : console.warn(j));

    function handleFileInfo(info) {
        //console.log(info);
        const link = document.querySelector(`div[role="rowheader"] a[title="${info.name}"]`);
        const timeCol = link.closest('div[role="row"]').lastElementChild;

        const sizeCol = document.createElement('div');
        sizeCol.style.width = '5em';
        if(info.type === 'file') {
            //http://stackoverflow.com/a/17663871/1869660
            //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Parameters
            sizeCol.textContent = (info.size/1024).toLocaleString('en-US', { minimumFractionDigits: 1, maximumFractionDigits: 1 }) + ' KB';
            sizeCol.style.textAlign = 'right';
            sizeCol.style.whiteSpace = 'nowrap';
        }

        timeCol.insertAdjacentElement('beforebegin', sizeCol);
    }
})();

Create a bookmarklet from the first piece of code, or just copy and paste it to your browser's console.

Sphinxxx
  • 11,400
  • 4
  • 49
  • 78
  • Nice. +1. I hope that does not create a burden on the API, I know there is a call rate limit, so if you browse too much... that won't work. – VonC Jul 30 '16 at 15:29
  • @VonC - Good point! Looks like the limit is 60 requests per hour (and no size limit): https://developer.github.com/v3/#rate-limiting – Sphinxxx Jul 31 '16 at 12:38
  • Does it work for private repos. It got a 404 XHR error for me. – Raymond Apr 22 '19 at 04:44
  • 1
    @RaymondPeng - Accessing private repos through the API probably requires authentication, which this bookmarklet doesn't do: https://developer.github.com/v3/#authentication – Sphinxxx Apr 22 '19 at 15:21
  • Thanks! Could you update it to the latest Github layout? It doesn't seem to work anymore. – d33tah Jun 24 '20 at 14:29
  • @d33tah - Updated – Sphinxxx Jun 25 '20 at 13:49
4

For Chrome browsers there exists an extension:

https://chrome.google.com/webstore/detail/github-repository-size/apnjnioapinblneaedefcnopcjepgkci

Sebastian
  • 1,486
  • 2
  • 8
  • 18
-1

No, the GitHub file browser is not configurable that way.

Getting back that extra information would mean transferring an enormous extra amount of data (for each pages of each repos) for GitHub, so I am not sure this is a feature you would see anytime soon.


Note that 'size' is an acceptable criteria for GitHub search though (meaning that size information is there and can be used, not just for browsing files).

element language:xml size:100

Matches code with the word "element" that's marked as being XML and has exactly 100 bytes.

VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • 3
    Thanks, but I don't see how that would be "an enormous extra amount of data". Could you elaborate a little? – Sphinxxx Jul 31 '16 at 12:35
  • 2
    @Sphinxxx simple: multiply those extra bits by the number of pages served every day by GitHub, and you will get GB of *additional* data to transger (not to mention to compute and to cache). Adding any extra information (like the size) if *not* trivial. If you want to know more about scalability issue at GitHub, listen to http://softwareengineeringdaily.com/2016/07/26/scaling-github-with-sam-lambert/ – VonC Jul 31 '16 at 12:50
  • 6
    I mean, if you want to have a popular web app, you have to transmit information. That's what we're paying for. I dearly hope they have better reasons than "sending file size info across the wire is too expensive". They could have it off by default and we'll see how many people want it by the number that turn it on. It's a regular annoyance for me when I'm exploring repos. – Michael Terry Feb 27 '18 at 19:33
  • well "enormous extra amount of data" is relative. such information could be cached. this feature would be pretty much up to github. – phil294 May 30 '18 at 19:11
  • 2
    @Blauhirn I agree, but unfortunately, at GitHub scale, it is very much *not* relative, and quite enormous. Plus caching means you need to maintain consistency of that cached information, which is not trivial considering the Git repositories hosted by GitHub are themselves... distributed(!) Through DGit and Spoke. (https://githubengineering.com/building-resilience-in-spokes/). So... It is indeed up to GitHub, but better them than me: that is no trivial task. – VonC May 30 '18 at 19:16