Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 48 additions & 52 deletions example/add-and-commit.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,55 @@
var git = require('../'),
path = require('path'),
fs = require('fs'),
fileName = 'newfile.txt',
fileContent = 'hello world'
;
var nodegit = require('../');
var path = require('path');
var Promise = require('nodegit-promise');
var promisify = require('promisify-node');
var fse = promisify(require('fs-extra'));
var fileName = 'newfile.txt';
var fileContent = 'hello world';

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

//open a git repo
git.Repo.open(path.resolve(__dirname, '../.git'), function(openReporError, repo) {
if (openReporError) throw openReporError;

//create the file in the repo's workdir
fs.writeFile(path.join(repo.workdir(), fileName), fileContent, function(writeError) {
if (writeError) throw writeError;

//add the file to the index...
repo.openIndex(function(openIndexError, index) {
if (openIndexError) throw openIndexError;

index.read(function(readError) {
if (readError) throw readError;

index.addByPath(fileName, function(addByPathError) {
if (addByPathError) throw addByPathError;

index.write(function(writeError) {
if (writeError) throw writeError;

index.writeTree(function(writeTreeError, oid) {
if (writeTreeError) throw writeTreeError;

//get HEAD
git.Reference.oidForName(repo, 'HEAD', function(oidForName, head) {
if (oidForName) throw oidForName;

//get latest commit (will be the parent commit)
repo.getCommit(head, function(getCommitError, parent) {
if (getCommitError) throw getCommitError;
var author = git.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60);
var committer = git.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90);

//commit
repo.createCommit('HEAD', author, committer, 'message', oid, [parent], function(error, commitId) {
console.log("New Commit:", commitId.sha());
});
});
});
});
});
});
});
});
});
var repo;
var index;
var oid;
var parent;

nodegit.Repository.open(path.resolve(__dirname, '../.git'))
.then(function(repoResult) {
repo = repoResult;
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
})
.then(function() {
return repo.openIndex();
})
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
return index.addByPath(fileName);
})
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
})
.then(function(oidResult) {
oid = oidResult;
return nodegit.Refs.nameToId(repo, 'HEAD');
})
.then(function(head) {
return repo.getCommit(head);
})
.then(function(parent) {
var author = nodegit.Signature.create("Scott Chacon", "schacon@gmail.com", 123456789, 60);
var committer = nodegit.Signature.create("Scott A Chacon", "scott@github.com", 987654321, 90);

return repo.createCommit('HEAD', author, committer, 'message', oid, [parent]);
})
.done(function(commitId) {
console.log('New Commit: ', commitId);
});
48 changes: 25 additions & 23 deletions example/clone.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
var git = require('../'),
rimraf = require('rimraf'),
path = "/tmp/nodegit-clone-demo";
var nodegit = require('../');
var rimraf = require('rimraf');
var path = "/tmp/nodegit-clone-demo";

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

repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5', function(error, commit) {
if (error) throw error;

commit.getEntry('README.md', function(error, entry) {
if (error) throw error;

entry.getBlob(function(error, blob) {
if (error) throw error;

console.log(entry.name(), entry.sha(), blob.size() + 'b');
console.log('========================================================\n\n');
var firstTenLines = blob.toString().split('\n').slice(0, 10).join('\n');
console.log(firstTenLines);
console.log('...');
});
});
});
nodegit.Clone.clone(
"https://github.com/nodegit/nodegit.git",
path,
{ ignoreCertErrors: 1})
.then(function(repo) {
return repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5');
})
.then(function(commit) {
return commit.getEntry('README.md')
})
.then(function(entryResult) {
entry = entryResult;
return entry.getBlob();
})
.done(function(blob) {
console.log(entry.filename(), entry.sha(), blob.rawsize() + 'b');
console.log('========================================================\n\n');
var firstTenLines = blob.toString().split('\n').slice(0, 10).join('\n');
console.log(firstTenLines);
console.log('...');
});
});
});
45 changes: 21 additions & 24 deletions example/diff-commits.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
var git = require('../'),
path = require('path');
var nodegit = require('../');
var path = require('path');

// This code examines the diffs between a particular commit and all of its
// parents. Since this commit is not a merge, it only has one parent. This is
// similar to doing `git show`.

git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
if (error) throw error;
nodegit.Repository.open(path.resolve(__dirname, '../.git'))
.then(function(repo) {
return repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5');
})
.then(function(commit) {
console.log('commit ' + commit.sha());
console.log('Author:', commit.author().name() + ' <' + commit.author().email() + '>');
console.log('Date:', commit.date());
console.log('\n ' + commit.message());

repo.getCommit('59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5', function(error, commit) {
if (error) throw error;

console.log('commit ' + commit.sha());
console.log('Author:', commit.author().name() + ' <' + commit.author().email() + '>');
console.log('Date:', commit.date());
console.log('\n ' + commit.message());

commit.getDiff(function(error, diffList) {
if (error) throw error;

diffList.forEach(function(diff) {
diff.patches().forEach(function(patch) {
console.log("diff", patch.oldFile().path(), patch.newFile().path());
patch.hunks().forEach(function(hunk) {
console.log(hunk.header().trim());
hunk.lines().forEach(function(line) {
console.log(String.fromCharCode(line.lineOrigin) + line.content.trim());
});
});
return commit.getDiff();
})
.done(function(diffList) {
diffList.forEach(function(diff) {
diff.patches().forEach(function(patch) {
console.log("diff", patch.oldFile().path(), patch.newFile().path());
patch.hunks().forEach(function(hunk) {
console.log(hunk.header().trim());
hunk.lines().forEach(function(line) {
console.log(String.fromCharCode(line.origin()) + line.content().trim());
});
});
});
Expand Down
27 changes: 11 additions & 16 deletions example/fetch.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
var git = require('../'),
path = require('path');

git.Repo.open(path.resolve(__dirname, '../.git'), function(error, repo) {
if (error) throw error;

var remote = repo.getRemote("origin");
remote.connect(0, function(error) {
if (error) throw error;

remote.download(null, function(error) {
if (error) throw error;

console.log("It worked!");
})
});
var nodegit = require('../');
var path = require('path');

nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
return nodegit.Remote.load(repo, "origin");
}).then(function(remote) {
remote.connect(0);
return remote.download();
})
.done(function() {
console.log("It worked!");
});
Loading