1- import * as github from '@actions/github' ;
21import { execSync } from 'child_process' ;
32import * as core from '@actions/core' ;
43import { Benchmark } from './extract' ;
@@ -16,46 +15,16 @@ export class GitGraphAnalyzer {
1615 }
1716
1817 /**
19- * Get current branch from GitHub context
18+ * Check if git CLI is available
2019 */
21- getCurrentBranch ( ) : string {
22- const context = github . context ;
23-
24- // For pull requests, get the head branch
25- if ( context . payload . pull_request ) {
26- return context . payload . pull_request . head . ref ;
27- }
28-
29- // Try to get branch from git CLI first if available
30- if ( this . gitCliAvailable ) {
31- try {
32- const branch = execSync ( 'git rev-parse --abbrev-ref HEAD' , {
33- encoding : 'utf8' ,
34- cwd : process . env . GITHUB_WORKSPACE ?? process . cwd ( ) ,
35- } ) . trim ( ) ;
36-
37- if ( branch && branch !== 'HEAD' ) {
38- return branch ;
39- }
40- } catch ( e ) {
41- core . debug ( `Failed to get branch from git CLI: ${ e } ` ) ;
42- }
43- }
44-
45- // For pushes, get the branch from ref
46- if ( context . ref ) {
47- // Remove 'refs/heads/' prefix if present
48- return context . ref . replace ( 'refs/heads/' , '' ) ;
49- }
50-
51- // Fallback to 'main' if we can't determine branch
52- return 'main' ;
20+ isGitAvailable ( ) : boolean {
21+ return this . gitCliAvailable ;
5322 }
5423
5524 /**
56- * Get git ancestry using topological order (only works in GitHub Actions environment)
25+ * Get git ancestry using topological order
5726 */
58- getBranchAncestry ( ref : string ) : string [ ] {
27+ getAncestry ( ref : string ) : string [ ] {
5928 if ( ! this . gitCliAvailable ) {
6029 core . warning ( 'Git CLI not available, cannot determine ancestry' ) ;
6130 return [ ] ;
@@ -78,10 +47,11 @@ export class GitGraphAnalyzer {
7847 }
7948
8049 /**
81- * Find previous benchmark commit based on git graph structure
50+ * Find previous benchmark commit based on git ancestry.
51+ * Falls back to execution time ordering if git ancestry is not available.
8252 */
8353 findPreviousBenchmark ( suites : Benchmark [ ] , currentSha : string ) : Benchmark | null {
84- const ancestry = this . getBranchAncestry ( currentSha ) ;
54+ const ancestry = this . getAncestry ( currentSha ) ;
8555
8656 if ( ancestry . length === 0 ) {
8757 core . warning ( `No ancestry found for commit ${ currentSha } , falling back to execution time ordering` ) ;
@@ -111,37 +81,16 @@ export class GitGraphAnalyzer {
11181 return null ;
11282 }
11383
114- /**
115- * Sort benchmark data by commit timestamp (for GitHub Pages visualization)
116- * This doesn't need git CLI - just uses the commit timestamps already stored
117- */
118- sortByGitOrder ( suites : Benchmark [ ] ) : Benchmark [ ] {
119- if ( suites . length === 0 ) return suites ;
120-
121- // For GitHub Pages, we don't have git CLI, so sort by commit timestamp
122- // This gives a reasonable approximation of git order
123- const sortedSuites = [ ...suites ] . sort ( ( a , b ) => {
124- const timestampA = new Date ( a . commit . timestamp ?? '1970-01-01T00:00:00Z' ) . getTime ( ) ;
125- const timestampB = new Date ( b . commit . timestamp ?? '1970-01-01T00:00:00Z' ) . getTime ( ) ;
126- return timestampA - timestampB ;
127- } ) ;
128-
129- core . debug ( 'Sorted benchmarks by commit timestamp (GitHub Pages mode)' ) ;
130- return sortedSuites ;
131- }
132-
13384 /**
13485 * Find the insertion index for a new benchmark entry based on git ancestry.
135- * Returns the index after which the new entry should be inserted.
136- * If no ancestor is found, returns -1 (insert at beginning) or suites.length (append to end).
86+ * Inserts after the most recent ancestor in the existing suites.
13787 */
13888 findInsertionIndex ( suites : Benchmark [ ] , newCommitSha : string ) : number {
13989 if ( ! this . gitCliAvailable || suites . length === 0 ) {
140- // Fallback: append to end
14190 return suites . length ;
14291 }
14392
144- const ancestry = this . getBranchAncestry ( newCommitSha ) ;
93+ const ancestry = this . getAncestry ( newCommitSha ) ;
14594 if ( ancestry . length === 0 ) {
14695 core . debug ( 'No ancestry found, appending to end' ) ;
14796 return suites . length ;
0 commit comments