forked from swift502/Sketchbook
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest2.ts
More file actions
88 lines (75 loc) · 1.99 KB
/
test2.ts
File metadata and controls
88 lines (75 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const git = require('isomorphic-git')
const fs = require('fs')
async function gitLogWithPatches(dir, filepath, limit = 100) {
const commits = await git.log({
fs,
dir,
depth: limit,
filepath: filepath
})
const results: any[] = []
for (const commit of commits) {
const { oid: commitOid } = commit
const parentOid = commit.parent[0] || null
let patch = ''
if (parentOid) {
const { blob: oldBlob } = await git.readBlob({
fs,
dir,
oid: parentOid,
filepath: filepath
}).catch(() => ({ blob: '' }))
const { blob: newBlob } = await git.readBlob({
fs,
dir,
oid: commitOid,
filepath: filepath
})
patch = await git.diff({
fs,
dir,
oldRef: parentOid,
newRef: commitOid,
filepath: filepath
})
} else {
const { blob } = await git.readBlob({
fs,
dir,
oid: commitOid,
filepath: filepath
})
patch = `+${blob.toString('utf8')}`
}
results.push({
oid: commitOid,
commit: commit.commit,
author: commit.author,
committer: commit.committer,
message: commit.message,
patch: patch
})
if (results.length >= limit) break
}
return results
}
// Usage example
async function main() {
const dir = '.' // Current directory, adjust as needed
const filepath = 'index.js'
const limit = 100
try {
const logResults = await gitLogWithPatches(dir, filepath, limit)
for (const result of logResults) {
console.log(`Commit: ${result.oid}`)
console.log(`Author: ${result.author.name} <${result.author.email}>`)
console.log(`Date: ${result.author.timestamp}`)
console.log(`\n ${result.message}\n`)
console.log(`Patch:\n${result.patch}\n`)
console.log('-'.repeat(50))
}
} catch (error) {
console.error('Error:', error)
}
}
main()