Skip to content

Commit

Permalink
fix(ng-dev): make new-main-branch command more flexible to local setup
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
devversion committed Apr 27, 2022
1 parent 6ea40a1 commit f77a668
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
26 changes: 18 additions & 8 deletions ng-dev/misc/new-main-branch/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion ng-dev/misc/new-main-branch/remote-fork-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f77a668

Please sign in to comment.