Skip to content

Commit

Permalink
Merge pull request #41 from tiktok/fix-checkout
Browse files Browse the repository at this point in the history
Fix checkout
  • Loading branch information
chengcyber authored Mar 2, 2024
2 parents 1d04be7 + 6055599 commit 4f684f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
37 changes: 20 additions & 17 deletions apps/sparo-lib/src/cli/commands/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
}

const targetProfileNames: Set<string> = new Set();
const currentProfileNames: Set<string> = new Set();
if (!isNoProfile) {
// Get target profile.
// 1. If profile specified from CLI parameter, preferential use it.
Expand All @@ -118,7 +119,10 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
if (profiles.size) {
profiles.forEach((p) => targetProfileNames.add(p));
} else if (localStateProfiles) {
Object.keys(localStateProfiles).forEach((p) => targetProfileNames.add(p));
Object.keys(localStateProfiles).forEach((p) => {
targetProfileNames.add(p);
currentProfileNames.add(p);
});
}

if (addProfiles.size) {
Expand Down Expand Up @@ -180,28 +184,27 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {
// if no profile specified, purge to skeleton
await this._gitSparseCheckoutService.purgeAsync();
} else if (targetProfileNames.size) {
let isCurrentSubsetOfTarget: boolean = true;
for (const currentProfileName of currentProfileNames) {
if (!targetProfileNames.has(currentProfileName)) {
isCurrentSubsetOfTarget = false;
break;
}
}

// In most case, sparo need to reset the sparse checkout cone.
// Only when the current profiles are subset of target profiles, we can skip this step.
if (!isCurrentSubsetOfTarget) {
await this._gitSparseCheckoutService.purgeAsync();
}

// TODO: policy #1: Can not sparse checkout with uncommitted changes in the cone.
for (const profile of profiles) {
for (const profile of targetProfileNames) {
// Since we have run localState.reset() before, for each profile we just add it to local state.
const { selections, includeFolders, excludeFolders } =
await this._gitSparseCheckoutService.resolveSparoProfileAsync(profile, {
localStateUpdateAction: 'add'
});
// for profiles, we use sparse checkout set
await this._gitSparseCheckoutService.checkoutAsync({
selections,
includeFolders,
excludeFolders,
checkoutAction: 'set'
});
}
for (const profile of addProfiles) {
// For each add profile we add it to local state.
const { selections, includeFolders, excludeFolders } =
await this._gitSparseCheckoutService.resolveSparoProfileAsync(profile, {
localStateUpdateAction: 'add'
});
// for add profiles, we use sparse checkout add
await this._gitSparseCheckoutService.checkoutAsync({
selections,
includeFolders,
Expand Down
10 changes: 10 additions & 0 deletions common/changes/sparo/fix-checkout_2024-03-02-04-33.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "sparo",
"comment": "Fix a regression in sparo checkout",
"type": "patch"
}
],
"packageName": "sparo"
}

0 comments on commit 4f684f1

Please sign in to comment.