Skip to content

Commit 2168877

Browse files
devversionjosephperrott
authored andcommitted
feat(ng-dev): add safety checks for shallow repositories
Adds safety checks for shallow repositories when executing the merge or release publish script. These two commands have similar checks on the local repository and these tools act on the local repository as part of a common development workflow where an error beforehand would be more useful.
1 parent 284cb3d commit 2168877

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

github-actions/slash-commands/main.js

Lines changed: 4 additions & 1 deletion
Large diffs are not rendered by default.

ng-dev/pr/merge/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ export async function mergePullRequest(prNumber: number, flags: PullRequestMerge
9393
),
9494
);
9595
return false;
96+
case MergeStatus.UNEXPECTED_SHALLOW_REPO:
97+
error(red(`Unable to perform merge in a local repository that is configured as shallow.`));
98+
error(red(`Please convert the repository to a complete one by syncing with upstream.`));
99+
error(red(`https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---unshallow`));
100+
return false;
96101
case MergeStatus.UNKNOWN_GIT_ERROR:
97102
error(
98103
red(

ng-dev/pr/merge/task.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {AutosquashMergeStrategy} from './strategies/autosquash-merge';
2424
export const enum MergeStatus {
2525
UNKNOWN_GIT_ERROR,
2626
DIRTY_WORKING_DIR,
27+
UNEXPECTED_SHALLOW_REPO,
2728
SUCCESS,
2829
FAILED,
2930
USER_ABORTED,
@@ -69,6 +70,14 @@ export class PullRequestMergeTask {
6970
* @param force Whether non-critical pull request failures should be ignored.
7071
*/
7172
async merge(prNumber: number, force = false): Promise<MergeResult> {
73+
if (this.git.hasUncommittedChanges()) {
74+
return {status: MergeStatus.DIRTY_WORKING_DIR};
75+
}
76+
77+
if (this.git.isShallowRepo()) {
78+
return {status: MergeStatus.UNEXPECTED_SHALLOW_REPO};
79+
}
80+
7281
// Check whether the given Github token has sufficient permissions for writing
7382
// to the configured repository. If the repository is not private, only the
7483
// reduced `public_repo` OAuth scope is sufficient for performing merges.
@@ -98,10 +107,6 @@ export class PullRequestMergeTask {
98107
};
99108
}
100109

101-
if (this.git.hasUncommittedChanges()) {
102-
return {status: MergeStatus.DIRTY_WORKING_DIR};
103-
}
104-
105110
const pullRequest = await loadAndValidatePullRequest(this, prNumber, force);
106111

107112
if (!isPullRequest(pullRequest)) {

ng-dev/release/publish/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export class ReleaseTool {
5151

5252
if (
5353
!(await this._verifyNoUncommittedChanges()) ||
54-
!(await this._verifyRunningFromNextBranch(nextBranchName))
54+
!(await this._verifyRunningFromNextBranch(nextBranchName)) ||
55+
!(await this._verifyNoShallowRepository())
5556
) {
5657
return CompletionState.FATAL_ERROR;
5758
}
@@ -137,6 +138,22 @@ export class ReleaseTool {
137138
return true;
138139
}
139140

141+
/**
142+
* Verifies that the local repository is not configured as shallow.
143+
* @returns a boolean indicating success or failure.
144+
*/
145+
private async _verifyNoShallowRepository(): Promise<boolean> {
146+
if (this._git.isShallowRepo()) {
147+
error(red(' ✘ The local repository is configured as shallow.'));
148+
error(red(` Please convert the repository to a complete one by syncing with upstream.`));
149+
error(
150+
red(` https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---unshallow`),
151+
);
152+
return false;
153+
}
154+
return true;
155+
}
156+
140157
/**
141158
* Verifies that the next branch from the configured repository is checked out.
142159
* @returns a boolean indicating success or failure.

ng-dev/utils/git/git-client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ export class GitClient {
137137
return this.run(['branch', branchName, '--contains', sha]).stdout !== '';
138138
}
139139

140+
/** Whether the local repository is configured as shallow. */
141+
isShallowRepo(): boolean {
142+
return this.run(['rev-parse', '--is-shallow-repository']).stdout.trim() === 'true';
143+
}
144+
140145
/** Gets the currently checked out branch or revision. */
141146
getCurrentBranchOrRevision(): string {
142147
const branchName = this.run(['rev-parse', '--abbrev-ref', 'HEAD']).stdout.trim();

tools/local-actions/changelog/main.js

Lines changed: 4 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)