Skip to content

Commit fc2222a

Browse files
committed
Ignore files can not be added
1 parent 9f79e47 commit fc2222a

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

src/execute-command.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export async function executeCommand(commandString: string) {
3434
alternative: alternativePrettier,
3535
original: originalPrettier,
3636
});
37+
} catch (error) {
38+
console.error(error);
39+
throw error;
3740
} finally {
3841
finished++;
3942
logger.brief(

src/prepare-repository.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ async function preparePrettierIgnoreFile(
3030

3131
const prettierIgnoreFile = path.join(directory, ".prettierignore");
3232
await writeFile(prettierIgnoreFile, content);
33-
await spawn("git", ["add", "."], { cwd: directory });
3433
}
3534

3635
export async function prepareRepository(
@@ -39,9 +38,11 @@ export async function prepareRepository(
3938
) {
4039
await cloneRepository(repository);
4140
await fs.cp(repository.directory, directory, { recursive: true });
41+
await fs.rm(path.join(directory, ".git"), { recursive: true, force: true });
4242
await preparePrettierIgnoreFile(directory, repository);
43+
await spawn("git", ["init"], { cwd: directory });
4344

44-
const commitHash = await commitChanges(directory, "Prepare");
45+
const commitHash = await commitChanges(directory, "Initialize", true);
4546

4647
return {
4748
async reset() {

src/utilities.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os from "node:os";
33
import path from "node:path";
44
import crypto from "node:crypto";
55
import { temporaryDirectory } from "./constants.ts";
6-
import spawn from "nano-spawn";
6+
import spawn, { SubprocessError } from "nano-spawn";
77
import assert from "node:assert/strict";
88

99
export async function createTemporaryDirectory() {
@@ -42,21 +42,51 @@ export async function readFile(file: string) {
4242

4343
export const unique = <T>(array: T[]): T[] => [...new Set(array)];
4444

45-
export const commitChanges = async (directory: string, message: string) => {
46-
await spawn("git", ["add", "."], { cwd: directory });
47-
const { stdout } = await spawn(
45+
async function gitAddFiles(cwd: string) {
46+
try {
47+
await spawn("git", ["add", "."], { cwd });
48+
return;
49+
} catch (error) {
50+
if (error instanceof SubprocessError) {
51+
const { stderr } = error;
52+
const match = stderr.match(
53+
/error: open\("(?<filename>.*)"\): Filename too long/,
54+
);
55+
const filename = match?.groups?.filename;
56+
if (filename) {
57+
console.log(`File '${filename}' can't be added, ignored.`);
58+
await fs.rm(path.join(cwd, filename), { force: true });
59+
return gitAddFiles(cwd);
60+
}
61+
}
62+
63+
throw error;
64+
}
65+
}
66+
67+
export const commitChanges = async (
68+
directory: string,
69+
message: string,
70+
ignoreFilesCannotAdded?: boolean,
71+
) => {
72+
await fs.rm(path.join(directory, ".gitattributes"), { force: true });
73+
await spawn("git", ["config", "set", "core.autocrlf", "false"], {
74+
cwd: directory,
75+
});
76+
77+
if (ignoreFilesCannotAdded) {
78+
await gitAddFiles(directory);
79+
} else {
80+
await spawn("git", ["add", "."], { cwd: directory });
81+
}
82+
83+
await spawn(
4884
"git",
4985
["commit", "--allow-empty", "--no-verify", "-m", message],
5086
{ cwd: directory },
5187
);
5288

53-
const match = stdout.match(/^\[(?<commitHash>[a-f0-9]{40}) [a-f0-9]{7,8}] /);
54-
if (!match?.groups!.commitHash) {
55-
throw new Error(`Unexpected commit hash '${stdout}'`);
56-
}
57-
58-
const commitHash = match.groups.commitHash;
59-
return commitHash;
89+
return getCommitHash(directory);
6090
};
6191

6292
export async function getCommitHash(directory: string) {

0 commit comments

Comments
 (0)