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

refactor(git): Split commitFiles function into 3 phases #13821

Merged
merged 8 commits into from Jan 26, 2022
Merged
Show file tree
Hide file tree
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
61 changes: 53 additions & 8 deletions lib/util/git/index.ts
Expand Up @@ -31,6 +31,7 @@ import { checkForPlatformFailure, handleCommitError } from './error';
import { configSigningKey, writePrivateKey } from './private-key';
import type {
CommitFilesConfig,
CommitResult,
CommitSha,
LocalConfig,
StatusResult,
Expand Down Expand Up @@ -696,15 +697,15 @@ async function handleCommitAuth(localDir: string): Promise<void> {
await writeGitAuthor();
}

export async function commitFiles({
export async function prepareCommit({
branchName,
files,
message,
force = false,
}: CommitFilesConfig): Promise<CommitSha | null> {
}: CommitFilesConfig): Promise<CommitResult | null> {
const { localDir } = GlobalConfig.get();
await syncGit();
logger.debug(`Committing files to branch ${branchName}`);
logger.debug(`Preparing files for commiting to branch ${branchName}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect one more 't' in the last verb. Also see https://www.spellchecker.net/misspellings/commiting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks. I'll fix it with an upcoming PR

await handleCommitAuth(localDir);
try {
await git.reset(ResetMode.HARD);
Expand Down Expand Up @@ -796,6 +797,29 @@ export async function commitFiles({
return null;
}

const result: CommitResult = {
sha: commit,
files: files.filter((fileChange) => {
if (fileChange.type === 'deletion') {
return deletedFiles.includes(fileChange.path);
}
return addedModifiedFiles.includes(fileChange.path);
}),
};

return result;
} catch (err) /* istanbul ignore next */ {
return handleCommitError(files, branchName, err);
}
}

export async function pushCommit({
branchName,
files,
}: CommitFilesConfig): Promise<void> {
await syncGit();
logger.debug(`Pushing branch ${branchName}`);
try {
const pushOptions: TaskOptions = {
'--force-with-lease': null,
'-u': null,
Expand All @@ -811,20 +835,41 @@ export async function commitFiles({
);
delete pushRes.repo;
logger.debug({ result: pushRes }, 'git push');
// Fetch it after create
incLimitedValue(Limit.Commits);
} catch (err) /* istanbul ignore next */ {
handleCommitError(files, branchName, err);
}
}

export async function fetchCommit({
branchName,
files,
}: CommitFilesConfig): Promise<CommitSha | null> {
await syncGit();
logger.debug(`Fetching branch ${branchName}`);
try {
const ref = `refs/heads/${branchName}:refs/remotes/origin/${branchName}`;
await git.fetch(['origin', ref, '--force']);
config.branchCommits[branchName] = (
await git.revparse([branchName])
).trim();
const commit = (await git.revparse([branchName])).trim();
config.branchCommits[branchName] = commit;
config.branchIsModified[branchName] = false;
incLimitedValue(Limit.Commits);
return commit;
} catch (err) /* istanbul ignore next */ {
return handleCommitError(files, branchName, err);
}
}

export async function commitFiles(
config: CommitFilesConfig
): Promise<CommitSha | null> {
const commitResult = await prepareCommit(config);
if (!commitResult) {
return null;
}
await pushCommit(config);
return fetchCommit(config);
}

export function getUrl({
protocol,
auth,
Expand Down
5 changes: 5 additions & 0 deletions lib/util/git/types.ts
Expand Up @@ -76,3 +76,8 @@ export interface CommitFilesConfig {
message: string;
force?: boolean;
}

export interface CommitResult {
sha: string;
files: FileChange[];
}