diff --git a/src/commands/infra/generate.test.ts b/src/commands/infra/generate.test.ts index a5b5b6182..3af590436 100644 --- a/src/commands/infra/generate.test.ts +++ b/src/commands/infra/generate.test.ts @@ -18,7 +18,7 @@ import { getParentGeneratedFolder, gitCheckout, gitClone, - gitFetchPull, + gitPull, retryRemoteValidate, validateDefinition, validateRemoteSource, @@ -130,23 +130,23 @@ describe("test checkRemoteGitExist function", () => { }); }); -const testGitFetchPull = async (positive: boolean): Promise => { +const testGitPull = async (positive: boolean): Promise => { const { safeLoggingUrl, sourcePath } = await getMockedDataForGitTests( positive ); if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) { - await gitFetchPull(sourcePath, safeLoggingUrl); + await gitPull(sourcePath, safeLoggingUrl); } }; -describe("test gitFetchPull function", () => { +describe("test gitPull function", () => { it("postive Test", async () => { - await testGitFetchPull(true); + await testGitPull(true); // no exception thrown }); it("negative Test", async () => { try { - await testGitFetchPull(false); + await testGitPull(false); expect(true).toBe(false); } catch (e) { expect(e).toBeDefined(); @@ -198,7 +198,7 @@ describe("test gitClone function", () => { describe("Validate remote git source", () => { test("Validating that a git source is cloned to .spk/templates", async () => { jest.spyOn(generate, "checkRemoteGitExist").mockResolvedValueOnce(); - jest.spyOn(generate, "gitFetchPull").mockResolvedValueOnce(); + jest.spyOn(generate, "gitPull").mockResolvedValueOnce(); jest.spyOn(generate, "gitCheckout").mockResolvedValueOnce(); const mockParentPath = "src/commands/infra/mocks/discovery-service"; @@ -393,7 +393,7 @@ describe("test retryRemoteValidate function", () => { it("positive test", async () => { jest.spyOn(fsExtra, "removeSync").mockReturnValueOnce(); jest.spyOn(generate, "gitClone").mockResolvedValueOnce(); - jest.spyOn(generate, "gitFetchPull").mockResolvedValueOnce(); + jest.spyOn(generate, "gitPull").mockResolvedValueOnce(); jest.spyOn(generate, "gitCheckout").mockResolvedValueOnce(); await retryRemoteValidate("source", "sourcePath", "safeLoggingUrl", "0.1"); }); diff --git a/src/commands/infra/generate.ts b/src/commands/infra/generate.ts index 059e1a9dc..11d0060df 100644 --- a/src/commands/infra/generate.ts +++ b/src/commands/infra/generate.ts @@ -22,6 +22,7 @@ import { import { copyTfTemplate } from "./scaffold"; import { build as buildError, log as logError } from "../../lib/errorBuilder"; import { errorStatusCode } from "../../lib/errorStatusCode"; +import { exec } from "../../lib/shell"; interface CommandOptions { project: string | undefined; @@ -146,14 +147,22 @@ export const createGenerated = (projectPath: string): void => { logger.info(`Created generated directory: ${projectPath}`); }; -export const gitFetchPull = async ( +export const gitPull = async ( sourcePath: string, safeLoggingUrl: string ): Promise => { // Make sure we have the latest version of all releases cached locally - await simpleGit(sourcePath).fetch("all"); - await simpleGit(sourcePath).pull("origin", "master"); - logger.info(`${safeLoggingUrl} already cloned. Performing 'git pull'...`); + try { + await exec("git", ["symbolic-ref", "HEAD"], { cwd: sourcePath }); + logger.info( + `${safeLoggingUrl} already cloned and a git branch is currently checked out. Performing 'git pull'...` + ); + await simpleGit(sourcePath).pull(); + } catch (err) { + logger.info( + `A git tag is currently checked out. Skipping 'git pull' operation.` + ); + } }; export const gitCheckout = async ( @@ -203,7 +212,7 @@ export const checkRemoteGitExist = async ( }; /** - * Creates "generated" directory if it does not already exists + * Attempts to remove cloned repo in ~/.spk/template directory * * @param source remote URL for cloning to cache * @param sourcePath Path to the template folder cache @@ -222,9 +231,9 @@ export const retryRemoteValidate = async ( createGenerated(sourcePath); const git = simpleGit(); await gitClone(git, source, sourcePath); - await gitFetchPull(sourcePath, safeLoggingUrl); logger.info(`Checking out template version: ${version}`); await gitCheckout(sourcePath, version); + await gitPull(sourcePath, safeLoggingUrl); logger.info(`Successfully re-cloned repo`); }; @@ -263,15 +272,15 @@ export const validateRemoteSource = async ( ); try { // Check if .git folder exists in ${sourcePath}, if not, then clone - // if already cloned, 'git pull' if (fs.existsSync(path.join(sourcePath, ".git"))) { - await gitFetchPull(sourcePath, safeLoggingUrl); + logger.info(`${source} already cloned. Proceeding with 'git checkout'.`); } else { const git = simpleGit(); await gitClone(git, source, sourcePath); } // Checkout tagged version await gitCheckout(sourcePath, version); + await gitPull(sourcePath, safeLoggingUrl); } catch (err) { if (err instanceof Error) { let retry = false;