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

Add `Repository#refreshIndex` by johnhaley81 · Pull Request #986 · 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  (23) .json  (1) All 2 file types 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
Add Repository#refreshIndex
`Repository#refreshIndex` will return an `Index` object back that has the latest data loaded off of disk.

This removes the need for the `Repository#index` then `Index#read(1)` pattern that was being used which seemed to have some weird timing issues that could have wrong data loaded into the returned index.

Calling this function also clears the `Index` that's cached in the `Repository` object.
  • Loading branch information
John Haley committed Apr 7, 2016
commit 2a5062389a292293552680be8f79f0fb2f053808
3 changes: 1 addition & 2 deletions examples/add-and-commit.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 @@ -35,11 +35,10 @@ nodegit.Repository.open(path.resolve(__dirname, "../.git"))
);
})
.then(function() {
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
// this file is in the root of the directory and doesn't need a full path
Expand Down
3 changes: 1 addition & 2 deletions examples/create-new-repo.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 @@ -20,11 +20,10 @@ fse.ensureDir(path.resolve(__dirname, repoDir))
return fse.writeFile(path.join(repository.workdir(), fileName), fileContent);
})
.then(function(){
return repository.openIndex();
return repository.refreshIndex();
})
.then(function(idx) {
index = idx;
return index.read(1);
})
.then(function() {
return index.addByPath(fileName);
Expand Down
2 changes: 1 addition & 1 deletion examples/details-for-tree-entry.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 @@ -13,7 +13,7 @@ nodegit.Repository.open(path.resolve(__dirname, "../.git"))

// Tree entry doesn't have any data associated with the actual entry
// To get that we need to get the index entry that this points to
return repo.openIndex().then(function(index) {
return repo.refreshIndex().then(function(index) {
var indexEntry = index.getByPath(treeEntry.path());

// With the index entry we can now view the details for the tree entry
Expand Down
2 changes: 1 addition & 1 deletion examples/general.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 @@ -319,7 +319,7 @@ nodegit.Repository.open(path.resolve(__dirname, "../.git"))

// The [index file API][gi] allows you to read, traverse, update and write
// the Git index file (sometimes thought of as the staging area).
return repo.openIndex();
return repo.refreshIndex();
})

.then(function(index) {
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Up @@ -5,7 +5,7 @@ var fse = promisify(require("fs-extra"));

nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function(repo) {
return repo.openIndex()
return repo.refreshIndex()
.then(function(index) {
var fileContent = {
newFile1: "this has some content",
Expand Down
6 changes: 2 additions & 4 deletions examples/merge-cleanly.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 @@ -43,10 +43,9 @@ fse.remove(path.resolve(__dirname, repoDir))

// Load up the repository index and make our initial commit to HEAD
.then(function() {
return repository.openIndex();
return repository.refreshIndex();
})
.then(function(index) {
index.read(1);
index.addByPath(ourFileName);
index.write();

Expand Down Expand Up @@ -79,10 +78,9 @@ fse.remove(path.resolve(__dirname, repoDir))
);
})
.then(function() {
return repository.openIndex();
return repository.refreshIndex();
})
.then(function(index) {
index.read(1);
index.addByPath(theirFileName);
index.write();

Expand Down
12 changes: 4 additions & 8 deletions examples/merge-with-conflicts.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 @@ -49,10 +49,9 @@ fse.remove(path.resolve(__dirname, repoDir))

// Load up the repository index and make our initial commit to HEAD
.then(function() {
return repository.openIndex();
return repository.refreshIndex();
})
.then(function(index) {
index.read(1);
index.addByPath(fileName);
index.write();

Expand Down Expand Up @@ -94,9 +93,8 @@ fse.remove(path.resolve(__dirname, repoDir))
);
})
.then(function() {
return repository.openIndex()
return repository.refreshIndex()
.then(function(index) {
index.read(1);
index.addByPath(fileName);
index.write();

Expand All @@ -122,8 +120,7 @@ fse.remove(path.resolve(__dirname, repoDir))
);
})
.then(function() {
return repository.openIndex().then(function(index) {
index.read(1);
return repository.refreshIndex().then(function(index) {
index.addByPath(fileName);
index.write();

Expand Down Expand Up @@ -173,9 +170,8 @@ fse.remove(path.resolve(__dirname, repoDir))
// we need to get a new index as the other one isnt backed to
// the repository in the usual fashion, and just behaves weirdly
.then(function() {
return repository.openIndex().then(function(index) {
return repository.refreshIndex().then(function(index) {

index.read(1);
index.addByPath(fileName);
index.write();

Expand Down
3 changes: 1 addition & 2 deletions 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 @@ -30,10 +30,9 @@ fse.remove(path.resolve(__dirname, repoDir))

// Load up the repository index and make our initial commit to HEAD
.then(function() {
return repository.openIndex();
return repository.refreshIndex();
})
.then(function(index) {
index.read(1);
index.addByPath(fileName);
index.write();

Expand Down
3 changes: 1 addition & 2 deletions examples/remove-and-commit.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 @@ -17,11 +17,10 @@ var _oid;
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function(repo) {
_repository = repo;
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(index){
_index = index;
return _index.read();
})
.then(function() {
//remove the file from the index...
Expand Down
6 changes: 5 additions & 1 deletion generate/input/descriptor.json
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 @@ -1833,7 +1833,11 @@
}
},
"git_repository_set_index": {
"ignore": true
"args": {
"index": {
"isOptional": true
}
}
},
"git_repository_set_odb": {
"ignore": true
Expand Down
34 changes: 26 additions & 8 deletions lib/repository.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 @@ -23,6 +23,27 @@ Object.defineProperty(Repository.prototype, "openIndex", {
enumerable: false,
value: Repository.prototype.index
});
/**
* Grabs a fresh copy of the index from the repository. Invalidates
* all previously grabbed indexes
*
* @async
* @return {Index}
*/
Repository.prototype.refreshIndex = function(callback) {
var repo = this;

repo.setIndex(); // clear the index

return repo.index()
.then(function(index) {
if (typeof callback === "function") {
callback(null, index);
}

return index;
}, callback);
};

/**
* Creates a branch with the passed in name pointing to the commit
Expand Down Expand Up @@ -521,10 +542,9 @@ Repository.prototype.createCommitOnHead = function(
var index;
var repo = this;

return repo.openIndex()
return repo.refreshIndex()
.then(function(index_) {
index = index_;
index.read(1);
if (!filesToAdd) { filesToAdd = []; }
filesToAdd.forEach(function(filePath) {
index.addByPath(filePath);
Expand Down Expand Up @@ -907,7 +927,7 @@ function performRebase(repository, rebase, signature, beforeNextFn) {
function getPromise() {
return rebase.next()
.then(function() {
return repository.openIndex()
return repository.refreshIndex()
.then(function(index) {
if (index.hasConflicts()) {
throw index;
Expand Down Expand Up @@ -1033,7 +1053,7 @@ Repository.prototype.continueRebase = function(signature, beforeNextFn) {

signature = signature || repo.defaultSignature();

return repo.openIndex()
return repo.refreshIndex()
.then(function(index) {
if (index.hasConflicts()) {
throw index;
Expand Down Expand Up @@ -1241,11 +1261,10 @@ Repository.prototype.stageFilemode = function(filePath, stageNew) {

return fse.remove(indexLock)
.then(function() {
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
return diffPromise;
Expand Down Expand Up @@ -1449,10 +1468,9 @@ Repository.prototype.stageLines =
});
};

return repo.openIndex()
return repo.refreshIndex()
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
return diffPromise();
Expand Down
3 changes: 1 addition & 2 deletions test/tests/checkout.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 @@ -126,9 +126,8 @@ describe("Checkout", function() {
.then(function(branch) {
fse.writeFileSync(packageJsonPath, "\n");

return test.repository.openIndex()
return test.repository.refreshIndex()
.then(function(index) {
index.read(1);
index.addByPath(packageJsonName);
index.write();

Expand Down
12 changes: 4 additions & 8 deletions test/tests/commit.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 @@ -55,11 +55,10 @@ describe("Commit", function() {

return fse.writeFile(path.join(repo.workdir(), fileName), fileContent)
.then(function() {
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
return index.addByPath(fileName);
Expand Down Expand Up @@ -162,11 +161,10 @@ describe("Commit", function() {
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
})
.then(function() {
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
return index.addByPath(fileName);
Expand Down Expand Up @@ -242,11 +240,10 @@ describe("Commit", function() {
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
})
.then(function() {
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
return index.addByPath(fileName);
Expand Down Expand Up @@ -290,11 +287,10 @@ describe("Commit", function() {
);
})
.then(function() {
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
return index.read(1);
})
.then(function() {
return index.addByPath(newFileName);
Expand Down
4 changes: 2 additions & 2 deletions test/tests/diff.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 @@ -27,7 +27,7 @@ describe("Diff", function() {
return Repository.open(reposPath).then(function(repository) {
test.repository = repository;

return repository.openIndex();
return repository.refreshIndex();
})
.then(function(index) {
test.index = index;
Expand Down Expand Up @@ -307,7 +307,7 @@ describe("Diff", function() {
return Repository.open(reposPath).then(function(repository) {
test.repository = repository;

return repository.openIndex();
return repository.refreshIndex();
})
.then(function(index) {
test.index = index;
Expand Down
6 changes: 3 additions & 3 deletions test/tests/index.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 @@ -22,7 +22,7 @@ describe("Index", function() {
return Repository.open(reposPath)
.then(function(repo) {
test.repository = repo;
return repo.openIndex();
return repo.refreshIndex();
})
.then(function(index) {
test.index = index;
Expand Down Expand Up @@ -342,7 +342,7 @@ describe("Index", function() {
return RepoUtils.addFileToIndex(repo, fileName);
})
.then(function() {
return repo.openIndex();
return repo.index();
})
.then(function(index) {
assert.ok(!index.hasConflicts());
Expand All @@ -353,7 +353,7 @@ describe("Index", function() {
);
})
.then(function() {
return repo.openIndex();
return repo.index();
})
.then(function(index) {
assert(index.hasConflicts());
Expand Down
Loading

Back | FazBrowse Home | New Git URL