-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadd_commit.js
60 lines (54 loc) · 1.44 KB
/
add_commit.js
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
'use strict';
const Git = require("nodegit");
const fs = require('fs');
const cnst = require('./constant');
const common = require('./common');
/**
* This example creates a certain file `newfile.txt`, adds it to the git
* index and commits it to head. Similar to a `git add newfile.txt`
* followed by a `git commit`
**/
let repo;
let index;
let oid;
// api 说明
/*
Repository.open : 打开本地仓库
Repository.createCommit : 创建commit
Index.write : 将Index.addByPath的内容写进Index
Index.writeTree : 配置Index.write一起使用, 将文件写入到tree(git概念)里面
*/
Git.Repository.open(cnst.test_git_path)
.then((repoResult) => (repo = repoResult))
.then(() => {
return fs.writeFileSync(
cnst.add_commit_filename,
'add & commit file content new_again 2'
);
})
.then(() => repo.refreshIndex())
.then((indexResult) => (index = indexResult))
.then(() => index.addByPath(cnst.add_commit_filename))
.then(() => index.write())
.then(() => index.writeTree())
// 注释以下行可以调试只add而不commit的情况
.then((oidResult) => {
oid = oidResult;
return Git.Reference.nameToId(repo, 'HEAD');
})
.then((head) => repo.getCommit(head))
.then((parent) => {
let commit_msg = 'commit msg from add & commit';
return repo.createCommit(
'HEAD',
common.get_commiter_sign(cnst),
common.get_commiter_sign(cnst),
commit_msg,
oid,
[parent]
);
})
.done((commitId) => {
repo.free();
console.log('New Commit: ', commitId);
});