在GitHub中列出文件大小

Sph*_*xxx 6 github

GitHub文件浏览器列出了文件的名称和有关上次提交的信息:

GitHub文件浏览器

有没有办法将每个文件的大小添加到这些列表中?

Sph*_*xxx 22

如果没有正式的方法,这里有一个书签,它使用GitHub的API来添加大小信息(在IE中不起作用):

javascript:(function(){function u(n){var r=document.querySelector('table.files a[title="'+n.name+'"]'),i=r.closest("tr").querySelector("td.age"),t=document.createElement("td");n.type==="file"&&(t.textContent=(n.size/1024).toLocaleString("en-US",{minimumFractionDigits:1,maximumFractionDigits:1})+" KB",t.style.textAlign="right");i.parentNode.insertBefore(t,i)}var n=window.location.pathname.split("/"),t,r,i;n[0].length===0&&(n=n.slice(1));t=["https://api.github.com/repos",n[0],n[1],"contents"];n.length>2&&(n.length>4&&(t=t.concat(n.slice(4))),r=n[3]);i=t.join("/");r&&(i+="?ref="+r);console.log(i);fetch(i).then(n=>n.json()).then(n=>{Array.isArray(n)&&n.forEach(u)})})()
Run Code Online (Sandbox Code Playgroud)

文件大小已添加

未经授权的来源:

(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('table.files a[title="' +info.name+ '"]');
        const ageCol = link.closest('tr').querySelector('td.age');

        const sizeCell = document.createElement('td');
        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
            sizeCell.textContent = (info.size/1024).toLocaleString('en-US', { minimumFractionDigits: 1, maximumFractionDigits: 1 }) + ' KB';
            sizeCell.style.textAlign = 'right';
        }

        ageCol.parentNode.insertBefore(sizeCell, ageCol);
    }
})();
Run Code Online (Sandbox Code Playgroud)

  • @RaymondPeng - 通过 API 访问私有存储库可能需要身份验证,而此小书签不执行此操作:https://developer.github.com/v3/#authentication (2认同)

Von*_*onC 0

不,GitHub 文件浏览器不能以这种方式配置。

取回这些额外信息意味着要为 GitHub 传输大量额外数据(每个存储库的每个页面),因此我不确定您是否会很快看到此功能。


请注意,“大小”是 GitHub 搜索的可接受标准(这意味着大小信息存在并且可以使用,而不仅仅是用于浏览文件)。

element language:xml size:100

将代码与标记为 XML 且正好有 100 个字节的“元素”一词相匹配。

  • 我的意思是,如果你想拥有一个流行的网络应用程序,你就必须传输信息。这就是我们所付出的代价。我衷心希望他们有更好的理由,而不是“通过网络发送文件大小信息太昂贵”。他们可以默认将其关闭,我们将通过打开它的数量来了解有多少人想要它。当我探索存储库时,这对我来说是一个常见的烦恼。 (7认同)
  • 谢谢,但我不明白这会是“大量的额外数据”。你能详细说明一下吗? (4认同)
  • @Sphinxxx 简单:将这些额外的位乘以 GitHub 每天提供的页面数量,您将获得 GB 的*额外*数据来传输(更不用说计算和缓存)。如果*不*微不足道的话,添加任何额外的信息(例如尺寸)。如果您想了解有关 GitHub 的可扩展性问题的更多信息,请收听 http://softwareengineeringdaily.com/2016/07/26/scaling-github-with-sam-lambert/ (2认同)
  • @Blauhirn 我同意,但不幸的是,在 GitHub 规模上,它非常“不是”相对的,而且相当巨大。另外,缓存意味着您需要保持缓存信息的一致性,考虑到 GitHub 托管的 Git 存储库本身就是……通过 DGit 和 Spoke 分布式(!),这并非易事。(https://githubengineering.com/building-resilience-in-spokes/)。所以……这确实取决于 GitHub,但他们比我更好:这不是一件简单的任务。 (2认同)