Skip to content

Commit e308618

Browse files
committed
Merge pull request nodegit#276 from nodegit/update-examples
Updated examples and added some more tests
2 parents 40ecc64 + 46cdc7d commit e308618

36 files changed

Lines changed: 923 additions & 719 deletions

example/add-and-commit.js

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,55 @@
1-
var git = require('../'),
2-
path = require('path'),
3-
fs = require('fs'),
4-
fileName = 'newfile.txt',
5-
fileContent = 'hello world'
6-
;
1+
var nodegit = require('../');
2+
var path = require('path');
3+
var Promise = require('nodegit-promise');
4+
var promisify = require('promisify-node');
5+
var fse = promisify(require('fs-extra'));
6+
var fileName = 'newfile.txt';
7+
var fileContent = 'hello world';
78

89
/**
910
* This example creates a certain file `newfile.txt`, adds it to the git index and
1011
* commits it to head. Similar to a `git add newfile.txt` followed by a `git commit`
1112
**/
1213

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-
});
14+
var repo;
15+
var index;
16+
var oid;
17+
var parent;
18+
19+
nodegit.Repository.open(path.resolve(__dirname, '../.git'))
20+
.then(function(repoResult) {
21+
repo = repoResult;
22+
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
23+
})
24+
.then(function() {
25+
return repo.openIndex();
26+
})
27+
.then(function(indexResult) {
28+
index = indexResult;
29+
return index.read(1);
30+
})
31+
.then(function() {
32+
return index.addByPath(fileName);
33+
})
34+
.then(function() {
35+
return index.write();
36+
})
37+
.then(function() {
38+
return index.writeTree();
39+
})
40+
.then(function(oidResult) {
41+
oid = oidResult;
42+
return nodegit.Refs.nameToId(repo, 'HEAD');
43+
})
44+
.then(function(head) {
45+
return repo.getCommit(head);
46+
})
47+
.then(function(parent) {
48+
var author = nodegit.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60);
49+
var committer = nodegit.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90);
50+
51+
return repo.createCommit('HEAD', author, committer, 'message', oid, [parent]);
52+
})
53+
.done(function(commitId) {
54+
console.log('New Commit: ', commitId);
5955
});

example/clone.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
var git = require('../'),
2-
rimraf = require('rimraf'),
3-
path = "/tmp/nodegit-clone-demo";
1+
var nodegit = require('../');
2+
var rimraf = require('rimraf');
3+
var path = "/tmp/nodegit-clone-demo";
44

55
rimraf(path, function() {
6-
git.Repo.clone("https://github.com/nodegit/nodegit.git", path, null, function(error, repo) {
7-
if (error) throw error;
6+
var entry;
87

9-
repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5', function(error, commit) {
10-
if (error) throw error;
11-
12-
commit.getEntry('README.md', function(error, entry) {
13-
if (error) throw error;
14-
15-
entry.getBlob(function(error, blob) {
16-
if (error) throw error;
17-
18-
console.log(entry.name(), entry.sha(), blob.size() + 'b');
19-
console.log('========================================================\n\n');
20-
var firstTenLines = blob.toString().split('\n').slice(0, 10).join('\n');
21-
console.log(firstTenLines);
22-
console.log('...');
23-
});
24-
});
25-
});
8+
nodegit.Clone.clone(
9+
"https://github.com/nodegit/nodegit.git",
10+
path,
11+
{ ignoreCertErrors: 1})
12+
.then(function(repo) {
13+
return repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5');
14+
})
15+
.then(function(commit) {
16+
return commit.getEntry('README.md')
17+
})
18+
.then(function(entryResult) {
19+
entry = entryResult;
20+
return entry.getBlob();
21+
})
22+
.done(function(blob) {
23+
console.log(entry.filename(), entry.sha(), blob.rawsize() + 'b');
24+
console.log('========================================================\n\n');
25+
var firstTenLines = blob.toString().split('\n').slice(0, 10).join('\n');
26+
console.log(firstTenLines);
27+
console.log('...');
2628
});
27-
});
29+
});

example/diff-commits.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
var git = require('../'),
2-
path = require('path');
1+
var nodegit = require('../');
2+
var 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-
git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
9-
if (error) throw error;
8+
nodegit.Repository.open(path.resolve(__dirname, '../.git'))
9+
.then(function(repo) {
10+
return repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5');
11+
})
12+
.then(function(commit) {
13+
console.log('commit ' + commit.sha());
14+
console.log('Author:', commit.author().name() + ' <' + commit.author().email() + '>');
15+
console.log('Date:', commit.date());
16+
console.log('\n ' + commit.message());
1017

11-
repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5', function(error, commit) {
12-
if (error) throw error;
13-
14-
console.log('commit ' + commit.sha());
15-
console.log('Author:', commit.author().name() + ' <' + commit.author().email() + '>');
16-
console.log('Date:', commit.date());
17-
console.log('\n ' + commit.message());
18-
19-
commit.getDiff(function(error, diffList) {
20-
if (error) throw error;
21-
22-
diffList.forEach(function(diff) {
23-
diff.patches().forEach(function(patch) {
24-
console.log("diff", patch.oldFile().path(), patch.newFile().path());
25-
patch.hunks().forEach(function(hunk) {
26-
console.log(hunk.header().trim());
27-
hunk.lines().forEach(function(line) {
28-
console.log(String.fromCharCode(line.lineOrigin) + line.content.trim());
29-
});
30-
});
18+
return commit.getDiff();
19+
})
20+
.done(function(diffList) {
21+
diffList.forEach(function(diff) {
22+
diff.patches().forEach(function(patch) {
23+
console.log("diff", patch.oldFile().path(), patch.newFile().path());
24+
patch.hunks().forEach(function(hunk) {
25+
console.log(hunk.header().trim());
26+
hunk.lines().forEach(function(line) {
27+
console.log(String.fromCharCode(line.origin()) + line.content().trim());
3128
});
3229
});
3330
});

example/fetch.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
var git = require('../'),
2-
path = require('path');
3-
4-
git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
5-
if (error) throw error;
6-
7-
var remote = repo.getRemote("origin");
8-
remote.connect(0, function(error) {
9-
if (error) throw error;
10-
11-
remote.download(null, function(error) {
12-
if (error) throw error;
13-
14-
console.log("It worked!");
15-
})
16-
});
1+
var nodegit = require('../');
2+
var path = require('path');
3+
4+
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
5+
return nodegit.Remote.load(repo, "origin");
6+
}).then(function(remote) {
7+
remote.connect(0);
8+
return remote.download();
9+
})
10+
.done(function() {
11+
console.log("It worked!");
1712
});

0 commit comments

Comments
 (0)