FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add examples by johnhaley81 · Pull Request #442 · nodegit/nodegit · GitHub

Repository navigation

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

Filter by extension

Filter by extension .js  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 21 additions & 0 deletions examples/create-branch.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var nodegit = require("../");
var path = require("path");
var Promise = require("nodegit-promise");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));

nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function(repo) {
// Create a new branch on head
return repo.getHeadCommit()
.then(function(commit) {
return repo.createBranch(
"new-branch",
commit,
0,
repo.defaultSignature(),
"Created new-branch on HEAD");
})
}).done(function() {
console.log("All done!");
});
134 changes: 134 additions & 0 deletions examples/index-add-and-remove.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
var nodegit = require("../");
var path = require("path");
var Promise = require("nodegit-promise");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));

nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function(repo) {
return repo.openIndex()
.then(function(index) {
var fileContent = {
newFile1: "this has some content",
newFile2: "and this will have more content"
};
var fileNames = Object.keys(fileContent);

return Promise.all(fileNames.map(function(fileName) {
fse.writeFile(
path.join(repo.workdir(), fileName), fileContent[fileName]);
}))



// This will add all files to the index
.then(function() {
return index.addAll();
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});

console.log(
"\n-------------------\n" +
"Added files: " +
"\n-------------------\n");
newFiles.forEach(function(entry) {
console.log(entry.path);
});
})
.then(function() {
// This will remove the files from the index
return index.removeAll("newFile*");
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});

console.log("New files in index: " + newFiles.length);
})



// We can also provide a pattern to add files to the index
.then(function() {
return index.addAll("newFile*");
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});

console.log(
"\n-------------------\n" +
"Added files with pattern: " +
"\n-------------------\n");
newFiles.forEach(function(entry) {
console.log(entry.path);
});
})
.then(function() {
// We're also using the pattern in the remove
return index.removeAll("newFile*");
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});

console.log("New files in index: " + newFiles.length);
})



// Callbacks can be used for a finer degree of control over what
// we add to the index
.then(function() {
return index.addAll(
"newFile*",
nodegit.Index.ADD_OPTION.ADD_CHECK_PATHSPEC,
function(path, matchedPattern) {
if (path == "newFile1") {
return 0; // add the file
}

return 1; // skip the file
});
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});

console.log(
"\n-------------------\n" +
"Added files with callback: " +
"\n-------------------\n");
newFiles.forEach(function(entry) {
console.log(entry.path);
});
})
.then(function() {
// Lets use a callback in the remove as well
return index.removeAll(null, function(path) {
if (~path.indexOf("newFile")) {
return 0; // remove the file
}

return 1; // don't remove the file
});
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});

console.log("Total: " + index.entries().length);
console.log("New files in index: " + newFiles.length);
});
});
}).done(function() {
console.log("All done!");
});
6 changes: 5 additions & 1 deletion examples/push.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ fse.remove(path.resolve(__dirname, repoDir))
});

// Create the push object for this remote
return nodegit.Push.create(remote)
return remote.push(
["refs/heads/master:refs/heads/master"],
null,
repository.defaultSignature(),
"Push to master")
.then(function(pushResult) {
push = pushResult;

Expand Down
52 changes: 52 additions & 0 deletions examples/walk-history-for-file.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var nodegit = require("../"),
path = require("path");

// This code walks the history of the master branch and prints results
// that look very similar to calling `git log` from the command line

nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function(repo) {
return repo.getMasterCommit();
})
.then(function(firstCommitOnMaster){
// History returns an event.
var history = firstCommitOnMaster.history(nodegit.Revwalk.SORT.Time);
var commits = [];

// History emits "commit" event for each commit in the branch's history
history.on("commit", function(commit) {
return commit.getDiff()
.then(function(diffList) {
var addCommit = diffList.reduce(function(prevVal, diff) {
var result = prevVal || diff.patches().reduce(function(prevValDiff, patch) {
var result =
prevValDiff ||
!!~patch.oldFile().path().indexOf("descriptor.json") ||
!!~patch.newFile().path().indexOf("descriptor.json");

return result;
}, false);

return result;
}, false);

if (addCommit) {
commits.push(commit);
}
});
});

history.on("end", function() {
commits.forEach(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());
});
});

// Don't forget to call `start()`!
history.start();
})
.done();

Back | FazBrowse Home | New Git URL