Skip to content

Commit

Permalink
fix: possible fix for nested projects flxbl-io#64
Browse files Browse the repository at this point in the history
  • Loading branch information
Goobles authored May 27, 2024
1 parent 90c6165 commit 1ae64f8
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions packages/sfp-cli/src/core/git/Git.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import SFPLogger, { Logger, LoggerLevel } from '@flxbl-io/sfp-logger';
import simplegit, { SimpleGit } from 'simple-git';
import simplegit, { CheckRepoActions, SimpleGit } from 'simple-git';
import fs = require('fs-extra');
import GitIdentity from './GitIdentity';
import { cpuUsage, cwd } from 'process';
import path = require('path');
const tmp = require('tmp');

//Git Abstraction
Expand Down Expand Up @@ -29,7 +31,7 @@ export default class Git {
return this._git.revparse(['HEAD']);
}

async getBaseBranchCommit(baseBranch:string): Promise<string> {
async getBaseBranchCommit(baseBranch: string): Promise<string> {
return this._git.revparse([baseBranch]);
}

Expand Down Expand Up @@ -132,17 +134,35 @@ export default class Git {
let locationOfCopiedDirectory = tmp.dirSync({ unsafeCleanup: true });

SFPLogger.log(`Copying the repository to ${locationOfCopiedDirectory.name}`, LoggerLevel.INFO, logger);
let repoDir = locationOfCopiedDirectory.name;
let tempRepoDir = locationOfCopiedDirectory.name;

// Copy source directory to temp dir
fs.copySync(process.cwd(), repoDir);
// Copy source repository to temp dir

let currentGit = simplegit();

if (!(await currentGit.checkIsRepo())) {
SFPLogger.log(`Unable to initiate repository, currently not in a git repository`, LoggerLevel.ERROR);
throw new Error('Unable to initiate repository, currently not in a git repository');
}

let currentDirParts = process.cwd().split(path.sep);
if (currentDirParts[0] == '') {
currentDirParts[0] = '/';
}
let projectDirParts = [];
while (!(await currentGit.checkIsRepo(CheckRepoActions.IS_REPO_ROOT))) {
projectDirParts.push(currentDirParts.pop());
currentGit = simplegit(path.join(...currentDirParts));
}

fs.copySync(path.join(...currentDirParts), tempRepoDir);

//Initiate git on new repo on using the abstracted object
let git = new Git(repoDir, logger);
let git = new Git(path.join(tempRepoDir, ...projectDirParts), logger);
git._isATemporaryRepo = true;
git.tempRepoLocation = locationOfCopiedDirectory;

await git.addSafeConfig(repoDir);
await git.addSafeConfig(tempRepoDir);
await git.getRemoteOriginUrl();
await git.fetch();
if (branch) {
Expand All @@ -153,7 +173,7 @@ export default class Git {
}

SFPLogger.log(
`Successfully created temporary repository at ${repoDir} with commit ${commitRef ? commitRef : 'HEAD'}`,
`Successfully created temporary repository at ${tempRepoDir} with commit ${commitRef ? commitRef : 'HEAD'}`,
LoggerLevel.INFO,
logger
);
Expand All @@ -172,7 +192,7 @@ export default class Git {

public raw(commands: string[]) {
return this._git.raw(commands);
}
}

public getRepositoryPath() {
return this.repositoryLocation;
Expand All @@ -183,14 +203,12 @@ export default class Git {
}

async addSafeConfig(repoDir: string) {
try
{
//add workaround for safe directory (https://github.com/actions/runner/issues/2033)
await this._git.addConfig('safe.directory', repoDir, false, 'global');
}catch(error)
{
try {
//add workaround for safe directory (https://github.com/actions/runner/issues/2033)
await this._git.addConfig('safe.directory', repoDir, false, 'global');
} catch (error) {
//ignore error
SFPLogger.log(`Unable to set safe.directory`,LoggerLevel.TRACE)
SFPLogger.log(`Unable to set safe.directory`, LoggerLevel.TRACE);
}
}

Expand Down

0 comments on commit 1ae64f8

Please sign in to comment.