nodegit icon indicating copy to clipboard operation
nodegit copied to clipboard

How do I remove-and-commit + fs.unlink(... and have git know that everything is up to date / commited?

Open map01ASCIIDOC10 opened this issue 10 years ago • 2 comments

If I create new files in my repo and use .addAll(file_paths), commit and then run git-cola in the directory I get 0 reports on files being in the directory which haven't been added to the repo.

The same thing happens when I use .removeAll(file_paths) but that only removes it from the index, not the actual files like git rm file.txt does, so I experimented with doing fs.unlink(... before and after doing removeAll but it always results in git-cola reporting modified files.

That is:

git init
touch test.txt
git add test.txt
git commit -m "test"
git rm test.txt
git commit -m "rm test"
git-cola

Will show 0 reports on changes needing to be committed but my nodejs equivilant does show changes needing to be commited, why is that and what can I do about it?


My nodegit removeAll code:

...
function removeAndCommitFiles(root, file_paths, message) {
  var dotgit = path.join(root, ".git");

  var repository;
  var index;
  var oid;

  nodegit.Repository.open(dotgit)
  .then(function(repo) {
    repository = repo;
    return fs.ensureDir(repository.workdir());
  })
  .then(function() {
    return repository.openIndex();
  })
  .then(function(indexResult) {
    index = indexResult;
    return index.read(1);
  })
  .then(function() {
    return index.removeAll(file_paths);
  })
  .then(function() {
    return index.write();
  })
  .then(function() {
    return index.writeTree();
  })
  .then(function(oidResult) {
    oid = oidResult;
    return nodegit.Reference.nameToId(repository, "HEAD");
  })
  .then(function(head) {
    return repository.getCommit(head);
  })
  .then(function(parent) {
    return repository.createCommit("HEAD", author, committer, message, oid, [parent]);
  })
  .done(function(commitId) {
    // ...
  });
}
...

map01ASCIIDOC10 avatar Jan 29 '16 19:01 map01ASCIIDOC10

This seems to work for me: https://gist.github.com/yofreke/20ed3ab5e15707fcc050

A few notes:

  • Make sure to return all promises
  • Best to include which libraries you are using in the issue
  • Your code didn't remove the files from the FS, so I added some code to do that part
  • Don't do path.join(root, ".git"), as this will result in things like path/to/my/repo/.git

The gist will result in the file test.txt being removed from the filesystem, as well as updating HEAD to the new commit. Running git status in the directory shows that everything is as expected.

Hope this helps!

yofreke avatar Feb 01 '16 07:02 yofreke

,

kabig avatar Jan 11 '19 21:01 kabig