2

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?

1 Answer 1

1

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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.