Skip to content

Commit 1cd48d7

Browse files
committed
added commit and revwalk to the library and worked on test repo
1 parent 587f847 commit 1cd48d7

9 files changed

Lines changed: 184 additions & 34 deletions

File tree

example/raw-repo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ var repo = new git2.Repo(),
44
error = new git2.Error();
55

66
// Access existing repository
7-
repo.open('./.git', function(err, path) {
8-
console.log(err, path);
7+
repo.open('.git', function(err, path) {
8+
console.log( error.strError(err), path);
99

1010
var master = new git2.Ref(repo);
11-
repo.lookupRef( master, "refs/heads/master", function( err, ref ) {
12-
console.log(err, ref);
11+
repo.lookupRef( master, 'refs/heads/master', function( err, ref ) {
12+
console.log(err, master);
1313
var oid = new git2.Oid();
1414
master.oid(oid);
1515
console.log( oid.toString(40) );

lib/commit.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var git = require( '../' );
2+
3+
var _Commit = function( repo ) {
4+
var self = {};
5+
6+
// Internal reference to a Git reference
7+
self.commit = commit || new git.git2.Commit( repo );
8+
9+
return self;
10+
};
11+
12+
exports.commit = _Commit;

lib/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var repo = require( './repo.js' ).repo,
22
error = require( './error.js' ).error,
3-
ref = require( './ref.js' ).ref;
4-
//commit = require( 'commit.js' );
3+
ref = require( './ref.js' ).ref,
4+
revwalk = require( './revwalk.js' ).revwalk,
5+
commit = require( './commit.js' ).commit;
56

67
exports.git2 = require( '../build/default/nodegit2.node' );
78
exports.repo = repo;
89
exports.ref = ref;
910
exports.error = error;
10-
//exports.commit = commit.commit;
11+
exports.revwalk = revwalk;
12+
exports.commit = commit;

lib/repo.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ var _Repo = function( path, callback ) {
2121
// Internal reference to a Git repository
2222
self.repo = new git.git2.Repo();
2323

24-
// Find all commits under a given repository and optionally filtered further by a Ref
25-
//self.commits = function( obj ) {
26-
// obj = obj || "HEAD";
27-
28-
// if( commits.length ) {
29-
// return commits;
30-
// }
31-
// else {
32-
//
33-
// }
34-
//};
24+
// Work with a specific branch
25+
self.branch = function( name, callback ) {
26+
var branch = new git.git2.Ref( self.repo );
27+
28+
self.repo.lookupRef( branch, 'refs/heads/'+ name, function() {
29+
var args = Array.prototype.slice.call( arguments );
30+
args[0] = error( args[0] );
31+
32+
callback.apply( branch, args.concat( branch ) );
33+
});
34+
};
3535

3636
// Find a single commit
3737
self.commit = function( sha, callback ) {
3838
var oid = new git.git2.Oid(),
3939
commit = new git.git2.Commit();
4040

41-
if(!callback) { return; }
41+
if( !callback ) { return; }
4242

4343
oid.mkstr( sha );
4444

@@ -54,7 +54,7 @@ var _Repo = function( path, callback ) {
5454
self.find = function( name, callback ) {
5555
var ref = new git.git2.Ref();
5656

57-
if(!callback) { return; }
57+
if( !callback ) { return; }
5858

5959
self.repo.lookupRef( ref, name, function() {
6060
var args = Array.prototype.slice.call( arguments ),
@@ -67,7 +67,7 @@ var _Repo = function( path, callback ) {
6767
};
6868

6969
self.init = function( path, is_bare, callback ) {
70-
if(!callback) { return; }
70+
if( !callback ) { return; }
7171

7272
self.repo.init( path, is_bare, function() {
7373
var args = Array.prototype.slice.call( arguments );
@@ -87,7 +87,7 @@ var _Repo = function( path, callback ) {
8787

8888
// Constructor use
8989
if( path && callback ) {
90-
if(!callback) { return; }
90+
if( !callback ) { return; }
9191

9292
self.repo.open( path, function() {
9393
var args = Array.prototype.slice.call( arguments );

lib/revwalk.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var git = require( '../' );
2+
3+
var _RevWalk = function( repo ) {
4+
var self = {};
5+
6+
// Internal reference to a Git reference
7+
self.revwalk = revwalk || new git.git2.RevWalk( repo );
8+
9+
// Walk will map to the next method
10+
self.walk = function( commit, callback ) {
11+
if( !callback ) { return; }
12+
13+
self.revwalk.push( commit );
14+
15+
revwalk.next( commit, function() {
16+
var args = Array.prototype.slice.call( arguments );
17+
18+
args[0] = error( args[0] );
19+
20+
21+
callback.apply( commit, args.concat( commit ) );
22+
});
23+
};
24+
25+
return self;
26+
};
27+
28+
exports.revwalk = _RevWalk;

test/convenience-repo.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,42 @@ exports.constructor = function( test ){
5353

5454
// Repo::Init
5555
exports.init = function( test ) {
56-
test.expect( 7 );
56+
test.expect( 5 );
5757

5858
// Test for function
5959
helper.testFunction( test.equals, git.repo().init, 'Repo::Init' );
6060

6161
// Test path argument existence
62-
helper.testException( test.ok, function() {
63-
git.repo().init();
64-
}, 'Throw an exception if no path' );
62+
//helper.testException( test.ok, function() {
63+
// git.repo().init();
64+
//}, 'Throw an exception if no path' );
6565

66-
// Test is_bare argument existence
67-
helper.testException( test.ok, function() {
68-
git.repo().init( 'some/path' );
69-
}, 'Throw an exception if no is_bare' );
66+
//// Test is_bare argument existence
67+
//helper.testException( test.ok, function() {
68+
// git.repo().init( 'some/path' );
69+
//}, 'Throw an exception if no is_bare' );
7070

7171
// Cleanup, remove test repo directory - if it exists
7272
rimraf( './test.git', function() {
7373
// Create bare repo and test for creation
7474
git.repo().init( './test.git', true, function( err, path, is_bare ) {
7575
test.equals( 0, err, 'Successfully created bare repository' );
7676
// Verify repo exists
77-
git.repo('./test.git', function(err, path) {
77+
git.repo('./test.git', function(err, path, repo) {
7878
test.equals( 0, err, 'Valid repository created' );
7979
test.equals( true, is_bare, 'Returns valid is_bare value' );
80+
81+
// Test repo branch lookup
82+
console.log( repo, repo.__proto__ );
83+
repo.branch( 'master', function( err ) {
84+
console.log(err);
85+
test.done();
86+
8087
// Cleanup, remove test repo directory
8188
rimraf( './test.git', function() {
82-
test.done();
89+
8390
});
91+
});
8492
});
8593
});
8694
});

test/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ reporter.run(
3737
'raw-repo.js',
3838
'raw-oid.js',
3939
'raw-commit.js',
40-
'raw-error.js'
40+
'raw-error.js',
41+
42+
// TODO:
43+
//'raw-revwalk.js'
4144

4245
// Convenience API
43-
//'convenience-repo.js'
46+
'convenience-repo.js'
4447
]
4548
);

test/raw-revwalk.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
var git = require( '../' ).git2,
2+
rimraf = require( '../vendor/rimraf' ) || require( 'rimraf' );
3+
4+
var testRepo = new git.Repo();
5+
6+
// Helper functions
7+
var helper = {
8+
// Test if obj is a true function
9+
testFunction: function( test, obj, label ) {
10+
// The object reports itself as a function
11+
test( typeof obj, 'function', label +' reports as a function.' );
12+
// This ensures the repo is actually a derivative of the Function [[Class]]
13+
test( toString.call( obj ), '[object Function]', label +' [[Class]] is of type function.' );
14+
},
15+
// Test code and handle exception thrown
16+
testException: function( test, fun, label ) {
17+
try {
18+
fun();
19+
test( false, label );
20+
}
21+
catch (ex) {
22+
test( true, label );
23+
}
24+
}
25+
};
26+
27+
// Oid
28+
exports.constructor = function( test ){
29+
test.expect( 3 );
30+
31+
// Test for function
32+
helper.testFunction( test.equals, git.Commit, 'Commit' );
33+
34+
// Ensure we get an instance of Oid
35+
test.ok( new git.Commit(testRepo) instanceof git.Commit, 'Invocation returns an instance of Commit' );
36+
37+
test.done();
38+
};
39+
40+
// Oid::Mkstr
41+
exports.lookup = function( test ) {
42+
var testOid = new git.Oid(),
43+
testCommit = new git.Commit(testRepo);
44+
45+
testOid.mkstr( 'cb09e99e91d41705197e0fb60823fdc7df776691' );
46+
47+
test.expect( 8 );
48+
49+
// Test for function
50+
helper.testFunction( test.equals, testCommit.lookup, 'Commit::Lookup' );
51+
52+
// Test repo argument existence
53+
helper.testException( test.ok, function() {
54+
testCommit.lookup();
55+
}, 'Throw an exception if no repo' );
56+
57+
// Test oid argument existence
58+
helper.testException( test.ok, function() {
59+
testCommit.lookup( testRepo );
60+
}, 'Throw an exception if no oid' );
61+
62+
// Test callback argument existence
63+
helper.testException( test.ok, function() {
64+
testCommit.lookup( testRepo, testOid );
65+
}, 'Throw an exception if no callback' );
66+
67+
// Test that both arguments result correctly
68+
helper.testException( test.ifError, function() {
69+
testCommit.lookup( testRepo, testOid, function() {} );
70+
}, 'No exception is thrown with proper arguments' );
71+
72+
testRepo.open( './dummyrepo/.git', function( err, path ) {
73+
// Test invalid commit
74+
testOid.mkstr( '100644' );
75+
testCommit.lookup( testRepo, testOid, function( err, details ) {
76+
test.notEqual( 0, err, 'Not a valid commit' );
77+
78+
// Test valid commit
79+
testOid.mkstr( '978feacee2432e67051f2714ec7d28ad80e16908' );
80+
testCommit.lookup( testRepo, testOid, function( err, details ) {
81+
test.equals( 0, err, 'Valid commit');
82+
83+
//test.equals( 'object', typeof details, 'Details is an object' );
84+
85+
//test.equals( 'string', typeof details.message, 'Details message is a String' );
86+
//if(details.message) {
87+
// test.equals( 'initial commit', details.message.trim(), 'Details has correct message' );
88+
//}
89+
90+
testRepo.free();
91+
92+
test.done();
93+
});
94+
});
95+
});
96+
};

test/test.git/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

0 commit comments

Comments
 (0)