Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] wp-env: Switch to simple git #28804

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 34 additions & 66 deletions packages/env/lib/download-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* External dependencies
*/
const util = require( 'util' );
const NodeGit = require( 'nodegit' );
const fs = require( 'fs' );
const got = require( 'got' );
const path = require( 'path' );
const simpleGit = require( 'simple-git' );

/**
* Promisified dependencies
Expand Down Expand Up @@ -97,73 +97,41 @@ async function downloadSource( source, options ) {
* @param {boolean} options.debug True if debug mode is enabled.
*/
async function downloadGitSource( source, { onProgress, spinner, debug } ) {
const log = debug
? ( message ) => {
spinner.info( `NodeGit: ${ message }` );
spinner.start();
}
: () => {};
onProgress( 0 );
// const log = debug
// ? ( message ) => {
// spinner.info( `NodeGit: ${ message }`);
// spinner.start();
// }
// : () => {};
// onProgress( 0 );

spinner.info( 'Cloning or getting the repo.' );
spinner.info( source.url );
spinner.info( source.clonePath );

await simpleGit()
.clone( source.url, source.clonePath, {
'--depth': '1',
} )
.catch( ( e ) => {
// If the repo has already been cloned, continue to fetch it instead of failing.
if ( ! e.message.includes( 'is not an empty directory' ) ) {
throw e;
}
} );

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();
}
},
},
},
};
const git = simpleGit( { baseDir: source.clonePath } );

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

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 );
}
spinner.info( 'Fetching the specified ref.' );
// const remote = await repository.getRemote( 'origin' );
// await remote.fetch( source.ref, gitFetchOptions.fetchOpts );
await git.fetch();
await git.checkout( source.ref );
// TODO: do we need to use `FETCH_HEAD`?
// TODO: do we need to use "setHeadDetached"?

onProgress( 1 );
// TODO: handle progress.
// onProgress( 1 );
}

/**
Expand All @@ -178,7 +146,7 @@ async function downloadGitSource( source, { onProgress, spinner, debug } ) {
async function downloadZipSource( source, { onProgress, spinner, debug } ) {
const log = debug
? ( message ) => {
spinner.info( `NodeGit: ${ message }` );
spinner.info( message );
spinner.start();
}
: () => {};
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.31.0",
"terminal-link": "^2.0.0",
"yargs": "^14.0.0"
},
Expand Down