Skip to content

Commit a703951

Browse files
committed
Added Commit Treee
1 parent d0a3fb4 commit a703951

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

.github/workflows/update-readme.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v2
1414

15-
- name: Set up Node.js
16-
uses: actions/setup-node@v2
17-
with:
18-
node-version: '14'
19-
20-
- name: Install dependencies
21-
run: npm install
15+
- name: Set up Node.js environment and dependencies
16+
run: |
17+
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
18+
sudo apt-get install -y nodejs
19+
npm install @gitgraph/js canvas fs-extra
2220
2321
- name: Generate commit tree
2422
run: node generate_commit_tree.js

generate_commit_tree.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { execSync } = require('child_process');
12
const { writeFile } = require('fs-extra');
23
const { Gitgraph } = require('@gitgraph/js');
34
const { createCanvas } = require('canvas');
@@ -13,25 +14,30 @@ const generateGitGraph = async () => {
1314
// Initialize Gitgraph
1415
const gitgraph = new Gitgraph(ctx);
1516

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+
});
3036

3137
// Save canvas to file
3238
const buffer = canvas.toBuffer('image/png');
3339
await writeFile('commit-tree.png', buffer);
3440
console.log('The file has been saved!');
3541
};
3642

37-
generateGitGraph();
43+
generateGitGraph().catch(console.error);

0 commit comments

Comments
 (0)