Following the add-and-commit.js in the example directory I was able to get my own application adding addByPath to commit new files. However, addByPath doesn't work on directories. Is there a way to add a whole directory to a repository?
Let me run over some of the details. I wrote a small test application using the code found at add-and-commit.js. I then created a new file named test.txt and ran the add and commit code. Sure enough, a new commit is generated and test.txt shows up in a new clone of the repository.
If I do the same thing, but instead of test.txt I do newdir/test.txt and run the code pointed at that file, I get a new commit but no file added. If I go to the repository and use git status I see the directory under "untracked files".
Attempting to run addByPath results in a "Is a directory" error, which is probably as it should be. Is there a way to add a file inside of a directory to the repository using Nodegit?
This _should_ be working now, but I'll confirm.
Confirmed, updated the example accordingly
https://github.com/nodegit/nodegit/pull/283
@maxkorp so you can use addByPath() to add an entire directory? Because I see the same error reported here when I try to add a dir.
Yup, this is not working. The changes to the example doesn't actually cover the case of passing a directory into addByPath, it's only checking for something like folder/file.txt.
[Error: cannot create blob from '/blah/blah/test-repo/tmp'; it is a directory]
var git = require("nodegit");
git.Repository.open("test-repo")
.then(function(r) {
return r.index();
}).then(function(index) {
return index.addByPath("tmp");
}).then(function(ret) {
console.log("good");
console.log(ret);
}).catch(function(err) {
console.log(err);
});
Since the JavaScript implementation is a direct wrapper of git_index_add_bypath and not a custom implementation, this is no surprise as the libgit2 API only takes a filename and not a directory name.
The addByPath(*) function should either be enhanced to take in a directory name or a completely new API function should be created for handling this case.
@rcjsuen I agree. The .addAll() function supports globs though, so that's an alternative. But that command fails as well if you pass in just a folder.
Hi, if i want to add several files, such as a/b/text1.js, c/d/test2.js . How can I do using the add functions?
@DawsonG Hi, if i want to add many files, such as a/b/text1.js, c/d/test2.js ... How can I do using the add functions? Can i add an Array of filePaths ?
Most helpful comment
Yup, this is not working. The changes to the example doesn't actually cover the case of passing a directory into
addByPath, it's only checking for something likefolder/file.txt.Since the JavaScript implementation is a direct wrapper of
git_index_add_bypathand not a custom implementation, this is no surprise as the libgit2 API only takes a filename and not a directory name.The
addByPath(*)function should either be enhanced to take in a directory name or a completely new API function should be created for handling this case.