Skip to content

Commit db9ba98

Browse files
committed
new add and remove file example
1 parent 06f1df9 commit db9ba98

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

example/add-and-commit.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var git = require('../'),
2+
path = require('path'),
3+
fs = require('fs'),
4+
fileName = 'newfile.txt',
5+
fileContent = 'hello world'
6+
;
7+
8+
/**
9+
* This example creates a certain file `newfile.txt`, adds it to the git index and
10+
* commits it to head. Similar to a `git add newfile.txt` followed by a `git commit`
11+
**/
12+
13+
//open a git repo
14+
git.Repo.open(path.resolve(__dirname, '../.git'), function(openReporError, repo) {
15+
if (openReporError) throw openReporError;
16+
17+
//create the file in the repo's workdir
18+
fs.writeFile(path.join(repo.workdir(), fileName), fileContent, function(writeError) {
19+
if (writeError) throw writeError;
20+
21+
//add the file to the index...
22+
repo.openIndex(function(openIndexError, index) {
23+
if (openIndexError) throw openIndexError;
24+
25+
index.read(function(readError) {
26+
if (readError) throw readError;
27+
28+
index.addByPath(fileName, function(addByPathError) {
29+
if (addByPathError) throw addByPathError;
30+
31+
index.write(function(writeError) {
32+
if (writeError) throw writeError;
33+
34+
index.writeTree(function(writeTreeError, oid) {
35+
if (writeTreeError) throw writeTreeError;
36+
37+
//get HEAD
38+
git.Reference.oidForName(repo, 'HEAD', function(oidForName, head) {
39+
if (oidForName) throw oidForName;
40+
41+
//get latest commit (will be the parent commit)
42+
repo.getCommit(head, function(getCommitError, parent) {
43+
if (getCommitError) throw getCommitError;
44+
var author = git.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60);
45+
var committer = git.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90);
46+
47+
//commit
48+
repo.createCommit('HEAD', author, committer, 'message', oid, [parent], function(error, commitId) {
49+
console.log("New Commit:", commitId.sha());
50+
});
51+
});
52+
});
53+
});
54+
});
55+
});
56+
});
57+
});
58+
});
59+
});

example/remove-and-commit.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var git = require('../'),
2+
path = require('path'),
3+
fileName = 'newfile.txt'
4+
;
5+
6+
/**
7+
* This example deletes a certain file `newfile.txt`, removes it from the git index and
8+
* commits it to head. Similar to a `git rm newfile.txt` followed by a `git commit`
9+
* Use add-and-commit.js to create the file first.
10+
**/
11+
12+
//open a git repo
13+
git.Repo.open(path.resolve(__dirname, '../.git'), function(openReporError, repo) {
14+
if (openReporError) throw openReporError;
15+
16+
//remove the file from the index...
17+
repo.openIndex(function(openIndexError, index) {
18+
if (openIndexError) throw openIndexError;
19+
20+
index.read(function(readError) {
21+
if (readError) throw readError;
22+
23+
index.removeByPath(fileName);
24+
25+
index.write(function(writeError) {
26+
if (writeError) throw writeError;
27+
28+
index.writeTree(function(writeTreeError, oid) {
29+
if (writeTreeError) throw writeTreeError;
30+
31+
//get HEAD
32+
git.Reference.oidForName(repo, 'HEAD', function(oidForName, head) {
33+
if (oidForName) throw oidForName;
34+
35+
//get latest commit (will be the parent commit)
36+
repo.getCommit(head, function(getCommitError, parent) {
37+
if (getCommitError) throw getCommitError;
38+
var author = git.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60);
39+
var committer = git.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90);
40+
41+
//commit
42+
repo.createCommit('HEAD', author, committer, 'message', oid, [parent], function(error, commitId) {
43+
console.log("New Commit:", commitId.sha());
44+
// the file is removed from the git repo, use fs.unlink now to remove it
45+
// from the filesystem.
46+
});
47+
});
48+
});
49+
});
50+
});
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)