Skip to content

Commit

Permalink
fix(git): commitFiles cleanup (#11570)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Sep 6, 2021
1 parent 3ee70c7 commit a163306
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions lib/util/git/index.ts
Expand Up @@ -705,20 +705,6 @@ export type CommitFilesConfig = {
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({
branchName,
files,
Expand All @@ -738,13 +724,14 @@ export async function commitFiles({
await git.reset(ResetMode.HARD);
await git.raw(['clean', '-fd']);
await git.checkout(['-B', branchName, 'origin/' + config.currentBranch]);
const fileNames: string[] = [];
const deletedFiles: string[] = [];
const addedModifiedFiles: string[] = [];
const ignoredFiles: string[] = [];
for (const file of files) {
let fileName = file.name;
// istanbul ignore if
if (file.name === '|delete|') {
const fileName = file.contents as string;
if (fileName === '|delete|') {
fileName = file.contents as string;
try {
await git.rm([fileName]);
deletedFiles.push(fileName);
Expand All @@ -753,24 +740,34 @@ export async function commitFiles({
logger.warn({ err, fileName }, 'Cannot delete file');
ignoredFiles.push(fileName);
}
} else if (await isDirectory(join(localDir, file.name))) {
fileNames.push(file.name);
await gitAdd(file.name);
} else if (await isDirectory(join(localDir, fileName))) {
logger.warn({ fileName }, 'Skipping directory commit');
ignoredFiles.push(fileName);
} else {
fileNames.push(file.name);
let contents: Buffer;
// istanbul ignore else
if (typeof file.contents === 'string') {
contents = Buffer.from(file.contents);
} else {
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 = {};
if (getNoVerify().includes(GitNoVerifyOption.Commit)) {
Expand All @@ -794,7 +791,7 @@ export async function commitFiles({
const commit = commitRes?.commit || 'unknown';
if (!force && !(await hasDiff(`origin/${branchName}`))) {
logger.debug(
{ branchName, fileNames },
{ branchName, deletedFiles, addedModifiedFiles, ignoredFiles },
'No file changes detected. Skipping commit'
);
return null;
Expand Down

0 comments on commit a163306

Please sign in to comment.