1
+ const { execSync } = require ( 'child_process' ) ;
1
2
const { writeFile } = require ( 'fs-extra' ) ;
2
3
const { Gitgraph } = require ( '@gitgraph/js' ) ;
3
4
const { createCanvas } = require ( 'canvas' ) ;
@@ -13,25 +14,30 @@ const generateGitGraph = async () => {
13
14
// Initialize Gitgraph
14
15
const gitgraph = new Gitgraph ( ctx ) ;
15
16
16
- // Create the main branch
17
- const master = gitgraph . branch ( "main" ) ;
18
- master . commit ( "Initial commit" ) ;
19
-
20
- // Add more commits
21
- master . commit ( "Second commit" ) ;
22
- master . commit ( "Third commit" ) ;
23
-
24
- // Create feature branch
25
- const feature = master . branch ( "feature-branch" ) ;
26
- feature . commit ( "Feature commit" ) ;
27
-
28
- // Merge feature branch back to main
29
- feature . merge ( master , "Merge feature branch" ) ;
17
+ // Get commits from git log
18
+ const log = execSync ( 'git log --pretty=format:"%h %p %s" --graph --all' ) . toString ( ) ;
19
+ const commits = log . split ( '\n' ) . map ( line => {
20
+ const [ hash , parent , ...message ] = line . trim ( ) . split ( ' ' ) ;
21
+ return { hash, parent, message : message . join ( ' ' ) } ;
22
+ } ) ;
23
+
24
+ const branches = new Map ( ) ;
25
+ const main = gitgraph . branch ( 'master' ) ;
26
+ branches . set ( 'master' , main ) ;
27
+
28
+ // Process commits
29
+ commits . forEach ( commit => {
30
+ const { hash, parent, message } = commit ;
31
+ const parentBranch = branches . get ( parent ) || main ;
32
+ const newBranch = parentBranch . branch ( hash ) ;
33
+ newBranch . commit ( message ) ;
34
+ branches . set ( hash , newBranch ) ;
35
+ } ) ;
30
36
31
37
// Save canvas to file
32
38
const buffer = canvas . toBuffer ( 'image/png' ) ;
33
39
await writeFile ( 'commit-tree.png' , buffer ) ;
34
40
console . log ( 'The file has been saved!' ) ;
35
41
} ;
36
42
37
- generateGitGraph ( ) ;
43
+ generateGitGraph ( ) . catch ( console . error ) ;
0 commit comments