使用simple-git的nodejs浅git克隆

我正在尝试使用simple-git创建浅表克隆。我正在尝试创建与此命令等效的命令:git clone --depth 1 https://github.com/steveukx/git-js.git。我的代码如下:

const git = require('simple-git')()

const repoURL = 'https://github.com/steveukx/git-js.git';
const localPath= './git-js';
const  options = ['--depth','1'];

const handlerFn = () => {
    console.log('DONE')
};

git.clone(repoURL,localPath,options,handlerFn());

我在--depth 1中指定了options,但是代码复制了整个回购历史记录,似乎完全忽略了给定的选项。我这样做是否正确,什么会导致这种行为?

zlzlyw 回答:使用simple-git的nodejs浅git克隆

经过深入研究后,问题出在git.clone(repoURL,localPath,options,handlerFn());中,您必须将引用传递给函数,而不是像git.clone(repoURL,handlerFn);这样的实际回调。

完整的实现方式如下:

const git = require('simple-git')();
const fs = require('fs')
const url = require('url');

this.gitURL = 'https://github.com/torvalds/linux.git';

const localURL = url.parse(this.gitURL);
const localRepoName = (localURL.hostname + localURL.path)
.replace('com','')
.replace('/','.')
.replace('.git','')

this.localPath = `./${localRepoName}`;
this.options = ['--depth','1'];
this.callback = () => {
    console.log('DONE')
}

if (fs.existsSync(this.localPath)) {
    // something
} else {
    git.outputHandler((command,stdout,stderr) => {
            stdout.pipe(process.stdout);
            stderr.pipe(process.stderr)

            stdout.on('data',(data) => {
                // Print data
                console.log(data.toString('utf8'))
            })
        })
        .clone(this.gitURL,this.localPath,this.options,this.callback)
}
本文链接:https://www.f2er.com/3099698.html

大家都在问