2

我目前正在编写一个 node.js 模块。我正在使用var exec = require('child_process').exec;node.js api。但我必须用 exec 连续执行 2 个 git 命令。我总是收到以下错误消息:

执行错误:错误:命令失败:致命:无法创建'~/Git/project/.git/index.lock':文件存在。

如果当前没有其他 git 进程在运行,这可能意味着之前该存储库中的 git 进程崩溃了。确保没有其他 git 进程正在运行并手动删除文件以继续。

我需要在第一个命令完成后执行第二个命令 - 关键字:sync 到目前为止,我使用execSync进行了尝试,但它无法在 Windows 系统上运行,这是不行的。

目前我正在使用asyncblock模块,但我也给了我上面提到的错误。

当 updateList 中的两个值都为真时会发生这种情况,如果一个为假,则所有工作都按预期工作。

var loadPackageJson = function(updateList){

        if(updateList.foo === true){
            update('foo/package.json');
        }

        if (updateList.bar === true){
            update('bar/package.json');
        }       
    };

        var update = function(path){
                var packageJson = JSON.parse(fs.readFileSync(path,'utf8'));
                incrementVersion(packageJson);

                asyncblock(function(flow){
                    exec('git log -1 HEAD', flow.add('commit'));
                    var result = flow.wait('commit');
                    var commit=result.split('\n');
                    console.info(commit[0]);

                    setValueInPackageJson('commit', commit[0], packageJson);
                    writeUpdatedPackageJson(path, packageJson);

                    flow.wait();

                    exec('git add ' + path, flow.add('add'));
                    flow.wait('add'); //error is thrown here
                    console.info('added ' + path + ' successful to commit');
                });
    };

如何同步执行两个 git 命令?

/edit 我知道 SO 上的以下线程,但我帮助我不要解决这个问题。我在这个线程上找到了带有 asyncblock 的提示。

/edit2 控制台上的输出如下所示:

Version: 0.6.14
Version: 0.0.901
commit a3ed4fd8545b736aa9feea1bea52c5e0aa6a07ac
foo/package.json successfully updated
commit a3ed4fd8545b736aa9feea1bea52c5e0aa6a07ac
bar/package.json successfully updated

在我看来,这看起来像是节点并行处理了loadPackageJson函数。

BR & 谢谢,我的贝克

4

0 回答 0