From 1ae64f86d6df4a54351c700023f7ac1b6b04a850 Mon Sep 17 00:00:00 2001 From: Gobius Dolhain <gobius@sparklink.be> Date: Mon, 27 May 2024 16:15:29 +0200 Subject: [PATCH] fix: possible fix for nested projects #64 --- packages/sfp-cli/src/core/git/Git.ts | 50 +++++++++++++++++++--------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/packages/sfp-cli/src/core/git/Git.ts b/packages/sfp-cli/src/core/git/Git.ts index 95c58b29a..e6af8d95c 100644 --- a/packages/sfp-cli/src/core/git/Git.ts +++ b/packages/sfp-cli/src/core/git/Git.ts @@ -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 @@ -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]); } @@ -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) { @@ -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 ); @@ -172,7 +192,7 @@ export default class Git { public raw(commands: string[]) { return this._git.raw(commands); - } + } public getRepositoryPath() { return this.repositoryLocation; @@ -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); } }