Skip to content

Commit c1acc68

Browse files
authored
Merge pull request #1802 from julien-c/syntax_examples
First stab at #1800 (async/await in examples)
2 parents ae7cc34 + 085c8ea commit c1acc68

7 files changed

Lines changed: 417 additions & 510 deletions

File tree

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

examples/create-new-repo.js

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,32 @@
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 repoDir = "../../newRepo";
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 repoDir = "../newRepo";
77

8-
var repository;
9-
var index;
108

11-
fse.ensureDir(path.resolve(__dirname, repoDir))
12-
.then(function() {
13-
return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
14-
})
15-
.then(function(repo) {
16-
repository = repo;
17-
return fse.writeFile(path.join(repository.workdir(), fileName), fileContent);
18-
})
19-
.then(function(){
20-
return repository.refreshIndex();
21-
})
22-
.then(function(idx) {
23-
index = idx;
24-
})
25-
.then(function() {
26-
return index.addByPath(fileName);
27-
})
28-
.then(function() {
29-
return index.write();
30-
})
31-
.then(function() {
32-
return index.writeTree();
33-
})
34-
.then(function(oid) {
35-
var author = nodegit.Signature.now("Scott Chacon",
9+
(async () => {
10+
await fs.promises.mkdir(path.resolve(__dirname, repoDir), {
11+
recursive: true,
12+
});
13+
const repo = await nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
14+
await fs.promises.writeFile(path.join(repo.workdir(), fileName), fileContent);
15+
const index = await repo.refreshIndex();
16+
await index.addByPath(fileName);
17+
await index.write();
18+
19+
const oid = await index.writeTree();
20+
21+
const author = nodegit.Signature.now("Scott Chacon",
3622
"schacon@gmail.com");
37-
var committer = nodegit.Signature.now("Scott A Chacon",
23+
const committer = nodegit.Signature.now("Scott A Chacon",
3824
"scott@github.com");
3925

4026
// Since we're creating an inital commit, it has no parents. Note that unlike
4127
// normal we don't get the head either, because there isn't one yet.
42-
return repository.createCommit("HEAD", author, committer, "message", oid, []);
43-
})
44-
.done(function(commitId) {
28+
const commitId = await repo.createCommit("HEAD", author, committer, "message", oid, []);
4529
console.log("New Commit: ", commitId);
46-
});
30+
})();
31+
32+

examples/details-for-tree-entry.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
var nodegit = require("../");
2-
var path = require("path");
1+
const nodegit = require("../");
2+
const path = require("path");
33

44
/**
55
* This shows how to get details from a tree entry or a blob
66
**/
77

8-
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
9-
.then(function(repo) {
10-
return repo.getTree("e1b0c7ea57bfc5e30ec279402a98168a27838ac9")
11-
.then(function(tree) {
12-
var treeEntry = tree.entryByIndex(0);
8+
(async () => {
9+
const repo = await nodegit.Repository.open(path.resolve(__dirname, "../.git"));
10+
const tree = await repo.getTree("e1b0c7ea57bfc5e30ec279402a98168a27838ac9");
11+
const treeEntry = tree.entryByIndex(0);
12+
13+
// Tree entry doesn't have any data associated with the actual entry
14+
// To get that we need to get the index entry that this points to
15+
const index = await repo.refreshIndex();
16+
const indexEntry = index.getByPath(treeEntry.path());
1317

14-
// Tree entry doesn't have any data associated with the actual entry
15-
// To get that we need to get the index entry that this points to
16-
return repo.refreshIndex().then(function(index) {
17-
var indexEntry = index.getByPath(treeEntry.path());
18+
// With the index entry we can now view the details for the tree entry
19+
console.log("Entry path: " + indexEntry.path);
20+
console.log("Entry time in seconds: " + indexEntry.mtime.seconds());
21+
console.log("Entry oid: " + indexEntry.id.toString());
22+
console.log("Entry size: " + indexEntry.fileSize);
23+
24+
console.log("Done!");
25+
})();
1826

19-
// With the index entry we can now view the details for the tree entry
20-
console.log("Entry path: " + indexEntry.path);
21-
console.log("Entry time in seconds: " + indexEntry.mtime.seconds());
22-
console.log("Entry oid: " + indexEntry.id.toString());
23-
console.log("Entry size: " + indexEntry.fileSize);
24-
});
25-
});
26-
})
27-
.done(function() {
28-
console.log("Done!");
29-
});

examples/diff-commits.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
var nodegit = require("../");
2-
var path = require("path");
1+
const nodegit = require("../");
2+
const path = require("path");
33

44
// This code examines the diffs between a particular commit and all of its
55
// parents. Since this commit is not a merge, it only has one parent. This is
66
// similar to doing `git show`.
77

8-
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
9-
.then(function(repo) {
10-
return repo.getCommit("59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5");
11-
})
12-
.then(function(commit) {
8+
(async () => {
9+
const repo = await nodegit.Repository.open(path.resolve(__dirname, "../.git"))
10+
const commit = await repo.getCommit("59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5");
1311
console.log("commit " + commit.sha());
14-
console.log("Author:", commit.author().name() +
15-
" <" + commit.author().email() + ">");
12+
console.log(
13+
"Author:", commit.author().name() +
14+
" <" + commit.author().email() + ">"
15+
);
1616
console.log("Date:", commit.date());
1717
console.log("\n " + commit.message());
1818

19-
return commit.getDiff();
20-
})
21-
.done(function(diffList) {
22-
diffList.forEach(function(diff) {
23-
diff.patches().then(function(patches) {
24-
patches.forEach(function(patch) {
25-
patch.hunks().then(function(hunks) {
26-
hunks.forEach(function(hunk) {
27-
hunk.lines().then(function(lines) {
28-
console.log("diff", patch.oldFile().path(),
29-
patch.newFile().path());
30-
console.log(hunk.header().trim());
31-
lines.forEach(function(line) {
32-
console.log(String.fromCharCode(line.origin()) +
33-
line.content().trim());
34-
});
35-
});
36-
});
37-
});
38-
});
39-
});
40-
});
41-
});
19+
const diffList = await commit.getDiff();
20+
for (const diff of diffList) {
21+
const patches = await diff.patches();
22+
for (const patch of patches) {
23+
const hunks = await patch.hunks();
24+
for (const hunk of hunks) {
25+
const lines = await hunk.lines();
26+
console.log(
27+
"diff",
28+
patch.oldFile().path(),
29+
patch.newFile().path()
30+
);
31+
console.log(hunk.header().trim());
32+
for (const line of lines) {
33+
console.log(
34+
String.fromCharCode(line.origin()) +
35+
line.content().trim()
36+
);
37+
}
38+
}
39+
}
40+
}
41+
})();

0 commit comments

Comments
 (0)