【问题标题】:nodejs shallow git clone using simple-git使用 simple-git 的 nodejs 浅 git 克隆
【发布时间】:2019-11-21 04:30:04
【问题描述】:

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

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

【问题讨论】:

    标签: node.js git npm shallow-clone


    【解决方案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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多