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

[Experiment] Backports script #43360

Draft
wants to merge 12 commits into
base: trunk
Choose a base branch
from
50 changes: 50 additions & 0 deletions bin/backports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* External dependencies
*/
import Diff from 'diff';
import SimpleGit from 'simple-git';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname( fileURLToPath( import.meta.url ) );

const simpleGit = SimpleGit( dirname( __dirname ) );

async function getCommits( file ) {
const options = {
file,
};
const log = await simpleGit.log( options );
return log.all;
}

function matchFilename( file ) {
if ( file.startsWith( 'lib/compat/wordpress-6.1/' ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Allow for different WP versions.

return file.replace( 'lib/compat/wordpress-6.1/', 'src/wp-includes/' );
}
return file;
}

const log = await getCommits( 'lib/compat/wordpress-6.1/' );

const prRegex = /\(#([0-9]+)\)$/;
const prs = log.map( ( { message } ) => message.match( prRegex )?.[ 1 ] );
console.log( prs );

const hash = log[ 1 ].hash;

const diff = await simpleGit.show( hash );

//console.log( diff );

const parsedDiff = Diff.parsePatch( diff );

//console.log( parsedDiff[ 0 ] );

const fileName = parsedDiff[ 0 ].oldFileName.replace( /a\//, '' );

console.log( matchFilename( fileName ) );

parsedDiff[ 0 ].newFileName = 'b/../wordpress-develop/' + matchFilename( fileName );

console.log( parsedDiff );