node.jsgitnpmshallow-clone

nodejs shallow git clone using simple-git


I'm trying to create shallow clone using simple-git. I'm trying to create an equivalent of this command: git clone --depth 1 https://github.com/steveukx/git-js.git. My code is as follows:

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());

I've specified --depth 1 in options, but the code copies the entire repo history, it seems to completely ignore the options given. Am I doing this correctly, what can cause this behaviour?


Solution

  • After some digging the issue was in git.clone(repoURL, localPath, options, handlerFn());, you have to pass the reference to function instead of actual callback, like this git.clone(repoURL, localPath, options, handlerFn);.

    The full implementation is bellow:

    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)
    }