From f77a6689d7dc27ec1e6e684a934cb1b47c83e887 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 26 Apr 2022 08:34:20 +0000 Subject: [PATCH] fix(ng-dev): make `new-main-branch` command more flexible to local setup After we will perform the default branch rename, there are three scenarios for a contributor: 1. They manually fetch `upstream/main`, run `yarn` and then the migration tool. 2. They will not sync with upstream (as `upstream/master` does not exist), run `yarn` and then the migration tool. 3. They will manually update everything locally and in their forks. This bullet-point is just for completion (not something we bother about here). For (1): The `master` branch and `main` branch will locally exist. We should ask to delete the `main` branch switch back to `master`. Then continue with the full migration (with all other changes to the local git config) For (2): We should run the migration as is (e.g. fetch main and set it up locally). --- ng-dev/misc/new-main-branch/cli.ts | 26 +++++++++++++------ .../{find-local-branch.ts => local-branch.ts} | 5 ++++ .../new-main-branch/remote-fork-update.ts | 2 +- 3 files changed, 24 insertions(+), 9 deletions(-) rename ng-dev/misc/new-main-branch/{find-local-branch.ts => local-branch.ts} (83%) diff --git a/ng-dev/misc/new-main-branch/cli.ts b/ng-dev/misc/new-main-branch/cli.ts index a4db65946..fe2cbb511 100644 --- a/ng-dev/misc/new-main-branch/cli.ts +++ b/ng-dev/misc/new-main-branch/cli.ts @@ -8,7 +8,7 @@ import {assertValidGithubConfig, getConfig} from '../../utils/config'; import {error, green, info, promptConfirm, red, warn, yellow} from '../../utils/console'; -import {findAvailableLocalBranchName, hasLocalBranch} from './find-local-branch'; +import {findAvailableLocalBranchName, getCurrentBranch, hasLocalBranch} from './local-branch'; import {getRemotesForRepo, isAngularOwnedRemote} from './remotes'; import {CommandModule} from 'yargs'; @@ -33,19 +33,29 @@ async function handler() { const config = getConfig([assertValidGithubConfig]); const repoSlug = `${config.github.owner}/${config.github.name}`; - if (config.github.mainBranchName !== 'main') { - error(red('Current project is not part of the default branch renaming.')); - return; - } - if (!hasLocalBranch(git, 'master')) { error(red('Local repository does not have a local branch named `master`. Aborting..')); return; } if (hasLocalBranch(git, 'main')) { - error(red('Local repository already has a branch named `main`. Aborting..')); - return; + console.warn(yellow('The new `main` branch is already fetched locally. In order to run')); + console.warn(yellow('this tool, the `main` branch needs to be non-existent locally.')); + console.warn(''); + console.warn(yellow('The tool will re-fetch the `main` branch and configure it properly.')); + + if (!(await promptConfirm('Do you want to proceed and delete the `main` branch?'))) { + error(red('Aborting..')); + return; + } + + // If we are already on the `main` branch, we cannot delete it without + // checking out a different branch. We switch to `master` in such a case. + if (getCurrentBranch(git) === 'main') { + git.run(['checkout', 'master']); + } + + git.run(['branch', '-D', 'main']); } const remotes = getRemotesForRepo(git); diff --git a/ng-dev/misc/new-main-branch/find-local-branch.ts b/ng-dev/misc/new-main-branch/local-branch.ts similarity index 83% rename from ng-dev/misc/new-main-branch/find-local-branch.ts rename to ng-dev/misc/new-main-branch/local-branch.ts index 97cfd7d1c..f04109745 100644 --- a/ng-dev/misc/new-main-branch/find-local-branch.ts +++ b/ng-dev/misc/new-main-branch/local-branch.ts @@ -25,3 +25,8 @@ export function findAvailableLocalBranchName(git: GitClient, baseName: string): export function hasLocalBranch(git: GitClient, branchName: string): boolean { return git.runGraceful(['rev-parse', `refs/heads/${branchName}`], {stdio: 'ignore'}).status === 0; } + +/** Gets the current branch name. */ +export function getCurrentBranch(git: GitClient): string { + return git.run(['rev-parse', '--abbrev-ref', 'HEAD']).stdout.trim(); +} diff --git a/ng-dev/misc/new-main-branch/remote-fork-update.ts b/ng-dev/misc/new-main-branch/remote-fork-update.ts index a867f7030..b7d1ef0eb 100644 --- a/ng-dev/misc/new-main-branch/remote-fork-update.ts +++ b/ng-dev/misc/new-main-branch/remote-fork-update.ts @@ -23,7 +23,7 @@ import {info} from 'console'; export async function promptForRemoteForkUpdate() { if ( !(await promptConfirm( - 'Do you want to update your fork(s) on Github to also use `main`? (recommended)', + 'Do you also want to update your fork(s) on Github to `main`? (recommended)', )) ) { return;