Skip to content

Instantly share code, notes, and snippets.

@regexyl
Last active January 23, 2024 19:20
Show Gist options
  • Save regexyl/4e06a8efac83f4034cef591cc8e678a6 to your computer and use it in GitHub Desktop.
Save regexyl/4e06a8efac83f4034cef591cc8e678a6 to your computer and use it in GitHub Desktop.
Shortcut to render the first page of commits from a public GitHub repo
// Execute this code snippet in the browser console when you're
// already on a repo's webpage. This is pretty helpful when you
// want to read a large OSS project but feel overwhelmed or not
// sure where to start - this is a neat hack to take you to see
// the original core concepts of the project, which might not
// always work but does the job most of the time.
(async () => {
try {
const pathMatches = window.location.pathname.match(/\/([^\/]+\/[^\/]+)(?:\/tree\/([^\/]+))?/);
if (!pathMatches) return;
const [, repo, sha] = pathMatches;
const response = await fetch(`https://api.github.com/repos/${repo}/commits?sha=${sha || ''}`);
let [linkHeader, commits] = await Promise.all([response.headers.get('link'), response.json()]);
if (linkHeader) {
const nextLink = linkHeader.split(',')[1].split(';')[0].slice(2, -1);
const nextResponse = await fetch(nextLink);
commits = await nextResponse.json();
}
const lastPageFirstSha = commits[0].sha;
window.location = `https://github.com/${repo}/commits?after=${lastPageFirstSha}+0`;
} catch (error) {
console.error('An error occurred:', error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment