Skip to content

Commit

Permalink
Add MergeOptions to regenerate scenario (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpajdzik authored Nov 9, 2018
1 parent 2ff8ddb commit 5ede64a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 11 additions & 9 deletions .scripts/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* license information.
*/

import { Cred, Index, Merge, Oid, Reference, Repository, Reset, Signature, StatusFile } from "nodegit";
import { Cred, Index, Merge, Oid, Reference, Repository, Reset, Signature, StatusFile, Branch as NodeBranch, MergeOptions } from "nodegit";
import { Logger } from "./logger";

export type ValidateFunction = (statuses: StatusFile[]) => boolean;
Expand Down Expand Up @@ -141,27 +141,27 @@ export async function getValidatedRepository(repositoryPath: string): Promise<Re
return repository;
}

export async function mergeBranch(repository: Repository, toBranch: Branch, fromBranch: Branch): Promise<Oid> {
export async function mergeBranch(repository: Repository, toBranch: Branch, fromBranch: Branch, mergeOptions?: MergeOptions): Promise<Oid> {
_logger.logTrace(`Merging "${fromBranch.fullName()}" to "${toBranch.fullName()}" branch in ${repository.path()} repository`);
try {
return repository.mergeBranches(toBranch.name, fromBranch.shorthand(), Signature.default(repository), Merge.PREFERENCE.NONE);
return repository.mergeBranches(toBranch.name, fromBranch.shorthand(), Signature.default(repository), Merge.PREFERENCE.NONE, mergeOptions);
} catch (error) {
throw new Error(`Probable merge conflicts. Error: ${error}`);
}
}

export async function mergeMasterIntoBranch(repository: Repository, toBranch: Branch): Promise<Oid> {
return mergeBranch(repository, toBranch, Branch.RemoteMaster);
export async function mergeMasterIntoBranch(repository: Repository, toBranch: Branch, mergeOptions?: MergeOptions): Promise<Oid> {
return mergeBranch(repository, toBranch, Branch.RemoteMaster, mergeOptions);
}

export async function pullBranch(repository: Repository, localBranch: Branch): Promise<void> {
export async function pullBranch(repository: Repository, localBranch: Branch, mergeOptions?: MergeOptions): Promise<void> {
_logger.logTrace(`Pulling "${localBranch.fullName()}" branch in ${repository.path()} repository`);

await repository.fetchAll();
_logger.logTrace(`Fetched all successfully`);

const remoteBranch = new Branch(localBranch.name, BranchLocation.Remote, localBranch.remote);
await mergeBranch(repository, localBranch, remoteBranch);
await mergeBranch(repository, localBranch, remoteBranch, mergeOptions);

const index = await repository.index();
if (index.hasConflicts()) {
Expand Down Expand Up @@ -202,10 +202,11 @@ export async function checkoutRemoteBranch(repository: Repository, remoteBranch:
if (branchExists) {
branchRef = await checkoutBranch(repository, remoteBranch.name);
} else {
branchRef = await createNewBranch(repository, remoteBranch.name, true);
branchRef = await createNewBranch(repository, remoteBranch.name);
await NodeBranch.setUpstream(branchRef, remoteBranch.shorthand());
const commit = await repository.getReferenceCommit(remoteBranch.name);
await checkoutBranch(repository, branchRef);
await Reset.reset(repository, commit as any, Reset.TYPE.HARD, {});
await pullBranch(repository, remoteBranch.toLocal());
}

return branchRef;
Expand Down Expand Up @@ -239,6 +240,7 @@ export async function checkoutMaster(repository: Repository): Promise<Reference>
}

export async function refreshRepository(repository: Repository) {
await repository.fetchAll();
await pullMaster(repository);
return checkoutMaster(repository);
}
Expand Down
8 changes: 6 additions & 2 deletions .scripts/gulp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import fs from "fs";
import * as path from "path";
import { SdkType } from "./commandLine";
import { contains, npmInstall } from "./common";
import { Branch, BranchLocation, checkoutRemoteBranch, commitAndPush, getValidatedRepository, mergeMasterIntoBranch, refreshRepository, unlockGitRepository, ValidateFunction, waitAndLockGitRepository } from "./git";
import { Branch, BranchLocation, checkoutRemoteBranch, commitAndPush, getValidatedRepository, mergeMasterIntoBranch, refreshRepository, unlockGitRepository, ValidateFunction, waitAndLockGitRepository, checkoutBranch } from "./git";
import { commitAndCreatePullRequest, findPullRequest, forcePrDiffRefresh, requestPullRequestReview } from "./github";
import { Logger } from "./logger";
import { findMissingSdks, findSdkDirectory, saveContentToFile } from "./packages";
import { copyExistingNodeJsReadme, findReadmeTypeScriptMdFilePaths, getAbsolutePackageFolderPathFromReadmeFileContents, getPackageNamesFromReadmeTypeScriptMdFileContents, getSinglePackageName, updateMainReadmeFile, updateTypeScriptReadmeFile } from "./readme";
import { Version } from "./version";
import { Merge } from 'nodegit';

const _logger = Logger.get();

Expand Down Expand Up @@ -182,6 +183,7 @@ export async function generateAllMissingSdks(azureSdkForJsRepoPath: string, azur

export async function regenerate(branchName: string, packageName: string, azureSdkForJsRepoPath: string, azureRestAPISpecsPath: string, pullRequestId: number, skipVersionBump?: boolean, requestReview?: boolean) {
const azureSdkForJsRepository = await getValidatedRepository(azureSdkForJsRepoPath);
const currentBranch = await azureSdkForJsRepository.getCurrentBranch();
await refreshRepository(azureSdkForJsRepository);
_logger.log(`Refreshed ${azureSdkForJsRepository.path()} repository successfully`);

Expand All @@ -190,7 +192,7 @@ export async function regenerate(branchName: string, packageName: string, azureS
_logger.log(`Checked out ${branchName} branch`);

const localBranch = remoteBranch.convertTo(BranchLocation.Local);
await mergeMasterIntoBranch(azureSdkForJsRepository, localBranch);
await mergeMasterIntoBranch(azureSdkForJsRepository, localBranch, { fileFavor: Merge.FILE_FAVOR.THEIRS });
_logger.log(`Merged master into ${localBranch.shorthand()} successfully`);

if (skipVersionBump) {
Expand Down Expand Up @@ -225,6 +227,8 @@ export async function regenerate(branchName: string, packageName: string, azureS
} else {
_logger.log("Skipping review requesting");
}

await checkoutBranch(azureSdkForJsRepository, currentBranch);
}

async function bumpMinorVersion(azureSdkForJsRepoPath: string, packageName: string) {
Expand Down

0 comments on commit 5ede64a

Please sign in to comment.