0

Hello,

I use Promise for a initialize node project.

I want to insert in my MongoDb the name of files on all of my branch on my git repository.

I use nodegit to manipulate repo, for every nodegit method the return is a Promise. But i need to loop on all branch reference for get all files and branch name.

After that i can prepare my array for insert in database on next promise.

Code look like :

// List all branchs
.then((branchs) => {
  let promises = [];
  let allBranchFiles = [];
  branchs.map((branch) => {
    let q = repo.checkoutBranch(branch, config.checkoutOpts)
      .then(() => repo.getCurrentBranch())
      .then((ref) => {
        return new Promise((resolve, reject) => {
          fs.readdir('./myRepo/', (err, files) => {
            files.map((file) => {
              allBranchFiles.push({fileName: file, branch: branch});
            });
            resolve(allBranchFiles);
          });
        });
      });
    promises.push(q);
  });
  return Promise.all(promises);
})

That code finish by two way :

  • First :
    { Error: the index is locked; this might be due to a concurrent or crashed process
        at Error (native) errno: -14 }

I'm sure no other process use git in this repo!

  • Second :

All of my files get the same value for "branch" but they are on separate branchs.

Thank you for helping me guys !

Cya.

1 Answers1

0

I try something and it's work but it's ugly.

I mean, I use a recursiv function for doing my stuff. I wait better for change that.

I let you know how I replace my code on the question.

// List all branchs
.then((branchs) => {
  return new Promise((resolve, reject) => recursiv(branchs).then(() => resolve(allBranchFiles))); 
})

And my function :

let recursiv = (branchs, index=0) => {
  return new Promise((resolved, rejected) => {
    repo.checkoutBranch(branchs[index], config.checkoutOpts)
      .then(() => repo.getCurrentBranch())
      .then((user) => {
        return new Promise((resolve, reject) => {
          fs.readdir('./myRepo/', (err, files) => {
            files.map((file) => {
              allBranchFiles.push({fileName: file, branch: branchs[index]});
            });
            resolve();
          });
        });
      })
      .done(() => {
        if (index < branchs.length-1) resolved(recursiv(branchs, index+1));
        else resolved();
      });
  });
};