fix(git): commitFiles cleanup (#11570)

This commit is contained in:
Rhys Arkins 2021-09-06 14:59:55 +02:00 committed by GitHub
parent 3ee70c73cb
commit a16330642b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -705,20 +705,6 @@ export type CommitFilesConfig = {
force?: boolean; force?: boolean;
}; };
async function gitAdd(files: string | string[]): Promise<void> {
try {
await git.add(files);
} catch (err) /* istanbul ignore next */ {
if (
!err.message.includes(
'The following paths are ignored by one of your .gitignore files'
)
) {
throw err;
}
}
}
export async function commitFiles({ export async function commitFiles({
branchName, branchName,
files, files,
@ -738,13 +724,14 @@ export async function commitFiles({
await git.reset(ResetMode.HARD); await git.reset(ResetMode.HARD);
await git.raw(['clean', '-fd']); await git.raw(['clean', '-fd']);
await git.checkout(['-B', branchName, 'origin/' + config.currentBranch]); await git.checkout(['-B', branchName, 'origin/' + config.currentBranch]);
const fileNames: string[] = [];
const deletedFiles: string[] = []; const deletedFiles: string[] = [];
const addedModifiedFiles: string[] = [];
const ignoredFiles: string[] = []; const ignoredFiles: string[] = [];
for (const file of files) { for (const file of files) {
let fileName = file.name;
// istanbul ignore if // istanbul ignore if
if (file.name === '|delete|') { if (fileName === '|delete|') {
const fileName = file.contents as string; fileName = file.contents as string;
try { try {
await git.rm([fileName]); await git.rm([fileName]);
deletedFiles.push(fileName); deletedFiles.push(fileName);
@ -753,11 +740,10 @@ export async function commitFiles({
logger.warn({ err, fileName }, 'Cannot delete file'); logger.warn({ err, fileName }, 'Cannot delete file');
ignoredFiles.push(fileName); ignoredFiles.push(fileName);
} }
} else if (await isDirectory(join(localDir, file.name))) { } else if (await isDirectory(join(localDir, fileName))) {
fileNames.push(file.name); logger.warn({ fileName }, 'Skipping directory commit');
await gitAdd(file.name); ignoredFiles.push(fileName);
} else { } else {
fileNames.push(file.name);
let contents: Buffer; let contents: Buffer;
// istanbul ignore else // istanbul ignore else
if (typeof file.contents === 'string') { if (typeof file.contents === 'string') {
@ -765,11 +751,22 @@ export async function commitFiles({
} else { } else {
contents = file.contents; contents = file.contents;
} }
await fs.outputFile(join(localDir, file.name), contents); await fs.outputFile(join(localDir, fileName), contents);
try {
await git.add(fileName);
addedModifiedFiles.push(fileName);
} catch (err) /* istanbul ignore next */ {
if (
!err.message.includes(
'The following paths are ignored by one of your .gitignore files'
)
) {
throw err;
}
logger.debug({ fileName }, 'Cannot commit ignored file');
ignoredFiles.push(file.name);
} }
} }
if (fileNames.length) {
await gitAdd(fileNames);
} }
const commitOptions: Options = {}; const commitOptions: Options = {};
@ -794,7 +791,7 @@ export async function commitFiles({
const commit = commitRes?.commit || 'unknown'; const commit = commitRes?.commit || 'unknown';
if (!force && !(await hasDiff(`origin/${branchName}`))) { if (!force && !(await hasDiff(`origin/${branchName}`))) {
logger.debug( logger.debug(
{ branchName, fileNames }, { branchName, deletedFiles, addedModifiedFiles, ignoredFiles },
'No file changes detected. Skipping commit' 'No file changes detected. Skipping commit'
); );
return null; return null;