Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(git): commitFiles cleanup #11570

Merged
merged 1 commit into from Sep 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
viceice marked this conversation as resolved.
Show resolved Hide resolved
} 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