Skip to content

Commit 1f173a4

Browse files
author
John Haley
committed
Updated add-and-commit and added a create commit test
1 parent 40ecc64 commit 1f173a4

File tree

5 files changed

+151
-70
lines changed

5 files changed

+151
-70
lines changed

example/add-and-commit.js

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
1-
var git = require('../'),
2-
path = require('path'),
3-
fs = require('fs'),
4-
fileName = 'newfile.txt',
5-
fileContent = 'hello world'
6-
;
1+
var git = 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+
git.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 git.Refs.nameToId(repo, 'HEAD');
43+
})
44+
.then(function(head) {
45+
return repo.getCommit(head);
46+
})
47+
.then(function(parentResult) {
48+
return Promise.all([
49+
git.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60),
50+
git.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90)
51+
])
52+
})
53+
.then(function(signatures){
54+
var author = signatures[0];
55+
var committer = signatures[1];
56+
57+
return repo.createCommit('HEAD', author, committer, 'message', oid, [parent]);
58+
})
59+
.done(function(commitId) {
60+
console.log('New Commit: ', commitId);
5961
});

generate/descriptor.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@
116116
"functions": {
117117
"git_commit_create": {
118118
"args": {
119+
"id": {
120+
"isReturn": true
121+
},
122+
"message_encoding": {
123+
"isOptional": true
124+
},
119125
"parents": {
120126
"cType": "const git_commit **",
121127
"cppClassName": "Array",
@@ -483,6 +489,9 @@
483489
"git_index_add_all": {
484490
"ignore": true
485491
},
492+
"git_index_add_bypath": {
493+
"jsFunctionName": "addByPath"
494+
},
486495
"git_index_conflict_get": {
487496
"ignore": true
488497
},
@@ -510,9 +519,6 @@
510519
"git_index_update_all": {
511520
"ignore": true
512521
},
513-
"git_index_write_tree": {
514-
"ignore": true
515-
},
516522
"git_index_write_tree_to": {
517523
"ignore": true
518524
}
@@ -990,13 +996,6 @@
990996
}
991997
}
992998
},
993-
"signature": {
994-
"functions": {
995-
"git_signature_new": {
996-
"ignore": true
997-
}
998-
}
999-
},
1000999
"smart": {
10011000
"functions": {
10021001
"git_smart_subtransport_git": {

lib/repository.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,11 @@ Repository.prototype.createCommit = function(
215215

216216
var createCommit = null;
217217
var commit = this;
218+
var repo = this;
218219

219220
if (tree instanceof Tree) {
220-
commit = Commit.create.call(
221-
this,
221+
commit = Commit.create(
222+
repo,
222223
updateRef,
223224
author,
224225
committer,
@@ -230,8 +231,8 @@ Repository.prototype.createCommit = function(
230231
);
231232
} else {
232233
createCommit = this.getTree(tree).then(function(tree) {
233-
return Commit.create.call(
234-
commit,
234+
return Commit.create(
235+
repo,
235236
updateRef,
236237
author,
237238
committer,

test/tests/commit.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var assert = require("assert");
22
var path = require("path");
3+
var Promise = require("nodegit-promise");
4+
var promisify = require("promisify-node");
5+
var fse = promisify(require("fs-extra"));
36

47
var NodeGit = require("../../");
58
var Repository = NodeGit.Repository;
@@ -46,6 +49,71 @@ describe("Commit", function() {
4649
assert.equal(this.commit.timeOffset(), 780);
4750
});
4851

52+
it("can create a commit", function() {
53+
var expectedCommitId = "315e77328ef596f3bc065d8ac6dd2c72c09de8a5";
54+
var fileName = "newfile.txt";
55+
var fileContent = "hello world";
56+
57+
var repo;
58+
var index;
59+
var treeOid;
60+
var parent;
61+
62+
return NodeGit.Repository.open(reposPath)
63+
.then(function(repoResult) {
64+
repo = repoResult;
65+
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
66+
})
67+
.then(function() {
68+
return repo.openIndex();
69+
})
70+
.then(function(indexResult) {
71+
index = indexResult;
72+
return index.read(1);
73+
})
74+
.then(function() {
75+
return index.addByPath(fileName);
76+
})
77+
.then(function() {
78+
return index.write();
79+
})
80+
.then(function() {
81+
return index.writeTree();
82+
})
83+
.then(function(oidResult) {
84+
treeOid = oidResult;
85+
return NodeGit.Refs.nameToId(repo, "HEAD");
86+
})
87+
.then(function(head) {
88+
return repo.getCommit(head);
89+
})
90+
.then(function(parentResult) {
91+
parent = parentResult;
92+
93+
return Promise.all([
94+
NodeGit.Signature.create("Foo Bar", "foo@bar.com", 123456789, 60),
95+
NodeGit.Signature.create("Foo A Bar", "foo@bar.com", 987654321, 90)
96+
]);
97+
})
98+
.then(function(signatures){
99+
var author = signatures[0];
100+
var committer = signatures[1];
101+
102+
return repo.createCommit(
103+
"HEAD",
104+
author,
105+
committer,
106+
"message",
107+
treeOid,
108+
[parent]);
109+
})
110+
.then(function(commitId) {
111+
assert.equal(expectedCommitId, commitId);
112+
}, function(rejected) {
113+
assert.fail();
114+
});
115+
});
116+
49117
describe("author", function() {
50118
before(function() {
51119
this.author = this.commit.author();

test/tests/refs.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
var assert = require("assert");
22
var path = require("path");
3+
var promisify = require("promisify-node");
4+
5+
// Have to wrap exec, since it has a weird callback signature.
6+
var exec = promisify(function(command, opts, callback) {
7+
return require("child_process").exec(command, opts, callback);
8+
});
39

410
describe("Refs", function() {
511
var reposPath = path.resolve("test/repos/workdir/.git");
@@ -10,12 +16,17 @@ describe("Refs", function() {
1016
before(function() {
1117
var test = this;
1218

13-
return Repository.open(reposPath).then(function(repository) {
19+
return exec("git reset --hard origin/master", {cwd: "test/repos/workdir"})
20+
.then(function() {
21+
return Repository.open(reposPath);
22+
})
23+
.then(function(repository) {
1424
test.repository = repository;
1525

16-
return repository.getReference("refs/heads/master").then(function(refs) {
17-
test.refs = refs;
18-
});
26+
return repository.getReference("refs/heads/master");
27+
})
28+
.then(function(refs) {
29+
test.refs = refs;
1930
});
2031
});
2132

0 commit comments

Comments
 (0)