Skip to content

Commit

Permalink
add gitCatFile for getting file contents without git checkouts, #355
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kauzmann <[email protected]>
  • Loading branch information
zepumph committed May 15, 2024
1 parent 3f8dd9f commit e4128f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
15 changes: 6 additions & 9 deletions js/common/ReleaseBranch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const getBranchVersion = require( './getBranchVersion' );
const getFileAtBranch = require( './getFileAtBranch' );
const getRepoVersion = require( './getRepoVersion' );
const gitCheckout = require( './gitCheckout' );
const gitCatFile = require( './gitCatFile' );
const gitCheckoutDirectory = require( './gitCheckoutDirectory' );
const gitCloneOrFetchDirectory = require( './gitCloneOrFetchDirectory' );
const gitFirstDivergingCommit = require( './gitFirstDivergingCommit' );
Expand Down Expand Up @@ -611,18 +612,14 @@ module.exports = ( function() {
* @returns {Promise.<boolean>}
*/
async usesChipper2() {
await gitCheckout( this.repo, this.branch );
const dependencies = await getDependencies( this.repo );
await gitCheckout( 'chipper', dependencies.chipper.sha );

const chipperVersion = ChipperVersion.getFromRepository();
// TODO: use better functions once we convert getDependencies and getPackageJSON, https://github.com/phetsims/perennial/issues/355
const dependencies = JSON.parse( await gitCatFile( this.repo, 'dependencies.json', this.branch ) );

const result = chipperVersion.major !== 0 || chipperVersion.minor !== 0;
const chipperPackageJSON = JSON.parse( await gitCatFile( 'chipper', 'package.json', dependencies.chipper.sha ) );
const chipperVersion = ChipperVersion.getFromPackageJSON( chipperPackageJSON );

await gitCheckout( this.repo, 'main' );
await gitCheckout( 'chipper', 'main' );

return result;
return chipperVersion.major !== 0 || chipperVersion.minor !== 0;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions js/common/gitCatFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2017, University of Colorado Boulder

/**
* retrieve the contents of a file without changing the git tree via checkouts.
*
* @author Michael Kauzmann (PhET Interactive Simulations)
* @author Jonathan Olson <[email protected]>
*/

const assert = require( 'assert' );
const execute = require( './execute' );

/**
* Gets the contents of the file at a given state in the git tree
* @public
*
* @param {string} repo - The repository name
* @param {string} file - Path to the file from the repo root, like js/myFile.js
* @param {string} branchOrSha - what revision to get the contents of the file at. "buoyancy-1.0" or "main" or
* "{{SHA}}". Defaults to the current checkout (HEAD)
* @returns {Promise.<string>} - Stdout
* @rejects {ExecuteError}
*/
module.exports = async function gitCatFile( repo, file, branchOrSha = 'HEAD' ) {
assert( typeof repo === 'string' );
assert( typeof file === 'string' );
assert( typeof branchOrSha === 'string' );

return execute( 'git', [ 'cat-file', 'blob', `${branchOrSha}:${file}` ], `../${repo}` );
};

0 comments on commit e4128f4

Please sign in to comment.