Skip to content

Commit e108948

Browse files
committed
init
0 parents  commit e108948

10 files changed

Lines changed: 1898 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/lib
3+
package-lock.json

README.MD

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# peg-git-diff-parser
2+
3+
使用 [peggy](https://peggyjs.org/documentation.html) 生成的 `git diff` 命令返回数据的解析器
4+
5+
> Peggy is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. You can use it to process complex data or computer languages and build transformers, interpreters, compilers and other tools easily.
6+
7+
配合 `simple-git` 之类,解析命令行中 `git diff` 命令返回的文本数据格式(只有修改模式)。
8+
9+
## 使用
10+
11+
`src` 下的 `gitDiffParser.peggy``peggy` 的规则逻辑,需要安装 `peggy` 才能使用解析。
12+
13+
`dist` 下的 `gitDiffParser.js` 是生成好的解析脚本,可以直接引入到项目中而无需安装 `peggy`
14+
15+
```js
16+
const GitDiffParser = require("./gitDiffParser");
17+
```
18+
19+
获取所需处理的文本,并导入到组件中
20+
解析器的第二个参数位设置参数 `REMOVE_INDENT`,用于控制是否去除解析后的文本的缩进。
21+
22+
```js
23+
const fileName = "diff_data.txt";
24+
const TEST_DATA = fs.readFileSync(
25+
path.resolve(__dirname, `../test/${fileName}`)
26+
);
27+
28+
const result = GitDiffParser.parse(TEST_DATA.toString(), {
29+
REMOVE_INDENT: false, // 参数,是否去除解析文本中的缩进空格
30+
});
31+
```
32+
33+
预期格式:
34+
35+
```
36+
diff --git a/package.json b/package.json
37+
index cb2f4bc..35455a2 100644
38+
--- a/package.json
39+
+++ b/package.json
40+
@@ -1,13 +1,14 @@
41+
{
42+
"name": "peg-git-diff-parser",
43+
- "version": "0.0.0",
44+
+ "version": "1.0.0",
45+
"description": "git diff 文本解析器",
46+
- "main": "index.js",
47+
+ "main": "src/index.js",
48+
"scripts": {
49+
"build": "peggy -o dist/gitDiffParser.js src/gitDiffParser.peggy",
50+
"test": "node src/index.js"
51+
},
52+
"author": "LnnCoCo",
53+
+ "new": "new",
54+
"license": "ISC",
55+
"dependencies": {
56+
"peggy": "^1.2.0"
57+
```
58+
59+
解析格式:
60+
61+
```js
62+
{
63+
type: 'changed',
64+
filePath: { beforePath: '/package.json', afterPath: '/package.json' },
65+
hash: { beforeHash: 'cb2f4bc', afterHash: '35455a2' },
66+
mode: '100',
67+
permissions: '644',
68+
change: [
69+
{
70+
chunkHeader: '@@ -1,13 +1,14 @@',
71+
chunkBeforeLine: '1,13',
72+
chunkAfterLine: '1,14',
73+
content: [
74+
{ type: '-', text: ' "version": "0.0.0",' },
75+
{ type: '+', text: ' "version": "1.0.0",' },
76+
{ type: '-', text: ' "main": "index.js",' },
77+
{ type: '+', text: ' "main": "src/index.js",' },
78+
{ type: '+', text: ' "new": "new",' }
79+
]
80+
}
81+
]
82+
}
83+
```
84+
85+
返回数据的 TS 定义:
86+
87+
```ts
88+
interface GitDiffParserResult {
89+
type: "changed";
90+
filePath: { beforePath: string; afterPath: string };
91+
hash: { beforeHash: string; afterHash: string };
92+
mode: string;
93+
permissions: string;
94+
change: {
95+
chunkHeader: string;
96+
chunkBeforeLine: string;
97+
chunkAfterLine: string;
98+
content: {
99+
type: "+" | "-";
100+
text: string;
101+
}[];
102+
}[];
103+
}
104+
```

0 commit comments

Comments
 (0)