Skip to content

Commit

Permalink
wp-env: Use Simple Git instead of NodeGit
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Feb 8, 2021
1 parent a1d5860 commit fad8401
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 57 deletions.
76 changes: 20 additions & 56 deletions packages/env/lib/download-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* External dependencies
*/
const util = require( 'util' );
const NodeGit = require( 'nodegit' );
const SimpleGit = require( 'simple-git' );
const fs = require( 'fs' );
const got = require( 'got' );
const path = require( 'path' );
Expand Down Expand Up @@ -99,69 +99,33 @@ async function downloadSource( source, options ) {
async function downloadGitSource( source, { onProgress, spinner, debug } ) {
const log = debug
? ( message ) => {
spinner.info( `NodeGit: ${ message }` );
spinner.info( `SimpleGit: ${ message }` );
spinner.start();
}
: () => {};
onProgress( 0 );

const gitFetchOptions = {
fetchOpts: {
callbacks: {
transferProgress( progress ) {
// Fetches are finished when all objects are received and indexed,
// so received objects plus indexed objects should equal twice
// the total number of objects when done.
onProgress(
( progress.receivedObjects() +
progress.indexedObjects() ) /
( progress.totalObjects() * 2 )
);
},
certificateCheck: () => 0,
credentials: ( url, userName ) => {
try {
return NodeGit.Cred.sshKeyFromAgent( userName );
} catch {
return NodeGit.Cred.defaultNew();
}
},
},
},
};

log( 'Cloning or getting the repo.' );
const repository = await NodeGit.Clone(
source.url,
source.clonePath,
gitFetchOptions
).catch( () => {
log( 'Repo already exists, get it.' );
return NodeGit.Repository.open( source.clonePath );
} );
const git = SimpleGit();

const isRepo =
fs.existsSync( source.clonePath ) &&
( await git.cwd( source.clonePath ).checkIsRepo() );

if ( isRepo ) {
log( 'Repo already exists, using it.' );
} else {
await git.clone( source.url, source.clonePath, {
'--depth': '1',
'--no-single-branch': null,
} );
}

log( 'Fetching the specified ref.' );
const remote = await repository.getRemote( 'origin' );
await remote.fetch( source.ref, gitFetchOptions.fetchOpts );
await remote.disconnect();
try {
log( 'Checking out the specified ref.' );
await repository.checkoutRef(
await repository
.getReference( 'FETCH_HEAD' )
// Sometimes git doesn't update FETCH_HEAD for things
// like tags so we try another method here.
.catch(
repository.getReference.bind( repository, source.ref )
),
{
checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE,
}
);
} catch ( error ) {
log( 'Ref needs to be set as detached.' );
await repository.setHeadDetached( source.ref );
}

await git.fetch( 'origin', source.ref );
log( 'Checking out the specified ref.' );
await git.checkout( source.ref );

onProgress( 1 );
}
Expand Down
2 changes: 1 addition & 1 deletion packages/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"got": "^10.7.0",
"inquirer": "^7.1.0",
"js-yaml": "^3.13.1",
"nodegit": "^0.27.0",
"ora": "^4.0.2",
"rimraf": "^3.0.2",
"simple-git": "^2.34.2",
"terminal-link": "^2.0.0",
"yargs": "^14.0.0"
},
Expand Down

0 comments on commit fad8401

Please sign in to comment.