The Commit Graph package is a React component suite designed to visualize commit graphs in an interactive and informative way. It showcases commit history within a repository with support for infinite scroll loading. See this post for the implementation details.
It is used by platforms like DoltHub to visualize database commit log histories.
Explore the demo for sample commit data and real GitHub repository graphs.
- Interactive Commit Graph Visualization: Provides a dynamic visual of commit history with detailed repository activity.
- Infinite Scroll Support:
CommitGraph.WithInfiniteScrolldynamically loads new content as users scroll through large commit histories. - Customizable Styles: Easily style the commit log graph, including node colors, spacing, and more.
- Diff Stats on Commit Click: If a
getDifffunction is provided, clicking a commit will display additional file change stats, including additions, deletions, and file modifications.
npm install commit-graphFor a basic implementation without infinite scroll:
importReactfrom"react";import{CommitGraph}from"commit-graph";constMyComponent=()=>{constcommits=[// Commits data according to the new Commit type];constbranchHeads=[// Branch heads data according to the new Branch type];return(<CommitGraphcommits={commits}branchHeads={branchHeads}graphStyle={{commitSpacing: 50,branchSpacing: 20,branchColors: ["#FF0000","#00FF00","#0000FF"],nodeRadius: 2,}}getDiff={async(base,head)=>{// Your implementation to fetch diff statsreturn{files: [{filename: "example.txt",status: "modified",additions: 10,deletions: 2,},],};}}/>);};exportdefaultMyComponent;For large commit histories requiring infinite scroll:
importReactfrom"react";import{CommitGraph}from"commit-graph";constMyComponent=()=>{// Your commit and branch head data, loadMore function, and hasMore flagreturn(<CommitGraph.WithInfiniteScrollcommits={/* Your commits data */}branchHeads={/* Your branch heads data */}loadMore={/* Your loadMore function */}hasMore={/* hasMore flag */}/>);};exportdefaultMyComponent;commits (array): An array of
Commitobjects representing the commit history.branchHeads (array): An array of
Branchobjects representing the branch heads.getDiff (function, optional): A function that returns a
Promiseresolving to aDiffobject. Fetches file changes for clicked commits.currentBranch (string, optional): The name of the current branch.
fullSha (boolean, optional): Displays the full SHA of a commit instead of the shortened SHA.
onCommitClick (function, optional): Function to be called when a commit is clicked, receiving the clicked commit as an argument.
dateFormatFn (function, optional): Formats the commit dates. Accepts a string, number, or Date and returns a formatted string.
dateFormatFn?: (d: string|number|Date)=>string;Example:
constcustomDateTimeFormatFn=(d: string|number|Date): string=>{returnnewDate(d).toLocaleString('en-US',{year: 'numeric',month: 'short',day: 'numeric',hour: '2-digit',minute: '2-digit',second: '2-digit',});};constMyComponent=()=>{// Your commit and branch head data, loadMore function, and hasMore flagreturn(<CommitGraph.WithInfiniteScrollcommits={/* Your commits data */}branchHeads={/*Yourbranchheadsdata*/}loadMore={/*YourloadMorefunction*/}hasMore={/* hasMore flag */}dateFormatFn={customDateTimeFormatFn}/>);};- graphStyle (object, optional): Customize the graph styling. Example:
constgraphStyle={commitSpacing: 60,branchSpacing: 20,nodeRadius: 2,branchColors: ["#FF0000","#00FF00","#0000FF"],};loadMore (function): Function to load more commits as the user scrolls.
hasMore (boolean): Boolean flag indicating whether more commits are available to load.
Represents individual commits:
typeParentCommit={sha: string;};exporttypeCommit={sha: string;commit: {author: {name: string;// The name of the commit authordate: string|number|Date;// The date of the commitemail?: string;// The email of the commit author (optional)};message: string;// The commit message};parents: ParentCommit[];// An array of parent commitshtml_url?: string;// The URL to view the commit (optional)};Defines repository branches, each associated with a commit:
exporttypeBranch={name: string;// The name of the branchcommit: {sha: string;// The SHA of the latest commit on the branch};link?: string;// A URL to the branch on GitHub (optional)};Represents changes between two commits:
exporttypeDiff={files: ChangedItem[];};Details of files changed between commits:
exporttypeChangedItem={filename: string;status: string;// "added", "modified", or "deleted"additions: number;deletions: number;patch?: string;// Optional patch data for the changesblob_url?: string;// Optional URL to the file blob};Explore the Commit Graph component and its features by running storybook:
npm run storybook