Skip to content

Commit 5c904bb

Browse files
authored
fix: only run git init in new actors if not running from a git repository (#925)
1 parent 8286567 commit 5c904bb

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

src/commands/create.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ export class CreateCommand extends ApifyCommand<typeof CreateCommand> {
314314

315315
// Initialize git repository before reporting success, but store result for later
316316
let gitInitResult: { success: boolean; error?: Error } = { success: true };
317-
if (!skipGitInit) {
317+
const cwdHasGit = await stat(join(cwd, '.git')).catch(() => null);
318+
319+
if (!skipGitInit && !cwdHasGit) {
318320
try {
319321
await execWithLog({
320322
cmd: 'git',
@@ -339,7 +341,7 @@ export class CreateCommand extends ApifyCommand<typeof CreateCommand> {
339341
}
340342

341343
// Report git initialization result after actor creation success
342-
if (!skipGitInit) {
344+
if (!skipGitInit && !cwdHasGit) {
343345
if (gitInitResult.success) {
344346
info({
345347
message: `Git repository initialized in '${actorName}'. You can now commit and push your Actor to Git.`,

test/local/__fixtures__/commands/run/python/works-with-spaces-in-path-to-actor.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async def main():
1616

1717
const { beforeAllCalls, afterAllCalls, joinCwdPath, forceNewCwd } = useTempPath(actorName.replaceAll('-', ' '), {
1818
create: true,
19-
remove: true,
19+
remove: false,
2020
cwd: true,
2121
cwdParent: false,
2222
});
@@ -36,7 +36,7 @@ describe('[python] spaces in path to actor', () => {
3636

3737
await writeFile(joinCwdPath('src', 'main.py'), mainFile);
3838

39-
outputPath = joinCwdPath(getLocalKeyValueStorePath(), 'OUTPUT.txt');
39+
outputPath = joinCwdPath(getLocalKeyValueStorePath(), 'OUTPUT');
4040
});
4141

4242
afterAll(async () => {

test/local/__fixtures__/python_support.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async def main():
7777
await testRunCommand(RunCommand, {});
7878

7979
// Check Actor output
80-
const actorOutputPath = joinPath(getLocalKeyValueStorePath(), 'OUTPUT.json');
80+
const actorOutputPath = joinPath(getLocalKeyValueStorePath(), 'OUTPUT');
8181
const actorOutput = JSON.parse(readFileSync(actorOutputPath, 'utf8'));
8282
expect(actorOutput).toStrictEqual(expectedOutput);
8383
});

test/local/commands/create.test.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { existsSync, readFileSync } from 'node:fs';
2+
import { mkdir, rm } from 'node:fs/promises';
23

34
import { KEY_VALUE_STORE_KEYS } from '@apify/consts';
45

@@ -9,12 +10,13 @@ import { useConsoleSpy } from '../../__setup__/hooks/useConsoleSpy.js';
910
import { useTempPath } from '../../__setup__/hooks/useTempPath.js';
1011

1112
const actName = 'create-my-actor';
12-
const { beforeAllCalls, afterAllCalls, joinPath, toggleCwdBetweenFullAndParentPath, tmpPath } = useTempPath(actName, {
13-
create: true,
14-
remove: true,
15-
cwd: true,
16-
cwdParent: true,
17-
});
13+
const { beforeAllCalls, afterAllCalls, joinPath, joinCwdPath, toggleCwdBetweenFullAndParentPath, tmpPath } =
14+
useTempPath(actName, {
15+
create: true,
16+
remove: true,
17+
cwd: true,
18+
cwdParent: true,
19+
});
1820

1921
const { lastErrorMessage } = useConsoleSpy();
2022

@@ -150,4 +152,26 @@ describe('apify create', () => {
150152
// Check that .git directory does not exist
151153
expect(existsSync(joinPath('.git'))).toBeFalsy();
152154
});
155+
156+
it('should skip git initialization when run from within an existing git repository', async () => {
157+
const ACT_TEMPLATE = 'project_empty';
158+
159+
// Create a .git folder in the parent directory (cwd) to simulate being inside a git repository
160+
await mkdir(joinCwdPath('.git'), { recursive: true });
161+
162+
await testRunCommand(CreateCommand, {
163+
args_actorName: actName,
164+
flags_template: ACT_TEMPLATE,
165+
flags_skipDependencyInstall: true,
166+
});
167+
168+
toggleCwdBetweenFullAndParentPath();
169+
170+
// Check that .git directory does not exist in the newly created actor directory
171+
expect(existsSync(joinCwdPath('.git'))).toBeFalsy();
172+
173+
// Cleanup
174+
toggleCwdBetweenFullAndParentPath();
175+
await rm(joinCwdPath('.git'), { recursive: true });
176+
});
153177
});

0 commit comments

Comments
 (0)