Skip to content

Commit f0e254f

Browse files
committed
update examples -> walk-tree walk-history remove-and-commit
1 parent c409a98 commit f0e254f

3 files changed

Lines changed: 64 additions & 85 deletions

File tree

example/remove-and-commit.js

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,42 @@
1-
var git = require('../'),
1+
var nodegit = require('../'),
22
path = require('path'),
3-
fileName = 'newfile.txt'
4-
;
3+
fileName = 'newfile.txt';
54

65
/**
7-
* This example deletes a certain file `newfile.txt`, removes it from the git index and
6+
* This example deletes a certain file `newfile.txt`, removes it from the git index and
87
* commits it to head. Similar to a `git rm newfile.txt` followed by a `git commit`
98
* Use add-and-commit.js to create the file first.
109
**/
1110

12-
//open a git repo
13-
git.Repo.open(path.resolve(__dirname, '../.git'), function(openReporError, repo) {
14-
if (openReporError) throw openReporError;
11+
var _repository;
12+
var _index;
13+
var _oid;
1514

15+
//open a git repo
16+
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
17+
_repository = repo;
18+
return repo.openIndex();
19+
}).then(function(index){
20+
_index = index;
21+
return _index.read();
22+
}).then(function() {
1623
//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-
});
24+
_index.removeByPath(fileName);
25+
return _index.write();
26+
}).then(function() {
27+
return _index.writeTree();
28+
}).then(function(oid) {
29+
_oid = oid;
30+
return nodegit.Refs.nameToId(_repository, "HEAD");
31+
}).then(function(head) {
32+
return _repository.getCommit(head);
33+
}).then(function(parent) {
34+
var author = nodegit.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60);
35+
var committer = nodegit.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90);
36+
37+
return _repository.createCommit('HEAD', author, committer, "message", _oid, [parent]);
38+
}).then(function(commitId) {
39+
// the file is removed from the git repo, use fs.unlink now to remove it
40+
// from the filesystem.
41+
console.log("New Commit:", commitId.allocfmt());
42+
}).done();

example/walk-history.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
var git = require('../'),
1+
var nodegit = require('../'),
22
path = require('path'),
3-
sort = git.RevWalk.Sort;
3+
sort = nodegit.Revwalk.Sort;
44

55
// This code walks the history of the master branch and prints results
66
// that look very similar to calling `git log` from the command line
77

8-
git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
9-
if (error) throw error;
8+
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
9+
return repo.getMaster();
10+
}).then(function(branch){
11+
// History returns an event.
12+
var history = branch.history(sort.Time);
1013

11-
repo.getMaster(function(error, branch) {
12-
if (error) throw error;
13-
14-
// History returns an event.
15-
var history = branch.history(sort.Time);
16-
17-
// History emits 'commit' event for each commit in the branch's history
18-
history.on('commit', function(commit) {
19-
console.log('commit ' + commit.sha());
20-
console.log('Author:', commit.author().name() + ' <' + commit.author().email() + '>');
21-
console.log('Date:', commit.date());
22-
console.log('\n ' + commit.message());
23-
});
24-
25-
// Don't forget to call `start()`!
26-
history.start();
14+
// History emits 'commit' event for each commit in the branch's history
15+
history.on('commit', function(commit) {
16+
console.log('commit ' + commit.sha());
17+
console.log('Author:', commit.author().name() + ' <' + commit.author().email() + '>');
18+
console.log('Date:', commit.date());
19+
console.log('\n ' + commit.message());
2720
});
28-
});
21+
22+
// Don't forget to call `start()`!
23+
history.start();
24+
}).done();

example/walk-tree.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
var git = require('../'),
1+
var nodegit = require('../'),
22
path = require('path');
33

44
// A `tree` in git is typically a representation of the filesystem at
55
// a revision. A tree has a set of entries, each entry being either a
66
// tree (directory), or a file.
77

8-
git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
9-
if (error) throw error;
10-
11-
repo.getMaster(function(error, branch) {
12-
if (error) throw error;
13-
14-
branch.getTree(function(error, tree) {
15-
if (error) throw error;
16-
17-
// `walk()` returns an event.
18-
var walker = tree.walk();
19-
walker.on('entry', function(entry) {
20-
console.log(entry.path());
21-
});
22-
23-
// Don't forget to call `start()`!
24-
walker.start();
25-
});
8+
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
9+
return repo.getMaster();
10+
}).then(function(branch) {
11+
return branch.getTree();
12+
}).then(function(tree) {
13+
// `walk()` returns an event.
14+
var walker = tree.walk();
15+
walker.on('entry', function(entry) {
16+
console.log(entry.path());
2617
});
27-
});
18+
19+
// Don't forget to call `start()`!
20+
walker.start();
21+
}).done();

0 commit comments

Comments
 (0)