|
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"; |
7 | 7 |
|
8 | 8 | /** |
9 | 9 | * This example creates a certain file `newfile.txt`, adds it to the git |
10 | 10 | * index and commits it to head. Similar to a `git add newfile.txt` |
11 | 11 | * followed by a `git commit` |
12 | 12 | **/ |
13 | 13 |
|
14 | | -var repo; |
15 | | -var index; |
16 | | -var oid; |
17 | 14 |
|
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( |
27 | 24 | path.join(repo.workdir(), directoryName, fileName), |
28 | 25 | fileContent |
29 | 26 | ); |
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 | + |
38 | 30 | // 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); |
42 | 32 | // 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)); |
46 | 34 | // 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", |
61 | 41 | "schacon@gmail.com"); |
62 | | - var committer = nodegit.Signature.now("Scott A Chacon", |
| 42 | + const committer = nodegit.Signature.now("Scott A Chacon", |
63 | 43 | "scott@github.com"); |
64 | 44 |
|
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 | + |
68 | 47 | console.log("New Commit: ", commitId); |
69 | | -}); |
| 48 | +})(); |
0 commit comments