node.js - 使用 simple-git 进行 Nodejs 浅 git 克隆

标签 node.js git npm shallow-clone

我正在尝试使用 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());

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

最佳答案

经过一番挖掘,问题出现在 git.clone(repoURL, localPath, options, handlerFn()); 中,您必须传递对函数的引用而不是实际的回调,就像这样 git.clone(repoURL, localPath, options, 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('/', '.')
.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)
}

关于node.js - 使用 simple-git 进行 Nodejs 浅 git 克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58864607/

相关文章:

git - hg 相当于 git notes

node.js - Kudu 控制台在 npm install 上挂起

git - "Secret text"Git 凭据未显示在 Jenkins 项目源代码管理部分

git - 为什么 Git 不被视为 "block chain"?

node.js - Nodejs + npm,在 ntfs 分区上安装模块

npm - 在npm安装单个软件包后运行脚本(如postinstall)?

javascript - 嵌套查询 - 未定义错误

javascript - NodeJS 中的导出语句

ios - 使用 RAW RSA 在 iOS 上加密和在 Node.js 上解密时遇到问题

node.js - Mongoose 仅​​在更改时验证