Skip to content

Commit d31140f

Browse files
committed
Merge pull request nodegit#442 from nodegit/add-examples-and-tests
Add examples
2 parents 0c4b5b2 + 74f31bf commit d31140f

File tree

4 files changed

+212
-1
lines changed

4 files changed

+212
-1
lines changed

examples/create-branch.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var nodegit = 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+
7+
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
8+
.then(function(repo) {
9+
// Create a new branch on head
10+
return repo.getHeadCommit()
11+
.then(function(commit) {
12+
return repo.createBranch(
13+
"new-branch",
14+
commit,
15+
0,
16+
repo.defaultSignature(),
17+
"Created new-branch on HEAD");
18+
})
19+
}).done(function() {
20+
console.log("All done!");
21+
});

examples/index-add-and-remove.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
var nodegit = 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+
7+
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
8+
.then(function(repo) {
9+
return repo.openIndex()
10+
.then(function(index) {
11+
var fileContent = {
12+
newFile1: "this has some content",
13+
newFile2: "and this will have more content"
14+
};
15+
var fileNames = Object.keys(fileContent);
16+
17+
return Promise.all(fileNames.map(function(fileName) {
18+
fse.writeFile(
19+
path.join(repo.workdir(), fileName), fileContent[fileName]);
20+
}))
21+
22+
23+
24+
// This will add all files to the index
25+
.then(function() {
26+
return index.addAll();
27+
})
28+
.then(function() {
29+
var newFiles = index.entries().filter(function(entry) {
30+
return ~fileNames.indexOf(entry.path);
31+
});
32+
33+
console.log(
34+
"\n-------------------\n" +
35+
"Added files: " +
36+
"\n-------------------\n");
37+
newFiles.forEach(function(entry) {
38+
console.log(entry.path);
39+
});
40+
})
41+
.then(function() {
42+
// This will remove the files from the index
43+
return index.removeAll("newFile*");
44+
})
45+
.then(function() {
46+
var newFiles = index.entries().filter(function(entry) {
47+
return ~fileNames.indexOf(entry.path);
48+
});
49+
50+
console.log("New files in index: " + newFiles.length);
51+
})
52+
53+
54+
55+
// We can also provide a pattern to add files to the index
56+
.then(function() {
57+
return index.addAll("newFile*");
58+
})
59+
.then(function() {
60+
var newFiles = index.entries().filter(function(entry) {
61+
return ~fileNames.indexOf(entry.path);
62+
});
63+
64+
console.log(
65+
"\n-------------------\n" +
66+
"Added files with pattern: " +
67+
"\n-------------------\n");
68+
newFiles.forEach(function(entry) {
69+
console.log(entry.path);
70+
});
71+
})
72+
.then(function() {
73+
// We're also using the pattern in the remove
74+
return index.removeAll("newFile*");
75+
})
76+
.then(function() {
77+
var newFiles = index.entries().filter(function(entry) {
78+
return ~fileNames.indexOf(entry.path);
79+
});
80+
81+
console.log("New files in index: " + newFiles.length);
82+
})
83+
84+
85+
86+
// Callbacks can be used for a finer degree of control over what
87+
// we add to the index
88+
.then(function() {
89+
return index.addAll(
90+
"newFile*",
91+
nodegit.Index.ADD_OPTION.ADD_CHECK_PATHSPEC,
92+
function(path, matchedPattern) {
93+
if (path == "newFile1") {
94+
return 0; // add the file
95+
}
96+
97+
return 1; // skip the file
98+
});
99+
})
100+
.then(function() {
101+
var newFiles = index.entries().filter(function(entry) {
102+
return ~fileNames.indexOf(entry.path);
103+
});
104+
105+
console.log(
106+
"\n-------------------\n" +
107+
"Added files with callback: " +
108+
"\n-------------------\n");
109+
newFiles.forEach(function(entry) {
110+
console.log(entry.path);
111+
});
112+
})
113+
.then(function() {
114+
// Lets use a callback in the remove as well
115+
return index.removeAll(null, function(path) {
116+
if (~path.indexOf("newFile")) {
117+
return 0; // remove the file
118+
}
119+
120+
return 1; // don't remove the file
121+
});
122+
})
123+
.then(function() {
124+
var newFiles = index.entries().filter(function(entry) {
125+
return ~fileNames.indexOf(entry.path);
126+
});
127+
128+
console.log("Total: " + index.entries().length);
129+
console.log("New files in index: " + newFiles.length);
130+
});
131+
});
132+
}).done(function() {
133+
console.log("All done!");
134+
});

examples/push.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ fse.remove(path.resolve(__dirname, repoDir))
6060
});
6161

6262
// Create the push object for this remote
63-
return nodegit.Push.create(remote)
63+
return remote.push(
64+
["refs/heads/master:refs/heads/master"],
65+
null,
66+
repository.defaultSignature(),
67+
"Push to master")
6468
.then(function(pushResult) {
6569
push = pushResult;
6670

examples/walk-history-for-file.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var nodegit = require("../"),
2+
path = require("path");
3+
4+
// This code walks the history of the master branch and prints results
5+
// that look very similar to calling `git log` from the command line
6+
7+
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
8+
.then(function(repo) {
9+
return repo.getMasterCommit();
10+
})
11+
.then(function(firstCommitOnMaster){
12+
// History returns an event.
13+
var history = firstCommitOnMaster.history(nodegit.Revwalk.SORT.Time);
14+
var commits = [];
15+
16+
// History emits "commit" event for each commit in the branch's history
17+
history.on("commit", function(commit) {
18+
return commit.getDiff()
19+
.then(function(diffList) {
20+
var addCommit = diffList.reduce(function(prevVal, diff) {
21+
var result = prevVal || diff.patches().reduce(function(prevValDiff, patch) {
22+
var result =
23+
prevValDiff ||
24+
!!~patch.oldFile().path().indexOf("descriptor.json") ||
25+
!!~patch.newFile().path().indexOf("descriptor.json");
26+
27+
return result;
28+
}, false);
29+
30+
return result;
31+
}, false);
32+
33+
if (addCommit) {
34+
commits.push(commit);
35+
}
36+
});
37+
});
38+
39+
history.on("end", function() {
40+
commits.forEach(function(commit) {
41+
console.log("commit " + commit.sha());
42+
console.log("Author:", commit.author().name() +
43+
" <" + commit.author().email() + ">");
44+
console.log("Date:", commit.date());
45+
console.log("\n " + commit.message());
46+
});
47+
});
48+
49+
// Don't forget to call `start()`!
50+
history.start();
51+
})
52+
.done();

0 commit comments

Comments
 (0)