Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
48 lines (37 loc) · 1.53 KB

File metadata and controls

48 lines (37 loc) · 1.53 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
const nodegit = require("../");
const path = require("path");
const fs = require("fs");
const fileName = "newfile.txt";
const fileContent = "hello world";
const directoryName = "salad/toast/strangerinastrangeland/theresnowaythisexists";
/**
* 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`
**/
(async () => {
const repo = await nodegit.Repository.open(path.resolve(__dirname, "../.git"));
await fs.promises.mkdir(path.join(repo.workdir(), directoryName), {
recursive: true,
});
await fs.promises.writeFile(path.join(repo.workdir(), fileName), fileContent);
await fs.promises.writeFile(
path.join(repo.workdir(), directoryName, fileName),
fileContent
);
const index = await repo.refreshIndex();
// this file is in the root of the directory and doesn't need a full path
await index.addByPath(fileName);
// this file is in a subdirectory and can use a relative path
await index.addByPath(path.posix.join(directoryName, fileName));
// this will write both files to the index
await index.write();
const oid = await index.writeTree();
const parent = await repo.getHeadCommit();
const author = nodegit.Signature.now("Scott Chacon",
"schacon@gmail.com");
const committer = nodegit.Signature.now("Scott A Chacon",
"scott@github.com");
const commitId = await repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
console.log("New Commit: ", commitId);
})();
Morty Proxy This is a proxified and sanitized view of the page, visit original site.