簡體   English   中英

nodejs 淺 git 使用 simple-git 克隆

[英]nodejs shallow git clone using simple-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());

我在options中指定--depth 1 ,但是代碼復制了整個 repo 歷史,它似乎完全忽略了給定的選項。 我這樣做是否正確,什么會導致這種行為?

經過一番挖掘,問題出在git.clone(repoURL, localPath, options, handlerFn()); ,您必須傳遞對 function 的引用而不是實際的回調,例如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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM