Skip to content

Commit eebd0ea

Browse files
committed
Made all the examples work
1 parent 1273fff commit eebd0ea

38 files changed

+280
-316
lines changed

example/apps/git_profanity_check.js

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,36 @@
55
// Script to detect cursewords in commit messages and provide the
66
// offending commit sha's.
77
// vim: ft=javascript
8-
var git = require( 'nodegit' );
8+
var git = require('../../');
99

10-
var curses = [ 'add', 'swears', 'here' ]
11-
, path = './.git'
12-
, branch = 'master'
13-
, reCurse = new RegExp('\\b(?:' + curses.join('|') + ')\\b', 'gi');
10+
var curses = ['add', 'swears', 'here'],
11+
path = './.git',
12+
branchName = 'master',
13+
reCurse = new RegExp('\\b(?:' + curses.join('|') + ')\\b', 'gi');
1414

1515

16-
// Set git path
17-
if ( process.argv.length < 3 ) {
18-
console.log( 'No path passed as argument, defaulting to ./.git' );
19-
}
20-
else {
16+
if (process.argv.length < 3) {
17+
console.log('No git path passed as argument, defaulting to ./.git');
18+
} else {
2119
path = process.argv[2];
2220

23-
// Set repo branch
24-
if ( process.argv.length < 4 ) {
25-
console.log( 'No branch passed as argument, defaulting to master' );
26-
}
27-
else {
28-
branch = process.argv[3];
21+
if (process.argv.length < 4) {
22+
console.log('No repo branchName passed as argument, defaulting to master');
23+
} else {
24+
branchName = process.argv[3];
2925
}
3026
}
3127

32-
// Open repository
33-
git.repo( path, function( err, repo ) {
34-
if ( err ) {
35-
throw new Error( err );
36-
}
28+
git.Repo.open(path, function(error, repo) {
29+
if (error) throw error;
3730

38-
// Open branch
39-
repo.branch( branch, function( err, branch ) {
40-
if ( err ) {
41-
throw new Error( err );
42-
}
31+
repo.getBranch(branchName, function(error, branch) {
32+
if (error) throw error;
4333

44-
// Iterate history
4534
var history = branch.history();
46-
history.on( 'commit', function( idx, commit ) {
47-
// Check commit messages first
48-
if ( reCurse.test(commit.message) ) {
49-
console.log( 'Curse detected in commit', commit.sha, 'message' );
50-
return;
51-
}
52-
});
35+
history.on('commit', function(commit) {
36+
if (reCurse.test(commit.message()))
37+
console.log('Curse detected in commit', commit.sha(), 'message', commit.message());
38+
}).start();
5339
});
5440
});

example/convenience-commit.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

example/convenience-repo.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

example/convenience-tree.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

example/diff-commits.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var git = require('../'),
2+
path = require('path');
3+
4+
// This code examines the diffs between a particular commit and all of its
5+
// parents. Since this commit is not a merge, it only has one parent. This is
6+
// similar to doing `git show`.
7+
8+
git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
9+
if (error) throw error;
10+
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+
});
31+
});
32+
});
33+
});
34+
});
35+
});

example/general.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var git = require('../index.js');
1+
var git = require('../'),
2+
path = require('path');
23

34
// **nodegit** is a javascript library for node.js that wraps libgit2, a
45
// pure C implementation of the Git core. It provides an asynchronous
@@ -15,7 +16,7 @@ var git = require('../index.js');
1516
// Nearly, all git operations in the context of a repository.
1617
// To open a repository,
1718

18-
git.Repo.open('.git', function(error, repo) {
19+
git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
1920
// For all of the following examples, error-handling will be performed in
2021
// this naive way:
2122
if (error) throw error;
@@ -201,7 +202,7 @@ git.Repo.open('.git', function(error, repo) {
201202

202203
// You can also access tree entries by path if you know the path of the
203204
// entry you're looking for.
204-
tree.getFile("example/general.js", function(error, entry) {
205+
tree.getEntry("example/general.js", function(error, entry) {
205206
if (error) throw error;
206207

207208
// Entries which are files have blobs associated with them:

example/raw-blob.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

example/raw-commit.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

example/raw-error.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/raw-oid.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)