Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 8b3b77c

Browse files
authored
resolve bug with gitPull between repo tags and repo branches (#521)
1 parent 01c4f28 commit 8b3b77c

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/commands/infra/generate.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
getParentGeneratedFolder,
1919
gitCheckout,
2020
gitClone,
21-
gitFetchPull,
21+
gitPull,
2222
retryRemoteValidate,
2323
validateDefinition,
2424
validateRemoteSource,
@@ -130,23 +130,23 @@ describe("test checkRemoteGitExist function", () => {
130130
});
131131
});
132132

133-
const testGitFetchPull = async (positive: boolean): Promise<void> => {
133+
const testGitPull = async (positive: boolean): Promise<void> => {
134134
const { safeLoggingUrl, sourcePath } = await getMockedDataForGitTests(
135135
positive
136136
);
137137
if (!positive || fs.existsSync(path.join(sourcePath, ".git"))) {
138-
await gitFetchPull(sourcePath, safeLoggingUrl);
138+
await gitPull(sourcePath, safeLoggingUrl);
139139
}
140140
};
141141

142-
describe("test gitFetchPull function", () => {
142+
describe("test gitPull function", () => {
143143
it("postive Test", async () => {
144-
await testGitFetchPull(true);
144+
await testGitPull(true);
145145
// no exception thrown
146146
});
147147
it("negative Test", async () => {
148148
try {
149-
await testGitFetchPull(false);
149+
await testGitPull(false);
150150
expect(true).toBe(false);
151151
} catch (e) {
152152
expect(e).toBeDefined();
@@ -198,7 +198,7 @@ describe("test gitClone function", () => {
198198
describe("Validate remote git source", () => {
199199
test("Validating that a git source is cloned to .spk/templates", async () => {
200200
jest.spyOn(generate, "checkRemoteGitExist").mockResolvedValueOnce();
201-
jest.spyOn(generate, "gitFetchPull").mockResolvedValueOnce();
201+
jest.spyOn(generate, "gitPull").mockResolvedValueOnce();
202202
jest.spyOn(generate, "gitCheckout").mockResolvedValueOnce();
203203

204204
const mockParentPath = "src/commands/infra/mocks/discovery-service";
@@ -393,7 +393,7 @@ describe("test retryRemoteValidate function", () => {
393393
it("positive test", async () => {
394394
jest.spyOn(fsExtra, "removeSync").mockReturnValueOnce();
395395
jest.spyOn(generate, "gitClone").mockResolvedValueOnce();
396-
jest.spyOn(generate, "gitFetchPull").mockResolvedValueOnce();
396+
jest.spyOn(generate, "gitPull").mockResolvedValueOnce();
397397
jest.spyOn(generate, "gitCheckout").mockResolvedValueOnce();
398398
await retryRemoteValidate("source", "sourcePath", "safeLoggingUrl", "0.1");
399399
});

src/commands/infra/generate.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { copyTfTemplate } from "./scaffold";
2323
import { build as buildError, log as logError } from "../../lib/errorBuilder";
2424
import { errorStatusCode } from "../../lib/errorStatusCode";
25+
import { exec } from "../../lib/shell";
2526

2627
interface CommandOptions {
2728
project: string | undefined;
@@ -146,14 +147,22 @@ export const createGenerated = (projectPath: string): void => {
146147
logger.info(`Created generated directory: ${projectPath}`);
147148
};
148149

149-
export const gitFetchPull = async (
150+
export const gitPull = async (
150151
sourcePath: string,
151152
safeLoggingUrl: string
152153
): Promise<void> => {
153154
// Make sure we have the latest version of all releases cached locally
154-
await simpleGit(sourcePath).fetch("all");
155-
await simpleGit(sourcePath).pull("origin", "master");
156-
logger.info(`${safeLoggingUrl} already cloned. Performing 'git pull'...`);
155+
try {
156+
await exec("git", ["symbolic-ref", "HEAD"], { cwd: sourcePath });
157+
logger.info(
158+
`${safeLoggingUrl} already cloned and a git branch is currently checked out. Performing 'git pull'...`
159+
);
160+
await simpleGit(sourcePath).pull();
161+
} catch (err) {
162+
logger.info(
163+
`A git tag is currently checked out. Skipping 'git pull' operation.`
164+
);
165+
}
157166
};
158167

159168
export const gitCheckout = async (
@@ -203,7 +212,7 @@ export const checkRemoteGitExist = async (
203212
};
204213

205214
/**
206-
* Creates "generated" directory if it does not already exists
215+
* Attempts to remove cloned repo in ~/.spk/template directory
207216
*
208217
* @param source remote URL for cloning to cache
209218
* @param sourcePath Path to the template folder cache
@@ -222,9 +231,9 @@ export const retryRemoteValidate = async (
222231
createGenerated(sourcePath);
223232
const git = simpleGit();
224233
await gitClone(git, source, sourcePath);
225-
await gitFetchPull(sourcePath, safeLoggingUrl);
226234
logger.info(`Checking out template version: ${version}`);
227235
await gitCheckout(sourcePath, version);
236+
await gitPull(sourcePath, safeLoggingUrl);
228237
logger.info(`Successfully re-cloned repo`);
229238
};
230239

@@ -263,15 +272,15 @@ export const validateRemoteSource = async (
263272
);
264273
try {
265274
// Check if .git folder exists in ${sourcePath}, if not, then clone
266-
// if already cloned, 'git pull'
267275
if (fs.existsSync(path.join(sourcePath, ".git"))) {
268-
await gitFetchPull(sourcePath, safeLoggingUrl);
276+
logger.info(`${source} already cloned. Proceeding with 'git checkout'.`);
269277
} else {
270278
const git = simpleGit();
271279
await gitClone(git, source, sourcePath);
272280
}
273281
// Checkout tagged version
274282
await gitCheckout(sourcePath, version);
283+
await gitPull(sourcePath, safeLoggingUrl);
275284
} catch (err) {
276285
if (err instanceof Error) {
277286
let retry = false;

0 commit comments

Comments
 (0)