Skip to content

Commit fb56e12

Browse files
committed
Update add-and-commit.js
1 parent 02c2147 commit fb56e12

File tree

1 file changed

+30
-51
lines changed

1 file changed

+30
-51
lines changed

examples/add-and-commit.js

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,48 @@
1-
var nodegit = require("../");
2-
var path = require("path");
3-
var fse = require("fs-extra");
4-
var fileName = "newfile.txt";
5-
var fileContent = "hello world";
6-
var directoryName = "salad/toast/strangerinastrangeland/theresnowaythisexists";
1+
const nodegit = require("../");
2+
const path = require("path");
3+
const fs = require("fs");
4+
const fileName = "newfile.txt";
5+
const fileContent = "hello world";
6+
const directoryName = "salad/toast/strangerinastrangeland/theresnowaythisexists";
77

88
/**
99
* This example creates a certain file `newfile.txt`, adds it to the git
1010
* index and commits it to head. Similar to a `git add newfile.txt`
1111
* followed by a `git commit`
1212
**/
1313

14-
var repo;
15-
var index;
16-
var oid;
1714

18-
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
19-
.then(function(repoResult) {
20-
repo = repoResult;
21-
return fse.ensureDir(path.join(repo.workdir(), directoryName));
22-
}).then(function(){
23-
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
24-
})
25-
.then(function() {
26-
return fse.writeFile(
15+
(async () => {
16+
const repo = await nodegit.Repository.open(path.resolve(__dirname, "../.git"));
17+
18+
await fs.promises.mkdir(path.join(repo.workdir(), directoryName), {
19+
recursive: true,
20+
});
21+
22+
await fs.promises.writeFile(path.join(repo.workdir(), fileName), fileContent);
23+
await fs.promises.writeFile(
2724
path.join(repo.workdir(), directoryName, fileName),
2825
fileContent
2926
);
30-
})
31-
.then(function() {
32-
return repo.refreshIndex();
33-
})
34-
.then(function(indexResult) {
35-
index = indexResult;
36-
})
37-
.then(function() {
27+
28+
const index = await repo.refreshIndex();
29+
3830
// this file is in the root of the directory and doesn't need a full path
39-
return index.addByPath(fileName);
40-
})
41-
.then(function() {
31+
await index.addByPath(fileName);
4232
// this file is in a subdirectory and can use a relative path
43-
return index.addByPath(path.posix.join(directoryName, fileName));
44-
})
45-
.then(function() {
33+
await index.addByPath(path.posix.join(directoryName, fileName));
4634
// this will write both files to the index
47-
return index.write();
48-
})
49-
.then(function() {
50-
return index.writeTree();
51-
})
52-
.then(function(oidResult) {
53-
oid = oidResult;
54-
return nodegit.Reference.nameToId(repo, "HEAD");
55-
})
56-
.then(function(head) {
57-
return repo.getCommit(head);
58-
})
59-
.then(function(parent) {
60-
var author = nodegit.Signature.now("Scott Chacon",
35+
await index.write();
36+
37+
const oid = await index.writeTree();
38+
39+
const parent = await repo.getHeadCommit();
40+
const author = nodegit.Signature.now("Scott Chacon",
6141
"schacon@gmail.com");
62-
var committer = nodegit.Signature.now("Scott A Chacon",
42+
const committer = nodegit.Signature.now("Scott A Chacon",
6343
"scott@github.com");
6444

65-
return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
66-
})
67-
.done(function(commitId) {
45+
const commitId = await repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
46+
6847
console.log("New Commit: ", commitId);
69-
});
48+
})();

0 commit comments

Comments
 (0)