|
| 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 | + }); |
0 commit comments