"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.rebaseMigrationBranch = void 0;
const global_1 = require("../../../../config/global");
const logger_1 = require("../../../../logger");
const commit_1 = require("../../../../modules/platform/commit");
const git_1 = require("../../../../util/git");
const common_1 = require("../common");
const commit_message_1 = require("./commit-message");
async function rebaseMigrationBranch(config, migratedConfigData) {
    logger_1.logger.debug('Checking if migration branch needs rebasing');
    const branchName = (0, common_1.getMigrationBranchName)(config);
    if (await (0, git_1.isBranchModified)(branchName)) {
        logger_1.logger.debug('Migration branch has been edited and cannot be rebased');
        return null;
    }
    const configFileName = migratedConfigData.filename;
    const contents = migratedConfigData.content;
    const existingContents = await (0, git_1.getFile)(configFileName, branchName);
    if (contents === existingContents) {
        logger_1.logger.debug('Migration branch is up to date');
        return null;
    }
    logger_1.logger.debug('Rebasing migration branch');
    if (global_1.GlobalConfig.get('dryRun')) {
        logger_1.logger.info('DRY-RUN: Would rebase files in migration branch');
        return null;
    }
    const commitMessageFactory = new commit_message_1.ConfigMigrationCommitMessageFactory(config, configFileName);
    const commitMessage = commitMessageFactory.getCommitMessage();
    await (0, git_1.checkoutBranch)(config.defaultBranch);
    return (0, commit_1.commitAndPush)({
        branchName,
        files: [
            {
                type: 'addition',
                path: configFileName,
                contents,
            },
        ],
        message: commitMessage.toString(),
        platformCommit: !!config.platformCommit,
    });
}
exports.rebaseMigrationBranch = rebaseMigrationBranch;
//# sourceMappingURL=rebase.js.map