Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
59 changes: 59 additions & 0 deletions 59 example/add-and-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var git = require('../'),
path = require('path'),
fs = require('fs'),
fileName = 'newfile.txt',
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());
});
});
});
});
});
});
});
});
});
});
53 changes: 53 additions & 0 deletions 53 example/remove-and-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var git = require('../'),
path = require('path'),
fileName = 'newfile.txt'
;

/**
* This example deletes a certain file `newfile.txt`, removes it from the git index and
* commits it to head. Similar to a `git rm newfile.txt` followed by a `git commit`
* Use add-and-commit.js to create the file first.
**/

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

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

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

index.removeByPath(fileName);

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());
// the file is removed from the git repo, use fs.unlink now to remove it
// from the filesystem.
});
});
});
});
});
});
});
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.